libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
UART configuration

Enabling and configuring the UART More...

Collaboration diagram for UART configuration:

Functions

void uart_enable (uint32_t uart)
 Enable the UART. More...
 
void uart_disable (uint32_t uart)
 Disable the UART. More...
 
void uart_set_baudrate (uint32_t uart, uint32_t baud)
 Set UART baudrate. More...
 
void uart_set_databits (uint32_t uart, uint8_t databits)
 Set UART databits. More...
 
void uart_set_stopbits (uint32_t uart, uint8_t stopbits)
 Set UART stopbits. More...
 
void uart_set_parity (uint32_t uart, enum uart_parity parity)
 Set UART parity. More...
 
void uart_set_flow_control (uint32_t uart, enum uart_flowctl flow)
 Set the flow control scheme. More...
 
void uart_clock_from_piosc (uint32_t uart)
 Clock the UART module from the internal oscillator. More...
 
void uart_clock_from_sysclk (uint32_t uart)
 Clock the UART module from the system clock. More...
 

Detailed Description

Enabling and configuring the UART

Enabling the UART is a two step process. The GPIO on which the UART resides must be enabled, and the UART pins must be configured as alternate function, digital pins. Pins must also be muxed to the appropriate alternate function. This is done with the GPIO API.

The second step involves enabling and the UART itself. The UART should be disabled while it is being configured.

  1. The UART clock must be enabled with periph_clock_enable().
  2. The UART must be disabled with uart_disable().
  3. The UART clock source should be chosen before setting the baudrate.
  4. Baudrate, data bits, stop bit length, and parity can be configured.
  5. If needed, enable CTS or RTS lines via the uart_set_flow_control().
  6. The UART can now be enabled with uart_enable().

For example, to enable UART1 at 115200 8n1 with hardware flow control:

// Enable the UART clock
// We need a brief delay before we can access UART config registers
__asm__("nop"); __asm__("nop"); __asm__("nop");
// Disable the UART while we mess with its settings
// Configure the UART clock source as precision internal oscillator
// Set communication parameters
// Enable RTC and CTS lines
uart_set_flow_control(UART1, UART_FLOWCTL_HARD_RTS_CTS);
// Now that we're done messing with the settings, enable the UART
void periph_clock_enable(enum lm4f_clken periph)
Enable the clock source for the peripheral.
Definition: systemcontrol.c:27
@ RCC_UART1
void uart_disable(uint32_t uart)
Disable the UART.
Definition: uart.c:103
void uart_set_baudrate(uint32_t uart, uint32_t baud)
Set UART baudrate.
Definition: uart.c:114
void uart_set_parity(uint32_t uart, enum uart_parity parity)
Set UART parity.
Definition: uart.c:175
void uart_clock_from_piosc(uint32_t uart)
Clock the UART module from the internal oscillator.
Definition: uart.c:238
void uart_set_flow_control(uint32_t uart, enum uart_flowctl flow)
Set the flow control scheme.
Definition: uart.c:216
void uart_enable(uint32_t uart)
Enable the UART.
Definition: uart.c:93
void uart_set_databits(uint32_t uart, uint8_t databits)
Set UART databits.
Definition: uart.c:139
void uart_set_stopbits(uint32_t uart, uint8_t stopbits)
Set UART stopbits.
Definition: uart.c:160
@ UART_PARITY_NONE
Definition: uart.h:365
#define UART1
Definition: uart.h:50

Function Documentation

◆ uart_clock_from_piosc()

void uart_clock_from_piosc ( uint32_t  uart)

Clock the UART module from the internal oscillator.

Parameters
[in]uartUART block register address base UART register base addresses

Definition at line 238 of file uart.c.

References UART_CC, and UART_CC_CS_PIOSC.

◆ uart_clock_from_sysclk()

void uart_clock_from_sysclk ( uint32_t  uart)

Clock the UART module from the system clock.

Parameters
[in]uartUART block register address base UART register base addresses

Definition at line 248 of file uart.c.

References UART_CC, and UART_CC_CS_SYSCLK.

◆ uart_disable()

void uart_disable ( uint32_t  uart)

Disable the UART.

Parameters
[in]uartUART block register address base UART register base addresses

Definition at line 103 of file uart.c.

References UART_CTL.

◆ uart_enable()

void uart_enable ( uint32_t  uart)

Enable the UART.

Enable the UART. The Rx and Tx lines are also enabled.

Parameters
[in]uartUART block register address base UART register base addresses

Definition at line 93 of file uart.c.

References UART_CTL, UART_CTL_RXE, UART_CTL_TXE, and UART_CTL_UARTEN.

◆ uart_set_baudrate()

void uart_set_baudrate ( uint32_t  uart,
uint32_t  baud 
)

Set UART baudrate.

Parameters
[in]uartUART block register address base UART register base addresses
[in]baudBaud rate in bits per second (bps).*

Definition at line 114 of file uart.c.

References rcc_get_system_clock_frequency(), UART_CC, UART_CC_CS_PIOSC, UART_FBRD, and UART_IBRD.

Here is the call graph for this function:

◆ uart_set_databits()

void uart_set_databits ( uint32_t  uart,
uint8_t  databits 
)

Set UART databits.

Parameters
[in]uartUART block register address base UART register base addresses
[in]databitsnumber of data bits per transmission.

Definition at line 139 of file uart.c.

References UART_LCRH.

◆ uart_set_flow_control()

void uart_set_flow_control ( uint32_t  uart,
enum uart_flowctl  flow 
)

Set the flow control scheme.

Set the flow control scheme by enabling or disabling RTS and CTS lines. This will only have effect if the given UART supports the RTS and CTS lines.

Parameters
[in]uartUART block register address base UART register base addresses
[in]flowThe flow control scheme to use (none, RTS, CTS or both)
UART_FLOWCTL_RTS – enable the RTS line
UART_FLOWCTL_CTS – enable the CTS line
UART_FLOWCTL_RTS_CTS – enable both RTS and CTS lines

Definition at line 216 of file uart.c.

References UART_CTL, UART_CTL_CTSEN, UART_CTL_RTSEN, UART_FLOWCTL_CTS, UART_FLOWCTL_RTS, and UART_FLOWCTL_RTS_CTS.

◆ uart_set_parity()

void uart_set_parity ( uint32_t  uart,
enum uart_parity  parity 
)

Set UART parity.

Parameters
[in]uartUART block register address base UART register base addresses
[in]bitsthe requested parity scheme.

Definition at line 175 of file uart.c.

References UART_LCRH, UART_LCRH_EPS, UART_LCRH_PEN, UART_LCRH_SPS, UART_PARITY_EVEN, UART_PARITY_NONE, UART_PARITY_ODD, UART_PARITY_STICK_0, and UART_PARITY_STICK_1.

◆ uart_set_stopbits()

void uart_set_stopbits ( uint32_t  uart,
uint8_t  stopbits 
)

Set UART stopbits.

Parameters
[in]uartUART block register address base UART register base addresses
[in]bitsthe requested number of stopbits, either 1 or 2.

Definition at line 160 of file uart.c.

References UART_LCRH, and UART_LCRH_STP2.