From a4eba5e6327125d7cbd15710ad932eac3c4d0556 Mon Sep 17 00:00:00 2001 From: bkleiner Date: Sat, 7 Dec 2024 19:56:04 +0100 Subject: [PATCH 1/2] h7 sdmmc: fix clock reset, disable power save, reduce speed --- src/main/drivers/sdcard/sdmmc_sdio_h7xx.c | 26 +++++++++++++---------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/main/drivers/sdcard/sdmmc_sdio_h7xx.c b/src/main/drivers/sdcard/sdmmc_sdio_h7xx.c index d8cf16ba83e..9a9eafe1b08 100644 --- a/src/main/drivers/sdcard/sdmmc_sdio_h7xx.c +++ b/src/main/drivers/sdcard/sdmmc_sdio_h7xx.c @@ -158,14 +158,16 @@ void HAL_SD_MspInit(SD_HandleTypeDef* hsd) if (sdioHardware->instance == SDMMC1) { __HAL_RCC_SDMMC1_CLK_DISABLE(); + __HAL_RCC_SDMMC1_CLK_ENABLE(); + __HAL_RCC_SDMMC1_FORCE_RESET(); __HAL_RCC_SDMMC1_RELEASE_RESET(); - __HAL_RCC_SDMMC1_CLK_ENABLE(); } else if (sdioHardware->instance == SDMMC2) { __HAL_RCC_SDMMC2_CLK_DISABLE(); + __HAL_RCC_SDMMC2_CLK_ENABLE(); + __HAL_RCC_SDMMC2_FORCE_RESET(); __HAL_RCC_SDMMC2_RELEASE_RESET(); - __HAL_RCC_SDMMC2_CLK_ENABLE(); } const IO_t clk = IOGetByTag(sdioPin[SDIO_PIN_CK].pin); @@ -248,28 +250,30 @@ bool SD_GetState(void) bool SD_Init(void) { - HAL_StatusTypeDef status; - memset(&hsd1, 0, sizeof(hsd1)); hsd1.Instance = sdioHardware->instance; - hsd1.Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING; - hsd1.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_ENABLE; + // falling seems to work better ?? no idea whats "right" here + hsd1.Init.ClockEdge = SDMMC_CLOCK_EDGE_FALLING; + + // drastically increases the time to respond from the sdcard + // lets leave it off + hsd1.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE; #ifdef SDCARD_SDIO_4BIT hsd1.Init.BusWide = SDMMC_BUS_WIDE_4B; #else - hsd1.Init.BusWide = SDMMC_BUS_WIDE_1B; // FIXME untested + hsd1.Init.BusWide = SDMMC_BUS_WIDE_1B; #endif hsd1.Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_ENABLE; #ifdef SDCARD_SDIO_NORMAL_SPEED - hsd1.Init.ClockDiv = SDMMC_NSpeed_CLK_DIV; + hsd1.Init.ClockDiv = SDMMC_NSpeed_CLK_DIV; #else - hsd1.Init.ClockDiv = 1; // 200Mhz / (2 * 1 ) = 100Mhz, used for "UltraHigh speed SD card" only, see HAL_SD_ConfigWideBusOperation, SDMMC_HSpeed_CLK_DIV, SDMMC_NSpeed_CLK_DIV + hsd1.Init.ClockDiv = SDMMC_HSpeed_CLK_DIV; // lets not go too crazy :) #endif - status = HAL_SD_Init(&hsd1); // Will call HAL_SD_MspInit - + // Will call HAL_SD_MspInit + const HAL_StatusTypeDef status = HAL_SD_Init(&hsd1); if (status != HAL_OK) { return SD_ERROR; } From 1c288a85acd1705fa40ad501f91184bb6f7b3b5e Mon Sep 17 00:00:00 2001 From: bkleiner Date: Sun, 8 Dec 2024 13:34:47 +0100 Subject: [PATCH 2/2] sdio: fix read error handling. --- src/main/drivers/sdcard/sdcard_sdio.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/drivers/sdcard/sdcard_sdio.c b/src/main/drivers/sdcard/sdcard_sdio.c index 58fc79acc1f..38e36811cc3 100644 --- a/src/main/drivers/sdcard/sdcard_sdio.c +++ b/src/main/drivers/sdcard/sdcard_sdio.c @@ -347,8 +347,20 @@ static bool sdcardSdio_poll(void) break; // Timeout not reached yet so keep waiting } // Timeout has expired, so fall through to convert to a fatal error + FALLTHROUGH; case SDCARD_RECEIVE_ERROR: + sdcardSdio_reset(); + + if (sdcard.pendingOperation.callback) { + sdcard.pendingOperation.callback( + SDCARD_BLOCK_OPERATION_READ, + sdcard.pendingOperation.blockIndex, + NULL, + sdcard.pendingOperation.callbackData + ); + } + goto doMore; break; }