High level interface for interacting with the Universal Asynchronous Receiver-Transmitter (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 by providing cts / rts pins in the device configurator or through manually generated structures.In case CTS flow control enablement status needs to be changed, mtb_hal_uart_enable_cts_flow_control function can be used.
The UART contains dedicated hardware buffers for transmit and receive. Optionally, either of these can be augmented with a software buffer. This can be done using the PDL functions
Interrupts are handled by callbacks based on events mtb_hal_uart_event_t If an event is disabled, the underlying interrupt is still enabled. Enabling or disabling an event only enables or disables the callback.
The following snippet sets up the UART block.
The snippet also shows how to use mtb_hal_uart_write, mtb_hal_uart_put, mtb_hal_uart_read API.
In the following snippet, UART events are handled in a callback function. The callback function has to be registered and then the events have to be enabled.
API Reference | |
UART HAL Results | |
UART specific return codes. | |
Typedefs | |
typedef void(* | mtb_hal_uart_event_callback_t) (void *callback_arg, mtb_hal_uart_event_t event) |
UART callback function type. | |
Enumerations | |
enum | mtb_hal_uart_event_t { MTB_HAL_UART_IRQ_NONE = 0 , MTB_HAL_UART_IRQ_TX_TRANSMIT_IN_FIFO = (MTB_HAL_MAP_UART_IRQ_TX_TRANSMIT_IN_FIFO) , MTB_HAL_UART_IRQ_TX_DONE = (MTB_HAL_MAP_UART_IRQ_TX_DONE) , MTB_HAL_UART_IRQ_RX_DONE = (MTB_HAL_MAP_UART_IRQ_RX_DONE) , MTB_HAL_UART_IRQ_RX_FULL = (MTB_HAL_MAP_UART_IRQ_RX_FULL) , MTB_HAL_UART_IRQ_RX_ERROR = (MTB_HAL_MAP_UART_IRQ_RX_ERROR) , MTB_HAL_UART_IRQ_TX_ERROR = (MTB_HAL_MAP_UART_IRQ_TX_ERROR) , MTB_HAL_UART_IRQ_RX_NOT_EMPTY = (MTB_HAL_MAP_UART_IRQ_RX_NOT_EMPTY) , MTB_HAL_UART_IRQ_TX_EMPTY = (MTB_HAL_MAP_UART_IRQ_TX_EMPTY) , MTB_HAL_UART_IRQ_TX_FIFO = (MTB_HAL_MAP_UART_IRQ_TX_FIFO) , MTB_HAL_UART_IRQ_RX_FIFO = (MTB_HAL_MAP_UART_IRQ_RX_FIFO) } |
UART enum to enable/disable/report interrupt cause flags. More... | |
Functions | |
cy_rslt_t | mtb_hal_uart_set_baud (mtb_hal_uart_t *obj, uint32_t baudrate, uint32_t *actualbaud) |
Configure the baud rate. More... | |
cy_rslt_t | mtb_hal_uart_get (mtb_hal_uart_t *obj, uint8_t *value, uint32_t timeout) |
Get a character. More... | |
cy_rslt_t | mtb_hal_uart_put (mtb_hal_uart_t *obj, uint32_t value) |
Send a character. More... | |
uint32_t | mtb_hal_uart_readable (mtb_hal_uart_t *obj) |
Check the number of bytes available to read from the receive buffers. More... | |
uint32_t | mtb_hal_uart_writable (mtb_hal_uart_t *obj) |
Check the number of bytes than can be written to the transmit buffer. More... | |
cy_rslt_t | mtb_hal_uart_clear (mtb_hal_uart_t *obj) |
Clear the UART buffers. More... | |
cy_rslt_t | mtb_hal_uart_enable_cts_flow_control (mtb_hal_uart_t *obj, bool enable) |
Configure the UART CTS for flow control. More... | |
cy_rslt_t | mtb_hal_uart_write (mtb_hal_uart_t *obj, void *tx, size_t *tx_length) |
Begin synchronous TX transfer. More... | |
cy_rslt_t | mtb_hal_uart_read (mtb_hal_uart_t *obj, void *rx, size_t *rx_length) |
Begin synchronous RX transfer (enable interrupt for data collecting) More... | |
bool | mtb_hal_uart_is_tx_active (mtb_hal_uart_t *obj) |
Determines if the UART peripheral is currently in use for TX. More... | |
void | mtb_hal_uart_register_callback (mtb_hal_uart_t *obj, mtb_hal_uart_event_callback_t callback, void *callback_arg) |
Register a uart callback handler. More... | |
void | mtb_hal_uart_enable_event (mtb_hal_uart_t *obj, mtb_hal_uart_event_t event, bool enable) |
Enable or disable specified UART events. More... | |
cy_rslt_t | mtb_hal_uart_process_interrupt (mtb_hal_uart_t *obj) |
Process interrupts related related to a UART instance. More... | |
enum mtb_hal_uart_event_t |
UART enum to enable/disable/report interrupt cause flags.
cy_rslt_t mtb_hal_uart_set_baud | ( | mtb_hal_uart_t * | obj, |
uint32_t | baudrate, | ||
uint32_t * | actualbaud | ||
) |
Configure the baud rate.
[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. |
cy_rslt_t mtb_hal_uart_get | ( | mtb_hal_uart_t * | obj, |
uint8_t * | value, | ||
uint32_t | timeout | ||
) |
Get a character.
This is a blocking call which waits till a character is received.
[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. Zero is wait forever |
cy_rslt_t mtb_hal_uart_put | ( | mtb_hal_uart_t * | obj, |
uint32_t | value | ||
) |
Send a character.
This is a blocking call which waits till the character is sent out from the UART completely.
[in] | obj | The UART object |
[in] | value | The character to be sent |
uint32_t mtb_hal_uart_readable | ( | mtb_hal_uart_t * | obj | ) |
Check the number of bytes available to read from the receive buffers.
[in] | obj | The UART object |
uint32_t mtb_hal_uart_writable | ( | mtb_hal_uart_t * | obj | ) |
Check the number of bytes than can be written to the transmit buffer.
[in] | obj | The UART object |
cy_rslt_t mtb_hal_uart_clear | ( | mtb_hal_uart_t * | obj | ) |
Clear the UART buffers.
[in] | obj | The UART object |
cy_rslt_t mtb_hal_uart_enable_cts_flow_control | ( | mtb_hal_uart_t * | obj, |
bool | enable | ||
) |
Configure the UART CTS for flow control.
It sets flow control in the hardware if a UART peripheral supports it, otherwise software emulation is used.
[in,out] | obj | The UART object |
[in] | enable | Enable or disable the CTS flow control |
cy_rslt_t mtb_hal_uart_write | ( | mtb_hal_uart_t * | obj, |
void * | tx, | ||
size_t * | tx_length | ||
) |
Begin synchronous TX transfer.
This will write either length
bytes or until the write buffer is full, whichever is less, then return. The value pointed to by length
will be updated to reflect the number of bytes that was actually written.
[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 |
cy_rslt_t mtb_hal_uart_read | ( | mtb_hal_uart_t * | obj, |
void * | rx, | ||
size_t * | rx_length | ||
) |
Begin synchronous RX transfer (enable interrupt for data collecting)
This will read either length
bytes or the number of bytes that are currently available in the receive buffer, whichever is less, then return. The value pointed to by length
will be updated to reflect the number of bytes that was actually read.
[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 |
Begin synchronous RX transfer (enable interrupt for data collecting)
bool mtb_hal_uart_is_tx_active | ( | mtb_hal_uart_t * | obj | ) |
Determines if the UART peripheral is currently in use for TX.
This checks if all the data in the UART TX FIFO is transmitted
[in] | obj | The UART object |
void mtb_hal_uart_register_callback | ( | mtb_hal_uart_t * | obj, |
mtb_hal_uart_event_callback_t | callback, | ||
void * | callback_arg | ||
) |
Register a uart callback handler.
This function will be called when one of the events enabled by mtb_hal_uart_enable_event occurs.
[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 |
void mtb_hal_uart_enable_event | ( | mtb_hal_uart_t * | obj, |
mtb_hal_uart_event_t | event, | ||
bool | enable | ||
) |
Enable or disable specified UART events.
When an enabled event occurs, the function specified by mtb_hal_uart_register_callback will be called.
[in] | obj | The UART object |
[in] | event | The uart event type, this argument supports the bitwise-or of multiple enum flag values |
[in] | enable | True to turn on interrupts, False to turn off |
cy_rslt_t mtb_hal_uart_process_interrupt | ( | mtb_hal_uart_t * | obj | ) |
Process interrupts related related to a UART instance.
obj | HAL object for which the interrupt should be processed |