Teensy frezees during reading of file if USB drive is removed

Hello there!

I am working on a project (sorry in advance, but I'm not allowed to post the code) using a Teensy 4.1 board. On of the main function is to enable the user to read and write data to an USB device connected to the teensy board using the following library: GitHub - wwatson4506/mscFS: mscFS is version of SD for use with MSC and follows the new Teeensy file system abstracion.. The library works pretty much fine, but I observed the following behavior which I don't really know how to fix: let's say I have a big file on the USB device, and I want to read it buffer by buffer and do some processing. I use an PFsFile object to open the file then use usbFile.read(buffer, bubberLen) to read chunks of data from the file. If I let the board and USB device untouched then the whole process works just fine. My problem happens if I try to remove the USB device during the process because the teensy gets into a freeze state inside the read function and I can't do anything until I reset the board. My first solution was to check every time before a read if the USB device was still attached, but from 20 tries, let's say, I couldn't catch the event I want (maybe just 1 or 2 times) and I still got to the read function and ended up in a freeze. I also found a solution on the web by using a watchdog that automatically resets the board if the "dog" is not "fed" (happens during a freeze), but I would want another suggestion if any of you could help me. If it is possible, I would want that the program to continue its processing, but to let me know somehow if it can't read from the file without freezes or resets. I hope someone had gone through something similar, maybe using the SD card, not the USB, because I think they could have similar behaviors (the USB lib I'm using was built based on the SD card filesystem). Once again sorry for not being able to post code or schematics of the board, but If you are so kind just to advise me then I'll be grateful.

I will take a SWAG as I am not familiar with your processor. Consider using a timer interrupt to check if the USB is connected or not. If so exit, if not do an abort procedure. When checking the USB device note where it is reading and when it repeats you know what has happened. You can do this operation flag when you have actually received a byte. It must be a global variable so your ISR (Interrupt Service Routine) can read it.

I am the person that wrote that library. Unfortunately that library is very outdated. MSC is now part of the Teensyduino library. You can use Teensyduino 1.58 up to 1.59B3 (the latest version) and Arduino 1.8.19 or Arduino IDE 2.1.1. Hot plugging is supported but all open files should be closed before removing the USB drive. There are examples of usage in the storage folder of the "USBHost_t36" library.

Just joined this forum and this is my first post here.

1 Like

How will you handle that issue when using Arduino libraries that that you didn't write and whose license requires making the source code available?

Thank you for the reply. I wanted to find a workaround for allowing to unplug without freezing if somehow the file was still open. Meanwhile I updated arduino and teensyduino to work with the latest versions available for the usb library.

Thanks for the reply. I tried something like this, but the solution was not good enaugh. Sometimes I could catch that event (unplugging the usb) and prevent reading from the file, but other times the the reading still happend (and froze my device) before having the chance to signal the unplug event.

That wasn’t really the case, at that time I was writing an app for research and development purpose to test some things (that I could not distribute). I wasn’t really going to publish an app using libraries with such licenses. I could have wrote a smaller example for my code, without sensitive data, if that was what you were expecting, but I thought that I explained my problem pretty good and I didn’t have a problem with my code, but with the library itself or how I was addressing the topic. Also I didn’t have the code on my pc at the time of writing this post.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.