Skip to main content
Associate III
August 19, 2024
Solved

Read/Write into SD card using FatFs in STM32u5A9

Hello,

I have been trying to read/write into an SD card using the SPI or SDMMC method with Fatfs. I wasn't able to make use of the filex option for file operation which is provided in the STM32u5A9 libraries, since I am using FreeRTOS in my project (as per notes, Filex cannot be used with FreeRTOS). So, I have taken the FatFs library from STM32F4 design and ported the same to STM32U5A9 design (took reference from https://deepbluembedded.com/stm32-sd-card-spi-fatfs-tutorial-examples/). Unfortunately, in both SPI and SDMMC methods, I was not able to initialize my SD card and the application keep on waiting for SPI Rx data in SPI mode, and in SDMMC method, the application keeps on waiting in a HAL_Delay() in HAL_SD_initcard() API

RiverDi_user_0-1724050298053.png

Is there any known issues in this board type? Can you point any working examples for STM32U5A9 with FatFs and FreeRTOS?

 

Your support is much appreciated!

Thanks & Regards

 

 

This topic has been closed for replies.
SHsBest answer
ST Employee
September 3, 2024

Hello @RiverDi_user ,

Could you please verify that the FreeRTOS task responsible for accessing the SD card has a high enough priority and sufficient stack size? Sometimes, insufficient stack size or lower priority might cause the task to be preempted or fail. Additionally, since FreeRTOS is a multitasking environment, concurrent access to the SD card can lead to issues. To address this, please consider using a mutex to ensure that only one task accesses the SD card at a time. For your convenience, you can follow this project as a reference.

 

Hope this helps!

Regards,

"Please close this topic by clicking on “Accept as solution"" button if it fully answered your question."

16 replies

AScha.3
Super User
August 19, 2024

Hi,

No need to play around with spi mode, if you have SDIO.

Just set 1 bit mode, standard speed (25 MHz) , and only try mount and read - from a good card with some files on it.

Only if this works perfect, try writing or creating a file.

Your problem is typically a hardware problem, so have wires to card close together and distance chip to card (wires) < 40 mm .

And set pins to medium speed and pull-ups on on all lined lines to sd- card.

If you feel a post has answered your question, please click on " Best Answer ".
Associate III
August 20, 2024

Hello,

I don't see SDIO option in my ioc file. I assume I can use only SPI or SDMMC (may be SDIO inbuilt) to access the SD card.

RiverDi_user_0-1724130761994.png

 

Just further information about my setup. I am using a RVT50HQSFWN00 River DI 5 inch display module which already has an SD card slot available onboard. So there are no physical connection that I have added additionally. Since it already had the SD card slot, I have enabled the SDMMC1 in the ioc and built my application. The clock device factor was set as 0, 8, 128 (tried all three settings), but the SDMMC init function "MX_SDMMC1_SD_Init()" was keep on waiting in a HAL_Delay() when I tried to debug the app.

RiverDi_user_1-1724130889425.png

Can you share some insights on what's wrong here?

 

Thanks and Regards

AScha.3
Super User
August 20, 2024

Just SDIO or sdmmc....sd-card bus connection.

Usually (for me...) always at first SD not working - so no surprise. At first the hardware has to work, so check connection to CPU, set pins on medium speed and pull-ups on. And 1 bit mode.

How long are the connections to sd-card?

If you feel a post has answered your question, please click on " Best Answer ".
ST Employee
August 22, 2024

Hello @RiverDi_user ,

Given that the SD card slot is integrated into the display module and you are using the SDMMC1 interface, could you please try to verify that the clock configuration for the SDMMC1 peripheral is correct? This could be a potential reason for your issue because the SDMMC clock should be derived from a stable clock source, typically the PLL or HCLK. It is advisable to meticulously review the clock tree in STM32CubeMX to confirm that the SDMMC1 clock is properly configured.

 

Best Regards,

"Please close this topic by clicking on “Accept as solution"" button if it fully answered your question."
Associate III
August 26, 2024

Hello,

I have crosschecked my clock configuration and made sure the pre-scalar values are set correctly. The SDMMC1 init function was not initialized properly, so I had to rebuild the ioc. Once after that rebuild and few timer changes, my SDMMC init worked and I was able to read/write into SD card, but only before the FreeRTOS tasks start, as soon as I start the FreeRTOS tasks, my SD card read/write operation waits in HAL_SD_ReadBlocks() indefinitely, so I am confused here. Why was I able to access SD before the FreeRTOS task and not after, can you please shed some light here?

My main function looks like this: (the SD read write API calls before  "/* USER CODE END 2 */" works fine, but not after FreeRTOS init)

int main(void)

