Skip to main content
AlaaM
Associate III
October 3, 2018
Solved

How to add code to HAL_PCD_ConnectCallback ?

  • October 3, 2018
  • 6 replies
  • 1846 views

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.
Best answer by AlaaM

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.

6 replies

waclawek.jan
Super User
October 3, 2018

> there's no option for user code there

And what, it's just a source file.

JW

AlaaM
AlaaMAuthor
Associate III
October 4, 2018

So how do I handle USB connection / disconnection event?

waclawek.jan
Super User
October 4, 2018

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
October 4, 2018

Yes that's exactly my concern. OK

AlaaM
AlaaMAuthorAnswer
Associate III
October 4, 2018

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
Technical Moderator
October 25, 2018

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
October 25, 2018

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