cancel
Showing results for 
Search instead for 
Did you mean: 

how to detect usb is connected at startup ?

SA.17
Associate III

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 ?

2 REPLIES 2
TDK
Guru

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

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

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