UARTDRV - UART Driver#

Universal Asynchronous Receiver/Transmitter Driver.

The source files for the UART driver library, uartdrv.c and uartdrv.h, are in the emdrv/uartdrv folder.

Introduction#

The UART driver supports the UART capabilities of the USART, UART, and LEUART peripherals. The driver is fully reentrant and supports multiple driver instances. The driver does not buffer or queue data. However, it queues UART transmit and receive operations. Both blocking and non-blocking transfer functions are available. Non-blocking transfer functions report transfer completion with callback functions. Transfers are done using DMA. Simple direct/forced transmit and receive functions are also available. Note that these functions are blocking and not suitable for low energy applications because they use CPU polling.

UART hardware flow control (CTS/RTS) is fully supported by the driver. UART software flow control (XON/XOFF) is partially supported by the driver. For more information about flow control support, see Flow Control Support.

Note

  • Transfer completion callback functions are called from within the DMA interrupt handler with interrupts disabled.

Configuration Options#

Some properties of the UARTDRV driver are compile-time configurable. These properties are set in a uartdrv_config.h file. A template for this file, containing default values, is in the emdrv/config folder. To configure UARTDRV for your application, provide a custom configuration file, or override the defines on the compiler command line. These are the available configuration parameters with default values defined.

  // Set to 1 to enable hardware flow control.
#define EMDRV_UARTDRV_FLOW_CONTROL_ENABLE       1

  // Maximum number of driver instances.
#define EMDRV_UARTDRV_MAX_DRIVER_INSTANCES      4

  // UART software flow control code: request peer to start Tx.
#define UARTDRV_FC_SW_XON                       0x11

  // UART software flow control code: request peer to stop Tx.
#define UARTDRV_FC_SW_XOFF                      0x13

The properties of each UART driver instance are set at run-time via the UARTDRV_InitUart_t data structure input parameter to the UARTDRV_InitUart() function for UART and USART peripherals, and the UARTDRV_InitLeuart_t data structure input parameter to the UARTDRV_InitLeuart() function for LEUART peripherals.

The API#

This section contains brief descriptions of the functions in the API. For more information on input and output parameters and return values, click on the hyperlinked function names. Most functions return an error code, ECODE_EMDRV_UARTDRV_OK is returned on success, see ecode.h and uartdrv.h for other error codes.

The application code must include uartdrv.h header file.

UARTDRV_InitUart(), UARTDRV_InitLeuart() and UARTDRV_DeInit() These functions initialize and deinitialize the UARTDRV driver. Typically, UARTDRV_InitUart() (for UART/USART) or UARTDRV_InitLeuart() (for LEUART) are called once in the startup code.

UARTDRV_GetReceiveStatus() and UARTDRV_GetTransmitStatus() Query the status of a current transmit or receive operations. Reports number of items (frames) transmitted and remaining.

UARTDRV_GetReceiveDepth() and UARTDRV_GetTransmitDepth() Get the number of queued receive or transmit operations.

UARTDRV_Transmit(), UARTDRV_Receive()UARTDRV_TransmitB(), UARTDRV_ReceiveB()UARTDRV_ForceTransmit() and UARTDRV_ForceReceive() Blocking and non-blocking transfer functions are included. The blocking versions have an uppercase B (for Blocking) at the end of their function name. Blocking functions do not return before the transfer is complete. The non-blocking functions signal a transfer completion with a callback function. UARTDRV_ForceTransmit() and UARTDRV_ForceReceive() are also blocking. These two functions access the UART peripheral directly without using DMA or interrupts. UARTDRV_ForceTransmit() does not respect flow control. UARTDRV_ForceReceive() forces RTS low.

UARTDRV_Abort() Abort current transmit or receive operations and remove all queued operations.

UARTDRV_FlowControlSet(), UARTDRV_FlowControlGetSelfStatus(), UARTDRV_FlowControlSetPeerStatus() and UARTDRV_FlowControlGetPeerStatus() Set and get flow control status of self or peer device. Note that the return value from these two functions depends on the flow control mode set by UARTDRV_FlowControlSet(), UARTDRV_InitUart(), or UARTDRV_InitLeuart().

UARTDRV_FlowControlIgnoreRestrain() Enables transmission when restrained by flow control.

