PSoC 6 Hardware Abstraction Layer (HAL)
UART (Universal Asynchronous Receiver-Transmitter)

General Description

High level interface for interacting with the Cypress UART.

The Universal Asynchronous Receiver/Transmitter (UART) protocol is an asynchronous serial interface protocol. UART communication is typically point-to-point. The UART interface consists of two signals:

Additionally, two side-band signals are used to implement flow control in UART. Note that the flow control applies only to TX functionality.

Flow control can be configured via cyhal_uart_set_flow_control()

The data frame size, STOP bits, and parity can be configured via cyhal_uart_cfg_t. The UART contains dedicated hardware buffers for transmit and receive. Optionally, either these can be augmented with a software buffer.

Data Structures

struct  cyhal_uart_cfg_t
 Initial UART configuration. More...
 

Macros

#define CYHAL_UART_RSLT_ERR_INVALID_PIN   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_UART, 0))
 
#define CYHAL_UART_RSLT_ERR_PM_CALLBACK   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_UART, 1))
 
#define CY_RSLT_ERR_CSP_UART_GETC_TIMEOUT   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_UART, 2))
 
#define CY_RSLT_WRN_CSP_UART_BAUD_TOLERANCE   (CY_RSLT_CREATE(CY_RSLT_TYPE_WARNING, CYHAL_RSLT_MODULE_UART, 3))
 
#define CYHAL_UART_DEFAULT_BAUD   115200
 
#define CYHAL_UART_MAX_BAUD_PERCENT_DIFFERENCE   10
 

Typedefs

typedef void(* cyhal_uart_event_callback_t) (void *callback_arg, cyhal_uart_event_t event)
 

Enumerations

enum  cyhal_uart_parity_t { CYHAL_UART_PARITY_NONE, CYHAL_UART_PARITY_EVEN, CYHAL_UART_PARITY_ODD }
 
enum  cyhal_uart_event_t {
  CYHAL_UART_IRQ_NONE = 0, CYHAL_UART_IRQ_TX_TRANSMIT_IN_FIFO = 1 << 1, CYHAL_UART_IRQ_TX_DONE = 1 << 2, CYHAL_UART_IRQ_TX_ERROR = 1 << 3,
  CYHAL_UART_IRQ_RX_FULL = 1 << 4, CYHAL_UART_IRQ_RX_DONE = 1 << 5, CYHAL_UART_IRQ_RX_ERROR = 1 << 6, CYHAL_UART_IRQ_RX_NOT_EMPTY = 1 << 7,
  CYHAL_UART_IRQ_TX_EMPTY = 1 << 8
}
 

Functions

cy_rslt_t cyhal_uart_init (cyhal_uart_t *obj, cyhal_gpio_t tx, cyhal_gpio_t rx, const cyhal_clock_divider_t *clk, const cyhal_uart_cfg_t *cfg)
 
void cyhal_uart_free (cyhal_uart_t *obj)
 
cy_rslt_t cyhal_uart_set_baud (cyhal_uart_t *obj, uint32_t baudrate, uint32_t *actualbaud)
 
cy_rslt_t cyhal_uart_configure (cyhal_uart_t *obj, const cyhal_uart_cfg_t *cfg)
 
cy_rslt_t cyhal_uart_getc (cyhal_uart_t *obj, uint8_t *value, uint32_t timeout)
 
cy_rslt_t cyhal_uart_putc (cyhal_uart_t *obj, uint32_t value)
 
uint32_t cyhal_uart_readable (cyhal_uart_t *obj)
 
uint32_t cyhal_uart_writable (cyhal_uart_t *obj)
 
cy_rslt_t cyhal_uart_clear (cyhal_uart_t *obj)
 
cy_rslt_t cyhal_uart_set_flow_control (cyhal_uart_t *obj, cyhal_gpio_t cts, cyhal_gpio_t rts)
 
cy_rslt_t cyhal_uart_write (cyhal_uart_t *obj, void *tx, size_t *tx_length)
 
cy_rslt_t cyhal_uart_read (cyhal_uart_t *obj, void *rx, size_t *rx_length)
 
