2.124 Universal Synchronous Asynchronous Receiver Transceiver (USART)

The USART peripheral library (PLIB) can be configured either in blocking (interrupt disabled), non-blocking (interrupt enabled) or ring buffer mode. It can also be configured to work as SPI in either blocking (interrupt disabled) or non-blocking (interrupt enabled) mode.

USART Mode

Blocking mode

In blocking mode, the USART peripheral interrupts are disabled and the transfer APIs block until the requested data is transferred.

Non-blocking mode

In non-blocking mode the peripheral interrupt is enabled. The transfer API initiates the transfer and returns immediately. The transfer is then completed from the peripheral interrupt. Application can either use a callback to get notified when the transfer is complete or can use the IsBusy API to check the completion status.

Ring buffer mode

In ring buffer mode, the receiver is always enabled, and the received data is saved in the internal receive ring buffer, size of which can be configured using MCC. The application can use the API calls to read the data out from the ring buffer. APIs are provided to query the number of (unread) bytes available in the receive buffer, free space in the receive buffer and size of the receive buffer. Similarly, during transmission, the application data is deep copied into the internal transmit ring buffer, size of which can be configured using MCC. This allows the use of local buffers for data transmission. APIs are provided to query the free space in the transmit buffer, number of bytes pending transmission and the size of the transmit buffer. Additionally, application can enable notifications to get notified when n bytes are available in the receive buffer or when n bytes of free space is available in the transmit buffer. The APIs allow application to set the threshold levels for notification in both receive and transmit buffers. Further, application can also choose to enable persistent notifications, whereby the application is notified until the threshold condition is met.

In all the modes, library provides API to change the baud, parity, data width and the number of stop bits at run time.

Using The Library

Blocking Mode

#define RX_BUFFER_SIZE 10
char message[] = "USART Example in blocking mode";
char receiveBuffer[RX_BUFFER_SIZE] = {0};
USART_ERROR errorStatus;
char rxData = 0;

int main ( void )
{    
    /* Initialize all modules */
    SYS_Initialize ( NULL );

    /* Transmit buffer*/
    USART1_Write(message, sizeof(message));
	
	/* Wait for a character to be received */
	while(USART1_ReceiverIsReady() == false);
		
	if(USART1_ErrorGet() == USART_ERROR_NONE)
	{
		/* Read a byte */
		rxData = USART1_ReadByte();
	}
	
	/* Receive buffer */
    if (USART1_Read(receiveBuffer, RX_BUFFER_SIZE)) == false)
	{
		/* Read failed, get the error */
		errorStatus USART1_ErrorGet();
		
		/* Handle the error */
	}
    else
	{
		/* Transmit the received buffer*/
		USART1_Write(receiveBuffer, RX_BUFFER_SIZE);
	}

	...

    /* Execution should not come here during normal operation */

    return ( EXIT_FAILURE );
}

Non-blocking Mode

#define RX_BUFFER_SIZE 10

char message[] = "**** Non-blocking Transfer with the interrupt  ****\r\n\
**** Type 10 characters. The received characters are echoed back ****\r\n";

char messageError[] = "**** USART error occurred ****\r\n";

char receiveBuffer[RX_BUFFER_SIZE] = {0};
char echoBuffer[RX_BUFFER_SIZE+4] = {0};

bool errorStatus = false;
bool writeStatus = false;
bool readStatus = false;

void APP_WriteCallback(uintptr_t context)
{
    writeStatus = true;
}

void APP_ReadCallback(uintptr_t context)
{
    if(USART1_ErrorGet() != USART_ERROR_NONE)
    {
        /* ErrorGet clears errors, set error flag to notify console */
        errorStatus = true;
    }
    else
    {
        readStatus = true;
    }
}

