cancel
Showing results for 
Search instead for 
Did you mean: 

STM32H755ZI: Garbled/Chinese Characters Printed When Using FATFS with SDMMC + DMA on SD Card

Daniel_Ismail
Associate II

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:

1. MCU and Core Context

  • MCU: STM32H755ZIT6

  • Core used for SD card operations: Cortex-M7

2. SDMMC1 Peripheral Configuration

  • 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

3. Clock Configuration

  • 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

4. FATFS Middleware Settings (For Cortex-M7)

  • 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)

5. MPU and Cache Configuration

  • 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)

6. NVIC Settings

  • SysTick Timer Priority: 14

  • SDMMC1 IRQ Priority: 14

7. Linker Settings

  • Minimum Heap: 0x400

  • Minimum Stack: 0x800
    (for both M7 and M4)

8. Code Behavior

Location: CM7/Core/main.c

9. Key Operations:

  • 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().

 

SD Card Info

  • 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
NVIC.jpg

 

FTFS
FTFS.jpg

 

 

 

 

 

 

 

 

 

 

Cortex M7
CORTEX M7.jpg

 

 

 

 

 

 

 

 

 

 

Request for Help

Could someone from ST or the community please:

  1. Review my configuration and highlight any potential issues?

  2. Suggest any MPU/cache settings or APIs I might be missing (e.g., cache invalidation)?

  3. 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)?

  4. 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!

2 REPLIES 2
mƎALLEm
ST Employee

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.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

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.