UARTDRV_PauseTransmit() and UARTDRV_ResumeTransmit() Pause a currently active transmit operation by preventing the DMA from loading the UART FIFO. Will not override HW flow control state (if applicable), but can be used in conjunction.

Flow Control Support#

If UART flow control is not required, make sure that EMDRV_UARTDRV_FLOW_CONTROL_ENABLE is set to 0. This reduces the code size and complexity of the driver.

Both hardware and software flow control are supported. To enable either of these, set EMDRV_UARTDRV_FLOW_CONTROL_ENABLE to 1 in uartdrv_config.h.

Hardware Flow Control#

UART hardware flow control uses two additional pins for flow control handshaking, the clear-to-send (CTS) and ready-to-send (RTS) pins. RTS is an output and CTS is an input. These are active-low signals. When CTS is high, the UART transmitter should stop sending frames. A receiver should set RTS high when it is no longer capable of receiving data.

  • Peripheral Hardware Flow Control#

Newer devices natively support CTS/RTS in the USART peripheral hardware. To enable hardware flow control, perform the following steps:

  • Set EMDRV_UARTDRV_FLOW_CONTROL_ENABLE to 1.

  • In the UARTDRV_InitUart_t struct passed to UARTDRV_InitUart(), set UARTDRV_InitUart_t.fcType = uartdrvFlowControlHwUart.

  • Define the pins for CTS and RTS by setting ctsPort, ctsPin, rtsPort and rtsPin in the init struct.

  • Also define the CTS and RTS locations by setting portLocationCts and portLocationRts in the init struct.

  • GPIO Hardware Flow Control#

To support hardware flow control on devices that don't have UART CTS/RTS hardware support, the driver includes the GPIOINT driver to emulate a hardware implementation of UART CTS/RTS flow control on these devices.

To enable hardware flow control, perform the following steps:

  • Set EMDRV_UARTDRV_FLOW_CONTROL_ENABLE to 1.

  • UART/USART: In the UARTDRV_InitUart_t struct passed to UARTDRV_InitUart(), set UARTDRV_InitUart_t.fcType = uartdrvFlowControlHw.

  • LEUART: In the UARTDRV_InitLeuart_t struct passed to UARTDRV_InitLeuart(), set UARTDRV_InitLeuart_t::fcType = uartdrvFlowControlHw.

  • Define the pins for CTS and RTS by setting ctsPort, ctsPin, rtsPort and rtsPin in the same init struct.

Note

  • Because of the limitations in GPIO interrupt hardware, you cannot select CTS pins in multiple driver instances with the same pin number. For example, pin A0 and B0 cannot serve as CTS pins in two concurrent driver instances.

RTS is set high whenever there are no Rx operations queued. The UART transmitter is halted when the CTS pin goes high. The transmitter completes the current frame before halting. DMA transfers are also halted.

Software Flow Control#

UART software flow control uses in-band signaling, meaning the receiver sends special flow control characters to the transmitter and thereby removes the need for dedicated wires for flow control. The two symbols UARTDRV_FC_SW_XON and UARTDRV_FC_SW_XOFF are defined in uartdrv_config.h.

To enable support for software flow control, perform the following steps:

  • Set EMDRV_UARTDRV_FLOW_CONTROL_ENABLE to 1.

  • UART/USART: In the UARTDRV_InitUart_t structure passed to UARTDRV_InitUart(), set UARTDRV_InitUart_t.fcType = uartdrvFlowControlSw.

  • LEUART: In the UARTDRV_InitLeuart_t structure passed to UARTDRV_InitLeuart(), set UARTDRV_InitLeuart_t::fcType = uartdrvFlowControlSw.

Note

  • Software flow control is partial only.

The application must monitor buffers and make decisions on when to send XON/ XOFF. XON/XOFF can be sent to the peer using UARTDRV_FlowControlSet(). Though UARTDRV_FlowControlSet() will pause the active transmit operation to send a flow control character, there is no way to guarantee the order. If the application implements a specific packet format where the flow control codes may appear only in fixed positions, the application should not use UARTDRV_FlowControlSet() but implement read and write of XON/XOFF into packet buffers. If the application code fully implements all the flow control logic, EMDRV_UARTDRV_FLOW_CONTROL_ENABLE should be set to 0 to reduce code space.

Example#

Modules#

UARTDRV_Buffer_t

