Skip to main content

Subsection 3.2.3 Part 2: How Does the UART Driver Work?

The driver is written with names instead of bit numbers. A register is a row of bits divided into fields, and the reference manual gives every field a name. Figure 3.2.8 shows one: bit 0 of the RCC I/O port clock enable register, RCC_IOPENR, is the field GPIOAEN, which turns on the clock of port A.
Figure 3.2.8. The reference manual’s description of RCC_IOPENR (RM0490 §5.4.11). The field GPIOAEN is bit 0: writing a 1 enables the clock of GPIO port A.
The chip’s manufacturer also supplies a header file that gives the same field a name in C. It is called a CMSIS header, for Common Microcontroller Software Interface Standard, and ES28.h includes it, so you can use its names in your code. The names follow the manual’s: PERIPHERAL_REGISTER_FIELD, so RCC_IOPENR_GPIOAEN is the GPIOAEN field of the IOPENR register of RCC. Compare enabling the clock of port A with a hand-counted bit and with the name:
RCC->IOPENR |= (1U << 0);            // raw: which bit is GPIOA's?  Look it up.
RCC->IOPENR |= RCC_IOPENR_GPIOAEN;   // named: the name says which field
The name of a one-bit field is a mask with that one bit set, so RCC_IOPENR_GPIOAEN is the same as (1U << 0), which is 0x1. A field of several bits gets two more names. Figure 3.2.9 shows the mode field of pin 2, MODE2, which occupies bits 5 and 4 of GPIOx_MODER. Its position name, GPIO_MODER_MODE2_Pos, is the number of its lowest bit, 4. Its mask name, GPIO_MODER_MODE2_Msk, has a 1 in each bit of the field, 0x30. The manual’s table gives the values the field can hold: 00 input, 01 output, 10 alternate function, 11 analog. Here is the mode of PA2 set to alternate function with raw numbers and with names:
// raw: clear both bits of the field, then set it to 10
GPIOA->MODER &= ~(0x3U << 4);
GPIOA->MODER |=  (2U << 4);

// named: the same two statements
GPIOA->MODER &= ~GPIO_MODER_MODE2_Msk;                       // 0x30
GPIOA->MODER |=  (GPIO_ALTERNATE << GPIO_MODER_MODE2_Pos);   // 2 << 4
GPIO_ALTERNATE is 2, binary 10, the alternate-function value from the manual’s table. It is one of the course’s own names, defined in ES28.h along with GPIO_INPUT (0), GPIO_OUTPUT (1) and GPIO_ANALOG (3): each is a value to store in a field, not a mask. To store a value in a field, shift it by the field’s position.
Figure 3.2.9. The reference manual’s description of GPIOx_MODER (RM0490 §6.4.1). Each pin owns a two-bit field; MODE2, outlined, is bits 5 and 4. The values 00 to 11 select input, output, alternate function and analog.

Activity 3.2.3. Reading the Names.

Each name below follows the pattern PERIPHERAL_REGISTER_FIELD.

(c)

GPIO_MODER_MODE3_Pos: which register and which field does it belong to, and is it a mask or a position?

(d)

