3.26.1 UART

Universal Asynchronous Receiver Transmitter

3.26.1.1 Introduction

The Universal Asynchronous Receiver Transmitter (UART) -capable peripherals come in different variants on microcontrollers. Sometimes, the peripheral is named UART or USART and sometimes it is called EUSART (Enhanced Universal Synchronous Asynchronous Receiver Transmitter) to emphasize enhanced functionalities. The data sheet of each device shows the type of UART peripheral it has.

3.26.1.2 Supported Device Families

PIC18F-K42PIC18F-K83PIC18F-Q20
PIC18F-Q24PIC18F-Q71PIC18F-Q83
PIC18F-Q84

3.26.1.3 Required header files:


#include "mcc_generated_files/uart/uart.h"

3.26.1.4 How to use the UART PLIB Driver

For use-cases for the UART, 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.26.1.5 Module Documentation

3.26.1.5.1 UART1

This file contains API prototypes and other data types for the UART1 module.

Module description

This file contains API prototypes and other data types for the UART1 module.

Version: UART1 Driver Version 3.0.6
Data structures
  • struct uart1_status_t

    This is an instance of UART1_STATUS for UART1 module.

Functions
Variables

Function Documentation

UART1_AutoBaudDetectCompleteReset()

void UART1_AutoBaudDetectCompleteReset (void )[inline]

Resets the UART1 Auto-Baud Detection Complete bit.

Parameters:
None.
Returns:

None.

UART1_AutoBaudDetectOverflowReset()

void UART1_AutoBaudDetectOverflowReset (void )[inline]

Resets the UART1 Auto-Baud Detection Overflow bit.

Parameters:
None.
Returns:

None.

UART1_AutoBaudQuery()

bool UART1_AutoBaudQuery (void )[inline]

Reads the UART1 Auto-Baud Detection Complete bit.

Parameters:
None.
Returns:

None.

UART1_AutoBaudSet()

void UART1_AutoBaudSet (bool enable)[inline]

Enables the UART1 Auto-Baud Detection bit.

Parameters:
bool

enable

Returns:

None.

UART1_Deinitialize()

void UART1_Deinitialize (void )

Deinitializes and disables the UART1 module.

Parameters:
None.
Returns:

None.

UART1_Disable()

void UART1_Disable (void )[inline]

Disables the UART1 module.

Parameters:
None.
Returns:

None.

UART1_Enable()

void UART1_Enable (void )[inline]

Enables the UART1 module.  

Parameters:
None.
Returns:

None.

UART1_ErrorCallbackRegister()

void UART1_ErrorCallbackRegister (void(*)(void) callbackHandler)

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

Parameters:
callbackHandler

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

Returns:

None.

UART1_ErrorGet()

size_t UART1_ErrorGet (void )

Gets the error status of the last read byte. Call this function before calling UART1_Read().

Precondition:

Call UART1_RxEnable() to enable RX before calling this API.

Parameters:
None.
Returns:

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

UART1_ErrorInterruptDisable()

void UART1_ErrorInterruptDisable (void )[inline]

Disables the UART1 Error Interrupt bit.

Parameters:
None.
Returns:

None.

UART1_ErrorInterruptEnable()

void UART1_ErrorInterruptEnable (void )[inline]

Enables the UART1 Error Interrupt bit.

Parameters:
None.
Returns:

None.

UART1_ErrorISR()

void UART1_ErrorISR (void )

This function is a ISR function to be called upon Error interrupt.

Parameters:
None.
Returns:

None.

UART1_FramingErrorCallbackRegister()

void UART1_FramingErrorCallbackRegister (void(*)(void) callbackHandler)

Calls the function upon UART1 framing error.

Parameters:
callbackHandler

- Function pointer called when the framing error condition occurs

Returns:

None.

UART1_GeneralInterruptCallbackRegister()

void UART1_GeneralInterruptCallbackRegister (void(*)(void) callbackHandler)

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

Parameters:
callbackHandler

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

Returns:

None.

UART1_GeneralInterruptDisable()

void UART1_GeneralInterruptDisable (void )[inline]

Disables the UART1 Interrupt.

Parameters:
None.
Returns:

None.

UART1_GeneralInterruptEnable()

void UART1_GeneralInterruptEnable (void )[inline]

Enables the UART1 Interrupt.

Parameters:
None.
Returns:

None.

UART1_GeneralISR()

