USART - Synchronous/Asynchronous Serial

Description

Universal Synchronous/Asynchronous Receiver/Transmitter Peripheral API.

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:

Clock enable:

#if !defined(_SILICON_LABS_32B_SERIES_2)
/* USART is a HFPERCLK peripheral. Enable HFPERCLK domain and USART0.
* We also need to enable the clock for GPIO to configure pins. */
#endif

To initialize the USART for asynchronous operation (e.g., UART):

/* Initialize with default settings and then update fields according to application requirements. */
initAsync.baudrate = 38400;
USART_InitAsync(USART0, &initAsync);

To initialize the USART for synchronous operation (e.g., SPI):

/* Initialize with default settings and then update fields according to application requirements. */
/* Operate as SPI master */
initSync.master = true;
/* Clock idle low, sample on falling edge. */
USART_InitSync(USART0, &initSync);

After pins are assigned for the application/board, enable pins at the desired location. Available locations can be obtained from the Pin Definitions section in the data sheet.

/* Enable I/O and set location */
USART0->ROUTEPEN |= USART_ROUTEPEN_RXPEN | USART_ROUTEPEN_TXPEN;
USART0->ROUTELOC0 = (USART0->ROUTELOC0
& ~(_USART_ROUTELOC0_TXLOC_MASK
| _USART_ROUTELOC0_RXLOC_MASK))
| (USER_TX_LOCATION << _USART_ROUTELOC0_TXLOC_SHIFT)
| (USER_RX_LOCATION << _USART_ROUTELOC0_RXLOC_SHIFT);
/* Also enable CS and CLK pins if the USART is configured for synchronous mode.
* Set GPIO mode. */
if (USART0->CTRL & USART_CTRL_SYNC) {
USART0->ROUTEPEN |= USART_ROUTEPEN_CSPEN | USART_ROUTEPEN_CLKPEN;
USART0->ROUTELOC0 = (USART0->ROUTELOC0
& ~(_USART_ROUTELOC0_CSLOC_MASK
| _USART_ROUTELOC0_CLKLOC_MASK))
| (USER_CS_LOCATION << _USART_ROUTELOC0_TXLOC_SHIFT)
| (USER_CLK_LOCATION << _USART_ROUTELOC0_RXLOC_SHIFT);
GPIO_PinModeSet((GPIO_Port_TypeDef)AF_USART0_TX_PORT(USER_TX_LOCATION), AF_USART0_TX_PIN(USER_TX_LOCATION), gpioModePushPull, 0);
GPIO_PinModeSet((GPIO_Port_TypeDef)AF_USART0_RX_PORT(USER_RX_LOCATION), AF_USART0_RX_PIN(USER_RX_LOCATION), gpioModeInput, 0);
GPIO_PinModeSet((GPIO_Port_TypeDef)AF_USART0_CS_PORT(USER_CS_LOCATION), AF_USART0_CS_PIN(USER_CS_LOCATION), gpioModePushPull, 0);
GPIO_PinModeSet((GPIO_Port_TypeDef)AF_USART0_CLK_PORT(USER_CLK_LOCATION), AF_USART0_CLK_PIN(USER_CLK_LOCATION), gpioModePushPull, 0);
} else {
/* To avoid false start, configure TX pin as initial high */
GPIO_PinModeSet((GPIO_Port_TypeDef)AF_USART0_TX_PORT(USER_TX_LOCATION), AF_USART0_TX_PIN(USER_TX_LOCATION), gpioModePushPull, 1);
GPIO_PinModeSet((GPIO_Port_TypeDef)AF_USART0_RX_PORT(USER_RX_LOCATION), AF_USART0_RX_PIN(USER_RX_LOCATION), gpioModeInput, 0);
/* Don't enable CTS/RTS hardware flow control pins in this example. Automatic hardware flow control is available and can be
* enabled by setting USART_ROUTEPEN_RTSPEN and USART_ROUTEPEN_CTSPEN, and enable GPIO for the RTS/CTS pins. */
}
Note
UARTDRV supports all types of UART flow control. Software assisted hardware flow control is available for parts without true UART hardware flow control.

Data Structures

