2018-10-03 04:49 AM
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
Solved! Go to Solution.
2018-10-04 12:34 AM
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.
2018-10-03 03:54 PM
> there's no option for user code there
And what, it's just a source file.
JW
2018-10-03 11:23 PM
So how do I handle USB connection / disconnection event?
2018-10-03 11:59 PM
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
2018-10-04 12:26 AM
Yes that's exactly my concern. OK
2018-10-04 12:34 AM
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.
2018-10-25 01:18 AM
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
2018-10-25 01:21 AM
I can't do that because it's already implemented in usbd_conf.c (not as weak). So I can't have 2 implementations...