1
\$\begingroup\$

I am writing the data logging portion for a real time embedded application. I have an SD card I communicate via SPI at 33Mhz. I need to be able to log at least 1024 bytes of data ever 1 milli-second. I have tried sending the pre-erase command with 2 blocks erased and then sending the multi-write command with 2 blocks but this takes longer than 1 milli-second. My SD card is rated at 20MB/s so it should be good for 20KB/ms at optimal speeds.

I have tried reading the SD card specification to see how to calculate the optimal write size or sector boundaries and can't tell exactly what to do. Should I be writing in multiples of the sector size at blocks that are multiples of the sector size?

\$\endgroup\$
1
\$\begingroup\$

I have tried reading the SD card specification

The document will not give you much information about performance tuning - including block size effect on the performance. For compatibility reasons (and for some other reasons) use 512 byte block size.

My SD card is rated at 20MB/s

Rated for which operations - or is it maximal SPI communication speed? Let's do simple math: you drive card at 33 MHz, without considering the control overhead each clock cycle is one bit, thus to transfer a byte in SPI mode (single wire) you will need 8 clocks, thus 33/8 = 4,125 million bytes per second.

enter image description here Source, page A-2.

Thus ideally SPI allows you transferring 1024 bytes in 248 us, which seem to be well below 1 ms.

However card's performance depends on some other things than SPI bus speed. It has its internal controller, which can be a bottleneck (sometimes - e.g. when CRC error or any other exception occurs, or always because it is slow), memory array with its timing can be a bottleneck. The actual speed of the cards can differ dramatically, thus you must dig into the specifications of the card deeper to find out read and write speeds, or better consult with manufacturer on what card may suit you and your pattern of access.

Example: Altera serial flash device I used recently (EPCS16) - same marking and same component source - earlier device erases sector within 1 second, later device erases it within 200 ms. This is dramatic performance improvement. The same for SD-cards - their timing may vary a lot. I also have heard that performance depends on the size of the card - bigger cards are slower, but I can not comment on it - just keep this possibility in mind.

\$\endgroup\$
1
\$\begingroup\$

I've used SD cards in a few projects as a maker, and one thing I've learned is that the SPI access mechanism - in contrast to the more modern 4 bit bus - is generally going to perform poorly compared to the card's specifications. I learned this through multiple iterations of my Orthrus project, the last of which used an ATSAMS17 chip with its HSMCI interface. Orthrus was doing crypto, and only single block I/O, but even then the results I get are better than SPI even taking into account the 4 bit versus 1 bit per clock difference.

To get the specs they promise, you need to negotiate very high clock speeds and reduced (1.8v) power, none of which are available via SPI.

\$\endgroup\$
0
\$\begingroup\$

fat16lib in the Arduino forum has done lots with SD cards, and has found 512 bytes to be optimal.

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.