libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
USART peripheral API
Collaboration diagram for USART peripheral API:

Functions

void usart_set_baudrate (uint32_t usart, uint32_t baud)
 USART Set Baudrate. More...
 
void usart_set_databits (uint32_t usart, uint32_t bits)
 USART Set Word Length. More...
 
void usart_set_stopbits (uint32_t usart, uint32_t stopbits)
 USART Set Stop Bit(s). More...
 
void usart_set_parity (uint32_t usart, uint32_t parity)
 USART Set Parity. More...
 
void usart_set_mode (uint32_t usart, uint32_t mode)
 USART Set Rx/Tx Mode. More...
 
void usart_set_flow_control (uint32_t usart, uint32_t flowcontrol)
 USART Set Hardware Flow Control. More...
 
void usart_enable (uint32_t usart)
 USART Enable. More...
 
void usart_disable (uint32_t usart)
 USART Disable. More...
 
void usart_send_blocking (uint32_t usart, uint16_t data)
 USART Send Data Word with Blocking. More...
 
uint16_t usart_recv_blocking (uint32_t usart)
 USART Read a Received Data Word with Blocking. More...
 
void usart_enable_rx_dma (uint32_t usart)
 USART Receiver DMA Enable. More...
 
void usart_disable_rx_dma (uint32_t usart)
 USART Receiver DMA Disable. More...
 
void usart_enable_tx_dma (uint32_t usart)
 USART Transmitter DMA Enable. More...
 
void usart_disable_tx_dma (uint32_t usart)
 USART Transmitter DMA Disable. More...
 
void usart_enable_rx_interrupt (uint32_t usart)
 USART Receiver Interrupt Enable. More...
 
void usart_disable_rx_interrupt (uint32_t usart)
 USART Receiver Interrupt Disable. More...
 
void usart_enable_tx_interrupt (uint32_t usart)
 USART Transmitter Interrupt Enable. More...
 
void usart_disable_tx_interrupt (uint32_t usart)
 USART Transmitter Interrupt Disable. More...
 
void usart_enable_tx_complete_interrupt (uint32_t usart)
 USART Transmission Complete Interrupt Enable. More...
 
void usart_disable_tx_complete_interrupt (uint32_t usart)
 USART Transmission Complete Interrupt Disable. More...
 
void usart_enable_idle_interrupt (uint32_t usart)
 USART Idle Interrupt Enable. More...
 
void usart_disable_idle_interrupt (uint32_t usart)
 USART Idle Interrupt Disable. More...
 
void usart_enable_error_interrupt (uint32_t usart)
 USART Error Interrupt Enable. More...
 
void usart_disable_error_interrupt (uint32_t usart)
 USART Error Interrupt Disable. More...
 
void usart_enable_data_inversion (uint32_t usart)
 USART enable data inversion. More...
 
void usart_disable_data_inversion (uint32_t usart)
 USART disable data inversion. More...
 
void usart_enable_tx_inversion (uint32_t usart)
 USART Enable TX pin active level inversion. More...
 
void usart_disable_tx_inversion (uint32_t usart)
 USART Disable TX pin active level inversion. More...
 
void usart_enable_rx_inversion (uint32_t usart)
 USART Enable RX pin active level inversion. More...
 
void usart_disable_rx_inversion (uint32_t usart)
 USART Disable RX pin active level inversion. More...
 
void usart_enable_halfduplex (uint32_t usart)
 USART Enable Half-duplex. More...
 
void usart_disable_halfduplex (uint32_t usart)
 USART Disable Half-duplex. More...
 
void usart_set_rx_timeout_value (uint32_t usart, uint32_t value)
 USART Set receiver timeout value. More...
 
void usart_enable_rx_timeout (uint32_t usart)
 USART enable receive timeout function. More...
 
void usart_disable_rx_timeout (uint32_t usart)
 USART disable receive timeout function. More...
 
void usart_enable_rx_timeout_interrupt (uint32_t usart)
 USART enable receive timeout interrupt. More...
 
void usart_disable_rx_timeout_interrupt (uint32_t usart)
 USART disable receive timeout interrupt. More...
 
void usart_send (uint32_t usart, uint16_t data)
 USART Send a Data Word. More...
 
