The latest development version of this page may be more current than this released 2.1.0 version.

UART

Overview

API Reference

group uart_interface

UART Interface.

Defines

UART_ERROR_BREAK

Backward compatibility defines, deprecated.

LINE_CTRL_BAUD_RATE
LINE_CTRL_RTS
LINE_CTRL_DTR
LINE_CTRL_DCD
LINE_CTRL_DSR
UART_OPTION_AFCE

Options for UART initialization.

Typedefs

typedef uart_callback_t

Define the application callback function signature for uart_callback_set() function.

Parameters
  • evt: Pointer to uart_event structure.

  • user_data: Pointer to data specified by user.

typedef uart_irq_callback_user_data_t

Define the application callback function signature for uart_irq_callback_user_data_set() function.

Parameters
  • user_data: Arbitrary user data.

typedef uart_irq_callback_t

Define the application callback function signature for legacy uart_irq_callback_set().

Parameters
  • port: Device struct for the UART device.

typedef uart_irq_config_func_t

For configuring IRQ on each individual UART device.

Enums

enum uart_line_ctrl

Line control signals.

Values:

UART_LINE_CTRL_BAUD_RATE = BIT(0)
UART_LINE_CTRL_RTS = BIT(1)
UART_LINE_CTRL_DTR = BIT(2)
UART_LINE_CTRL_DCD = BIT(3)
UART_LINE_CTRL_DSR = BIT(4)
enum uart_event_type

Types of events passed to callback in UART_ASYNC_API.

Receiving:

  1. To start receiving, uart_rx_enable has to be called with first buffer

  2. When receiving starts to current buffer, UART_RX_BUF_REQUEST will be generated, in response to that user can either:

    • Provide second buffer using uart_rx_buf_rsp, when first buffer is filled, receiving will automatically start to second buffer.

    • Ignore the event, this way when current buffer is filled UART_RX_DONE event will be generated and receiving will be stopped.

  3. If some data was received and timeout occurred UART_RX_RDY event will be generated. It can happen multiples times for the same buffer. RX timeout is counted from last byte received i.e. if no data was received, there won’t be any timeout event.

  4. After buffer is filled UART_RX_RDY will be generated, immediately followed by UART_RX_BUF_RELEASED indicating that current buffer is no longer used.

  5. If there was second buffer provided, it will become current buffer and we start again at point 2. If no second buffer was specified receiving is stopped and UART_RX_DISABLED event is generated. After that whole process can be repeated.

Any time during reception UART_RX_STOPPED event can occur. It will be followed by UART_RX_BUF_RELEASED event for every buffer currently passed to driver and finally by UART_RX_DISABLED event.

Receiving can be disabled using uart_rx_disable, after calling that function any data received will be lost, UART_RX_BUF_RELEASED event will be generated for every buffer currently passed to driver and UART_RX_DISABLED event will occur.

Transmitting:

  1. Transmitting starts by uart_tx function.

  2. If whole buffer was transmitted UART_TX_DONE is generated. If timeout occurred UART_TX_ABORTED will be generated.

Transmitting can be aborted using uart_tx_abort, after calling that function UART_TX_ABORTED event will be generated.

Values:

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 two cases:

  • When RX timeout occurred, and data was stored in provided buffer. This can happen multiple times in the same buffer.

  • When provided buffer is full.

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_rx_stop_reason

Reception stop reasons.

Values that correspond to events or errors responsible for stopping receiving.

Values:

UART_ERROR_OVERRUN = (1 << 0)

Overrun error.

UART_ERROR_PARITY = (1 << 1)

Parity error.

UART_ERROR_FRAMING = (1 << 2)

Framing error.

UART_BREAK = (1 << 3)

Break interrupt.

A break interrupt was received. This happens when the serial input is held at a logic ‘0’ state for longer than the sum of start time + data bits + parity + stop bits.

enum uart_config_parity

Parity modes.

Values:

UART_CFG_PARITY_NONE
UART_CFG_PARITY_ODD
UART_CFG_PARITY_EVEN
UART_CFG_PARITY_MARK
UART_CFG_PARITY_SPACE
enum uart_config_stop_bits

Number of stop bits.

Values:

UART_CFG_STOP_BITS_0_5
UART_CFG_STOP_BITS_1
UART_CFG_STOP_BITS_1_5
UART_CFG_STOP_BITS_2
enum uart_config_data_bits

Number of data bits.

Values:

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
enum uart_config_flow_control

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.

Values:

UART_CFG_FLOW_CTRL_NONE
UART_CFG_FLOW_CTRL_RTS_CTS
UART_CFG_FLOW_CTRL_DTR_DSR

Functions

int uart_err_check(struct device *dev)

Check whether an error was detected.

Parameters
  • dev: UART device structure.

Return Value
  • uart_rx_stop_reason: If error during receiving occurred.

  • 0: Otherwise.

int uart_poll_in(struct device *dev, unsigned char *p_char)

Poll the device for input.

Parameters
  • dev: UART device structure.

  • p_char: Pointer to character.

Return Value
  • 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

void uart_poll_out(struct device *dev, unsigned char out_char)

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.

Parameters
  • dev: UART device structure.

  • out_char: Character to send.

int uart_configure(struct device *dev, const struct uart_config *cfg)

Set UART configuration.

Sets UART configuration using data from *cfg.

Parameters
  • dev: UART device structure.

  • cfg: UART configuration structure.

