cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L4 USB Connect/Disconnect Detection

jacob2
Associate II
Posted on October 18, 2016 at 23:43

I'm working on USB on an STM32L4 chip and cannot seem to reliably trigger when a USB Device is both Connected and Disconnected. I trigger the interrupt overall and handle it, but I need changes to happen as soon as a device is connected and then handle some special case for when a USB is disconnected. USBD_LL_DevConnected() will trigger off reset for the very first connection of USB, but never again after when I unplug and re-plug in the USB and USBD_LL_DevDisconnected() never seems to trigger. Has anyone else found a way around this? Overall, my OTG_FS_IRQHandler() does get called and connects to the device correctly. Does anyone know if there is a setting I am missing that would enable the detection of both of these states?

Thank you.

#stm32 #stm32l4 #usb
3 REPLIES 3
Noyb
Associate II
Posted on October 19, 2016 at 08:42

Common way to do that is poll USB connection every second.

Walid FTITI_O
Senior II
Posted on October 19, 2016 at 10:19

Hi kenney.jacob

,

If you want to know if you have a device is either connected or disconnected, use can a global variable inside both  HAL_PCD_ConnectCallback() and

HAL_PCD_DisconnectCallback()

.

If USBD_LL_DevConnected status is USBD_OK you should change the value of the global variable to 1 (or whatever you prefer). 

You should write the code to update this global variable into every related callbacks.

HAL_PCD_CconnectCallback() is called inside HAL_PCD_IRQHandler() (stm32fxx_hal_pcd.c), by GOTGINT.SEDET (session end detected) interrupt

.

To ensure this interrupt is fired you need the following requirement:

  • VBUS (PA9) pin should be connected to VBUS (5V) pin of the USB connector (for OTG_HS on on-chip FS PHY, VBUS (PB13) pin)
  • Enable the Vbus sensing (vbus_sensing_enable is 'ENABLE'd in USBD_LL_Init() )

-Hannibal-

jacob2
Associate II
Posted on October 19, 2016 at 15:50

I have vbus_sensing_enable ENABLED and have VBUS (5V connection brought down to 3.3V to not damage the processor, that wouldn't cause a problem, would it?) connected to PA9. However, HAL_PCD_ConnectCallback() will only get called on the initial USB plugged in and never again when I unplug and re-plug in the device and HAL_PCD_DisconnectCallback() never gets triggered in this interrupt, are there additional settings elsewhere I may need?

Where can I configure the interrupt to trigger on both the rising and falling edge for the HAL_PCD_IRQHandler(&hpcd_USB_OTG_FS)?