Skip to main content
Graduate II
February 19, 2025
Question

SD Card detect pin initialization

  • February 19, 2025
  • 5 replies
  • 1183 views

Hello,

Does anyone know how to link the sdcard_detect gpio pin in cubemx?  If the card is not inserted after power up, the system doesn't come up because  MX_SDMMC1_SD_Init(); fails.

I looked at: https://community.st.com/t5/stm32-mcus-embedded-software/understand-sd-card-detect-pin-initialization/td-p/758978

but I'm using AzRTOS, and I can't set the option in FatFS, and can't find the option in FileX.  

Can someone point me in the right direction?

thanks

Matthew

    This topic has been closed for replies.

    5 replies

    matt-crcAuthor
    Graduate II
    February 19, 2025

    Hi @mƎALLEm 

    How do you link the SD_detect pin when AzRTOS is enabled?  it seems FATFS is grayed out in my CubeMX

    mattcrc_0-1739932415607.png

    I cant seem to find that option under FileX.

    Thanks

    Matthew

     

    matt-crcAuthor
    Graduate II
    February 19, 2025

    I've added this code to prevent the system from crashing when initializing the SDMMC on powerup.

    mattcrc_0-1739938840066.png

    But is there an example somewhere on how to re-initialized the driver when a card is plugged in, and get AzRtos/FileX to start reading the SDMMC?  Is there anything in a library without having to re-write a basic function?

    I looked at these examples, Fx_Dual_instance, and Fx_MultiAccess, but it seems it will only run if the card is plugged in, and i didn't find any code for the card detect pin.  Shouldn't that be part of the base Driver code?

    Super User
    February 19, 2025

    Please see How To Insert Source Code - not as an image.

    matt-crcAuthor
    Graduate II
    February 19, 2025

    I've added this code to prevent the system from crashing when initializing the SDMMC on powerup.

    static void MX_SDMMC1_SD_Init(void) {
    
    	/* USER CODE BEGIN SDMMC1_Init 0 */
    	if (HAL_GPIO_ReadPin(SD_Detect_GPIO_Port, SD_Detect_Pin) == 1) {
    		printf("SD Card not found\r\n");
    		return;
    	} else {
    		printf("SD Card Detected\r\n");
    	}
    	HAL_Delay(1000);
    	/* USER CODE END SDMMC1_Init 0 */

     

    But is there an example somewhere on how to re-initialized the driver when a card is plugged in, and get AzRtos/FileX to start reading the SDMMC?  Is there anything in a library without having to re-write a basic function?

    I looked at these examples, Fx_Dual_instance, and Fx_MultiAccess, but it seems it will only run if the card is plugged in, and i didn't find any code for the card detect pin.  Shouldn't that be part of the base Driver code?

    Technical Moderator
    February 19, 2025

    Hello @matt-crc 

    Please refer to the example 'Projects/STM32U575I-EV/Applications/FileX/Fx_uSD_File_Edit' to manage card insertion/removal detection in your application.

     

    matt-crcAuthor
    Graduate II
    February 19, 2025

    Hi Omar @Saket_Om 

    Correct me if I'm wrong, but it seems the software would go into ErrorHandler when main starts (line 247) if there is no card plugged in on bootup:

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

    So, if I can add code to check if card is present using:

     /* Check SD card detect pin */
     if (HAL_GPIO_ReadPin(SDCARD_DETECT_GPIO_Port, SDCARD_DETECT_Pin) == GPIO_PIN_SET)
     {
     ret = HAL_ERROR;
     }
     else
     {
     ret = HAL_OK;
     }

    With the HAL_SD driver offline (card not inserted), the FX_thread does not start properly and will go into a while(1)

    So, my questions are:

    1- Do I need to re-initialize the HAL_SD driver each time a card is inserted?

    2- what happens if a different size or type of card is inserted using your current demo, would the HAL driver need to be re-initialized anyway?

    thanks

    Matthew

     

     

    Technical Moderator
    February 19, 2025

    Hello @matt-crc 

    The code below is the initialization of the SDIO peripheral on the MCU. It is not related to the SD card.

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

     

    matt-crcAuthor
    Graduate II
    February 19, 2025

    Hi @Saket_Om 

    HAL_SD_Init() calls HAL_SD_InitCard() which is supposed to get the parameters from the card.  So HAL_SD_Init() is related to the SD card.   

    Technical Moderator
    February 20, 2025

    Hello @matt-crc 

    You are right!

    In this case, you can perform a check to detect the presence of the card before calling the HAL_SD_Init() function.

    In the application mentioned above the card should plugged before running the code. Please see the readme.

    Saket_Om_0-1740042625183.png