struct  USART_InitAsync_TypeDef
 Asynchronous mode initialization structure.
 
struct  USART_PrsTriggerInit_TypeDef
 USART PRS trigger enable.
 
struct  USART_InitSync_TypeDef
 Synchronous mode initialization structure.
 
struct  USART_InitIrDA_TypeDef
 IrDA mode initialization structure.
 
struct  USART_InitI2s_TypeDef
 I2S mode initialization structure.
 

Functions

void USART_BaudrateAsyncSet (USART_TypeDef *usart, uint32_t refFreq, uint32_t baudrate, USART_OVS_TypeDef ovs)
 Configure USART/UART operating in asynchronous mode to use a given baudrate (or as close as possible to a specified baudrate).
 
uint32_t USART_BaudrateCalc (uint32_t refFreq, uint32_t clkdiv, bool syncmode, USART_OVS_TypeDef ovs)
 Calculate baudrate for USART/UART given reference frequency, clock division, and oversampling rate (if async mode).
 
uint32_t USART_BaudrateGet (USART_TypeDef *usart)
 Get the current baudrate for USART/UART.
 
void USART_BaudrateSyncSet (USART_TypeDef *usart, uint32_t refFreq, uint32_t baudrate)
 Configure the USART operating in synchronous mode to use a given baudrate (or as close as possible to a specified baudrate).
 
void USART_Enable (USART_TypeDef *usart, USART_Enable_TypeDef enable)
 Enable/disable USART/UART receiver and/or transmitter.
 
void USART_InitAsync (USART_TypeDef *usart, const USART_InitAsync_TypeDef *init)
 Initialize USART/UART for normal asynchronous mode.
 
void USART_InitSync (USART_TypeDef *usart, const USART_InitSync_TypeDef *init)
 Initialize USART for synchronous mode.
 
void USARTn_InitIrDA (USART_TypeDef *usart, const USART_InitIrDA_TypeDef *init)
 Initialize USART for asynchronous IrDA mode.
 
void USART_InitI2s (USART_TypeDef *usart, USART_InitI2s_TypeDef *init)
 Initialize USART for I2S mode.
 
void USART_InitPrsTrigger (USART_TypeDef *usart, const USART_PrsTriggerInit_TypeDef *init)
 Initialize the automatic transmissions using PRS channel as a trigger.
 
void USART_Reset (USART_TypeDef *usart)
 Reset USART/UART to the same state that it was in after a hardware reset.
 
uint8_t USART_Rx (USART_TypeDef *usart)
 Receive one 4-8 bit frame, (or part of 10-16 bit frame).
 
uint16_t USART_RxDouble (USART_TypeDef *usart)
 Receive two 4-8 bit frames or one 10-16 bit frame.
 
uint32_t USART_RxDoubleExt (USART_TypeDef *usart)
 Receive two 4-9 bit frames, or one 10-16 bit frame with extended information.
 
uint16_t USART_RxExt (USART_TypeDef *usart)
 Receive one 4-9 bit frame (or part of 10-16 bit frame) with extended information.
 
uint8_t USART_SpiTransfer (USART_TypeDef *usart, uint8_t data)
 Perform one 8 bit frame SPI transfer.
 
void USART_Tx (USART_TypeDef *usart, uint8_t data)
 Transmit one 4-9 bit frame.
 
void USART_TxDouble (USART_TypeDef *usart, uint16_t data)
 Transmit two 4-9 bit frames or one 10-16 bit frame.
 
void USART_TxDoubleExt (USART_TypeDef *usart, uint32_t data)
 Transmit two 4-9 bit frames or one 10-16 bit frame with extended control.
 
void USART_TxExt (USART_TypeDef *usart, uint16_t data)
 Transmit one 4-9 bit frame with extended control.
 
void USART_IntClear (USART_TypeDef *usart, uint32_t flags)
 Clear one or more pending USART interrupts.
 
void USART_IntDisable (USART_TypeDef *usart, uint32_t flags)
 Disable one or more USART interrupts.
 
void USART_IntEnable (USART_TypeDef *usart, uint32_t flags)
 Enable one or more USART interrupts.
 
uint32_t USART_IntGet (USART_TypeDef *usart)
 Get pending USART interrupt flags.
 
