Skip to content

Commit bd529a0

Browse files
authored
Merge pull request #188 from pennam/TestSDCARD
Update TestSDCARD example
2 parents c315f5f + 4e5ecb5 commit bd529a0

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed
+34-11
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,59 @@
1+
/*
2+
Portenta - TestSDCARD
3+
4+
The sketch shows how to mount an SDCARD and list its content.
5+
6+
The circuit:
7+
- Portenta H7 + Vision Shield
8+
- Portenta H7 + Portenta Breakout
9+
10+
This example code is in the public domain.
11+
*/
112
#include "SDMMCBlockDevice.h"
213
#include "FATFileSystem.h"
314

415
SDMMCBlockDevice block_device;
516
mbed::FATFileSystem fs("fs");
617

718
void setup() {
8-
// put your setup code here, to run once:
9-
delay(2000);
19+
Serial.begin(9600);
20+
while (!Serial);
21+
22+
Serial.println("Mounting SDCARD...");
1023
int err = fs.mount(&block_device);
1124
if (err) {
1225
// Reformat if we can't mount the filesystem
1326
// this should only happen on the first boot
14-
printf("No filesystem found, formatting... ");
15-
fflush(stdout);
27+
Serial.println("No filesystem found, formatting... ");
1628
err = fs.reformat(&block_device);
1729
}
30+
if (err) {
31+
Serial.println("Error formatting SDCARD ");
32+
while(1);
33+
}
34+
1835
DIR *dir;
1936
struct dirent *ent;
20-
printf("try to open dir\n");
37+
int dirIndex = 0;
38+
39+
Serial.println("List SDCARD content: ");
2140
if ((dir = opendir("/fs")) != NULL) {
22-
/* print all the files and directories within directory */
41+
// Print all the files and directories within directory (not recursively)
2342
while ((ent = readdir (dir)) != NULL) {
24-
printf ("%s\n", ent->d_name);
43+
Serial.println(ent->d_name);
44+
dirIndex++;
2545
}
2646
closedir (dir);
2747
} else {
28-
/* could not open directory */
29-
printf ("error\n");
48+
// Could not open directory
49+
Serial.println("Error opening SDCARD\n");
50+
while(1);
51+
}
52+
if(dirIndex == 0) {
53+
Serial.println("Empty SDCARD");
3054
}
3155
}
3256

3357
void loop() {
34-
// put your main code here, to run repeatedly:
35-
58+
// Empty
3659
}

0 commit comments

Comments
 (0)