1
0
mirror of https://github.com/CTCaer/hekate.git synced 2024-12-24 12:14:11 +00:00

bdk: sd: improve init error handling

- Management of sd init done is now on sd init retry function
 Also manages inserted, since it can set the sd init done to false if failed
- Init will now always check if SD is also initialized, since it doesn't manage it anymore. Because of that, mounting is no longer forced, but checked first.
- Unmount/End will now always set the sd as unmounted, since no data residue is expected after the fact

The above will improve handling of faulty SD cards or faulty SD readers.
This commit is contained in:
CTCaer 2023-02-22 13:19:12 +02:00
parent a44a4881d4
commit 55e01ca735

View File

@ -1,6 +1,6 @@
/*
* Copyright (c) 2018 naehrwert
* Copyright (c) 2018-2022 CTCaer
* Copyright (c) 2018-2023 CTCaer
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
@ -24,6 +24,7 @@
static bool sd_mounted = false;
static bool sd_init_done = false;
static bool inserted = false;
static u16 sd_errors[3] = { 0 }; // Init and Read/Write errors.
static u32 sd_mode = SD_UHS_SDR104;
@ -54,8 +55,11 @@ u16 *sd_get_error_count()
bool sd_get_card_removed()
{
if (sd_init_done && !sdmmc_get_sd_inserted())
if (inserted && !sdmmc_get_sd_inserted())
{
inserted = false;
return true;
}
return false;
}
@ -109,7 +113,16 @@ int sd_init_retry(bool power_cycle)
sd_mode = SD_UHS_SDR104;
}
return sdmmc_storage_init_sd(&sd_storage, &sd_sdmmc, bus_width, type);
int res = sdmmc_storage_init_sd(&sd_storage, &sd_sdmmc, bus_width, type);
if (res)
{
inserted = true;
sd_init_done = true;
}
else
sd_init_done = false;
return res;
}
bool sd_initialize(bool power_cycle)
@ -146,7 +159,7 @@ bool sd_initialize(bool power_cycle)
bool sd_mount()
{
if (sd_mounted)
if (sd_init_done && sd_mounted)
return true;
int res = 0;
@ -165,8 +178,8 @@ bool sd_mount()
}
else
{
sd_init_done = true;
res = f_mount(&sd_fs, "0:", 1); // Volume 0 is SD.
if (!sd_mounted)
res = f_mount(&sd_fs, "0:", 1); // Volume 0 is SD.
if (res == FR_OK)
{
sd_mounted = true;
@ -187,16 +200,18 @@ static void _sd_deinit(bool deinit)
if (deinit && sd_mode == SD_INIT_FAIL)
sd_mode = SD_UHS_SDR104;
if (sd_init_done && sd_mounted)
if (sd_init_done)
{
f_mount(NULL, "0:", 1); // Volume 0 is SD.
sd_mounted = false;
}
if (sd_init_done && deinit)
{
sdmmc_storage_end(&sd_storage);
sd_init_done = false;
if (sd_mounted)
f_mount(NULL, "0:", 1); // Volume 0 is SD.
if (deinit)
{
sdmmc_storage_end(&sd_storage);
sd_init_done = false;
}
}
sd_mounted = false;
}
void sd_unmount() { _sd_deinit(false); }