cancel
Showing results for 
Search instead for 
Did you mean: 

Read/Write into SD card using FatFs in STM32u5A9

RiverDi_user
Associate III

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
SHs
ST Employee

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.

View solution in original post

9 REPLIES 9
AScha.3
Chief II

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 "Accept as Solution".

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

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 "Accept as Solution".

Hello Thanks for the quick response.

Here is more info about my connection.

RiverDi_user_2-1724140643554.png

The SDMMC is set as 1 bit mode with GPIO pins are at very high speed. So I modified the pin speed to High (only High and Very High was available, no medium), but it still didnt work.

My application always waits at Hal_Delay() no matter what value or clock speed of the sdmmc.

RiverDi_user_0-1724140529244.png

If I try to bypass the Hal_Delay, I get the hard fault handler error.

About the SD card location, its 

RiverDi_user_1-1724140575848.png

 

DO you have any further suggestion?

 

Aha, that's one of the new TFT with CPU on board - and an EVE chip  or using the U5A LCD controller - right?

So it should work, if you set up right.

About the hal-delay: did you set up the system with a given ioc file, or something you put together?

Because if generating code from a "useful" setup, the hal functions working fine. But you have to enable it, setting the systick and also the debug or swd set/enable.

The sdmmc should work then, also at high speed, because connection is short.

And set pull-ups on all SD card pins.

And put a small delay at start of program, maybe hal_delay (100 ), because most SD card need some time to get ready after power up.

Then try mount (...,1), mount now, and if this is working, can read directory, etc.

Only if this works fine, try create file or write to file.

If you feel a post has answered your question, please click "Accept as Solution".
SHs
ST Employee

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.

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

 

SHs
ST Employee

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.

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.