UARTDRV_Buffer_FifoQueue_t

UARTDRV_InitLeuart_t

UARTDRV_InitEuart_t

UARTDRV_HandleData_t

Error Codes

Status Codes

Enumerations#

enum
uartdrvFlowControlNone = 0
uartdrvFlowControlSw = 1
uartdrvFlowControlHw = 2
uartdrvFlowControlHwUart = 3
}

UARTDRV Flow Control method.

enum
uartdrvFlowControlOn = 0
uartdrvFlowControlOff = 1
uartdrvFlowControlAuto = 2
}

Flow Control state.

enum
uartdrvAbortTransmit = 1
uartdrvAbortReceive = 2
uartdrvAbortAll = 3
}

Transfer abort type.

Typedefs#

typedef uint32_t

A UART transfer count.

typedef uint32_t

A UART status return type. Bitfield of UARTDRV_STATUS_* values.

typedef void(*
UARTDRV_Callback_t)(struct UARTDRV_HandleData *handle, Ecode_t transferStatus, uint8_t *data, UARTDRV_Count_t transferCount)

UARTDRV transfer completion callback function.

Handle pointer.

Functions#

UARTDRV_InitLeuart(UARTDRV_Handle_t handle, const UARTDRV_InitLeuart_t *initData)
UARTDRV_InitEuart(UARTDRV_Handle_t handle, const UARTDRV_InitEuart_t *initData)

Initialize a EUART driver instance.

UARTDRV_DeInit(UARTDRV_Handle_t handle)

Deinitialize a UART driver instance.

UARTDRV_GetPeripheralStatus(UARTDRV_Handle_t handle)

Return the status of the UART peripheral associated with a given handle.

UARTDRV_GetReceiveStatus(UARTDRV_Handle_t handle, uint8_t **buffer, UARTDRV_Count_t *bytesReceived, UARTDRV_Count_t *bytesRemaining)

Check the status of the UART and gather information about any ongoing receive operations.

UARTDRV_GetTransmitStatus(UARTDRV_Handle_t handle, uint8_t **buffer, UARTDRV_Count_t *bytesSent, UARTDRV_Count_t *bytesRemaining)

Check the status of the UART and gather information about any ongoing transmit operations.

uint8_t
UARTDRV_GetReceiveDepth(UARTDRV_Handle_t handle)

Return the number of queued receive operations.

uint8_t
UARTDRV_GetTransmitDepth(UARTDRV_Handle_t handle)

Returns the number of queued transmit operations.

UARTDRV_Transmit(UARTDRV_Handle_t handle, uint8_t *data, UARTDRV_Count_t count, UARTDRV_Callback_t callback)

Start a non-blocking transmit.

UARTDRV_Receive(UARTDRV_Handle_t handle, uint8_t *data, UARTDRV_Count_t count, UARTDRV_Callback_t callback)

Start a non-blocking receive.

UARTDRV_TransmitB(UARTDRV_Handle_t handle, uint8_t *data, UARTDRV_Count_t count)

Start a blocking transmit.

UARTDRV_ReceiveB(UARTDRV_Handle_t handle, uint8_t *data, UARTDRV_Count_t count)

Start a blocking receive.

UARTDRV_ForceTransmit(UARTDRV_Handle_t handle, uint8_t *data, UARTDRV_Count_t count)

Direct transmit without interrupts or callback.

UARTDRV_ForceReceive(UARTDRV_Handle_t handle, uint8_t *data, UARTDRV_Count_t maxLength)

Direct receive without interrupts or callback.

UARTDRV_Abort(UARTDRV_Handle_t handle, UARTDRV_AbortType_t type)

Abort ongoing UART transfers.

UARTDRV_PauseTransmit(UARTDRV_Handle_t handle)

Pause an ongoing transmit operation.

UARTDRV_ResumeTransmit(UARTDRV_Handle_t handle)

Resume a paused transmit operation.

UARTDRV_FlowControlGetSelfStatus(UARTDRV_Handle_t handle)

Check the self flow control status.

UARTDRV_FlowControlGetPeerStatus(UARTDRV_Handle_t handle)

Check the peer's flow control status.

UARTDRV_FlowControlSet(UARTDRV_Handle_t handle, UARTDRV_FlowControlState_t state)

Set UART flow control state.

