From 9898020267a960b4ceb8503125574edcae65c865 Mon Sep 17 00:00:00 2001 From: Aurora Date: Mon, 14 Nov 2016 17:27:10 +0100 Subject: [PATCH] Add delays when using the MCU, move screen init routines after OTPless install completes --- source/installer.c | 18 ++++++++-------- source/screen.c | 3 +++ source/utils.c | 51 ++++++++++++++++++++++++++++++++++------------ source/utils.h | 5 +++++ 4 files changed, 56 insertions(+), 21 deletions(-) diff --git a/source/installer.c b/source/installer.c index 4e027b7..8e5e82c 100755 --- a/source/installer.c +++ b/source/installer.c @@ -64,14 +64,19 @@ static const u8 sectorHashRetail[SHA_256_HASH_SIZE] = { u32 posY; -void main(void) +static void drawTitle(void) { - bool isOtpless = ISA9LH && magic == 0xDEADCAFE; - initScreens(); posY = drawString(TITLE, 10, 10, COLOR_TITLE); posY = drawString("Thanks to delebile, #cakey and StandardBus", 10, posY + SPACING_Y, COLOR_WHITE); +} + +void main(void) +{ + bool isOtpless = ISA9LH && magic == 0xDEADCAFE; + + if(!isOtpless) drawTitle(); if(!sdmmc_sdcard_init(isOtpless)) shutdown(1, "Error: failed to initialize SD and NAND"); @@ -84,11 +89,6 @@ void main(void) posY = drawString("Press any other button to shutdown", 10, posY, COLOR_WHITE); pressed = waitInput(); } - else - { - posY = drawString("Finalizing install...", 10, posY + SPACING_Y, COLOR_WHITE); - pressed = 0; - } if(isOtpless || pressed == BUTTON_SELECT) installer(isOtpless); if(pressed == BUTTON_START && ISA9LH) uninstaller(); @@ -289,6 +289,8 @@ static inline void installer(bool isOtpless) writeFirm((u8 *)FIRM0_OFFSET, false, FIRM0_SIZE); + if(isOtpless) drawTitle(); + shutdown(2, ISA9LH && !isOtpless ? "Update: success!" : "Full install: success!"); } diff --git a/source/screen.c b/source/screen.c index b2467ab..e1374ab 100644 --- a/source/screen.c +++ b/source/screen.c @@ -36,6 +36,7 @@ #include "screen.h" #include "cache.h" +#include "utils.h" #include "i2c.h" vu32 *arm11Entry = (vu32 *)BRAHMA_ARM11_ENTRY; @@ -183,6 +184,8 @@ void initScreens(void) { invokeArm11Function(initSequence); + wait(3ULL); + //Turn on backlight i2cWriteRegister(I2C_DEV_MCU, 0x22, 0x2A); } diff --git a/source/utils.c b/source/utils.c index b9c6292..1813cd7 100755 --- a/source/utils.c +++ b/source/utils.c @@ -8,33 +8,56 @@ #include "cache.h" #include "i2c.h" +static void startChrono(void) +{ + REG_TIMER_CNT(0) = 0; //67MHz + for(u32 i = 1; i < 4; i++) REG_TIMER_CNT(i) = 4; //Count-up + + for(u32 i = 0; i < 4; i++) REG_TIMER_VAL(i) = 0; + + REG_TIMER_CNT(0) = 0x80; //67MHz; enabled + for(u32 i = 1; i < 4; i++) REG_TIMER_CNT(i) = 0x84; //Count-up; enabled +} + +static u64 chrono(void) +{ + u64 res; + for(u32 i = 0; i < 4; i++) res |= REG_TIMER_VAL(i) << (16 * i); + + res /= (TICKS_PER_SEC / 1000); + + return res; +} + u32 waitInput(void) { bool pressedKey = false; - u32 key; + u32 key, + oldKey = HID_PAD; - //Wait for no keys to be pressed - while(HID_PAD); - - do + while(!pressedKey) { - //Wait for a key to be pressed - while(!HID_PAD); - key = HID_PAD; - //Make sure it's pressed - for(u32 i = 0x13000; i > 0; i--) + if(!key) oldKey = key; + else if(key != oldKey) { - if(key != HID_PAD) break; - if(i == 1) pressedKey = true; + //Make sure the key is pressed + u32 i; + for(i = 0; i < 0x13000 && key == HID_PAD; i++); + if(i == 0x13000) pressedKey = true; } } - while(!pressedKey); return key; } +void wait(u64 amount) +{ + startChrono(); + while(chrono() < amount); +} + void mcuReboot(void) { clearScreens(); @@ -42,6 +65,8 @@ void mcuReboot(void) //Ensure that all memory transfers have completed and that the data cache has been flushed flushEntireDCache(); + wait(3ULL); + i2cWriteRegister(I2C_DEV_MCU, 0x20, 1 << 2); while(true); } diff --git a/source/utils.h b/source/utils.h index 7eb7ed3..cfec731 100644 --- a/source/utils.h +++ b/source/utils.h @@ -21,9 +21,14 @@ #define COLOR_RED 0x0000FF #define COLOR_GREEN 0x00FF00 +#define TICKS_PER_SEC 67027964ULL +#define REG_TIMER_CNT(i) *(vu16 *)(0x10003002 + 4 * i) +#define REG_TIMER_VAL(i) *(vu16 *)(0x10003000 + 4 * i) + extern u32 posY; u32 waitInput(void); +void wait(u64 amount); void mcuReboot(void); void inputSequence(void); void shutdown(u32 mode, const char *message); \ No newline at end of file