cancel
Showing results for 
Search instead for 
Did you mean: 

How to generate callback functions in STM32CubeMx?

MK8290
Associate II

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Paul1
Lead

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

View solution in original post

2 REPLIES 2
Peter BENSCH
ST Employee

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
Lead

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