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;

 

 

 

8 REPLIES 8
LCE
Principal

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 offsettof 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 and CPU cycles...)

 

 


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

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