usart.h

Universal Synchronous/Asynchronous Receiver/Transmitter (USART, or commonly serial port) support.

Types

struct usart_reg_map

USART register map type.

Public Members
__io uint32 SR

Status register.

__io uint32 DR

Data register.

__io uint32 BRR

Baud rate register.

__io uint32 CR1

Control register 1.

__io uint32 CR2

Control register 2.

__io uint32 CR3

Control register 3.

__io uint32 GTPR

Guard time and prescaler register.

struct usart_dev

USART device type.

Public Members
usart_reg_map * regs

Register map.

ring_buffer * rb

RX ring buffer.

uint32 max_baud

Deprecated.

Maximum baud rate.

uint8 rx_buf[USART_RX_BUF_SIZE]

Deprecated.

Actual RX buffer used by rb. This field will be removed in a future release.

rcc_clk_id clk_id

RCC clock information.

nvic_irq_num irq_num

USART NVIC interrupt.

Devices

struct usart_dev * USART1

USART1 device.

struct usart_dev * USART2

USART2 device.

struct usart_dev * USART3

USART3 device.

struct usart_dev * UART4

UART4 device.

struct usart_dev * UART5

UART5 device.

Functions

void usart_init(usart_dev * dev)

Initialize a serial port.

Parameters:
  • dev -

    Serial port to be initialized

void usart_set_baud_rate(usart_dev * dev, uint32 clock_speed, uint32 baud)

void usart_enable(usart_dev * dev)

Enable a serial port.

USART is enabled in single buffer transmission mode, multibuffer receiver mode, 8n1.

Serial port must have a baud rate configured to work properly.

Parameters:
  • dev -

    Serial port to enable.

See:

usart_set_baud_rate()

void usart_disable(usart_dev * dev)

Turn off a serial port.

Parameters:
  • dev -

    Serial port to be disabled

void usart_disable_all(void)

Disable all serial ports.

void usart_foreach(void(*)( usart_dev *dev) fn)

uint32 usart_rx(usart_dev * dev, uint8 * buf, uint32 len)

Nonblocking USART receive.

Parameters:
  • dev -

    Serial port to receive bytes from

  • buf -

    Buffer to store received bytes into

  • len -

    Maximum number of bytes to store

Return:

Number of bytes received

uint32 usart_tx(usart_dev * dev, const uint8 * buf, uint32 len)

Nonblocking USART transmit.

Parameters:
  • dev -

    Serial port to transmit over

  • buf -

    Buffer to transmit

  • len -

    Maximum number of bytes to transmit

Return:

Number of bytes transmitted

void usart_putudec(usart_dev * dev, uint32 val)

Transmit an unsigned integer to the specified serial port in decimal format.

This function blocks until the integer’s digits have been completely transmitted.

Parameters:
  • dev -

    Serial port to send on

  • val -

    Number to print

void usart_putc(usart_dev * dev, uint8 byte)

Transmit one character on a serial port.

This function blocks until the character has been successfully transmitted.

Parameters:
  • dev -

    Serial port to send on.

  • byte -

    Byte to transmit.

void usart_putstr(usart_dev * dev, const char * str)

Transmit a character string on a serial port.

This function blocks until str is completely transmitted.

Parameters:
  • dev -

    Serial port to send on

  • str -

    String to send

uint8 usart_getc(usart_dev * dev)

Read one character from a serial port.

It’s not safe to call this function if the serial port has no data available.

Parameters:
  • dev -

    Serial port to read from

Return:

byte read

See:

usart_data_available()

uint32 usart_data_available(usart_dev * dev)

Return the amount of data available in a serial port’s RX buffer.

Parameters:
  • dev -

    Serial port to check

Return:

Number of bytes in dev’s RX buffer.

void usart_reset_rx(usart_dev * dev)

Discard the contents of a serial port’s RX buffer.

Parameters:
  • dev -

    Serial port whose buffer to empty.

Register Map Base Pointers

USART1_BASE

USART1 register map base pointer.

USART2_BASE

