Hi,
as in title, sounds strange but behave as it.
I have GIGA with display shield and use USB stick to save data, in the code on the beginning USB is initialized and I read config file - it always works right. Then I initialize rest of hardware and menu, form menu I call data grabbing function saving data to file on USB (quite close to example code, of course without mounting again USB, which was already done in first initialization), this function also plot graph.
Generally all works but I notice, if I do some changes in the code (section not related to USB or data grabbing) simply add an extra plot the code behave differently (generally it hang up when grabbing function is called) it begin to plot graph (axis and legend) but it stop in middle of it (calling USB is way after that, when all axis/legend is already plotted).
This is a bit random behave, some changes cause crashing some not and sometimes works partially = few times save data and crash or crash and after reset works well.
I have Arduino OS GIGA 4.2.1, Display 1.0.2 and Touch 1.0.1.
Any idea what could be the source of such behave?
Regards
It might be code size but I'm not familiar with your board so can't advice on that.
It however often is an issue where you have bugs in your code; e.g. writing outside the boundaries of arrays will overwrite other variables resulting in undefined behaviour.
Hi,
Thank you for comments,
I check the code many times, also use "example code" to avoid own errors/mistakes.
It behave randomly good/bad, after same code loading once it crashes (red LED blink 4x4 times short/long) but after some hard reset it works perfectly for long time.
This is beginning of the function which cause crashes:
void CoolingDraw()
{
unsigned long StartTime; // time counter
int prvx=20;
int prvy=380;
int prvyv=380;
int prvyc=380;
int err;
//String DataString;
String fn="/usb/" + SerialFileName() + ".csv";
Serial.println(fn);
display.fillScreen(LIGHTGREY);
drawAxisCooling();
// USB file save init, assume is already mounted
mbed::fs_file_t file;
struct dirent *ent;
FILE *f = fopen(fn.c_str(), "w+");
if (f == NULL)
{
Serial.println("Error opening file-------------------------------");
Serial.print("fopen failed, errno = ");
Serial.println(errno);
}
Serial.println("USB opened");
// USB file save init
StartTime=millis();
- SerialFileName() - return string which will be the file name
- drawAxisCooling() - draw empty graph, seems that it stop just after it.
f - does not return anything, or rather it is not executed because it hangup/crash already.
Regards
Hi,
My suspicions are directed at:
FILE *f = fopen(fn.c_str(), "w+");
fopen require filename as * const char * _name in my case it is String and converted to char.
I perform some tests and if the name is not corrected it hangup/freeze at this step.
Here comes my question:
How to correctly convert String to 'const char *' format required by fopen?
Generally I would like to have name as variable and my input function return value as String.
Regards
Does it return String object?
If you post all the relevant code you might be able to get some better advice.
The way you do it.
Hi,
yes my function SerialFileName() return string,
I tested already:
- fopen("/usb/filename.txt","w+") - works sometimes
- fopen(string.c_str(),"w+") - works sometimes, where string is output from any function returning string, tested on my own and reading string from serial.
Usually just after the code is uploaded it crash, after many resets it begin to work and it could work many hours.
The problem is that the device will collect some data and such instability is not acceptable.
For me is difficult to understand why it behave randomly bad/good.
If I did something wrong in the code it should fail always or work well always not randomly behave good/bad.
Seems quite close to that: GigaR1 USB filewrite - #22 by ptillisch
Some code which works only sometimes:
String serRead() // return string from serial port
{
String a="";
Serial.println("file name:");
while (a.length()<4) // 3chars
{
while(Serial.available())
{
a= Serial.readString();
}
}
return a.substring(0,a.length()-1);
}
void CoolingDraw()
{
Serial.print("WRITE -------------------------------------------- ");
String fname="/usb/";
fname.concat(serRead());
fname.concat(".txt");
Serial.println(fname);
while (!msd.connect())
{
Serial.println("msd.connect() wait ...");
delay(500);
}
mbed::fs_file_t file;
struct dirent *ent;
int dirIndex = 0;
int res = 0;
FILE *f = fopen(fname.c_str(), "w+");
for (int i = 0; i < 10; i++) {
Serial.print("Writing numbers (");
Serial.print(i);
Serial.println("/10)");
fflush(stdout);
int 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);
int 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");
}
}
it randomly hangup on while (!msd.connect())
Regards
Hi,
here is output/debug of my code, write function = CoolingDraw() above, once works perfectly, then partially or do not work at all.
Is there any way to read msd.connect() error code, I mean interpretation of the codes?
-------------------------------------------------
Viayner - Gettering/Cooling device prototype
-------------------------------------------------
searching for USB .........
msd.connect
0
end while
Mounting USB device...
Done
-----exist------settingsRTC.spc
Open /usb/settingsRTC.spc
File content:
xxxxxxxxxxxxx
File closing
File closed
Attempting to connect to SSID: xxxxxxxxxxxxxxx
3
Connected to WiFi
SSID: xxxxxxxxx
IP Address: xxxxxxxxxx
signal strength (RSSI):-57 dBm
ADC0 ready
System Clock: 2025-01-13 11:05:25
Select box =0
WRITE -------------------------------------------- file name:
/usb/123.txt
Writing numbers (0/10)
Writing numbers (1/10)
Writing numbers (2/10)
Writing numbers (3/10)
Writing numbers (4/10)
Writing numbers (5/10)
Writing numbers (6/10)
Writing numbers (7/10)
Writing numbers (8/10)
Writing numbers (9/10)
File closing
File closed
-------------------------------------------------
Viayner - Gettering/Cooling device prototype
-------------------------------------------------
searching for USB .........
msd.connect
0
end while
Mounting USB device...
Done
-----exist------settingsRTC.spc
Open /usb/settingsRTC.spc
File content:
xxxxxxxxxxxxxxx
File closing
File closed
Attempting to connect to SSID: xxxxxxxxxxx
3
Connected to WiFi
SSID: xxxxxxxxxxx
IP Address: xxxxxxxxxxxx
signal strength (RSSI):-60 dBm
ADC0 ready
System Clock: 2025-01-13 11:06:41
Select box =0
WRITE -------------------------------------------- file name:
/usb/12558.txt
Writing numbers (0/10) <---- FREEZE here
-------------------------------------------------
Viayner - Gettering/Cooling device prototype
-------------------------------------------------
searching for USB .........
msd.connect
0
end while
Mounting USB device...
Done
-----exist------settingsRTC.spc
Open /usb/settingsRTC.spc
File content:
xxxxxxxxxxxxxxx
File closing
File closed
Attempting to connect to SSID: xxxxxxxxxxx
3
Connected to WiFi
SSID: xxxxxxxxxxx
IP Address: xxxxxxxxxxxx
signal strength (RSSI):-60 dBm
ADC0 ready
System Clock: 2025-01-13 11:07:24
Select box =0
WRITE -------------------------------------------- file name:
/usb/123.txt
msd.connect() wait ...
msd.connect() wait ...
msd.connect() wait ...
msd.connect() wait ...
msd.connect() wait ...
msd.connect() wait ...
msd.connect() wait ...
msd.connect() wait ...
msd.connect() wait ...
msd.connect() wait ...
msd.connect() wait ...
msd.connect() wait ...
msd.connect() wait ...
msd.connect() wait ...
msd.connect() wait ...
msd.connect() wait ...
msd.connect() wait ...
msd.connect() wait ...
msd.connect() wait ...
msd.connect() wait ...
msd.connect() wait ...
msd.connect() wait ...
msd.connect() wait ...
msd.connect() wait ...
msd.connect() wait ...
msd.connect() wait ...
As mentioned not familiar with your board or the coding around it.
Maybe you should close the connection once done. Or maybe do not attempt to connect multiple times?
Hi,
what is the proper way to close the connection or to eject USB?
I am sorry for such questions but I become a bit frustrated with stability/behavior of GIGA.
I tested downgrade of the Arduino_USBHostMbed5 library and I have got some progress.
v0.3.0 produce code (without any changes) which after two resets works stable, if I can call it like that. I never have a working device after loading the code to GIGA and first reset.
Regards
One more what I notice recently,
it works but only if I use the connection quite often.
If there is a break like 30s between calling USB it freeze - red LED
issue.
What I do wrong?
Can I have please maybe some code example?
- first I need to read txt file where is some configuration, that one seems to work, it never fail
- then I need to create file with given as string name, save some data, close file
- could be next same as previous step
- safe remove USB device and save mount new one if inserted
Regards
Hi @viayner The first thing I would do is eliminate the usb flash drive as the problem. As you can see here, the platform is very sensitive to the drive.
Try SanDisk Ultra Fit 32GB, Verbatim's Metal Exec 16GB or Kingston USB DataTraveler Micro Metal Flash Drive USB 3.2 64GB. All work without issue.
Hi,
I already tested with DataTravel 111 16GB and metal one 8GB which is branded for us and look like Kingston USB DataTraveler but I am not sure where it come from/no details in driver.
The first one works very rarely and often reports msd.connected = not connected, when trying to write, the second if begin to work, works until reset. But both are mounted without a problem and at the very beginning of my code I read the configuration text file from them, it never reported an error.
Regards
Hi,
I tested already few more USB sticks and seems that you could be right.
Some device works from booting but if I do not use it for more than 30s it freeze and crash when I call it again.
I found one device DataTravel 100 G3 16GB which works always (since I tested it only today) with some exception: it does not always is mounted on the beginning.
I have function which mount USB on boot, based on demo code:
while (!msd.connect())
{
Serial.println("msd.connect");
Serial.println(count);
delay(700);
count ++;
if (count==5)
break;
}
it means it wait for 3.5s to mount USB, and never fail with any other USB.
If fail when I do hardware reset it works.
Generally is there way to improve USB protocol? such behave is rather toy than real use (for me) I design device which save data on USB but if it randomly save or not is not acceptable in my application, I need to add backup for the data which make USB use a bit unnecessary.
The user must have only that one USB stick.
Is there a software way to prevent from freezing? I mean detect that something wrong is with USB and do not use it but do not allow that OS freeze? and how to safe umount USB?
Regards
I have used SanDisk Ultra Fit 32GB and Kingston USB DataTraveler Micro Metal Flash Drive USB 3.2 64GB on my 3 GIGA boards and they always mount and read/write without problems. I don't, however, ever stop writing (unless I'm reading) so the device is never idle.
Not without a lot of work and a lot of knowledge about usb and mbed. The library (Arduino_USBHostMbed5) as the name suggests came from mbed version 5 and was ported to the Arduino. Arm decided not to include it in mbed 6 and there is no replacement or maintenance of USBHostMSD.
My solution (some details in the earlier link) is to put data I want logged into a shared memory area and run the code that interfaces with the USB on the M4 co-processor. I periodically poll the M4 code to check all is ok. That way any blocking delays or failures do not impact my main code on the M7. This is a hobby project and I am not sure I would trust it in a commercial environment.
I only remove mine when the device is powered down but if you flush buffers and unmount the filesystem, and make sure your code doesn't try to access it, I suspect this would work. There are callbacks available to detect insertion/removal but I've not used them.
I agree. If you need guaranteed reliability then I think an SD card / external data logger approach would be better. That is sad, but true, given the problems.
Hi,
Thank you and everyone for help with this problem.
Current situation is: I found one working USB stick and order next one "recommended", let see, I think backup of the data is not required but obligatory with such behave.
Actually if you have any good suggestions how to properly perform it please share the idea.
I would like to avoid situation what I observe with "bad USB stick" that the whole OS crash (red LED blinking) when I call USB, seems that only right approach is to call USB at the very end when all data is collected already and store somewhere, there is no internal EEPROM or SRAM with backup power which will be best in such case.
Regards
Depending on the frequency you need to backup the data and the volume of data, you could use the QSPI flash as temporary storage
Hi,
that was my idea also, I did not found any explanation how to access Flash, could you please let me know how to easy access from my code Flash? Each set of data contain longint and 3x float. Data saving will be like 1data set per 1s.
Regards
That's far from the ideal use case for the QSPI as it will wear out over time. However, as your dataset is small you could use the FAT32 partition (user data 8MB or OTA + user data 13MB) created by the QSPIFormat example sketch, and access it like this:
#include "QSPIFBlockDevice.h"
#include "MBRBlockDevice.h"
#include "FATFileSystem.h"
QSPIFBlockDevice root;
//
// mbed::MBRBlockDevice ota_and_user_data(&root, 2);
// or
// mbed::MBRBlockDevice user_data(&root, 3);
//
// depending on the scheme chosen in QSPIFormat
mbed::FATFileSystem data_fs("fs");
void setup() {
// data_fs.mount(&ota_and_user_data);
// or
// data_fs.mount(&user_data);
...
f = fopen("/fs/filename.csv", "w+");
...
}
If you add this to your sketch you will see the partitions when you connect to your pc
#include <PluggableUSBMSD.h>
USBMSD MassStorage(&root);
Hi,
looks rather not complicated, I create already flash as drive but did not found way to access own drive. Thank you, I will test it.
Regards