Here is an example from a ST CMSIS header:
#define USART_ISR_TC_Pos (6U)
#define USART_ISR_TC_Msk (0x1UL << USART_ISR_TC_Pos)
Everywhere in CMSIS headers, the bitfield positions (_Pos
) are given as decimal integer constants of type unsigned int
and the unshifted masks are unsigned long int
.
Why is it that they both are not specified as unsigned long int
?
Bit position: Position in the register cannot be more than 31 and any integer type in C can hold it. There is no reason even to make the position unsigned.
The mask. As the minimum unsigned int
size required by the C standard is not big enough to hold 32 bit value, It has to be declared as unsigned long. CMSIS authors do not know the compiler you are going to use so they use the minimal sufficient type.