Unable to reliably use SD card with SDMMC2 using M4 core of NucleoH755-ZI
Hi there,
I am trying to read and write to an SD card using SDMMC2 from the M4 core on an STM32H755, using an NucleoH755-ZI and a custom SD card shield.
The shield connections are simple, short traces, and as follows:
SDMMC2_CK -> PD6
SDMMC2_CMD -> PA0
SDMMC2_D0 -> PB14
SDMMC_D1 -> PB15
SDMMC_D2 -> PB3
SDMMC_D3 -> PB4
- All SD card pins have 47k pullup resistors to 3v3, except for the clock line.
- The card is powered with 3v3.
- There is no external transceiver.
- I have removed Nucleo R75 to ensure PB14 is not connected to the Nucleo red LED.
- I have checked that Nucelo SB81 is not connected to ensure that PA0 is not connected to the Nucleo user switch.
- I have configured with project in CubeMX to have a sensible SDMMC clock (6MHz) and confirmed it with a scope.
Attached are pictures of my SDMMC2 settings, and my FatFS settings.
Here is the code I am using, taken from this tutorial by ST: https://www.youtube.com/watch?v=I9KDN1o6924
FRESULT res; /* FatFs function common result code */
uint32_t byteswritten, bytesread; /* File write/read counts */
uint8_t wtext[] = "Hellooo?"; /* File write buffer */
uint8_t rtext[_MAX_SS];/* File read buffer */
// f_mkfs((TCHAR const*)SDPath, FM_ANY, 0, rtext, sizeof(rtext));
if(f_mount(&SDFatFS, (TCHAR const*)SDPath, 0) != FR_OK)
{
Error_Handler();
}
else
{
FATFS* fs_ptr = &SDFatFS;
volatile uint32_t freeBytes;
if(f_open(&SDFile, "FILETEST0.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
{
Error_Handler();
}
else
{
//Write to the text file
res = f_write(&SDFile, wtext, strlen((char *)wtext), (void *)&byteswritten);
if((byteswritten == 0) || (res != FR_OK))
{
// Error_Handler();
}
else
{
f_close(&SDFile);
}
}
}There is ofcourse also the Cube generated code including:
- MX_SDMMC2_SD_Init();
- MX_FATFS_Init();
Problem 1
The SD card fails to initialise, resulting in a time-out.
This was eventually solved by adding a 1ms delay to line 3084 of stm32h7xx_hal_sd.c (in SD_PowerON()), in the while loop when determining the card's operating voltage. It is not filling me with confidence that a 1ms magically makes this works reliably, and does not work at all without it. While it seems to be fixed, perhaps this is indicative of a larger issue.
Problem 2
The card cannot be read or written.
inside f_open, find_volume (inside ff.c) always fails, returning 'FR_NO_FILESYSTEM'.
f_mount returns success with a delayed mount, but failuer with immediate mount option.
The 4GB SDHC card has been formatted with FAT (I have also tried FAT32), with an allocation size of 4096 ( I have also tried 1024, 2048, and 8192). I have also tried deleting the volume and using f_mkfs to format the SD card, as shown in the commented out line in the code above. The result is always the same.
I have also tried all of the above with another 2GB SD card. Both cards work fine on my PC.
Things I have tried, to no avail:
- Various SDMMC clock frequencies
- 10k external pullups on SD lines
- internal pullups on SD lines
- Checked the card detect function is working correctly
- Checked that SDMMC2 DMA and M4 both have access to all RAM regions
- Increasing _VOLUMES to 4 in ffconf.h
- Gone through various fatfs examples in the cube repository to try and see if I'm missing some code (such as 'sd_initialise()' that some seem to use.) No variations have made any difference.
- #define ENABLE_SD_DMA_CACHE_MAINTENANCE 1
So what could I be doing wrong? has anyone successfully used SDMMC2 with the H7's M4 core? Does cubeMX perhaps not configure the DMA properly?
Any help greatly appreciated.
Many thanks, Matt
