Go Down

Topic: Teensy detecting USB active (Read 3370 times) previous topic - next topic

dlemmink

Is there any way for the Teensy (using teensyduino software) to detect if the USB port is connected?

I can use the Keyboard.print commands, but the sketch will freeze if I disconnect the usb port.

Coding Badly


Have you checked the datasheet for the processor?  I vaguely recall that the USB circuitry had several status flags available.

dlemmink

Great call....With a bit of research I found the status bit was available, but only if you enabled it using the following bit:

? 4 - OTGPADE: VBUS Pad Enable
Set to enable the VBUS pad. Clear to disable the VBUS pad.
Note that this bit can be set/cleared even if USBE=0. That allows the VBUS detection even if the
USB macro is disable.

It's in the USBCON register.

Once enabled you can check the status of the usb using the UDINT register.

Here's what I found worked:

Code: [Select]

#define USBCON 0E0
#define UDINT 0xD8

void setup(){

USBCON = USBCON | B00010000;

//setup code here!!!!
}

void loop(){

 if (UDINT & B00000001){

// USD Disconnected code here

 }
  else {

//USB Connected code here

 }

//LOOP code here

}



Thanks for the heading me in the right direction.  Now the code won't lock up if the usb port is disconnected while running.


Go Up