Skip to main content
MFrie.6
Associate II
May 2, 2023
Solved

How to integrate SPI sd card on STM32L452CC?

  • May 2, 2023
  • 10 replies
  • 5269 views

I have a custom board with the l452cc controller. I am trying to run an sd card with fatfs through spi. I have generated cubemx fatfs files, but there is no interface layer for spi to fatfs. Are there some examples I can reference off of, or what is the best way to approach this?

Thanks.

This topic has been closed for replies.
Best answer by MFrie.6

Ok so it was a driver problem. I haven't pinned it down yet, but it has to do with the dma, because when I use normal spi mode, it will do a basic read write. So the process to get an sd card working is this:

Generate fatfs files with cubemx

Copy BSP package in from the adafruit sd shield example

Write a spi driver, and tie it into the BSP package

In theory it should then work. I may have missed a few details, so feel free to ask if you have any questions.

10 replies

HDaji.1
Senior
May 5, 2023

I followed this link (https://embetronicx.com/tutorials/microcontrollers/stm32/stm32-sd-card-interfacing-with-example/), and have made my F411 custom board to read/write SD card through SPI.

However, there is an issue with reading when file size is large than 10k; strangely there is no problem in writing.

More details: I could write 1000+ float number to a file in SD card, but only read 900+ back before running into FR_INT_ERR.

Is there any sample code provided by STM?

I can only find one with SDMMC (https://www.youtube.com/watch?v=I9KDN1o6924).

Tesla DeLorean
Guru
May 5, 2023

ST has provided an SPI example via a driver for an Adafruit shield, it's in the BSP of most Cube trees. I think I've reported some multi-sector issues in the past​.

The 10KB threshold doesn't ring any bells here.​

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
HDaji.1
Senior
May 5, 2023

@Community member​ Can you be more specific on where I can find that sample code?

I have found it. thx.

HDaji.1
Senior
May 8, 2023

In here (http://elm-chan.org/docs/fat_e.html), it is mentioned that sector size (BPB_BytsPerSec) must be the same as the sector size of the storage contains the FAT volume. How come none of the online tutorials never mention this?

Tesla DeLorean
Guru
May 8, 2023

SD Cards are pretty much universally 512-byte sector devices, FATFS should be able to deal with this.

Formatting in an optimal way should have clusters fall on Erase Block boundaries, these are quite large. I wouldn't use MKFS on SD Cards, leave the cards as delivered or use the SD Card Industry formatter for optimal alignment of structures.

For using QSPI NOR FLASH the use of 4KB physical sectors is STRONGLY recommended as it significantly simplifies the erase/write operation, and you don't have to juggle and deblock the data operations.

I don't recall the exact details of the bugs, I posted them to the forum eons ago, I typically don't use SPI, it's very slow and all the STM32 I'm using support SDIO/SDMMC at a peripheral level.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
HDaji.1
Senior
May 9, 2023

Is FatFS able to handle if SD card's sector size is not 512?

MFrie.6
MFrie.6Author
Associate II
May 8, 2023

Thanks for the input, I'll see how far I get and ask back if I have questions. Thanks

HDaji.1
Senior
May 9, 2023

If you make it work, please also post your tech details here.

MFrie.6
MFrie.6Author
Associate II
May 9, 2023

Well I'm making some progress. I implimented the NUCLEO-L452RE example project basically. 'BSP_SD_INIT()' returns success. f_mount however returns 'FR_NO_FILESYSTEM'. Sd card is a 16GB formatted with exFat currently. I have tried formatting it with FAT32 as well, with no success. Is this a low level problem, or is there something fundamentally wrong with my implimentation. Here is the section of applicable code.

static void SDCard_Config(void) { 
 /* Initialize the SD */
 if (BSP_SD_Init() != MSD_OK) {
#if DEBUG
 debug_printf("BSP_SD_INIT_FAILED\r\n");
#endif
 } else {
#if DEBUG
 debug_printf("BSP_SD_INIT_SUCCESS\r\n");
#endif
 }
}

void MX_FATFS_Init(void)
{
 /*## FatFS: Link the USER driver ###########################*/
 retUSER = FATFS_LinkDriver(&SD_Driver, USERPath);
 
 /* USER CODE BEGIN Init */
 /* additional user code for init */
 /* USER CODE END Init */
}
MX_FATFS_Init();
 SDCard_Config();
 FIL fil; /* File object */
 char line[100]; /* Line buffer */
 FRESULT fr; /* FatFs return code */
 
 /* Give a work area to the default drive */
 fr = f_mount(&SD_FatFs, "", 1);
 if (fr) {
 __NOP();
 }
 
 /* Open a text file */
 fr = f_open(&fil, "message.txt", FA_READ);
 if (fr) {
 __NOP();
 }
 
 /* Read every line and display it */
 while (f_gets(line, sizeof line, &fil)) {
 debug_printf(line);
 }
 
 /* Close the file */
 f_close(&fil);

Thanks

MFrie.6
MFrie.6Author
Associate II
May 9, 2023

Here is a logic capture of the above code. You will have to rename 'SD_init.sal.txt' to 'SD_init.sal', thanks to a forum quirk.

MFrie.6
MFrie.6Author
Associate II
May 9, 2023

So apparently 'Check boot record signature' is what is firing the error.

if (ld_word(fs->win + BS_55AA) != 0xAA55) return 3;	/* Check boot record signature (always placed here even if the sector size is >512) */

It would almost seem like I have a spi driver problem, but if I look in 'fs->win' there appears to be some random data lines 440 to 461 in the sector. So not sure if it is a driver problem or not.

MFrie.6
MFrie.6AuthorBest answer
Associate II
May 9, 2023

Ok so it was a driver problem. I haven't pinned it down yet, but it has to do with the dma, because when I use normal spi mode, it will do a basic read write. So the process to get an sd card working is this:

Generate fatfs files with cubemx

Copy BSP package in from the adafruit sd shield example

Write a spi driver, and tie it into the BSP package

In theory it should then work. I may have missed a few details, so feel free to ask if you have any questions.

HDaji.1
Senior
May 10, 2023

Are you able to read/write file with large size, like a few tens or even hundreds bytes?

How do you format the SD card? What is the sector size?

MFrie.6
MFrie.6Author
Associate II
May 10, 2023

I haven't tried much besides a basic hello world text file. I will see here at some point. The card is formatted with FAT, and I have tried exFat too and that works. I will post back after I have tried some bigger files.

MFrie.6
MFrie.6Author
Associate II
May 10, 2023

Well I don't know if this gets what you are wanting to know but here is a piece of code that I ran.

 BYTE buf[2048];
 memset(buf, 0x4c, sizeof(buf));
 UINT br, bw;
 fr = f_write(&fil, buf, sizeof(buf), &bw);

The file when I look at it is filled with 0x4c, so it seems like it is working.