It's a stupid macro. Define Assignments

Source: Internet
Author: User

First on the source code:

The file is portmacro.h, derived from the underlying code in the dynasty project (Nvic Interrupt control section)

1 #definePortnvic_int_ctrl_reg (* (volatile uint32_t *) 0xe000ed04ul)2 #definePortnvic_pendsvset_bit (1UL << 28UL)3 4 5 voidVportyield (void )6 {7     /*Set a PENDSV to request a context switch.*/8Portnvic_int_ctrl_reg =Portnvic_pendsvset_bit;9 Ten     /*barriers is normally not required but does ensure the code is completely One within the specified behaviour for the architecture.*/ A __DSB (); - __isb (); -}

This sentence makes me crazy.

1 portnvic_int_ctrl_reg = portnvic_pendsvset_bit;

A macro definition can also be assigned a value!!? The previous cognition is that the macro is defined as a constant, the constants can not be assigned, so the macro is generally on the right side of the operator!!

#define PORTNVIC_INT_CTRL_REG        (* (volatile uint32_t *) 0xe000ed04ul)

Although I can read this sentence, Portnvic_int_ctrl_reg represents the value in the 0xe000ed04ul address. To verify, the test procedure is as follows
1 #defineMacro_a (* (volatile uint32_t *) 0x20000300)2 #defineMacro_b 2003 #defineMacro_c (a)4 5 intA =Ten;6 7 voidmacrotest ()8 {9     intNval = -;Ten  OnePwapp_trace (("Macro_c:%d", Macro_c)); A  -Macro_c = -; -Pwapp_trace (("Macro_c:%d", Macro_c)); thePwapp_trace (("macro_a:%d", Macro_a)); -Pwapp_trace (("Address:%d",( * ( (volatileuint32_t *)0x20000300 )) )); -Macro_a =Macro_b; -  +Pwapp_trace (("macro_a:%d", Macro_a)); -Pwapp_trace (("Macro_b:%d", Macro_b)); +  AMacro_a =Nval; atPwapp_trace (("macro_a:%d", Macro_a)); -  -     return; -}

The result of the test is:

[Pwapp] [Macrotest], line[1230]>>>macro_c:20
[Pwapp] [Macrotest], line[1233]>>>macro_c:20
[Pwapp] [Macrotest], line[1235]>>>macro_a:50
[Pwapp] [Macrotest], line[1236]>>>address:0
[Pwapp] [Macrotest], line[1240]>>>macro_a:200
[Pwapp] [Macrotest], line[1241]>>>macro_b:200
[Pwapp] [Macrotest], line[1244]>>>macro_a:50

  The results show that you can actually assign a value to a macro.

Paste the definition of the macro:

Macro in computer science is a kind of title of batch processing. Generally, a macro is a rule or pattern, or syntax substitution, that describes how a particular input (usually a string) is converted to the corresponding output (usually a string) based on a predefined rule. This substitution occurs during precompilation, called macro expansion.

The interesting point in the discovery definition is this "syntax substitution".

When the macro defines the value itself as a constant, the macro is replaced by a constant.

such as:"#define Macro_b", Macro_b can not be re-assigned value.

When the macro definition itself represents a variable, the macro replaces the variable, and it also has the feature that the variable can be assigned to,

Such as

"     #define Macro_c (a)

int a = 10; "

At this point Macro_c replaces the shaping variable A, while the Macro_c can also be assigned a value.

and  #define MACRO_A (* (volatile uint32_t *) 0x20000300) is essentially the same as the # define Macro_c (A). * (Volatile uint32_t *) 0x20000300 and a represent the same meaning.

So, whether or not Marco can be assigned in the subsequent code depends on if the macro represents a constant or a variable when it is defined.

  In fact, there is a very important application problem, although macro can be re-assigned value, but in the actual code to use macro to replace the variable is appropriate? What about the application scenario? 

Often the use of macros instead of constants, because the definition of a macro can clearly represent the meaning of the constant.


The personal guess of the use of #define MACRO_A (* (volatile uint32_t *) 0x20000300) is that it allows direct access to the hardware address or driver register, and the chances of applying it in the underlying code are much higher. It also allows developers to know the meaning of the address clearly. As in previous

1 #define Portnvic_int_ctrl_reg        (* (volatile uint32_t *) 0xe000ed04ul)

is the representative of the Nvic Drive control register.


At present, the use of this macro assignment can be thought of the basic only these, there may be other application scenarios, I hope you can add ~

Finally, I wish everyone good health ~

It's a stupid macro. Define Assignments

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.