Skip to main content
Visitor II
July 7, 2021
Question

STM32L4 how to get USB connect/disconnect events

  • July 7, 2021
  • 3 replies
  • 2517 views

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).

    This topic has been closed for replies.

    3 replies

    Super User
    July 7, 2021

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

    JW

    CZ.4Author
    Visitor II
    July 7, 2021

    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?

    Super User
    July 8, 2021

    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.

    CZ.4Author
    Visitor II
    July 8, 2021

    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?

    Super User
    July 8, 2021

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

    Super User
    July 8, 2021

    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