void UART1_GeneralISR (void )

This function is a ISR function to be called upon general interrupt.

Parameters:
void.
Returns:

None.

UART1_Initialize()

void UART1_Initialize (void )

Initializes the UART1 module. This routine is called only once during system initialization, before calling other APIs.

Parameters:
None.
Returns:

None.

Section: UART1 APIs

UART1_IsAutoBaudDetectOverflow()

bool UART1_IsAutoBaudDetectOverflow (void )[inline]

Reads the UART1 Auto-Baud Detection Overflow bit.

Parameters:
None.
Returns:

None.

UART1_IsRxReady()

bool UART1_IsRxReady (void )

Checks if the UART1 receiver has received data and is ready to be read.

Parameters:
None.
Return values:
True

- UART1 receiver FIFO has data

False

- UART1 receiver FIFO is empty

UART1_IsTxDone()

bool UART1_IsTxDone (void )

Returns the status of the Transmit Shift Register (TSR).

Parameters:
None.
Return values:
True

- Data completely shifted out from the TSR

False

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

UART1_IsTxReady()

bool UART1_IsTxReady (void )

Checks if the UART1 transmitter is ready to accept a data byte.

Parameters:
None.
Return values:
True

- The UART1 transmitter FIFO has at least a one byte space

False

- The UART1 transmitter FIFO is full

UART1_OverrunErrorCallbackRegister()

void UART1_OverrunErrorCallbackRegister (void(*)(void) callbackHandler)

Calls the function upon UART1 overrun error.

Parameters:
callbackHandler

- Function pointer called when the overrun error condition occurs

Returns:

None.

UART1_ParityErrorCallbackRegister()

void UART1_ParityErrorCallbackRegister (void(*)(void) callbackHandler)

Calls the function upon UART1 parity error.

Parameters:
callbackHandler

- Function pointer called when the parity error condition occurs

Returns:

None.

UART1_Read()

uint8_t UART1_Read (void )

Reads the eight bits from the Receiver FIFO register.

Precondition:

Check the transfer status to see if the receiver is not empty before calling this function. Check UART1_IsRxReady() in if () before calling this API.

Parameters:
None.
Returns:

8-bit data from the RX FIFO register

UART1_ReceiveDisable()

void UART1_ReceiveDisable (void )[inline]

Disables the UART1 receiver.

Parameters:
None.
Returns:

None.

UART1_ReceiveEnable()

void UART1_ReceiveEnable (void )[inline]

Enables the UART1 receiver. This routine also enables UART1 to send bytes over the RX pin.

Parameters:
None.
Returns:

None.

UART1_ReceiveInterruptDisable()

void UART1_ReceiveInterruptDisable (void )[inline]

Disables the UART1 receiver interrupt.

Parameters:
None.
Returns:

None.

UART1_ReceiveInterruptEnable()

void UART1_ReceiveInterruptEnable (void )[inline]

Enables the UART1 receiver interrupt.

Parameters:
None.
Returns:

None.

UART1_ReceiveISR()

void UART1_ReceiveISR (void )

Implements the ISR for the UART1 receiver interrupt.

Parameters:
void.
Returns:

None.

UART1_RxCompleteCallbackRegister()

void UART1_RxCompleteCallbackRegister (void(*)(void) callbackHandler)

Registers the function to be called when the receiver interrupt occurs.

Parameters:
callbackHandler

- Function pointer called when the receiver interrupt condition occurs

Returns:

None.

UART1_SendBreakControlDisable()

void UART1_SendBreakControlDisable (void )[inline]

Disables the UART1 Send Break Control bit.

Parameters:
None.
Returns:

None.

UART1_SendBreakControlEnable()

void UART1_SendBreakControlEnable (void )[inline]

Enables the UART1 Send Break Control bit.

Parameters:
None.
Returns:

None.

UART1_TransmitDisable()

void UART1_TransmitDisable (void )[inline]

Disables the UART1 transmitter.

Parameters:
None.
Returns:

None.

UART1_TransmitEnable()

void UART1_TransmitEnable (void )[inline]

Enables the UART1 transmitter. This routine also enables UART1 to send bytes over the TX pin.

Parameters:
None.
Returns:

None.

UART1_TransmitInterruptDisable()

void UART1_TransmitInterruptDisable (void )[inline]

Disables the UART1 transmitter interrupt.

Parameters:
None.
Returns:

