cancel
Showing results for 
Search instead for 
Did you mean: 

Example code for stm32f4 SD card read via SPI

Param sivan
Associate II
Posted on February 20, 2018 at 11:27

Hi,

Kindly help me to find out the example code for read/write the SD card via SPI. I am trying to write or copy the files both USB disk and/or SD card. Please advice.

https://community.st.com/tags♯/?tags=stm32f4%20debug%20systemworkbench

4spi &sharpstm32f4 hal driver 

https://community.st.com/tags♯/?tags=iar%20embedded%20workbench

‌

#stm32f4-debug-systemworkbench #sd-card-mmc-stm32cubemx #iar-embedded-workbench #sd-spi-sdio
12 REPLIES 12
Andrew Neil
Evangelist III
Posted on February 20, 2018 at 11:29

http://www.lmgtfy.com?q=SD+Card+SPI+STM32

Posted on February 20, 2018 at 12:28

There should be the AdaFruit shield code under the HAL BSP directories, and some FatFs code in the Application directories.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Andrew Neil
Evangelist III
Posted on February 20, 2018 at 12:38

http://elm-chan.org/fsw/ff/00index_e.html

 
Posted on February 20, 2018 at 15:38

Command level SPI SD routine

STM32Cube_FW_F4_V1.16.0\Drivers\BSP\Adafruit_Shield\stm32_adafruit_sd.c

Board level SPI SD support routines

STM32Cube_FW_F4_V1.16.0\Drivers\BSP\STM32F4xx-Nucleo\stm32f4xx_nucleo.c

Relatively portable across a number of STM32 platforms/families.

Pretty low performance, I recommend 4-bit SDIO to deliver any sort of usable speed.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on February 28, 2018 at 15:58

Thanks for the link. I had ported the code, but Icouldn't put the SD in the idle state (SD_GoIdleState). I am using SanDisk16 GBmicro sd card. I have tested the data signals of command bytes, it is sending perfectly. The response of sendSD_ANSWER_R1_EXPECTED is 224 not 0x Kindly help to troubleshoot this bug.

Please confirm,

SD_IO_CSState(1) means just HIGH the CS line , Is it? Since most of the data are written to the SD while the CS line is HIGH.

Thanks.

#stm32 spi

#sd%20card%20mmc%20stm32cubemx

#sd%20spi%20sdio

Posted on February 28, 2018 at 17:15

I'm afraid I can't provide resources to debug this, I can however confirm that the software, other than the multi-sector issue highlighted in another thread, does work properly for SDHC/XC cards.

SD_IO_CSState(1) drives the CS line High, while 

SD_IO_CSState(0) drives it low. The commands and responses are sent while the pin is low.  Some 0xFF sequences are sent with it high the clear the interface.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on March 07, 2018 at 14:57

Hi Clive,

I am getting FR_NOT_READY error after disk_initialize() function. I am testing on the Sandisk uSdHC- 16 GB card .

Posted on April 10, 2018 at 13:28

Hey Param, 

I want to do the same thing as you! Now I also run into problems when interacting to the SD card with the FatFs. 

Did you achieve to get it working? I did achieve to compile the project as described above but opening the SD card gives me an error. 

Kind regards 

Nico

Daniel Koster
Associate II
Posted on May 09, 2018 at 19:07

I wrote this program (Atollic Studio) for an STM32L4 and it worked for me (tested on 4GB and 16GB cards only).

Hope it helps!

https://embdev.net/topic/451203#5417501