diff --git a/source/arm9/dev.c b/source/arm9/dev.c index 27342b4..59ea52c 100644 --- a/source/arm9/dev.c +++ b/source/arm9/dev.c @@ -25,6 +25,8 @@ #include "arm9/ncsd.h" #include "arm9/hardware/sdmmc.h" #include "arm9/hardware/spiflash.h" +#include "arm9/hardware/interrupt.h" +#include "fs.h" #include "arm9/hardware/crypto.h" #include "hardware/cache.h" #include "arm9/hardware/timer.h" @@ -131,6 +133,8 @@ const dev_struct *dev_flash = &dev_wififlash; +static void sdioHandler(UNUSED u32 id); + // -------------------------------- sd card glue functions -------------------------------- bool sdmmc_sd_init(void) { @@ -159,11 +163,26 @@ if(SD_Init()) return false; dev_sd.initialized = true; + IRQ_registerHandler(IRQ_SDIO_1, sdioHandler); } return true; } +static void sdioHandler(UNUSED u32 id) +{ + // Hacky way to detect SD pulls. We need a proper MMC driver. + if(!(sdmmc_read16(REG_SDSTATUS0) & TMIO_STAT0_SIGSTATE)) + { + const u32 oldState = enterCriticalSection(); + + dev_sd.initialized = false; + fUnmount(FS_DRIVE_SDMC); + + leaveCriticalSection(oldState); + } +} + bool sdmmc_sd_read_sector(u32 sector, u32 count, void *buf) { if(!dev_sd.initialized) return false;