Skip to main content
Visitor II
November 4, 2020
Question

how to detect usb is connected at startup ?

  • November 4, 2020
  • 1 reply
  • 1358 views

i am using stm32l072XX

I am using USB msc mode

found below functions to detect connection and disconnection of usb in file usbd_core.c

usbd_LL_suspend(***)

usbd_LL_resume(***)

they work properly when i insert or remove usb cable(i have added print statement in both functions)(connected one end to microcontroller and other to pc)

but when i keep usb cable connected and then power on the microcontroller, these functions are not called and my code doesn't knows that usb is connected.

usb is still working as i can see in my computer(get a mass storage device), but the code in microcontroller doesn't knows it as i am watching above two functions only to detect.

How to find at startup itself that USB is connected ?

    This topic has been closed for replies.

    1 reply

    Super User
    November 5, 2020

    For USB devices, one would typically monitor the VBUS pin and, if high, the cable is there and you should initialize USB. You should also deinitialize it when VBUS drops.

    You can of course blindly try to initialize USB on startup without checking for VBUS, which is technically not allowed for dual powered devices but probably won't hurt anything. The state of the USB should be reflected in one of its structures (USBD_HandleTypeDef).

    SA.17Author
    Visitor II
    November 5, 2020

    how to detect VBUS status using software ?

    cannot find VBUS in datasheet or reference manual ,but some functions are available with the name vbus in the code generated by cubemx