uint32_t USART_IntGetEnabled (USART_TypeDef *usart)
 Get enabled and pending USART interrupt flags.
 
void USART_IntSet (USART_TypeDef *usart, uint32_t flags)
 Set one or more pending USART interrupts from SW.
 
uint32_t USART_StatusGet (USART_TypeDef *usart)
 Get USART STATUS register.
 
uint8_t USART_RxDataGet (USART_TypeDef *usart)
 Receive one 4-8 bit frame, (or part of 10-16 bit frame).
 
uint16_t USART_RxDoubleGet (USART_TypeDef *usart)
 Receive two 4-8 bit frames, or one 10-16 bit frame.
 
uint32_t USART_RxDoubleXGet (USART_TypeDef *usart)
 Receive two 4-9 bit frames, or one 10-16 bit frame with extended information.
 
uint16_t USART_RxDataXGet (USART_TypeDef *usart)
 Receive one 4-9 bit frame, (or part of 10-16 bit frame) with extended information.
 

Macros

#define USART_INITASYNC_DEFAULT
 Default configuration for USART asynchronous initialization structure.
 
#define USART_INITPRSTRIGGER_DEFAULT
 Default configuration for USART PRS triggering structure.
 
#define USART_INITSYNC_DEFAULT
 Default configuration for USART sync initialization structure.
 
#define USART_INITIRDA_DEFAULT
 Default configuration for IrDA mode initialization structure.
 
#define USART_INITI2S_DEFAULT
 Default configuration for I2S mode initialization structure.
 

Typedefs

typedef uint8_t USART_PRS_Channel_t
 PRS Channel type.
 

Enumerations

enum  USART_Databits_TypeDef {
  usartDatabits4 = USART_FRAME_DATABITS_FOUR,
  usartDatabits5 = USART_FRAME_DATABITS_FIVE,
  usartDatabits6 = USART_FRAME_DATABITS_SIX,
  usartDatabits7 = USART_FRAME_DATABITS_SEVEN,
  usartDatabits8 = USART_FRAME_DATABITS_EIGHT,
  usartDatabits9 = USART_FRAME_DATABITS_NINE,
  usartDatabits10 = USART_FRAME_DATABITS_TEN,
  usartDatabits11 = USART_FRAME_DATABITS_ELEVEN,
  usartDatabits12 = USART_FRAME_DATABITS_TWELVE,
  usartDatabits13 = USART_FRAME_DATABITS_THIRTEEN,
  usartDatabits14 = USART_FRAME_DATABITS_FOURTEEN,
  usartDatabits15 = USART_FRAME_DATABITS_FIFTEEN,
  usartDatabits16 = USART_FRAME_DATABITS_SIXTEEN
}
 Databit selection.
 
enum  USART_Enable_TypeDef {
  usartDisable = 0x0,
  usartEnableRx = USART_CMD_RXEN,
  usartEnableTx = USART_CMD_TXEN,
  usartEnable = (USART_CMD_RXEN | USART_CMD_TXEN)
}
 Enable selection.
 
enum  USART_OVS_TypeDef {
  usartOVS16 = USART_CTRL_OVS_X16,
  usartOVS8 = USART_CTRL_OVS_X8,
  usartOVS6 = USART_CTRL_OVS_X6,
  usartOVS4 = USART_CTRL_OVS_X4
}
 Oversampling selection, used for asynchronous operation.
 
enum  USART_Parity_TypeDef {
  usartNoParity = USART_FRAME_PARITY_NONE,
  usartEvenParity = USART_FRAME_PARITY_EVEN,
  usartOddParity = USART_FRAME_PARITY_ODD
}
 Parity selection, mainly used for asynchronous operation.
 
enum  USART_Stopbits_TypeDef {
  usartStopbits0p5 = USART_FRAME_STOPBITS_HALF,
  usartStopbits1 = USART_FRAME_STOPBITS_ONE,
  usartStopbits1p5 = USART_FRAME_STOPBITS_ONEANDAHALF,
  usartStopbits2 = USART_FRAME_STOPBITS_TWO
}
 Stop bits selection, used for asynchronous operation.
 
