Skip to main content

Subsection 3.3.4 Step 4: Enable Transmitter, Receiver, and USART

USART_CR1 and USART_CR2 together control the frame format and operating mode. The reset value of 0x00000000 already gives us 8 data bits, 1 stop bit, no parity, and oversampling by 16, exactly what we need for 8N1 operation. The only bits we must set explicitly are TE (bit 3, transmitter enable), RE (bit 2, receiver enable), and UE (bit 0, USART enable). UE must be set after BRR: BRR can only be written while UE = 0, so if UE goes first the BRR write is ignored, the divisor stays at its reset value of 0, and the baud rate is wrong.
Figure 3.3.11. USART_CR1 (top) and USART_CR2 (bottom) with the relevant bits annotated. The reset value of 0x0000 0000 already selects oversampling by 16 (OVER8 = 0), 8 data bits (M0 = M1 = 0), no parity (PCE = 0), and 1 stop bit (STOP[1:0] = 00). The only bits to write are TE, RE and UE, with UE set after BRR is loaded.
// Enable receiver (RE) and transmitter (TE); BRR is already loaded
USART2->CR1 = USART_CR1_RE | USART_CR1_TE;
// Then enable the USART (UE), which locks BRR against further writes
USART2->CR1 |= USART_CR1_UE;