Hi I am using Teensy 4.1 with Arduino IDE 2.3.4.
Consider the following standard setup for transferring data using an external USB stick attached to the Teensy.
#include <USBHost_t36.h> // import the Teensy USB library
int mscError=0; // USB error flag
USBHost myusb; // USB Host
USBHub hub1(myusb); // USB Hub
USBDrive myDrive(myusb); // Instance for one drive
USBDrive *drive_list[]={&myDrive}; // USB device array
byte blocks; // no of blocks to read or write
unsigned long startAddr; // start address (sector no)
uint32_t TotBytes=0; // no of bytes to read or write (blocks x 512)
byte USBData_writeBytes[16384]; // 16k write buffer
byte USBData_readBytes[16384]; // 16k read buffer
void setup() {
myusb.begin(); // initiate USB
delay(3000); // and wait a bit
}
void loop () {
myusb.Task();
USBDrive *pdrive=drive_list[0]; // initialise first (and only in this case) USB device
if (pdrive->msDriveInfo.connected==0) { // problem with USB device
Serial.println(" Problem with USB device \n");
}
else { // USB OK
if (writeFlag==1) { // write flag is true
mscError = myDrive.msWriteBlocks(startAddr, blocks, (uint16_t)512, USBData_writeBytes);
if (mscError==0) { // successful write
Serial.println("Successful write to USB device \n");
}
else { // failed write
Serial.println("Failed write to USB device \n");
}
}
if (readFlag==1) { // read flag is true
mscError = myDrive.msReadBlocks(startAddr, blocks, (uint16_t)512, USBData_readBytes);
if (mscError==0) { // successful read
Serial.println("Successful read from USB device \n");
}
else { // failed read
Serial.println("Failed to read from USB device \n");
}
}
} // usb ok
} // loop
The problem seems to be that if the USB media is removed during either the myDrive.msReadBlocks or myDrive.msWriteBlocks operations then the Teesny freezes and has to be physically reset.
Is the best method to catch this with a software driven reset from an timed interrupt method or via a threaded timer watching the state of the USB and initiating a software reset?
I was wondering if anybody else has encountered this problem?
Cheers
Consider the following standard setup for transferring data using an external USB stick attached to the Teensy.
#include <USBHost_t36.h> // import the Teensy USB library
int mscError=0; // USB error flag
USBHost myusb; // USB Host
USBHub hub1(myusb); // USB Hub
USBDrive myDrive(myusb); // Instance for one drive
USBDrive *drive_list[]={&myDrive}; // USB device array
byte blocks; // no of blocks to read or write
unsigned long startAddr; // start address (sector no)
uint32_t TotBytes=0; // no of bytes to read or write (blocks x 512)
byte USBData_writeBytes[16384]; // 16k write buffer
byte USBData_readBytes[16384]; // 16k read buffer
void setup() {
myusb.begin(); // initiate USB
delay(3000); // and wait a bit
}
void loop () {
myusb.Task();
USBDrive *pdrive=drive_list[0]; // initialise first (and only in this case) USB device
if (pdrive->msDriveInfo.connected==0) { // problem with USB device
Serial.println(" Problem with USB device \n");
}
else { // USB OK
if (writeFlag==1) { // write flag is true
mscError = myDrive.msWriteBlocks(startAddr, blocks, (uint16_t)512, USBData_writeBytes);
if (mscError==0) { // successful write
Serial.println("Successful write to USB device \n");
}
else { // failed write
Serial.println("Failed write to USB device \n");
}
}
if (readFlag==1) { // read flag is true
mscError = myDrive.msReadBlocks(startAddr, blocks, (uint16_t)512, USBData_readBytes);
if (mscError==0) { // successful read
Serial.println("Successful read from USB device \n");
}
else { // failed read
Serial.println("Failed to read from USB device \n");
}
}
} // usb ok
} // loop
The problem seems to be that if the USB media is removed during either the myDrive.msReadBlocks or myDrive.msWriteBlocks operations then the Teesny freezes and has to be physically reset.
Is the best method to catch this with a software driven reset from an timed interrupt method or via a threaded timer watching the state of the USB and initiating a software reset?
I was wondering if anybody else has encountered this problem?
Cheers