enum  USART_HwFlowControl_TypeDef {
  usartHwFlowControlNone = 0,
  usartHwFlowControlCts = USART_ROUTEPEN_CTSPEN,
  usartHwFlowControlRts = USART_ROUTEPEN_RTSPEN,
  usartHwFlowControlCtsAndRts = USART_ROUTEPEN_CTSPEN | USART_ROUTEPEN_RTSPEN
}
 Hardware Flow Control Selection.
 
enum  USART_ClockMode_TypeDef {
  usartClockMode0 = USART_CTRL_CLKPOL_IDLELOW | USART_CTRL_CLKPHA_SAMPLELEADING,
  usartClockMode1 = USART_CTRL_CLKPOL_IDLELOW | USART_CTRL_CLKPHA_SAMPLETRAILING,
  usartClockMode2 = USART_CTRL_CLKPOL_IDLEHIGH | USART_CTRL_CLKPHA_SAMPLELEADING,
  usartClockMode3 = USART_CTRL_CLKPOL_IDLEHIGH | USART_CTRL_CLKPHA_SAMPLETRAILING
}
 Clock polarity/phase mode.
 
enum  USART_IrDAPw_Typedef {
  usartIrDAPwONE = USART_IRCTRL_IRPW_ONE,
  usartIrDAPwTWO = USART_IRCTRL_IRPW_TWO,
  usartIrDAPwTHREE = USART_IRCTRL_IRPW_THREE,
  usartIrDAPwFOUR = USART_IRCTRL_IRPW_FOUR
}
 Pulse width selection for IrDA mode.
 
enum  USART_I2sFormat_TypeDef {
  usartI2sFormatW32D32 = USART_I2SCTRL_FORMAT_W32D32,
  usartI2sFormatW32D24M = USART_I2SCTRL_FORMAT_W32D24M,
  usartI2sFormatW32D24 = USART_I2SCTRL_FORMAT_W32D24,
  usartI2sFormatW32D16 = USART_I2SCTRL_FORMAT_W32D16,
  usartI2sFormatW32D8 = USART_I2SCTRL_FORMAT_W32D8,
  usartI2sFormatW16D16 = USART_I2SCTRL_FORMAT_W16D16,
  usartI2sFormatW16D8 = USART_I2SCTRL_FORMAT_W16D8,
  usartI2sFormatW8D8 = USART_I2SCTRL_FORMAT_W8D8
}
 I2S format selection.
 
enum  USART_I2sJustify_TypeDef {
  usartI2sJustifyLeft = USART_I2SCTRL_JUSTIFY_LEFT,
  usartI2sJustifyRight = USART_I2SCTRL_JUSTIFY_RIGHT
}
 I2S frame data justify.
 

Function Documentation

◆ USART_BaudrateAsyncSet()

void USART_BaudrateAsyncSet ( USART_TypeDef *  usart,
uint32_t  refFreq,
uint32_t  baudrate,
USART_OVS_TypeDef  ovs 
)

Configure USART/UART operating in asynchronous mode to use a given baudrate (or as close as possible to a specified baudrate).

Parameters
[in]usartA pointer to the USART/UART peripheral register block.
[in]refFreqUSART/UART reference clock frequency in Hz. If set to 0, the currently configured reference clock is assumed.
[in]baudrateBaudrate to try to achieve for USART/UART.
[in]ovsOversampling to be used. Normal is 16x oversampling but lower oversampling may be used to achieve higher rates or better baudrate accuracy in some cases. Notice that lower oversampling frequency makes the channel more vulnerable to bit faults during reception due to clock inaccuracies compared to the link partner.

◆ USART_BaudrateCalc()

uint32_t USART_BaudrateCalc ( uint32_t  refFreq,
uint32_t  clkdiv,
bool  syncmode,
USART_OVS_TypeDef  ovs 
)

Calculate baudrate for USART/UART given reference frequency, clock division, and oversampling rate (if async mode).

This function returns the baudrate that a USART/UART module will use if configured with the given frequency, clock divisor, 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.

Parameters
[in]refFreqUSART/UART HF peripheral frequency used.
[in]clkdivA clock division factor to be used.
[in]syncmode
  • True - synchronous mode operation.
  • False - asynchronous mode operation.
