detecting usb connection on a teensy 4.0

jrdarrah

Well-known member
On the teensy 3.2 I was able to detect if a USB cable was connected by testing a register USB0_OTGSTAT.

Code:
  if (!bitRead(USB0_OTGSTAT,5))
  {
    USBConnected = true;
  }

which is documented in K20 Sub-Family Reference Manual for: MK20DX64VLH7, MK20DX128VLH7, MK20DX256VLH7

a search for a similar manual on the arm website didn't turn up anything useful.

What would I need to check on the teensy 4.0 to detect the USB connected status?
Any ideas on where to find the ARM M7 reference manual would also be appreciated.
 
Thanks, this returned disconnected with the USB cable plugged in to my Windows PC in my sketch (below). The LED didn't change when I disconnected the cable. When I tried using bit 6 (DEVPLUGIN_STATUS - Indicates that the device has been connected on the USB_DP and USB_DM lines) it returned connected but when I disconnected the cable the LED didn't change

The K20 USB_OTGSTAT LINESTABLE field worked on the Teensy 3.2. I need something to test to see if a USB cable is present.

I tried several fields in the USB1_OTGSC field, related to session valid, or VBUS valid. None changed states when the cable was disconnected.

Code:
  if (!bitRead(USBPHY1_STATUS,8))
  {
    USBConnected = true;
  }


My test sketch

Code:
#define BlueLED 23
#define RedLED 22   

void setup() {
// Set up LED pins and turn them off
  pinMode(BlueLED, OUTPUT);               //port to drive LED
  pinMode(RedLED, OUTPUT);               //port to drive LED
  digitalWrite(RedLED, LOW);             
  digitalWrite(BlueLED, HIGH);
  Serial.begin(115400);
}

void loop() {
   if (!bitRead(USBPHY1_STATUS,8))
     {
      //usb connected
        digitalWrite(RedLED,LOW); 
        digitalWrite(BlueLED, HIGH);
     } 
     else
     {
      //usb disconnected
        digitalWrite(RedLED, HIGH); 
        digitalWrite(BlueLED, LOW);     
     }

}
 
What finally worked was to check the suspend flag in the PORTSC1 register. 1 = Suspend state, 0 = not in Suspend state.

Code:
   if (!bitRead(USB1_PORTSC1,7))
     {
      //usb connected
 ;
     } 
     else
     {
      //usb disconnected
    
     }
 
What finally worked was to check the suspend flag in the PORTSC1 register. 1 = Suspend state, 0 = not in Suspend state.

Code:
   if (!bitRead(USB1_PORTSC1,7))
     {
      //usb connected
 ;
     } 
     else
     {
      //usb disconnected
    
     }

That's awesome and greatly cleans up the kludge I had been using previously. Thank you!
 
Back
Top