cancel
Showing results for 
Search instead for 
Did you mean: 

CubeMX SD card initialization bug

cb.7
Associate III

In the update to CubeMX version 6.17, the SD card initialization (in my case, SDMMC2) was changed, and the software goes into "Error_Handler()". In my project I use STM32H7B0VBT6, FATFS, not RTOS.


Specifically, version 6.17:

static void MX_SDMMC2_SD_Init(void)
{

  /* USER CODE BEGIN SDMMC2_Init 0 */

  /* USER CODE END SDMMC2_Init 0 */

  /* USER CODE BEGIN SDMMC2_Init 1 */

  /* USER CODE END SDMMC2_Init 1 */
  hsd2.Instance = SDMMC2;
  hsd2.Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
  hsd2.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
  hsd2.Init.BusWide = SDMMC_BUS_WIDE_4B;
  hsd2.Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
  hsd2.Init.ClockDiv = 10;
  if (HAL_SD_Init(&hsd2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN SDMMC2_Init 2 */

  /* USER CODE END SDMMC2_Init 2 */

}

 

In previous versions:

static void MX_SDMMC2_SD_Init(void)
{

  /* USER CODE BEGIN SDMMC2_Init 0 */

  /* USER CODE END SDMMC2_Init 0 */

  /* USER CODE BEGIN SDMMC2_Init 1 */

  /* USER CODE END SDMMC2_Init 1 */
  hsd2.Instance = SDMMC2;
  hsd2.Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
  hsd2.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
  hsd2.Init.BusWide = SDMMC_BUS_WIDE_4B;
  hsd2.Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
  hsd2.Init.ClockDiv = 10;
  /* USER CODE BEGIN SDMMC2_Init 2 */

  /* USER CODE END SDMMC2_Init 2 */

}

 

for now I solved it like this:

static void MX_SDMMC2_SD_Init(void)
{

  /* USER CODE BEGIN SDMMC2_Init 0 */
	  hsd2.Instance = SDMMC2;
	  hsd2.Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
	  hsd2.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
	  hsd2.Init.BusWide = SDMMC_BUS_WIDE_4B;
	  hsd2.Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
	  hsd2.Init.ClockDiv = 10;
	  return;
  /* USER CODE END SDMMC2_Init 0 */

  /* USER CODE BEGIN SDMMC2_Init 1 */

  /* USER CODE END SDMMC2_Init 1 */
  hsd2.Instance = SDMMC2;
  hsd2.Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
  hsd2.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
  hsd2.Init.BusWide = SDMMC_BUS_WIDE_4B;
  hsd2.Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
  hsd2.Init.ClockDiv = 10;
  if (HAL_SD_Init(&hsd2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN SDMMC2_Init 2 */

  /* USER CODE END SDMMC2_Init 2 */

}

 

I hope this will be fixed in the next release....

 

 

6 REPLIES 6
Ghofrane GSOURI
ST Employee

Hello @cb.7 

I'm currently checking this behavior. I will get back to you ASAP.

THX

Ghofrane

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Ghofrane GSOURI
ST Employee

Hello @cb.7 

You changed the SDMMC2 init function generated by CubeMX 6.17 so that it no longer calls HAL_SD_Init().

You have added a 

return;

Because of this return;, the rest of the function

if (HAL_SD_Init(&hsd2) != HAL_OK)
{
    Error_Handler();
}

is never executed. As a result:

HAL_SD_Init() is not called at all.The SD card initialization that used to fail and send you into Error_Handler() is now completely skipped.
The firmware no longer locks up in Error_Handler(), but the SDMMC2 peripheral is also not properly initialized by the HAL in this function.

Your workaround hides the failure instead of fixing it so you should carefully check your SDMMC2 configuration, because HAL_SD_Init() is failing due to some misconfiguration, not because the if (...) { Error_Handler(); } line is wrong.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

That's not exactly true; the "HAL_SD_Init(&hsd2)" function is called later within "BSP_SD_Init(void)" in the "bsp_driver_sd.c" file.
I confirm that the "HAL_SD_Init(&hsd2)" function called within "MX_SDMMC2_SD_Init(void)" always generates "Error_Handler()," even if the SD card is not physically inserted.

Hello @cb.7 

Could you please provide your IOC?

THX

Ghofrane

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

sure, here it is

cb.7
Associate III

I noticed that if I insert the SD card, "HAL_SD_Init(&hsd2)" works correctly and doesn't return "Error_Handler()." Perhaps it's a hardware or timing issue?
I install 47K pull-ups on all lines of the SD card except the CLK line.

The error comes from "SD_PowerON"