STM32 and SD card (FATFS and SPI)

Blaitox picture Blaitox · Feb 19, 2017 · Viewed 25.7k times · Source

I'm starting to work with a stm32l0 board and it was fine until I had some more interesting functions to use. I never read SD card out of windows, I found Pins links and started to read about SPI which could be a suitable solution for our prject (I think).

But the issue is in the programming part, every tutorial I find skips basics. I uses STM32CubeMX to generate code with HAL and FATFS but I don't know how to initialize and then use my SD card. I try to paste code I find but I can't debug it then and there is no point just pasting someone's code.

Thanks and sorry for my grammar,

EDIT : I came here after this post : link I'm now trying to access a SDcard with a stm32l0 controller. I've been advised to use FATFS to read the device but what I understood is that the library needs a way to access the data : SPI. I selected FATFS and SPI2 in STM32CubeMX and imported the project in Eclipse (I'm using HAL and the SW4STM32). On the pin description I saw MISO,CK,MOSI in CubeMX which were on pins PB12,PB13,PB14. But in the main file those pins are never used.
Another thing affraid me in the post I mentionned : the SDcard needs to be initialized in a way a never heard of.
I don't know if you need to have the SPI_Init and GPIO_Init functions, I will edit this message if you need it.
I will edit anyway in the morning because the post must be so confuzed, my brain is burning up i'm sorry. Ask if you need precision on whatever. Thanks !

Answer

SamR picture SamR · Feb 20, 2017

If you use CubeMx and configure SPI to access SD card, the pins are automatically configured in your code. Look at the definitions/implementations of the first few functions in your generated main.c.

If you do not have any experience in STM32 and its HAL drivers, first try to understand how the things stack up.

The quickest way to do that is to try an example.

All your CubeMx Firmware libraries are located at

C:/Users/UserName/STM32Cube/Repository/STM32Cube_FW_XX

Go to Projects>Examples folder (may not be exact - but paths are similar) and you will find the examples.