3.15 USART - Universal Synchronous and Asynchronous Receiver and Transmitter

3.15.1 Introduction

The Universal Synchronous and Asynchronous serial Receiver and Transmitter (USART) is a fast and flexible serial communication peripheral. The USART supports several different modes of operation that can accommodate multiple types of applications and communication devices.

3.15.2 Supported Device Families

AVR® DxAVR® ExATtinyATmega

3.15.3 Required header files:

#include"mcc_generated_files/uart/usart[X].h"
Note: Replace [X] with the instance number of the selected USART module.

3.15.4 How to use the USART PLIB Driver

For use-cases for the USART, see: How to use the UART Driver.

The examples there use the portable API interface of the driver, but could easily be modified to directly us the PLIB API.

3.15.5 Module Documentation

3.15.5.1 USART0

This file contains API prototypes and other datatypes for USART0 module.

3.15.5.1.1 Module description

This file contains API prototypes and other datatypes for USART0 module.

Version: USART0 Driver Version
Functions
  • void USART0_Initialize (void)

    This API initializes the USART0 driver. This routine initializes the USART0 module. This routine must be called before any other USART0 routine is called. This routine should only be called once during system initialization.

  • void USART0_Deinitialize (void)

    This API Deinitializes the USART0 driver. This routine disables the USART0 module.

  • void USART0_Enable (void)

    This API enables the USART0 module.

  • void USART0_Disable (void)

    This API disables the USART0 module.

  • void USART0_TxEnable (void)

    This API enables the USART0 transmitter. USART0 should also be enable to send bytes over TX pin.

  • void USART0_TxDisable (void)

    This API disables the USART0 transmitter.

  • void USART0_RxEnable (void)

    This API enables the USART0 Receiver. USART0 should also be enable to receive bytes over RX pin.

  • void USART0_RxDisable (void)

    This API disables the USART0 Receiver.

  • void USART0_TXInterruptEnable (void)

    This API enables the USART0 transmitter interrupt.

  • void USART0_TXInterruptDisable (void)

    This API disables the USART0 transmitter interrupt.

  • void USART0_RXInterruptEnable (void)

    This API enables the USART0 receiver interrupt.

  • void USART0_RXInterruptDisable (void)

    This API disables the USART0 receiver interrupt.

  • bool USART0_IsRxReady (void)

    This API checks if USART0 receiver has received data and ready to be read.

  • bool USART0_IsTxReady (void)

    This function checks if USART0 transmitter is ready to accept a data byte.

  • bool USART0_IsTxDone (void)

    This function return the status of transmit shift register (TSR).

  • uint8_t USART0_ErrotStatusGet (void)

    This function gets the error status of the last read byte. This function should be called before USART0_Read().

  • uint8_t USART0_Read (void)

    This function reads the 8 bits from receiver FIFO register.

  • void USART0_Write (uint8_t txData)

    This function writes a byte of data to the transmitter FIFO register.

  • void USART0_FramingErrorCallbackRegister (void(*callbackHandler)(void))

    This API registers the function to be called upon USART0 framing error.

  • void USART0_OverrunErrorCallbackRegister (void(*callbackHandler)(void))

    This API registers the function to be called upon USART0 overrun error.

  • void USART0_DefaultErrorCallbackRegister (void(*callbackHandler)(void))

    This API registers the function to be called upon USART0 default error.

  • void USART0_ParityErrorCallbackRegister (void(*callbackHandler)(void))

    This API registers the function to be called upon USART0 Parity error.

  • void USART0_TransmitCallbackRegister (void(*callbackHandler)(void))

    This API registers the function to be called upon Transmitter interrupt.

  • void USART0_ReceiveCallbackRegister (void(*callbackHandler)(void))

    This API registers the function to be called upon Receiver interrupt.

Variables
  • const uart_interface_t UART

    External object for usart0_interface.

  • const uart_interface_t UART

    External object for usart0_interface.

3.15.5.1.2 Function Documentation

USART0_DefaultErrorCallbackRegister()

void USART0_DefaultErrorCallbackRegister (void(*)(void) callbackHandler)

This API registers the function to be called upon USART0 default error.

Parameters:
callbackHandler

