cancel
Showing results for 
Search instead for 
Did you mean: 

HAL drivers suggestion: Add a user pointer to all peripheral handles

dhaselwood
Associate II

With the current "typedef struct" for peripheral handles, callback code that deals with multiple instances of a peripheral, such as uarts, CANs, etc., often has to do a lookup a lookup on the handle passed to it by the peripheral callback. If the handle included a pointer that the user could set, the callback lookup could be eliminated.

If the .h files for the drivers included "USER" sections, the user could insert a pointer, or add other elements to the struct, but since it appears there is no provision for user code sections in the handle definitions, a pointer would be a solution.

Obviously, one can go into the code at make the changes, but those changes are overwritten if STM32CubeMX is used to update the configuration.

13 REPLIES 13
S.Ma
Principal

C++ in C

Grouping elements related to the same functionality is also good when you want to debug them, you'll expand them with the [+] on the watch window....

dhaselwood
Associate II

After reviewing the comments and looking at some current code, adding a pointer to the handle typedef struct looks like is still the simplest overall approach. If this means modifying the CubeMX generated .h file, it is only one place that needs restoriation after regeneration with the current CubeMX/HAL drivers. If HAL drivers included a pointer, no restoration is needed after regeneration (and of course no added "USER" sections).

Some months ago when showing CubeMX code to a friend (who has a MS in computer science and 50+ year career of doing this kind of hw & sw), he said that he came around to always including a couple of user settable pointers in these types of structs as he had found that frequently it would turn out later they would be needed.

If CubeMX was used as a OTO code generation tool and no regeneration was to be done, the "SUPER struct" idea would be an alternative. Regeneration of the code involves more places needing restoration than adding the pointer to the typedef struct handle, making the (mistake free!) restoration a bit more of a task.

S.Ma
Principal

Can your friend share the most enlightening example from his experience here?

I will ask him the next time we get together. He has developed a health problem that makes it almost impossible to type at the keyboard, but his cognitive functioning is good. We are both getting rather ancient. (I wrote my first program in 1960 for an IBM 704 (vacuum tube machine.)

When this issue came up a few months ago he described a notable case where he said that including the user pointers proved to be very useful. IIRC, it had to do with medical monitoring equipment. He wrote the RTOS, roughly 30 yrs ago, and though it has been ported to new processors they are still using the same code.