cy_rslt_t cyhal_uart_write_async (cyhal_uart_t *obj, void *tx, size_t length)
 
cy_rslt_t cyhal_uart_read_async (cyhal_uart_t *obj, void *rx, size_t length)
 
bool cyhal_uart_is_tx_active (cyhal_uart_t *obj)
 
bool cyhal_uart_is_rx_active (cyhal_uart_t *obj)
 
cy_rslt_t cyhal_uart_write_abort (cyhal_uart_t *obj)
 
cy_rslt_t cyhal_uart_read_abort (cyhal_uart_t *obj)
 
void cyhal_uart_register_callback (cyhal_uart_t *obj, cyhal_uart_event_callback_t callback, void *callback_arg)
 
void cyhal_uart_enable_event (cyhal_uart_t *obj, cyhal_uart_event_t event, uint8_t intrPriority, bool enable)
 

Data Structure Documentation

◆ cyhal_uart_cfg_t

struct cyhal_uart_cfg_t
Data Fields
uint32_t data_bits The number of start bits.
uint32_t stop_bits The number of stop bits.
cyhal_uart_parity_t parity The parity.
uint8_t * rx_buffer The rx software buffer pointer, if NULL, no rx software buffer will be used.
uint32_t rx_buffer_size The number of bytes in the rx software buffer.

Macro Definition Documentation

◆ CYHAL_UART_RSLT_ERR_INVALID_PIN

#define CYHAL_UART_RSLT_ERR_INVALID_PIN   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_UART, 0))

The requested resource type is invalid

◆ CYHAL_UART_RSLT_ERR_PM_CALLBACK

#define CYHAL_UART_RSLT_ERR_PM_CALLBACK   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_UART, 1))

Failed to configure power management callback

◆ CY_RSLT_ERR_CSP_UART_GETC_TIMEOUT

#define CY_RSLT_ERR_CSP_UART_GETC_TIMEOUT   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_UART, 2))

The getc call timed out with no received data

◆ CY_RSLT_WRN_CSP_UART_BAUD_TOLERANCE

#define CY_RSLT_WRN_CSP_UART_BAUD_TOLERANCE   (CY_RSLT_CREATE(CY_RSLT_TYPE_WARNING, CYHAL_RSLT_MODULE_UART, 3))

The actual baud rate is greater than 10% off the requested baud rate

◆ CYHAL_UART_DEFAULT_BAUD

#define CYHAL_UART_DEFAULT_BAUD   115200

The baud rate to set to if no clock is specified in the init function

◆ CYHAL_UART_MAX_BAUD_PERCENT_DIFFERENCE

#define CYHAL_UART_MAX_BAUD_PERCENT_DIFFERENCE   10

The maximum allowable difference between baud requested and actual baud

Typedef Documentation

◆ cyhal_uart_event_callback_t

typedef void(* cyhal_uart_event_callback_t) (void *callback_arg, cyhal_uart_event_t event)

UART callback function type

Enumeration Type Documentation

◆ cyhal_uart_parity_t

UART Parity

Enumerator
CYHAL_UART_PARITY_NONE 

UART has no parity check

CYHAL_UART_PARITY_EVEN 

UART has even parity check

CYHAL_UART_PARITY_ODD 

UART has odd parity check

◆ cyhal_uart_event_t

Enum to enable/disable/report interrupt cause flags.

Enumerator
CYHAL_UART_IRQ_NONE 

No interrupt.

CYHAL_UART_IRQ_TX_TRANSMIT_IN_FIFO 

All tx data from transmit has been moved to uart FIFO.

CYHAL_UART_IRQ_TX_DONE 

All tx data has been transmitted.

CYHAL_UART_IRQ_TX_ERROR 

An error occurred in tx.

CYHAL_UART_IRQ_RX_FULL 

The rx software buffer is full, additional data are store into fifo buffer.

CYHAL_UART_IRQ_RX_DONE 

All rx data has been received.

CYHAL_UART_IRQ_RX_ERROR 

An error occurred in rx.

CYHAL_UART_IRQ_RX_NOT_EMPTY 

