cancel
Showing results for 
Search instead for 
Did you mean: 

How to set USE_HAL_***_REGISTER_CALLBACKS from CubeMX

ALowe
Associate II

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

14 REPLIES 14
DK??k
Associate II
GSome
Associate III

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
Associate II

@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
Associate II

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

JWhit.7
Associate

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

Credits to KnarfB who pointed it out

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

Mario Simunic
Associate III

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

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.

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.