USART - Synchronous/Asynchronous Serial#

Universal Synchronous/Asynchronous Receiver/Transmitter Peripheral API.


Introduction#

The Universal Synchronous/Asynchronous Receiver/Transmitter (USART) is a very flexible serial I/O module. It supports full duplex asynchronous UART communication as well as RS-485, SPI, MicroWire, and 3-wire. It can also interface with ISO7816 Smart-Cards, and IrDA devices.

The USART has a wide selection of operating modes, frame formats, and baud rates. All features are supported through the API of this module.

Triple buffering and DMA support makes high data-rates possible with minimal CPU intervention. It is possible to transmit and receive large frames while the MCU remains in EM1 Sleep.

This module does not support DMA configuration. The UARTDRV and SPIDRV drivers provide full support for DMA and more.

The following steps are necessary for basic operation:

Note

  • UARTDRV supports all types of UART flow control. Software assisted hardware flow control is available for parts without true UART hardware flow control.


Example#

USART Asynchronous UART TX example:

{
  #define GPIO_TX    PB0

  sl_hal_usart_async_init_t init = SL_HAL_USART_INIT_ASYNC_DEFAULT;

  // Configure the clocks.
  sl_clock_manager_enable_bus_clock(SL_BUS_CLOCK_USART0);
  sl_clock_manager_enable_bus_clock(SL_BUS_CLOCK_GPIO);

  // Calculate and configure the usart's clock divider.
  uint32_t baudrate = 38400;  // Desired baudrate.
  sl_clock_branch_t clock_branch = sl_device_peripheral_get_clock_branch(SL_PERIPHERAL_USART0);
  uint32_t freq;
  sl_clock_manager_get_clock_branch_frequency(clock_branch, &freq);
  init.clock_div = sl_hal_usart_async_calculate_clock_div(freq, baudrate, init.oversampling);

  // Configure the USART port.
  sl_gpio_set_pin_mode(GPIO_TX, SL_GPIO_MODE_PUSH_PULL, 0);
  GPIO->USARTROUTE[USART_NUM(USART0)].TXROUTE = (GPIO_TX->port << _GPIO_USART_TXROUTE_PORT_SHIFT)
                                              | (GPIO_TX->pin << _GPIO_USART_TXROUTE_PIN_SHIFT);
  GPIO->USARTROUTE[USART_NUM(USART0)].ROUTEEN = GPIO_USART_ROUTEEN_TXPEN;

  // Initialize the USART.
  sl_hal_usart_init_async(USART0, &init);
  sl_hal_usart_enable(USART0);
  sl_hal_usart_enable_tx(USART0);

  uint16_t data = 0xFF;
  sl_hal_usart_tx(USART0, data);
}

USART Synchronous SPI Initialization example:

{
  #define GPIO_MOSI    PC0
  #define GPIO_MISO    PC1
  #define GPIO_SCLK    PC2

  sl_hal_usart_sync_init_t init = SL_HAL_USART_INIT_SYNC_DEFAULT;
  // Operate as SPI master.
  init.master = true;
  // Clock idle low, sample on falling edge.
  init.clock_mode = SL_HAL_USART_CLOCK_MODE_1;

  // Configure the clocks.
  sl_clock_manager_enable_bus_clock(SL_BUS_CLOCK_USART1);
  sl_clock_manager_enable_bus_clock(SL_BUS_CLOCK_GPIO);

  // Calculate and configure the usart's clock divider.
  uint32_t baudrate  = 1000000;  // Desired baudrate.
  sl_clock_branch_t clock_branch = sl_device_peripheral_get_clock_branch(SL_PERIPHERAL_USART1);
  uint32_t freq;
  sl_clock_manager_get_clock_branch_frequency(clock_branch, &freq);
  init.clock_div = sl_hal_usart_sync_calculate_clock_div(freq, baudrate);

  // Configure the SPI ports.
  sl_gpio_set_pin_mode(GPIO_MOSI, SL_GPIO_MODE_PUSH_PULL, 0);
  sl_gpio_set_pin_mode(GPIO_MISO, SL_GPIO_MODE_INPUT, 0);
  sl_gpio_set_pin_mode(GPIO_SCLK, SL_GPIO_MODE_PUSH_PULL, 0);

  // Connect USART to ports.
  GPIO->USARTROUTE[USART_NUM(USART1)].TXROUTE = (GPIO_MOSI->port << _GPIO_USART_TXROUTE_PORT_SHIFT)
                                              | (GPIO_MOSI->pin << _GPIO_USART_TXROUTE_PIN_SHIFT);
  GPIO->USARTROUTE[USART_NUM(USART1)].RXROUTE = (GPIO_MISO->port << _GPIO_USART_RXROUTE_PORT_SHIFT)
                                              | (GPIO_MISO->pin << _GPIO_USART_RXROUTE_PIN_SHIFT);
  GPIO->USARTROUTE[USART_NUM(USART1)].CLKROUTE = (GPIO_SCLK->port << _GPIO_USART_CLKROUTE_PORT_SHIFT)
                                               | (GPIO_SCLK->pin << _GPIO_USART_CLKROUTE_PIN_SHIFT);
  GPIO->USARTROUTE[USART_NUM(USART1)].ROUTEEN = GPIO_USART_ROUTEEN_TXPEN | GPIO_USART_ROUTEEN_RXPEN | GPIO_USART_ROUTEEN_CLKPEN;

  // Initialize the USART.
  sl_hal_usart_init_sync(USART1, &init);
  sl_hal_usart_enable(USART1);
  sl_hal_usart_enable_rx(USART1);
  sl_hal_usart_enable_tx(USART1);
}

Modules#

sl_hal_usart_async_init_t

sl_hal_usart_sync_init_t

sl_hal_usart_irda_init_t

sl_hal_usart_i2s_init_t

Enumerations#

