Skip to main content
LLea
Visitor II
May 22, 2019
Question

STMCubeIDE USE_HAL_***_REGISTER_CALLBACKS definition

  • May 22, 2019
  • 19 replies
  • 16429 views

Hi all,

I'd like to register some user defined callbacks using the HAL_***_RegisterCallback() functions.

I understood that first I need to define the compilation flag USE_HAL_***_REGISTER_CALLBACKS but where should I put it? Looking through the various examples I stepped into F4-Discovery DMA_FLASHtoRAM example and its stm32fxxx_hal_conf.h, copied below, which (hopefully) provides the answer.

Considering that i don't want to manually modify this file in my project (nor there is a user code section in it), how can i set the code generator to automatically insert the required defines?

Thanks,

Lorenzo

/* ########################### System Configuration ######################### */
/**
 * @brief This is the HAL system configuration section
 */ 
#define VDD_VALUE (3300U) /*!< Value of VDD in mv */
#define TICK_INT_PRIORITY (0x0FU) /*!< tick interrupt priority */
#define USE_RTOS 0U
#define PREFETCH_ENABLE 0U /* The prefetch will be enabled in SystemClock_Config(), depending on the used 
 STM32F405/415/07/417 device: RevA (prefetch must be off) or RevZ (prefetch can be on/off) */
#define INSTRUCTION_CACHE_ENABLE 1U
#define DATA_CACHE_ENABLE 1U
 
#define USE_HAL_ADC_REGISTER_CALLBACKS 0U /* ADC register callback disabled */
#define USE_HAL_CAN_REGISTER_CALLBACKS 0U /* CAN register callback disabled */
#define USE_HAL_CEC_REGISTER_CALLBACKS 0U /* CEC register callback disabled */
#define USE_HAL_CRYP_REGISTER_CALLBACKS 0U /* CRYP register callback disabled */
#define USE_HAL_DAC_REGISTER_CALLBACKS 0U /* DAC register callback disabled */
#define USE_HAL_DCMI_REGISTER_CALLBACKS 0U /* DCMI register callback disabled */
#define USE_HAL_DFSDM_REGISTER_CALLBACKS 0U /* DFSDM register callback disabled */
#define USE_HAL_DMA2D_REGISTER_CALLBACKS 0U /* DMA2D register callback disabled */
#define USE_HAL_DSI_REGISTER_CALLBACKS 0U /* DSI register callback disabled */
#define USE_HAL_ETH_REGISTER_CALLBACKS 0U /* ETH register callback disabled */
#define USE_HAL_HASH_REGISTER_CALLBACKS 0U /* HASH register callback disabled */
#define USE_HAL_HCD_REGISTER_CALLBACKS 0U /* HCD register callback disabled */
#define USE_HAL_I2C_REGISTER_CALLBACKS 0U /* I2C register callback disabled */
#define USE_HAL_FMPI2C_REGISTER_CALLBACKS 0U /* FMPI2C register callback disabled */
#define USE_HAL_I2S_REGISTER_CALLBACKS 0U /* I2S register callback disabled */
#define USE_HAL_IRDA_REGISTER_CALLBACKS 0U /* IRDA register callback disabled */
#define USE_HAL_LPTIM_REGISTER_CALLBACKS 0U /* LPTIM register callback disabled */
#define USE_HAL_LTDC_REGISTER_CALLBACKS 0U /* LTDC register callback disabled */
#define USE_HAL_MMC_REGISTER_CALLBACKS 0U /* MMC register callback disabled */
#define USE_HAL_NAND_REGISTER_CALLBACKS 0U /* NAND register callback disabled */
#define USE_HAL_NOR_REGISTER_CALLBACKS 0U /* NOR register callback disabled */
#define USE_HAL_PCCARD_REGISTER_CALLBACKS 0U /* PCCARD register callback disabled */
#define USE_HAL_PCD_REGISTER_CALLBACKS 0U /* PCD register callback disabled */
#define USE_HAL_QSPI_REGISTER_CALLBACKS 0U /* QSPI register callback disabled */
#define USE_HAL_RNG_REGISTER_CALLBACKS 0U /* RNG register callback disabled */
#define USE_HAL_RTC_REGISTER_CALLBACKS 0U /* RTC register callback disabled */
#define USE_HAL_SAI_REGISTER_CALLBACKS 0U /* SAI register callback disabled */
#define USE_HAL_SD_REGISTER_CALLBACKS 0U /* SD register callback disabled */
#define USE_HAL_SMARTCARD_REGISTER_CALLBACKS 0U /* SMARTCARD register callback disabled */
#define USE_HAL_SDRAM_REGISTER_CALLBACKS 0U /* SDRAM register callback disabled */
#define USE_HAL_SRAM_REGISTER_CALLBACKS 0U /* SRAM register callback disabled */
#define USE_HAL_SPDIFRX_REGISTER_CALLBACKS 0U /* SPDIFRX register callback disabled */
#define USE_HAL_SMBUS_REGISTER_CALLBACKS 0U /* SMBUS register callback disabled */
#define USE_HAL_SPI_REGISTER_CALLBACKS 0U /* SPI register callback disabled */
#define USE_HAL_TIM_REGISTER_CALLBACKS 0U /* TIM register callback disabled */
#define USE_HAL_UART_REGISTER_CALLBACKS 0U /* UART register callback disabled */
#define USE_HAL_USART_REGISTER_CALLBACKS 0U /* USART register callback disabled */
#define USE_HAL_WWDG_REGISTER_CALLBACKS 0U /* WWDG register callback disabled */

