bdk: sd: better removal detection handling

This commit is contained in:
CTCaer 2023-03-31 08:31:20 +03:00
parent 2f7e841b50
commit 7f32c6d211

View file

@ -26,7 +26,8 @@ static bool sd_mounted = false;
static bool sd_init_done = false; static bool sd_init_done = false;
static bool insertion_event = false; static bool insertion_event = false;
static u16 sd_errors[3] = { 0 }; // Init and Read/Write errors. static u16 sd_errors[3] = { 0 }; // Init and Read/Write errors.
static u32 sd_mode = SD_UHS_SDR104; static u32 sd_mode = SD_DEFAULT_SPEED;
sdmmc_t sd_sdmmc; sdmmc_t sd_sdmmc;
sdmmc_storage_t sd_storage; sdmmc_storage_t sd_storage;
@ -93,21 +94,26 @@ int sd_init_retry(bool power_cycle)
{ {
case SD_INIT_FAIL: // Reset to max. case SD_INIT_FAIL: // Reset to max.
return 0; return 0;
case SD_1BIT_HS25: case SD_1BIT_HS25:
bus_width = SDMMC_BUS_WIDTH_1; bus_width = SDMMC_BUS_WIDTH_1;
type = SDHCI_TIMING_SD_HS25; type = SDHCI_TIMING_SD_HS25;
break; break;
case SD_4BIT_HS25: case SD_4BIT_HS25:
type = SDHCI_TIMING_SD_HS25; type = SDHCI_TIMING_SD_HS25;
break; break;
case SD_UHS_SDR82: case SD_UHS_SDR82:
type = SDHCI_TIMING_UHS_SDR82; type = SDHCI_TIMING_UHS_SDR82;
break; break;
case SD_UHS_SDR104: case SD_UHS_SDR104:
type = SDHCI_TIMING_UHS_SDR104; type = SDHCI_TIMING_UHS_SDR104;
break; break;
default: default:
sd_mode = SD_UHS_SDR104; sd_mode = SD_DEFAULT_SPEED;
break;
} }
int res = sdmmc_storage_init_sd(&sd_storage, &sd_sdmmc, bus_width, type); int res = sdmmc_storage_init_sd(&sd_storage, &sd_sdmmc, bus_width, type);
@ -135,7 +141,7 @@ bool sd_initialize(bool power_cycle)
return true; return true;
else if (!sdmmc_get_sd_inserted()) // SD Card is not inserted. else if (!sdmmc_get_sd_inserted()) // SD Card is not inserted.
{ {
sd_mode = SD_UHS_SDR104; sd_mode = SD_DEFAULT_SPEED;
break; break;
} }
else else
@ -194,8 +200,12 @@ bool sd_mount()
static void _sd_deinit(bool deinit) static void _sd_deinit(bool deinit)
{ {
if (deinit && sd_mode == SD_INIT_FAIL) if (deinit)
sd_mode = SD_UHS_SDR104; {
insertion_event = false;
if (sd_mode == SD_INIT_FAIL)
sd_mode = SD_DEFAULT_SPEED;
}
if (sd_init_done) if (sd_init_done)
{ {
@ -205,8 +215,7 @@ static void _sd_deinit(bool deinit)
if (deinit) if (deinit)
{ {
sdmmc_storage_end(&sd_storage); sdmmc_storage_end(&sd_storage);
sd_init_done = false; sd_init_done = false;
insertion_event = false;
} }
} }
sd_mounted = false; sd_mounted = false;