Showing results for 
Search instead for 
Did you mean: 

USB Device Connection and Disconnection


STM32F4 is working in USB Device MSC mode successfully.

I need to detect the USB connection state. I put the HAL_PCD_ConnectCallback function in main.c but on compiling I received warning.

Debug/../USB_DEVICE/Target/usbd_conf.c:307: multiple definition of `HAL_PCD_ConnectCallback';

I see it is weak in HAL_PCB_IRQHandler. Please suggest what I am missing. 



Chief II

Maybe not change the ConnectCallback ...try: 


USBD_Get_USB_Status() -> until : usb_status = USBD_OK;


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

I just realized.. i can simply use PA9 to detect. 

Pavel A.
Evangelist III

>I received warning

Read the compiler messages carefully, there must be reference to previous definition. 

STM32F4 is set in USB DEVICE MSC mode.

I am seeing LED is glowing even when USB is not connected to HOST laptop.  Intensity of LED glowing is low.  Anyway, on connection to HOST laptop, the LED glow at high intensity which makes sense.

I am not using PA9 for VBUS detection. I set PA9 in reset condition and also rise/fall edge detection with PD but still LED2 is glowing.

Does USB hardware in STM32 always provide some power to PA9 pin ?



If you have usb-device active, there is a pullup (1,5k) on DP , so host can check: there is something...

and you have the diode-array , so the "+" pullup comes to the LED. 

Thats the side-effect of your circuit with protection , just check voltage at VBUS, when not connected - and you see.

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

ok.. now it is clear to me why led is glowing.. thanks.. 

Now I am not sure how to detect USB connect & disconnect host, so I can interrupt my main application program. 

I can not use PA9 as EXTI trigger as it is always high.

Should I poll continuously USBD_Get_USB_Status() in main while loop as you advised earlier? 

I prefer call back interrupt function.

Depends on, what you want:

just check, is host cable connected ? Then use PA9 (and put resistor 1k at vusb-> GND, to pull it low enough)

or check logical "connect" to host ? Then check USB_Status, maybe every 500ms (nobody can connect or disconnect the cable faster...), or every 1000ms ; this will not keep your cpu "busy"...(maybe 0.1% cpu load )  🙂

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