How to detect USB device class port is disconnected from PC?

liudr

Well-known member
I know this is not a common use case that I disconnect teensy from PC but suppose teensy is powered by a battery thus disconnecting it from PC won't shut it off. I want to know how to detect disconnection. I was looking at void usb_isr(void) in usb.c but there's no mention of disconnection or maybe detached state. There's reset state which can happen without disconnection. I checked the USB_nUSBSTS field descriptions. There is no disconnected interrupt.

I looked into the manual and found a bit in the PHY that detects disconnection in device mode on USBPHYx_CTRLn:

5
DEVPLUGIN_POLARITY
For device mode, if this bit is cleared to 0, then it trips the interrupt if the device is plugged in. If
set to 1, then it trips the interrupt if the device is unplugged.

Or is it in USB_ANALOG_USB1_VBUS_DETECT_STAT such as the following?

2
AVALID
Indicates VBus is valid for a A-peripheral. This bit is a read only version of the state of the analog signal. It
can not be overritten by software.
 
I'm using teensyduino 1.55. Maybe this VBUS detection is not implemented? In usb_init():

// TODO: only enable when VBUS detected
// TODO: return to low power mode when VBUS removed
// TODO: protect PMU access with MPU
 
Back
Top