int main ( void )
{
    /* Initialize all modules */
    SYS_Initialize ( NULL );

    /* Register callback functions and send start message */
    USART1_WriteCallbackRegister(APP_WriteCallback, 0);
    USART1_ReadCallbackRegister(APP_ReadCallback, 0);
    USART1_Write(&message[0], sizeof(message));

    while ( true )
    {
        if(errorStatus == true)
        {
            /* Send error message to console */
            errorStatus = false;
            USART1_Write(&messageError[0], sizeof(messageError));
        }
        else if(readStatus == true)
        {
            /* Echo back received buffer and Toggle LED */
            readStatus = false;

            echoBuffer[0] = '\n';
            echoBuffer[1] = '\r';
            memcpy(&echoBuffer[2], receiveBuffer,sizeof(receiveBuffer));
            echoBuffer[RX_BUFFER_SIZE+2] = '\n';
            echoBuffer[RX_BUFFER_SIZE+3] = '\r';

            USART1_Write(&echoBuffer[0], sizeof(echoBuffer));
        }
        else if(writeStatus == true)
        {
            /* Submit buffer to read user data */
            writeStatus = false;
            USART1_Read(&receiveBuffer[0], sizeof(receiveBuffer));
        }
        else
        {
            /* Repeat the loop */
            ;
        }
    }

    /* Execution should not come here during normal operation */

    return ( EXIT_FAILURE );
}

Ring buffer Mode

uint8_t txBuffer[50];
uint8_t rxBuffer[10];
volatile uint32_t nBytesRead = 0;
volatile bool txThresholdEventReceived = false;
volatile bool rxThresholdEventReceived = false;

void USARTReadEventHandler(USART_EVENT event, uintptr_t context )
{
    uint32_t nBytesAvailable = 0;
    
    if (event == USART_EVENT_READ_THRESHOLD_REACHED)
    {
        /* Receiver should atleast have the thershold number of bytes in the ring buffer */
        nBytesAvailable = USART1_ReadCountGet();
        
        nBytesRead += USART1_Read((uint8_t*)&rxBuffer[nBytesRead], nBytesAvailable);                          
    }
}

void USARTWriteEventHandler(USART_EVENT event, uintptr_t context )
{
    txThresholdEventReceived = true;
}

int main ( void )
{
    uint32_t nBytes = 0;        
    
    /* Initialize all modules */
    SYS_Initialize ( NULL );          
    
    /* Register a callback for write events */
    USART1_WriteCallbackRegister(USARTWriteEventHandler, (uintptr_t) NULL);
    
    /* Register a callback for read events */
    USART1_ReadCallbackRegister(USARTReadEventHandler, (uintptr_t) NULL);              
    
    /* Print the size of the read buffer on the terminal */
    nBytes = sprintf((char*)txBuffer, "RX Buffer Size = %d\r\n", (int)USART1_ReadBufferSizeGet());
    
    USART1_Write((uint8_t*)txBuffer, nBytes);  
    
    /* Print the size of the write buffer on the terminal */
    nBytes = sprintf((char*)txBuffer, "TX Buffer Size = %d\r\n", (int)USART1_WriteBufferSizeGet());
    
    USART1_Write((uint8_t*)txBuffer, nBytes);    
    
    USART1_Write((uint8_t*)"Adding 10 characters to the TX buffer - ", sizeof("Adding 10 characters to the TX buffer - "));    
    
    /* Wait for all bytes to be transmitted out */
    while (USART1_WriteCountGet() != 0);    
    
    USART1_Write((uint8_t*)"0123456789", 10);           
        
    /* Print the amount of free space available in the TX buffer. This should be 10 bytes less than the configured write buffer size. */
    nBytes = sprintf((char*)txBuffer, "\r\nFree Space in Transmit Buffer = %d\r\n", (int)USART1_WriteFreeBufferCountGet());

    USART1_Write((uint8_t*)txBuffer, nBytes);    
    
    /* Let's enable notifications to get notified when the TX buffer is empty */
    USART1_WriteThresholdSet(USART1_WriteBufferSizeGet());   
    
    /* Enable notifications */
    USART1_WriteNotificationEnable(true, false);
   
    /* Wait for the TX buffer to become empty. Flag "txThresholdEventReceived" is set in the callback. */
    while (txThresholdEventReceived == false);
    
    txThresholdEventReceived = false;    
    
    /* Disable TX notifications */
    USART1_WriteNotificationEnable(false, false);
    
    USART1_Write((uint8_t*)"Enter 10 characters. The received characters are echoed back. \r\n>", sizeof("Enter 10 characters. The received characters are echoed back. \r\n>"));               
            
    /* Wait till 10 (or more) characters are received */
    while (USART1_ReadCountGet() < 10);
    
    /* At-least 10 characters are available in the RX buffer. Read out into the application buffer */
    USART1_Read((uint8_t*)rxBuffer, 10);  
    
    /* Echo the received data */
    USART1_Write((uint8_t*)rxBuffer, 10);    
    
    /* Now demonstrating receiver notifications */
    USART1_Write((uint8_t*)"\r\n Now turning on RX notifications \r\n>", sizeof("\r\n Now turning on RX notifications \r\n>"));
    
    /* For demonstration purpose, set a threshold value to receive a callback after every 5 characters are received */
    USART1_ReadThresholdSet(5);
    
    /* Enable RX event notifications */
    USART1_ReadNotificationEnable(true, false);
                   
    while(1)
    {
        /* Wait until at-least 10 characters are entered by the user */
        while (nBytesRead < 10);    
    
        /* Echo the received data */
        USART1_Write((uint8_t*)rxBuffer, nBytesRead);
        
        USART1_Write((uint8_t*)"\r\n>", 3);

        nBytesRead = 0;
    }

    /* Execution should not come here during normal operation */

    return ( EXIT_FAILURE );
}

SPI Mode

The SPI PLIB supports master mode

SPI master mode

In SPI master mode, the PLIB can be configured to run in blocking mode or non-blocking mode. In blocking mode the peripheral interrupt is disabled and the transfer API blocks until the transfer is complete. In non-blocking mode, the peripheral interrupt is enabled. The transfer API initiates the transfer and returns immediately. The transfer is completed from the peripheral interrupt. Application can either use a callback to get notified when the transfer is complete or can use the IsBusy API to check the completion status.

SPI Master in blocking (peripheral interrupt disabled) mode

// Following code demonstrates SPI self loopback with the PLIB configured in blocking mode
uint8_t txData[]  = "SELF LOOPBACK FOR SPI!";
uint8_t rxData[sizeof(txData)];

int main ( void )
{
    /* Initialize all modules */
    SYS_Initialize ( NULL );
               
    /* SPI Write Read */
    USART1_SPI_WriteRead(&txData[0], sizeof(txData), &rxData[0], sizeof(rxData));

    /* Compare received data with the transmitted data */
    if ((memcmp(txData, rxData, sizeof(txData)) == 0))
    {
        /* Pass: Received data is same as transmitted data */        
    }
    else
    {       
        /* Fail: Received data is not same as transmitted data */
    }

    while ( true )
    { 	
    }

    /* Execution should not come here during normal operation */

    return ( EXIT_FAILURE );
}

SPI Master in non-blocking (peripheral interrupt enabled) mode

// Following code demonstrates SPI self loopback with the PLIB configured in non-blocking mode

uint8_t txData[]  = "SELF LOOPBACK FOR SPI!";
uint8_t rxData[sizeof(txData)];
volatile bool transferStatus = false;

/* This function will be called by SPI PLIB when transfer is completed */
void APP_SPI_Callback(uintptr_t context )
{
    transferStatus = true;
}

int main ( void )
{
    /* Initialize all modules */
    SYS_Initialize ( NULL );

    /* Register callback function   */
    USART1_SPI_CallbackRegister(APP_SPI_Callback, 0);
   
    /* SPI Write Read */
    USART1_SPI_WriteRead(&txData[0], sizeof(txData), &rxData[0], sizeof(rxData));
	
	while (1)
	{
		/* Perform other tasks here ...*/
		
		/* Check if transfer has completed */
		if(transferStatus == true)
		{
			/* Compare received data with the transmitted data */
			if(memcmp(txData, rxData, sizeof(txData)) == 0)
			{
				/* Pass: Received data is same as transmitted data */		
			}
			else
			{   
				/* Fail: Received data is not same as transmitted data */		
			}   
		}		        
	}
    
}

