USB Device has Malfunctioned

Status
Not open for further replies.

Spencez

Well-known member
I have a project in which I am powering a Teensy 4.0 with 5V directly from a power supply and I have the trace cut so I can still use the USB for acquiring data over serial. I noticed recently that when I programmed or unplugged + plugged in the USB I would get a message in Windows stating "USB device not recognized". I would have to press the button on the Teensy to program it or cycle the power and all would be well.

Note: As I was writing this post I actually solved this issue but I imagine this could help someone out in the future.

Here is an example scenario where this issue could arise. If you run this code and unplug the USB then plug it back in while the Teensy continues to have power it will give you the error message described and the Teensy will fail to function as a Serial device.

Code:
byte bytes[100];
void setup() {
  Serial.begin(115200);
  while(!Serial);
}

void loop() {
  delay(50);
  for(int i=0; i<100; i++)
  {
    bytes[i] = random(256);
  }
  noInterrupts();
  Serial.write(bytes, 100);
  interrupts();
}

This example doesn't reflect my code but the main problem is that I was disabling interrupts while calling Serial.write... I believe when I plugged the USB back in it was trying to initialize but Windows was receiving my data instead since interrupts were disabled at the same time I was writing. The reason I was disabling interrupts was to make a copy of data being retrieved in an interrupt to a temp variable in loop() before sending it out. But my mistake was leaving the Serial.write inside of the disabled interrupt block as well.

The lesson learned for me today is, continue to disable interrupts to copy data from a data acquisition interrupt before writing, but do NOT write out the data while the interrupts are disabled.
 
Last edited:
Status
Not open for further replies.
Back
Top