- a function pointer which will be called upon default error condition.

Returns:

None.

USART0_Deinitialize()

void USART0_Deinitialize (void )

This API Deinitializes the USART0 driver. This routine disables the USART0 module.

Parameters:
None.
Returns:

None.

USART0_Disable()

void USART0_Disable (void )

This API disables the USART0 module.

Parameters:
None.
Returns:

None.

USART0_Enable()

void USART0_Enable (void )

This API enables the USART0 module.

Parameters:
None.
Returns:

None.

USART0_ErrotStatusGet()

uint8_t USART0_ErrotStatusGet (void )

This function gets the error status of the last read byte. This function should be called before USART0_Read().

Parameters:
None.
Returns:

Status of the last read byte. See usart0_status_t struct for more details.

USART0_FramingErrorCallbackRegister()

void USART0_FramingErrorCallbackRegister (void(*)(void) callbackHandler)

This API registers the function to be called upon USART0 framing error.

Parameters:
callbackHandler

- a function pointer which will be called upon framing error condition.

Returns:

None.

USART0_Initialize()

void USART0_Initialize (void )

This API initializes the USART0 driver. This routine initializes the USART0 module. This routine must be called before any other USART0 routine is called. This routine should only be called once during system initialization.

Parameters:
None.
Returns:

None.

Section: USART0 APIs

Parameters:
None.
Returns:

None.

Section: USART0 APIs

USART0_IsRxReady()

bool USART0_IsRxReady (void )

This API checks if USART0 receiver has received data and ready to be read.

Parameters:
None.
Return values:
true

if USART0 receiver FIFO has a data

false

USART0 receiver FIFO is empty

USART0_IsTxDone()

bool USART0_IsTxDone (void )

This function return the status of transmit shift register (TSR).

Parameters:
None.
Return values:
true

if Data completely shifted out from the TSR

false

if Data is present in Transmit FIFO and/or in TSR

USART0_IsTxReady()

bool USART0_IsTxReady (void )

This function checks if USART0 transmitter is ready to accept a data byte.

Parameters:
None.
Return values:
true

if USART0 transmitter FIFO has atleast 1 byte space

false

if USART0 transmitter FIFO is full

USART0_OverrunErrorCallbackRegister()

void USART0_OverrunErrorCallbackRegister (void(*)(void) callbackHandler)

This API registers the function to be called upon USART0 overrun error.

Parameters:
callbackHandler

- a function pointer which will be called upon overrun error condition.

Returns:

None.

USART0_ParityErrorCallbackRegister()

void USART0_ParityErrorCallbackRegister (void(*)(void) callbackHandler)

This API registers the function to be called upon USART0 Parity error.

Parameters:
callbackHandler

- a function pointer which will be called upon Parity error condition.

Returns:

None.

USART0_Read()

uint8_t USART0_Read (void )

This function reads the 8 bits from receiver FIFO register.

Precondition:

The transfer status should be checked to see if the receiver is not empty before calling this function. USART0_IsRxReady() should be checked in if () before calling this API.

Parameters:
None.
Returns:

8-bit data from RX FIFO register.

USART0_ReceiveCallbackRegister()

void USART0_ReceiveCallbackRegister (void(*)(void) callbackHandler)

This API registers the function to be called upon Receiver interrupt.

Parameters:
callbackHandler

- a function pointer which will be called upon Receiver interrupt condition.

Returns:

None.

USART0_RxDisable()

void USART0_RxDisable (void )

This API disables the USART0 Receiver.

Parameters:
None.
Returns:

None.

USART0_RxEnable()

void USART0_RxEnable (void )

This API enables the USART0 Receiver. USART0 should also be enable to receive bytes over RX pin.

Parameters:
None.
Returns:

None.

USART0_RXInterruptDisable()

void USART0_RXInterruptDisable (void )

This API disables the USART0 receiver interrupt.

Parameters:
None.
Returns:

None.

USART0_RXInterruptEnable()

void USART0_RXInterruptEnable (void )

This API enables the USART0 receiver interrupt.

Parameters:
None.
Returns:

None.

USART0_TransmitCallbackRegister()

void USART0_TransmitCallbackRegister (void(*)(void) callbackHandler)