uint16_t usart_recv (uint32_t usart)
 USART Read a Received Data Word. More...
 
void usart_wait_send_ready (uint32_t usart)
 USART Wait for Transmit Data Buffer Empty. More...
 
void usart_wait_recv_ready (uint32_t usart)
 USART Wait for Received Data Available. More...
 
bool usart_get_flag (uint32_t usart, uint32_t flag)
 USART Read a Status Flag. More...
 

Detailed Description

Author
© 2009 Uwe Hermann uwe@h.nosp@m.erma.nosp@m.nn-uw.nosp@m.e.de

This library supports the USART/UART in the STM32F series of ARM Cortex Microcontrollers by ST Microelectronics.

Devices can have up to 3 USARTs and 2 UARTs.

Author
© 2016 Cem Basoglu cem.b.nosp@m.asog.nosp@m.lu@we.nosp@m.b.de

Function Documentation

◆ usart_disable()

void usart_disable ( uint32_t  usart)

USART Disable.

At the end of the current frame, the USART is disabled to reduce power.

Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses

Definition at line 195 of file usart_common_all.c.

References USART_CR1.

◆ usart_disable_data_inversion()

void usart_disable_data_inversion ( uint32_t  usart)

USART disable data inversion.

Logical data from the data register are send/received in positive/direct logic. (1=H, 0=L)

Note
This bit field can only be written when the USART is disabled.
Parameters
[in]usartUSART block register address base USART register base addresses

Definition at line 56 of file usart_common_v2.c.

References USART_CR2.

◆ usart_disable_error_interrupt()

void usart_disable_error_interrupt ( uint32_t  usart)

USART Error Interrupt Disable.

Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses

Definition at line 411 of file usart_common_all.c.

References USART_CR3.

◆ usart_disable_halfduplex()

void usart_disable_halfduplex ( uint32_t  usart)

USART Disable Half-duplex.

This bit field can only be written when the USART is disabled.

Parameters
[in]usartUSART block register address base USART register base addresses

Definition at line 147 of file usart_common_v2.c.

References USART_CR3.

◆ usart_disable_idle_interrupt()

void usart_disable_idle_interrupt ( uint32_t  usart)

USART Idle Interrupt Disable.

Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses

Definition at line 387 of file usart_common_all.c.

References USART_CR1.

◆ usart_disable_rx_dma()

void usart_disable_rx_dma ( uint32_t  usart)

USART Receiver DMA Disable.

Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses

Definition at line 259 of file usart_common_all.c.

References USART_CR3.

◆ usart_disable_rx_interrupt()

void usart_disable_rx_interrupt ( uint32_t  usart)

USART Receiver Interrupt Disable.

Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses

Definition at line 314 of file usart_common_all.c.

References USART_CR1.

◆ usart_disable_rx_inversion()

void usart_disable_rx_inversion ( uint32_t  usart)

USART Disable RX pin active level inversion.

RX pin signal works using the standard logic levels (VDD =1/idle, Gnd=0/mark)

This bit field can only be written when the USART is disabled.

Parameters
[in]usartUSART block register address base USART register base addresses

Definition at line 112 of file usart_common_v2.c.

References USART_CR2.

◆ usart_disable_rx_timeout()

void usart_disable_rx_timeout ( uint32_t  usart)

USART disable receive timeout function.

Note
If the USART does not support the Receiver timeout feature, this bit is reserved and forced by hardware to ‘0’.
Parameters
[in]usartUSART block register address base USART register base addresses

Definition at line 195 of file usart_common_v2.c.

References USART_CR2.

◆ usart_disable_rx_timeout_interrupt()

void usart_disable_rx_timeout_interrupt ( uint32_t  usart)

USART disable receive timeout interrupt.

Note
If the USART does not support the Receiver timeout feature, this bit is reserved and forced by hardware to ‘0’.
Parameters
[in]usartUSART block register address base USART register base addresses

Definition at line 224 of file usart_common_v2.c.

References USART_CR1.

◆ usart_disable_tx_complete_interrupt()

void usart_disable_tx_complete_interrupt ( uint32_t  usart)

USART Transmission Complete Interrupt Disable.

Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses

Definition at line 364 of file usart_common_all.c.

References USART_CR1.

