Skip to main content
AlaaM
Associate III
Oct 3, 2018 11:49 AM UTC
Solved

How to add code to HAL_PCD_ConnectCallback ?

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

This topic has been closed for replies.
AlaaM
AlaaMBest answer
Associate III
Oct 4, 2018 7:34 AM UTC

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.

7 replies

waclawek.jan
Super User
Oct 3, 2018 10:54 PM UTC

> there's no option for user code there

And what, it's just a source file.

JW

AlaaM
AlaaMAuthor
Associate III
Oct 4, 2018 6:23 AM UTC

So how do I handle USB connection / disconnection event?

waclawek.jan
Super User
Oct 4, 2018 6:59 AM UTC

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
AlaaMAuthor
Associate III
Oct 4, 2018 7:26 AM UTC

Yes that's exactly my concern. OK

AlaaM
AlaaMAuthorBest answer
Associate III
Oct 4, 2018 7:34 AM UTC

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 Technical Moderator
Oct 25, 2018 8:18 AM UTC

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

AlaaM
AlaaMAuthor
Associate III
Oct 25, 2018 8:21 AM UTC

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