Skip to content

Commit a51348b

Browse files
committed
rewrite sd card driver
1 parent cbd73bf commit a51348b

File tree

16 files changed

+1043
-842
lines changed

16 files changed

+1043
-842
lines changed

examples/SD/ReadWrite/ReadWrite.ino

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,57 +25,62 @@
2525
#endif
2626
#include <Seeed_FS.h>
2727
#include "SD/Seeed_SD.h"
28+
#define SERIAL Serial
2829

29-
#define csPin 29
30+
#define csPin 4
31+
#ifdef ARDUINO_ARCH_SAMD
32+
#undef SERIAL Serial
33+
#define SERIAL SerialUSB
34+
#endif
3035

3136
File myFile;
3237

3338
void setup() {
34-
// Open serial communications and wait for port to open:
35-
Serial.begin(9600);
36-
while (!Serial) {
37-
; // wait for serial port to connect. Needed for native USB port only
39+
// Open SERIAL communications and wait for port to open:
40+
SERIAL.begin(115200);
41+
while (!SERIAL) {
42+
; // wait for SERIAL port to connect. Needed for native USB port only
3843
}
3944

4045

41-
Serial.print("Initializing SD card...");
46+
SERIAL.print("Initializing SD card...");
4247

4348
if (!SD.begin(csPin)) {
44-
Serial.println("initialization failed!");
49+
SERIAL.println("initialization failed!");
4550
while (1);
4651
}
47-
Serial.println("initialization done.");
52+
SERIAL.println("initialization done.");
4853

4954
// open the file. note that only one file can be open at a time,
5055
// so you have to close this one before opening another.
5156
myFile = SD.open("test.txt", FILE_WRITE);
5257

5358
// if the file opened okay, write to it:
5459
if (myFile) {
55-
Serial.print("Writing to test.txt...");
60+
SERIAL.print("Writing to test.txt...");
5661
myFile.println("testing 1, 2, 3.");
5762
// close the file:
5863
myFile.close();
59-
Serial.println("done.");
64+
SERIAL.println("done.");
6065
} else {
6166
// if the file didn't open, print an error:
62-
Serial.println("error opening test.txt");
67+
SERIAL.println("error opening test.txt");
6368
}
6469

6570
// re-open the file for reading:
6671
myFile = SD.open("test.txt");
6772
if (myFile) {
68-
Serial.println("test.txt:");
73+
SERIAL.println("test.txt:");
6974

7075
// read from the file until there's nothing else in it:
7176
while (myFile.available()) {
72-
Serial.write(myFile.read());
77+
SERIAL.write(myFile.read());
7378
}
7479
// close the file:
7580
myFile.close();
7681
} else {
7782
// if the file didn't open, print an error:
78-
Serial.println("error opening test.txt");
83+
SERIAL.println("error opening test.txt");
7984
}
8085
}
8186

examples/SD/SD_Test/SD_Test.ino

Lines changed: 78 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -16,128 +16,135 @@
1616
#include <Seeed_FS.h>
1717
#include "SD/Seeed_SD.h"
1818

19-
#define csPin 29
19+
#define SERIAL Serial
20+
21+
#define csPin 4
22+
#ifdef ARDUINO_ARCH_SAMD
23+
#undef SERIAL Serial
24+
#define SERIAL SerialUSB
25+
#endif
26+
2027

2128
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
22-
Serial.print("Listing directory: ");
23-
Serial.println(dirname);
29+
SERIAL.print("Listing directory: ");
30+
SERIAL.println(dirname);
2431

2532
File root = fs.open(dirname);
2633
if(!root){
27-
Serial.println("Failed to open directory");
34+
SERIAL.println("Failed to open directory");
2835
return;
2936
}
3037
if(!root.isDirectory()){
31-
Serial.println("Not a directory");
38+
SERIAL.println("Not a directory");
3239
return;
3340
}
3441

3542
File file = root.openNextFile();
3643
while(file){
3744
if(file.isDirectory()){
38-
Serial.print(" DIR : ");
39-
Serial.println(file.name());
45+
SERIAL.print(" DIR : ");
46+
SERIAL.println(file.name());
4047
if(levels){
4148
listDir(fs, file.name(), levels -1);
4249
}
4350
} else {
44-
Serial.print(" FILE: ");
45-
Serial.print(file.name());
46-
Serial.print(" SIZE: ");
47-
Serial.println(file.size());
51+
SERIAL.print(" FILE: ");
52+
SERIAL.print(file.name());
53+
SERIAL.print(" SIZE: ");
54+
SERIAL.println(file.size());
4855
}
4956
file = root.openNextFile();
5057
}
5158
}
5259

5360
void createDir(fs::FS &fs, const char * path){
54-
Serial.print("Creating Dir: ");
55-
Serial.println(path);
61+
SERIAL.print("Creating Dir: ");
62+
SERIAL.println(path);
5663
if(fs.mkdir(path)){
57-
Serial.println("Dir created");
64+
SERIAL.println("Dir created");
5865
} else {
59-
Serial.println("mkdir failed");
66+
SERIAL.println("mkdir failed");
6067
}
6168
}
6269

6370
void removeDir(fs::FS &fs, const char * path){
64-
Serial.print("Removing Dir: ");
65-
Serial.println(path);
71+
SERIAL.print("Removing Dir: ");
72+
SERIAL.println(path);
6673
if(fs.rmdir(path)){
67-
Serial.println("Dir removed");
74+
SERIAL.println("Dir removed");
6875
} else {
69-
Serial.println("rmdir failed");
76+
SERIAL.println("rmdir failed");
7077
}
7178
}
7279

7380
void readFile(fs::FS &fs, const char * path){
74-
Serial.print("Reading Dir: ");
75-
Serial.println(path);
81+
SERIAL.print("Reading Dir: ");
82+
SERIAL.println(path);
7683
File file = fs.open(path);
7784
if(!file){
78-
Serial.println("Failed to open file for reading");
85+
SERIAL.println("Failed to open file for reading");
7986
return;
8087
}
8188

82-
Serial.print("Read from file: ");
89+
SERIAL.print("Read from file: ");
8390
while(file.available()){
84-
Serial.write(file.read());
91+
SERIAL.write(file.read());
8592
}
8693
file.close();
8794
}
8895

8996
void writeFile(fs::FS &fs, const char * path, const char * message){
90-
Serial.print("Writing file: ");
91-
Serial.println(path);
97+
SERIAL.print("Writing file: ");
98+
SERIAL.println(path);
9299
File file = fs.open(path, FILE_WRITE);
93100
if(!file){
94-
Serial.println("Failed to open file for writing");
101+
SERIAL.println("Failed to open file for writing");
95102
return;
96103
}
97104
if(file.print(message)){
98-
Serial.println("File written");
105+
SERIAL.println("File written");
99106
} else {
100-
Serial.println("Write failed");
107+
SERIAL.println("Write failed");
101108
}
102109
file.close();
103110
}
104111

105112
void appendFile(fs::FS &fs, const char * path, const char * message){
106-
Serial.print("Appending to file: ");
107-
Serial.println(path);
113+
SERIAL.print("Appending to file: ");
114+
SERIAL.println(path);
108115

109116
File file = fs.open(path, FILE_APPEND);
110117
if(!file){
111-
Serial.println("Failed to open file for appending");
118+
SERIAL.println("Failed to open file for appending");
112119
return;
113120
}
114121
if(file.print(message)){
115-
Serial.println("Message appended");
122+
SERIAL.println("Message appended");
116123
} else {
117-
Serial.println("Append failed");
124+
SERIAL.println("Append failed");
118125
}
119126
file.close();
120127
}
121128

122129
void renameFile(fs::FS &fs, const char * path1, const char * path2){
123-
Serial.print("Renaming file ");
124-
Serial.print(path1);
125-
Serial.print(" to ");
126-
Serial.println(path2);
130+
SERIAL.print("Renaming file ");
131+
SERIAL.print(path1);
132+
SERIAL.print(" to ");
133+
SERIAL.println(path2);
127134
if (fs.rename(path1, path2)) {
128-
Serial.println("File renamed");
135+
SERIAL.println("File renamed");
129136
} else {
130-
Serial.println("Rename failed");
137+
SERIAL.println("Rename failed");
131138
}
132139
}
133140

