Skip to main content
ALowe
Associate II
July 10, 2019
Question

How to set USE_HAL_***_REGISTER_CALLBACKS from CubeMX

  • July 10, 2019
  • 8 replies
  • 12365 views

In order to use the HAL's register callback capability, various flags are set for each peripheral in the stm32***_hal_config.h generated by CubeMX such as USE_HAL_FDCAN_REGISTER_CALLBACKS.

The issue is that these defines don't look for a prior define first, so it's not possible to override via compiler options etc and each time the project is regenerated it replaces this file.

Is there not a way that these can be set that won't be erased upon each generation?

Many thanks

This topic has been closed for replies.

8 replies

DK??k
Associate
August 2, 2019
GSome
Visitor II
August 2, 2019

Callback functions are defined as weak functions.

My understanding for this is if you define the same function name in your code this function will not be compiled but will use yours instead.

For example DMA half-transfer callback in my program.

In stm32f3xx_hal_adc.c callback function is defined as:

stm32f3xx_hal_adc.c

/**

 * @brief Conversion DMA half-transfer callback in non blocking mode

 * @param hadc ADC handle

 * @retval None

 */

__weak void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc)

{

 /* Prevent unused argument(s) compilation warning */

 UNUSED(hadc);

 /* NOTE : This function should not be modified. When the callback is needed,

           function HAL_ADC_ConvHalfCpltCallback must be implemented in the user file.

 */

}

In main.c I have this code, this is the code the system uses.

Note exact same wording for function except delete __weak

Do not change code in library.

void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc)

{

   pgmState = Finished_half_get_ADC;

}

ALowe
ALoweAuthor
Associate II
August 2, 2019

@GSome​ This isn't this issue that we're having, the issue is around generation of a pre-compiler flag that forces the code relating to HAL-level callbacks to be excluded from the build, preventing us from registering and using the callbacks full stop.

As an update I raised this in a support ticket with ST and was told it would be fed back to the development team.

ALowe
ALoweAuthor
Associate II
March 25, 2020

Come on guys, we reported this almost a year ago and it's still a pain in the ****. Any progress?

JWhit.7
Visitor II
August 11, 2020

It's in STM32CubeMX Project Manager > Advanced Settings > Register Callback

Credits to KnarfB who pointed it out

angus schmaloer
Associate III
February 10, 2021

was bout to reply this as well. yes, Project Manager > Advanced Settings > Register Callback, works fine

Mario Simunic
Associate III
March 18, 2021

Hi,

I'm using STM32CubeIDE 1.6.0. and STM32L053C8 device.

The project for the STM32L053C8 device is configured and generated using CubeIDE and integrated CubeMX configurator.

I can't find STM32CubeMX Project Manager > Advanced Settings > Register Callback in CubeMX.

I've searched on google and this forum about registering the Callback function, but without success in registering callback using methods described.

I'm not sure why in the case of HAL_TIM_PeriodElapsed_Callback() compiler attribute __weak isn't working!?

User redefinition of callback generated in file stm32l0xx_hal_tim.c results in errors because of multiple definitions, although generated callback has __weak attribute.

Defining USE_HAL_TIM_REGISTER_CALLBACKS 1 in project settings results in build with error due to multiple definitions of parameter USE_HAL_TIM_REGISTER_CALLBACKS.

Anyway, My solution is this:

In the file stm32l0xx_hal_conf.h I've forced #define USE_HAL_TIM_REGISTER_CALLBACKS    1U.

Then I wrote My own TIM21_IRQ Callback function void User_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim).

In main.c, after Timer21_Init is a call to register

MX_TIM21_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_RegisterCallback(&htim21, HAL_TIM_PERIOD_ELAPSED_CB_ID, User_TIM_PeriodElapsedCallback);
HAL_TIM_Base_Start_IT(&htim21);

This solves the problem about registering the callback, but every time the project code is regenerated by the CubeMX, I must modify the file stm32l0xx_hal_conf.h to set the #define USE_HAL_TIM_REGISTER_CALLBACKS 1U

Roberto C
Associate II
March 18, 2021

Hi Mario,

I'm using STM32CubeMx Version 6.2.0, and a DEV KIT NUCLEO-G491RE

In Project Manager....Advanced setting... there's a box on the right called "Register Callback". There you can find all callbacks you can ENABLE or DISABLE.

I tried to enable for FDCAN and after regenerating files, i can find in stm32g4xx_hal_conf.h the expected line:

#define USE_HAL_FDCAN_REGISTER_CALLBACKS   1U

Hope this can help you.

Best Regards.

Roberto.

Mario Simunic
Associate III
March 19, 2021

Roberto,

Yes, this is what I need and it helps.

Like I was blind that I didn't see the right side of the configurator :)

Thank you very much.

S.Ma
Principal
July 25, 2022

What cubemx is to add a compiler command option with this callbacks, and this is why it is not visible in source and header files. Beware, the callback by id will generate a switch case inside the time critical interrupt service routine, hence longer duration.

Piranha
Principal III
July 26, 2022

The ID is used only for configuration to indicate which function pointer is being set. Processing, including the ISR, doesn't need ID and just calls the callback function. On the contrary the older approach of using a single callback function for all peripheral instances requires the application to implement an if/switch to determine for which particular peripheral instance the callback is being called.

S.Ma
Principal
July 27, 2022

Also true and related to multiple instances. My comment is more related to the multiple interrupt sources of one peripheral. Transmit, receive, break, error, compare, overflow... etc

Piranha
Principal III
July 27, 2022

On the other hand splitting the events to separate functions often makes the code ridiculous. For example, for DMA events HT and TC one almost always needs the same processing code just with some index or address being different. With multiple callbacks one has to either copy the code, which is stupid and inefficient, or implement it in the third function, which need an additional parameter to signal from which callback it is being called and again sort it out in that third function, which is even more ridiculous.