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

Maximum baud.

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

usart_dev * USART1

USART1 device.

usart_dev * USART2

USART2 device.

usart_dev * USART3

USART3 device.

usart_dev * UART4

UART4 device.

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)

Configure a serial port’s baud rate.

Parameters:
  • dev -

    Serial port to be configured

  • clock_speed -

    Clock speed, in megahertz.

  • baud -

    Baud rate for transmit/receive.

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 *) fn)

Call a function on each USART.

Parameters:
  • fn -

    Function to call.

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

USART_SR_LBD_BIT

USART_SR_TXE_BIT

USART_SR_TC_BIT

USART_SR_RXNE_BIT

USART_SR_IDLE_BIT

USART_SR_ORE_BIT

USART_SR_NE_BIT

USART_SR_FE_BIT

USART_SR_PE_BIT

USART_SR_CTS

USART_SR_LBD

USART_SR_TXE

USART_SR_TC

USART_SR_RXNE

USART_SR_IDLE

USART_SR_ORE

USART_SR_NE

USART_SR_FE

USART_SR_PE

Data register

USART_DR_DR

Baud Rate Register

USART_BRR_DIV_MANTISSA

USART_BRR_DIV_FRACTION

Control Register 1

USART_CR1_UE_BIT

USART_CR1_M_BIT

USART_CR1_WAKE_BIT

USART_CR1_PCE_BIT

USART_CR1_PS_BIT

USART_CR1_PEIE_BIT

USART_CR1_TXEIE_BIT

USART_CR1_TCIE_BIT

USART_CR1_RXNEIE_BIT

USART_CR1_IDLEIE_BIT

USART_CR1_TE_BIT

USART_CR1_RE_BIT

USART_CR1_RWU_BIT

USART_CR1_SBK_BIT

USART_CR1_UE

USART_CR1_M

USART_CR1_WAKE

USART_CR1_WAKE_IDLE

USART_CR1_WAKE_ADDR

USART_CR1_PCE

USART_CR1_PS

USART_CR1_PS_EVEN

USART_CR1_PS_ODD

USART_CR1_PEIE

USART_CR1_TXEIE

USART_CR1_TCIE

USART_CR1_RXNEIE

USART_CR1_IDLEIE

USART_CR1_TE

USART_CR1_RE

USART_CR1_RWU

USART_CR1_RWU_ACTIVE

USART_CR1_RWU_MUTE

USART_CR1_SBK

Control Register 2

USART_CR2_LINEN_BIT

USART_CR2_CLKEN_BIT

USART_CR2_CPOL_BIT

USART_CR2_CPHA_BIT

USART_CR2_LBCL_BIT

USART_CR2_LBDIE_BIT

USART_CR2_LBDL_BIT

USART_CR2_LINEN

USART_CR2_STOP

USART_CR2_STOP_BITS_1

USART_CR2_STOP_BITS_POINT_5

USART_CR2_STOP_BITS_1_POINT_5

USART_CR2_STOP_BITS_2

USART_CR2_CLKEN

USART_CR2_CPOL

USART_CR2_CPOL_LOW

USART_CR2_CPOL_HIGH

USART_CR2_CPHA

USART_CR2_CPHA_FIRST

USART_CR2_CPHA_SECOND

USART_CR2_LBCL

USART_CR2_LBDIE

USART_CR2_LBDL

USART_CR2_LBDL_10_BIT

USART_CR2_LBDL_11_BIT

USART_CR2_ADD

Control Register 3

USART_CR3_CTSIE_BIT

USART_CR3_CTSE_BIT

USART_CR3_RTSE_BIT

USART_CR3_DMAT_BIT

USART_CR3_DMAR_BIT

USART_CR3_SCEN_BIT

USART_CR3_NACK_BIT

USART_CR3_HDSEL_BIT

USART_CR3_IRLP_BIT

USART_CR3_IREN_BIT

USART_CR3_EIE_BIT

USART_CR3_CTSIE

USART_CR3_CTSE

USART_CR3_RTSE

USART_CR3_DMAT

USART_CR3_DMAR

USART_CR3_SCEN

USART_CR3_NACK

USART_CR3_HDSEL

USART_CR3_IRLP

USART_CR3_IRLP_NORMAL

USART_CR3_IRLP_LOW_POWER

USART_CR3_IREN

USART_CR3_EIE

Guard Time and Prescaler Register

USART_GTPR_GT

USART_GTPR_PSC