The rx hardware buffer is not empty.

CYHAL_UART_IRQ_TX_EMPTY 

The tx hardware buffer is empty.

Function Documentation

◆ cyhal_uart_init()

cy_rslt_t cyhal_uart_init ( cyhal_uart_t *  obj,
cyhal_gpio_t  tx,
cyhal_gpio_t  rx,
const cyhal_clock_divider_t *  clk,
const cyhal_uart_cfg_t *  cfg 
)

Initialize the uart peripheral. It sets the default parameters for uart peripheral, and configures its specifieds pins.

Parameters
[out]objThe uart object
[in]txThe TX pin name, if no TX pin use NC
[in]rxThe RX pin name, if no RX pin use NC
[in]clkThe clock to use can be shared, if not provided a new clock will be, allocated and the default baud rate set
[in]cfgThe uart configuration data for data bits, stop bits and parity, if not provided, default values of (8, 1, none) will be used
Returns
The status of the init request

◆ cyhal_uart_free()

void cyhal_uart_free ( cyhal_uart_t *  obj)

Release the uart peripheral, not currently invoked. It requires further resource management.

Parameters
[in,out]objThe uart object

◆ cyhal_uart_set_baud()

cy_rslt_t cyhal_uart_set_baud ( cyhal_uart_t *  obj,
uint32_t  baudrate,
uint32_t *  actualbaud 
)

Configure the baud rate

Parameters
[in,out]objThe uart object
[in]baudrateThe baud rate to be configured
[out]actualbaudThe actual baud rate achieved by the HAL Specify NULL if you do not want this information.
Returns
The status of the set_baud request

◆ cyhal_uart_configure()

cy_rslt_t cyhal_uart_configure ( cyhal_uart_t *  obj,
const cyhal_uart_cfg_t *  cfg 
)

Configure the data bits, stop bits, and parity

Parameters
[in,out]objThe uart object
[in]cfgThe uart configuration data for data bits, stop bits and parity. rx_buffer and rx_buffer_size are ignored.
Returns
The status of the configure request

◆ cyhal_uart_getc()

cy_rslt_t cyhal_uart_getc ( cyhal_uart_t *  obj,
uint8_t *  value,
uint32_t  timeout 
)

Get character. This is a blocking call, waiting for a character

Parameters
[in]objThe uart object
[out]valueThe value read from the serial port
[in]timeoutThe time in ms to spend attempting to receive from serial port timeout = zero is wait forever
Returns
The status of the getc request

◆ cyhal_uart_putc()

cy_rslt_t cyhal_uart_putc ( cyhal_uart_t *  obj,
uint32_t  value 
)

Send a character. This is a blocking call, waiting for the peripheral to be available for writing

Parameters
[in]objThe uart object
[in]valueThe character to be sent
Returns
The status of the putc request

◆ cyhal_uart_readable()

uint32_t cyhal_uart_readable ( cyhal_uart_t *  obj)

Check the number of bytes avaialable to read from the receive buffers

Parameters
[in]objThe uart object
Returns
The number of readable bytes

◆ cyhal_uart_writable()

uint32_t cyhal_uart_writable ( cyhal_uart_t *  obj)

Check the number of bytes than can be written to the transmit buffer

Parameters
[in]objThe uart object
Returns
The number of bytes that can be written

◆ cyhal_uart_clear()

cy_rslt_t cyhal_uart_clear ( cyhal_uart_t *  obj)

Clear the uart peripheral buffers

Parameters
[in]objThe uart object
Returns
The status of the clear request

◆ cyhal_uart_set_flow_control()

cy_rslt_t cyhal_uart_set_flow_control ( cyhal_uart_t *  obj,
cyhal_gpio_t  cts,
cyhal_gpio_t  rts 
)

Configure the uart for the flow control. It sets flow control in the hardware if a uart peripheral supports it, otherwise software emulation is used.

Parameters
[in,out]objThe uart object
[in]ctsThe TX pin name
[in]rtsThe RX pin name
Returns
The status of the init_flow_control request

◆ cyhal_uart_write()

