Using sdfatlib CS pin defaults. SD.begin() returns success or failure.

This commit is contained in:
David A. Mellis 2010-11-20 13:50:45 -05:00
parent 6f0ea10600
commit d05a57af19
4 changed files with 16 additions and 15 deletions

View File

@ -319,21 +319,17 @@ boolean callback_remove(SdFile& parentDir, char *filePathComponent,
void SDClass::begin(uint8_t csPin) {
boolean SDClass::begin(uint8_t csPin) {
/*
Performs the initialisation required by the sdfatlib library.
Does not return if initialisation fails.
Return true if initialization succeeds, false otherwise.
*/
// TODO: Allow chip select pin to be supplied?
if (!(card.init(SPI_HALF_SPEED, csPin)
&& volume.init(card) && root.openRoot(volume))) {
while (true) {
// Bail
}
}
return card.init(SPI_HALF_SPEED, csPin) &&
volume.init(card) &&
root.openRoot(volume);
}

View File

@ -20,9 +20,6 @@
#include <utility/SdFat.h>
#include <utility/SdFatUtil.h>
// Use this to configure the chip select pin of the SD card.
#define SD_CARD_CHIP_SELECT_PIN 4 // For use with Arduino Ethernet Shield
class File : public Stream {
public:
virtual void write(uint8_t);
@ -47,7 +44,7 @@ private:
public:
// This needs to be called to set up the connection to the SD card
// before other methods are used.
void begin(uint8_t csPin = SD_CARD_CHIP_SELECT_PIN);
boolean begin(uint8_t csPin = SD_CHIP_SELECT_PIN);
// Open the specified file/directory with the supplied mode (e.g. read or
// write, etc). Returns a File object for interacting with the file.

View File

@ -6,7 +6,11 @@ void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
SD.begin();
// On the Ethernet Shield, CS is pin 4.
if (!SD.begin(4)) {
Serial.println("failed!");
return;
}
Serial.println("done.");
if (SD.exists("example.txt")) Serial.println("example.txt exists.");

View File

@ -6,7 +6,11 @@ void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
SD.begin();
// On the Ethernet Shield, CS is pin 4.
if (!SD.begin(4)) {
Serial.println("failed!");
return;
}
Serial.println("done.");
f = SD.open("test.txt", true, false);