Library Interface

USART peripheral library provides the following interfaces:

USART Mode

Functions

NameDescriptionBlocking modeNon-blocking modeRing buffer mode
USARTx_InitializeInitializes given instance of the USART peripheralYesYesYes
USARTx_SerialSetupSets up serial configurations for USART peripheralYesYesYes
USARTx_WriteWrites data to the given USART peripheral instanceYesYesYes
USARTx_ReadReads data from the given USART peripheral instanceYesYesYes
USARTx_WriteIsBusyReturns the write request status associated with the given USART peripheral instanceNoYesNo
USARTx_ReadIsBusyReturns the read request status associated with the given USART peripheral instanceNoYesNo
USARTx_WriteCountGetGets the byte count of processed bytes for a given USART read operation in non-blocking mode. Returns the number of bytes pending to be transmitted out in the transmit buffer in ring buffer mode.NoYesYes
USARTx_ReadCountGetGets the byte count of processed bytes for a given USART read operation in non-blocking mode. Returns the number of bytes available in the internal receive buffer of the PLIB in ring buffer mode.NoYesYes
USARTx_TransmitterIsReadyReturns the hardware status of the USART TransmitterYesNoNo
USARTx_ReceiverIsReadyReturns the hardware status of the USART ReceiverYesNoNo
USARTx_ErrorGetGets the error of the given USART peripheral instanceYesYesYes
USARTx_WriteCallbackRegisterSets the pointer to the function (and it's context) to be called when the given USART's write events occurNoYesYes
USARTx_ReadCallbackRegisterSets the pointer to the function (and it's context) to be called when the given USART's read events occurNoYesYes
USARTx_ReadByteSubmits request to read a byte of data to the given USART peripheralYesNoNo
USARTx_WriteByteSubmits a byte of data to the given USART peripheral to transferYesNoNo
USARTx_ReadAbortAborts the ongoing read requestNoYesNo
USARTx_WriteFreeBufferCountGetReturns the number of bytes of free space available in the internal transmit bufferNoNoYes
USARTx_WriteBufferSizeGetReturns the size of the internal transmit ring bufferNoNoYes
USARTx_WriteNotificationEnableThis API lets the application turn the transmit notifications on/offNoNoYes
USARTx_WriteThresholdSetThis API allows the application to set a threshold level on the number of free space available in the transmit bufferNoNoYes
USARTx_ReadFreeBufferCountGetReturns the number of bytes of free space available in the internal receive bufferNoNoYes
USARTx_ReadBufferSizeGetReturns the size of the receive ring bufferNoNoYes
USARTx_ReadNotificationEnableThis API lets the application turn the receive notifications on/offNoNoYes
USARTx_ReadThresholdSetThis API allows the application to set a threshold level on the number of bytes of data available in the receive bufferNoNoYes

Data types and constants

NameTypeDescriptionBlocking modeNon-blocking modeRing buffer mode
USART_ERRORMacros and TypedefDefines the macros and typedefs associated with the USART peripheral errorsYesYesYes
USART_DATAEnumDefines the data width types for the USART peripheralYesYesYes
USART_PARITYEnumDefines the parity types for the USART peripheralYesYesYes
USART_STOPEnumDefines the data type for the USART peripheral stop bitsYesYesYes
USART_SERIAL_SETUPStructDefines the data structure which is used to configure USART serial parameters at run timeYesYesYes
USART_CALLBACKTypedefDefines the data type and function signature of the USART peripheral library callback functionYesYesNo
USART_EVENTEnumDefines the enums associated with the USART events in the ring buffer modeYesYesYes
USART_RING_BUFFER_CALLBACKTypedefDefines the data type and function signature for the USART peripheral callback function in the ring buffer modeNoNoYes

SPI Mode

Functions

NameDescriptionMaster (blocking/interrupt disabled) modeMaster (non-blocking/interrupt enabled) mode
USARTx_SPI_InitializeInitializes USART peripheral in SPI modeYesYes
USARTx_SPI_TransferSetupConfigure SPI operational parameters at run timeYesYes
USARTx_SPI_WriteReadWrite and Read data on SPI peripheralYesYes
USARTx_SPI_WriteWrites data to SPI peripheralYesYes
USARTx_SPI_ReadReads data on the SPI peripheralYesYes
USARTx_SPI_CallbackRegisterAllows application to register a callback with the PLIBNoYes
USARTx_SPI_IsBusyReturns transfer status of SPINoYes

Data types and constants

NameDescriptionMaster (blocking/interrupt disabled) modeMaster (non-blocking/interrupt enabled) mode
USART_SPI_CLOCK_PHASEEnumIdentifies SPI Clock Phase OptionsYes
USART_SPI_CLOCK_POLARITYEnumIdentifies SPI Clock Polarity OptionsYes
USART_SPI_DATA_BITSEnumIdentifies SPI bits per transferYes
USART_SPI_TRANSFER_SETUPStructData structure containing the SPI parameters which can be changed at run timeYes
USART_SPI_CALLBACKTypedefDefines the data type and function signature for the USART SPI peripheral callback functionNo

LIN Mode

Functions

NameDescriptionHost (blocking/interrupt disabled) modeHost (non-blocking/interrupt enabled) modeClient (blocking/interrupt disabled) mode Client (non-blocking/interrupt enabled) mode
USARTx_LIN_ChecksumEnable FunctionEnables hardware checksum for data transfer YesYesYesYes
USARTx_LIN_ChecksumTypeSet FunctionSet the type of checksum to be used during data transfer.YesYesYesYes
USARTx_LIN_DataLenModeSet FunctionSet Data Length ModeYesYesYesYes
USARTx_LIN_FrameSlotEnable FunctionEnable/Disable Frame Slot ModeYesYesNoNo
USARTx_LIN_IdentifierRead FunctionHelps the application to read LIN ID to corresponding hardware registerYesYesYesYes
USARTx_LIN_IdentifierWrite FunctionHelps the application to write LIN ID to corresponding hardware registerYesYesYesYes
USARTx_LIN_NodeActionSet FunctionAllows to set the Action to be performed by LIN Node (PUBLISH, SUBSCRIBE or IGNORE)YesYesYesYes
USARTx_LIN_ParityEnable FunctionAllows to Enable/Disable LIN Identifier parity modeYesYesYesYes
USARTx_LIN_ResponseDataLenSet FunctionAllowas to set the data length to be transferredYesYesYesYes
USARTx_LIN_TransferComplete FunctionAllows to check if the requested data transfer is completed or notYesYesYesYes
USARTx_LINBreakCallbackRegister FunctionAllows to register a callback function for the PLIB to call back when the Break Field of the requested LIN header transfer operation has completed.NoYesNoYes
USARTx_LINIdCallbackRegister FunctionAllows to register a callback function for the PLIB to call back when the requested LIN ID transfer operation has completed.NoYesNoYes
USARTx_LINTcCallbackRegister FunctionAllows to register a callback function for the PLIB to call back when the requested LIN data transfer operation has completedNoYesNo Yes

Data types and constants

NameDescriptionMaster (blocking/interrupt disabled) modeMaster (non-blocking/interrupt enabled) modeClient (blocking/interrupt disabled) mode Client (non-blocking/interrupt enabled) mode
USART_LIN_CALLBACK TypedefDefines the data type and function signature for the USART LIN peripheral callback function.NoYesNoYes
USART_LIN_CHECKSUM_TYPE EnumDefines the data type for the USART LIN checksum type bitsYesYesYesYes
USART_LIN_DATA_LEN EnumDefines the data type for the USART LIN Data Length Mode bitsYesYesYesYes
USART_LIN_NACT EnumDefines the data type for the USART LIN node action bitsYesYesYesYes
Note: Not all APIs maybe implemented. See the specific device family section for available APIs.