Universal Asynchronous Receiver Transmitter

The UART offers a very flexible, full-duplex, industrial standard, serial receiver / transmitter.
Including IrDA and modem operations (CTS/RTS).




Features


Configuration Registers

USART_CR1 - Configuration register 1




RE0Receiver disable (reset state)
1Receiver enable
TE0Transmitter disable (reset state)
1Transmitter enable
RXNEIE0Receiver not empty interrupt disable (reset state)
1Receiver not empty interrupt enable
TCIE0Transmission complete interrupt disable (reset state)
1Transmission complete enable
TXEIE0Transmitter empty interrupt disable (reset state)
1Transmitter empty interrupt enable
M08 data bits (reset state)
19 data bits enable
UE0USART disable (reset state)
1USART enable


USART_CR2 - Configuration register 2




STOP00 1 stop bit (reset state)
01 0,5 stop bit
10 2 stop bit
11 1,5 stop bit


USART_BRR - Baud rate register




FractionxDefines fraction of PCLK divider
MantissaxDefines Mantissa of the PCLK divider


Status Register

USART_SR - Status register




RXNE0Data is not received
1Received data is ready to be read
TC0Transmission not complete
1Transmission complete
TXE0Data is not transferred to the shift register
1Data transferred to the shift register


Data Register

USART_DR - Data register





Programming Example

Setup GPIO

#include "reg_stm32f4xx.h"
 
RCC->AHBENR[0] |= (0x1 << 0u);       /* Enable GPIOA clock */
 
/* Configure GPIO pin A.0 and A.1 in alternate function mode. */
GPIOA->MODER &= ~(0xf << 0u);        /* Clear existing mode bits 0 to 3. */
GPIOA->MODER |= (0xa << 0u);         /* Set pin 0..1 to alternate function mode. */
 
GPIOA->OSPEEDR &= ~(0xff << 0u);     /* Clear existing output speed bits 0 to 3. */
GPIOA->OSPEEDR |= (0xff << 0u);      /* Set pin 0..1 to high speed (100MHz) mode. */
 
GPIOA->AFR[0] &= ~(0xff << 0u);      /* Clear existing af bits 0 to 7. */
GPIOA->AFR[0] |= (0x88 << 0u);       /* Set pin 0..1 to AF8 (UART4). */


Transmitting Data

#include "reg_stm32f4xx.h"
 
RCC->APBENR[0] |= (0x1 << 19u);      /* Enable UART4 clock */
 
UART4->CR1 |= (0x1 << 13u);          /* Enable UART */
UART4->BRR |= (42000000 / 115200);   /* Set baudrate to 115'200 */
 
UART4->CR1 |= (0x1 << 3u);           /* Enable transmission */
UART4->DR = data_send[0];            /* Send first data */
while(!(UART4->SR & (0x1 << 7u)));   /* Wait til TXE flag is set */
UART4->DR = data_send[1];            /* Send first data */
while(!(UART4->SR & (0x1 << 7u)));   /* Wait til TXE flag is set */
UART4->DR = data_send[2];            /* Send last data */
while(!(UART4->SR & (0x1 << 6u)));   /* Wait til TC flag is set */


Receiving Data

#include "reg_stm32f4xx.h"
 
RCC->APBENR[0] |= (0x1 << 19u);      /* Enable UART4 clock */
 
UART4->CR1 |= (0x1 << 13u);          /* Enable UART */
UART4->BRR |= (42000000 / 115200);   /* Set baudrate to 115'200 */
 
UART4->CR1 |= (0x1 << 2u);           /* Enable receiver */
while(!(UART4->SR & (0x1 << 5u)));   /* Wait til RXNE flag is set */
data_received = UART4->DR            /* Read data */