2025-05-09 1:38 AM - edited 2025-05-09 8:15 AM
Hi ST Community,
I'm working on an STM32H755ZIT6 project where I'm interfacing an SD card using SDMMC1 + DMA and FATFS (with exFAT) from the Cortex-M7 core. However, I'm facing a strange issue: the first time the board runs after programming, it writes and reads English text correctly to/from the SD card, but after one or two resets, the file content becomes garbled or shows Chinese-like characters instead of English. I suspect this may be cache, MPU, or alignment-related, but I'm not sure what I’m missing.
Here is a detailed overview of my configuration and setup:
MCU: STM32H755ZIT6
SDMMC Mode: SD 4-bit Wide Bus
SDMMC1 Clock Divide Factor: 0x04 (Targeting 50 MHz actual clock)
SDMMC1 Interrupt: Enabled, priority set in NVIC to 14
SYSCLK = 400 MHz
CPU1_CLK (M7) = 400 MHz
CPU2_CLK (M4) = 200 MHz
PCLK = 200 MHz
SDMMC1_CLK = 200 MHz
Actual SDMMC1 Clock = 200 MHz / 4 = 50 MHz
FATFS enabled under FATFS_M7
Filesystem: exFAT
LFN Mode: Static allocation, work buffer in .bss
Enabled DMA Template
Platform setting:
Card Detect Pin: PA0 (SDMMC1_uSD_DETECT)
CPU I-Cache: Enabled
CPU D-Cache: Enabled
MPU Region 1 (AXI SRAM Region):
Base Address: 0x24000000
Size: 512KB
Access: Full access
TEX: Level 1
Shareable: Disabled
Cacheable: Disabled
Bufferable: Disabled
(MPU Region 0 is default)
SysTick Timer Priority: 14
SDMMC1 IRQ Priority: 14
Minimum Heap: 0x400
Minimum Stack: 0x800
(for both M7 and M4)
Location: CM7/Core/main.c
Mount filesystem
Format (optional)
Write simple text: "[CORE_CM7]: Hello FATFS"
Read back to an aligned 32-byte buffer rtext[64]
Compare with original
All file I/O is wrapped with error checks and logs printed over UART using printf().
Capacity: 128GB
Format: exFAT with 32KB cluster size
CODE:
ALIGN_32BYTES(uint8_t rtext[64]); // Read buffer, 32-byte aligned
uint8_t workBuffer[_MAX_SS]; // Work buffer for exFAT formatting
int main(void)
{
printf("[CORE_CM7]: Program Starting... \r\n");
// Perform file operations
FS_FileOperations();
}
static void FS_FileOperations(void)
{
FRESULT res;
uint32_t bytesWritten, bytesRead;
uint8_t wtext[] = "[CORE_CM7]: Hello FATFS";
// Mount the SD card
res = f_mount(&SDFatFS, (TCHAR const*)SDPath, 0);
if (res != FR_OK)
{
printf("[CORE_CM7/FatFs]: Failed to mount SD card (error: %d)\n", res);
Error_Handler();
}
// Open file for writing
res = f_open(&SDFile, "TEST.TXT", FA_CREATE_ALWAYS | FA_WRITE);
if (res != FR_OK)
{
printf("[CORE_CM7/FatFs]: Failed to open file for writing (error: %d)\n", res);
Error_Handler();
}
// Write to file
res = f_write(&SDFile, wtext, sizeof(wtext), (void*)&bytesWritten);
if (res != FR_OK || bytesWritten != sizeof(wtext))
{
printf("[CORE_CM7/FatFs]: Write failed (error: %d, bytes written: %lu)\n", res, bytesWritten);
Error_Handler();
}
f_close(&SDFile);
// Open file for reading
res = f_open(&SDFile, "STM32.TXT", FA_READ);
if (res != FR_OK)
{
printf("[CORE_CM7/FatFs]: Failed to open file for reading (error: %d)\n", res);
Error_Handler();
}
// Read from file
res = f_read(&SDFile, rtext, sizeof(wtext), (void*)&bytesRead);
if (res != FR_OK || bytesRead != bytesWritten)
{
printf("[CORE_CM7/FatFs]: Read mismatch (error: %d, read: %lu, expected: %lu)\n", res, bytesRead, bytesWritten);
Error_Handler();
}
f_close(&SDFile);
// Compare buffers
if (Buffercmp(rtext, wtext, bytesWritten) == 0)
{
printf("[CORE_CM7/FatFs]: Data written and read are identical\n");
}
else
{
printf("[CORE_CM7/FatFs]: Data mismatch – printed text appears garbled\n");
Error_Handler();
}
}
static uint8_t Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint32_t BufferLength)
{
while (BufferLength--)
{
if (*pBuffer1 != *pBuffer2)
return 1;
pBuffer1++;
pBuffer2++;
}
return 0;
}
--------------------------------------------------
NVIC
FTFS
Cortex M7
Could someone from ST or the community please:
Review my configuration and highlight any potential issues?
Suggest any MPU/cache settings or APIs I might be missing (e.g., cache invalidation)?
Advise whether switching from DMA to MDMA would improve performance or stability in this context, and if so, how should I configure MDMA for SDMMC1 (step-by-step or with CubeMX guidance)?
If possible, share a working example or reference for STM32H7 + SDMMC + FATFS + DMA or MDMA, including MPU/cache setup and buffer alignment practices?
Any help would be greatly appreciated!
2025-05-09 7:33 AM
Hello @Daniel_Ismail ,
Please use </> button to share a code. Please, refer to this post. I'm editing your post.
Thank you for your understanding.
2025-05-09 8:13 AM
Thank you, I’ll make sure to use the code block feature moving forward. By the way, I’d really appreciate it if you could share any feedback or thoughts on the post.
2025-05-10 8:17 AM
Hi,
Just to see, cache is a problem: switch off D cache or not enable it.
And try setting higher priority for sdmmc, I use 3 , try.
And is sdmmc working fine, without DMA?
2025-05-10 9:22 AM - edited 2025-05-10 9:22 AM
Hello, I didn't run any DMA, so having D cache on wouldn't affect it. SDMMC is working, but not exactly how it should as its printing texts in Chinese.
2025-05-16 7:37 AM
Hello @Daniel_Ismail
Please ensure that you hardware setup is set correctly.
Use logic analyser to probe the data on the bus.
2025-05-16 10:35 AM
Hello @Saket_Om
Thank you for the response. I have thoroughly double-checked the hardware setup and verified all wiring connections one by one. Interestingly, when I use my own implementation of the code, the setup works as expected. This suggests that the issue might be related to the code or configuration in the provided example rather than the hardware itself.
Appreciate your support.
2025-05-16 1:25 PM
The hardware, and the SDMMC / Card are agnostic to what the 8-bit byte contains. It doesn't care nor change Chinese characters or representations.
FATFS might need to accommodation for CODEPAGEs and legal characters you can put in short file names, and perhaps the Unicode in long file names. Check LFN is enabled.
Figure out and illustrate WHAT it is you're complaining about so we can focus on what the problem is, and where you should be looking.
2025-05-16 2:08 PM
Hi @Tesla DeLorean , thank you for your response. LFN is enabled, and I've also enabled exFAT. To clarify the issue: I'm writing an English string for instance,
[CORE_CM7]: Hello FATFS
to the text file, but when I open the file on my PC, the content appears as Chinese characters instead of the original English text.
2025-05-16 2:52 PM
That's kind of indicative that you're failing at the DISKIO Read / Write functionality
If you've heavily validated the Read, prior to Write, you can do a lot of testing without damaging the card content or structures. Best to do the FIRST as it eliminates a lot of bugs / failures later.
I suggest writing a large pseudo-random data sequence on the card as a file, and writing equivalent f_read() code on the STM32, and confirm the file is 100% readable, end-to-end and repeatably.
Dump your test pattern on the STM32, and have a Hex Dumping or Viewing tool on the PC so you can inspect the data. Not as ASCII.
If you're Writing junk, consider why.
Create a test using f_write() which does the equivalent pseudo-random pattern writing.
Check or CRC on the PC side, see if any pattern to the failure. ie bits set, bytes/bits shifted.