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:
- TX: Transmitter output
- RX: Receiver input
Additionally, two side-band signals are used to implement flow control in UART. Note that the flow control applies only to TX functionality.
- Clear to Send (CTS): This is an input signal to the transmitter. When active, it indicates that the slave is ready for the master to transmit data.
- Ready to Send (RTS): This is an output signal from the receiver. When active, it indicates that the receiver is ready to receive data
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.
|
| 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
} |
| |
|
| 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) |
| |
◆ 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. |
◆ CYHAL_UART_RSLT_ERR_INVALID_PIN
The requested resource type is invalid
◆ CYHAL_UART_RSLT_ERR_PM_CALLBACK
Failed to configure power management callback
◆ CY_RSLT_ERR_CSP_UART_GETC_TIMEOUT
The getc call timed out with no received data
◆ CY_RSLT_WRN_CSP_UART_BAUD_TOLERANCE
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
◆ cyhal_uart_event_callback_t
| typedef void(* cyhal_uart_event_callback_t) (void *callback_arg, cyhal_uart_event_t event) |
UART callback function type
◆ 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.
|
◆ cyhal_uart_init()
Initialize the uart peripheral. It sets the default parameters for uart peripheral, and configures its specifieds pins.
- Parameters
-
| [out] | obj | The uart object |
| [in] | tx | The TX pin name, if no TX pin use NC |
| [in] | rx | The RX pin name, if no RX pin use NC |
| [in] | clk | The clock to use can be shared, if not provided a new clock will be, allocated and the default baud rate set |
| [in] | cfg | The 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()
Release the uart peripheral, not currently invoked. It requires further resource management.
- Parameters
-
| [in,out] | obj | The uart object |
◆ cyhal_uart_set_baud()
Configure the baud rate
- Parameters
-
| [in,out] | obj | The uart object |
| [in] | baudrate | The baud rate to be configured |
| [out] | actualbaud | The 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()
Configure the data bits, stop bits, and parity
- Parameters
-
| [in,out] | obj | The uart object |
| [in] | cfg | The 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()
Get character. This is a blocking call, waiting for a character
- Parameters
-
| [in] | obj | The uart object |
| [out] | value | The value read from the serial port |
| [in] | timeout | The 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()
Send a character. This is a blocking call, waiting for the peripheral to be available for writing
- Parameters
-
| [in] | obj | The uart object |
| [in] | value | The character to be sent |
- Returns
- The status of the putc request
◆ cyhal_uart_readable()
Check the number of bytes avaialable to read from the receive buffers
- Parameters
-
- Returns
- The number of readable bytes
◆ cyhal_uart_writable()
Check the number of bytes than can be written to the transmit buffer
- Parameters
-
- Returns
- The number of bytes that can be written
◆ cyhal_uart_clear()
Clear the uart peripheral buffers
- Parameters
-
- Returns
- The status of the clear request
◆ cyhal_uart_set_flow_control()
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] | obj | The uart object |
| [in] | cts | The TX pin name |
| [in] | rts | The RX pin name |
- Returns
- The status of the init_flow_control request
◆ cyhal_uart_write()
Begin synchronous TX transfer. The used buffer is specified in the uart object, tx_buff
- Parameters
-
| [in] | obj | The uart object |
| [in] | tx | The 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()
Begin synchronous RX transfer (enable interrupt for data collecting) The used buffer is specified in the uart object - rx_buff
- Parameters
-
| [in] | obj | The uart object |
| [in] | rx | The 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()
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] | obj | The uart object |
| [in] | tx | The transmit buffer |
| [in] | length | The number of bytes to transmit |
- Returns
- The status of the tx_async request
◆ cyhal_uart_read_async()
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] | obj | The uart object |
| [out] | rx | The user specified receive buffer |
| [in] | length | The number of bytes to receive |
- Returns
- The status of the rx_async request
◆ cyhal_uart_is_tx_active()
Attempts to determine if the uart peripheral is already in use for TX
- Parameters
-
- Returns
- Is the TX channel active
◆ cyhal_uart_is_rx_active()
Attempts to determine if the uart peripheral is already in use for RX
- Parameters
-
- Returns
- Is the RX channel active
◆ cyhal_uart_write_abort()
Abort the ongoing TX transaction. It disables the enabled interupt for TX and flushes the TX hardware buffer if TX FIFO is used
- Parameters
-
- Returns
- The status of the tx_abort request
◆ cyhal_uart_read_abort()
Abort the ongoing read transaction. It disables the enabled interrupt for RX and flushes the RX hardware buffer if RX FIFO is used
- Parameters
-
- Returns
- The status of the read_abort request
◆ cyhal_uart_register_callback()
The uart callback handler registration
- Parameters
-
| [in] | obj | The uart object |
| [in] | callback | The callback handler which will be invoked when the interrupt fires |
| [in] | callback_arg | Generic argument that will be provided to the callback when called |
◆ cyhal_uart_enable_event()
Configure uart interrupt. This function is used for word-approach
- Parameters
-
| [in] | obj | The uart object |
| [in] | event | The uart event type, this argument supports the bitwise-or of multiple enum flag values |
| [in] | intrPriority | The priority for NVIC interrupt events |
| [in] | enable | True to turn on interrupts, False to turn off |