Skip to content

Instantly share code, notes, and snippets.

@bradmartin333
Created August 3, 2023 13:16
Show Gist options
  • Save bradmartin333/13a9f1e769499f6d85cbb7795496e305 to your computer and use it in GitHub Desktop.
Save bradmartin333/13a9f1e769499f6d85cbb7795496e305 to your computer and use it in GitHub Desktop.
List directory of Mbed OS filesystem on APMC
/*
Portenta - DirList
The sketch shows how to mount an usb storage device and how to
get a list of the existing folders and files.
The circuit:
- Portenta H7
This example code is in the public domain.
*/
#include <DigitalOut.h>
#include <FATFileSystem.h>
#include <Arduino_USBHostMbed5.h>
USBHostMSD msd;
mbed::FATFileSystem usb("USB");
// If you are using a Portenta Machine Control uncomment the following line
mbed::DigitalOut otg(PB_14, 0);
void setup()
{
Serial.begin(115200);
while (!Serial)
;
Serial.println("Starting USB Dir List example...");
// if you are using a Max Carrier uncomment the following line
// start_hub();
while (!msd.connect()) {
//while (!port.connected()) {
delay(1000);
}
Serial.print("Mounting USB device... ");
int err = usb.mount(&msd);
if (err) {
Serial.print("Error mounting USB device ");
Serial.println(err);
while (1);
}
Serial.println("done.");
char buf[256];
// Display the root directory
Serial.print("Opening the root directory... ");
DIR* d = opendir("/USB/");
Serial.println(!d ? "Fail :(" : "Done");
if (!d) {
snprintf(buf, sizeof(buf), "error: %s (%d)\r\n", strerror(errno), -errno);
Serial.print(buf);
}
Serial.println("done.");
Serial.println("Root directory:");
unsigned int count { 0 };
while (true) {
struct dirent* e = readdir(d);
if (!e) {
break;
}
count++;
snprintf(buf, sizeof(buf), " %s\r\n", e->d_name);
Serial.print(buf);
}
Serial.print(count);
Serial.println(" files found!");
snprintf(buf, sizeof(buf), "Closing the root directory... ");
Serial.print(buf);
fflush(stdout);
err = closedir(d);
snprintf(buf, sizeof(buf), "%s\r\n", (err < 0 ? "Fail :(" : "OK"));
Serial.print(buf);
if (err < 0) {
snprintf(buf, sizeof(buf), "error: %s (%d)\r\n", strerror(errno), -errno);
Serial.print(buf);
}
}
void loop()
{
delay(1000);
// handle disconnection and reconnection
if (!msd.connected()) {
msd.connect();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment