Skip to main content
brunoferren9
Associate III
March 13, 2018
Question

STM3210B-EVAL Mass storage - SD card example is too slow

  • March 13, 2018
  • 1 reply
  • 2202 views
Posted on March 13, 2018 at 13:19

I am currently using the STM3210B-EVAL Mass Storage example, which allow access of the SD card from a PC, through Mass Storage USB connection. The SD card is accessed through SPI. I'm using a 2GB microSD card, and I can successfully read and write files on it through Windows. So far so good.

However, I measure very poor write performances, something like 80 kB/s (writing a 10MB file takes approx 2 minutes). I measured up to 3MB/s writing performances on the same micro SD card, when used on a card reader, so the issue seems not to be that the card is too slow in itself.

Actually, I think I found out where most of the time is lost, but I fail to see why and how to correct this. Here is how it goes :

For each 512 bytes buffer to write, a write command (CMD24) is sent, followed by the 512 bytes of data. I modified the code and I use a DMA here so there is no wasted time. With a 12MHz clock on the SPI, this take approx 340ns. The SD card then send the 'data ok' ack (0x05, or 0xE5 masked), and then sends zeros (MISO line is low) while writing the data. And there is the issue : this takes something like 2 to 4 ms! Then the data line goes up again, indicating the end of the write process, and the next buffer is sent.

There is a little time lost elsewhere, but even if there were not, counting just these 2-4ms for each 512 bytes, I can't expect more than approx 150kB/s of writing speed. This is way too low.

I noticed the example code is really simple, with only a few commands used (CMD17 and 24 for read and write, CMD9, 10 and 13 for infos and status, CMD0 and 1 for startup, and that's it).

I'm not looking for maximum performances, I know the SPI bus will limit the max speed, but what I notice here seems in no way linked to the limitations of the SPI bus, but to the time the SD card takes to write a buffer.

How would you solve this ?

    This topic has been closed for replies.

    1 reply

    Tesla DeLorean
    Guru
    March 13, 2018
    Posted on March 13, 2018 at 15:09

    Single sector accesses will have a very high command processing overhead, you need to do multi-sector IO.

    Performance will be brutal at the f_write() level if you do a lot of small writes. You should manage your buffering to write blocks which are aligned to the sector/cluster boundaries. A buffer of 32KB will be near optimal.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    brunoferren9
    Associate III
    March 13, 2018
    Posted on March 13, 2018 at 15:41

    You mean using the CMD25 (WRITE_MULTIPLE_BLOCK) command ? That's indeed what I'm currently investigating.

    Tesla DeLorean
    Guru
    March 13, 2018
    Posted on March 13, 2018 at 16:28

    Yes

    https://community.st.com/0D50X00009XkYXdSAN

    But honestly why are you using a STM3210B and SPI? The part is like a decade old, if you must use an F103 series part pick one with SDIO.

    USB in your case is going to put a 700-800 KBps ceiling on things in the most optimumimplementation.

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