enum
SL_HAL_USART_DATA_BITS_4 = _USART_FRAME_DATABITS_FOUR
SL_HAL_USART_DATA_BITS_5 = _USART_FRAME_DATABITS_FIVE
SL_HAL_USART_DATA_BITS_6 = _USART_FRAME_DATABITS_SIX
SL_HAL_USART_DATA_BITS_7 = _USART_FRAME_DATABITS_SEVEN
SL_HAL_USART_DATA_BITS_8 = _USART_FRAME_DATABITS_EIGHT
SL_HAL_USART_DATA_BITS_9 = _USART_FRAME_DATABITS_NINE
SL_HAL_USART_DATA_BITS_10 = _USART_FRAME_DATABITS_TEN
SL_HAL_USART_DATA_BITS_11 = _USART_FRAME_DATABITS_ELEVEN
SL_HAL_USART_DATA_BITS_12 = _USART_FRAME_DATABITS_TWELVE
SL_HAL_USART_DATA_BITS_13 = _USART_FRAME_DATABITS_THIRTEEN
SL_HAL_USART_DATA_BITS_14 = _USART_FRAME_DATABITS_FOURTEEN
SL_HAL_USART_DATA_BITS_15 = _USART_FRAME_DATABITS_FIFTEEN
SL_HAL_USART_DATA_BITS_16 = _USART_FRAME_DATABITS_SIXTEEN
}

Databit selection.

enum
SL_HAL_USART_OVS_16 = _USART_CTRL_OVS_X16
SL_HAL_USART_OVS_8 = _USART_CTRL_OVS_X8
SL_HAL_USART_OVS_6 = _USART_CTRL_OVS_X6
SL_HAL_USART_OVS_4 = _USART_CTRL_OVS_X4
}

Oversampling selection, used for asynchronous operation.

enum
SL_HAL_USART_NO_PARITY = _USART_FRAME_PARITY_NONE
SL_HAL_USART_EVEN_PARITY = _USART_FRAME_PARITY_EVEN
SL_HAL_USART_ODD_PARITY = _USART_FRAME_PARITY_ODD
}

Parity selection, mainly used for asynchronous operation.

enum
SL_HAL_USART_STOP_BITS_0P5 = _USART_FRAME_STOPBITS_HALF
SL_HAL_USART_STOP_BITS_1 = _USART_FRAME_STOPBITS_ONE
SL_HAL_USART_STOP_BITS_1P5 = _USART_FRAME_STOPBITS_ONEANDAHALF
SL_HAL_USART_STOP_BITS_2 = _USART_FRAME_STOPBITS_TWO
}

Stop bits selection, used for asynchronous operation.

enum
SL_HAL_USART_MAJORITY_VOTE_ENABLE = 0
SL_HAL_USART_MAJORITY_VOTE_DISABLE = 1
}

Majority vote enable.

enum
SL_HAL_USART_HW_FLOW_CONTROL_NONE = 0
SL_HAL_USART_HW_FLOW_CONTROL_CTS
SL_HAL_USART_HW_FLOW_CONTROL_RTS
SL_HAL_USART_HW_FLOW_CONTROL_CTS_RTS
}

Hardware Flow Control Selection.

enum
SL_HAL_USART_CLOCK_MODE_0 = USART_CTRL_CLKPOL_IDLELOW | USART_CTRL_CLKPHA_SAMPLELEADING
SL_HAL_USART_CLOCK_MODE_1 = USART_CTRL_CLKPOL_IDLELOW | USART_CTRL_CLKPHA_SAMPLETRAILING
SL_HAL_USART_CLOCK_MODE_2 = USART_CTRL_CLKPOL_IDLEHIGH | USART_CTRL_CLKPHA_SAMPLELEADING
SL_HAL_USART_CLOCK_MODE_3 = USART_CTRL_CLKPOL_IDLEHIGH | USART_CTRL_CLKPHA_SAMPLETRAILING
}

Clock polarity/phase mode.

enum
SL_HAL_USART_PULSE_WIDTH_ONE = _USART_IRCTRL_IRPW_ONE
SL_HAL_USART_PULSE_WIDTH_TWO = _USART_IRCTRL_IRPW_TWO
SL_HAL_USART_PULSE_WIDTH_THREE = _USART_IRCTRL_IRPW_THREE
SL_HAL_USART_PULSE_WIDTH_FOUR = _USART_IRCTRL_IRPW_FOUR
}

Pulse width selection for IrDA mode.

enum
SL_HAL_USART_I2S_FORMAT_W32D32 = _USART_I2SCTRL_FORMAT_W32D32
SL_HAL_USART_I2S_FORMAT_W32D24M = _USART_I2SCTRL_FORMAT_W32D24M
SL_HAL_USART_I2S_FORMAT_W32D24 = _USART_I2SCTRL_FORMAT_W32D24
SL_HAL_USART_I2S_FORMAT_W32D16 = _USART_I2SCTRL_FORMAT_W32D16
SL_HAL_USART_I2S_FORMAT_W32D8 = _USART_I2SCTRL_FORMAT_W32D8
SL_HAL_USART_I2S_FORMAT_W16D16 = _USART_I2SCTRL_FORMAT_W16D16
SL_HAL_USART_I2S_FORMAT_W16D8 = _USART_I2SCTRL_FORMAT_W16D8
SL_HAL_USART_I2S_FORMAT_W8D8 = _USART_I2SCTRL_FORMAT_W8D8
}

I2S format selection.

enum
SL_HAL_USART_JUSTIFY_LEFT = _USART_I2SCTRL_JUSTIFY_LEFT
SL_HAL_USART_JUSTIFY_RIGHT = _USART_I2SCTRL_JUSTIFY_RIGHT
}

I2S frame data justify.

Functions#

void
sl_hal_usart_init_async(USART_TypeDef *usart, const sl_hal_usart_async_init_t *init)

