Giga R1 USB and file reading

Hi everyone,
I came across an interesting problem. I want to read a file from a USB thumb stick which worked perfectly fine with a previous sketch of mine. However, now I modified the code so that the program checks periodically whether a USB stick is attached and initialised or not. This part works fine, but now I get an error and the file cannot be opened and read anymore. I tried a couple of things (switching from if statements to while loops, existence of file, file path), but nothing helped. Any ideas?? :grinning:

I declared all variables globally and created the function:

void usbConnection() {
  unsigned long connectMillis = millis(); 

  if (connectMillis - tUSBCheck >= tUSBCheckInterval) { // Check USB status periodically
    tUSBCheck = connectMillis;
      
    if (!msd.connected()) {
      lv_label_set_text(label, "No USB drive found.");
      static lv_style_t style;
      lv_style_init(&style);
      lv_style_set_text_color(&style, lv_palette_main(LV_PALETTE_RED));
      lv_obj_add_style(label, &style, 0);

      Serial.println("MSD not connected");
      usbConnected = false; // Update flag to false if not connected
      msd.connect(); // Attempt to connect to USB drive
      delay(500);
    }

    else if (msd.connected() && !usbConnected) {
      lv_label_set_text(label, "USB drive found.");
      static lv_style_t style;
      lv_style_init(&style);
      lv_style_set_text_color(&style, lv_palette_main(LV_PALETTE_GREEN));
      lv_obj_add_style(label, &style, 0);

      Serial.println("MSD connected");
      usbConnected = true; // Update flag to true if connected
    } 
  }
}  

I call this function in
void loop() { usbConnection(); }

and to read the file on the USB stick, I created:

void btnLoadFileClicked(lv_event_t * e) {
  Serial.println("Load file button clicked");

  if (!msd.connected()) {
    Serial.println("USB not connected. Please connect a USB drive.");
    return;
    }

  // Opening .txt file for loading of parameters
  delay(2500);
  settings = fopen("/usb/settings.txt", "r"); 

  if (settings) {
    Serial.println("Opened file");
// logic for file handling

    fclose(settings);
    Serial.println("Closed file");

    } else if (!settings) {
    Serial.println("Failed to open file");
  }
}

I'm always getting "Failed to open file" in the serial monitor... :wink:

Best, Lea

I am wondering if the problem actualy occurrs because the USB port is basically busy doing something else (msd.connected()) and thus the file cannot be opened. I also tried changing/adding/removing delays in the code snippets.

Hello @lerehmi,

Could you please share with us the full sketch that you are using?

Looking forward to your response. :wink:

Follow this guide's Guide to Arduino GIGA USB Features
USB Mass Storage section.
If you have issue with the example, do the setps below before trying the examples again.

I have a new GIGA R1 WiFi and I cannot access a USB drive. I am using:

IDE v2.3.6

Arduino_USBHostMbed5 v 0.3.1

Arduino Mbed OS GIGA Boards v 4.4.1

When I load the provided Arduino_USBHostMbed examples ( I have tried DirList and FileRead) the msd.connect() always returns a False indicating a failure to connect to the USB device that is plugged into to USB-A port. The USB drive is formatted FAT32 with MSBR. I have tried several USB devices - same result.

I have also done a WiFi Firmware update and reset the flash memory as indicated above. I have not done a Burn of the bootloader (this looks a bit complex for me), but I can load and run other programs on to the GIGA without problems.

I would appreciate some help.

Geoff

Hi Geoff, it is very fussy about the particular drive being used. see here Unreliable file writing on connected flash drive
I'm currently using Kingston 64GB DataTraveler Micro Metal Flash Drive USB 3.2 without issue.

I would suggest getting a drive known to work before investigating further

Dear Steve, thanks for the feedback, but I cannot get any USB drive to work, including a new SanDisk Cruzer Blade I purchased today!

Any other thoughts?

Geoff

Geoff, have you enabled the usb port as per:
https://docs.arduino.cc/tutorials/giga-r1-wifi/giga-usb

by adding:

  pinMode(PA_15, OUTPUT);
  digitalWrite(PA_15, HIGH);

to the examples setup?

Yes, done that. Here is my code:

void loadUSBdata()
{
  int count = 0;
  pinMode(PA_15, OUTPUT);
  digitalWrite(PA_15, HIGH);
  delay(2000);
  
  while (!msd.connect() && count < 10)
  {
    Serial.println("Waiting for USB device: "+String(count)+ "  Error: "+String(msd.connect()));
    delay(1000);
    count++;
  }
  if(!msd.connected() )
  {
    Serial.println("Error, msd failed to connect");
    return;
  }
  int err = usb.mount(&msd);
  if(err)
  {
    Serial.println("Error mounting USB");
  }

  FILE *file = fopen(melodiesFileName, "r");
  if (!file) 
  {
    Serial.println("Failed to open file.");
    return;
  }
  Serial.println("File opened: "+String(melodiesFileName));

  fclose(file);
  usb.unmount();
}

Don't know what else to suggest other than my previous recommendation to try a stick known to work. If that doesn't, then I'd RMA the board as defective.