Electronic – How to figure out SD Card optimal write size

sdspi

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?

Best Answer

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.