I received my GIGA R1 this week and was going through the USBHostMBed5 USBMSD examples. DirLIst and FileWrite examples worked fine but FileRead.ino crashes the GIGA with red light flashing. I narrowed it down to the fgets() call. If I commented that out the sketch would finish without a crash. I tried fread() and got thee same results as fgets().
Starting USB File Read example...
Mounting USB device...
read done Open file..
File content:
That is as far as it gets. I am using USBHostMbed5 version 3.1. I have tried this with Ubuntu 22.04 and Windows 11 and got the same results. I actually went through all of the versions of USBHostMbed5 and the results were the same. The sketch crashed the GIGA.
The sketch I was using:
/*
Portenta - FileWrite
The sketch shows how to mount an usb storage device and how to
write a file, eventually overwriting the original content.
The circuit:
- Portenta H7
This example code is in the public domain.
*/
#include <Arduino_USBHostMbed5.h>
#include <DigitalOut.h>
#include <FATFileSystem.h>
USBHostMSD msd;
mbed::FATFileSystem usb("usb");
// mbed::DigitalOut pin5(PC_6, 0);
mbed::DigitalOut otg(PB_8, 1);
void setup() {
Serial.begin(115200);
while (!Serial);
while (!msd.connect()) {
delay(1000);
}
Serial.println("Mounting USB device...");
int err = usb.mount(&msd);
if (err) {
Serial.print("Error mounting USB device ");
Serial.println(err);
while (1);
}
Serial.print("read done ");
mbed::fs_file_t file;
struct dirent *ent;
int dirIndex = 0;
int res = 0;
Serial.println("Open /usb/numbers.txt");
FILE *f = fopen("/usb/numbers.txt", "w+");
for (int i = 0; i < 10; i++) {
Serial.print("Writing numbers (");
Serial.print(i);
Serial.println("/10)");
fflush(stdout);
err = fprintf(f, "%d\n", i);
if (err < 0) {
Serial.println("Fail :(");
error("error: %s (%d)\n", strerror(errno), -errno);
}
}
Serial.println("File closing");
fflush(stdout);
err = fclose(f);
if (err < 0) {
Serial.print("fclose error:");
Serial.print(strerror(errno));
Serial.print(" (");
Serial.print(-errno);
Serial.print(")");
} else {
Serial.println("File closed");
}
}
void loop() {
delay(1000);
// handle disconnection and reconnection
if (!msd.connected()) {
msd.connect();
}
}
I have gone through all of posts in the GIGA hardware posts along with several other searches including:
Sorry, I have not tried running the File system stuff in a while.
Currently playing with other devices and the USBHostMbed5 stuff.
And have had some better luck with first make sure you are at least up to
USBHostMBed5 0.3.1 which hopefully fixed most of the crashes on callbacks.
I am running on my own fork/branch of it as I mentioned in thread about hid objects...
Hi @KurtE - You were very much helpful along with the others. At first I wasn't really sure were to start but Just followed through the other thread and had both DirList and FileWrite working. I am using version 3.1. I have rebuilt Arduino Mbed-os wiith the debug info turned on. That's where I'll start playing with this. I still am a little familiar with Mbed-os from playing with it on the other forum. Just need to bone up using some sort of debugger for now, probably will use gdb with serial.
Thanks for the reply @Dozie - Tried the FileRead.ino sketch from that post and got the same results. Red lights and a crash. Again, it's crashing with:
It does not fail. I don't think it has anything to do with the USBHostMbed5 library at all. It has something to do with the Posix layer fgets, fgetc, fscanf etc...
I have been trying to find out where in the Mbed os library these are implemented. So far I cannot find it. Again this is happening with Ubuntu 22.04 and Windows 11. I am wondering if it has something to do with the version of arm-none-eabi-gcc I have. This only affects the FileRead sketch not FileWrite or DirList sketches. I have tried several USB sticks all formatted to Fat32 without succes.
Any other ideas are very much appreciated
Thanks
I deleted Arduino IDE 2.2.1 and .arduino15 then reinstalled the IDE. Went to the boards manager and install version 4.0.8 and reinstalled the latest USBHostMbed5 library 3.1.
It is still showing:
Hi @wwatson
Just tried a slightly modified version of your sketch, main difference is how I enabled USB host adapter and of course I am using @KurtE version of the MBedos5. But its worth giving it a try with version 3.1 of the lib.
There is note in one of the other examples about the digitalout:
// If you are using a Portenta Machine Control uncomment the following line
mbed::DigitalOut otg(PB_14, 0);
/*
Portenta - FileWrite
The sketch shows how to mount an usb storage device and how to
write a file, eventually overwriting the original content.
The circuit:
- Portenta H7
This example code is in the public domain.
*/
#include <Arduino_USBHostMbed5.h>
#include <FATFileSystem.h>
USBHostMSD msd;
mbed::FATFileSystem usb("usb");
void setup() {
Serial.begin(115200);
while (!Serial);
// Enable the USBHost
pinMode(PA_15, OUTPUT);
digitalWrite(PA_15, HIGH);
delay(100);
while (!msd.connect()) {
delay(1000);
}
Serial.println("Mounting USB device...");
int err = usb.mount(&msd);
if (err) {
Serial.print("Error mounting USB device ");
Serial.println(err);
while (1);
}
Serial.print("read done ");
mbed::fs_file_t file;
struct dirent *ent;
int dirIndex = 0;
int res = 0;
Serial.println("Open /usb/numbers.txt");
FILE *f = fopen("/usb/numbers.txt", "w+");
for (int i = 0; i < 10; i++) {
Serial.print("Writing numbers (");
Serial.print(i);
Serial.println("/10)");
fflush(stdout);
err = fprintf(f, "%d\n", i);
if (err < 0) {
Serial.println("Fail :(");
error("error: %s (%d)\n", strerror(errno), -errno);
}
}
Serial.println("File closing");
fflush(stdout);
err = fclose(f);
if (err < 0) {
Serial.print("fclose error:");
Serial.print(strerror(errno));
Serial.print(" (");
Serial.print(-errno);
Serial.print(")");
} else {
Serial.println("File closed");
}
}
void loop() {
delay(1000);
// handle disconnection and reconnection
if (!msd.connected()) {
msd.connect();
}
}
Hi @Merlin513 - I just realized that the sketch I posted in #1 was the FileWrite sketch. My mistake
It is the FileRead sketch that is crashing the GIGA. Here is the sketch:
/*
Portenta - FileRead
The sketch shows how to mount an usb storage device and how to
read from an existing file.
to use this sketch create a .txt file named Arduino.txt,
in your storage device and write some content inside.
The circuit:
- Portenta H7
This example code is in the public domain.
*/
#include <Arduino_USBHostMbed5.h>
#include <DigitalOut.h>
#include <FATFileSystem.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);
delay(2500);
Serial.println("Starting USB File Read example...");
// if you are using a Max Carrier uncomment the following line
//start_hub();
while (!msd.connect()) {
delay(1000);
}
Serial.println("Mounting USB device...");
int err = usb.mount(&msd);
if (err) {
Serial.print("Error mounting USB device ");
Serial.println(err);
while (1);
}
Serial.print("read done ");
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:");
char *test;
while ((test = fgets(buf, 256, f)) != NULL) {
Serial.print(buf);
}
Serial.println("File closing");
fflush(stdout);
err = fclose(f);
if (err < 0) {
Serial.print("fclose error:");
Serial.print(strerror(errno));
Serial.print(" (");
Serial.print(-errno);
Serial.print(")");
} else {
Serial.println("File closed");
}
}
void loop() {
delay(1000);
// handle disconnection and reconnection
if (!msd.connected()) {
msd.connect();
}
}
Ok I reverted to 3.1 and reran the write sketch and it worked without any errors.
As for the read sketch I am using:
/*
Portenta - FileRead
The sketch shows how to mount an usb storage device and how to
read from an existing file.
to use this sketch create a .txt file named Arduino.txt,
in your storage device and write some content inside.
The circuit:
- Portenta H7
This example code is in the public domain.
*/
#include <Arduino_USBHostMbed5.h>
#include <DigitalOut.h>
#include <FATFileSystem.h>
USBHostMSD msd;
mbed::FATFileSystem usb("usb");
void setup() {
Serial.begin(115200);
while (!Serial);
pinMode(PA_15, OUTPUT); //enable the USB-A port
digitalWrite(PA_15, HIGH);
delay(2500);
Serial.println("Starting USB File Read example...");
// if you are using a Max Carrier uncomment the following line
//start_hub();
while (!msd.connect()) {
delay(1000);
}
Serial.println("Mounting USB device...");
int err = usb.mount(&msd);
if (err) {
Serial.print("Error mounting USB device ");
Serial.println(err);
while (1);
}
Serial.print("read done ");
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:");
char *test;
while ((test = fgets(buf, 256, f)) != NULL) {
Serial.print(buf);
}
Serial.println("File closing");
fflush(stdout);
err = fclose(f);
if (err < 0) {
Serial.print("fclose error:");
Serial.print(strerror(errno));
Serial.print(" (");
Serial.print(-errno);
Serial.print(")");
} else {
Serial.println("File closed");
}
}
void loop() {
delay(1000);
// handle disconnection and reconnection
if (!msd.connected()) {
msd.connect();
}
}
and yep sure enough I got red lights of doom!!!!!! So its not you.
Well think I found the primary issue. With the Write sketch it creates a file named numbers.txt but the read sketch is looking for Arduino.txt so big crash!
If you use the following write sketch:
/*
Portenta - FileWrite
The sketch shows how to mount an usb storage device and how to
write a file, eventually overwriting the original content.
The circuit:
- Portenta H7
This example code is in the public domain.
*/
#include <Arduino_USBHostMbed5.h>
#include <DigitalOut.h>
#include <FATFileSystem.h>
USBHostMSD msd;
mbed::FATFileSystem usb("usb");
void setup() {
Serial.begin(115200);
while (!Serial);
pinMode(PA_15, OUTPUT); //enable the USB-A port
digitalWrite(PA_15, HIGH);
delay(100);
while (!msd.connect()) {
delay(1000);
}
Serial.println("Mounting USB device...");
int err = usb.mount(&msd);
if (err) {
Serial.print("Error mounting USB device ");
Serial.println(err);
while (1);
}
Serial.print("read done ");
mbed::fs_file_t file;
struct dirent *ent;
int dirIndex = 0;
int res = 0;
Serial.println("Open /usb/numbers.txt");
FILE *f = fopen("/usb/numbers.txt", "w+");
for (int i = 0; i < 10; i++) {
Serial.print("Writing numbers (");
Serial.print(i);
Serial.println("/10)");
fflush(stdout);
err = fprintf(f, "%d\n", i);
if (err < 0) {
Serial.println("Fail :(");
error("error: %s (%d)\n", strerror(errno), -errno);
}
}
Serial.println("File closing");
fflush(stdout);
err = fclose(f);
if (err < 0) {
Serial.print("fclose error:");
Serial.print(strerror(errno));
Serial.print(" (");
Serial.print(-errno);
Serial.print(")");
} else {
Serial.println("File closed");
}
}
void loop() {
delay(1000);
// handle disconnection and reconnection
}
and read sketch
/*
Portenta - FileRead
The sketch shows how to mount an usb storage device and how to
read from an existing file.
to use this sketch create a .txt file named Arduino.txt,
in your storage device and write some content inside.
The circuit:
- Portenta H7
This example code is in the public domain.
*/
#include <Arduino_USBHostMbed5.h>
#include <DigitalOut.h>
#include <FATFileSystem.h>
USBHostMSD msd;
mbed::FATFileSystem usb("usb");
void setup() {
Serial.begin(115200);
while (!Serial);
pinMode(PA_15, OUTPUT); //enable the USB-A port
digitalWrite(PA_15, HIGH);
delay(2500);
Serial.println("Starting USB File Read example...");
// if you are using a Max Carrier uncomment the following line
//start_hub();
while (!msd.connect()) {
delay(1000);
}
Serial.println("Mounting USB device...");
int err = usb.mount(&msd);
if (err) {
Serial.print("Error mounting USB device ");
Serial.println(err);
while (1);
}
Serial.print("read done ");
mbed::fs_file_t file;
struct dirent *ent;
int dirIndex = 0;
int res = 0;
Serial.println("Open file..");
FILE *f = fopen("/usb/numbers.txt", "r+");
char buf[256];
Serial.println("File content:");
while ((fgets(buf, 256, f)) != NULL) {
Serial.print(buf);
}
Serial.println("File closing");
fflush(stdout);
err = fclose(f);
if (err < 0) {
Serial.print("fclose error:");
Serial.print(strerror(errno));
Serial.print(" (");
Serial.print(-errno);
Serial.print(")");
} else {
Serial.println("File closed");
}
}
void loop() {
delay(1000);
}
it works
Starting USB File Read example...
Mounting USB device...
read done Open file..
File content:
0
1
2
3
4
5
6
7
8
9
File closing
File closed
@Merlin513@KurtE - I modified the FileRead sketch to check for a non-existent file. It does set an error in the errno variable when checked.
Here is the modified sketch. I removed the 'n' in /usb/numbers.txt:
/*
Portenta - FileRead
The sketch shows how to mount an usb storage device and how to
read from an existing file.
to use this sketch create a .txt file named Arduino.txt,
in your storage device and write some content inside.
The circuit:
- Portenta H7
This example code is in the public domain.
*/
#include <Arduino_USBHostMbed5.h>
#include <DigitalOut.h>
#include <FATFileSystem.h>
#include <error.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);
delay(2500);
Serial.println("Starting USB File Read example...");
// if you are using a Max Carrier uncomment the following line
//start_hub();
while (!msd.connect()) {
delay(1000);
}
Serial.println("Mounting USB device...");
int err = usb.mount(&msd);
if (err) {
Serial.print("Error mounting USB device ");
Serial.println(err);
while (1);
}
Serial.print("read done ");
mbed::fs_file_t file;
struct dirent *ent;
int dirIndex = 0;
int res = 0;
Serial.println("Open file..");
FILE *f = fopen("/usb/umbers.txt", "r+");
// Check for error and report it.
if(f == NULL) {
Serial.print("Faied: error code: ");
Serial.println(errno,DEC);
Serial.println(strerror(errno));
Serial.println("Halting...");
while(1);
}
char buf[256];
Serial.println("File content:");
while (fgets(buf, 256, f) != NULL) {
Serial.print(buf);
}
Serial.println("File closing");
fflush(stdout);
err = fclose(f);
if (err < 0) {
Serial.print("fclose error:");
Serial.print(strerror(errno));
Serial.print(" (");
Serial.print(-errno);
Serial.print(")");
} else {
Serial.println("File closed");
}
}
void loop() {
delay(1000);
// handle disconnection and reconnection
if (!msd.connected()) {
msd.connect();
}
}
It reports:
Starting USB File Read example...
Mounting USB device...
read done Open file..
Faied: error code: 2
No such file or directory
Halting...