Initialize USART/UART for normal asynchronous mode.

uint32_t
sl_hal_usart_async_calculate_baudrate(uint32_t ref_freq, uint32_t clk_div, sl_hal_usart_ovs_t ovs)

Calculate baudrate for USART/UART given reference frequency, clock division, and oversampling rate.

uint32_t
sl_hal_usart_async_calculate_clock_div(uint32_t ref_freq, uint32_t baudrate, sl_hal_usart_ovs_t ovs)

Calculate UART clock divider using given reference frequency, oversampling and baudrate.

void
sl_hal_usart_init_sync(USART_TypeDef *usart, const sl_hal_usart_sync_init_t *init)

Initialize USART for synchronous mode.

uint32_t
sl_hal_usart_sync_calculate_baudrate(uint32_t ref_freq, uint32_t clk_div)

Calculate baudrate for USART/UART (sync mode) given reference frequency, clock division.

uint32_t
sl_hal_usart_sync_calculate_clock_div(uint32_t ref_freq, uint32_t baudrate)

Calculate UART (sync mode) clock divider using given reference frequency and baudrate.

void
sl_hal_usart_init_irda(USART_TypeDef *usart, const sl_hal_usart_irda_init_t *init)

Initialize USART for asynchronous IrDA mode.

void
sl_hal_usart_init_i2s(USART_TypeDef *usart, const sl_hal_usart_i2s_init_t *init)

Initialize USART for I2S mode.

void
sl_hal_usart_reset(USART_TypeDef *usart)

Reset USART/UART to the same state that it was in after a hardware reset.

void
sl_hal_usart_tx(USART_TypeDef *usart, uint8_t data)

Transmit one 4-9 bit frame.

uint8_t
sl_hal_usart_spi_transfer(USART_TypeDef *usart, uint8_t data)

Perform one 8 bit frame SPI transfer.

void
sl_hal_usart_tx_double(USART_TypeDef *usart, uint16_t data)

Transmit two 4-9 bit frames or one 10-16 bit frame.

void
sl_hal_usart_tx_double_ext(USART_TypeDef *usart, uint32_t data)

Transmit two 4-9 bit frames or one 10-16 bit frame with extended control.

void
sl_hal_usart_tx_ext(USART_TypeDef *usart, uint16_t data)

Transmit one 4-9 bit frame with extended control.

void
sl_hal_usart_enable_rx_prs_trigger(USART_TypeDef *usart, uint8_t channel)

Enable automatic enabling of reception using the PRS as a trigger.

void

Disable automatic enabling of reception using the PRS as a trigger.

void
sl_hal_usart_enable_tx_prs_trigger(USART_TypeDef *usart, uint8_t channel)

Enable automatic enabling of transmission using the PRS as a trigger.

void

Disable automatic enabling of transmission using the PRS as a trigger.

void
sl_hal_usart_async_set_clock_div(USART_TypeDef *usart, uint32_t clock_div)

Set UART mode clock divider.

uint32_t
sl_hal_usart_async_get_clock_div(USART_TypeDef *usart)

Get UART mode clock divider.

void
sl_hal_usart_enable(USART_TypeDef *usart)

Enable USART.

void
sl_hal_usart_disable(USART_TypeDef *usart)

Disable USART.

void
sl_hal_usart_enable_rx(USART_TypeDef *usart)

Enable USART receiver.

void
sl_hal_usart_disable_rx(USART_TypeDef *usart)

Disable USART receiver.

void
sl_hal_usart_enable_tx(USART_TypeDef *usart)

Enable USART transmitter.

void
sl_hal_usart_disable_tx(USART_TypeDef *usart)

Disable USART transmitter.

void
sl_hal_usart_clear_rx(USART_TypeDef *usart)

Clear RX FIFO.

void
sl_hal_usart_clear_tx(USART_TypeDef *usart)

Clear TX FIFO.

uint8_t
sl_hal_usart_rx(USART_TypeDef *usart)

Receive one 4-8 bit frame, (or part of 10-16 bit frame).

uint16_t
sl_hal_usart_rx_double(USART_TypeDef *usart)

Receive two 4-8 bit frames or one 10-16 bit frame.

uint32_t
sl_hal_usart_rx_double_ext(USART_TypeDef *usart)

Receive two 4-9 bit frames, or one 10-16 bit frame with extended information.

uint16_t
sl_hal_usart_rx_ext(USART_TypeDef *usart)

Receive one 4-9 bit frame (or part of 10-16 bit frame) with extended information.

uint8_t
sl_hal_usart_rx_data_get(USART_TypeDef *usart)

Receive one 4-8 bit frame, (or part of 10-16 bit frame).

uint16_t
sl_hal_usart_rx_double_get(USART_TypeDef *usart)

Receive two 4-8 bit frames, or one 10-16 bit frame.

uint32_t
sl_hal_usart_rx_double_x_get(USART_TypeDef *usart)

Receive two 4-9 bit frames, or one 10-16 bit frame with extended information.

uint16_t
sl_hal_usart_rx_data_x_get(USART_TypeDef *usart)

Receive one 4-9 bit frame, (or part of 10-16 bit frame) with extended information.

void
sl_hal_usart_clear_interrupts(USART_TypeDef *usart, uint32_t flags)

Clear one or more pending USART interrupts.

void
sl_hal_usart_disable_interrupts(USART_TypeDef *usart, uint32_t flags)

Disable one or more USART interrupts.

void
sl_hal_usart_enable_interrupts(USART_TypeDef *usart, uint32_t flags)

Enable one or more USART interrupts.

uint32_t

Get pending USART interrupt flags.

uint32_t

Get enabled and pending USART interrupt flags.

void
sl_hal_usart_set_interrupts(USART_TypeDef *usart, uint32_t flags)

Set one or more pending USART interrupts from SW.

