SD cards with FAT files Mar 2017
Those little plastic µSD-to-SD card adapters, of which you may have a bunch lying around since they are often included with new µSD cards, make excellent µSD card sockets:
There are many libraries (in C for the Arduino, for example) which support accessing SD cards from an embedded µC. These all rely on a well-known feature of these cards of supporting SPI:
The only tricky bit is getting them into that mode. For this, the SPI clock has to be temporarily lowered to 100..400 KHz, and then a few magic pin toggles and byte sends will do the rest.
There is a superb description of all the details at www.elm-chan.org.
And now, there is an sdcard.fs package in the Embello repository which implements this for Mecrisp Forth. Keep in mind that it’s young and still has a few weak spots:
- it assumes SPI1 is used, and has hard-wired the necessary clock slow-down
- it has only been tested on 2 GB µSD cards (4 GB and above will probably not work)
But apart from that, it works like a charm. Just call
sd-init to initialise
SPI and connect to the card. After that, you get three simple words to use it:
sd-sizewill return the size of the card (in 512-byte blocks)
sd-readtakes a block number and reads that block into a buffer called
sd-writealso takes a block number and writes
sd-bufto the SD card
Reading and writing each take 1 to 2 ms - and that’s all there is to it!
But one of the key attractions of SD cards, is that they can make a very easy data interchange possible with the “bigger” computers out there: no doubt also due to digital cameras (for SD) and mobile phones (for µSD), many modern laptops now include an SD card slot.
And this is where it gets complicated… how do you treat a bunch of blocks as a file-system?
FAT was introduced some 40 years ago and then evolved to VFAT (supporting larger disks), LFN (removing the 8.3 filename restriction), and ending with exFAT (which feels more like an attempt to secure a new licensing model than anything else), 10 years ago.
As it turns out, if we keep things simple, even a µC can handle such a file system with ease.
The sdcard.fs package mentioned earlier has been extended to support reading and writing files under the following constraints:
- the SD card must be formatted as FAT16 (the default for storage media up to 2 GB)
- LFNs are not supported, but pose no problems: the special entries are ignored
- subdirectories are ignored, only files in root can be accessed by the current code
- only existing files up to 8 MB can be read and written
- files can be read and written, but they cannot grow or shrink
- in other words: we can easily overwrite existing data blocks, but not much more
This may all seem a bit limiting, but there are cases where that’s already sufficient to be of use. And of course anyone is free to take the code and extend it with more powerful capabilities.
Again, a few Forth words is all it takes to expose this functionality:
sd-mounttakes and initialized SD card and analyses the file system for further use
sd-mount.(note the extra dot) does the same, but also prints out some details
ls- displays a list of all entries in the root directory
fat-findfinds an entry by name, and returns its starting cluster
fat-chainstores the file access map in one of a small number of “open file slots”
fat-maptakes a logical file block and slot, and returns a physical SD block number
The following example inits the SD card, mounts the FAT file system, looks up a
file called “READ.ME”, opens the file chain as slot 3, and reads block #0 of
: fatdemo ( -- ) sd-init sd-mount s" READ ME " drop fat-find 3 fat-chain 0 3 fat-map sd-read ;
This really is a very rudimentary implementation at the moment:
- you have to know how long the file is, the code does not keep track of the end
- filenames have to be passed as fixed-size 11-byte strings that omit the dot
- there are only 4 slots, i.e. at most 4 open files can be used at any poin in time
- there’s almost no error checking: it’s best to treat this as proof-of-concept for now
But hey, it’s a start and it makes it possible to exchange data in terms of files on an SD card!