Skip to main content

Section 3.3 Configuring USART2 at the Register Level

Understanding what uart2_init() does inside prepares you to configure any UART peripheral in any STM32 project. There are four steps: configure the GPIO pins for alternate function, enable the USART clock, set the baud rate, and enable the transmitter, receiver, and peripheral (in that order).
The diagram below shows the internal architecture of the USART peripheral. The registers we’ll use are USART_TDR (transmit data register), USART_RDR (receive data register), USART_BRR (baud rate), USART_CR1 (control), and USART_ISR (status flags). The TX and RX shift registers handle the actual bit-by-bit serialization in hardware.
Figure 3.3.1. USART block diagram (from the STM32C031 reference manual). Writing to TDR loads the TxFIFO; the TX shift register serializes each byte onto the TX pin. Incoming bits are sampled into the RX shift register, assembled into bytes, and placed in RDR. The baudrate generator divides the APB clock to produce the bit clock.