This API registers the function to be called upon Transmitter interrupt.

Parameters:
callbackHandler

- a function pointer which will be called upon Transmitter interrupt condition.

Returns:

None.

USART0_TxDisable()

void USART0_TxDisable (void )

This API disables the USART0 transmitter.

Parameters:
None.
Returns:

None.

USART0_TxEnable()

void USART0_TxEnable (void )

This API enables the USART0 transmitter. USART0 should also be enable to send bytes over TX pin.

Parameters:
None.
Returns:

None.

USART0_TXInterruptDisable()

void USART0_TXInterruptDisable (void )

This API disables the USART0 transmitter interrupt.

Parameters:
None.
Returns:

None.

USART0_TXInterruptEnable()

void USART0_TXInterruptEnable (void )

This API enables the USART0 transmitter interrupt.

Parameters:
None.
Returns:

None.

USART0_Write()

void USART0_Write (uint8_t txData)

This function writes a byte of data to the transmitter FIFO register.

Precondition:

The transfer status should be checked to see if the transmitter is ready to accept a byte before calling this function. USART0_IsTxReady() should be checked in if() before calling this API.

Parameters:
txData

- Data byte to write to the TX FIFO.

Returns:

None.

3.15.5.1.3 Variable Documentation

UART[1/2]

const uart_interface_t UART

External object for usart0_interface.

Section: Data Type Definitions

Section: Driver Interface

Section: Included FilesSection: Macro DeclarationsSection: Driver Interface

UART[1/2]

const uart_interface_t UART

External object for usart0_interface.

Section: Data Type Definitions

Section: Driver Interface

Section: Included FilesSection: Macro DeclarationsSection: Driver Interface

3.15.6 Class Documentation

3.15.6.1 usart0_status_t Struct Reference

3.15.6.1.1 Detailed Description

@breif This is an instance of USART0_STATUS for USART0 module

#include <usart_interrupt.h>

3.15.6.1.2 Member Data Documentation

The documentation for this struct was generated from the following files:

source/

usart_interrupt.h

source/

usart_polling.h

ferr

unsigned usart0_status_t::ferr

This is a bit field for Framing Error status

oerr

unsigned usart0_status_t::oerr

This is a bit field for Overfrun Error status

perr

unsigned usart0_status_t::perr

This is a bit field for Parity Error status

reserved

unsigned usart0_status_t::reserved

Reserved

status

uint8_t usart0_status_t::status

Group byte for status errors

3.15.7 File Documentation

3.15.7.1 source/usart_interrupt.c File Reference

This is the generated driver implementation file for the USART0 driver using.

#include "../usart0.h"

