cancel
Showing results for 
Search instead for 
Did you mean: 

How to integrate SPI sd card on STM32L452CC?

MFrie.6
Associate III

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.

1 ACCEPTED SOLUTION

Accepted Solutions
MFrie.6
Associate III

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.

View solution in original post

19 REPLIES 19
HDaji.1
Senior

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

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
Up vote any posts that you find helpful, it shows what's working..

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

I have found it. thx.

Just read through stm32_adafruit_sd.c, unfortunately there is no file system. :sleepy_face:

Right, you need something like FatFS and a DISKIO layer calling the SDIO or SPI sector read/write functions ​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Not your board, but an example of using the Adafruit Shield with SPI connectivity to an SD Card

STM32Cube_FW_L0_V1.11.3\Projects\NUCLEO-L053R8\Applications\FatFs\FatFs_uSD_Standalone

STM32Cube_FW_L4_V1.14.0\Projects\NUCLEO-L452RE\Demonstrations\Adafruit_LCD_1_8_SD_Joystick

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Hi @Community member​ ,

Could you show me more info about the multi-sectors issues that you mentioned?

My problem may be due to those issues.

My total file size is about 11k and I can read back about 8.8k; after I doubled the sector size from 8k to 16k during formatting, my reading problem disappeared.

HDaji.1
Senior

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?

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
Up vote any posts that you find helpful, it shows what's working..