From 040ddd6db31cc5a06f353c8e0e427c95492e1c97 Mon Sep 17 00:00:00 2001 From: Pk11 Date: Sun, 9 Jan 2022 21:36:18 -0600 Subject: [PATCH] Fix FAT mismatch if exited by holding POWER --- arm7/src/main.c | 34 +++++++++++++++++++++++++++------- arm9/src/main.c | 9 +++++++-- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/arm7/src/main.c b/arm7/src/main.c index ad4fa78..68c3043 100644 --- a/arm7/src/main.c +++ b/arm7/src/main.c @@ -27,11 +27,12 @@ distribution. ---------------------------------------------------------------------------------*/ -#include +#include "my_sdmmc.h" + //#include //#include -#include "string.h" -#include "my_sdmmc.h" +#include +#include //--------------------------------------------------------------------------------- void VblankHandler(void) { @@ -47,11 +48,24 @@ void VcountHandler() { } volatile bool exitflag = false; +volatile bool reboot = false; +// https://github.com/devkitPro/libnds/blob/154a21cc3d57716f773ff2b10f815511c1b8ba9f/source/common/interrupts.c#L51-L69 //--------------------------------------------------------------------------------- -void powerButtonCB() { +TWL_CODE void i2cIRQHandlerCustom() { //--------------------------------------------------------------------------------- - exitflag = true; + int cause = (i2cReadRegister(I2C_PM, I2CREGPM_PWRIF) & 0x3) | (i2cReadRegister(I2C_GPIO, 0x02)<<2); + + switch (cause & 3) { + case 1: + reboot = true; + exitflag = true; + break; + case 2: + reboot = false; + exitflag = true; + break; + } } void set_ctr(u32* ctr){ @@ -96,6 +110,7 @@ int main() { ledBlink(0); irqInit(); + irqSetAUX(IRQ_I2C, i2cIRQHandlerCustom); // Start the RTC tracking IRQ initClockIRQ(); fifoInit(); @@ -159,8 +174,6 @@ int main() { irqEnable( IRQ_VBLANK | IRQ_VCOUNT | IRQ_NETWORK); - setPowerButtonCB(powerButtonCB); - // Keep the ARM7 mostly idle while (!exitflag) { if ( 0 == (REG_KEYINPUT & (KEY_SELECT | KEY_START | KEY_L | KEY_R))) { @@ -174,5 +187,12 @@ int main() { fifoWaitValue32(FIFO_USER_02); fifoCheckValue32(FIFO_USER_02); + if (reboot) { + i2cWriteRegister(I2C_PM, I2CREGPM_RESETFLAG, 1); + i2cWriteRegister(I2C_PM, I2CREGPM_PWRCNT, 1); + } else { + writePowerManagement(PM_CONTROL_REG,PM_SYSTEM_PWR); + } + return 0; } diff --git a/arm9/src/main.c b/arm9/src/main.c index 1eade61..3687c3d 100644 --- a/arm9/src/main.c +++ b/arm9/src/main.c @@ -8,6 +8,7 @@ bool programEnd = false; bool sdnandMode = true; +bool arm7Exiting = false; PrintConsole topScreen; PrintConsole bottomScreen; @@ -51,7 +52,6 @@ static int _mainMenu(int cursor) iprintf("\tTitle Manager for HiyaCFW\n"); iprintf("\nversion %s\n", VERSION); iprintf("\n\n\x1B[41mWARNING:\x1B[47m This tool can write to\nyour internal NAND!\n\nThis always has a risk, albeit\nlow, of \x1B[41mbricking\x1B[47m your system\nand should be done with caution!\n"); - iprintf("\nDo not exit by holding POWER,\ntap it or choose \"Shut Down\".\n"); iprintf("\x1b[22;0HJeff - 2018-2019"); iprintf("\x1b[23;0HPk11 - 2022"); @@ -95,7 +95,10 @@ static int _mainMenu(int cursor) void fifoHandler(u32 value32, void* userdata) { if (value32 == 0x54495845) // 'EXIT' + { programEnd = true; + arm7Exiting = true; + } } int main(int argc, char **argv) @@ -138,7 +141,6 @@ int main(int argc, char **argv) } messageBox("\x1B[41mWARNING:\x1B[47m This tool can write to\nyour internal NAND!\n\nThis always has a risk, albeit\nlow, of \x1B[41mbricking\x1B[47m your system\nand should be done with caution!"); - messageBox("Do not exit by holding POWER,\ntap it or choose \"Shut Down\"."); //main menu int cursor = 0; @@ -194,6 +196,9 @@ int main(int argc, char **argv) fifoSendValue32(FIFO_USER_02, 0x54495845); // 'EXIT' + while (arm7Exiting) + swiWaitForVBlank(); + return 0; }