UARTDRV_FlowControlSetPeerStatus(UARTDRV_Handle_t handle, UARTDRV_FlowControlState_t state)

Set peer UART flow control state.

UARTDRV_FlowControlIgnoreRestrain(UARTDRV_Handle_t handle)

Enable transmission when restrained by flow control.

Macros#

#define
DEFINE_BUF_QUEUE (qSize, qName)

Macros to define FIFO and buffer queues.

Enumeration Documentation#

UARTDRV_FlowControlType_t#

UARTDRV_FlowControlType_t

UARTDRV Flow Control method.

Enumerator
uartdrvFlowControlNone

None.

uartdrvFlowControlSw

Software XON/XOFF.

uartdrvFlowControlHw

nRTS/nCTS hardware handshake

uartdrvFlowControlHwUart

UART peripheral controls nRTS/nCTS.


UARTDRV_FlowControlState_t#

UARTDRV_FlowControlState_t

Flow Control state.

Enumerator
uartdrvFlowControlOn

XON or nRTS/nCTS low.

uartdrvFlowControlOff

XOFF or nRTS/nCTS high.

uartdrvFlowControlAuto

This driver controls the state.


UARTDRV_AbortType_t#

UARTDRV_AbortType_t

Transfer abort type.

Enumerator
uartdrvAbortTransmit

Abort current and queued transmit operations.

uartdrvAbortReceive

Abort current and queued receive operations.

uartdrvAbortAll

Abort all current and queued operations.


Typedef Documentation#

UARTDRV_Count_t#

typedef uint32_t UARTDRV_Count_t

A UART transfer count.


UARTDRV_Status_t#

typedef uint32_t UARTDRV_Status_t

A UART status return type. Bitfield of UARTDRV_STATUS_* values.


UARTDRV_Callback_t#

typedef void(* UARTDRV_Callback_t) (struct UARTDRV_HandleData *handle, Ecode_t transferStatus, uint8_t *data, UARTDRV_Count_t transferCount) )(struct UARTDRV_HandleData *handle, Ecode_t transferStatus, uint8_t *data, UARTDRV_Count_t transferCount)

UARTDRV transfer completion callback function.

Parameters
TypeDirectionArgument NameDescription
[in]handle

The UARTDRV device handle used to start the transfer.

[in]transferStatus

Completion status of the transfer operation.

[in]data

A pointer to the transfer data buffer.

[in]transferCount

A number of bytes transferred.

Called when a transfer is complete. An application should check the transferStatus and itemsTransferred values.


UARTDRV_Handle_t#

typedef UARTDRV_HandleData_t* UARTDRV_Handle_t

Handle pointer.


Function Documentation#

UARTDRV_InitLeuart#

Ecode_t UARTDRV_InitLeuart (UARTDRV_Handle_t handle, const UARTDRV_InitLeuart_t * initData)
Parameters
TypeDirectionArgument NameDescription
UARTDRV_Handle_tN/Ahandle
const UARTDRV_InitLeuart_t *N/AinitData

UARTDRV_InitEuart#

Ecode_t UARTDRV_InitEuart (UARTDRV_Handle_t handle, const UARTDRV_InitEuart_t * initData)

Initialize a EUART driver instance.

Parameters
TypeDirectionArgument NameDescription
UARTDRV_Handle_t[out]handle

Pointer to a UARTDRV handle, refer to UARTDRV_Handle_t.

const UARTDRV_InitEuart_t *[in]initData

Pointer to an initialization data structure, refer to UARTDRV_InitEuart_t.

Returns


UARTDRV_DeInit#

Ecode_t UARTDRV_DeInit (UARTDRV_Handle_t handle)

Deinitialize a UART driver instance.

Parameters
TypeDirectionArgument NameDescription
UARTDRV_Handle_t[in]handle

Pointer to a UART driver handle.

Returns


UARTDRV_GetPeripheralStatus#

UARTDRV_Status_t UARTDRV_GetPeripheralStatus (UARTDRV_Handle_t handle)

Return the status of the UART peripheral associated with a given handle.

Parameters
TypeDirectionArgument NameDescription
UARTDRV_Handle_t[in]handle

Pointer to a UART driver handle.

Returns

  • UART status value


UARTDRV_GetReceiveStatus#

