Skip to main content

Subsection 5.2.3 Part 2: STM32 Bit-Manipulation Macros

In this part we’ll rewrite our register writes so that the bit numbers come from named constants instead of from counting. Every register write you have done so far has used raw numeric constants: bit positions counted by hand, masks written as hex literals. That works, but a wrong shift value is invisible to the compiler and can be very hard to spot in a code review, which is why we’ll use names instead.
The CMSIS (Common Microcontroller Software Interface Standard) headers, which ES28.h includes for us, define a named macro for every bit field in every register. Each field gets a mask (_Msk) and a position (_Pos) constant, the names you decoded when you annotated the UART driver. The pattern is the same for the fields that configure a pin: read the register, clear the field, OR in the value shifted into place, and write the register once, as the UART driver (uart.c) does. In C this is a single statement, REG = (REG & ~FIELD_Msk) | (VALUE << FIELD_Pos); The raw version below writes the register three times; the macro version writes it once. To make this concrete, we will use the MODER register to show how to configure PA5 as an output pin.
Figure 5.2.1. The MODER register. Each pin has a 2-bit field. CMSIS defines GPIO_MODER_MODE5_Msk (the mask for pin 5’s field, 0xC00) and GPIO_MODER_MODE5_Pos (the bit position, 10), so you never need to count bits by hand. The field of pin 5 is outlined.
// Raw version - a wrong constant is a silent bug
GPIOA->MODER &= ~(1U << 11);  // clear bit 11 of the MODE5 field
GPIOA->MODER &= ~(1U << 10);  // clear bit 10 of the MODE5 field
GPIOA->MODER |=  (1U << 10);  // set bit 10 (output mode = 0b01)

// Macro version - names are checked by the compiler
GPIOA->MODER = (GPIOA->MODER & ~GPIO_MODER_MODE5_Msk)
             | (GPIO_OUTPUT << GPIO_MODER_MODE5_Pos);
The ES28 header defines convenient mode constants: GPIO_INPUT (0), GPIO_OUTPUT (1), GPIO_ALTERNATE (2), and GPIO_ANALOG (3).
Figure 5.2.2. The GPIO constants defined in ES28.h: the pin modes for MODER, the pull settings for PUPDR (none, pull-up, pull-down), and the alternate-function numbers GPIO_AF0 through GPIO_AF15. These are the course’s own definitions, not CMSIS names. Each is a field value, which is shifted into place with the CMSIS _Pos constant for that field.

Activity 5.2.2. Rewriting with Macros.

The code below configures PB5 as an output, the pin that will drive the transistor base in the lab. It is written with raw constants. Rewrite it using CMSIS macros.
// Enable GPIOB clock
RCC->IOPENR |= (1U << 1);

// Set PB5 to output mode
GPIOB->MODER &= ~(1U << 11);  // clear bit 11 of the MODE5 field
GPIOB->MODER &= ~(1U << 10);  // clear bit 10 of the MODE5 field
GPIOB->MODER |=  (1U << 10);  // set bit 10 (output mode = 0b01)

// Drive PB5 high
GPIOB->ODR |= (1U << 5);

(a)

Rewrite the three MODER lines as one statement that changes the register in a single write, using GPIO_MODER_MODE5_Msk, GPIO_MODER_MODE5_Pos, and GPIO_OUTPUT.

(b)

The RCC->IOPENR line enables the GPIOB clock with a raw bit shift. What CMSIS macro would replace (1U << 1)? (Hint: look at how GPIOA’s clock was enabled in the UART driver.)

(c)

Rewrite the ODR line using the CMSIS macro GPIO_ODR_OD5 instead of the raw bit shift (1U << 5).
The link below opens the simulator loaded with the code from this activity, placed inside a main() so that it runs. First attach an LED to PB5: click + LED in the component bay, then click the Arduino header pin D6, which is PB5. Run the program and note the values of RCC->IOPENR, GPIOB->MODER and GPIOB->ODR in the register panel. Then replace the raw constants with CMSIS macros and run it again. If your rewrite is right, the LED lights and all three registers hold the same values as before. Matching values show that your macros stand for the right bits. They do not show that you chose the right names. GPIO_PUPDR_PUPD5_Pos is 10, the same as GPIO_MODER_MODE5_Pos, so check that each name belongs to the register it is written to.
The simulator does not save your code, so use the download button (the downward arrow) to keep a copy of your work.