uint32_t
sl_hal_usart_get_status(USART_TypeDef *usart)

Get USART STATUS register.

Macros#

#define
SL_HAL_USART_REF_VALID (ref)

Validation of USART register block pointer reference for assert statements. */.

#define
SL_HAL_USART_INIT_ASYNC_DEFAULT undefined

Default configuration for USART asynchronous initialization structure.

#define
SL_HAL_USART_INIT_SYNC_DEFAULT undefined

Default configuration for USART sync initialization structure.

#define
SL_HAL_USART_INIT_IRDA_DEFAULT undefined

Default configuration for USART IRDA initialization structure.

#define
SL_HAL_USART_INIT_I2S_DEFAULT undefined

Default USART Sync configuration for series 2 devices.

Enumeration Documentation#

sl_hal_usart_data_bits_t#

sl_hal_usart_data_bits_t

Databit selection.

Enumerator
SL_HAL_USART_DATA_BITS_4

4 data bits (not available for UART).

SL_HAL_USART_DATA_BITS_5

5 data bits (not available for UART).

SL_HAL_USART_DATA_BITS_6

6 data bits (not available for UART).

SL_HAL_USART_DATA_BITS_7

7 data bits (not available for UART).

SL_HAL_USART_DATA_BITS_8

8 data bits.

SL_HAL_USART_DATA_BITS_9

9 data bits.

SL_HAL_USART_DATA_BITS_10

10 data bits (not available for UART).

SL_HAL_USART_DATA_BITS_11

11 data bits (not available for UART).

SL_HAL_USART_DATA_BITS_12

12 data bits (not available for UART).

SL_HAL_USART_DATA_BITS_13

13 data bits (not available for UART).

SL_HAL_USART_DATA_BITS_14

14 data bits (not available for UART).

SL_HAL_USART_DATA_BITS_15

15 data bits (not available for UART).

SL_HAL_USART_DATA_BITS_16

16 data bits (not available for UART).


sl_hal_usart_ovs_t#

sl_hal_usart_ovs_t

Oversampling selection, used for asynchronous operation.

Enumerator
SL_HAL_USART_OVS_16

16x oversampling (normal).

SL_HAL_USART_OVS_8

8x oversampling.

SL_HAL_USART_OVS_6

6x oversampling.

SL_HAL_USART_OVS_4

4x oversampling.


sl_hal_usart_parity_t#

sl_hal_usart_parity_t

Parity selection, mainly used for asynchronous operation.

Enumerator
SL_HAL_USART_NO_PARITY

No parity.

SL_HAL_USART_EVEN_PARITY

Even parity.

SL_HAL_USART_ODD_PARITY

Odd parity.


sl_hal_usart_stop_bits_t#

sl_hal_usart_stop_bits_t

Stop bits selection, used for asynchronous operation.

Enumerator
SL_HAL_USART_STOP_BITS_0P5

0.5 stop bits.

SL_HAL_USART_STOP_BITS_1

1 stop bits.

SL_HAL_USART_STOP_BITS_1P5

1.5 stop bits.

SL_HAL_USART_STOP_BITS_2

2 stop bits.


sl_hal_usart_majority_vote_t#

sl_hal_usart_majority_vote_t

Majority vote enable.

Enumerator
SL_HAL_USART_MAJORITY_VOTE_ENABLE

Enable majority vote for 16x, 8x and 6x oversampling modes.

SL_HAL_USART_MAJORITY_VOTE_DISABLE

Disable majority vote for 16x, 8x and 6x oversampling modes.


sl_hal_usart_hw_flow_control_t#

sl_hal_usart_hw_flow_control_t

Hardware Flow Control Selection.

Enumerator
SL_HAL_USART_HW_FLOW_CONTROL_NONE

No hardware flow control.

SL_HAL_USART_HW_FLOW_CONTROL_CTS

CTS signal is enabled for TX flow control.

SL_HAL_USART_HW_FLOW_CONTROL_RTS

RTS signal is enabled for RX flow control.

SL_HAL_USART_HW_FLOW_CONTROL_CTS_RTS

CTS and RTS signals are enabled for TX and RX flow control.


sl_hal_usart_clock_mode_t#

sl_hal_usart_clock_mode_t

Clock polarity/phase mode.

Enumerator
SL_HAL_USART_CLOCK_MODE_0

Clock idle low, sample on rising edge.

SL_HAL_USART_CLOCK_MODE_1

Clock idle low, sample on falling edge.

SL_HAL_USART_CLOCK_MODE_2

Clock idle high, sample on falling edge.

SL_HAL_USART_CLOCK_MODE_3

Clock idle high, sample on rising edge.


sl_hal_usart_irda_pulse_width_t#

sl_hal_usart_irda_pulse_width_t

Pulse width selection for IrDA mode.

Enumerator
SL_HAL_USART_PULSE_WIDTH_ONE

IrDA pulse width is 1/16 for OVS=0 and 1/8 for OVS=1.

SL_HAL_USART_PULSE_WIDTH_TWO

IrDA pulse width is 2/16 for OVS=0 and 2/8 for OVS=1.

SL_HAL_USART_PULSE_WIDTH_THREE

IrDA pulse width is 3/16 for OVS=0 and 3/8 for OVS=1.

SL_HAL_USART_PULSE_WIDTH_FOUR

IrDA pulse width is 4/16 for OVS=0 and 4/8 for OVS=1.


sl_hal_usart_i2s_format_t#

sl_hal_usart_i2s_format_t

I2S format selection.

Enumerator
SL_HAL_USART_I2S_FORMAT_W32D32

32-bit word, 32-bit data.

SL_HAL_USART_I2S_FORMAT_W32D24M

32-bit word, 32-bit data with 8 lsb masked.

SL_HAL_USART_I2S_FORMAT_W32D24

32-bit word, 24-bit data.

