Keil Logo

CMSIS: Why is __I different in C and C++


Information in this knowledgebase article applies to:

  • CMSIS-Core

QUESTION

What is the reason for the different definition of the __I IO Type Qualifier in C and C++?

I noticed that __I IO Type Qualifier in the header files core_cmx.h are different for C and C++. However the other IO Type Qualifiers are identical. These definitions are used to define the peripheral registers for the various I/O registers in the device.h file.

#ifdef __cplusplus
  #define   __I   volatile             /*!< Defines 'read only' permissions */
#else
  #define   __I   volatile const       /*!< Defines 'read only' permissions */
#endif
#define     __O   volatile             /*!< Defines 'write only' permissions */
#define     __IO  volatile             /*!< Defines 'read / write' permissions */

What is the reason for the different definition of the __I IO Type Qualifier in C and C++?

ANSWER

In C the const type qualifier indicates that a variable is constant. However there is no further optimization to that variable.

In C++ variables that have a const type qualifier can be removed and instead of the variable a constant value can be inserted by the C++ compiler. This optimization would be fatal for peripheral registers and therefore the __I IO Type Qualifier in C++ lacks the const attribute. As a consequence, registers defined with __I are not write protected in C++.

Note:
Since CMSIS-Core V4.20 the macros __I, __O and __IO should only be used for scalar variables. For peripheral register definitions in structures, the macros __IM, __OM and __IOM should now be used. The definition __IM always contains the const type qualifier because C++ does not optimize struct-members to constants. This way, registers can be write-protected even in C++.

MORE INFORMATION

Last Reviewed: Friday, February 23, 2018


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.