UARTDRV_Status_t UARTDRV_GetReceiveStatus (UARTDRV_Handle_t handle, uint8_t ** buffer, UARTDRV_Count_t * itemsReceived, UARTDRV_Count_t * itemsRemaining)

Check the status of the UART and gather information about any ongoing receive operations.

Parameters
TypeDirectionArgument NameDescription
UARTDRV_Handle_t[in]handle

Pointer to a UART driver handle.

uint8_t **[out]buffer

Pointer to the current data buffer.

UARTDRV_Count_t *[out]itemsReceived

Current bytes received count.

UARTDRV_Count_t *[out]itemsRemaining

Current bytes remaining count.

Returns

  • UART status.


UARTDRV_GetTransmitStatus#

UARTDRV_Status_t UARTDRV_GetTransmitStatus (UARTDRV_Handle_t handle, uint8_t ** buffer, UARTDRV_Count_t * itemsSent, UARTDRV_Count_t * itemsRemaining)

Check the status of the UART and gather information about any ongoing transmit operations.

Parameters
TypeDirectionArgument NameDescription
UARTDRV_Handle_t[in]handle

Pointer to a UART driver handle.

uint8_t **[out]buffer

Pointer to the current data buffer.

UARTDRV_Count_t *[out]itemsSent

Current bytes sent count.

UARTDRV_Count_t *[out]itemsRemaining

Current bytes remaining count.

Returns

  • UART status.


UARTDRV_GetReceiveDepth#

uint8_t UARTDRV_GetReceiveDepth (UARTDRV_Handle_t handle)

Return the number of queued receive operations.

Parameters
TypeDirectionArgument NameDescription
UARTDRV_Handle_t[in]handle

Pointer to a UART driver handle.

Returns

  • The number of queued operations.


UARTDRV_GetTransmitDepth#

uint8_t UARTDRV_GetTransmitDepth (UARTDRV_Handle_t handle)

Returns the number of queued transmit operations.

Parameters
TypeDirectionArgument NameDescription
UARTDRV_Handle_t[in]handle

Pointer to a UART driver handle.

Returns

  • The number of queued operations.


UARTDRV_Transmit#

Ecode_t UARTDRV_Transmit (UARTDRV_Handle_t handle, uint8_t * data, UARTDRV_Count_t count, UARTDRV_Callback_t callback)

Start a non-blocking transmit.

Parameters
TypeDirectionArgument NameDescription
UARTDRV_Handle_t[in]handle

Pointer to a UART driver handle.

uint8_t *[in]data

A transmit data buffer.

UARTDRV_Count_t[in]count

A number of bytes to transmit.

UARTDRV_Callback_t[in]callback

A transfer completion callback.

Returns


UARTDRV_Receive#

Ecode_t UARTDRV_Receive (UARTDRV_Handle_t handle, uint8_t * data, UARTDRV_Count_t count, UARTDRV_Callback_t callback)

Start a non-blocking receive.

Parameters
TypeDirectionArgument NameDescription
UARTDRV_Handle_t[in]handle

Pointer to a UART driver handle.

uint8_t *[in]data

A receive data buffer.

UARTDRV_Count_t[in]count

A number of bytes received.

UARTDRV_Callback_t[in]callback

A transfer completion callback.

Returns


UARTDRV_TransmitB#

Ecode_t UARTDRV_TransmitB (UARTDRV_Handle_t handle, uint8_t * data, UARTDRV_Count_t count)

Start a blocking transmit.

Parameters
TypeDirectionArgument NameDescription
UARTDRV_Handle_t[in]handle

Pointer to a UART driver handle.

uint8_t *[in]data

A transmit data buffer.

UARTDRV_Count_t[in]count

A number of bytes to transmit.

Returns


UARTDRV_ReceiveB#

Ecode_t UARTDRV_ReceiveB (UARTDRV_Handle_t handle, uint8_t * data, UARTDRV_Count_t count)

Start a blocking receive.

Parameters
TypeDirectionArgument NameDescription
UARTDRV_Handle_t[in]handle

Pointer to a UART driver handle.

uint8_t *[in]data

A receive data buffer.

UARTDRV_Count_t[in]count

A number of bytes received.

Returns


UARTDRV_ForceTransmit#

Ecode_t UARTDRV_ForceTransmit (UARTDRV_Handle_t handle, uint8_t * data, UARTDRV_Count_t count)

