Teensy 3 detect unplug USB cable

Status
Not open for further replies.

ftrias

Well-known member
My Teensy 3 is self-powered by a battery and I am trying to detect when the USB cable is unplugged from a host. When USB is plugged into a host, "usb_configuration" changes from 0 to 1. But when the cable is unplugged usb_configuration does not change back to 0. To fix this, I made a change to usb_dev.c in usb_isr() around line 937:

Code:
        if ((status & USB_ISTAT_SLEEP /* 10 */ )) {
                //serial_print("sleep\n");
                usb_configuration = 0;       // ADDED to reset current configuration
                USB0_ISTAT = USB_ISTAT_SLEEP;
        }

Is this a sensible way to go about it? Is there a better way?
 
Well another way would be attach VUSB with a voltage divider, and then feed that into a digital pin which you read, and when the pin returns LOW, you are no longer using the 5v power from USB.

Note, I don't know the USB support, but I could imagine that if you are using something like a USB charger that provides 5v power but no communication, that you wouldn't be able to tell when it is removed.
 
Status
Not open for further replies.
Back
Top