This topic has been closed for replies.

19 replies

DK??k
Associate
August 2, 2019

I have the same problem. As I tried to add a User Constant the compiler complained about redefinition of USE_HAL_SPI_REGISTER_CALLBACKS constant. Please tell how to mofify these!

MGerz
Associate
September 5, 2019

Same here. Is there any way to modify this through code or Device Configuration Tool (neé STM32CubeMX) or any hidden config file?

Also, is there any way to enable a peripherial in config tool, but do not generate ANY code for it?

DGuly
Visitor II
September 27, 2019

Have the same problem, that HAL conf header is reset every time code is generated through CubeMX, and I have to go in and change it back.

Pavel A.
September 27, 2019

Maybe user constants is where they designed to be, because CubeMX will know your preference to use callbacks when generating code.

User constants go to main.h

To avoid compiler complaints, define the user constants exactly as they are defined in other place, or remove duplicate definitions from your code.

-- pa

DGuly
Visitor II
September 27, 2019

As d.koekeny noted, that doesn't quite work. There is contradictory info in the SDK, too: there is a hal_conf_template.h in Drivers/HAL_Driver/Inc/ that is supposed to be copied and renamed into the application source folder, which is where CubeMX puts it, but then it also ignores edits to it and clobbers it when regenerating code.

scottSD
Senior III
May 27, 2020

And no answer to this?

TPeča.1
Associate
June 5, 2020

I came to the same issue, and if you want the short answer, it's not supported (at least up to the current version of STM32CubeIDE - v1.3.0).

Has been fixed in STM32CubeIDE v1.4.0, see answers below.

My current workaround is to copy the _hal_conf.h file to some other directory, modify it to my liking and add its path to the top of the Include directories list. I also needed to set the Is a workspace path option to keep the code generator from changing its order.

0693W000001qAs6QAE.png

Obviously, when you're doing any major reconfiguration (enabling/disabling peripherals etc.) you need to manually update your customized variant, which isn't ideal.

 See next post.

---

If you want the long answer: I did some digging around STM32CubeIDE files.

If you look at any of the internal template files for *_hal_conf.h at

stm32cubeide/plugins/com.st.stm32cube.common.mx_X.X.X.XXXXXXXXXXXX/db/templates/

(version numbers replaced with X)

The value of those defines is not part of a template, which means the code generator simply copies them as-is.

I also did some tests to check if I could override the template by adding a customized variant as a user template (these are partially described in STMCubeMX manual @ 6.3 Custom code generation, in STM32CubeIDE you configure it via Project Manager > Code Generator > Template Settings). It seemed like a neat idea, given that that user template configuration is saved as part of the ioc file.

What I've figured out is that while you can override the Cube-provided file, most internal templates fail to parse correctly (you get an empty file) since the code generator does not seem to provide the same context to user templates.

Soooo, they need to fix their internal templates.

TPeča.1
Associate
June 7, 2020

I've expanded on my previous solution by using user templates feature to patch the _hal_conf.h file with USER CODE sections.

This allows us to manually manage REGISTER_CALLBACKS section while the rest is managed by the code generator.

See the details here.

JThat.1
Visitor II
August 8, 2020

You can enable callbacks by defining the macro USE_HAL_UART_REGISTER_CALLBACKS in the Project->Properties dialog:

0693W000003OmLCQA0.png

KnarfB
Super User
August 8, 2020

At least not in recent versions of STM32CubeIDE. It will be redefined by generated code as described above.

JThat.1
Visitor II
August 8, 2020

I'm using CubeIDE 1.3.1 and setting the symbol above allows me to use HAL_UART_RegisterCallback() to register callbacks and is working fine. Maybe it depends on the specific target or options selected in CubeMX.

KnarfB
Super User
August 10, 2020

It's and old topic, but today i stumbled upon STM32CubeMX Project Manager > Advanced Settings > Register Callback where those settings can be configured.

TPeča.1
Associate
August 26, 2020

This feature has also been added to STM32CubeIDE 1.4.0 now.

Do note that the Register Callback pane might be obscured, you need to scroll right inside the device configuration window.

0693W000002lGMtQAM.png

scottSD
Senior III
October 28, 2020

While this does actually work, it is annoying that the comment in the file still says "register callback disabled" even though it is enabled. For example:

#define USE_HAL_DCMI_REGISTER_CALLBACKS 1U /* DCMI register callback disabled */

This is not a functionality issue, but it would be nice if the comment reflected what the setting actually is.

RaphyLoulou
Associate
December 21, 2020

Hello, I just want to point out a solution on same topic that was proposed here:

https://community.st.com/s/question/0D50X0000B436VS/how-to-set-usehalregistercallbacks-from-cubemx?t=1608561662426