USART2 register map base pointer.

USART3_BASE

USART3 register map base pointer.

UART4_BASE

UART4 register map base pointer.

UART5_BASE

UART5 register map base pointer.

Register Bit Definitions

Status Register

USART_SR_CTS_BIT

Clear to send bit.

USART_SR_LBD_BIT

Line break detection bit.

USART_SR_TXE_BIT

Transmit data register empty bit.

USART_SR_TC_BIT

Transmission complete bit.

USART_SR_RXNE_BIT

Read data register not empty bit.

USART_SR_IDLE_BIT

IDLE line detected bit.

USART_SR_ORE_BIT

Overrun error bit.

USART_SR_NE_BIT

Noise error bit.

USART_SR_FE_BIT

Framing error bit.

USART_SR_PE_BIT

Parity error bit.

USART_SR_CTS

Clear to send mask.

USART_SR_LBD

Line break detected mask.

USART_SR_TXE

Transmit data register empty mask.

USART_SR_TC

Transmission complete mask.

USART_SR_RXNE

Read data register not empty mask.

USART_SR_IDLE

IDLE line detected mask.

USART_SR_ORE

Overrun error mask.

USART_SR_NE

Noise error mask.

USART_SR_FE

Framing error mask.

USART_SR_PE

Parity error mask.

Data register

USART_DR_DR

Data register data value mask.

Baud Rate Register

USART_BRR_DIV_MANTISSA

Mantissa of USARTDIV mask.

USART_BRR_DIV_FRACTION

Fraction of USARTDIV mask.

Control Register 1

USART_CR1_UE_BIT

USART enable bit.

USART_CR1_M_BIT

Word length bit.

USART_CR1_WAKE_BIT

Wakeup method bit.

USART_CR1_PCE_BIT

Parity control enable bit.

USART_CR1_PS_BIT

Parity selection bit.

USART_CR1_PEIE_BIT

Parity error interrupt enable bit.

USART_CR1_TXEIE_BIT

Transmit data regsiter not empty interrupt enable bit.

USART_CR1_TCIE_BIT

Transmission complete interrupt enable bit.

USART_CR1_RXNEIE_BIT

RXNE interrupt enable bit.

USART_CR1_IDLEIE_BIT

IDLE interrupt enable bit.

USART_CR1_TE_BIT

Transmitter enable bit.

USART_CR1_RE_BIT

Receiver enable bit.

USART_CR1_RWU_BIT

Receiver wakeup bit.

USART_CR1_SBK_BIT

Send break bit.

USART_CR1_UE

USART enable mask.

USART_CR1_M

Word length mask.

USART_CR1_WAKE

Wakeup method mask.

USART_CR1_WAKE_IDLE

Wakeup on idle line.

USART_CR1_WAKE_ADDR

Wakeup on address mark.

USART_CR1_PCE

Parity control enable mask.

USART_CR1_PS

Parity selection mask.

USART_CR1_PS_EVEN

Parity selection: even parity.

USART_CR1_PS_ODD

Parity selection: odd parity.

USART_CR1_PEIE

Parity error interrupt enable mask.

USART_CR1_TXEIE

Transmit data register empty interrupt enable mask.

USART_CR1_TCIE

Transmission complete interrupt enable mask.

USART_CR1_RXNEIE

RXNE interrupt enable mask.

USART_CR1_IDLEIE

IDLE line interrupt enable mask.

USART_CR1_TE

Transmitter enable mask.

USART_CR1_RE

Receiver enable mask.

USART_CR1_RWU

Receiver wakeup mask.

USART_CR1_RWU_ACTIVE

Receiver wakeup: receiver in active mode.

USART_CR1_RWU_MUTE

Receiver wakeup: receiver in mute mode.

USART_CR1_SBK

Send break.

Control Register 2

USART_CR2_LINEN_BIT

LIN mode enable bit.

USART_CR2_CLKEN_BIT

Clock enable bit.

USART_CR2_CPOL_BIT

Clock polarity bit.