None.

UART1_TransmitInterruptEnable()

void UART1_TransmitInterruptEnable (void )[inline]

Enables the UART1 transmitter interrupt.

Parameters:
None.
Returns:

None.

UART1_TransmitISR()

void UART1_TransmitISR (void )

Implements the Interrupt Service Routine (ISR) for the UART1 transmitter interrupt.

Parameters:
None.
Returns:

None.

UART1_TxCompleteCallbackRegister()

void UART1_TxCompleteCallbackRegister (void(*)(void) callbackHandler)

Registers the function to be called when the transmitter interrupt occurs.

Parameters:
callbackHandler

- Function pointer called when the transmitter interrupt condition occurs

Returns:

None.

UART1_Write()

void UART1_Write (uint8_t txData)

Writes a byte of data to the Transmitter FIFO register.

Precondition:

Check the transfer status to see if the transmitter is not empty before calling this function. Check UART1_IsTxReady() in if () before calling this API.

Parameters:
txData

- Data byte to write to the TX FIFO

Returns:

None.

Variable Documentation

UART1

const uart_drv_interface_t UART1

External object for uart_drv_interface.

Section: Driver Interface

UART1_ErrorInterruptHandler

void(* UART1_ErrorInterruptHandler) (void)

This is a pointer to the function that will be called upon error interrupt.

Precondition:

Initialize the UART1 module with error interrupt enabled

Parameters:
None.
Returns:

None.

UART1_GeneralInterruptHandler

void(* UART1_GeneralInterruptHandler) (void)

This is a pointer to the function that will be called upon general interrupt.

Precondition:

Initialize the UART1 module with general interrupt enabled

Parameters:
None.
Returns:

None.

UART1_RxInterruptHandler

void(* UART1_RxInterruptHandler) (void)

Pointer to the function called when the receiver interrupt occurs.

Precondition:

Initialize the UART1 module with the receive interrupt enabled.

Parameters:
None.
Returns:

None.

UART1_TxInterruptHandler

void(* UART1_TxInterruptHandler) (void)

Pointer to the function called when the transmitter interrupt occurs.

Precondition:

Initialize the UART1 module with the transmit interrupt enabled.

Parameters:
None.
Returns:

None.

3.26.1.5.2 UART_DRV_INTERFACE

This file contains API prototypes and data types of the Universal Asynchronous Receiver and Transmitter (UART) interface.

This file contains API prototypes and data types of the Universal Asynchronous Receiver and Transmitter (UART) interface.

Version: UART Driver Version 3.0.1

3.26.1.5.3 UART_TYPES

This file contains the enumeration of different Universal Asynchronous Receiver and Transmitter (UART) baud rates.

This file contains the enumeration of different Universal Asynchronous Receiver and Transmitter (UART) baud rates.

Version: UART Driver Version 3.0.1

3.26.1.6 Data Structure Documentation

3.26.1.6.1 uart1_status_t Struct Reference

This is an instance of UART1_STATUS for UART1 module.

Detailed Description

This is an instance of UART1_STATUS for UART1 module.

#include <uart1.h>

Field Documentation

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

source/

uart1.h

@1

struct { ... }

ferr

uint8_t ferr

This is a bit field for Framing Error status

oerr

uint8_t oerr

This is a bit field for Overfrun Error status

perr

uint8_t perr

This is a bit field for Parity Error status

reserved

uint8_t reserved

Reserved

status

size_t status

Group byte for status errors

3.26.1.6.2 uart_drv_interface_t Struct Reference

Structure containing the function pointers of UART driver.

Detailed Description

Structure containing the function pointers of UART driver.

Section: Included Files

#include <uart_drv_interface.h>

Data Fields

Field Documentation

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

source/

uart_drv_interface.h

AutoBaudEventEnableGet

bool(* AutoBaudEventEnableGet) (void)

AutoBaudQuery

bool(* AutoBaudQuery) (void)

AutoBaudSet

void(* AutoBaudSet) (bool enable)

BaudRateGet

uint32_t(* BaudRateGet) (void)

BaudRateSet

void(* BaudRateSet) (uint32_t baudRate)

BRGCountGet

uint32_t(* BRGCountGet) (void)

BRGCountSet

void(* BRGCountSet) (uint32_t brgValue)

Deinitialize

void(* Deinitialize) (void)

ErrorGet

size_t(* ErrorGet) (void)

