[HAL][SD] Fix SD card initialization with 4-bit bus width #45
+25
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
When hsd->Init.BusWide is set to SDIO_BUS_WIDE_4B, the SD card fails to initialize. Users are forced to use a workaround: set BusWide = SDIO_BUS_WIDE_1B and manually call HAL_SD_ConfigWideBusOperation() afterward.
This issue was discussed in the ST Community forum:
Running SDIO 4BIT wide mode
The recommended workaround requires users to modify MX_SDIO_SD_Init() and is inconsistent with the STM32H7 HAL behavior where 4-bit mode works out of the box.
Root Cause
In the internal function SD_InitCard(), the SDIO peripheral was reconfigured with user parameters including BusWide:
At this point, the SD card has not received the ACMD6 command to switch bus width. This causes desynchronization:
Solution
Two changes were made to align STM32F4 HAL behavior with STM32H7 HAL:
Fixed SDIO_Init() call in SD_InitCard() to force 1-bit bus width. The SDIO peripheral reconfiguration now explicitly sets 1-bit bus width while preserving other user settings (ClockDiv, ClockEdge, etc.): This ensures the SDIO controller and SD card remain synchronized in 1-bit mode until proper bus width switching via ACMD6.
Added automatic bus width configuration in HAL_SD_Init(). After successful card initialization, HAL_SD_Init() now automatically calls HAL_SD_ConfigWideBusOperation() to apply the user-specified bus width, followed by a readiness check:
This is consistent with the STM32H7 HAL implementation where this exact code exists and works correctly.
Benefits