cancel
Showing results for 
Search instead for 
Did you mean: 

STM32H7 FatFS f_write fails at low-level disk_read operation

rtonet
Associate II

Hi ST Community.

I am using an STM32H725IGK MCU on a custom board. My board has a uSD card slot connected to the SDIO pins, and I am using SDMMC1 peripheral. I am also using FatFS to use a filesystem on the uSD card.

I can operate the SD card properly most of the time. I can mount, format, create files and folders, and read and write to files. However, sometimes, some operations fail. Normally, it's either f_write or f_lseek. They fail at the low-level disk_read() operation. I do not understand why.

I will share my system configuration:
- Clock MUX:

rtonet_0-1756738551053.png

- SDMMC Parameters:

rtonet_1-1756738631926.png

The clock divide factor of 2 gives 32 MHz. I also tried setting the clock to 16 MHz and 8 MHz, but the problem persists.

- SDMMC GPIO settings:

rtonet_2-1756738664513.png

I also adjusted the output speed of the pins to guarantee the signal integrity is good, but the issue remains.
- FatFS defines:

rtonet_3-1756738932423.pngrtonet_4-1756738954559.png

- FatFS Advanced Settings:

rtonet_5-1756738990487.png

My system also utilizes FreeRTOS, and I use semaphores to ensure that only one task accesses the SD card at a time. I have the FS_REENTRANT disabled.

I tried SD cards from different brands and different sizes. It seems that 16 GB cards work better, but still fail. The 32 GB fails more often.

Is there a limitation on the number of bytes that I can use on f_write or f_lseek? Is there a problem with my configuration that I'm not seeing? Do I have to use multiples of the maximum sector size, 512, in my configuration?

Any help is deeply appreciated. I've been stuck with this issue for a while now.



 

12 REPLIES 12
MA4
Senior

Did you try to set a breakpoint in HAL_SD_ErrorCallback to see if you have an error in hsd->ErrorCode ?

MA4
Senior

Is your buffer in an accessible memory ?

Screenshot 2025-09-08 at 10.23.15.png

rtonet
Associate II

Dear MA4,

My buffers are in RAM_D1. It seems that the problem is related to threads. When only the SD card thread is active, the system operates correctly, even when writing 4096 bytes every 2 milliseconds. I already tried setting the SDMMC interrupt priority higher than all the others, but it still does not solve the issue. By setting the SD card thread with the highest priority, I avoid errors. However, the rest of the system eventually fails.

I need to better understand the impacts of the other threads on the SD card operation and vice versa.