◆ usart_disable_tx_dma()

void usart_disable_tx_dma ( uint32_t  usart)

USART Transmitter DMA Disable.

Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses

Definition at line 289 of file usart_common_all.c.

References USART_CR3.

◆ usart_disable_tx_interrupt()

void usart_disable_tx_interrupt ( uint32_t  usart)

USART Transmitter Interrupt Disable.

Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses

Definition at line 338 of file usart_common_all.c.

References USART_CR1.

◆ usart_disable_tx_inversion()

void usart_disable_tx_inversion ( uint32_t  usart)

USART Disable TX pin active level inversion.

TX pin signal works using the standard logic levels (VDD =1/idle, Gnd=0/mark)

Note
This bit field can only be written when the USART is disabled.
Parameters
[in]usartUSART block register address base USART register base addresses

Definition at line 84 of file usart_common_v2.c.

References USART_CR2.

◆ usart_enable()

void usart_enable ( uint32_t  usart)

USART Enable.

Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses

Definition at line 181 of file usart_common_all.c.

References USART_CR1, and USART_CR1_UE.

◆ usart_enable_data_inversion()

void usart_enable_data_inversion ( uint32_t  usart)

USART enable data inversion.

Logical data from the data register are send/received in negative/inverse logic. (1=L, 0=H). The parity bit is also inverted.

Note
This bit field can only be written when the USART is disabled.
Parameters
[in]usartUSART block register address base USART register base addresses

Definition at line 41 of file usart_common_v2.c.

References USART_CR2, and USART_CR2_DATAINV.

◆ usart_enable_error_interrupt()

void usart_enable_error_interrupt ( uint32_t  usart)

USART Error Interrupt Enable.

Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses

Definition at line 399 of file usart_common_all.c.

References USART_CR3, and USART_CR3_EIE.

◆ usart_enable_halfduplex()

void usart_enable_halfduplex ( uint32_t  usart)

USART Enable Half-duplex.

  • The TX and RX lines are internally connected.
  • The RX pin is no longer used
  • The TX pin is always released when no data is transmitted. Thus, it acts as a standard I/O in idle or in reception. It means that the I/O must be configured so that TX is configured as alternate function open-drain with an external pull-up.

Apart from this, the communication protocol is similar to normal USART mode. Any conflicts on the line must be managed by software

This bit field can only be written when the USART is disabled.

Parameters
[in]usartUSART block register address base USART register base addresses

Definition at line 135 of file usart_common_v2.c.

References USART_CR3, and USART_CR3_HDSEL.

◆ usart_enable_idle_interrupt()

void usart_enable_idle_interrupt ( uint32_t  usart)

USART Idle Interrupt Enable.

Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses

Definition at line 375 of file usart_common_all.c.

References USART_CR1, and USART_CR1_IDLEIE.

◆ usart_enable_rx_dma()

void usart_enable_rx_dma ( uint32_t  usart)

USART Receiver DMA Enable.

DMA is available on:

  • USART1 Rx DMA1 channel 5.
  • USART2 Rx DMA1 channel 6.
  • USART3 Rx DMA1 channel 3.
  • UART4 Rx DMA2 channel 3.
Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses

Definition at line 247 of file usart_common_all.c.

References USART_CR3, and USART_CR3_DMAR.

◆ usart_enable_rx_interrupt()

void usart_enable_rx_interrupt ( uint32_t  usart)

USART Receiver Interrupt Enable.

Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses

Definition at line 301 of file usart_common_all.c.

References USART_CR1, and USART_CR1_RXNEIE.

◆ usart_enable_rx_inversion()

void usart_enable_rx_inversion ( uint32_t  usart)

USART Enable RX pin active level inversion.

RX pin signal values are inverted. (VDD =0/mark, Gnd=1/idle).

This bit field can only be written when the USART is disabled.

Parameters
[in]usartUSART block register address base USART register base addresses

Definition at line 98 of file usart_common_v2.c.

References USART_CR2, and USART_CR2_RXINV.

◆ usart_enable_rx_timeout()

void usart_enable_rx_timeout ( uint32_t  usart)

USART enable receive timeout function.

Note
If the USART does not support the Receiver timeout feature, this bit is reserved and forced by hardware to ‘0’.
Parameters
[in]usartUSART block register address base USART register base addresses