[in]ovsOversampling used if in asynchronous mode. Not used if syncmode is true.
Returns
Baudrate with given settings.

◆ USART_BaudrateGet()

uint32_t USART_BaudrateGet ( USART_TypeDef *  usart)

Get the current baudrate for USART/UART.

This function returns the actual baudrate (not considering oscillator inaccuracies) used by a USART/UART peripheral.

Parameters
[in]usartA pointer to the USART/UART peripheral register block.
Returns
The current baudrate.

◆ USART_BaudrateSyncSet()

void USART_BaudrateSyncSet ( USART_TypeDef *  usart,
uint32_t  refFreq,
uint32_t  baudrate 
)

Configure the USART operating in synchronous mode to use a given baudrate (or as close as possible to a specified baudrate).

The configuration will be set to use a baudrate <= the specified baudrate to ensure that the baudrate does not exceed the specified value.

The fractional clock division is suppressed, although the hardware design allows it. It could cause half clock cycles to exceed a specified limit and thus potentially violate specifications for the slave device. In some special situations, a fractional clock division may be useful even in synchronous mode, but in those cases it must be directly adjusted, possibly assisted by USART_BaudrateCalc():

Warning
The consequence of the aforementioned suppression of the fractional part of the clock divider is that some frequencies won't be achievable. The divider will only be able to be an integer value so the reference clock will only be dividable by N (where N is a positive integer).
Parameters
[in]usartA pointer to the USART peripheral register block. (Cannot be used on UART modules.)
[in]refFreqA USART reference clock frequency in Hz that will be used. If set to 0, the currently-configured reference clock is assumed.
[in]baudrateBaudrate to try to achieve for USART.

◆ USART_Enable()

void USART_Enable ( USART_TypeDef *  usart,
USART_Enable_TypeDef  enable 
)

Enable/disable USART/UART receiver and/or transmitter.

Notice that this function does not do any configuration. Enabling should normally be done after initialization (if not enabled as part of initialization).

Parameters
[in]usartA pointer to the USART/UART peripheral register block.
[in]enableSelect the status for the receiver/transmitter.

◆ USART_InitAsync()

void USART_InitAsync ( USART_TypeDef *  usart,
const USART_InitAsync_TypeDef init 
)

Initialize USART/UART for normal asynchronous mode.

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.)

Parameters
[in]usartA pointer to the USART/UART peripheral register block.
[in]initA pointer to the initialization structure used to configure the basic async setup.

◆ USART_InitSync()

void USART_InitSync ( USART_TypeDef *  usart,
const USART_InitSync_TypeDef init 
)

Initialize USART for synchronous mode.

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.)

Parameters
[in]usartA pointer to the USART peripheral register block. (UART does not support this mode.)
[in]initA pointer to the initialization structure used to configure basic async setup.

◆ USARTn_InitIrDA()

void USARTn_InitIrDA ( USART_TypeDef *  usart,
const USART_InitIrDA_TypeDef init 
)

Initialize USART for asynchronous IrDA mode.

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.)

Parameters
[in]usartA pointer to the USART peripheral register block.
[in]initA pointer to the initialization structure used to configure async IrDA setup.
Note
Not all USART instances support IrDA. See the data sheet for your device.

◆ USART_InitI2s()

void USART_InitI2s ( USART_TypeDef *  usart,
USART_InitI2s_TypeDef init 
)

Initialize USART for I2S mode.

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.)

Parameters
[in]usartA pointer to the USART peripheral register block. (UART does not support this mode.)
[in]initA pointer to the initialization structure used to configure the basic I2S setup.
Note
This function does not apply to all USART's. See the chip Reference Manual.

◆ USART_InitPrsTrigger()

void USART_InitPrsTrigger ( USART_TypeDef *  usart,
const USART_PrsTriggerInit_TypeDef init 
)

Initialize the automatic transmissions using PRS channel as a trigger.

Note
Initialize USART with USART_Init() before setting up the PRS configuration.
Parameters
[in]usartA pointer to USART to configure.
[in]initA pointer to the initialization structure.

◆ USART_Reset()

void USART_Reset ( USART_TypeDef *  usart)