4.1.5.5. USART on STM32F411
4.1.5.5.1. Overview of USART Functionality
USART (Universal Synchronous/Asynchronous Receiver/Transmitter) is a key feature in STM32F411 for serial communication. It enables bidirectional data exchange between the microcontroller and external devices like computers, sensors, or other microcontrollers.
4.1.5.5.2. Common Use Cases
Debugging via a serial monitor
Communication with peripheral devices
Wireless data transmission modules (e.g., Bluetooth or Wi-Fi)
4.1.5.5.3. Key Configurations
Baud rate: The speed of data transmission.
Data format: Frame format (number of data bits, parity, and stop bits).
Control registers: Settings for enabling transmission, reception, and interrupts.
4.1.5.5.4. USART Modes: Synchronous vs Asynchronous
Asynchronous Mode
Uses only two lines: TX (transmit) and RX (receive).
The transmitter and receiver operate independently, with communication timing controlled by the baud rate.
Commonly used in standard UART communication (e.g., serial terminals).
Synchronous Mode
Uses an additional clock line (SCLK) for synchronized communication between the transmitter and receiver.
The transmitter generates the clock, ensuring precise timing for data exchange.
Suitable for high-speed communication or scenarios where the clock signal needs to be shared.
Enabling Synchronous Mode
Set the USART_CR2.CLKEN bit to enable the clock signal.
Configure the clock polarity (CPOL) and phase (CPHA) in USART_CR2.
4.1.5.5.5. Registers and Settings for USART Communication
Baud Rate Configuration
The baud rate is set in the USART_BRR (Baud Rate Register). The exact formula to calculate the baud rate includes oversampling and is defined as:
\[Baud\ Rate = \frac{f_{PCLK}}{8 \times (2 - OVER8) \times USARTDIV}\]Where:
f_PCLK: Peripheral clock frequency (APB1 or APB2 clock, depending on the USART instance).
OVER8: Over-sampling factor (0 for 16x, 1 for 8x).
USARTDIV: Value set in the USART_BRR register, determined by the desired baud rate.
If OVER8 = 0, the formula simplifies to:
\[Baud\ Rate = \frac{f_{PCLK}}{16 \times USARTDIV}\]Control Registers
USART_CR1 (Control Register 1): Enables USART, selects word length, parity control, and interrupts.
USART_CR2 (Control Register 2): Configures stop bits, synchronous mode settings, and clock options.
USART_CR3 (Control Register 3): Configures flow control and DMA settings.
Data Register
USART_DR: Used for reading received data or writing data to be transmitted.
4.1.5.5.6. Practical Example: Setting Up USART for Asynchronous Communication
Enable USART Clock
RCC->APB2ENR |= RCC_APB2ENR_USART1EN; // Enable USART1 clock
Configure GPIO Pins for USART
GPIOA->MODER |= (2 << (9 * 2)) | (2 << (10 * 2)); // Set PA9 and PA10 to alternate function mode GPIOA->AFR[1] |= (7 << (1 * 4)) | (7 << (2 * 4)); // Set PA9 and PA10 to AF7 (USART)
Set Baud Rate
USART1->BRR = 16000000 / 9600; // Assuming 16 MHz clock and 9600 baud rate
Enable USART
USART1->CR1 |= USART_CR1_UE | USART_CR1_TE | USART_CR1_RE; // Enable USART, TX, and RX
Transmit Data
while (!(USART1->SR & USART_SR_TXE)); USART1->DR = 'A'; // Send character 'A'
Receive Data
while (!(USART1->SR & USART_SR_RXNE)); USART1->DR; // Read received data
4.1.5.5.7. Summary
USART in STM32F411 supports two modes:
Asynchronous Mode: Simple, widely used for serial communication without a shared clock.
Synchronous Mode: Adds a clock line for precise timing, ideal for high-speed or clock-sensitive applications.
The baud rate is configured using a precise formula to ensure accurate communication timing. This flexibility makes USART highly adaptable for various communication needs.