SL_HAL_USART_I2S_FORMAT_W32D16

32-bit word, 16-bit data.

SL_HAL_USART_I2S_FORMAT_W32D8

32-bit word, 8-bit data.

SL_HAL_USART_I2S_FORMAT_W16D16

16-bit word, 16-bit data.

SL_HAL_USART_I2S_FORMAT_W16D8

16-bit word, 8-bit data.

SL_HAL_USART_I2S_FORMAT_W8D8

8-bit word, 8-bit data.


sl_hal_usart_i2s_justify_t#

sl_hal_usart_i2s_justify_t

I2S frame data justify.

Enumerator
SL_HAL_USART_JUSTIFY_LEFT

Data is left-justified within the frame.

SL_HAL_USART_JUSTIFY_RIGHT

Data is right-justified within the frame.


Function Documentation#

sl_hal_usart_init_async#

void sl_hal_usart_init_async (USART_TypeDef * usart, const sl_hal_usart_async_init_t * init)

Initialize USART/UART for normal asynchronous mode.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

A pointer to the USART/UART peripheral register block.

const sl_hal_usart_async_init_t *[in]init

A pointer to the initialization structure used to configure the basic async setup.

This function will configure basic settings to operate in normal asynchronous mode.

A special control setup not covered by this function must be done after using this function by direct modification of the CTRL register.

Notice that pins used by the USART/UART module must be properly configured by the user explicitly for the USART/UART to work as intended. (When configuring pins, remember to consider the sequence of configuration to avoid unintended pulses/glitches on output pins.)


sl_hal_usart_async_calculate_baudrate#

uint32_t sl_hal_usart_async_calculate_baudrate (uint32_t ref_freq, uint32_t clk_div, sl_hal_usart_ovs_t ovs)

Calculate baudrate for USART/UART given reference frequency, clock division, and oversampling rate.

Parameters
TypeDirectionArgument NameDescription
uint32_t[in]ref_freq

USART/UART HF peripheral frequency used.

uint32_t[in]clk_div

A clock division factor to be used.

sl_hal_usart_ovs_t[in]ovs

Oversample value of the USART.

This function returns the baudrate that a USART/UART module will use if configured with the given frequency, clock divider, and mode. Notice that this function will not use the hardware configuration. It can be used to determine if a given configuration is sufficiently accurate for the application.

Returns

  • Baudrate with given settings.


sl_hal_usart_async_calculate_clock_div#

uint32_t sl_hal_usart_async_calculate_clock_div (uint32_t ref_freq, uint32_t baudrate, sl_hal_usart_ovs_t ovs)

Calculate UART clock divider using given reference frequency, oversampling and baudrate.

Parameters
TypeDirectionArgument NameDescription
uint32_t[in]ref_freq

Peripheral clock frequency.

uint32_t[in]baudrate

Transmission speed of the UART interface.

sl_hal_usart_ovs_t[in]ovs

Oversampling rate.

Returns

  • USART clock divider for desired baud rate.


sl_hal_usart_init_sync#

void sl_hal_usart_init_sync (USART_TypeDef * usart, const sl_hal_usart_sync_init_t * init)

Initialize USART for synchronous mode.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

A pointer to the USART peripheral register block. (UART does not support this mode.)

const sl_hal_usart_sync_init_t *[in]init

A pointer to the initialization structure used to configure basic async setup.

This function will configure basic settings to operate in synchronous mode.

A special control setup not covered by this function must be done after using this function by direct modification of the CTRL register.

Notice that pins used by the USART module must be properly configured by the user explicitly for the USART to work as intended. (When configuring pins remember to consider the sequence of configuration to avoid unintended pulses/glitches on output pins.)


sl_hal_usart_sync_calculate_baudrate#

uint32_t sl_hal_usart_sync_calculate_baudrate (uint32_t ref_freq, uint32_t clk_div)

Calculate baudrate for USART/UART (sync mode) given reference frequency, clock division.

Parameters
TypeDirectionArgument NameDescription
uint32_t[in]ref_freq

USART/UART HF peripheral frequency used.

uint32_t[in]clk_div

A clock division factor to be used.

This function returns the baudrate that a USART/UART module will use if configured with the given frequency, clock divider, and mode. Notice that this function will not use the hardware configuration. It can be used to determine if a given configuration is sufficiently accurate for the application.

Returns

  • Baudrate with given settings.


sl_hal_usart_sync_calculate_clock_div#

uint32_t sl_hal_usart_sync_calculate_clock_div (uint32_t ref_freq, uint32_t baudrate)

Calculate UART (sync mode) clock divider using given reference frequency and baudrate.

Parameters
TypeDirectionArgument NameDescription
uint32_t[in]ref_freq

Peripheral clock frequency.

uint32_t[in]baudrate

Transmission speed of the USART/UART interface.

Returns

  • USART clock divider for desired baud rate.


sl_hal_usart_init_irda#

void sl_hal_usart_init_irda (USART_TypeDef * usart, const sl_hal_usart_irda_init_t * init)

Initialize USART for asynchronous IrDA mode.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

A pointer to the USART peripheral register block.

const sl_hal_usart_irda_init_t *[in]init

A pointer to the initialization structure used to configure async IrDA setup.

This function will configure basic settings to operate in asynchronous IrDA mode.

A special control setup not covered by this function must be done after using this function by direct modification of the CTRL and IRCTRL registers.

Notice that pins used by the USART/UART module must be properly configured by the user explicitly for the USART/UART to work as intended. (When configuring pins, remember to consider the sequence of configuration to avoid unintended pulses/glitches on output pins.)

Note

  • Not all USART instances support IrDA.


sl_hal_usart_init_i2s#

void sl_hal_usart_init_i2s (USART_TypeDef * usart, const sl_hal_usart_i2s_init_t * init)

Initialize USART for I2S mode.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