EventCallbackRegister

void(* EventCallbackRegister) (void(*CallbackHandler)(void))

FramingErrorCallbackRegister

void(* FramingErrorCallbackRegister) (void(*CallbackHandler)(void))

Initialize

void(* Initialize) (void)

IsRxReady

bool(* IsRxReady) (void)

IsTxDone

bool(* IsTxDone) (void)

IsTxReady

bool(* IsTxReady) (void)

OverrunErrorCallbackRegister

void(* OverrunErrorCallbackRegister) (void(*CallbackHandler)(void))

ParityErrorCallbackRegister

void(* ParityErrorCallbackRegister) (void(*CallbackHandler)(void))

Read

uint8_t(* Read) (void)

RxCompleteCallbackRegister

void(* RxCompleteCallbackRegister) (void(*CallbackHandler)(void))

TransmitDisable

void(* TransmitDisable) (void)

TransmitEnable

void(* TransmitEnable) (void)

TxCollisionCallbackRegister

void(* TxCollisionCallbackRegister) (void(*CallbackHandler)(void))

TxCompleteCallbackRegister

void(* TxCompleteCallbackRegister) (void(*CallbackHandler)(void))

Write

void(* Write) (uint8_t)

3.26.1.7 File Documentation

3.26.1.7.1 source/uart1.c File Reference

This is the generated driver implementation file for the UART1 driver using Configurable Custom Logic (CCL).

#include "../uart1.h"

Functions

Variables

Detailed Description

This is the generated driver implementation file for the UART1 driver using Configurable Custom Logic (CCL).

UART1 Generated Driver API Header File

Version: UART1 Driver Version 3.0.6

Function Documentation

getch()

int getch (void )

putch()

void putch (char txData)

UART1_DefaultFramingErrorCallback()

static void UART1_DefaultFramingErrorCallback (void )[static]

UART1_DefaultOverrunErrorCallback()

static void UART1_DefaultOverrunErrorCallback (void )[static]

UART1_DefaultParityErrorCallback()

static void UART1_DefaultParityErrorCallback (void )[static]

Macro Definition Documentation

UART1_RX_BUFFER_MASK

#define UART1_RX_BUFFER_MASK (UART1_RX_BUFFER_SIZE - 1)

UART1_RX_BUFFER_SIZE

#define UART1_RX_BUFFER_SIZE (8)

UART1_TX_BUFFER_MASK

#define UART1_TX_BUFFER_MASK (UART1_TX_BUFFER_SIZE - 1)

UART1_TX_BUFFER_SIZE

#define UART1_TX_BUFFER_SIZE (8)

Section: Included Files Section: Macro Declarations

Variable Documentation

UART1_CommunicationErrorInterruptHandler

void(* UART1_CommunicationErrorInterruptHandler) (void)[static]

UART1_ErrorInterruptHandler

void(* UART1_ErrorInterruptHandler) (void)

UART1_FramingErrorHandler

void(* UART1_FramingErrorHandler) (void)

Section: UART1 APIs

UART1_GeneralErrorInterruptHandler

void(* UART1_GeneralErrorInterruptHandler) (void)[static]

UART1_GeneralInterruptHandler

void(* UART1_GeneralInterruptHandler) (void)

UART1_OverrunErrorHandler

void(* UART1_OverrunErrorHandler) (void)

UART1_ParityErrorHandler

void(* UART1_ParityErrorHandler) (void)

UART1_RxCompleteInterruptHandler

void(* UART1_RxCompleteInterruptHandler) (void)[static]

UART1_RxInterruptHandler

void(* UART1_RxInterruptHandler) (void)

UART1_TxCompleteInterruptHandler

void(* UART1_TxCompleteInterruptHandler) (void)[static]

UART1_TxInterruptHandler

void(* UART1_TxInterruptHandler) (void)

uart1RxBuffer

volatile uint8_t uart1RxBuffer[UART1_RX_BUFFER_SIZE][static]

uart1RxCount

volatile uint8_t uart1RxCount

uart1RxHead

volatile uint8_t uart1RxHead = 0[static]

uart1RxLastError

volatile uart1_status_t uart1RxLastError

uart1RxStatusBuffer

volatile uart1_status_t uart1RxStatusBuffer[UART1_RX_BUFFER_SIZE][static]

uart1RxTail

volatile uint8_t uart1RxTail = 0[static]