cy_rslt_t cyhal_uart_write ( cyhal_uart_t *  obj,
void *  tx,
size_t *  tx_length 
)

Begin synchronous TX transfer. The used buffer is specified in the uart object, tx_buff

Parameters
[in]objThe uart object
[in]txThe transmit buffer
[in,out]tx_length[in] The number of bytes to transmit, [out] number actually transmitted
Returns
The status of the tx request

◆ cyhal_uart_read()

cy_rslt_t cyhal_uart_read ( cyhal_uart_t *  obj,
void *  rx,
size_t *  rx_length 
)

Begin synchronous RX transfer (enable interrupt for data collecting) The used buffer is specified in the uart object - rx_buff

Parameters
[in]objThe uart object
[in]rxThe receive buffer
[in,out]rx_length[in] The number of bytes to receive, [out] number actually received
Returns
The status of the rx request

◆ cyhal_uart_write_async()

cy_rslt_t cyhal_uart_write_async ( cyhal_uart_t *  obj,
void *  tx,
size_t  length 
)

Begin asynchronous TX transfer. The transmit buffer is a user defined buffer that will be sent on the uart. The user must register a callback with cyhal_uart_irq_register_callback. If desired, TX callback events can be enabled using cyhal_uart_enable_event with the appropriate events.

Parameters
[in]objThe uart object
[in]txThe transmit buffer
[in]lengthThe number of bytes to transmit
Returns
The status of the tx_async request

◆ cyhal_uart_read_async()

cy_rslt_t cyhal_uart_read_async ( cyhal_uart_t *  obj,
void *  rx,
size_t  length 
)

Begin asynchronous RX transfer. Recevied data is placed in the user specified buffer. The user must register a callback with cyhal_uart_irq_register_callback. RX callback events can be enabled using cyhal_uart_enable_event with the appropriate events.

Parameters
[in]objThe uart object
[out]rxThe user specified receive buffer
[in]lengthThe number of bytes to receive
Returns
The status of the rx_async request

◆ cyhal_uart_is_tx_active()

bool cyhal_uart_is_tx_active ( cyhal_uart_t *  obj)

Attempts to determine if the uart peripheral is already in use for TX

Parameters
[in]objThe uart object
Returns
Is the TX channel active

◆ cyhal_uart_is_rx_active()

bool cyhal_uart_is_rx_active ( cyhal_uart_t *  obj)

Attempts to determine if the uart peripheral is already in use for RX

Parameters
[in]objThe uart object
Returns
Is the RX channel active

◆ cyhal_uart_write_abort()

cy_rslt_t cyhal_uart_write_abort ( cyhal_uart_t *  obj)

Abort the ongoing TX transaction. It disables the enabled interupt for TX and flushes the TX hardware buffer if TX FIFO is used

Parameters
[in]objThe uart object
Returns
The status of the tx_abort request

◆ cyhal_uart_read_abort()

cy_rslt_t cyhal_uart_read_abort ( cyhal_uart_t *  obj)

Abort the ongoing read transaction. It disables the enabled interrupt for RX and flushes the RX hardware buffer if RX FIFO is used

Parameters
[in]objThe uart object
Returns
The status of the read_abort request

◆ cyhal_uart_register_callback()

void cyhal_uart_register_callback ( cyhal_uart_t *  obj,
cyhal_uart_event_callback_t  callback,
void *  callback_arg 
)

The uart callback handler registration

Parameters
[in]objThe uart object
[in]callbackThe callback handler which will be invoked when the interrupt fires
[in]callback_argGeneric argument that will be provided to the callback when called

◆ cyhal_uart_enable_event()

void cyhal_uart_enable_event ( cyhal_uart_t *  obj,
cyhal_uart_event_t  event,
uint8_t  intrPriority,
bool  enable 
)

Configure uart interrupt. This function is used for word-approach

Parameters
[in]objThe uart object
[in]eventThe uart event type, this argument supports the bitwise-or of multiple enum flag values
[in]intrPriorityThe priority for NVIC interrupt events
[in]enableTrue to turn on interrupts, False to turn off