How to Decrease the write time for SD card FATFs?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to Decrease the write time for SD card FATFs?

13,297 Views
Amit_Kumar1
Senior Contributor II

Hi

I am using K60 100 Mhz uc with cw10.6. I want to write around 50 bytes of data 50 times a second. using FATFs PE component. I am writing the data to the file using fwrite function. I noticed It takes around 2.10 ms to write, obviously it is very less but due to this I am not able to get proper 50 Hz writing frequency. some times 49 times the data is written and some times 50 times. Without write command I am able to toggle the GPIO pin and observe that the rest of the program is working properly and data is being stored to the buffer at 50 times a second. on activating write function there is inconsistency in the writing rate.  following is the waveform which i observed during write operation.

Bit1_SetVal();

SD_Write();

Bit1_ClrVal();


TEK0004.JPG.jpg


Kind Regards

Amit Kumar

Tags (5)
5 Replies

4,064 Views
Masmiseim
Senior Contributor I

Hey,

I have done a similar application.

I had to write six Bytes 2000 times a second. It works very good, but you should never write directly. I had implement a FiFo which collects the Data and works also as a synchronization Buffer when the SD-Card does some internal Garbage-Collection which results in a temporary bigger delay.

It is also good to write bigger packages because it is a lot faster. I used 4096 Bytes in my application.

Regards

0 Kudos
Reply

4,064 Views
Amit_Kumar1
Senior Contributor II

Hi Markus

Thanks for the reply. Did you use FATfs ?

Kind Regards

Amit Kumar

0 Kudos
Reply

4,064 Views
Masmiseim
Senior Contributor I

Hey,

Yes I did use FATfs with a SDIO connection. You can see the results of my speed tests in this thread:

https://community.freescale.com/thread/99202

Regards

Markus

0 Kudos
Reply

4,064 Views
mjbcswitzerland
Specialist V

Hi

Always write to FAT using 512 byte multiples (eg. collect the data in a buffer of thsi length or a multiple of it and only write when filled). Use the buffer to also buffer periods where the SD card write takes a long time (it is doing internal house keeping and so somethimes will slow for a short time - often writes will take about 2..4ms but occasionally jump to 200ms or even longer for one or two write cycles).

This will allow about 800kByte/s write throughput with FAT on about any card, whereby FAT will be based on sector writes (thus the 512 byte unit).

To increase to up to around 4MBytes/s throughput it is necessary to use block writes rather than sector writes (not usually supported byte FAT implementations) but again some (or more) RAM buffering is required to bridge the occasional SD card slow down.

Regards

Mark

4,064 Views
BlackNight
NXP Employee
NXP Employee

The the low level access to the SD card (SPI) is heavily depending on the SPI clock speed, and the SD card itself.

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

I have seen slow SD cards which need up to 200 ms for a write operation, and even if you buy some consumer cards from the same brand, you might see variations.

See as well

http://elm-chan.org/fsw/ff/en/appnote.html

So not sure if you are using an expensive industrial SD card or a consumer SD card. And my experience is that some old (512 MByte) cards are sometimes better than the new big cards.

Additionally, writing performance might depend on the sector/area you are writing. So ultimately you do not have a guranteed and always the same writing time.

Additionally, writing the small chunks of data with 50 Hz sounds not economical to me. In my applications I'm not writing from an interrupt (that would be bad anyway). Instead, I buffer the data to be written and then write it say once a second. That greatly reduces the overhead of the system, as I can write data in blocks of 500 bytes or more.

I hope this helps,

Erich