cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 and SDHC over SPI

longchair
Associate III
Posted on October 01, 2012 at 13:32

Hello,

Recently I need to use an SDHC in order to store some data for my application.

I have studied the documentation in order to find out some samples and came out with the following :

- sdio samples use SD cards but they require the sdio pins which are not available on my 64 pin LQFP stm32F103.

- spi_sd sample seem to be doing the spi communication. I have tried to use the sample code, connecting the SD to my controller on SPI1 (SCK, MISO, MOSI are connected to the SPI1 adequante pins), I have used PC10 to toggle the CS, and Power and GND ar directly connected to permanent supply.

I am having big diffuculties to get all this working together.

So first I have the following questions 

- Is there is a lib / code samples that I should use other than the samples in the stdperiph lib ?

- Is the spi_sd sample supposed to work with a regular SDHC card ? (I am using an olimex H103 board which has no specific SD features, but I have connected a 4Go SDHC Card to the proper pins mentionned above).

When using the spi_sd code sample, After the spi Init, the code will stop and wait un definitely on the code in    SD_WriteByte:

/*!< Wait until the transmit buffer is empty */

 

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

 

  {

 

  }

 

 

 

  /*!< Send the byte */

 

  SPI_I2S_SendData(SD_SPI, Data);

 

 

 

  /*!< Wait to receive a byte*/

 

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

 

  {

 

  }

The first while loop will never exit as if the transfer was not complete and I cannot figure out why. I have checked the pinout many times and it seems ok. Any other suggestions ?

Thanks in advance.

Lionel.

5 REPLIES 5
longchair
Associate III
Posted on October 01, 2012 at 21:00

Posted on October 01, 2012 at 21:22

SDHC cards are accessed via block address, not byte address. I think you should be reviewing the SDHC specs wrt to the MMC/SD specs to better understand the command, and functional differences, and how those might impact the implementation.

The SPI side is implemented in a couple of ST examples, including one for the L1 series eval boards. For the F2 and F4 there is an implementation for SDHC via SDIO, and those aren't particularly polished in their construction. I've done some porting to the STM32F4-Discovery board, and fixes a number of issues that come from MMC/SD/SDHC, and 32-bit addressing issues for cards >2GB.

You should digest all the examples.

You might also want to get some older cards, and get some functional experience with the sub 2GB cards, and the ones like the 128MB ones that shipped with some of the ST EVAL boards, and for which the code was targeted/tested. As observed before, these are particularly refined.

http://www.nerdkits.com/library/sdhc/

Martin Thomas has done a bunch of work in this area

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

There should also be dozen of blog posts about such things too.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
longchair
Associate III
Posted on October 02, 2012 at 23:03

Well I got it started.

I can read and write into the SDHC. The main issue was that the init sequence which is different between SD and SDHC.

Though I am having some wierd behaviour. The transfer rate seems to be quite low. When i use 1 block transfer (CMD24), and that I write 512 bytes blocks sequentially into the flash it takes quite a long time.

To give figure if i write 1024 blocks of 512 bytes in the flash in a loop and monitor the time spent it will take about 36 seconds ... which is quite bad transfer rate (14 Kbytes / sec).

This is wierd because, I am running the SPI with a prescaler of 2.

When I look at the sequence to see where the time is spent, I can see that after the CMD24 command, the wait for busy state to come down is where it takes the time. it can take up to 80ms.

I would like to know from you experience if this transfer rate is normal in SPI mode or if I should look for another issue.

I guess SDIO would go faster, but still I was expecting this SD card IO operations to be much faster in SPI mode.

Thanks for your enlightments :)

Lionel.

longchair
Associate III
Posted on October 03, 2012 at 21:22

Hello,

In order to clarify a bit my question I will state it differently :

- what speed would you expect to reach for wrtiing on a SDHC card in SPI Mode ?

- what would be the expected speed for the same thing in sdio mode ?

Thanks,

Lionel.
Posted on October 03, 2012 at 22:31

- what would be the expected speed for the same thing in sdio mode ?

STM32F4 USB-FS MSC SDIO MicroSD Card implementation

2GB Generic MicroSD Card 700 KBps Read, 430 KBps Write

16GB SDHC Ultra Card 710 KBps Read, 460 KBps Read

I haven't done equivalent testing using SPI or F1/L1 boards. At least some of the speed here is limited by the USB-FS 12 Mbps interface.

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