(Embedded) C: void function() or #define FUNCTION()
im programming embedded devices and I was wondering what to use for a macrofunction, for example an init of some registers. should i make this static/const or define it as a macro?
for example, this:
#define FPGA_INIT()\
{ \
/* Set function and direction of start_code pin*/\
P0SEL &= ~0x04; \
P0DIR |= 0x04; \
FPGA_START_CODE = 0; \
}
or this?
static void fpga_init()
{
/* Set function and direction of start_code pin*/
P0SEL &= ~0x04;
P0DIR |= 0x04;
FPGA_STAR开发者_StackOverflow中文版T_CODE = 0;
}
And what's the difference in memory-placement?
In general, it is best to reserve the use of preprocessor macros for times when you need the capability of the preprocessor to manipulate the code before it is compiled---you should not use preprocessor macros to define procedures that can be implemented as normal functions. Because the preprocessor substitutes the macro code wherever the macro is invoked, it can be difficult to debug errors that occur. For example, if you call your FPGA_INIT() macro, the (presumably) global variables P0SEL, P0DIR, and FPGA_START_CODE might be hidden by locals of the same name.
If you declare a fpga_init() function, the compiler will place the code for that function along with the code for other functions you declare, according to whatever rules it knows for the platform you are targeting. If you declare a FPGA_INIT() macro, the compiler will never know it exists, as all references to it will be resolved by the preprocessor; the compiler will see and compile the macro's statements separately within each function it was invoked.
Unless you need to call this code with great frequency (in an inner loop), the performance of the macro and function implementations will likely be indistinguishable. If you do need to call the code frequently, you should try measure the performance each way: depending on the architecture of your processor, it may be faster to use the preprocessor to place the code inline, or it may be faster to have the code in a separate function (particularly if expanding every invocation causes an important loop to overflow a cache line).
If you use a macro, then wherever you use it, the pre-processor will replace it with the full body of it. This means that if you call it say for 10 times, then you need 10 times more the memory. However the code will execute faster, since there isn't the overhead of making a function call.
I would in generally avoid using macros for functions. Macros are difficult to debug and maintain. A cleaner solution if you need in-place code is to use inline functions, which most modern compilers support.
A macro saves you the overhead of a function call during operation (speed).
A function saves you the overhead of having the same code in multiple places (program space).
Pick which of speed and space is more critical for you and use the approprate option.
Put the function into a header file and prefix with the inline
keyword:
inline void fpga_init()
{
/* Set function and direction of start_code pin*/
P0SEL &= ~0x04;
P0DIR |= 0x04;
FPGA_START_CODE = 0;
}
The inline keyword recommends that the compiler place the statements "in-inline" with other code, similar to a #define
macro. This is better than a macro because it behaves like a function and doesn't have the pitfalls of a macro.
Trust your compiler- make it a regular function, let the compiler pick what to do with it.
Consider using macro for init code to save memory space because the init code usually be removed after using.
精彩评论