Skip to main content
Junde
Senior III
May 8, 2024 6:57 AM UTC
Solved

How to config SDIO with FATFS?

Hi all,

I config the SDIO & FATFS by cubeMx for my STM32F429BI board.

When I config the SDIO to SDIO_BUS_WIDE_1B, everything is work well; Even the generated MX_SDIO_SD_Init() is NOT calling the HAL_SD_Init() function. (Why don't need HAL_SD_Init() , is this some mistake?)

 

void MX_SDIO_SD_Init(void)
{
 HAL_StatusTypeDef hal_sta;

 hsd.Instance = SDIO;
 hsd.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
 hsd.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
 hsd.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
 hsd.Init.BusWide = SDIO_BUS_WIDE_1B;
 hsd.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
 hsd.Init.ClockDiv = 0;
}

 

However, when I change SDIO_BUS_WIDE_1B to SDIO_BUS_WIDE_4B, the operate to SD card is error.

But when I change the code to below, the program works well again:

 

void MX_SDIO_SD_Init(void)
{
 HAL_StatusTypeDef hal_sta;

 hsd.Instance = SDIO;
 hsd.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
 hsd.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
 hsd.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
 hsd.Init.BusWide = SDIO_BUS_WIDE_1B;
 hsd.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
 hsd.Init.ClockDiv = 0;

	hal_sta = HAL_SD_Init(&hsd);
	if(hal_sta != HAL_OK) {
		LOG_ERR("SD init err[%d]\n", hal_sta);
	}
	hal_sta = HAL_SD_ConfigWideBusOperation(&hsd, SDIO_BUS_WIDE_4B);
	if(hal_sta != HAL_OK) {
		LOG_ERR("SD configWideBus err[%d]\n", hal_sta);
	} else {
		LOG_DBG("SD configWideBus to 4bit ok\n");
	}
}

 

I can't understand what the difference to config the BusWide between HAL_SD_Init() and HAL_SD_ConfigWideBusOperation();

Could someone can explain it, thank you!

 

This topic has been closed for replies.
mƎALLEm
mƎALLEmBest answer
ST Technical Moderator
May 8, 2024 8:29 AM UTC

Hello,

What you did is correct and this is what you supposed to do.

Please refer to that thread:

https://community.st.com/t5/stm32cubemx-mcus/sdio-interface-not-working-in-4bits-with-stm32f4-firmware/m-p/591803#M26033

 

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.

2 replies

mƎALLEm
mƎALLEmBest answer
ST Technical Moderator
May 8, 2024 8:29 AM UTC

Hello,

What you did is correct and this is what you supposed to do.

Please refer to that thread:

https://community.st.com/t5/stm32cubemx-mcus/sdio-interface-not-working-in-4bits-with-stm32f4-firmware/m-p/591803#M26033

 

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
ST Technical Moderator
May 8, 2024 3:06 PM UTC

Hello @Junde 

The function HAL_SD_Init() is responsible for the initial setup of the SDIO using the STM32 HAL library. It calls HAL_SD_InitCard(), which configures the SDIO peripheral with default settings for the first initialization. Here is a snippet of the HAL_SD_InitCard() function:

 

HAL_StatusTypeDef HAL_SD_InitCard(SD_HandleTypeDef *hsd)
{
 uint32_t errorstate;
 HAL_StatusTypeDef status;
 SD_InitTypeDef Init;
 
 /* Default SDIO peripheral configuration for SD card initialization */
 Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
 Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
 Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
 Init.BusWide = SDIO_BUS_WIDE_1B;
 Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
 Init.ClockDiv = SDIO_INIT_CLK_DIV;

 /* Initialize SDIO peripheral interface with default configuration */
 status = SDIO_Init(hsd->Instance, Init);
 if(status != HAL_OK)
 {
 return HAL_ERROR;
 }

 /* Disable SDIO Clock */
 __HAL_SD_DISABLE(hsd);

 /* Set Power State to ON */
 (void)SDIO_PowerState_ON(hsd->Instance);

 /* Enable SDIO Clock */
 __HAL_SD_ENABLE(hsd);

 /* Required power up waiting time before starting the SD initialization sequence */
 HAL_Delay(2);

 /* Identify card operating voltage */
 errorstate = SD_PowerON(hsd);
 if(errorstate != HAL_SD_ERROR_NONE)
 {
 hsd->State = HAL_SD_STATE_READY;
 hsd->ErrorCode |= errorstate;
 return HAL_ERROR;
 }

 /* Card initialization */
 errorstate = SD_InitCard(hsd);
 if(errorstate != HAL_SD_ERROR_NONE)
 {
 hsd->State = HAL_SD_STATE_READY;
 hsd->ErrorCode |= errorstate;
 return HAL_ERROR;
 }

 /* Set Block Size for Card */
 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
 if(errorstate != HAL_SD_ERROR_NONE)
 {
 /* Clear all the static flags */
 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
 hsd->ErrorCode |= errorstate;
 hsd->State = HAL_SD_STATE_READY;
 return HAL_ERROR;
 }

 return HAL_OK;
}

 

To enable 4-bit wide bus operation on an SD card, you need to call the function HAL_SD_ConfigWideBusOperation() separately after initializing the card with HAL_SD_InitCard().

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Saket_Om