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

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:

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:

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?

Again, thanks to the wonders of digital photography, this has all been solved long ago: the FAT file system became famous in MS-DOS, but is in fact an evolution of the CP/M 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:

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:

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 512-bytes into sd.buf:

: 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:

But hey, it’s a start and it makes it possible to exchange data in terms of files on an SD card!

Weblog © Jean-Claude Wippler. Generated by Hugo.