cancel
Showing results for 
Search instead for 
Did you mean: 

Feature request: Pass arbitrary data pointer in HAL callbacks

tdecker2
Associate III

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;

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
mƎALLEm
ST Employee

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.

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.

View solution in original post

11 REPLIES 11
LCE
Principal II

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.

Pavel A.
Evangelist III

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...)

 

 


@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.

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.
mƎALLEm
ST Employee

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?

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.

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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Hi @Pavel A.

yes of course. I'm starting to remember my Linux driver development days... Going to implement it that way until ST may add the user pointer to the struct.

"Other users" won't have overhead when the don't activate the #ifdef.

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

Hello,

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

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.

New request for the same feature:

https://community.st.com/t5/stm32-mcus-embedded-software/custom-context-in-callback-handlers/td-p/781688

 

older request (May 2021):

https://community.st.com/t5/stm32-mcus-products/is-there-a-way-to-provide-context-pointer-to-hal-callback/m-p/182064

 

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.