cancel
Showing results for 
Search instead for 
Did you mean: 

FatFS SDIO DMA write speed

pbox
Associate II
Posted on July 23, 2014 at 14:28

Hello.

I'm trying to get more SD card write performance with STM32F207ZET6 + SDIO + DMA + FatFS.

I'm using Kingston 8GB MicroSDHC 4 class. I formatted it with SD Formatter tool. File system FAT32 with 32KB clusters.

Write speed on PC with USB card reader is 5 MB/s.

PCB design seems fine:

1. 33k pull-ups for all SD card lines except CLK.

2. 3.3V powered SD card and decoupled with 100n ceramic in parallel with 4u7 ceramic X7R and 68u tantalum.

3. Short PCB traces from uC to SD card (less then 30mm).

With 32KB write buffer I can get only 1.66 MB/s write speed.

I tried few different FatFS + STM32 + SDIO + DMA implementation:

1. This code from clive1:

https://docs.google.com/file/d/0B7OY5pub_GfIY01DaHY4OVp4NUk/edit?pli=1

2. This code from Nemui:

http://nemuisan.blog.bai.ne.jp/?eid=192848

3. STM32CubeF2 FatFs_uSD code example:

stm32cubef2\STM32Cube_FW_F2_V1.1.0\Projects\STM322xG_EVAL\Applications\FatFs\FatFs_uSD

It's all configured as SDIO + DMA + 120MHz SYSCLK + 48MHz SDIOCLK + 24MHz SDIO_CK + 4-bit bus wide.

And all this gave the same write speed test result - 1.66 MB/s with 32KB buffer.

I measured write speed with this code:

&sharpdefine SD_WRITE_BUF_SIZE 32768

    // Test write

    uint8_t sd_buf_write[SD_WRITE_BUF_SIZE];

    uint32_t cnt = 0;

    uint32_t i = 0;

    while (i < SD_WRITE_BUF_SIZE)

    {

        sd_buf_write[i++] = (cnt>>24)&0x0FF;

        sd_buf_write[i++] = (cnt>>16)&0x0FF;

        sd_buf_write[i++] = (cnt>>8)&0x0FF;

        sd_buf_write[i++] = (cnt>>0)&0x0FF;

        cnt++;

    }

    for (;;)

    {

        rc = f_write(&Fil, sd_buf_write, SD_WRITE_BUF_SIZE, &bw);

        if (bw<SD_WRITE_BUF_SIZE) while(1);

        if (rc) while(1);

        rc = f_sync(&Fil);

        if (rc) while(1);

    }

    

I started it for 100 seconds and calculated the write speed value as file_size/100.

I got fastest write speed with 3*32KB buffer - 2.8 MB/s.

I read on this forum that clive managed to get up to 10 MB/s write speed with fewer buffer size.

What could be the the bottleneck in my case?

#dma #fatfs #sdio
5 REPLIES 5
Posted on July 23, 2014 at 15:00

Given your thoroughness I'm going to believe your numbers, the PC number might be overly optimistic due to caching and lazy writing, unless you used a specific SD card speed testing app.

I suspect your problem is the card. There is a lot of junk out there, Class 4 doesn't really impart any particular speed guarantee. Writing is generally slower, but some cards are significantly worse than others. I would look for some Class 10 cards that claim 30, 40, or 80 MBps type performance. I've personally had a lot of success with SanDisk Ultra type cards, generally you can find them at less than $1 per GB.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on July 23, 2014 at 15:05

Don't fsync() the file, the fwrite() will commit the data to the card. You should fclose() or fsync() the file when you are done and you want it to commit the FAT and directory sectors. Ideally you should cache those until you've actually finished otherwise you'll burn up the card.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
pbox
Associate II
Posted on July 23, 2014 at 16:22

Thanks, Clive!

Frequent fsync was the case. I need to write fast adc data flow to SD card from device power on to device power off so I need to periodically execute fsync.

I don't really need to sync every write so now I changed code to sync every 16MB and I got write speed 4.5 MB/s with 32KB buffer.

I tested this SD card on PC with just file copy so yes, it may be questionable.

I tried to test it with CrystalDiskMark now and got this results:

-----------------------------------------------------------------------

CrystalDiskMark 3.0.3 (C) 2007-2013 hiyohiyo

                           Crystal Dew World : http://crystalmark.info/

-----------------------------------------------------------------------

* MB/s = 1,000,000 byte/s [SATA/300 = 300,000,000 byte/s]

           Sequential Read :    19.393 MB/s

          Sequential Write :     5.210 MB/s

         Random Read 512KB :    19.087 MB/s

        Random Write 512KB :     0.589 MB/s

    Random Read 4KB (QD=1) :     2.761 MB/s [   674.0 IOPS]

   Random Write 4KB (QD=1) :     0.005 MB/s [     1.3 IOPS]

   Random Read 4KB (QD=32) :     0.000 MB/s [     0.0 IOPS]

  Random Write 4KB (QD=32) :     0.000 MB/s [     0.0 IOPS]

  Test : 100 MB [G: 0.0% (0.0/7452.0 MB)] (x5)

  Date : 2014/07/23 21:21:10

    OS : Windows 7 Professional SP1 [6.1 Build 7601] (x86)

francescatodiego
Associate II
Posted on July 23, 2014 at 16:28

SD card register CSD define rd  speed and wr speed for card

https://www.sdcard.org/downloads/pls/simplified_specs/part1_410.pdf pag 114

R2W_FACTOR field define max write speed for SD card

read here:

http://goughlui.com/?p=5497 you can decode the CSD register value online

(select the link CSD version1 or CSD version2 and copy the CSD register value )

You can also try with the values in the page

pbox
Associate II
Posted on July 23, 2014 at 17:19

This does not apply for SDHC and SDXC cards, they have fixed  100ms/250ms read/write timeouts.