2025-02-18 06:53 PM
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
Solved! Go to Solution.
2025-02-19 06:42 AM - edited 2025-02-19 06:44 AM
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.
2025-02-18 06:34 PM
Hi @mƎALLEm
How do you link the SD_detect pin when AzRTOS is enabled? it seems FATFS is grayed out in my CubeMX
I cant seem to find that option under FileX.
Thanks
Matthew
2025-02-18 08:27 PM
I've added this code to prevent the system from crashing when initializing the SDMMC on powerup.
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?
2025-02-19 01:29 AM
Please see How To Insert Source Code - not as an image.
2025-02-19 01:37 AM
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?
2025-02-19 06:42 AM - edited 2025-02-19 06:44 AM
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.
2025-02-19 08:20 AM
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
2025-02-19 08:24 AM
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();
}
2025-02-19 08:31 AM - edited 2025-02-19 10:37 PM
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.
2025-02-20 01:15 AM - edited 2025-02-20 01:21 AM
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.