{

 

/* USER CODE BEGIN 1 */

 

/* USER CODE END 1 */

 

/* MCU Configuration--------------------------------------------------------*/

 

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */

HAL_Init();

 

/* USER CODE BEGIN Init */

 

/* USER CODE END Init */

 

/* Configure the system clock */

SystemClock_Config();

 

/* Configure the peripherals common clocks */

PeriphCommonClock_Config();

 

/* Configure the System Power */

SystemPower_Config();

 

/* USER CODE BEGIN SysInit */

// GPIO_ENCODER_INIT();

HAL_Delay(5000);

/* USER CODE END SysInit */

 

/* Initialize all configured peripherals */

MX_GPIO_Init();

MX_ADC1_Init();

MX_ADC2_Init();

MX_CORDIC_Init();

MX_CRC_Init();

MX_DAC1_Init();

MX_DCACHE1_Init();

MX_DCACHE2_Init();

MX_DMA2D_Init();

MX_FDCAN1_Init();

MX_GPU2D_Init();

MX_HASH_Init();

MX_I2C1_Init();

MX_I2C2_Init();

MX_I2C4_Init();

MX_LPTIM2_Init();

MX_LTDC_Init();

MX_OCTOSPI1_Init();

MX_RNG_Init();

MX_RTC_Init();

MX_SPI1_Init();

MX_SPI2_Init();

MX_TIM3_Init();

MX_TIM6_Init();

MX_TIM8_Init();

MX_TIM15_Init();

MX_USART1_UART_Init();

MX_USART3_UART_Init();

MX_USART6_UART_Init();

MX_USB_OTG_HS_USB_Init();

MX_TIM7_Init();

MX_SDMMC1_SD_Init();

MX_ICACHE_Init();

MX_TouchGFX_Init();

/* Call PreOsInit function */

MX_TouchGFX_PreOSInit();

/* USER CODE BEGIN 2 */

if (HAL_TIM_Base_Start_IT(&htim3) != HAL_OK)

{

/* Starting Error */

Error_Handler();

}

 

if (HAL_TIM_PWM_Start(&htim15, TIM_CHANNEL_1) != HAL_OK)

{

/* PWM Generation Error */

Error_Handler();

}

 

/*Configure GPIO pin Output Level */

 

HAL_GPIO_WritePin(LCD_DISP_RESET_GPIO_Port, LCD_DISP_RESET_Pin, GPIO_PIN_SET);

 

UARTTask_Init();

UARTQueue_Init();

SelfTestTask_Init();

CANTask_Init();

MX_FATFS_Init();

 

HAL_Delay(100); // This delay is not mandatory but it gives me some time to prep the SD card

 

 

Mount_SD ("/");

sprintf (filename_buffer, "/%d-%d-%dT%d_%d_%d.txt", sTime.Hours,sTime.Minutes,sTime.Seconds,sDate.Month,sDate.Month,sDate.Year);

Create_File(&filename_buffer);

Unmount_SD("/");

 

SDMMC_SDCard_Test();

HAL_Delay(100); // This delay is not mandatory but it gives me some time to prep the SD card

 

SDMMC_SDCard_Test();

 

/* USER CODE END 2 */

 

/* Init scheduler */

osKernelInitialize();

 

/* Call init function for freertos objects (in cmsis_os2.c) */

MX_FREERTOS_Init();

 

/* Start scheduler */

osKernelStart();

 

/* We should never get here as control is now taken by the scheduler */

 

/* Infinite loop */

/* USER CODE BEGIN WHILE */

while (1)

{

/* USER CODE END WHILE */

 

/* USER CODE BEGIN 3 */

}

/* USER CODE END 3 */

}


In one of the FreeRTOS task, I am calling the SDMMC_SDCard_Test() API again, which doesnt proceed further from

HAL_SD_ReadBlocks() API's HAL_Delay.

RiverDi_user_0-1724655102337.png
 
Thanks & Regards

 

SHsBest answer
ST Employee
September 3, 2024

Hello @RiverDi_user ,

Could you please verify that the FreeRTOS task responsible for accessing the SD card has a high enough priority and sufficient stack size? Sometimes, insufficient stack size or lower priority might cause the task to be preempted or fail. Additionally, since FreeRTOS is a multitasking environment, concurrent access to the SD card can lead to issues. To address this, please consider using a mutex to ensure that only one task accesses the SD card at a time. For your convenience, you can follow this project as a reference.

 

Hope this helps!

Regards,

"Please close this topic by clicking on “Accept as solution"" button if it fully answered your question."
Associate III
September 5, 2024

Thank you for the suggestion. I was able to make the read/write work with high priority for SD card task, lastweek. I forgot to update in the post.