Hi, i am trying to read root directory from my thumb drive connected to USB-A on my Arduino board, however I still receive error -19 "No such device".
I am using code directly from "cheat sheet", I've already tried 3 different thumb drives - all formatted to FAT32 (their capacity: 2GB, 4GB and 4GB). I've tried to upgrade Arduino_USBHostMbed5 library to 0.0.4 and still the same :-/
OH NO, dont tell me, I've made this stupid mistake :D, so mbed::FATFileSystem usb("USB_DRIVE_DESIGNATION"), flash drive has to be named "USB_DRIVE_DESIGNATION"?
Oh yeah, I thought that "if I run exactly that code, the thumb drive should be named like that..."
I dont think issues is in this however, because it didnt help. Error -19 occurs while trying to read root dir - so msb is connected. I am attaching my complete sketch and output on serial line:
#include <FATFileSystem.h>
#include <Arduino_USBHostMbed5.h> //doinstalovat pomoci LibraryManageru
USBHostMSD msd;
mbed::FATFileSystem usb("MCDONALDS"); //toto je jakesi "designation", ktere pote musime pouzit v pripade prace se soubory
//FILE *f = fopen("/usb/text.txt", "r+");
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println("\n\n");
Serial.println("Setting PA_15 and enabling USB-A port");
pinMode(PA_15, OUTPUT); //enable the USB-A port
digitalWrite(PA_15, HIGH);
while (!msd.connect()) {
Serial.println("Connecting msb");
delay(1000);
}
Serial.println("\t...done");
Serial.println("Mounting USB device...");
int err = usb.mount(&msd);
if (err)
{
Serial.print("Error while mounting on USB drive, code: ");
Serial.println(err);
while (true);
}
Serial.println("\t...done");
rootDirDisplay();
}
void loop() {
}
void rootDirDisplay()
{
Serial.println("Opening the root directory... ");
DIR* d = opendir("/usb/");
char buf[256];
if (!d)
{
snprintf(buf, sizeof(buf), "Fail, error: %s (%d)\r\n", strerror(errno), -errno);
Serial.print(buf);
}
else
{
Serial.println("...\tdone");
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);
int 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 readFile()
{
mbed::fs_file_t file;
struct dirent *ent;
int dirIndex = 0;
int res = 0;
Serial.println("Open file..");
FILE *f = fopen("/usb/Arduino.txt", "r+");
char buf[256];
Serial.println("File content:");
while (fgets(buf, 256, f) != NULL)
{
Serial.print(buf);
}
Serial.println("File closing");
res = fclose(f);
}
Setting PA_15 and enabling USB-A port
Connecting msb
Connecting msb
...done
Mounting USB device...
...done
Opening the root directory...
Fail, error: No such device (-19)