cancel
Showing results for 
Search instead for 
Did you mean: 

USB Device Connection and Disconnection

Nico3
Senior

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. 

 

 

8 REPLIES 8
AScha.3
Super User

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.
Super User

>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 ?

Nico3_0-1718822248112.png

 

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".
Totof43
Associate

I made some tests configuring PA9 with USB_OTG_FS_VBUS, with the recommended resistor bridge as per STM32.

Unfortunately, there was some kind of pull-down resistor configured for PA9 (may be by PCB has an issue?) 27k, and then the value read was 1.81V instead of 3.38V expected. I was missing something for sure.

Finally, detecting that the VBUS is 5V through the bridge but PA9 configured as logical input (no pul-up no pull-down) made the trick. That was sufficient for me to read the value in the main loop. Which means that any kind of GPIO digital input can do the job.

As I can change that easily the hardware design, I didn't want to go further and test other pins. I think that with a proper interrupt, it would be better than polling in the main loop.

I strongly recommend to use the resistor bridge, as under some condition, the pin could be damaged in transitory phases (e.g. power through USB)