Return Value
  • -ENOTSUP: If configuration is not supported by device. or driver does not support setting configuration in runtime.

  • 0: If successful, negative errno code otherwise.

int uart_config_get(struct device *dev, struct uart_config *cfg)

Get UART configuration.

Stores current UART configuration to *cfg, can be used to retrieve initial configuration after device was initialized using data from DTS.

Parameters
  • dev: UART device structure.

  • cfg: UART configuration structure.

Return Value
  • -ENOTSUP: If driver does not support getting current configuration.

  • 0: If successful, negative errno code otherwise.

static int uart_fifo_fill(struct device *dev, const u8_t *tx_data, int size)

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).

Return

Number of bytes sent.

Parameters
  • dev: UART device structure.

  • tx_data: Data to transmit.

  • size: Number of bytes to send.

static int uart_fifo_read(struct device *dev, u8_t *rx_data, const int size)

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.

Return

Number of bytes read.

Parameters
  • dev: UART device structure.

  • rx_data: Data container.

  • size: Container size.

void uart_irq_tx_enable(struct device *dev)

Enable TX interrupt in IER.

Return

N/A

Parameters
  • dev: UART device structure.

void uart_irq_tx_disable(struct device *dev)

Disable TX interrupt in IER.

Return

N/A

Parameters
  • dev: UART device structure.

static int uart_irq_tx_ready(struct device *dev)

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.

Parameters
  • dev: UART device structure.

Return Value
  • 1: If at least one char can be written to UART.

  • 0: Otherwise.

void uart_irq_rx_enable(struct device *dev)

Enable RX interrupt.

Return

N/A

Parameters
  • dev: UART device structure.

void uart_irq_rx_disable(struct device *dev)

Disable RX interrupt.

Return

N/A

Parameters
  • dev: UART device structure.

static int uart_irq_tx_complete(struct device *dev)

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.

Parameters
  • dev: UART device structure.

Return Value
  • 1: If nothing remains to be transmitted.

  • 0: Otherwise.

  • -ENOTSUP: if this function is not supported

static int uart_irq_rx_ready(struct device *dev)

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.

Parameters
  • dev: UART device structure.

Return Value
  • 1: If a received char is ready.

  • 0: Otherwise.

  • -ENOTSUP: if this function is not supported

void uart_irq_err_enable(struct device *dev)

Enable error interrupt.

Return

N/A

Parameters
  • dev: UART device structure.

void uart_irq_err_disable(struct device *dev)

Disable error interrupt.

Parameters
  • dev: UART device structure.

Return Value
  • 1: If an IRQ is ready.

  • 0: Otherwise.

int uart_irq_is_pending(struct device *dev)

Check if any IRQs is pending.

Parameters
  • dev: UART device structure.

Return Value
  • 1: If an IRQ is pending.

  • 0: Otherwise.

int uart_irq_update(struct device *dev)

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:

  • For devices with auto-acknowledge of interrupt status on register read to cache the value of this register (rx_ready, etc. then use this case).

  • For devices with explicit acknowledgement of interrupts, to ack any pending interrupts and likewise to cache the original value.

  • For devices with implicit acknowledgement, this function will be empty. But the ISR must perform the actions needs to ack the interrupts (usually, call uart_fifo_read() on rx_ready, and uart_fifo_fill() on tx_ready).

Parameters
  • dev: UART device structure.

Return Value
  • 1: Always.

static void uart_irq_callback_user_data_set(struct device *dev, uart_irq_callback_user_data_t cb, void *user_data)

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.

Return

N/A

Parameters
  • dev: UART device structure.

  • cb: Pointer to the callback function.

  • user_data: Data to pass to callback function.

static void uart_irq_callback_set(struct device *dev, uart_irq_callback_t cb)

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.

Return

N/A

Parameters
  • dev: UART device structure.

  • cb: Pointer to the callback function.

int uart_drv_cmd(struct device *dev, u32_t cmd, u32_t p)

Send extra command to driver.

Implementation and accepted commands are driver specific. Refer to the drivers for more information.

Parameters
  • dev: UART device structure.

  • cmd: Command to driver.

  • p: Parameter to the command.

Return Value
  • 0: If successful.

  • failed: Otherwise.

struct uart_event_tx
#include <uart.h>

UART TX event data.

struct uart_event_rx
#include <uart.h>

UART RX event data.

The data represented by the event is stored in rx.buf[rx.offset] to rx.buf[rx.offset+rx.len]. That is, the length is relative to the offset.

struct uart_event_rx_buf
#include <uart.h>

UART RX buffer released event data.

struct uart_event_rx_stop
#include <uart.h>

UART RX stopped data.

struct uart_event
#include <uart.h>

Structure containing information about current event.

union

Event data.

Public Members

UART_TX_DONE and UART_TX_ABORTED events data.

UART_RX_RDY event data.

UART_RX_BUF_RELEASED event data.

UART_RX_STOPPED event data.

struct uart_config
#include <uart.h>

UART controller configuration structure.

Parameters
  • baudrate: Baudrate setting in bps

  • parity: Parity bit, use uart_config_parity

  • stop_bits: Stop bits, use uart_config_stop_bits

  • data_bits: Data bits, use uart_config_data_bits

  • flow_ctrl: Flow control setting, use uart_config_flow_control

struct uart_device_config
#include <uart.h>

UART device configuration.

Parameters
  • port: Base port number

  • base: Memory mapped base address

  • regs: Register address

  • sys_clk_freq: System clock frequency in Hz

struct uart_driver_api
#include <uart.h>

Driver API structure.