(嵌入式)C:void function() 或 #define FUNCTION()

发布于 2024-09-06 18:12:35 字数 463 浏览 3 评论 0原文

我正在对嵌入式设备进行编程,我想知道宏函数要使用什么,例如一些寄存器的初始化。 我应该将其设为静态/常量还是将其定义为宏?

例如,这个:

 #define FPGA_INIT()\
{ \
  /* Set function and direction of start_code pin*/\
  P0SEL &= ~0x04; \
  P0DIR |= 0x04; \
  FPGA_START_CODE = 0; \
}

或者这个?

static void fpga_init()
{ 
  /* Set function and direction of start_code pin*/
  P0SEL &= ~0x04; 
  P0DIR |= 0x04; 
  FPGA_START_CODE = 0; 
}

内存放置有什么区别?

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_START_CODE = 0; 
}

And what's the difference in memory-placement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

dawn曙光 2024-09-13 18:12:35

一般来说,最好在需要预处理器在编译之前操作代码的时候保留使用预处理器宏——不应该使用预处理器宏来定义可以作为普通函数实现的过程。由于无论何时调用宏,预处理器都会替换宏代码,因此很难调试发生的错误。例如,如果您调用 FPGA_INIT() 宏,(大概)全局变量 P0SEL、P0DIR 和 FPGA_START_CODE 可能会被同名的本地变量隐藏。

如果您声明一个 fpga_init() 函数,编译器将根据它所知道的针对您目标平台的任何规则,将该函数的代码与您声明的其他函数的代码一起放置。如果您声明一个 FPGA_INIT() 宏,编译器将永远不会知道它的存在,因为对它的所有引用都将由预处理器解析;编译器将在调用的每个函数中单独查看并编译宏的语句。

除非您需要频繁调用此代码(在内部循环中),否则宏和函数实现的性能可能无法区分。如果您确实需要频繁调用代码,则应该尝试测量每种方式的性能:根据处理器的体系结构,使用预处理器内联代码可能会更快,或者让代码更快在单独的函数中(特别是如果扩展每个调用会导致重要循环溢出缓存行)。

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).

我乃一代侩神 2024-09-13 18:12:35

如果您使用宏,那么无论您在何处使用它,预处理器都会将其替换为宏的完整内容。这意味着如果你调用它10次,那么你需要10倍以上的内存。然而,代码执行速度会更快,因为没有进行函数调用的开销。

我通常会避免对函数使用宏。宏很难调试和维护。如果您需要就地代码,一个更干净的解决方案是使用 内联函数,大多数现代编译器都使用它支持。

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.

·深蓝 2024-09-13 18:12:35

宏可以节省操作过程中函数调用的开销(速度)。

函数可以节省您在多个位置(程序空间)使用相同代码的开销。

选择速度和空间中哪一个对您来说更重要,并使用适当的选项。

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.

水溶 2024-09-13 18:12:35

将函数放入头文件中,并添加 inline 关键字作为前缀:

inline void fpga_init()
{ 
  /* Set function and direction of start_code pin*/
  P0SEL &= ~0x04; 
  P0DIR |= 0x04; 
  FPGA_START_CODE = 0; 
}

inline 关键字建议编译器将语句与其他代码“内联”放置,类似于 #define宏。这比宏更好,因为它的行为类似于函数并且没有宏的缺陷。

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.

国粹 2024-09-13 18:12:35

相信你的编译器——让它成为一个常规函数,让编译器选择如何处理它。

Trust your compiler- make it a regular function, let the compiler pick what to do with it.

风苍溪 2024-09-13 18:12:35

考虑对初始化代码使用宏以节省内存空间,因为初始化代码通常在使用后被删除。

Consider using macro for init code to save memory space because the init code usually be removed after using.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文