Skip to main content
Graduate II
October 4, 2024
Solved

Feature request: Pass arbitrary data pointer in HAL callbacks

  • October 4, 2024
  • 5 replies
  • 2234 views

It is really sad that ST decided to not implement this requested and really helpful feature: https://github.com/STMicroelectronics/STM32CubeG4/issues/23

Maybe we can change their minds by creating some more attention to this topic here? Leave a comment, when you also find this useful ;)

Example solution for the I²C driver by adding a void* to the handle:

 

 

typedef struct __I2C_HandleTypeDef
{
/* Rest of struct... */
#if (USE_HAL_CALLBACK_CONTEXT == 1)
 void *UserContext;
#endif /* USE_HAL_CALLBACK_CONTEXT */
} I2C_HandleTypeDef;

 

 

 

    This topic has been closed for replies.
    Best answer by mƎALLEm

    Hello all,

    Getting back to you regarding this point and the dev team informed us that there is no plan to deploy this feature on the existing STM32CubeFW packages due to the heavy work to do considering the number of HAL drivers and STM32 series to cover.
    So customer must rely on his own implementation for such a need. But the point could be addressed for new products in the future.

    5 replies

    Graduate II
    October 4, 2024

    I find it useful.

    But for all the important stuff - and when memory size is small - I'm using direct register access and no HAL.

    Or I edit the HAL driver as I need them, as I don't let anything update these anyway.

    Super User
    October 4, 2024

    The context can be implemented in the "idiomatic Linux way": make the xxx_HandleTypeDef struct part of a bigger user defined struct, then use container_of to access the outer struct with all your custom fields. In C++ way, "subclass" the xxx_HandleTypeDef structs.

    IMHO ST cannot just add the user context pointer, because the other half of users that don't need it would object (waste of precious memory...)

     

     

    Super User
    October 4, 2024

    @Pavel A. wrote:

    IMHO ST cannot just add the user context pointer, because the other half of users that don't need it would object (waste of precious memory and CPU cycles...)


    As shown in the OP, it's optional:

    typedef struct __I2C_HandleTypeDef
    {
    /* Rest of struct... */
    #if (USE_HAL_CALLBACK_CONTEXT == 1)
     void *UserContext;
    #endif /* USE_HAL_CALLBACK_CONTEXT */
    } I2C_HandleTypeDef;

     So it would make no difference at all to those users who didn't want it.

    Technical Moderator
    October 4, 2024

    Hello @tdecker2 ,

    Thank you for your suggestion. 

    Just one question: why this needs to be in the activated "registered callback" context? couldn't be also used in the "weak" callbacks?

    tdecker2Author
    Graduate II
    October 7, 2024

    Doesn't need to be in the registered callback context as suggested at GitHub. Can be used for both callback variants.

    Technical Moderator
    October 7, 2024

    Hello,

    Proposal raised internally over an internal ticket 192982. I'll get back to you for any feedback. 

    Graduate II
    October 4, 2024

    Couldn't you just overload the structure with whatever you want? Put a structure in front or behind the one ST provides for? They just blindly pass a pointer, you can concatenate what ever you want with it.

    mƎALLEmAnswer
    Technical Moderator
    April 18, 2025

    Hello all,

    Getting back to you regarding this point and the dev team informed us that there is no plan to deploy this feature on the existing STM32CubeFW packages due to the heavy work to do considering the number of HAL drivers and STM32 series to cover.
    So customer must rely on his own implementation for such a need. But the point could be addressed for new products in the future.