USART_CR2_CPHA_BIT

Clock phase bit.

USART_CR2_LBCL_BIT

Last bit clock pulse bit.

USART_CR2_LBDIE_BIT

LIN break detection interrupt enable bit.

USART_CR2_LBDL_BIT

LIN break detection length bit.

USART_CR2_LINEN

LIN mode enable mask.

USART_CR2_STOP

STOP bits mask.

USART_CR2_STOP_BITS_1

STOP bits: 1 stop bit.

USART_CR2_STOP_BITS_POINT_5

STOP bits: 0.5 stop bits Not available on UART4, UART5.

USART_CR2_STOP_BITS_1_POINT_5

STOP bits: 1.5 stop bits Not available on UART4, UART5.

USART_CR2_STOP_BITS_2

STOP bits: 2 stop bits.

USART_CR2_CLKEN

Clock enable.

Not available on UART4, UART5

USART_CR2_CPOL

Clock polarity mask.

Not available on UART4, UART5

USART_CR2_CPOL_LOW

Clock polarity: low.

USART_CR2_CPOL_HIGH

Clock polarity: high.

USART_CR2_CPHA

Clock phase mask.

Not available on UART4, UART5

USART_CR2_CPHA_FIRST

Clock phase: first First clock transition is the first data capture edge.

USART_CR2_CPHA_SECOND

Clock phase: second Second clock transition is the first data capture edge.

USART_CR2_LBCL

Last bit clock pulse mask.

When set, the last bit transmitted causes a clock pulse in synchronous mode.

Not available on UART4, UART5

USART_CR2_LBDIE

LIN break detection interrupt enable mask.

USART_CR2_LBDL

LIN break detection length.

USART_CR2_LBDL_10_BIT

LIN break detection length: 10 bits.

USART_CR2_LBDL_11_BIT

LIN break detection length: 11 bits.

USART_CR2_ADD

Address of the USART node This is useful during multiprocessor communication.

Control Register 3

USART_CR3_CTSIE_BIT

Clear to send interrupt enable bit.

USART_CR3_CTSE_BIT

Clear to send enable bit.

USART_CR3_RTSE_BIT

Ready to send enable bit.

USART_CR3_DMAT_BIT

DMA enable transmitter bit.

USART_CR3_DMAR_BIT

DMA enable receiver bit.

USART_CR3_SCEN_BIT

Smartcard mode enable bit.

USART_CR3_NACK_BIT

Smartcard NACK enable bit.

USART_CR3_HDSEL_BIT

Half-duplex selection bit.

USART_CR3_IRLP_BIT

IrDA low power bit.

USART_CR3_IREN_BIT

IrDA mode enable bit.

USART_CR3_EIE_BIT

Error interrupt enable bit.

USART_CR3_CTSIE

Clear to send interrupt enable Not available on UART4, UART5.

USART_CR3_CTSE

Clear to send enable Not available on UART4, UART5.

USART_CR3_RTSE

Ready to send enable Not available on UART4, UART5.

USART_CR3_DMAT

DMA enable transmitter Not available on UART5.

USART_CR3_DMAR

DMA enable receiver Not available on UART5.

USART_CR3_SCEN

Smartcard mode enable Not available on UART4, UART5.

USART_CR3_NACK

Smartcard NACK enable Not available on UART4, UART5.

USART_CR3_HDSEL

Half-duplex selection When set, single-wire half duplex mode is selected.

USART_CR3_IRLP

IrDA low power mode.

USART_CR3_IRLP_NORMAL

IrDA mode: normal.

USART_CR3_IRLP_LOW_POWER

IrDA mode: low power.

USART_CR3_IREN

IrDA mode enable.

USART_CR3_EIE

Error interrupt enable.

Guard Time and Prescaler Register

USART_GTPR_GT

Guard time value mask Used in Smartcard mode.

Not available on UART4, UART5.

USART_GTPR_PSC

Prescaler value mask Restrictions on this value apply, depending on the USART mode.

Not available on UART4, UART5.