mirror of
https://github.com/arduino/Arduino.git
synced 2025-02-19 13:54:23 +01:00
Combining MemoryCard and SdFat libraries into SD library.
Defaulting to CS pin 4 (for Arduino Ethernet Shield). Holding the SS pin high (for disabling W5100 on the Ethernet Shield).
This commit is contained in:
parent
676ca6aebc
commit
6e449d7f5b
@ -1,25 +0,0 @@
|
||||
/*
|
||||
|
||||
MemoryCard - a slightly more friendly wrapper for sdfatlib
|
||||
|
||||
This library aims to expose a subset of SD card functionality
|
||||
in the form of a higher level "wrapper" object.
|
||||
|
||||
License: GNU General Public License V3
|
||||
(Because sdfatlib is licensed with this.)
|
||||
|
||||
(C) Copyright 2010 SparkFun Electronics
|
||||
|
||||
*/
|
||||
|
||||
#ifndef __MEMORY_CARD_H__
|
||||
#define __MEMORY_CARD_H__
|
||||
|
||||
#include "MemoryCardDevice.h"
|
||||
|
||||
extern MemoryCardDevice MemoryCard;
|
||||
|
||||
// Preinstantiate required object
|
||||
MemoryCardDevice MemoryCard;
|
||||
|
||||
#endif
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
|
||||
MemoryCard - a slightly more friendly wrapper for sdfatlib
|
||||
SD - a slightly more friendly wrapper for sdfatlib
|
||||
|
||||
This library aims to expose a subset of SD card functionality
|
||||
in the form of a higher level "wrapper" object.
|
||||
@ -13,13 +13,13 @@
|
||||
|
||||
This library provides four key benefits:
|
||||
|
||||
* Including `MemoryCard.h` automatically creates a global
|
||||
`MemoryCard` object which can be interacted with in a similar
|
||||
* Including `SD.h` automatically creates a global
|
||||
`SD` object which can be interacted with in a similar
|
||||
manner to other standard global objects like `Serial` and `Ethernet`.
|
||||
|
||||
* Boilerplate initialisation code is contained in one method named
|
||||
`begin` and no further objects need to be created in order to access
|
||||
the memory card.
|
||||
the SD card.
|
||||
|
||||
* Calls to `open` can supply a full path name including parent
|
||||
directories which simplifies interacting with files in subdirectories.
|
||||
@ -50,10 +50,10 @@
|
||||
|
||||
*/
|
||||
|
||||
#include "MemoryCardDevice.h"
|
||||
#include "SD.h"
|
||||
|
||||
// Use this to configure the chip select pin of the SD card.
|
||||
#define SD_CARD_CHIP_SELECT_PIN 8 // For use with SparkFun MicroSD shield
|
||||
#define SD_CARD_CHIP_SELECT_PIN 4 // For use with Arduino Ethernet Shield
|
||||
|
||||
|
||||
// Used by `getNextPathComponent`
|
||||
@ -286,7 +286,7 @@ boolean callback_openPath(SdFile& parentDir, char *filePathComponent,
|
||||
Callback used to open a file specified by a filepath that may
|
||||
specify one or more directories above it.
|
||||
|
||||
Expects the context object to be an instance of `MemoryCard` and
|
||||
Expects the context object to be an instance of `SDClass` and
|
||||
will use the `file` property of the instance to open the requested
|
||||
file/directory with the associated file open mode property.
|
||||
|
||||
@ -298,7 +298,7 @@ boolean callback_openPath(SdFile& parentDir, char *filePathComponent,
|
||||
|
||||
*/
|
||||
if (isLastComponent) {
|
||||
MemoryCardDevice *p_MemoryCard = static_cast<MemoryCardDevice*>(object);
|
||||
SDClass *p_MemoryCard = static_cast<SDClass*>(object);
|
||||
p_MemoryCard->file.open(parentDir, filePathComponent,
|
||||
p_MemoryCard->fileOpenMode);
|
||||
// TODO: Return file open result?
|
||||
@ -309,9 +309,9 @@ boolean callback_openPath(SdFile& parentDir, char *filePathComponent,
|
||||
|
||||
|
||||
|
||||
/* Implementation of class used to create `MemoryCard` object. */
|
||||
/* Implementation of class used to create `SDCard` object. */
|
||||
|
||||
void MemoryCardDevice::begin() {
|
||||
void SDClass::begin() {
|
||||
/*
|
||||
|
||||
Performs the initialisation required by the sdfatlib library.
|
||||
@ -329,14 +329,14 @@ void MemoryCardDevice::begin() {
|
||||
}
|
||||
|
||||
|
||||
boolean MemoryCardDevice::open(char *filepath,
|
||||
boolean SDClass::open(char *filepath,
|
||||
boolean write, boolean append) {
|
||||
/*
|
||||
|
||||
Open the supplied file path for reading or writing.
|
||||
|
||||
The file content can be accessed via the `file` property of
|
||||
the `MemoryCardDevice` object--this property is currently
|
||||
the `SDClass` object--this property is currently
|
||||
a standard `SdFile` object from `sdfatlib`.
|
||||
|
||||
Defaults to read only.
|
||||
@ -375,7 +375,7 @@ boolean MemoryCardDevice::open(char *filepath,
|
||||
}
|
||||
|
||||
|
||||
boolean MemoryCardDevice::close() {
|
||||
boolean SDClass::close() {
|
||||
/*
|
||||
|
||||
Closes the file opened by the `open` method.
|
||||
@ -385,7 +385,7 @@ boolean MemoryCardDevice::close() {
|
||||
}
|
||||
|
||||
|
||||
boolean MemoryCardDevice::exists(char *filepath) {
|
||||
boolean SDClass::exists(char *filepath) {
|
||||
/*
|
||||
|
||||
Returns true if the supplied file path exists.
|
||||
@ -395,7 +395,7 @@ boolean MemoryCardDevice::exists(char *filepath) {
|
||||
}
|
||||
|
||||
|
||||
boolean MemoryCardDevice::exists(char *filepath, SdFile& parentDir) {
|
||||
boolean SDClass::exists(char *filepath, SdFile& parentDir) {
|
||||
/*
|
||||
|
||||
Returns true if the supplied file path rooted at `parentDir`
|
||||
@ -406,7 +406,7 @@ boolean MemoryCardDevice::exists(char *filepath, SdFile& parentDir) {
|
||||
}
|
||||
|
||||
|
||||
boolean MemoryCardDevice::makeDir(char *filepath) {
|
||||
boolean SDClass::makeDir(char *filepath) {
|
||||
/*
|
||||
|
||||
Makes a single directory or a heirarchy of directories.
|
||||
@ -416,3 +416,5 @@ boolean MemoryCardDevice::makeDir(char *filepath) {
|
||||
*/
|
||||
return walkPath(filepath, root, callback_makeDirPath);
|
||||
}
|
||||
|
||||
SDClass SD;
|
@ -17,9 +17,9 @@
|
||||
|
||||
#include <WProgram.h>
|
||||
|
||||
#include <SdFat.h>
|
||||
#include <utility/SdFat.h>
|
||||
|
||||
class MemoryCardDevice {
|
||||
class SDClass {
|
||||
|
||||
private:
|
||||
// These are required for initialisation and use of sdfatlib
|
||||
@ -62,4 +62,6 @@ class MemoryCardDevice {
|
||||
int fileOpenMode; // TODO: Don't make this public?
|
||||
};
|
||||
|
||||
extern SDClass SD;
|
||||
|
||||
#endif
|
@ -1,27 +1,25 @@
|
||||
/*
|
||||
|
||||
A sketch to demonstrate the features of the MemoryCard library.
|
||||
A sketch to demonstrate the features of the SD library.
|
||||
|
||||
*/
|
||||
|
||||
// At the moment you need to include both the SdFat and MemoryCard libraries.
|
||||
#include <SdFat.h>
|
||||
#include <MemoryCard.h>
|
||||
#include <SD.h>
|
||||
|
||||
void setup() {
|
||||
|
||||
Serial.begin(9600);
|
||||
|
||||
|
||||
// You always need to initialise the MemoryCard library
|
||||
Serial.println("Init MemoryCard...");
|
||||
MemoryCard.begin();
|
||||
// You always need to initialise the SD library
|
||||
Serial.println("Init SD...");
|
||||
SD.begin();
|
||||
|
||||
|
||||
// This demonstrates making a directory hierarchy
|
||||
Serial.println();
|
||||
Serial.println("Make directory...");
|
||||
MemoryCard.makeDir("/apple/banana/cabbage/");
|
||||
SD.makeDir("/apple/banana/cabbage/");
|
||||
|
||||
|
||||
// You can check for the existence of specific files/directories
|
||||
@ -32,7 +30,7 @@ void setup() {
|
||||
|
||||
Serial.print(filePathOne);
|
||||
Serial.print(" does ");
|
||||
if (MemoryCard.exists(filePathOne)) {
|
||||
if (SD.exists(filePathOne)) {
|
||||
Serial.println("exist.");
|
||||
} else {
|
||||
Serial.println("not exist.");
|
||||
@ -40,7 +38,7 @@ void setup() {
|
||||
|
||||
Serial.print(filePathTwo);
|
||||
Serial.print(" does ");
|
||||
if (MemoryCard.exists(filePathTwo)) {
|
||||
if (SD.exists(filePathTwo)) {
|
||||
Serial.println("exist.");
|
||||
} else {
|
||||
Serial.println("not exist.");
|
||||
@ -52,11 +50,11 @@ void setup() {
|
||||
Serial.println();
|
||||
Serial.println("Writing to 'dolphin.txt'.");
|
||||
|
||||
MemoryCard.open("/apple/banana/cabbage/dolphin.txt", true);
|
||||
SD.open("/apple/banana/cabbage/dolphin.txt", true);
|
||||
|
||||
MemoryCard.file.println("This line was appended to the file.");
|
||||
SD.file.println("This line was appended to the file.");
|
||||
|
||||
MemoryCard.close();
|
||||
SD.close();
|
||||
|
||||
|
||||
// Demonstrate writing to a file in the root directory and overwriting any
|
||||
@ -64,45 +62,45 @@ void setup() {
|
||||
Serial.println();
|
||||
Serial.println("Writing to 'top.txt'.");
|
||||
|
||||
MemoryCard.open("/top.txt", true, false);
|
||||
SD.open("/top.txt", true, false);
|
||||
|
||||
MemoryCard.file.println("This line overwrote the previous content of the file.");
|
||||
SD.file.println("This line overwrote the previous content of the file.");
|
||||
|
||||
MemoryCard.close();
|
||||
SD.close();
|
||||
|
||||
|
||||
// Demonstrate reading from a file in a subdirectory
|
||||
Serial.println();
|
||||
Serial.println("Reading 'dolphin.txt':");
|
||||
|
||||
MemoryCard.open("/apple/banana/cabbage/dolphin.txt");
|
||||
SD.open("/apple/banana/cabbage/dolphin.txt");
|
||||
|
||||
int c;
|
||||
|
||||
// This approach may be easier to follow
|
||||
while(true) {
|
||||
c = MemoryCard.file.read();
|
||||
c = SD.file.read();
|
||||
if (c < 0) {
|
||||
break;
|
||||
}
|
||||
Serial.print((char) c);
|
||||
}
|
||||
|
||||
MemoryCard.close();
|
||||
SD.close();
|
||||
|
||||
|
||||
// Demonstrate reading from a file in the root directory in a slightly different way
|
||||
Serial.println();
|
||||
Serial.println("Reading 'top.txt':");
|
||||
|
||||
MemoryCard.open("/top.txt");
|
||||
SD.open("/top.txt");
|
||||
|
||||
// This approach is more compact
|
||||
while((c = MemoryCard.file.read()) >= 0) {
|
||||
while((c = SD.file.read()) >= 0) {
|
||||
Serial.print((char) c);
|
||||
}
|
||||
|
||||
MemoryCard.close();
|
||||
SD.close();
|
||||
|
||||
|
||||
// Demonstration complete!
|
@ -227,6 +227,7 @@ uint8_t Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) {
|
||||
#ifndef SOFTWARE_SPI
|
||||
// SS must be in output mode even it is not chip select
|
||||
pinMode(SS_PIN, OUTPUT);
|
||||
digitalWrite(SS_PIN, HIGH); // disable any SPI device using hardware SS pin
|
||||
// Enable SPI, Master, clock rate f_osc/128
|
||||
SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR1) | (1 << SPR0);
|
||||
// clear double speed
|
Loading…
x
Reference in New Issue
Block a user