uart1TxBuffer

volatile uint8_t uart1TxBuffer[UART1_TX_BUFFER_SIZE][static]

uart1TxBufferRemaining

volatile uint8_t uart1TxBufferRemaining

uart1TxHead

volatile uint8_t uart1TxHead = 0[static]

Section: UART1 variables

uart1TxTail

volatile uint8_t uart1TxTail = 0[static]

3.26.1.7.2 source/uart1.h File Reference

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

Data structures

  • struct uart1_status_t

    This is an instance of UART1_STATUS for UART1 module.

Functions

Macros

Variables

Detailed Description

UART1 Generated Driver API Header File

Macro Definition Documentation

UART1__AutoBaudEventEnableGet

#define UART1__AutoBaudEventEnableGet (NULL)

UART1__IsRxReady

#define UART1__IsRxReady UART1_IsRxReady

UART1_AutoBaudDetectCompleteReset

#define UART1_AutoBaudDetectCompleteReset UART1_AutoBaudDetectCompleteReset

UART1_AutoBaudDetectOverflowReset

#define UART1_AutoBaudDetectOverflowReset UART1_AutoBaudDetectOverflowReset

UART1_AutoBaudQuery

#define UART1_AutoBaudQuery UART1_AutoBaudQuery

UART1_AutoBaudSet

#define UART1_AutoBaudSet UART1_AutoBaudSet

UART1_BaudRateGet

#define UART1_BaudRateGet (NULL)

UART1_BaudRateSet

#define UART1_BaudRateSet (NULL)

UART1_BRGCountGet

#define UART1_BRGCountGet (NULL)

UART1_BRGCountSet

#define UART1_BRGCountSet (NULL)

UART1_Deinitialize

#define UART1_Deinitialize UART1_Deinitialize

UART1_ErrorGet

#define UART1_ErrorGet UART1_ErrorGet

UART1_EventCallbackRegister

#define UART1_EventCallbackRegister (NULL)

UART1_FramingErrorCallbackRegister

#define UART1_FramingErrorCallbackRegister UART1_FramingErrorCallbackRegister

UART1_Initialize

#define UART1_Initialize UART1_Initialize

UART1_interface

#define UART1_interface UART1

UART1_IsAutoBaudDetectOverflow

#define UART1_IsAutoBaudDetectOverflow UART1_IsAutoBaudDetectOverflow

UART1_IsTxDone

#define UART1_IsTxDone UART1_IsTxDone

UART1_IsTxReady

#define UART1_IsTxReady UART1_IsTxReady

UART1_OverrunErrorCallbackRegister

#define UART1_OverrunErrorCallbackRegister UART1_OverrunErrorCallbackRegister

UART1_ParityErrorCallbackRegister

#define UART1_ParityErrorCallbackRegister UART1_ParityErrorCallbackRegister

UART1_Read

#define UART1_Read UART1_Read

UART1_RxCompleteCallbackRegister

#define UART1_RxCompleteCallbackRegister UART1_RxCompleteCallbackRegister

UART1_TransmitDisable

#define UART1_TransmitDisable UART1_TransmitDisable

UART1_TransmitEnable

#define UART1_TransmitEnable UART1_TransmitEnable

UART1_TxCollisionCallbackRegister

#define UART1_TxCollisionCallbackRegister (NULL)

UART1_TxCompleteCallbackRegister

#define UART1_TxCompleteCallbackRegister UART1_TxCompleteCallbackRegister

UART1_Write

#define UART1_Write UART1_Write

3.26.1.7.3 source/uart_drv_interface.h File Reference

#include <stdbool.h>
#include <stdint.h>
#include "uart_types.h"

Data structures

Detailed Description

UART Generated Driver Interface Header File

3.26.1.7.4 source/uart_types.h File Reference

#include <stdbool.h>
#include <stdint.h>

Detailed Description

UART Generated Driver Interface Header File

Enumeration Type Documentation

UART_STANDARD_BAUDS

enum UART_STANDARD_BAUDS

This Enum can be used to set UART standard baud-rates using UARTx_BRGSet function e.g. UART1_BRGSet.

Section: Included Files Section: Data Type Definitions

UART_110
UART_300
UART_600
UART_1200
UART_2400
UART_4800
UART_9600
UART_14400
UART_19200
UART_38400
UART_57600
UART_115200
UART_230400
UART_460800
UART_921600