Subsection3.2.3Part 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.
Figure3.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.
Figure3.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.
// 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. The course’s header ES28.h defines it, with GPIO_INPUT (0), GPIO_OUTPUT (1) and GPIO_ANALOG (3).
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:
uint32_t tmp = GPIOA->MODER; // 1. read the 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
Pin n’s field starts at bit 2n: _Pos is 4 for PA2 and 6 for PA3. _Msk has a 1 in each bit of the field (0x30 for PA2).
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.
The next two slides show uart.c with all of its comments stripped out; the #include lines and the prototypes are left off. Annotate your section: what each line does, and why it is necessary.
Groups A & B take uart2_init (the GPIO and alternate-function lines, then the clock, BRR and CR1), and Group B also takes the two helper functions that compute BRR. Groups C & D take uart2_write, with the __io_putchar that calls it, and uart2_read.
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.