3.15.7.1.1 Functions

  • static void USART0_DefaultFramingErrorCallback (void)

  • static void USART0_DefaultOverrunErrorCallback (void)

  • static void USART0_DefaultErrorCallback (void)

  • static void USART0_DefaultParityErrorCallback (void)

  • void USART0_TransmitISR (void)

  • void USART0_ReceiveISR (void)

  • void USART0_Initialize (void)

    This API initializes the USART0 driver. This routine initializes the USART0 module. This routine must be called before any other USART0 routine is called. This routine should only be called once during system initialization.

  • void USART0_Deinitialize (void)

    This API Deinitializes the USART0 driver. This routine disables the USART0 module.

  • void USART0_Enable (void)

    This API enables the USART0 module.

  • void USART0_Disable (void)

    This API disables the USART0 module.

  • void USART0_TxEnable (void)

    This API enables the USART0 transmitter. USART0 should also be enable to send bytes over TX pin.

  • void USART0_TxDisable (void)

    This API disables the USART0 transmitter.

  • void USART0_RxEnable (void)

    This API enables the USART0 Receiver. USART0 should also be enable to receive bytes over RX pin.

  • void USART0_RxDisable (void)

    This API disables the USART0 Receiver.

  • void USART0_TXInterruptEnable (void)

    This API enables the USART0 transmitter interrupt.

  • void USART0_TXInterruptDisable (void)

    This API disables the USART0 transmitter interrupt.

  • void USART0_RXInterruptEnable (void)

    This API enables the USART0 receiver interrupt.

  • void USART0_RXInterruptDisable (void)

    This API disables the USART0 receiver interrupt.

  • bool USART0_IsRxReady (void)

    This API checks if USART0 receiver has received data and ready to be read.

  • bool USART0_IsTxReady (void)

    This function checks if USART0 transmitter is ready to accept a data byte.

  • bool USART0_IsTxDone (void)

    This function return the status of transmit shift register (TSR).

  • uint8_t USART0_ErrotStatusGet (void)

    This function gets the error status of the last read byte. This function should be called before USART0_Read().

  • uint8_t USART0_Read (void)

    This function reads the 8 bits from receiver FIFO register.

  • ISR (USART0_RXC_vect)

  • void USART0_Write (uint8_t txData)

    This function writes a byte of data to the transmitter FIFO register.

  • ISR (USART0_DRE_vect)

  • ISR (USART0_TXC_vect)

  • void USART0_FramingErrorCallbackRegister (void(*callbackHandler)(void))

    This API registers the function to be called upon USART0 framing error.

  • void USART0_OverrunErrorCallbackRegister (void(*callbackHandler)(void))

    This API registers the function to be called upon USART0 overrun error.

  • void USART0_DefaultErrorCallbackRegister (void(*callbackHandler)(void))

    This API registers the function to be called upon USART0 default error.

  • void USART0_ParityErrorCallbackRegister (void(*callbackHandler)(void))

    This API registers the function to be called upon USART0 Parity error.

  • void USART0_ReceiveCallbackRegister (void(*callbackHandler)(void))

    This API registers the function to be called upon Receiver interrupt.

  • void USART0_TransmitCallbackRegister (void(*callbackHandler)(void))

    This API registers the function to be called upon Transmitter interrupt.

3.15.7.1.3 Variables

  • const uart_interface_t UART

    External object for usart0_interface.

  • static volatile uint8_t usart0TxHead = 0

  • static volatile uint8_t usart0TxTail = 0

  • static volatile uint8_t usart0TxBuffer [USART0_TX_BUFFER_SIZE]

  • volatile uint8_t usart0TxBufferRemaining

  • static volatile uint8_t usart0RxHead = 0

  • static volatile uint8_t usart0RxTail = 0

  • static volatile uint8_t usart0RxBuffer [USART0_RX_BUFFER_SIZE]

  • static volatile usart0_status_tusart0RxStatusBuffer [USART0_RX_BUFFER_SIZE]

  • volatile uint8_t usart0RxCount

  • static volatile usart0_status_tusart0RxLastError

  • void(* USART0_FramingErrorHandler )(void)

  • void(* USART0_OverrunErrorHandler )(void)

  • void(* USART0_DefaultErrorHandler )(void)

  • void(* USART0_ParityErrorHandler )(void)

  • void(* USART0_TxInterruptHandler )(void)

  • void(* USART0_RxInterruptHandler )(void)

3.15.7.1.4 Detailed Description

This is the generated driver implementation file for the USART0 driver using.

USART0 Generated Driver API Header File

Version: USART0 Driver Version

3.15.7.1.5 Macro Definition Documentation

USART0_TX_BUFFER_SIZE

#define USART0_TX_BUFFER_SIZE (8)

Section: Included FilesSection: Macro Declarations

3.15.7.1.6 Variable Documentation

USART0_FramingErrorHandler

void(* USART0_FramingErrorHandler) (void)

Section: USART0 APIs

usart0TxHead

volatile uint8_t usart0TxHead = 0[static]

Section: USART0 variables

3.15.7.2 source/usart_interrupt.h File Reference

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "../system/system.h"
#include "uart_interface.h"