A pointer to the USART peripheral register block. (UART does not support this mode.)

const sl_hal_usart_i2s_init_t *[in]init

A pointer to the initialization structure used to configure the basic I2S setup.

This function will configure basic settings to operate in I2S mode.

A special control setup not covered by this function must be done after using this function by direct modification of the CTRL and I2SCTRL registers.

Notice that pins used by the USART module must be properly configured by the user explicitly for the USART to work as intended. (When configuring pins, remember to consider the sequence of configuration to avoid unintended pulses/glitches on output pins.)

Note

  • This function does not apply to all USART's.


sl_hal_usart_reset#

void sl_hal_usart_reset (USART_TypeDef * usart)

Reset USART/UART to the same state that it was in after a hardware reset.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

A pointer to USART/UART peripheral register block.


sl_hal_usart_tx#

void sl_hal_usart_tx (USART_TypeDef * usart, uint8_t data)

Transmit one 4-9 bit frame.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

A pointer to the USART/UART peripheral register block.

uint8_t[in]data

Data to transmit. See details above for more information.

Depending on the frame length configuration, 4-8 (least significant) bits from data are transmitted. If the frame length is 9, 8 bits are transmitted from data and one bit as specified by CTRL register, BIT8DV field. See sl_hal_usart_tx_ext() for transmitting 9 bit frame with full control of all 9 bits.

Notice that possible parity/stop bits in asynchronous mode are not considered part of a specified frame bit length.

Note

  • This function will stall if the buffer is full until the buffer becomes available.


sl_hal_usart_spi_transfer#

uint8_t sl_hal_usart_spi_transfer (USART_TypeDef * usart, uint8_t data)

Perform one 8 bit frame SPI transfer.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

A pointer to the USART peripheral register block.

uint8_t[in]data

Data to transmit.

Note

  • This function will stall if the transmit buffer is full. When a transmit buffer becomes available, data is written and the function will wait until data is fully transmitted. The SPI return value is then read out and returned.

Returns

  • Data received.


sl_hal_usart_tx_double#

void sl_hal_usart_tx_double (USART_TypeDef * usart, uint16_t data)

Transmit two 4-9 bit frames or one 10-16 bit frame.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

A pointer to the USART/UART peripheral register block.

uint16_t[in]data

Data to transmit, the least significant byte holds the frame transmitted first. See details above for more info.

Depending on the frame length configuration, 4-8 (least significant) bits from each byte in data are transmitted. If frame length is 9, 8 bits are transmitted from each byte in data adding one bit as specified by the CTRL register, BIT8DV field, to each byte. See sl_hal_usart_tx_double_ext() for transmitting two 9 bit frames with full control of all 9 bits.

If the frame length is 10-16, 10-16 (least significant) bits from data are transmitted.

Notice that possible parity/stop bits in asynchronous mode are not considered part of a specified frame bit length.

Note

  • This function will stall if the buffer is full until the buffer becomes available.


sl_hal_usart_tx_double_ext#

void sl_hal_usart_tx_double_ext (USART_TypeDef * usart, uint32_t data)

Transmit two 4-9 bit frames or one 10-16 bit frame with extended control.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

A pointer to the USART/UART peripheral register block.

uint32_t[in]data

Data to transmit with extended control. Contains two 16 bit words concatenated. Least significant word holds the frame transmitted first. If the frame length is 4-9, two frames with 4-9 least significant bits from each 16 bit word are transmitted.

Notice that possible parity/stop bits in asynchronous mode are not considered part of a specified frame bit length.

Note

  • This function will stall if the buffer is full until the buffer becomes available.

  • If the frame length is 10-16 bits, 8 data bits are taken from the least significant 16 bit word and the remaining bits from the other 16 bit word.

  • Additional control bits are available as documented in the reference manual (set to 0 if not used). For 10-16 bit frame length, these control bits are taken from the most significant 16 bit word.


sl_hal_usart_tx_ext#

void sl_hal_usart_tx_ext (USART_TypeDef * usart, uint16_t data)

Transmit one 4-9 bit frame with extended control.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

A pointer to the USART/UART peripheral register block.

uint16_t[in]data

Data to transmit with extended control. Least significant bit contains frame bits. Additional control bits are available as documented in the reference manual (set to 0 if not used).

Notice that possible parity/stop bits in asynchronous mode are not considered part of a specified frame bit length.

Note

  • This function will stall if the buffer is full until the buffer becomes available.


sl_hal_usart_enable_rx_prs_trigger#

void sl_hal_usart_enable_rx_prs_trigger (USART_TypeDef * usart, uint8_t channel)

Enable automatic enabling of reception using the PRS as a trigger.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

Pointer to the USART peripheral register block.

uint8_t[in]channel

PRS channel to use.


sl_hal_usart_disable_rx_prs_trigger#

void sl_hal_usart_disable_rx_prs_trigger (USART_TypeDef * usart)

Disable automatic enabling of reception using the PRS as a trigger.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

Pointer to the USART peripheral register block.


sl_hal_usart_enable_tx_prs_trigger#

void sl_hal_usart_enable_tx_prs_trigger (USART_TypeDef * usart, uint8_t channel)

Enable automatic enabling of transmission using the PRS as a trigger.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

Pointer to the USART peripheral register block.

uint8_t[in]channel

PRS channel number.


sl_hal_usart_disable_tx_prs_trigger#

void sl_hal_usart_disable_tx_prs_trigger (USART_TypeDef * usart)

Disable automatic enabling of transmission using the PRS as a trigger.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

Pointer to the USART peripheral register block.


sl_hal_usart_async_set_clock_div#

void sl_hal_usart_async_set_clock_div (USART_TypeDef * usart, uint32_t clock_div)

Set UART mode clock divider.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

Pointer to the USART peripheral register block.

uint32_t[in]clock_div

Clock divider value.


