|
| 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 | +*/ |
1 | 12 | #include "SDMMCBlockDevice.h"
|
2 | 13 | #include "FATFileSystem.h"
|
3 | 14 |
|
4 | 15 | SDMMCBlockDevice block_device;
|
5 | 16 | mbed::FATFileSystem fs("fs");
|
6 | 17 |
|
7 | 18 | 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..."); |
10 | 23 | int err = fs.mount(&block_device);
|
11 | 24 | if (err) {
|
12 | 25 | // Reformat if we can't mount the filesystem
|
13 | 26 | // this should only happen on the first boot
|
14 |
| - printf("No filesystem found, formatting... "); |
15 |
| - fflush(stdout); |
| 27 | + Serial.println("No filesystem found, formatting... "); |
16 | 28 | err = fs.reformat(&block_device);
|
17 | 29 | }
|
| 30 | + if (err) { |
| 31 | + Serial.println("Error formatting SDCARD "); |
| 32 | + while(1); |
| 33 | + } |
| 34 | + |
18 | 35 | DIR *dir;
|
19 | 36 | struct dirent *ent;
|
20 |
| - printf("try to open dir\n"); |
| 37 | + int dirIndex = 0; |
| 38 | + |
| 39 | + Serial.println("List SDCARD content: "); |
21 | 40 | 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) |
23 | 42 | while ((ent = readdir (dir)) != NULL) {
|
24 |
| - printf ("%s\n", ent->d_name); |
| 43 | + Serial.println(ent->d_name); |
| 44 | + dirIndex++; |
25 | 45 | }
|
26 | 46 | closedir (dir);
|
27 | 47 | } 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"); |
30 | 54 | }
|
31 | 55 | }
|
32 | 56 |
|
33 | 57 | void loop() {
|
34 |
| - // put your main code here, to run repeatedly: |
35 |
| - |
| 58 | + // Empty |
36 | 59 | }
|
0 commit comments