2024-09-19 11:44 PM - last edited on 2024-09-21 01:32 AM by Andrew Neil
Hi everyone,
I’ve noticed that STM32 drivers make extensive use of macros, and I’m curious about the reasoning behind this. Is the primary motivation is to increase performance / improve maintainability / are there other factors involved?
2024-09-20 07:01 AM
Because they work and take up no memory. It's common in C and has the least headaches with implementation as you don't need to resolve symbols at the linker stage. Also will be the fastest.
2024-09-20 07:13 AM - edited 2024-09-20 07:15 AM
(never mind)
2024-09-20 07:18 AM - edited 2024-09-20 07:24 AM
@sarun wrote:I’ve noticed that STM32 drivers make extensive use of macros, and I’m curious about the reasoning behind this
As @TDK said, it's pretty standard C practice.
Can you give an example, and say what alternative implementation(s) you would find less surprising?
PS:
Here you have someone asking for more macros:
https://community.st.com/t5/stm32cubeide-mcus/macro-for-different-dma-types/td-p/722443
2024-09-20 09:49 PM
Being the fastest makes sense, but the majority of macros I’ve seen are used for initialization purposes. Is there really a need for such speed during initialization? What would happen if we converted these macros to regular functions or static inline functions instead?
2024-09-20 09:55 PM
Hey, I’m not complaining—I just want to understand the reasoning behind it. Nowadays, many are aiming for MISRA code qualification, so for a product as comprehensive as the HAL, why not take a more compliant approach? From what I understand, using macros in this way may violate MISRA guidelines.
As for alternatives, I’m considering the use of inline functions or regular functions. However, I believe the decision would depend on the performance requirements.
2024-09-21 01:26 AM - edited 2024-09-21 01:31 AM
@sarun wrote:What would happen if we converted these macros to regular functions or static inline functions instead?
An issue with inline is that it's really just a hint to the compiler, so the result becomes is compiler dependant.
PS:
"inline does not force inlining; the compiler is free to choose not to inline the function at all, or only in some cases. Different compilers vary in how complex a function they can manage to inline"
and other issues:
https://en.wikipedia.org/wiki/Inline_function#Problems
2024-09-21 05:01 AM
It could be done other ways. Need is difficult to define when the project involves a multitude of projects.
Every programmer has a different opinion. Better to use what exists than try to change everything you come across.
> What would happen if we converted these macros to regular functions or static inline functions instead?
At the least, execution would be slower and compile/link time would increase, while the functionality would remain the same.
2024-09-21 02:02 PM - edited 2024-09-22 10:36 AM
> As for alternatives, I’m considering the use of inline functions
This is what the "modern C++" school teaches, and a good idea generally. But a simple example:
#define FOO() (*moo)
If macro FOO is not used in the compilation unit and moo is not defined, there's no problem. But if FOO is defined as inline it will compile and fail when moo is not defined (or has wrong type):
inline int FOO(void) { return *moo; }
2024-09-22 08:57 PM - edited 2024-09-22 11:50 PM
Based on the data I’ve read, we have two main options: either stick with the macro or switch to normal functions, In the long run using inline functions seems to be the least preferred solution.