sl_hal_usart_async_get_clock_div#

uint32_t sl_hal_usart_async_get_clock_div (USART_TypeDef * usart)

Get UART mode clock divider.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

Pointer to the USART peripheral register block.

Returns

  • Clock divider value.


sl_hal_usart_enable#

void sl_hal_usart_enable (USART_TypeDef * usart)

Enable USART.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

A pointer to the USART peripheral register block.


sl_hal_usart_disable#

void sl_hal_usart_disable (USART_TypeDef * usart)

Disable USART.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

A pointer to the USART peripheral register block.


sl_hal_usart_enable_rx#

void sl_hal_usart_enable_rx (USART_TypeDef * usart)

Enable USART receiver.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

A pointer to the USART peripheral register block.


sl_hal_usart_disable_rx#

void sl_hal_usart_disable_rx (USART_TypeDef * usart)

Disable USART receiver.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

A pointer to the USART peripheral register block.


sl_hal_usart_enable_tx#

void sl_hal_usart_enable_tx (USART_TypeDef * usart)

Enable USART transmitter.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

A pointer to the USART peripheral register block.


sl_hal_usart_disable_tx#

void sl_hal_usart_disable_tx (USART_TypeDef * usart)

Disable USART transmitter.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

A pointer to the USART peripheral register block.


sl_hal_usart_clear_rx#

void sl_hal_usart_clear_rx (USART_TypeDef * usart)

Clear RX FIFO.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

Pointer to the USART peripheral register block.


sl_hal_usart_clear_tx#

void sl_hal_usart_clear_tx (USART_TypeDef * usart)

Clear TX FIFO.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

Pointer to the USART peripheral register block.


sl_hal_usart_rx#

uint8_t sl_hal_usart_rx (USART_TypeDef * usart)

Receive one 4-8 bit frame, (or part of 10-16 bit frame).

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

A pointer to the USART/UART peripheral register block.

This function is normally used to receive one frame when operating with frame length 4-8 bits. See sl_hal_usart_rx_ext() for reception of 9 bit frames.

Notice that possible parity/stop bits in asynchronous mode are not considered part of a specified frame bit length.

Note

  • This function will stall if the buffer is empty until data is received. Alternatively, the user can explicitly check whether data is available. If data is available, call sl_hal_usart_rx_data_get() to read the RXDATA register directly.

Returns

  • Data received.


sl_hal_usart_rx_double#

uint16_t sl_hal_usart_rx_double (USART_TypeDef * usart)

Receive two 4-8 bit frames or one 10-16 bit frame.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

A pointer to the USART/UART peripheral register block.

This function is normally used to receive one frame when operating with frame length 10-16 bits. See sl_hal_usart_rx_double_ext() for reception of two 9 bit frames.

Notice that possible parity/stop bits in asynchronous mode are not considered part of a specified frame bit length.

Note

  • This function will stall if the buffer is empty until data is received. Alternatively, the user can explicitly check whether data is available. If data is available, call sl_hal_usart_rx_double_get() to read the RXDOUBLE register directly.

Returns

  • Data received.


sl_hal_usart_rx_double_ext#

uint32_t sl_hal_usart_rx_double_ext (USART_TypeDef * usart)

Receive two 4-9 bit frames, or one 10-16 bit frame with extended information.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

A pointer to the USART/UART peripheral register block.

This function is normally used to receive one frame when operating with frame length 10-16 bits and additional RX status information is required.

Notice that possible parity/stop bits in asynchronous mode are not considered part of a specified frame bit length.

Note

  • This function will stall if buffer is empty until data is received. Alternatively, the user can explicitly check whether data is available. If data is available, call sl_hal_usart_rx_double_x_get() to read the RXDOUBLEX register directly.

Returns

  • Data received.


sl_hal_usart_rx_ext#

uint16_t sl_hal_usart_rx_ext (USART_TypeDef * usart)

Receive one 4-9 bit frame (or part of 10-16 bit frame) with extended information.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

A pointer to the USART/UART peripheral register block.

This function is normally used to receive one frame when operating with frame length 4-9 bits and additional RX status information is required.

Notice that possible parity/stop bits in asynchronous mode are not considered part of a specified frame bit length.

Note

  • This function will stall if the buffer is empty until data is received. Alternatively, the user can explicitly check whether data is available. If data is available, call sl_hal_usart_rx_data_get() to read the RXDATAX register directly.

Returns

  • Data received.


sl_hal_usart_rx_data_get#

uint8_t sl_hal_usart_rx_data_get (USART_TypeDef * usart)

Receive one 4-8 bit frame, (or part of 10-16 bit frame).

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

Pointer to USART/UART peripheral register block.

This function is used to quickly receive one 4-8 bits frame by reading the RXDATA register directly, without checking the STATUS register for the RXDATAV flag. This can be useful from the RXDATAV interrupt handler, i.e., waiting is superfluous, in order to quickly read the received data. Please refer to sl_hal_usart_rx_ext() for reception of 9 bit frames.

Note

  • Because this function does not check whether the RXDATA register actually holds valid data, it should only be used in situations when it is certain that there is valid data, ensured by some external program routine, e.g., when handling an RXDATAV interrupt. The sl_hal_usart_rx() is normally a better choice if the validity of the RXDATA register is not certain.

  • Notice that possible parity/stop bits in asynchronous mode are not considered part of specified frame bit length.

Returns

  • Data received.


sl_hal_usart_rx_double_get#

uint16_t sl_hal_usart_rx_double_get (USART_TypeDef * usart)

Receive two 4-8 bit frames, or one 10-16 bit frame.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

Pointer to USART/UART peripheral register block.