Direct transmit without interrupts or callback.

Parameters
TypeDirectionArgument NameDescription
UARTDRV_Handle_t[in]handle

Pointer to a UART driver handle.

uint8_t *[in]data

Pointer to the buffer.

UARTDRV_Count_t[in]count

A number of bytes to transmit.

This is a blocking function. that ignores flow control if enabled.

Returns


UARTDRV_ForceReceive#

UARTDRV_Count_t UARTDRV_ForceReceive (UARTDRV_Handle_t handle, uint8_t * data, UARTDRV_Count_t maxCount)

Direct receive without interrupts or callback.

Parameters
TypeDirectionArgument NameDescription
UARTDRV_Handle_t[in]handle

Pointer to a UART driver handle.

uint8_t *[in]data

pointer to buffer.

UARTDRV_Count_t[in]maxCount

Maximum number of bytes to receive.

This is a blocking function.

Returns

  • Number of bytes received.


UARTDRV_Abort#

Ecode_t UARTDRV_Abort (UARTDRV_Handle_t handle, UARTDRV_AbortType_t type)

Abort ongoing UART transfers.

Parameters
TypeDirectionArgument NameDescription
UARTDRV_Handle_t[in]handle

Pointer to a UART driver handle.

UARTDRV_AbortType_t[in]type

Abort type – whether to abort only Tx, only Rx, or both.

All ongoing or queued operations of the given abort type will be aborted.

Returns


UARTDRV_PauseTransmit#

Ecode_t UARTDRV_PauseTransmit (UARTDRV_Handle_t handle)

Pause an ongoing transmit operation.

Parameters
TypeDirectionArgument NameDescription
UARTDRV_Handle_t[in]handle

Pointer to a UART driver handle.

Returns


UARTDRV_ResumeTransmit#

Ecode_t UARTDRV_ResumeTransmit (UARTDRV_Handle_t handle)

Resume a paused transmit operation.

Parameters
TypeDirectionArgument NameDescription
UARTDRV_Handle_t[in]handle

Pointer to a UART driver handle.

Returns


UARTDRV_FlowControlGetSelfStatus#

UARTDRV_FlowControlState_t UARTDRV_FlowControlGetSelfStatus (UARTDRV_Handle_t handle)

Check the self flow control status.

Parameters
TypeDirectionArgument NameDescription
UARTDRV_Handle_t[in]handle

Pointer to a UART driver handle.

Returns

  • Returns uartdrvFlowControlOn if requesting to send.


UARTDRV_FlowControlGetPeerStatus#

UARTDRV_FlowControlState_t UARTDRV_FlowControlGetPeerStatus (UARTDRV_Handle_t handle)

Check the peer's flow control status.

Parameters
TypeDirectionArgument NameDescription
UARTDRV_Handle_t[in]handle

Pointer to a UART driver handle.

Returns

  • Returns uartdrvFlowControlOn if clear to send.


UARTDRV_FlowControlSet#

Ecode_t UARTDRV_FlowControlSet (UARTDRV_Handle_t handle, UARTDRV_FlowControlState_t state)

Set UART flow control state.

Parameters
TypeDirectionArgument NameDescription
UARTDRV_Handle_t[in]handle

Pointer to a UART driver handle.

UARTDRV_FlowControlState_t[in]state

Flow control state.

Set nRTS pin if hardware flow control is enabled. Send XON/XOFF if software flow control is enabled.

Returns


UARTDRV_FlowControlSetPeerStatus#

Ecode_t UARTDRV_FlowControlSetPeerStatus (UARTDRV_Handle_t handle, UARTDRV_FlowControlState_t state)

Set peer UART flow control state.

Parameters
TypeDirectionArgument NameDescription
UARTDRV_Handle_t[in]handle

Pointer to a UART driver handle.

UARTDRV_FlowControlState_t[in]state

Flow control state.

Pause/resume transmit accordingly. Only for manual software flow control.

Returns


UARTDRV_FlowControlIgnoreRestrain#

Ecode_t UARTDRV_FlowControlIgnoreRestrain (UARTDRV_Handle_t handle)

Enable transmission when restrained by flow control.

Parameters
TypeDirectionArgument NameDescription
UARTDRV_Handle_t[in]handle

Pointer to a UART driver handle.

Returns