From 80e96f5815c9c17a22e28d9ad4814d5e870a1a1e Mon Sep 17 00:00:00 2001 From: d0k3 Date: Wed, 16 Mar 2016 15:45:24 +0100 Subject: [PATCH] Actually check if SD card is inserted when mounting --- source/fatfs/diskio.c | 3 ++- source/fs.c | 2 ++ source/godmode.c | 12 ++++++------ source/{fatfs => nand}/delay.s | 0 source/nand/sdmmc.c | 12 +++++++++--- source/nand/sdmmc.h | 2 +- 6 files changed, 20 insertions(+), 11 deletions(-) rename source/{fatfs => nand}/delay.s (100%) diff --git a/source/fatfs/diskio.c b/source/fatfs/diskio.c index c369899..67fb3f3 100644 --- a/source/fatfs/diskio.c +++ b/source/fatfs/diskio.c @@ -105,7 +105,8 @@ DSTATUS disk_initialize ( ) { if (pdrv == 0) { // a mounted SD card is the preriquisite for everything else - sdmmc_sdcard_init(); + if (!sdmmc_sdcard_init()) + return RES_PARERR; } else if (pdrv < 4) { nand_type_sys = CheckNandType(false); } else if (pdrv < 7) { diff --git a/source/fs.c b/source/fs.c index d524090..868d88f 100644 --- a/source/fs.c +++ b/source/fs.c @@ -30,6 +30,8 @@ bool InitSDCardFS() { } bool InitNandFS() { + if (!fs_mounted[0]) + return false; for (u32 i = 1; i < MAX_FS; i++) { char fsname[8]; snprintf(fsname, 7, "%lu:", i); diff --git a/source/godmode.c b/source/godmode.c index a32fb85..0a97043 100644 --- a/source/godmode.c +++ b/source/godmode.c @@ -142,9 +142,9 @@ u32 GodMode() { u32 scroll = 0; ClearScreenF(true, true, COLOR_STD_BG); - if (!InitSDCardFS()) { - ShowPrompt(false, "Initialising SD card failed!"); - return exit_mode; + while (!InitSDCardFS()) { + if (!ShowPrompt(true, "Initialising SD card failed! Retry?")) + return exit_mode; } InitNandCrypto(); InitNandFS(); @@ -189,9 +189,9 @@ u32 GodMode() { } else if (pad_state & BUTTON_B) { // unmount SD card DeinitFS(); ShowPrompt(false, "SD card unmounted, you can eject now.\nPut it back in before you press ."); - if (!InitSDCardFS()) { - ShowPrompt(false, "Reinitialising SD card failed!"); - return exit_mode; + while (!InitSDCardFS()) { + if (!ShowPrompt(true, "Reinitialising SD card failed! Retry?")) + return exit_mode; } InitNandFS(); GetDirContents(current_dir, current_path); diff --git a/source/fatfs/delay.s b/source/nand/delay.s similarity index 100% rename from source/fatfs/delay.s rename to source/nand/delay.s diff --git a/source/nand/sdmmc.c b/source/nand/sdmmc.c index 9d69965..dbf353e 100644 --- a/source/nand/sdmmc.c +++ b/source/nand/sdmmc.c @@ -512,7 +512,11 @@ int SD_Init() { inittarget(&handelSD); //waitcycles(0x3E8); - waitcycles(0xF000); + //waitcycles(0xF000); + + waitcycles(1u << 19); //Card needs a little bit of time to be detected, it seems + if (!(*((volatile uint16_t*)0x1000601c) & TMIO_STAT0_SIGSTATE)) return -1; // check if card inserted + DEBUGPRINT(topScreen, "0x00000 ", handelSD.error, 10, 20 + 14*8, RGB(40, 40, 40), RGB(208, 208, 208)); sdmmc_send_command(&handelSD,0,0); DEBUGPRINT(topScreen, "0x10408 ", handelSD.error, 10, 20 + 14*8, RGB(40, 40, 40), RGB(208, 208, 208)); @@ -607,7 +611,7 @@ int SD_Init() return 0; } -void sdmmc_sdcard_init() +int sdmmc_sdcard_init() { DEBUGPRINT(topScreen, "sdmmc_sdcard_init ", handelSD.error, 10, 20 + 2*8, RGB(40, 40, 40), RGB(208, 208, 208)); InitSD(); @@ -615,8 +619,10 @@ void sdmmc_sdcard_init() //Nand_Init(); Nand_Init(); DEBUGPRINT(topScreen, "nand_res ", nand_res, 10, 20 + 3*8, RGB(40, 40, 40), RGB(208, 208, 208)); - SD_Init(); + if (SD_Init() != 0) return FALSE; DEBUGPRINT(topScreen, "sd_res ", sd_res, 10, 20 + 4*8, RGB(40, 40, 40), RGB(208, 208, 208)); + + return TRUE; } int sdmmc_get_cid( int isNand, uint32_t *info) diff --git a/source/nand/sdmmc.h b/source/nand/sdmmc.h index ee612b5..e440bdf 100644 --- a/source/nand/sdmmc.h +++ b/source/nand/sdmmc.h @@ -122,7 +122,7 @@ extern "C" { uint32_t res; } mmcdevice; - void sdmmc_sdcard_init(); + int sdmmc_sdcard_init(); int sdmmc_sdcard_readsector(uint32_t sector_no, uint8_t *out); int sdmmc_sdcard_readsectors(uint32_t sector_no, uint32_t numsectors, uint8_t *out); int sdmmc_sdcard_writesector(uint32_t sector_no, uint8_t *in);