UART Drivers

The Embedded SDK includes a UART driver for the microcontrollers that contain a UART peripheral. The UART drivers are provided with a common public API so that they can be used across any of the supported platforms. The UART drivers do not preallocate any memory or other resources; therefore unused ports will not use any resources unless explicitly opened by the application.

Opening A Port

The code snippet below demonstrates how to initialize the driver and open a UART port.

#include "mcu_uart.h"
#include <assert.h>

static UART uart;                           /* Allocate an instance of the driver */

void APP_Function(void)
{
    STATUS status;


    status = UART_Open(&uart,               /* Open a UART */		 
                       0,                   /* Port number (0 in this case) */
                       115200,              /* Baud rate, in bits per second */
                       8,                   /* Number of data bits */
                       STOPBITS_ONE,        /* Number of stop bits */
                       PARITY_NONE,         /* Parity option */
                       HANDSHAKE_NONE);     /* Handshake option */

    assert(status == SUCCESS);
}

Transferring Data

The UART drivers utilize circular buffers that are provided by the kernel for maintaining data while either an interrupt or DMA can complete the actual transfers. The UART driver will automatically place received data into its underlying receive buffer. Use UART_Read() to retrieve data from the receive buffer.

The snippet below demonstrates how to receive and echo data over a UART.

void APP_Function(void)
{
    STATUS status;
    UINT32 actual;
    BYTE buf[32];


    status = UART_Read(&uart,                   /* Retrieve data from the UART */
                       buf,                     /* Pointer to a buffer to receive the data */
                       sizeof(buf),             /* The size, in bytes, of the buffer */
                       &actual,                 /* A pointer to variable to get actual amount */
                       10);                     /* Max time to wait, in kernel ticks */

    if (status == SUCCESS) {                    /* Any error occur? */
        if (actual > 0) {                       /* Any data received? */

            /* actual = the number of bytes
            that have been returned in
            the buffer 'buf' */

            status = UART_Write(&uart,          /* Write the received data out (echo) */
                                buf,            /* Pointer to the data to write */
                                actual,         /* Number of bytes to write */
                                NULL,           /* Pointer to variable for actual bytes written */
                                1000);          /* Max time to wait to complete, in kernel ticks */

            assert(status == SUCCESS);
        }
    }
}

Configuration

The size of the underlying buffers used by the UART driver can be configured within the mcu_cfg.h header file. The snippet below shows the configuration items for adjusting the UART buffer sizes.

/*******************************************************************************************************************************************

    UART RX BUFFER SIZE             The size, in bytes, of the buffer used for receiving data from a UART.

*******************************************************************************************************************************************/
#define CFG_UARTRXBUFFERSIZE        128

/*******************************************************************************************************************************************

    UART TX BUFFER SIZE             The size, in bytes, of the buffer used for transmitting data to a UART.

*******************************************************************************************************************************************/
#define CFG_UARTTXBUFFERSIZE        128

API Reference