Definition at line 182 of file usart_common_v2.c.

References USART_CR2, and USART_CR2_RTOEN.

◆ usart_enable_rx_timeout_interrupt()

void usart_enable_rx_timeout_interrupt ( uint32_t  usart)

USART enable receive timeout interrupt.

An interrupt is generated when the RTOF Flag is set in the USART_ISR register.

Note
If the USART does not support the Receiver timeout feature, this bit is reserved and forced by hardware to ‘0’.
Parameters
[in]usartUSART block register address base USART register base addresses

Definition at line 211 of file usart_common_v2.c.

References USART_CR1, and USART_CR1_RTOIE.

◆ usart_enable_tx_complete_interrupt()

void usart_enable_tx_complete_interrupt ( uint32_t  usart)

USART Transmission Complete Interrupt Enable.

Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses

Definition at line 351 of file usart_common_all.c.

References USART_CR1, and USART_CR1_TCIE.

◆ usart_enable_tx_dma()

void usart_enable_tx_dma ( uint32_t  usart)

USART Transmitter DMA Enable.

DMA is available on:

  • USART1 Tx DMA1 channel 4.
  • USART2 Tx DMA1 channel 7.
  • USART3 Tx DMA1 channel 2.
  • UART4 Tx DMA2 channel 5.
Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses

Definition at line 277 of file usart_common_all.c.

References USART_CR3, and USART_CR3_DMAT.

◆ usart_enable_tx_interrupt()

void usart_enable_tx_interrupt ( uint32_t  usart)

USART Transmitter Interrupt Enable.

Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses

Definition at line 326 of file usart_common_all.c.

References USART_CR1, and USART_CR1_TXEIE.

◆ usart_enable_tx_inversion()

void usart_enable_tx_inversion ( uint32_t  usart)

USART Enable TX pin active level inversion.

TX pin signal values are inverted. (VDD =0/mark, Gnd=1/idle).

Note
This bit field can only be written when the USART is disabled.
Parameters
[in]usartUSART block register address base USART register base addresses

Definition at line 70 of file usart_common_v2.c.

References USART_CR2, and USART_CR2_TXINV.

◆ usart_get_flag()

bool usart_get_flag ( uint32_t  usart,
uint32_t  flag 
)

USART Read a Status Flag.

Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses
[in]flagUnsigned int32. Status register flag USART_ISR Values.
Returns
boolean: flag set.

Definition at line 300 of file usart_common_v2.c.

References USART_ISR.

◆ usart_recv()

uint16_t usart_recv ( uint32_t  usart)

USART Read a Received Data Word.

If parity is enabled the MSB (bit 7 or 8 depending on the word length) is the parity bit.

Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses
Returns
unsigned 16 bit data word.

Definition at line 254 of file usart_common_v2.c.

References USART_RDR, and USART_RDR_MASK.

Referenced by usart_recv_blocking().

Here is the caller graph for this function:

◆ usart_recv_blocking()

uint16_t usart_recv_blocking ( uint32_t  usart)

USART Read a Received Data Word with Blocking.

Wait until a data word has been received then return the word.

Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses
Returns
unsigned 16 bit data word.

Definition at line 227 of file usart_common_all.c.

References usart_recv(), and usart_wait_recv_ready().

Here is the call graph for this function:

◆ usart_send()

void usart_send ( uint32_t  usart,
uint16_t  data 
)

USART Send a Data Word.

Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses
[in]dataunsigned 16 bit.

Definition at line 237 of file usart_common_v2.c.

References USART_TDR, and USART_TDR_MASK.

Referenced by usart_send_blocking().

Here is the caller graph for this function:

◆ usart_send_blocking()

void usart_send_blocking ( uint32_t  usart,
uint16_t  data 
)

USART Send Data Word with Blocking.

Blocks until the transmit data buffer becomes empty then writes the next data word for transmission.

Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses
[in]dataunsigned 16 bit.

Definition at line 211 of file usart_common_all.c.

References usart_send(), and usart_wait_send_ready().

Here is the call graph for this function:

◆ usart_set_baudrate()

void usart_set_baudrate ( uint32_t  usart,
uint32_t  baud 
)

USART Set Baudrate.

