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
2128void 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
5360void 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
6370void 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
7380void 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
8996void 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
105112void 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
122129void 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
134141void 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
192199void 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
231242void loop (){
0 commit comments