You are viewing an older revision! See the latest version
SD Card File System
A library to allow SD Cards to be accessed as a filesystem, using a SPI interface.
Hello World!¶
Import program
00001 // example writing to SD card, sford 00002 00003 #include "mbed.h" 00004 #include "SDFileSystem.h" 00005 00006 SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board 00007 00008 int main() { 00009 printf("Hello World!\n"); 00010 00011 mkdir("/sd/mydir", 0777); 00012 00013 FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); 00014 if(fp == NULL) { 00015 error("Could not open file for write\n"); 00016 } 00017 fprintf(fp, "Hello fun SD Card World!"); 00018 fclose(fp); 00019 00020 printf("Goodbye World!\n"); 00021 }
Library¶
The SDFileSystem library, for accessing SD Cards using fopen, fprintf, etc.
Details¶
SD Cards are widely used by loads of devices for storage; phones, mp3 players, pc's etc. That means they are a very cheap option for storing large amounts of non-volatile data (i.e. the data is not lost when the power is removed). They should be ideal for data logging and storing audio/images.
SD and MMC cards support various protocols, but common to them all is one based on SPI. This is the one used here as, whilst not being the most high performance, it uses a generic SPI interface so will be more portable.
SD Cards are block devices. That means you read/write data in multiples of the block size (usually 512-bytes); the interface is basically "read from block address n", "write to block address m". Note that a filesystem (e.g. FAT) is an abstraction on top of this, and the disk itself knows nothing about the filesystem.
Based on a SparkFun MicroSD Breakout Board, here is the wiring that will work (you can obviously use either SPI port, and any DigitalOut):

SparkFun MicroSD Breakout Board MicroSD Breakout mbed CS o-------------o 8 (DigitalOut cs) DI o-------------o 5 (SPI mosi) VCC o-------------o VOUT SCK o-------------o 7 (SPI sclk) GND o-------------o GND DO o-------------o 6 (SPI miso) CD o
Reference¶
- SD Card Specification
- Notes on the development of this library: SDCards!