134141
void deleteFile(fs::FS &fs, const char * path){
135-
Serial.print("Deleting file: ");
136-
Serial.println(path);
142+
SERIAL.print("Deleting file: ");
143+
SERIAL.println(path);
137144
if(fs.remove(path)){
138-
Serial.println("File deleted");
145+
SERIAL.println("File deleted");
139146
} else {
140-
Serial.println("Delete failed");
147+
SERIAL.println("Delete failed");
141148
}
142149
}
143150

@@ -160,19 +167,19 @@ void testFileIO(fs::FS &fs, const char * path){
160167
len -= toRead;
161168
}
162169
end = millis() - start;
163-
Serial.print(flen);
164-
Serial.print(" bytes read for ");
165-
Serial.print(end);
166-
Serial.println(" ms");
170+
SERIAL.print(flen);
171+
SERIAL.print(" bytes read for ");
172+
SERIAL.print(end);
173+
SERIAL.println(" ms");
167174
file.close();
168175
} else {
169-
Serial.println("Failed to open file for reading");
176+
SERIAL.println("Failed to open file for reading");
170177
}
171178

172179

173180
file = fs.open(path, FILE_WRITE);
174181
if(!file){
175-
Serial.println("Failed to open file for writing");
182+
SERIAL.println("Failed to open file for writing");
176183
return;
177184
}
178185

@@ -182,31 +189,35 @@ void testFileIO(fs::FS &fs, const char * path){
182189
file.write(buf, 512);
183190
}
184191
end = millis() - start;
185-
Serial.print( 2048 * 512);
186-
Serial.print(" bytes read for ");
187-
Serial.print(end);
188-
Serial.println(" ms");
192+
SERIAL.print( 2048 * 512);
193+
SERIAL.print(" bytes read for ");
194+
SERIAL.print(end);
195+
SERIAL.println(" ms");
189196
file.close();
190197
}
191198

192199
void setup(){
193-
Serial.begin(115200);
194-
if(!SD.begin(csPin)){
195-
Serial.println("Card Mount Failed");
200+
SERIAL.begin(115200);
201+
pinMode(5, OUTPUT);
202+
digitalWrite(5, HIGH);
203+
while(!SERIAL){};
204+
while(!SD.begin(csPin, SPI, 12500000)){
205+
SERIAL.println("Card Mount Failed");
196206
return;
197207
}
208+
198209
uint8_t cardType = SD.cardType();
199210

200211
if(cardType == CARD_NONE){
201-
Serial.println("No SD card attached");
212+
SERIAL.println("No SD card attached");
202213
return;
203214
}
204215

205216
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
206217

207-
Serial.print("SD Card Size: ");
208-
Serial.print(cardSize);
209-
Serial.println("MB");
218+
SERIAL.print("SD Card Size: ");
219+
SERIAL.print((uint32_t)cardSize);
220+
SERIAL.println("MB");
210221

211222
listDir(SD, "/", 0);
212223
createDir(SD, "/mydir");
@@ -220,12 +231,12 @@ void setup(){
220231
renameFile(SD, "/hello.txt", "/foo.txt");
221232
readFile(SD, "/foo.txt");
222233
testFileIO(SD, "/test.txt");
223-
Serial.print("Total space: ");
224-
Serial.print(SD.totalBytes() / (1024 * 1024));
225-
Serial.println("MB");
226-
Serial.print("Used space: ");
227-
Serial.print(SD.usedBytes() / (1024 * 1024));
228-
Serial.println("MB");
234+
SERIAL.print("Total space: ");
235+
SERIAL.print((uint32_t)SD.totalBytes() / (1024 * 1024));
236+
SERIAL.println("MB");
237+
SERIAL.print("Used space: ");
238+
SERIAL.print((uint32_t)SD.usedBytes() / (1024 * 1024));
239+
SERIAL.println("MB");
229240
}
230241

231242
void loop(){

0 commit comments

Comments
 (0)