cancel
Showing results for 
Search instead for 
Did you mean: 

How to add code to HAL_PCD_ConnectCallback ?

AlaaM
Associate III

I want to implement a callback for USB connection / disconnection.

I see that CubeMX generates HAL_PCD_ConnectCallback, as __weak in file stm32f7xx_hal_pcd.c, but the problem is that it's already defined in file usbd_conf.c. And there's no option for user code there. So how can I add my code to this callback?

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
AlaaM
Associate III

I solved the problem by editing the template file at:

C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeMX\db\templates\usbdconf_f7_c.ftl

I just deleted the implementation of HAL_PCD_ConnectCallback and HAL_PCD_DisconnectCallback in that file. Now I can write my own implementation in my files and it won't be overwritten.

View solution in original post

7 REPLIES 7

> there's no option for user code there

And what, it's just a source file.

JW

AlaaM
Associate III

So how do I handle USB connection / disconnection event?

As you've said it above: add your code to HAL_PCD_ConnectCallback in usbd_conf.c.

If your concern is that CubeMX overwrites that file in the future, well then deal with that problem - I simply don't use CubeMX.

JW

AlaaM
Associate III

Yes that's exactly my concern. OK

AlaaM
Associate III

I solved the problem by editing the template file at:

C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeMX\db\templates\usbdconf_f7_c.ftl

I just deleted the implementation of HAL_PCD_ConnectCallback and HAL_PCD_DisconnectCallback in that file. Now I can write my own implementation in my files and it won't be overwritten.

Nawres GHARBI
ST Employee

Hi Alaa,

HAL_PCD_ConnectCallback is declared as weak in the FW so you can declare it under your main .c and place it between user sections so your implementation will be kept

I can't do that because it's already implemented in usbd_conf.c (not as weak). So I can't have 2 implementations...