Home > Software engineering >  Why does STMCubeMX use redundancy when defining bits?
Why does STMCubeMX use redundancy when defining bits?

Time:06-11

I was looking at how bits are defined in the header file generated by STM32CubeMX and this is what I found:

#define RCC_AHB1LPENR_GPIOALPEN_Pos        (0U)                                
#define RCC_AHB1LPENR_GPIOALPEN_Msk        (0x1UL << RCC_AHB1LPENR_GPIOALPEN_Pos) /*!< 0x00000001*/
#define RCC_AHB1LPENR_GPIOALPEN            RCC_AHB1LPENR_GPIOALPEN_Msk

What is the reason for this redundancy? Wouldn't this

#define RCC_AHB1LPENR_GPIOALPEN_Pos        (0U)                                
#define RCC_AHB1LPENR_GPIOALPEN            (0x1UL << RCC_AHB1LPENR_GPIOALPEN_Pos) /*!< 0x00000001*/

or this

#define RCC_AHB1LPENR_GPIOALPEN            (0x1UL << 0U) /*!< 0x00000001*/

work in the exact same way? I tried Search Text > Workspace (Ctrl Alt G), and position nor mask are used anywhere else in the header file...

CodePudding user response:

The *_Pos constant is needed for shifting your own values into the correct bitfield.

The name of the bitfield without _Msk is used by most code for historical reasons. The name of the bitfield with _Msk is a newer idea introduced to try to give more consistency.

There are some named constants defined which are bitfield values with other than all bits set, these do not have the _Msk suffix, but append another word to the register and bitfield name to idicate the purpose of the value.

CodePudding user response:

*_Pos definitions becomes especially useful when the setting/field in question is not a single bit but a collection of two or more bits, like GPIOx_MODER bits for example.

For single bit settings/fields, using the mask directly is easier and *_Pos definitions aren't needed.

  • Related