This function is used to quickly receive one 10-16 bits frame or two 4-8 bit frames by reading the RXDOUBLE register directly, without checking the STATUS register for the RXDATAV flag. This can be useful from the RXDATAV interrupt handler, i.e., waiting is superfluous, in order to quickly read the received data. This function is normally used to receive one frame when operating with frame length 10-16 bits. Please refer to sl_hal_usart_rx_double_x_get() for reception of two 9 bit frames.

Note

  • Because this function does not check whether the RXDOUBLE register actually holds valid data, it should only be used in situations when it is certain that there is valid data, ensured by some external program routine, e.g., when handling an RXDATAV interrupt. The sl_hal_usart_rx_double() is normally a better choice if the validity of the RXDOUBLE register is not certain.

  • Notice that possible parity/stop bits in asynchronous mode are not considered part of specified frame bit length.

Returns

  • Data received.


sl_hal_usart_rx_double_x_get#

uint32_t sl_hal_usart_rx_double_x_get (USART_TypeDef * usart)

Receive two 4-9 bit frames, or one 10-16 bit frame with extended information.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

Pointer to USART/UART peripheral register block.

This function is used to quickly receive one 10-16 bits frame or two 4-9 bit frames by reading the RXDOUBLEX register directly, without checking the STATUS register for the RXDATAV flag. This can be useful from the RXDATAV interrupt handler, i.e., waiting is superfluous, in order to quickly read the received data.

Note

  • Because this function does not check whether the RXDOUBLEX register actually holds valid data, it should only be used in situations when it is certain that there is valid data, ensured by some external program routine, e.g., when handling an RXDATAV interrupt. The sl_hal_usart_rx_double_ext() is normally a better choice if the validity of the RXDOUBLEX register is not certain.

  • Notice that possible parity/stop bits in asynchronous mode are not considered part of specified frame bit length.

Returns

  • Data received.


sl_hal_usart_rx_data_x_get#

uint16_t sl_hal_usart_rx_data_x_get (USART_TypeDef * usart)

Receive one 4-9 bit frame, (or part of 10-16 bit frame) with extended information.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

Pointer to USART/UART peripheral register block.

This function is used to quickly receive one 4-9 bit frame, (or part of 10-16 bit frame) with extended information by reading the RXDATAX register directly, without checking the STATUS register for the RXDATAV flag. This can be useful from the RXDATAV interrupt handler, i.e., waiting is superfluous, in order to quickly read the received data.

Note

  • Because this function does not check whether the RXDATAX register actually holds valid data, it should only be used in situations when it is certain that there is valid data, ensured by some external program routine, e.g., when handling an RXDATAV interrupt. The sl_hal_usart_rx_ext() is normally a better choice if the validity of the RXDATAX register is not certain.

  • Notice that possible parity/stop bits in asynchronous mode are not considered part of specified frame bit length.

Returns

  • Data received.


sl_hal_usart_clear_interrupts#

void sl_hal_usart_clear_interrupts (USART_TypeDef * usart, uint32_t flags)

Clear one or more pending USART interrupts.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

Pointer to the USART/UART peripheral register block.

uint32_t[in]flags

Pending USART/UART interrupt source(s) to clear. Use one or more valid interrupt flags for the USART module (USART_IF_nnn) OR'ed together.


sl_hal_usart_disable_interrupts#

void sl_hal_usart_disable_interrupts (USART_TypeDef * usart, uint32_t flags)

Disable one or more USART interrupts.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

Pointer to the USART/UART peripheral register block.

uint32_t[in]flags

USART/UART interrupt source(s) to disable. Use one or more valid interrupt flags for the USART module (USART_IF_nnn) OR'ed together.


sl_hal_usart_enable_interrupts#

void sl_hal_usart_enable_interrupts (USART_TypeDef * usart, uint32_t flags)

Enable one or more USART interrupts.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

Pointer to the USART/UART peripheral register block.

uint32_t[in]flags

USART/UART interrupt source(s) to enable. Use one or more valid interrupt flags for the USART module (USART_IF_nnn) OR'ed together.

Note

  • Depending on the use, a pending interrupt may already be set prior to enabling the interrupt. To ignore a pending interrupt, consider using USART_IntClear() prior to enabling the interrupt.


sl_hal_usart_get_pending_interrupts#

uint32_t sl_hal_usart_get_pending_interrupts (USART_TypeDef * usart)

Get pending USART interrupt flags.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

Pointer to the USART/UART peripheral register block.

Note

  • The event bits are not cleared by the use of this function.

Returns

  • USART/UART interrupt source(s) pending. Returns one or more valid interrupt flags for the USART module (USART_IF_nnn) OR'ed together.


sl_hal_usart_get_enabled_pending_interrupts#

uint32_t sl_hal_usart_get_enabled_pending_interrupts (USART_TypeDef * usart)

Get enabled and pending USART interrupt flags.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

Pointer to the USART/UART peripheral register block.

Useful for handling more interrupt sources in the same interrupt handler.

Note

  • Interrupt flags are not cleared by the use of this function.

Returns

  • Pending and enabled USART interrupt sources. The return value is the bitwise AND combination of

    • the OR combination of enabled interrupt sources in USARTx_IEN_nnn register (USARTx_IEN_nnn) and

    • the OR combination of valid interrupt flags of the USART module (USARTx_IF_nnn).


sl_hal_usart_set_interrupts#

void sl_hal_usart_set_interrupts (USART_TypeDef * usart, uint32_t flags)

Set one or more pending USART interrupts from SW.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

A pointer to the USART/UART peripheral register block.

uint32_t[in]flags

USART/UART interrupt source(s) to set to pending. Use one or more valid interrupt flags for the USART module(USART_IF_nnn) OR'ed together.


sl_hal_usart_get_status#

uint32_t sl_hal_usart_get_status (USART_TypeDef * usart)

Get USART STATUS register.

Parameters
TypeDirectionArgument NameDescription
USART_TypeDef *[in]usart

A pointer to the USART/UART peripheral register block.

Returns

  • STATUS register value.