The baud rate is computed from the APB high-speed prescaler clock (for USART1/6) or the APB low-speed prescaler clock (for other USARTs). These values must be correctly set before calling this function (refer to the rcc_clock_setup-* functions in RCC).

Note: For LPUART, baudrates over 2**24 (~16.7 Mbaud) may overflow the calculation and are therefore not supported by this function.

Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses
[in]baudunsigned 32 bit. Baud rate specified in Hz.

Definition at line 53 of file usart_common_all.c.

References rcc_get_usart_clk_freq(), and USART_BRR.

Here is the call graph for this function:

◆ usart_set_databits()

void usart_set_databits ( uint32_t  usart,
uint32_t  bits 
)

USART Set Word Length.

The word length is set to 8 or 9 bits. Note that the last bit will be a parity bit if parity is enabled, in which case the data length will be 7 or 8 bits respectively.

Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses
[in]bitsunsigned 32 bit. Word length in bits 8 or 9.

Definition at line 89 of file usart_common_all.c.

References USART_CR1, and USART_CR1_M.

◆ usart_set_flow_control()

void usart_set_flow_control ( uint32_t  usart,
uint32_t  flowcontrol 
)

USART Set Hardware Flow Control.

The flow control bit can be selected as none, RTS, CTS or RTS+CTS.

Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses
[in]flowcontrolunsigned 32 bit. Flowcontrol USART Hardware Flow Control Selection.

Definition at line 165 of file usart_common_all.c.

References USART_CR3.

◆ usart_set_mode()

void usart_set_mode ( uint32_t  usart,
uint32_t  mode 
)

USART Set Rx/Tx Mode.

The mode can be selected as Rx only, Tx only or Rx+Tx.

Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses
[in]modeunsigned 32 bit. Mode USART Tx/Rx Mode Selection.

Definition at line 146 of file usart_common_all.c.

References USART_CR1.

◆ usart_set_parity()

void usart_set_parity ( uint32_t  usart,
uint32_t  parity 
)

USART Set Parity.

The parity bit can be selected as none, even or odd.

Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses
[in]parityunsigned 32 bit. Parity USART Parity Selection.

Definition at line 127 of file usart_common_all.c.

References USART_CR1.

◆ usart_set_rx_timeout_value()

void usart_set_rx_timeout_value ( uint32_t  usart,
uint32_t  value 
)

USART Set receiver timeout value.

Sets the receive timeout value in terms of number of bit duration. The USART_ISR_RTOF is set if, after the last received character, no new start bit is detected for more than the receive timeout value.

Note
The timeout value can also be written when USART is enabled. If the new value is lower/equals the internal hardware counter, the RTOF flag will be set.
Parameters
[in]usartUSART block register address base USART register base addresses
[in]valueThe receive timeout value in terms of number of bit duration.

Definition at line 166 of file usart_common_v2.c.

References USART_RTOR, USART_RTOR_RTO_MASK, and USART_RTOR_RTO_VAL.

◆ usart_set_stopbits()

void usart_set_stopbits ( uint32_t  usart,
uint32_t  stopbits 
)

USART Set Stop Bit(s).

The stop bits are specified as 0.5, 1, 1.5 or 2.

Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses
[in]stopbitsunsigned 32 bit. Stop bits USART Stop Bit Selection.

Definition at line 108 of file usart_common_all.c.

References USART_CR2.

◆ usart_wait_recv_ready()

void usart_wait_recv_ready ( uint32_t  usart)

USART Wait for Received Data Available.

Blocks until the receive data buffer holds a valid received data word.

Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses

Definition at line 285 of file usart_common_v2.c.

References USART_ISR, and USART_ISR_RXNE.

Referenced by usart_recv_blocking().

Here is the caller graph for this function:

◆ usart_wait_send_ready()

void usart_wait_send_ready ( uint32_t  usart)

USART Wait for Transmit Data Buffer Empty.

Blocks until the transmit data buffer becomes empty and is ready to accept the next data word.

Parameters
[in]usartunsigned 32 bit. USART block register address base USART register base addresses

Definition at line 270 of file usart_common_v2.c.

References USART_ISR, and USART_ISR_TXE.

Referenced by usart_send_blocking().

Here is the caller graph for this function: