Skip to main content
MK8290
Associate II
January 19, 2022
Solved

How to generate callback functions in STM32CubeMx?

  • January 19, 2022
  • 2 replies
  • 8783 views

Hi,

I am configuring an IO is GPIO_EXTI. I can't figure out how to generate the callback function. I looked into example projects and I saw this function. 0693W00000JMbmqQAD.pngHAL_GPIO_EXTI_Callback definitely looks like something generated by CubeMX. I would like to understand how to generate such callback functions.

Thanks.

This topic has been closed for replies.
Best answer by Paul1

STM32CubeMX Interrupt & Callback setup:

- Pinout & Configuration - System Core - NVIC – Code Generation – IRQ Handler.

  • For some interrupts it may be useful to generate interrupt handler code as examples, then copy to different file and disable the interrupt generation in MX so no clash.

- Project Manager - Advanced Settings - Register Callback (far right of page).

Paul

2 replies

Peter BENSCH
ST Technical Moderator
January 20, 2022

The callback functions are already defined in the HAL or LL library with the __weak attribute:

/**
 * @brief EXTI line detection callback.
 * @param GPIO_Pin: Specifies the port pin connected to corresponding EXTI line.
 * @retval None
 */
__weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
 /* Prevent unused argument(s) compilation warning */
 UNUSED(GPIO_Pin);
 
 /* NOTE: This function should not be modified, when the callback is needed,
 the HAL_GPIO_EXTI_Callback could be implemented in the user file
 */
}

In the library, the function is created as an empty function, so to speak, so that the linker does not throw an error. If you want to use such a function, you only need to define it in your code, e.g. in main.c - but without the __weak attribute, just like you showed in your screenshot.

You can find the basics of __weak in various places on the web, e.g. on Wikipedia. The use in the HAL or LL library is described in the user manual of the library of the respective STM32 family - just search for "description hal low" on st.com.

Good luck!

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
Paul1
Paul1Best answer
Senior III
January 20, 2022

STM32CubeMX Interrupt & Callback setup:

- Pinout & Configuration - System Core - NVIC – Code Generation – IRQ Handler.

  • For some interrupts it may be useful to generate interrupt handler code as examples, then copy to different file and disable the interrupt generation in MX so no clash.

- Project Manager - Advanced Settings - Register Callback (far right of page).

Paul