arduino-libraries / Arduino_UnifiedStorage

Read and write files to flash, USB mass storage and SD cards in a unified way.
GNU Lesser General Public License v2.1
11 stars 3 forks source link

Arduino_UnifiedStorage causing my Arduino to crash #21

Closed Ken-488 closed 7 months ago

Ken-488 commented 9 months ago

I am new to GitHub and relatively new to Arduino programming, so apologies if this is my fault.

Including the Arduino_UnifiedStorage.h header file in my sketch causes the the arduino to crash after its first serial print command.

I am using a Portenta Machine Control board that has a Portenta H7 processor board.

It was previously working and then i started a new sketch and include the file. That was when my problems started, now none of my older sketches work, or the examples included in the library. this was the first time that the #include was in <Arrows> rather than "Quotationmarks".


I have tried removing the library and re installing it.

aliphys commented 9 months ago

Hello @Ken-488 , Thanks for providing feedback! Can you provide the sketch you used? cc @cristidragomir97

Ken-488 commented 9 months ago

Here it is:

/*
    SimpleStorageWriteRead

    Demonstrates basic usage of the "Arduino_UnifiedStorage" library to write and read data to storage.
    Supports SD card, USB storage, and internal storage (default, uncomment to choose).

    In the setup function, the code initializes serial communication, mounts the storage medium,
    creates a root directory with three subdirectories, and writes data to three files in each subdirectory.

    Following this, the code showcases reading data from files by using "seek" and "available" methods,
    switching file modes to read, resetting file pointers to the start,
    and printing the read data to the serial monitor using a while loop.

    Created 28th July 2023
    By Cristian Dragomir

    Modified 24th August 2023
    By Ali Jahangiri

    https://github.com/arduino-libraries/Arduino_UnifiedStorage/blob/main/examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino

*/

#include "Arduino_UnifiedStorage.h"

void printFolderContents(Folder dir, int indentation = 0) {
  std::vector<Folder> directories = dir.getFolders();
  std::vector<UFile> files = dir.getFiles();

  // Print directories
  for (Folder subdir : directories) {
    for (int i = 0; i < indentation; i++) {
      Serial.print("  ");
    }
    Serial.print("[D] ");
    Serial.println(subdir.getPath());
    printFolderContents(subdir, indentation + 1);
  }

  // Print files
  for (UFile file : files) {
    for (int i = 0; i < indentation; i++) {
      Serial.print("  ");
    }
    Serial.print("[F] ");
    Serial.println(file.getPath());
  }
}

// Uncomment one of the three lines below to select between SD card, USB or internal storage
//SDStorage unifiedStorage = SDStorage();             // Create an instance for interacting with SD card storage
USBStorage unifiedStorage = USBStorage();            // Create an instance for interacting with USB storage
//InternalStorage internalStorage = InternalStorage();  // Create an instance for interacting with internal Flash storage (default)

void setup() {
  Serial.begin(115200);
  while (!Serial);

  if(!unifiedStorage.begin(FS_FAT)){
    Serial.println("Error mounting storage device.");
  }

  // Create a root directory in storage device
  Folder root = unifiedStorage.getRootFolder();

  // Create subdirectories inside the root directory
  Folder subdir1 = root.createSubfolder("subdir1");
  Folder subdir2 = root.createSubfolder("subdir2");
  Folder subdir3 = root.createSubfolder("subdir3");

  // Create .txt files inside the subdirectories
  UFile file1 = subdir1.createFile("file1.txt", FileMode::WRITE);
  UFile file2 = subdir2.createFile("file2.txt", FileMode::WRITE);
  UFile file3 = subdir3.createFile("file3.txt", FileMode::WRITE);

  // Write data to the files
  file1.write("This is file 1.");
  file2.write("This is file 2.");
  file3.write("This is file 3.");

  // Read data from the files using seek and available
  Serial.println("Reading data from files using seek and available:");

  // Close and open files in reading mode
  file1.changeMode(FileMode::READ);
  file2.changeMode(FileMode::READ);
  file3.changeMode(FileMode::READ);

  // Read data from file1
  file1.seek(0); // Move the file pointer to the beginning
  Serial.println(file1.available());
  while (file1.available()) {
  char data = file1.read();
    Serial.write(data);
  }
  Serial.println();

  // Read data from file2
  file2.seek(0); // Move the file pointer to the beginning
  while (file2.available()) {
    char data = file2.read();
    Serial.print(data);
  }
  Serial.println();

  // Read data from file3
  file3.seek(0); // Move the file pointer to the beginning
  while (file3.available()) {
    char data = file3.read();
    Serial.print(data);
  }
  Serial.println();

  printFolderContents(unifiedStorage.getRootFolder());
}

