Feature request: Pass arbitrary data pointer in HAL callbacks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-10-04 6: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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-10-04 6: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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-10-04 6:19 AM - edited ‎2025-03-11 1:01 PM
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...)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-10-04 6: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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-10-04 7:16 AM - edited ‎2024-10-04 7: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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-10-04 8:43 AM - edited ‎2024-10-04 8: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.
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-10-07 1:11 AM
Hello,
Proposal raised internally over an internal ticket 192982. I'll get back to you for any feedback.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-03-11 8:14 AM
New request for the same feature:
older request (May 2021):
