Skip to content

Update TestSDCARD example #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions libraries/Portenta_SDCARD/examples/TestSDCARD/TestSDCARD.ino
Original file line number Diff line number Diff line change
@@ -1,36 +1,59 @@
/*
Portenta - TestSDCARD

The sketch shows how to mount an SDCARD and list its content.

The circuit:
- Portenta H7 + Vision Shield
- Portenta H7 + Portenta Breakout

This example code is in the public domain.
*/
#include "SDMMCBlockDevice.h"
#include "FATFileSystem.h"

SDMMCBlockDevice block_device;
mbed::FATFileSystem fs("fs");

void setup() {
// put your setup code here, to run once:
delay(2000);
Serial.begin(9600);
while (!Serial);

Serial.println("Mounting SDCARD...");
int err = fs.mount(&block_device);
if (err) {
// Reformat if we can't mount the filesystem
// this should only happen on the first boot
printf("No filesystem found, formatting... ");
fflush(stdout);
Serial.println("No filesystem found, formatting... ");
err = fs.reformat(&block_device);
}
if (err) {
Serial.println("Error formatting SDCARD ");
while(1);
}

DIR *dir;
struct dirent *ent;
printf("try to open dir\n");
int dirIndex = 0;

Serial.println("List SDCARD content: ");
if ((dir = opendir("/fs")) != NULL) {
/* print all the files and directories within directory */
// Print all the files and directories within directory (not recursively)
while ((ent = readdir (dir)) != NULL) {
printf ("%s\n", ent->d_name);
Serial.println(ent->d_name);
dirIndex++;
}
closedir (dir);
} else {
/* could not open directory */
printf ("error\n");
// Could not open directory
Serial.println("Error opening SDCARD\n");
while(1);
}
if(dirIndex == 0) {
Serial.println("Empty SDCARD");
}
}

void loop() {
// put your main code here, to run repeatedly:

// Empty
}