New SdFat optimized for Mega and Teensy 3.0

pito,

The SPI clock for SD.h on the Mega was 4 MHz. The standard Arduino SD library has no option for SPI clock speed.

I did a test at 8 MHz with SD.h by editing the source for begin(). Doubling the SPI clock speed increased the write speed from 265.95 KB/sec to 376.29 KB/sec and the read speed from 314.07 KB/sec to 463.86 KB/sec.

File size 5MB
Buffer size 4096 bytes
Starting write test. Please wait up to a minute
Write 376.29 KB/sec
Maximum latency: 78644 usec, Minimum Latency: 9808 usec, Avg Latency: 10873 usec

Starting read test. Please wait up to a minute
Read 463.86 KB/sec
Maximum latency: 9940 usec, Minimum Latency: 8784 usec, Avg Latency: 8823 usec

The SPI clock speed for Mega was 8 MHz with the new version of SdFat.

The SPI clock speed for Teensy 3.0 was 24 MHz.

The key to high speed writes is to use a record size that is a multiple of 512 bytes. This insures that very little data needs to be copied to the cache. In these tests with the new SdFat, only directory entries and the FAT table need to be cached.

Using a record size that is a power of two increases performance slightly. This insures that writes are aligned with file clusters.

It is very important to use a freshly formatted SD card so that a contiguous file is created. SD cards perform very poorly for random writes since the internal flash for most cards have a very large page size. The entire page must be rewritten to newly erased flash for a random write.

The new SdFat still has only one block of cache so overhead is dramatically increased if write data must be cached. This means that the cache block must be used for both write data and the FAT table.

I am considering options to use more cache but this adds complexity and there is still the overhead of copying data to the cache. Adding cache would not improve the above results but would help when record size is not a multiple of 512.