3.15.7.2.2 Functions

  • void USART0_Initialize (void)

    This API initializes the USART0 driver. This routine initializes the USART0 module. This routine must be called before any other USART0 routine is called. This routine should only be called once during system initialization.

  • void USART0_Deinitialize (void)

    This API Deinitializes the USART0 driver. This routine disables the USART0 module.

  • void USART0_Enable (void)

    This API enables the USART0 module.

  • void USART0_Disable (void)

    This API disables the USART0 module.

  • void USART0_TxEnable (void)

    This API enables the USART0 transmitter. USART0 should also be enable to send bytes over TX pin.

  • void USART0_TxDisable (void)

    This API disables the USART0 transmitter.

  • void USART0_RxEnable (void)

    This API enables the USART0 Receiver. USART0 should also be enable to receive bytes over RX pin.

  • void USART0_RxDisable (void)

    This API disables the USART0 Receiver.

  • void USART0_TXInterruptEnable (void)

    This API enables the USART0 transmitter interrupt.

  • void USART0_TXInterruptDisable (void)

    This API disables the USART0 transmitter interrupt.

  • void USART0_RXInterruptEnable (void)

    This API enables the USART0 receiver interrupt.

  • void USART0_RXInterruptDisable (void)

    This API disables the USART0 receiver interrupt.

  • bool USART0_IsRxReady (void)

    This API checks if USART0 receiver has received data and ready to be read.

  • bool USART0_IsTxReady (void)

    This function checks if USART0 transmitter is ready to accept a data byte.

  • bool USART0_IsTxDone (void)

    This function return the status of transmit shift register (TSR).

  • uint8_t USART0_ErrotStatusGet (void)

    This function gets the error status of the last read byte. This function should be called before USART0_Read().

  • uint8_t USART0_Read (void)

    This function reads the 8 bits from receiver FIFO register.

  • void USART0_Write (uint8_t txData)

    This function writes a byte of data to the transmitter FIFO register.

  • void USART0_FramingErrorCallbackRegister (void(*callbackHandler)(void))

    This API registers the function to be called upon USART0 framing error.

  • void USART0_OverrunErrorCallbackRegister (void(*callbackHandler)(void))

    This API registers the function to be called upon USART0 overrun error.

  • void USART0_DefaultErrorCallbackRegister (void(*callbackHandler)(void))

    This API registers the function to be called upon USART0 default error.

  • void USART0_ParityErrorCallbackRegister (void(*callbackHandler)(void))

    This API registers the function to be called upon USART0 Parity error.

  • void USART0_TransmitCallbackRegister (void(*callbackHandler)(void))

    This API registers the function to be called upon Transmitter interrupt.

  • void USART0_ReceiveCallbackRegister (void(*callbackHandler)(void))

    This API registers the function to be called upon Receiver interrupt.

3.15.7.2.3 Macros

3.15.7.2.4 Variables

  • const uart_interface_t UART

    External object for usart0_interface.

3.15.7.2.5 Detailed Description

USART0 Generated Driver API Header File

3.15.7.2.6 Macro Definition Documentation

USART0_BAUD_RATE

#define USART0_BAUD_RATE( BAUD_RATE) (((float)4000000 * 64 / (16 * (float)BAUD_RATE)) + 0.5)

Section: Included Files

3.15.7.3 source/usart_polling.c File Reference

This is the generated driver implementation file for the USART0 driver using.

#include "../usart0.h"

3.15.7.3.1 Functions

  • static void USART0_DefaultFramingErrorCallback (void)

  • static void USART0_DefaultOverrunErrorCallback (void)

  • static void USART0_DefaultErrorCallback (void)

  • static void USART0_DefaultParityErrorCallback (void)

  • void USART0_Initialize (void)

    This API initializes the USART0 driver. This routine initializes the USART0 module. This routine must be called before any other USART0 routine is called. This routine should only be called once during system initialization.

  • void USART0_Deinitialize (void)

    This API Deinitializes the USART0 driver. This routine disables the USART0 module.

  • void USART0_Enable (void)

    This API enables the USART0 module.

  • void USART0_Disable (void)

    This API disables the USART0 module.

  • void USART0_TxEnable (void)

    This API enables the USART0 transmitter. USART0 should also be enable to send bytes over TX pin.

  • void USART0_TxDisable (void)

    This API disables the USART0 transmitter.

  • void USART0_RxEnable (void)

    This API enables the USART0 Receiver. USART0 should also be enable to receive bytes over RX pin.

  • void USART0_RxDisable (void)

    This API disables the USART0 Receiver.

  • bool USART0_IsRxReady (void)

    This API checks if USART0 receiver has received data and ready to be read.

  • bool USART0_IsTxReady (void)

    This function checks if USART0 transmitter is ready to accept a data byte.

  • bool USART0_IsTxDone (void)

    This function return the status of transmit shift register (TSR).

  • uint8_t USART0_ErrotStatusGet (void)

    This function gets the error status of the last read byte. This function should be called before USART0_Read().

  • uint8_t USART0_Read (void)

    This function reads the 8 bits from receiver FIFO register.

  • void USART0_Write (uint8_t txData)

    This function writes a byte of data to the transmitter FIFO register.

  • void USART0_FramingErrorCallbackRegister (void(*callbackHandler)(void))

    This API registers the function to be called upon USART0 framing error.

  • void USART0_OverrunErrorCallbackRegister (void(*callbackHandler)(void))

    This API registers the function to be called upon USART0 overrun error.

  • void USART0_DefaultErrorCallbackRegister (void(*callbackHandler)(void))

    This API registers the function to be called upon USART0 default error.

  • void USART0_ParityErrorCallbackRegister (void(*callbackHandler)(void))

    This API registers the function to be called upon USART0 Parity error.

