From 8b80ae22787c5e78a2aebb48d72d9dfa81bdf740 Mon Sep 17 00:00:00 2001 From: Andrey Makoldin Date: Sun, 14 Dec 2025 18:14:06 +0300 Subject: [PATCH] [HAL][SD] Fix SD card initialization with 4-bit bus width --- Src/stm32f4xx_hal_sd.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/Src/stm32f4xx_hal_sd.c b/Src/stm32f4xx_hal_sd.c index 8e27a52..b2386f0 100644 --- a/Src/stm32f4xx_hal_sd.c +++ b/Src/stm32f4xx_hal_sd.c @@ -334,6 +334,8 @@ static void SD_DMARxAbort(DMA_HandleTypeDef *hdma); */ HAL_StatusTypeDef HAL_SD_Init(SD_HandleTypeDef *hsd) { + uint32_t tickstart; + /* Check the SD handle allocation */ if(hsd == NULL) { @@ -381,6 +383,24 @@ HAL_StatusTypeDef HAL_SD_Init(SD_HandleTypeDef *hsd) return HAL_ERROR; } + /* Configure the bus wide */ + if (HAL_SD_ConfigWideBusOperation(hsd, hsd->Init.BusWide) != HAL_OK) + { + return HAL_ERROR; + } + + /* Verify that SD card is ready to use after Initialization */ + tickstart = HAL_GetTick(); + while ((HAL_SD_GetCardState(hsd) != HAL_SD_CARD_TRANSFER)) + { + if ((HAL_GetTick() - tickstart) >= SDMMC_SWDATATIMEOUT) + { + hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT; + hsd->State = HAL_SD_STATE_READY; + return HAL_TIMEOUT; + } + } + /* Initialize the error code */ hsd->ErrorCode = HAL_SD_ERROR_NONE; @@ -2675,6 +2695,7 @@ static uint32_t SD_InitCard(SD_HandleTypeDef *hsd) HAL_SD_CardCSDTypeDef CSD; uint32_t errorstate; uint16_t sd_rca = 1U; + SD_InitTypeDef Init; /* Check the power State */ if(SDIO_GetPowerState(hsd->Instance) == 0U) @@ -2748,8 +2769,10 @@ static uint32_t SD_InitCard(SD_HandleTypeDef *hsd) return errorstate; } - /* Configure SDIO peripheral interface */ - (void)SDIO_Init(hsd->Instance, hsd->Init); + /* Configure the SDIO peripheral */ + Init = hsd->Init; + Init.BusWide = SDIO_BUS_WIDE_1B; + (void)SDIO_Init(hsd->Instance, Init); /* All cards are initialized */ return HAL_SD_ERROR_NONE;