void loop() {}
sebromero commented 8 months ago

Hi @Ken-488 If I understand correctly it prints Reading data from files using seek and available: and then crashes which makes the red LED blink the SOS pattern. Correct? Have you tried with a different USB thumb drive or reformatting the one you have?

Ken-488 commented 8 months ago

hi, yes that is exactly what it is doing, just never see the red light as it is inside the Portenta Machine Control. i have just tried a section of memory sticks and reformatted them. it still has the same result but it does write the 3 folders and files to the sticks before crashing.

sebromero commented 8 months ago

@Ken-488 Odd. Can you please check if you have the latest version (0.2.0) of Arduino_USBHostMbed5 installed? Can you also add some print statements after the changeMode calls to see if it reaches that part of the code? If necessary, please add some additional print statements further down in the code so we can pinpoint the function that makes the board crash.

Ken-488 commented 8 months ago

Hi, yes i have version 0.2.0. Just tried that, it crashed after printing the first Serial.print() or Serial.println() (Tried both) command inserted as below

  Serial.begin(115200);
  while (!Serial);

 Serial.print("Start setup");
 Serial.print("Start setup2");

  if(!unifiedStorage.begin(FS_FAT)){
    Serial.println("Error mounting storage device.");
  }
sebromero commented 8 months ago

@cristidragomir97 Any idea on how to debug this?

Ken-488 commented 8 months ago

Hi, are you able to recreate the problem? i previously spent a few days tying to isolate the problem prior to posting. the library was used in a different program, one day it was working then i added another library and changed the sketch, then i had some problem compiling the modified sketch (unable to find Libraries). so i deleted all the #include statements at the start of the sketch, i re included all of the libraries using the include library option in the menu. the changed the statement from

#include "Arduino_UnifiedStorage.h" 

to

#include <Arduino_UnifiedStorage.h>

after much more fighting to get the sketch to compile and find all the liaries, then it would compile and crash and spent a long time trying to isolate why it crashed, i could never get the Library to function with any sketch, always the same problem. during this time i got to the concussion that it probably was a conflict between "Arduino_UnifiedStorage.h" library and the Serial.print() command, i did wonder if it was a problem from an update or with the Arduino IDE, so did try re installing it, i've not tried another computer yet.

sebromero commented 8 months ago

@Ken-488 No, we haven't. We did extensive testing with this library, also with Portenta Machine Control, but we haven't encountered the issue that you're describing.

Ken-488 commented 8 months ago

oh, that is sounding a lot like my computer is causing the problem when it is compiling then

sebromero commented 8 months ago

It's possible. We could create a binary from your sketch and share that with you so you can test if your computer creates a faulty binary or if the issue is elsewhere.

Ken-488 commented 8 months ago

I have Bin File here, not sure how to post it, (sorry new to this)

sebromero commented 8 months ago

@Ken-488 You probably need to create a zip file and then attach it here along with a comment. See https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/attaching-files

Ken-488 commented 8 months ago

sketch_oct19a.zip

i have zipped up the whole sketch folder

alrvid commented 8 months ago

Thank you for reporting the problem! I've been able to reproduce it when running your binary, but the crash doesn't happen if I compile the same sketch on my own computer. I'll have a closer look at it and see if I can figure out why it happens.

Ken-488 commented 8 months ago

Thank you for the update, this could be quite challenging to figure out, i have already tried uninstalling Arduino IDE, deleting any remaining files and then reinstalling it.

alrvid commented 8 months ago

It seems like the bug you've run into is in fact the same bug that's reported here for the Giga:

https://github.com/arduino-libraries/Arduino_USBHostMbed5/issues/29

We're still actively working on it.

sebromero commented 7 months ago

Hi @Ken-488 ! Could you try again with the latest release of this library and also this release of Arduino_USBHostMbed5?

Ken-488 commented 7 months ago

Hi, i can confirm that Arduino_USBHostMbed5 0.3.1 appears to have resolved the issue i was experiencing, Thank you all for you help,