3.15.7.3.3 Detailed Description

This is the generated driver implementation file for the USART0 driver using.

USART0 Generated Driver API Header File

Version: USART0 Driver Version

3.15.7.3.4 Variable Documentation

USART0_FramingErrorHandler

void(* USART0_FramingErrorHandler) (void)

Section: USART0 APIs

usart0RxLastError

volatile usart0_status_t usart0RxLastError[static]

Section: USART0 variables

3.15.7.4 source/usart_polling.h File Reference

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "../system/system.h"
#include "uart_interface.h"

3.15.7.4.2 Functions

  • void USART0_Initialize (void)

    This API initializes the USART0 driver. This routine initializes the USART0 module. This routine must be called before any other USART0 routine is called. This routine should only be called once during system initialization.

  • void USART0_Deinitialize (void)

    This API Deinitializes the USART0 driver. This routine disables the USART0 module.

  • void USART0_Enable (void)

    This API enables the USART0 module.

  • void USART0_Disable (void)

    This API disables the USART0 module.

  • void USART0_TxEnable (void)

    This API enables the USART0 transmitter. USART0 should also be enable to send bytes over TX pin.

  • void USART0_TxDisable (void)

    This API disables the USART0 transmitter.

  • void USART0_RxEnable (void)

    This API enables the USART0 Receiver. USART0 should also be enable to receive bytes over RX pin.

  • void USART0_RxDisable (void)

    This API disables the USART0 Receiver.

  • bool USART0_IsRxReady (void)

    This API checks if USART0 receiver has received data and ready to be read.

  • bool USART0_IsTxReady (void)

    This function checks if USART0 transmitter is ready to accept a data byte.

  • bool USART0_IsTxDone (void)

    This function return the status of transmit shift register (TSR).

  • uint8_t USART0_ErrotStatusGet (void)

    This function gets the error status of the last read byte. This function should be called before USART0_Read().

  • uint8_t USART0_Read (void)

    This function reads the 8 bits from receiver FIFO register.

  • void USART0_Write (uint8_t txData)

    This function writes a byte of data to the transmitter FIFO register.

  • void USART0_FramingErrorCallbackRegister (void(*callbackHandler)(void))

    This API registers the function to be called upon USART0 framing error.

  • void USART0_OverrunErrorCallbackRegister (void(*callbackHandler)(void))

    This API registers the function to be called upon USART0 overrun error.

  • void USART0_DefaultErrorCallbackRegister (void(*callbackHandler)(void))

    This API registers the function to be called upon USART0 default error.

  • void USART0_ParityErrorCallbackRegister (void(*callbackHandler)(void))

    This API registers the function to be called upon USART0 Parity error.

3.15.7.4.3 Macros

3.15.7.4.4 Variables

  • const uart_interface_t UART

    External object for usart0_interface.

3.15.7.4.5 Detailed Description

USART0 Generated Driver API Header File

3.15.7.4.6 Macro Definition Documentation

USART0_BAUD_RATE

#define USART0_BAUD_RATE( BAUD_RATE) (((float)4000000 * 64 / (16 * (float)BAUD_RATE)) + 0.5)

Section: Included Files