cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L4 how to get USB connect/disconnect events

CZ.4
Associate II

I'm using a STM32L4A6 with OTG_FS peripheral. I use the USB driver from STM. I only get the suspend callback, but never resume, or connected, or disconnected callback. Suspend is not good indicator for plug in or out. Even USB cable plug in will also generate suspend event. I'd like to know when the device is plugged into or unplugged from a computer's USB port. How to do that? Thanks!

I'm using STM32 CubeL4 firmware package1.17.0 (and STM32L4xx HAL 1.13.0).

6 REPLIES 6

Detect VBUS presence. You can do it on any pin (after conditioning the VBUS voltage), not just the dedicated VBUS pin.

JW

Are you saying VBUS can be routed to a GPIO pin and then try to detect the pin to be 0 or 1? I checked out schematics it seems the VBUS pin (PA9 on the STM32L4A6) is not connected.

Also what are those call backs for - connected/disconnected callback?

TDK
Guru

You don't need to use the VBUS pin for this, you can use any GPIO pin, preferably with an inline resistor or other current limiting mechanism. You can set it up as EXTI and trigger on rising/falling edges to detect when the cable is connected/disconnected.

If you feel a post has answered your question, please click "Accept as Solution".
CZ.4
Associate II

Ok, I see.

But if the VBUS from the cable actually is connected to the VBUS pin, then the USB driver library will automatically detect the presence and trigger those callbacks?

I don't think the ST USB peripheral drivers do anything with the VBUS signal. Could be wrong.

If you feel a post has answered your question, please click "Accept as Solution".

The VBUS detection behaviour is described in the OTG chapter of RM, for device, in Peripheral states/Powered state subchapter/section. If VBUS detection is enabled (by OTG_GCCFG.VBDEN), it will automatically attach/detach the DP pullup (provided it's not forced by OTG_GUSBCFG.FDMOD or not switched to host by either OTG_GUSBCFG.FHMOD or by the ID pin being pulled low) and throw OTG_GINTSTS.SRQINT interrupt.

As said above, you can use any pin to detect VBUS presence and handle the pullup and USB machine "manually".

I don't care what's in Cube, I don't use it (other than an auxiliary source of information to RM). It's open source, you can look it up yourself.

JW