From 4e5ecb5c290c5fd26ff5c2a73b2213d74cb7fc0b Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 13 Apr 2021 10:37:41 +0200 Subject: [PATCH] Update TestSDCARD example --- .../examples/TestSDCARD/TestSDCARD.ino | 45 ++++++++++++++----- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/libraries/Portenta_SDCARD/examples/TestSDCARD/TestSDCARD.ino b/libraries/Portenta_SDCARD/examples/TestSDCARD/TestSDCARD.ino index 6430a99ab..1b1b0fb58 100644 --- a/libraries/Portenta_SDCARD/examples/TestSDCARD/TestSDCARD.ino +++ b/libraries/Portenta_SDCARD/examples/TestSDCARD/TestSDCARD.ino @@ -1,3 +1,14 @@ +/* + 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" @@ -5,32 +16,44 @@ 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 }