2024-10-04 06:00 AM
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;
2024-10-04 06:06 AM
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.
2024-10-04 06:19 AM - edited 2024-10-04 06:21 AM
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...)
2024-10-04 06:44 AM
@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.
2024-10-04 07:16 AM - edited 2024-10-04 07:19 AM
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?
2024-10-04 08:43 AM - edited 2024-10-04 08:44 AM
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.
2024-10-06 10:42 PM
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.
2024-10-06 10:43 PM
Doesn't need to be in the registered callback context as suggested at GitHub. Can be used for both callback variants.
2024-10-07 01:11 AM
Hello,
Proposal raised internally over an internal ticket 192982. I'll get back to you for any feedback.