The native Arduino SD library was add to 022 as a wrapper for SdFat to make access to SD cards simpler.
I have updated SdFat to make it easier to use, added almost all features of the native SD library plus many new features.
Error handling has been improved and SPI access to the SD card has been rewritten. Try running the examples below with no SD card to see a sample message.
Typical sketches in the new SdFat have fewer line of code than the native SD library. Sketches generally take a few bytes less flash than the native library even with the new features and error handling.
The new library can be downloaded here Google Code Archive - Long-term storage for Google Code Project Hosting..
I have ported two example from the native SD library as an illustration. I would appreciate any comments on the new SdFat library so I can improve it.
Here is the DumPFile example:
/*
SD card file dump - modified for the ne SdFat library by Bill Greiman
This example shows how to read a file from the SD card using the
new SdFat library and send it over the serial port.
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created 22 December 2010
modified Apr 11 2011 by Bill Greiman
This example code is in the public domain.
*/
#include <SdFat.h>
SdFat sd; // Almost the same object as SD in the native SD library
SdFile dataFile; // file to read from
// On the Ethernet Shield, CS is pin 4. SdFat handles setting SS
const int chipSelect = 4;
void setup() {
Serial.begin(9600);
// initialize SdFat or print a detailed error message and halt
// Use half speed like native library.
// change to SPI_FULL_SPEED for more performance.
if (!sd.init(SPI_HALF_SPEED, chipSelect))sd.initErrorHalt();
// open data file
if (!dataFile.open("datalog.txt", O_READ)) {
sd.errorHalt("error opening datalog.txt");
}
int data;
// copy file to Serial
while ((data = dataFile.read()) >= 0) Serial.write(data);
}
void loop() {}
Here is the ReadWrite.pde example:
/*
SD card read/write - modified by Bill Greiman for SdFat
This example shows how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created Nov 2010
by David A. Mellis
updated 2 Dec 2010
by Tom Igoe
modified by Bill Greiman 11 Apr 2011
This example code is in the public domain.
*/
#include <SdFat.h>
SdFat sd;
SdFile myFile;
// On the Ethernet Shield, CS is pin 4. SdFat handles setting SS
const int chipSelect = 4;
void setup() {
Serial.begin(9600);
// Initialize SdFat or print a detailed error message and halt
// Use half speed like native library.
// change to SPI_FULL_SPEED for more performance.
if (!sd.init(SPI_HALF_SPEED, chipSelect)) sd.initErrorHalt();
// open the file for write at end like the Native SD library
if (!myFile.open("test.txt", O_WRITE | O_CREAT | O_APPEND)) {
sd.errorHalt("error opening test.txt for write");
}
// if the file opened okay, write to it:
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
// re-open the file for reading:
if (!myFile.open("test.txt", O_READ)) {
sd.errorHalt("error opening test.txt for read");
}
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
int data;
while ((data = myFile.read()) >= 0) Serial.write(data);
// close the file:
myFile.close();
}
void loop() {
// nothing happens after setup
}