Skip to main content

Subsection 3.3.1 Step 1: GPIO Alternate Function Configuration

The STM32C031C6 has two USART instances: USART1 (full feature set) and USART2 (basic feature set, more than enough for this course). Both can be routed to several different pins via the alternate function system, but on the Nucleo board only one set of pins per peripheral is connected via solder bridges to the ST-LINK MCU: for USART2 these are TX on PA2 and RX on PA3. Note that PA2 and PA3 do not appear on the Arduino-compatible headers, but that does not matter here, because the connection to the ST-LINK is made internally on the board, no external wiring is needed at all.
Figure 3.3.2. The NUCLEO-C031C6 board. The ST-LINK section (top) contains a separate microcontroller that acts as programmer and USB-to-serial bridge. PA2 (TX) and PA3 (RX) of the STM32C031C6 connect internally to the ST-LINK, which appears on your laptop as a USB serial device.
To use PA2 and PA3 for USART2, two register fields must be configured for each pin. First, the GPIOA_MODER register must be set to 10 (Alternate Function mode) for both pins.
Figure 3.3.3. GPIO port mode register (GPIOA_MODER). Each pin occupies two bits: 00 = Input, 01 = Output, 10 = Alternate Function, 11 = Analog. PA2 occupies bits [5:4] and PA3 occupies bits [7:6]; both must be set to 10.
Setting MODER to 10 puts a pin in Alternate Function mode, but the chip still needs to know which peripheral to route it to. The alternate function register GPIOA->AFR[0] (covering pins 0โ€“7) stores a 4-bit code for each pin: AF0โ€“AF15 each map to a different peripheral. The table below shows that PA2 and PA3 need AF1, which connects them to USART2.
Figure 3.3.4. Port A alternate function mapping (Table 13 from the STM32C031C6 datasheet). PA2 AF1 = USART2_TX; PA3 AF1 = USART2_RX. Writing 0001 into each pinโ€™s four-bit field in AFRL selects AF1.
Figure 3.3.5. GPIO Alternate Function Low Register (GPIOx_AFRL). Each pin occupies four bits; PA2 is at bits [11:8] and PA3 is at bits [15:12]. Writing 0001 to each field selects AF1 (USART2).
Figure 3.3.6. RCC I/O port clock enable register (RCC_IOPENR). Setting bit 0 (GPIOAEN) ungates the clock to GPIOA; without this, writes to MODER and AFR have no effect. Reset value is 0x0000 0000, all GPIO clocks off by default.
// Enable GPIOA clock
RCC->IOPENR |= RCC_IOPENR_GPIOAEN;

// Set PA2 and PA3 to Alternate Function mode (MODER = 10)
uint32_t tmp = GPIOA->MODER;           // read the whole register
tmp &= ~((3U << 4) | (3U << 6));       // clear bits for PA2, PA3 (in tmp only)
tmp |=  ((2U << 4) | (2U << 6));       // set AF mode (10) (in tmp only)
GPIOA->MODER = tmp;                    // one write to the register

// Select AF1 (USART2) for PA2 and PA3 in AFRL
tmp = GPIOA->AFR[0];
tmp &= ~((0xFU << 8) | (0xFU << 12));  // clear PA2, PA3 AF fields
tmp |=  ((1U  << 8) | (1U  << 12));    // AF1 = USART2
GPIOA->AFR[0] = tmp;
If you look at uart.c, you will see the same operations written differently: each pin is configured in its own statement, without a temporary variable, and the bit positions are given by named macros from CMSIS (Common Microcontroller Software Interface Standard) instead of by raw numbers. For example:
GPIOA->MODER = (GPIOA->MODER & ~GPIO_MODER_MODE2_Msk)
             | (GPIO_ALTERNATE << GPIO_MODER_MODE2_Pos);

GPIOA->MODER = (GPIOA->MODER & ~GPIO_MODER_MODE3_Msk)
             | (GPIO_ALTERNATE << GPIO_MODER_MODE3_Pos);
Both forms leave the same bits in the register. The driver style is easier to read at a glance: the macro names tell you which pin and which field are being touched without having to count bit positions yourself. GPIO_MODER_MODE2_Pos is the position of the lowest bit of PA2โ€™s field (4), and GPIO_MODER_MODE2_Msk is a mask with a 1 in each bit of that field (0x30). So ~GPIO_MODER_MODE2_Msk clears exactly PA2โ€™s two bits and leaves the other pins alone, and shifting the value left by _Pos moves it into that field.
In both forms each field is changed by a single write to the register, which matters for a pin that is already in use. The alternative is to apply the clear and the set to the register itself, with GPIOA->MODER &= ~GPIO_MODER_MODE2_Msk; followed by GPIOA->MODER |= ...;. Those are two separate writes, and between them PA2โ€™s field holds 00, which is input mode. For a short time the pin is neither in its old mode nor in the new one: it is an input, and whatever it was driving before is no longer driven. PA2 is in analog mode after reset and is not driving anything yet, so in uart2_init() the brief input state has no effect. On a pin that is already driving a transistor, a motor driver or a serial line, the same two statements stop driving that line briefly, which the connected device can respond to as a short pulse. With a temporary variable, or with the whole expression on the right-hand side of a single assignment, the intermediate value is never written to the register, and the field changes from its old value directly to its new one. uart.c changes the alternate function field in AFR in one write as well. The pin as a whole still passes through one intermediate state, because the mode field selects alternate function before AFR selects which one; after reset AFR holds AF0, so for an instant PA2 is routed to AF0 and not yet to USART2.