Zephyr API Documentation
2.7.0-rc2
A Scalable Open Source RTOS
|
UART Interface. More...
Data Structures | |
struct | uart_event_tx |
UART TX event data. More... | |
struct | uart_event_rx |
UART RX event data. More... | |
struct | uart_event_rx_buf |
UART RX buffer released event data. More... | |
struct | uart_event_rx_stop |
UART RX stopped data. More... | |
struct | uart_event |
Structure containing information about current event. More... | |
struct | uart_config |
UART controller configuration structure. More... | |
struct | uart_device_config |
UART device configuration. More... | |
struct | uart_driver_api |
Driver API structure. More... | |
Typedefs | |
typedef void(* | uart_callback_t) (const struct device *dev, struct uart_event *evt, void *user_data) |
Define the application callback function signature for uart_callback_set() function. More... | |
typedef void(* | uart_irq_callback_user_data_t) (const struct device *dev, void *user_data) |
Define the application callback function signature for uart_irq_callback_user_data_set() function. More... | |
typedef void(* | uart_irq_config_func_t) (const struct device *dev) |
For configuring IRQ on each individual UART device. More... | |
Functions | |
static int | uart_callback_set (const struct device *dev, uart_callback_t callback, void *user_data) |
Set event handler function. More... | |
int | uart_tx (const struct device *dev, const uint8_t *buf, size_t len, int32_t timeout) |
Send given number of bytes from buffer through UART. More... | |
int | uart_tx_abort (const struct device *dev) |
Abort current TX transmission. More... | |
int | uart_rx_enable (const struct device *dev, uint8_t *buf, size_t len, int32_t timeout) |
Start receiving data through UART. More... | |
static int | uart_rx_buf_rsp (const struct device *dev, uint8_t *buf, size_t len) |
Provide receive buffer in response to uart_event_type::UART_RX_BUF_REQUEST event. More... | |
int | uart_rx_disable (const struct device *dev) |
Disable RX. More... | |
int | uart_err_check (const struct device *dev) |
Check whether an error was detected. More... | |
int | uart_poll_in (const struct device *dev, unsigned char *p_char) |
Poll the device for input. More... | |
void | uart_poll_out (const struct device *dev, unsigned char out_char) |
Output a character in polled mode. More... | |
int | uart_configure (const struct device *dev, const struct uart_config *cfg) |
Set UART configuration. More... | |
int | uart_config_get (const struct device *dev, struct uart_config *cfg) |
Get UART configuration. More... | |
static int | uart_fifo_fill (const struct device *dev, const uint8_t *tx_data, int size) |
Fill FIFO with data. More... | |
static int | uart_fifo_read (const struct device *dev, uint8_t *rx_data, const int size) |
Read data from FIFO. More... | |
void | uart_irq_tx_enable (const struct device *dev) |
Enable TX interrupt in IER. More... | |
void | uart_irq_tx_disable (const struct device *dev) |
Disable TX interrupt in IER. More... | |
static int | uart_irq_tx_ready (const struct device *dev) |
Check if UART TX buffer can accept a new char. More... | |
void | uart_irq_rx_enable (const struct device *dev) |
Enable RX interrupt. More... | |
void | uart_irq_rx_disable (const struct device *dev) |
Disable RX interrupt. More... | |
static int | uart_irq_tx_complete (const struct device *dev) |
Check if UART TX block finished transmission. More... | |
static int | uart_irq_rx_ready (const struct device *dev) |
Check if UART RX buffer has a received char. More... | |
void | uart_irq_err_enable (const struct device *dev) |
Enable error interrupt. More... | |
void | uart_irq_err_disable (const struct device *dev) |
Disable error interrupt. More... | |
int | uart_irq_is_pending (const struct device *dev) |
Check if any IRQs is pending. More... | |
int | uart_irq_update (const struct device *dev) |
Start processing interrupts in ISR. More... | |
static void | uart_irq_callback_user_data_set (const struct device *dev, uart_irq_callback_user_data_t cb, void *user_data) |
Set the IRQ callback function pointer. More... | |
static void | uart_irq_callback_set (const struct device *dev, uart_irq_callback_user_data_t cb) |
Set the IRQ callback function pointer (legacy). More... | |
int | uart_line_ctrl_set (const struct device *dev, uint32_t ctrl, uint32_t val) |
Manipulate line control for UART. More... | |
int | uart_line_ctrl_get (const struct device *dev, uint32_t ctrl, uint32_t *val) |
Retrieve line control for UART. More... | |
int | uart_drv_cmd (const struct device *dev, uint32_t cmd, uint32_t p) |
Send extra command to driver. More... | |
UART Interface.
uart_callback_t |
#include <include/drivers/uart.h>
Define the application callback function signature for uart_callback_set() function.
dev | UART device structure. |
evt | Pointer to uart_event structure. |
user_data | Pointer to data specified by user. |
uart_irq_callback_user_data_t |
#include <include/drivers/uart.h>
Define the application callback function signature for uart_irq_callback_user_data_set() function.
dev | UART device structure. |
user_data | Arbitrary user data. |
uart_irq_config_func_t |
#include <include/drivers/uart.h>
For configuring IRQ on each individual UART device.
dev | UART device structure. |
#include <include/drivers/uart.h>
Number of data bits.
Enumerator | |
---|---|
UART_CFG_DATA_BITS_5 | |
UART_CFG_DATA_BITS_6 | |
UART_CFG_DATA_BITS_7 | |
UART_CFG_DATA_BITS_8 | |
UART_CFG_DATA_BITS_9 |
#include <include/drivers/uart.h>
Hardware flow control options.
With flow control set to none, any operations related to flow control signals can be managed by user with uart_line_ctrl functions. In other cases, flow control is managed by hardware/driver.
Enumerator | |
---|---|
UART_CFG_FLOW_CTRL_NONE | |
UART_CFG_FLOW_CTRL_RTS_CTS | |
UART_CFG_FLOW_CTRL_DTR_DSR |
enum uart_config_parity |
#include <include/drivers/uart.h>
Parity modes.
Enumerator | |
---|---|
UART_CFG_PARITY_NONE | |
UART_CFG_PARITY_ODD | |
UART_CFG_PARITY_EVEN | |
UART_CFG_PARITY_MARK | |
UART_CFG_PARITY_SPACE |
#include <include/drivers/uart.h>
Number of stop bits.
Enumerator | |
---|---|
UART_CFG_STOP_BITS_0_5 | |
UART_CFG_STOP_BITS_1 | |
UART_CFG_STOP_BITS_1_5 | |
UART_CFG_STOP_BITS_2 |
enum uart_event_type |
#include <include/drivers/uart.h>
Types of events passed to callback in UART_ASYNC_API.
Receiving:
Any time during reception uart_event_type::UART_RX_STOPPED event can occur. if there is any data received, uart_event_type::UART_RX_RDY event will be generated. It will be followed by uart_event_type::UART_RX_BUF_RELEASED event for every buffer currently passed to driver and finally by uart_event_type::UART_RX_DISABLED event.
Receiving can be disabled using uart_rx_disable, after calling that function, if there is any data received, uart_event_type::UART_RX_RDY event will be generated. uart_event_type::UART_RX_BUF_RELEASED event will be generated for every buffer currently passed to driver and finally uart_event_type::UART_RX_DISABLED event will occur.
Transmitting:
Transmitting can be aborted using uart_tx_abort, after calling that function uart_event_type::UART_TX_ABORTED event will be generated.
Enumerator | |
---|---|
UART_TX_DONE | Whole TX buffer was transmitted. |
UART_TX_ABORTED | Transmitting aborted due to timeout or uart_tx_abort call. When flow control is enabled, there is a possibility that TX transfer won't finish in the allotted time. Some data may have been transferred, information about it can be found in event data. |
UART_RX_RDY | Received data is ready for processing. This event is generated in the following cases:
|
UART_RX_BUF_REQUEST | Driver requests next buffer for continuous reception. This event is triggered when receiving has started for a new buffer, i.e. it's time to provide a next buffer for a seamless switchover to it. For continuous reliable receiving, user should provide another RX buffer in response to this event, using uart_rx_buf_rsp function If uart_rx_buf_rsp is not called before current buffer is filled up, receiving will stop. |
UART_RX_BUF_RELEASED | Buffer is no longer used by UART driver. |
UART_RX_DISABLED | RX has been disabled and can be reenabled. This event is generated whenever receiver has been stopped, disabled or finished its operation and can be enabled again using uart_rx_enable |
UART_RX_STOPPED | RX has stopped due to external event. Reason is one of uart_rx_stop_reason. |
enum uart_line_ctrl |
#include <include/drivers/uart.h>
Line control signals.
Enumerator | |
---|---|
UART_LINE_CTRL_BAUD_RATE | |
UART_LINE_CTRL_RTS | |
UART_LINE_CTRL_DTR | |
UART_LINE_CTRL_DCD | |
UART_LINE_CTRL_DSR |
enum uart_rx_stop_reason |
#include <include/drivers/uart.h>
Reception stop reasons.
Values that correspond to events or errors responsible for stopping receiving.
|
inlinestatic |
#include <include/drivers/uart.h>
Set event handler function.
dev | UART device structure. |
callback | Event handler. |
user_data | Data to pass to event handler function. |
-ENOTSUP | If not supported. |
0 | If successful, negative errno code otherwise. |
int uart_config_get | ( | const struct device * | dev, |
struct uart_config * | cfg | ||
) |
#include <include/drivers/uart.h>
Get UART configuration.
Stores current UART configuration to *cfg, can be used to retrieve initial configuration after device was initialized using data from DTS.
dev | UART device structure. |
cfg | UART configuration structure. |
-ENOTSUP | If driver does not support getting current configuration. |
0 | If successful, negative errno code otherwise. |
int uart_configure | ( | const struct device * | dev, |
const struct uart_config * | cfg | ||
) |
#include <include/drivers/uart.h>
Set UART configuration.
Sets UART configuration using data from *cfg.
dev | UART device structure. |
cfg | UART configuration structure. |
-ENOSYS | If configuration is not supported by device. or driver does not support setting configuration in runtime. |
0 | If successful, negative errno code otherwise. |
#include <include/drivers/uart.h>
Send extra command to driver.
Implementation and accepted commands are driver specific. Refer to the drivers for more information.
dev | UART device structure. |
cmd | Command to driver. |
p | Parameter to the command. |
0 | If successful. |
failed | Otherwise. |
int uart_err_check | ( | const struct device * | dev | ) |
#include <include/drivers/uart.h>
Check whether an error was detected.
dev | UART device structure. |
uart_rx_stop_reason | If error during receiving occurred. |
0 | Otherwise. |
|
inlinestatic |
#include <include/drivers/uart.h>
Fill FIFO with data.
This function is expected to be called from UART interrupt handler (ISR), if uart_irq_tx_ready() returns true. Result of calling this function not from an ISR is undefined (hardware-dependent). Likewise, not calling this function from an ISR if uart_irq_tx_ready() returns true may lead to undefined behavior, e.g. infinite interrupt loops. It's mandatory to test return value of this function, as different hardware has different FIFO depth (oftentimes just 1).
dev | UART device structure. |
tx_data | Data to transmit. |
size | Number of bytes to send. |
|
inlinestatic |
#include <include/drivers/uart.h>
Read data from FIFO.
This function is expected to be called from UART interrupt handler (ISR), if uart_irq_rx_ready() returns true. Result of calling this function not from an ISR is undefined (hardware-dependent). It's unspecified whether "RX ready" condition as returned by uart_irq_rx_ready() is level- or edge- triggered. That means that once uart_irq_rx_ready() is detected, uart_fifo_read() must be called until it reads all available data in the FIFO (i.e. until it returns less data than was requested).
Note that the calling context only applies to physical UARTs and no to the virtual ones found in USB CDC ACM code.
dev | UART device structure. |
rx_data | Data container. |
size | Container size. |
|
inlinestatic |
#include <include/drivers/uart.h>
Set the IRQ callback function pointer (legacy).
This sets up the callback for IRQ. When an IRQ is triggered, the specified function will be called with the device pointer.
dev | UART device structure. |
cb | Pointer to the callback function. |
|
inlinestatic |
#include <include/drivers/uart.h>
Set the IRQ callback function pointer.
This sets up the callback for IRQ. When an IRQ is triggered, the specified function will be called with specified user data. See description of uart_irq_update() for the requirements on ISR.
dev | UART device structure. |
cb | Pointer to the callback function. |
user_data | Data to pass to callback function. |
#include <include/drivers/uart.h>
Disable error interrupt.
dev | UART device structure. |
1 | If an IRQ is ready. |
0 | Otherwise. |
#include <include/drivers/uart.h>
Enable error interrupt.
dev | UART device structure. |
int uart_irq_is_pending | ( | const struct device * | dev | ) |
#include <include/drivers/uart.h>
Check if any IRQs is pending.
dev | UART device structure. |
1 | If an IRQ is pending. |
0 | Otherwise. |
#include <include/drivers/uart.h>
Disable RX interrupt.
dev | UART device structure. |
#include <include/drivers/uart.h>
Enable RX interrupt.
dev | UART device structure. |
|
inlinestatic |
#include <include/drivers/uart.h>
Check if UART RX buffer has a received char.
Check if UART RX buffer has at least one pending character (i.e. uart_fifo_read() will succeed and return non-zero). This function must be called in a UART interrupt handler, or its result is undefined. Before calling this function in the interrupt handler, uart_irq_update() must be called once per the handler invocation. It's unspecified whether condition as returned by this function is level- or edge- triggered (i.e. if this function returns true when RX FIFO is non-empty, or when a new char was received since last call to it). See description of uart_fifo_read() for implication of this.
dev | UART device structure. |
1 | If a received char is ready. |
0 | Otherwise. |
-ENOTSUP | if this function is not supported |
|
inlinestatic |
#include <include/drivers/uart.h>
Check if UART TX block finished transmission.
Check if any outgoing data buffered in UART TX block was fully transmitted and TX block is idle. When this condition is true, UART device (or whole system) can be power off. Note that this function is not useful to check if UART TX can accept more data, use uart_irq_tx_ready() for that. This function must be called in a UART interrupt handler, or its result is undefined. Before calling this function in the interrupt handler, uart_irq_update() must be called once per the handler invocation.
dev | UART device structure. |
1 | If nothing remains to be transmitted. |
0 | Otherwise. |
-ENOTSUP | if this function is not supported |
#include <include/drivers/uart.h>
Disable TX interrupt in IER.
dev | UART device structure. |
#include <include/drivers/uart.h>
Enable TX interrupt in IER.
dev | UART device structure. |
|
inlinestatic |
#include <include/drivers/uart.h>
Check if UART TX buffer can accept a new char.
Check if UART TX buffer can accept at least one character for transmission (i.e. uart_fifo_fill() will succeed and return non-zero). This function must be called in a UART interrupt handler, or its result is undefined. Before calling this function in the interrupt handler, uart_irq_update() must be called once per the handler invocation.
dev | UART device structure. |
1 | If at least one char can be written to UART. |
0 | Otherwise. |
int uart_irq_update | ( | const struct device * | dev | ) |
#include <include/drivers/uart.h>
Start processing interrupts in ISR.
This function should be called the first thing in the ISR. Calling uart_irq_rx_ready(), uart_irq_tx_ready(), uart_irq_tx_complete() allowed only after this.
The purpose of this function is:
dev | UART device structure. |
1 | Always. |
#include <include/drivers/uart.h>
Retrieve line control for UART.
dev | UART device structure. |
ctrl | The line control to retrieve (see enum uart_line_ctrl). |
val | Pointer to variable where to store the line control value. |
0 | If successful. |
failed | Otherwise. |
#include <include/drivers/uart.h>
Manipulate line control for UART.
dev | UART device structure. |
ctrl | The line control to manipulate (see enum uart_line_ctrl). |
val | Value to set to the line control. |
0 | If successful. |
failed | Otherwise. |
#include <include/drivers/uart.h>
Poll the device for input.
dev | UART device structure. |
p_char | Pointer to character. |
0 | If a character arrived. |
-1 | If no character was available to read (i.e., the UART input buffer was empty). |
-ENOTSUP | If the operation is not supported. |
-EBUSY | If reception was enabled using uart_rx_enabled |
#include <include/drivers/uart.h>
Output a character in polled mode.
This routine checks if the transmitter is empty. When the transmitter is empty, it writes a character to the data register.
To send a character when hardware flow control is enabled, the handshake signal CTS must be asserted.
dev | UART device structure. |
out_char | Character to send. |
#include <include/drivers/uart.h>
Provide receive buffer in response to uart_event_type::UART_RX_BUF_REQUEST event.
Provide pointer to RX buffer, which will be used when current buffer is filled.
dev | UART device structure. |
buf | Pointer to receive buffer. |
len | Buffer length. |
-ENOTSUP | If not supported. |
-EBUSY | Next buffer already set. |
-EACCES | Receiver is already disabled (function called too late?). |
0 | If successful, negative errno code otherwise. |
int uart_rx_disable | ( | const struct device * | dev | ) |
#include <include/drivers/uart.h>
Disable RX.
uart_event_type::UART_RX_BUF_RELEASED event will be generated for every buffer scheduled, after that uart_event_type::UART_RX_DISABLED event will be generated. Additionally, if there is any pending received data, the uart_event_type::UART_RX_RDY event for that data will be generated before the uart_event_type::UART_RX_BUF_RELEASED events.
dev | UART device structure. |
-ENOTSUP | If not supported. |
-EFAULT | There is no active reception. |
0 | If successful, negative errno code otherwise. |
#include <include/drivers/uart.h>
Start receiving data through UART.
Function sets given buffer as first buffer for receiving and returns immediately. After that event handler, set using uart_callback_set, is called with uart_event_type::UART_RX_RDY or uart_event_type::UART_RX_BUF_REQUEST events.
dev | UART device structure. |
buf | Pointer to receive buffer. |
len | Buffer length. |
timeout | Inactivity period after receiving at least a byte which triggers uart_event_type::UART_RX_RDY event. Given in milliseconds. SYS_FOREVER_MS disables timeout. See uart_event_type for details. |
-ENOTSUP | If not supported. |
-EBUSY | RX already in progress. |
0 | If successful, negative errno code otherwise. |
#include <include/drivers/uart.h>
Send given number of bytes from buffer through UART.
Function returns immediately and event handler, set using uart_callback_set, is called after transfer is finished.
dev | UART device structure. |
buf | Pointer to transmit buffer. |
len | Length of transmit buffer. |
timeout | Timeout in milliseconds. Valid only if flow control is enabled. SYS_FOREVER_MS disables timeout. |
-ENOTSUP | If not supported. |
-EBUSY | There is already an ongoing transfer. |
0 | If successful, negative errno code otherwise. |
int uart_tx_abort | ( | const struct device * | dev | ) |
#include <include/drivers/uart.h>
Abort current TX transmission.
uart_event_type::UART_TX_DONE event will be generated with amount of data sent.
dev | UART device structure. |
-ENOTSUP | If not supported. |
-EFAULT | There is no active transmission. |
0 | If successful, negative errno code otherwise. |