STATUS UART_Open(UART* port, BYTE number, UINT32 baud, UINT32 nbits, STOPBITS stopbits, PARITY parity, HANDSHAKE hs)
Opens a UART port for communications.
PARAMETERS
port A pointer to a caller allocated UART port to be opened.
number The port number for the port to be opened.
baud The baud rate in bits per second for the uart.
nbits The number of bits per byte transmitted and received (5-8).
stopbits The number of stopbits to be used.
STOPBITS_ONE Use 1 stop bit.
STOPBITS_ONE5 Use 1.5 stop bits.
STOPBITS_TWO Use 2 stop bits.
parity The parity check to be used by the uart.
PARITY_NONE No parity check.
PARITY_EVEN Use even parity check.
PARITY_ODD Use odd parity check.
PARITY_MARK Use mark parity check.
PARITY_SPACE Use space parity check.
hs The handshake to be used by the uart.
HANDSHAKE_NONE No handshake.
HANDSHAKE_RTSCTS Use RTS/CTS hardware flow control.
HANDSHAKE_XONXOFF Use XON/XOFF software flow control.
RETURNS
SUCCESS The port has been initialized and is open for communications.
ERR_NULLREFERENCE The argument 'port' was found to be NULL.
ERR_INVALIDCONTEXT The operation is not supported from the context of an interrupt service routine (ISR).
ERR_NOTSUPPORTED An unsupported port number, nbits, stopbits, parity or handshake was specified.
STATUS UART_Close(UART* port)
Closes an UART port.
PARAMETERS
port A pointer to the port to be closed.
RETURNS
SUCCESS The port has been closed.
ERR_NULLREFERENCE The argument 'port' was found to be NULL.
ERR_INVALIDCONTEXT The operation is not supported from the context of an interrupt service routine (ISR).
ERR_NOTINITIALIZED The specified port has not been opened.
BOOLEAN UART_IsOpen(UART* port)
Returns an indication of whether an UART port is currently open for communications.
PARAMETERS
port A pointer to the target UART port.
RETURNS
TRUE if the UART port is currently open; otherwise FALSE.
STATUS UART_Clear(UART* port)
Clears and removes all data from the UART's receive buffer.
PARAMETERS
port A pointer to the port to be cleared.
RETURNS
SUCCESS The port's receive buffer has been cleared.
ERR_NULLREFERENCE The argument 'port' was found to be NULL.
STATUS UART_SetBreak(UART* port)
Sets or clears a break signal on a UART port.
PARAMETERS
port A pointer to the target port.
RETURNS
SUCCESS The port's break signal has been configured.
ERR_NULLREFERENCE The argument 'port' was found to be NULL.
STATUS UART_Read(UART* port, BYTE address, void* buf, UINT32 nbytes, UINT32* actual, UINT32 timeout)
Reads and returns data received from a UART port.
PARAMETERS
port A pointer to the port to receive the data.
address The target device address to be read.
buf A pointer to a caller allocated buffer used to receive the data.
nbytes The total number of bytes to be received.
actual A pointer to a caller allocated value used to determine the actual number of bytes that have been returned. Use 'NULL' to only return either all or none of the requested amount.
timeout The maximum amount of time, in kernel ticks, to block and wait for the transfer to complete. Must not be zero and must be large enough to allow the transfer to complete; based upon clock rate and amount of data being transferred. Use INFINITE to wait indefinitely.
RETURNS
SUCCESS The data was been received.
ERR_NULLREFERENCE The argument 'port' or 'buf' was found to be NULL.
ERR_INVALIDCONTEXT The operation is not supported from the context of an interrupt service routine (ISR).
ERR_NOTINITIALIZED The specified port has not been opened.
ERR_TIMEOUT The specified timeout interval has elapsed prior to the transfer being completed.
STATUS UART_Write(UART* port, BYTE address, const void* data, UINT32 nbytes, UINT32* actual, UINT32 timeout)
Writes data to be transmitted over a UART port.
PARAMETERS
port A pointer to the port to be written.
address The target device address to be written.
data A pointer to the data to be written.
nbytes The total number of bytes to be written.
actual A pointer to a caller allocated value used to determine the actual number of bytes that have been queued for transmission. Use 'NULL' to force either all or none of the data to be written.
timeout The maximum amount of time, in kernel ticks, to block and wait for the transfer to complete. Must not be zero and must be large enough to allow the transfer to complete; based upon clock rate and amount of data being transferred. Use INFINITE to wait indefinitely.
RETURNS
SUCCESS The data has been written.
ERR_NULLREFERENCE The argument 'port' or 'data' was found to be NULL.
ERR_INVALIDCONTEXT The operation is not supported from the context of an interrupt service routine (ISR).
ERR_NOTINITIALIZED The specified port has not been opened.
ERR_TIMEOUT The specified timeout interval has elapsed prior to the transfer being completed.