Skip to content

Commit

Permalink
fixed FR_DISK_ERROR on sd card reinsert
Browse files Browse the repository at this point in the history
  • Loading branch information
Naim2000 committed Jun 6, 2024
1 parent 057847c commit a66ae33
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions source/sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,32 @@ bool SDMount(void) {

sdmc->startup();


const char spin[] = "|/-\\";
const char* p = spin;
bool inserted = sdmc->isInserted();
while (!inserted) {
printf(pWarn "Insert your SD card, then press (A) to continue. [%c]\r", *p++);
printf(pWarn "Please insert your SD card. [%c] (Press HOME to cancel)\r", *p++);
scanpads();
if (buttons_down(WPAD_BUTTON_A)) break;
if (buttons_down(WPAD_BUTTON_HOME)) break;

if (!*p) p = spin;
if (!*p) {
p = spin;
sdmc->startup();
inserted = sdmc->isInserted();
}

usleep(100000);
}
clearln(0);

if (!sdmc->isInserted()) {
// puts(pWarn "Operation cancelled by user...");
puts(pBad "No SD card inserted?");
if (!inserted) {
puts(pWarn "Operation cancelled by user...");
return false;
}

printf(pInfo "Mounting SD card ...\r");
// usleep(1000000);
sdmc->startup();
FRESULT fres = f_mount(&fs, "sdmc:/", true);
if (fres == FR_OK) {
unsigned long freeSpace = (fs.free_clst * fs.csize);
Expand All @@ -53,14 +57,13 @@ bool SDMount(void) {
if (totalSpace < 0x400000) // 2GB (4M sectors)
{
printf(pWarn "Your SD card is under 2GB? (%luMB)\n", totalSpace / 0x800);
sleep(2);
usleep(2000000);
}


if (freeSpace < 0x8000)
if (freeSpace < 0x8000) // 16MB (32K sectors)
{
printf(pBad "Error #06: Insufficient SD card space!\n"
// Double, because if we inject MSET9 and dip below 16MB wel'll have trouble uninjecting it
pBad "At least 512 blocks (32MB) are required, you have %lu!", freeSpace / 0x100);

SDUnmount();
Expand All @@ -76,9 +79,8 @@ bool SDMount(void) {

void SDUnmount(void) {
if (sdMounted) {

f_unmount("sdmc:/");
// sdmc->shutdown();
// sdmc->shutdown();
puts(pInfo "Unmounted SD card.");
sdMounted = false;
}
Expand All @@ -104,11 +106,13 @@ bool SDRemount(void) {
usleep(100000);
}

sdmc->shutdown();

clearln(0);
if (inserted) return false;

puts(pGood "Ejected SD card.");
sleep(5);
puts(pGood "SD card ejected.");
usleep(10000000);

return SDMount();
}

0 comments on commit a66ae33

Please sign in to comment.