GPIO_AFRL_AFSEL2_Msk: which register and which field does it belong to, and is it a mask or a position?
Before we read the driver, we’ll look at how it changes the entire two-bit mode field of a pin with one statement. Previously, in the Blinky program, we set the two mode bits of PA5 one bit at a time, in two separate statements. Applied to the mode field of PA2, the same approach, a clear followed by a set, looks like this:
// before: two writes to the register
GPIOA->MODER &= ~GPIO_MODER_MODE2_Msk;
GPIOA->MODER |= (GPIO_ALTERNATE << GPIO_MODER_MODE2_Pos);
Pin n’s field starts at bit 2n, so the field of PA2 is bits 5 and 4. GPIO_MODER_MODE2_Pos is the position of the lowest bit of that field (4), and GPIO_MODER_MODE2_Msk has a 1 in each bit of the field (0x30). Both are names defined in the CMSIS (Common Microcontroller Software Interface Standard) headers that ES28.h includes. GPIO_ALTERNATE, defined in ES28.h, is 2, binary 10, the alternate-function mode. Between the two statements above, PA2’s field holds 00, which is input mode, so for a short time the pin is neither in its old mode nor in the new one. The driver avoids that intermediate state by writing the register once. Here is the driver’s change in three steps, using a temporary variable:
uint32_t tmp = GPIOA->MODER;                              // 1. read the whole register
tmp &= ~GPIO_MODER_MODE2_Msk;                             // 2. clear PA2's field, in tmp only
tmp |= (GPIO_ALTERNATE << GPIO_MODER_MODE2_Pos);          //    set the new value, in tmp only
GPIOA->MODER = tmp;                                       // 3. write the register once
Steps 1 and 2 change only the variable tmp, not GPIOA->MODER, so they have no effect on the pin. Only step 3 writes to the register, and the field then goes directly from its old value to its new one. uart.c combines the three steps into a single statement:
// driver: one write to the register
GPIOA->MODER = (GPIOA->MODER & ~GPIO_MODER_MODE2_Msk)
             | (GPIO_ALTERNATE << GPIO_MODER_MODE2_Pos);
Below is uart.c with all of its comments stripped out. We’ll split the driver between groups: annotate your section, explaining what each line does and, more importantly, why it is necessary. You’ll then teach your section to someone who annotated a different one, so aim for an annotation they could follow without you sitting next to them. A joins C, B joins D.
Open today’s slides on your laptop (the Slides link for Day 5) to see your group’s code while you work. Do not open the uart.c in your project’s mylib folder during the activity: it is the commented version, and its comments contain the answers.
#include "uart.h"
#include "ES28.h"

#define SYS_FREQ        12000000U
#define APB_CLK         SYS_FREQ
#define UART_BAUD_RATE  9600

static void uart_set_baudrate(USART_TypeDef *usartx, uint32_t periphClk, uint32_t baudRate);
static uint16_t compute_uart_bd(uint32_t periphClk, uint32_t baudRate);
void uart2_write(int ch);

// -- Group C ------------------------------------------------------
int __io_putchar(int ch) {
    uart2_write(ch);
    return ch;
}
// -----------------------------------------------------------------

void uart2_init(void) {

    // -- Group A --------------------------------------------------
    RCC->IOPENR |= RCC_IOPENR_GPIOAEN;

    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);

    GPIOA->AFR[0] = (GPIOA->AFR[0] & ~GPIO_AFRL_AFSEL2_Msk)
                  | (GPIO_AF1 << GPIO_AFRL_AFSEL2_Pos);

    GPIOA->AFR[0] = (GPIOA->AFR[0] & ~GPIO_AFRL_AFSEL3_Msk)
                  | (GPIO_AF1 << GPIO_AFRL_AFSEL3_Pos);
    // -------------------------------------------------------------

    // -- Group B --------------------------------------------------
    RCC->APBENR1 |= RCC_APBENR1_USART2EN;

    uart_set_baudrate(USART2, APB_CLK, UART_BAUD_RATE);

    USART2->CR1 = USART_CR1_RE | USART_CR1_TE;
    USART2->CR1 |= USART_CR1_UE;
    // -------------------------------------------------------------
}

// -- Group C ------------------------------------------------------
void uart2_write(int ch) {
    while (!(USART2->ISR & USART_ISR_TXE_TXFNF)) {}
    USART2->TDR = (ch & 0xFF);
}
// -----------------------------------------------------------------

// -- Group D ------------------------------------------------------
int uart2_read(void) {
    while (!(USART2->ISR & USART_ISR_RXNE_RXFNE)) {}
    return (USART2->RDR & 0xFF);
}
// -----------------------------------------------------------------

// -- Group B ------------------------------------------------------
static void uart_set_baudrate(USART_TypeDef *usartx, uint32_t periphClk, uint32_t baudRate) {
    usartx->BRR = compute_uart_bd(periphClk, baudRate);
}

static uint16_t compute_uart_bd(uint32_t periphClk, uint32_t baudRate) {
    return ((periphClk + (baudRate / 2U)) / baudRate);
}
// -----------------------------------------------------------------