cancel
Showing results for 
Search instead for 
Did you mean: 

Driver to use MicroSD card with SPI

ronandouguet
Associate II
Posted on March 07, 2012 at 11:12

Hi,

I develop a project on the STM32F4 discovery board and I want use a MicroSD card. I work with Atollic TrueStudio for STM32.

So I search a driver to use my MicroSD card with SPI. I have an API to FATFs and an API to configure the SPI module but I haven't the driver to use MicroSD card.

My goal is to achieve a datalogger.

Thanks for your help.
23 REPLIES 23
antti23
Associate II
Posted on March 08, 2012 at 10:56

eLua for STM32F4 includes spi mode sd driver.. that supposedly works

on our testing however it did not work, we are not investigating it any more as we need the real SD card support not spi mode

Antti

antti23
Associate II
Posted on March 08, 2012 at 15:31

ok SD card things work, but need to disable GCC optimization to get it working

just an update

Antti

zaurozavr
Associate II
Posted on March 08, 2012 at 16:50

To work with uSD by SPI no extra driver required.

Make your board SPI ports & pins initialization, and add to the FATFS diskio:

for transmission:

#define

xmit_spi(dat)   SD_SPI->DR =(dat);

while

((SD_SPI->SR & SPI_I2S_FLAG_TXE) == RESET);\

while

(SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_BSY) == SET);

 

 

for reception:

#define

rcvr_spi_m(dst)   SD_SPI->DR=0xFF;

while

((SD_SPI->SR & SPI_I2S_FLAG_RXNE) == RESET);\*(dst)=SD_SPI->DR;

while

(SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_BSY) == SET)

 

 

and

static 

BYTE

rcvr_spi (

void

)

 

{

 

SD_SPI->

DR

= 0xFF;

 

while

(SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_RXNE) ==

RESET

);

 

while

(SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_BSY) ==

SET

);

 

return

SD_SPI->

DR

;

 

}

Of course, add some code to the FATAFS diskio FCLK_FAST(),  FCLK_SLOW() etc.

And DO not forget to skip CRC Error in  xmit_datablock (), as STM ERRATA says. A lot of people have overlooked this.

After that all works fine. 

PS. Speaking ''as STM ERRATA says'' i mean not the exatly case for SDIO IF but recommendations how to avoid the problem.
ronandouguet
Associate II
Posted on March 09, 2012 at 14:52

Thanks for this response !

but

I still can

not

run

my sd card

:(

I found the driver elua (elua_mmc.c) but my sd card work on SDC mode and no MMC.

I also test the project by Martin Thomas (

http://www.siwawi.arubi.uni-kl.de/avr_projects/arm_projects/arm_memcards/index.html#stm32_memcard

) but it doesn't work on my card !!

I think I can create my own driver :(  because I find difficult to understand the library and I

can not

make it work

!!

antti23
Associate II
Posted on March 10, 2012 at 13:16

we are soon porting the SD mode driver for eLua, keep tuned I post the link to our project soon.

We got the SD mode stuff working after disabling compiler optimization so its only a matter of adjusting some files to get it done for lua

zaurozavr
Associate II
Posted on March 10, 2012 at 14:26

I really can't understand what kind of driver you are speaking and looking for.

SPI is the simpliest communication protocol, especially without DMA.

what do you need to drive SD by SPI:

HW

1) properly connect signal lines MOSI,MISO,SCLK (and SS or any GPIO for SD CS)

2) Set pullup resistors (at least to  MOSI and MISO lines), optionally decoupling capacitor between power lines close to card

SW

1) initialize SPI with a standard peripheral driver

2) enable SPI

Then, select SD.

To write data to :

1) SD_SPI -> DR = data;

wait for the end of transmission:  

2) while (SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_TXE) == RESET);

when all bytes send wait for BSY flag:

3) while (SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_BSY) == SET);

4)deselect SD.

If you don't use the highest speed, you can skip waiting for BSY flag and even TXE. But better keep them at place.

To read data from SD:

Send dummy byte:

1) SD_SPI -> DR = 0xff;

wait for the end of transmission:  

2) while (SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_RXNE) == RESET);

get the data:

data = SD_SPI -> DR;

if needed, check BSY flag.

That's all.

Due to SD is a block device, I showed  some macros for FATFS to read from/write to SD in my previous post.

But what does your valuable driver do?

antti23
Associate II
Posted on March 10, 2012 at 15:23

and I do not understand to whom you reply... :(

eLua includes built in support for SD over SPI, there is no code for SD hardware.

so there is some porting needed.. or maybe you did not reply to me at all.

we have 0% interest in SD over SPI at all.. (its TOO SIMPLE :)

Antti

zaurozavr
Associate II
Posted on March 10, 2012 at 15:37

Mainly I'm speaking to topic starter.

He said that his projet uses  FATFS, where proper low-level SD card initialization occurs. (though it is nothing more than several lines of code) and SPI uSD.

In this case you should provide some hardware-depended procedures to FATFS, like SPI low clock setting procedure (400KHz or less) for SD initialization.

  

ronandouguet
Associate II
Posted on March 21, 2012 at 19:15

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6ko&d=%2Fa%2F0X0000000buf%2FuE7InGrfB5fpMl1Px7CXj7k8dvYdW8w7CnM4s24pxXs&asPdf=false