cancel
Showing results for 
Search instead for 
Did you mean: 

STM32U575 - SD card Hard Fault

StefanoSperandio_ST
Associate III

Post edited by ST moderator to be inline with the community rules especially with the code sharing. In next time please use </> button to paste your code. Please read this post: How to insert source code

Hi everyone,

I’m interfacing a microSD card with an STM32U575 microcontroller using the SDMMC1 peripheral. In my project (generated by CubeIDE) I’m using the MX_SDMMC1_SD_Init( ) function to initialize the peripheral. Everything works fine when the card is inserted.

The problem occurs when I remove the microSD card (or if it’s not inserted at all): during initialization, the code stops at this point:

/* 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;

}

This condition returns HAL_ERROR, which is then handled by the default Error_Handler( ) function.

However, the default Error_Handler( ) generated by CubeIDE contains a while(1) loop, which completely

blocks the system.

I’m wondering if this behavior is “normal”:

  • Is it expected that when the SD card is not present, the initialization fails and the code ends up in a blocking while(1)?

  • Shouldn’t the peripheral simply return an initialization error, allowing the firmware to handle it gracefully without blocking the entire system?

In other words, what is the correct way to handle the absence of the SD card (or a failed initialization) so that the

firmware can continue running and handle the error more gracefully instead of entering an infinite loop inside Error_Handler( )?

Any advice or best practices on how to properly manage this scenario?

Thanks in advance!

 

0 REPLIES 0