Some things to pay attention to in embedded development

        Friends who develop using STM32 wonder if they have ever found such macro definitions? as follows:

#if defined (__CC_ARM)
#pragma anon_unions
#endif

        When I saw the above statement, I really didn’t understand why these things were written at first. After searching on the Internet, I figured out the principle of use.

        In the above code snippet we can see two parts:

        1)__CC_ARM

        2)#pragma anon_unions

        What are these two useful for? Wait for me to come one by one!

1. __CC_ARM is a macro option in ARM compilation

        __CC_ARM is a compiler option. In ARM development, there are several optional macro options depending on the development environment.

        These options can view the core file of the chip being developed. For example, I am using STM32F407, so I can check it from the core_cm4.h file:

#if   defined ( __CC_ARM )
  #define __ASM            __asm                                      /*!< asm keyword for ARM Compiler          */
  #define __INLINE         __inline                                   /*!< inline keyword for ARM Compiler       */
  #define __STATIC_INLINE  static __inline

#elif defined ( __GNUC__ )
  #define __ASM            __asm                                      /*!< asm keyword for GNU Compiler          */
  #define __INLINE         inline                                     /*!< inline keyword for GNU Compiler       */
  #define __STATIC_INLINE  static inline

#elif defined ( __ICCARM__ )
  #define __ASM            __asm                                      /*!< asm keyword for IAR Compiler          */
  #define __INLINE         inline                                     /*!< inline keyword for IAR Compiler. Only available in High optimization mode! */
  #define __STATIC_INLINE  static inline

#elif defined ( __TMS470__ )
  #define __ASM            __asm                                      /*!< asm keyword for TI CCS Compiler       */
  #define __STATIC_INLINE  static inline

#elif defined ( __TASKING__ )
  #define __ASM            __asm                                      /*!< asm keyword for TASKING Compiler      */
  #define __INLINE         inline                                     /*!< inline keyword for TASKING Compiler   */
  #define __STATIC_INLINE  static inline

#elif defined ( __CSMC__ )
  #define __packed
  #define __ASM            _asm                                      /*!< asm keyword for COSMIC Compiler      */
  #define __INLINE         inline                                    /*use -pc99 on compile line !< inline keyword for COSMIC Compiler   */
  #define __STATIC_INLINE  static inline

#endif

in:

        1) __CC_ARM corresponds to the ARM RealView development platform. It needs to be used in conjunction with development environments such as uvision, eclipse or CodeWarrior.

        2) __ICCARM__ corresponds to the IAR EWARM development environment, which is an integrated development environment developed by IAR Systems for ARM microprocessors.

        3) __GNUC__ corresponds to the GNU Compiler Collection development platform. GCC was originally a compiler specially written for the GNU operating system and is an open source software.

        4) __TASKING__ corresponds to the Altinum Designer development platform. Altium Designer is an integrated electronic product development system launched by Altium, the original Protel software developer.

2. #pragma anon_unions supports anonymous structures/unions

        Anonymous structure/union refers to an unnamed structure. Because there is no corresponding name, the object or variable will not be created directly. It is generally used in nested structures.

        The reason why we need to use this thing is because the gcc compiler supports the use of anonymous structures/combinations, but it is not supported in ARMCC, so if you want to use anonymous structures/combinations in ARMCC, just You need to use #pragma anon_unions to inform the compiler, otherwise the compilation will fail.

        So what exactly is an anonymous structure/union? Let me give you an example below, and you will understand it at a glance.

        The following code:

typedef union
{
        
        
    struct {
        
        
        uint32_t Address;  
        uint32_t OutputBits; 
        uint32_t ExtendedAddressBits; 
        uint32_t ExtendedOutputBit;  
        uint32_t FrameID;
        uint32_t EmptyBits;
    };
    uint32_t Value;
} StdFrame_t;

        As can be seen from the above, when defining a structure or union, if it is not named, then the structure/union is anonymous.

        Note: The #pragma directive is used to set the status of the compiler or instruct the compiler to complete some specific actions.

3. Add a few very good debugging operations

3.1、__LINE__

        __LINE__ is used to indicate the position information of this statement in the source file, that is, the line number of this statement in the source file.

3.2、__FILE__

        __FILE__ is used to indicate the file name of the source file where the statement in this line is located, that is, in which source file this statement appears.

3.3、__func__

        __func__ is used to indicate in which function a certain statement appears, that is, the function name of the function in which the statement appears can be output.

        Note: This keyword is not supported when using vc6.0 under windows.

Guess you like

Origin blog.csdn.net/weixin_43866583/article/details/128828723