Monday 15 July 2013

Accessing SD Cards from Microcontrollers

This post reviews the available choices for reading or writing FAT-formatted SD cards from Arduino and Energia over SPI. All of the libraries mentioned here support only 8.3 file-names.

Arduino ships with a library called SD, which is a wrapper (by Adafruit) around the more-comprehensive SdFat library (by William Greiman). Features of this library are:
  • FAT16 and FAT32 support on both standard and high-capacity SD cards
  • File: create, read, write, delete and truncate
  • Directory: create, read, write and delete
  • Multiple volume/partition support
The same author also provides a smaller fat16lib with features:
  • FAT16 support on standard SD cards only
  • File: create, read and write
  • Root-directory support only
Another choice for Arduino is tinyFAT (by Henning Karlsen). Its features are:
  • FAT16 on SD cards
  • File: create, read, write, rename, delete (ASCII and Binary I/O)
  • Root-directory support only, only one file open at a time
Energia, on the other hand ships with a C++ wrapper around a lower-level C-library (by ChaN) called FatFs. This is aimed at embedded systems generally and has broadly similar functionality to SdFat but also supports:
  • Multiple underlying storage technologies (e.g., SD, ATA, USB and NAND)
  • File-system creation
  • Permissions and timestamps
Petit FatFs is a subset of FatFs for 8-bit microcontrollers with limited RAM. Its features are:
  • FAT16 and FAT32 support
  • File: open, read, streaming-read, seek
  • Directory: read
  • Single volume and single file
Petit FatFs also allows features which are not required to be conditionally-compiled out (e.g., FAT32 and directory support). It also allows other libraries' low-level SPI functions to be used, to reduce code-size.

Since FatFs and Petit FatFs were originally Arduino libraries, they can be used on both Energia and Arduino. The converse is not true of FatFs.

An important point which is not obvious is the RAM footprint required. With the sole exception of Petit FatFS, all of the libraries mentioned allocate a static 512-byte buffer --- this is the sector-size of an SD card. 

1 comment: