Skip to main content
Senior
June 15, 2024
Question

USB Device Connection and Disconnection

  • June 15, 2024
  • 3 replies
  • 2465 views

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. 

 

 

3 replies

AScha.3
Super User
June 15, 2024

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""."
Nico3Author
Senior
June 16, 2024

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

Nico3Author
Senior
June 19, 2024

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

 

Pavel A.
Super User
June 16, 2024

>I received warning

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

Visitor II
February 16, 2026

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)