The friendly Operating System for the Internet of Things

Low-level UART peripheral driver. More...

Detailed Description

This is a basic UART (Universal Asynchronous Receiver Transmitter) interface to allow platform independent access to the MCU's serial communication abilities. This interface is intentionally designed to be as simple as possible, to allow for easy implementation and maximum portability. In RIOT we only use the common 8-N-1 format of the serial port (8 data bits, no parity bit, one stop bit).

The simple interface provides capabilities to initialize the serial communication module, which automatically enables for receiving data, as well as writing data to the UART port, which means transmitting data. The UART device and the corresponding pins need to be mapped in RIOT/boards/ * /include/periph_conf.h. Furthermore you need to select the baudrate for initialization which is typically {9600, 19200, 38400, 57600, 115200} baud. Additionally you should register a callback function that is executed in interrupt context when data is being received. The driver will then read the received data byte, call the registered callback function and pass the received data to it via its argument. The interface enforces the receiving to be implemented in an interrupt driven mode. Thus, you never know how many bytes are going to be received and might want to handle that in your specific callback function. The transmit function can be implemented in any way.

By default the UART_DEV(0) device of each board is initialized and mapped to STDIO in RIOT which is used for standard input/output functions like printf() or puts().


file  uart.h
 Low-level UART peripheral driver interface definition.

Data Structures

struct  uart_isr_ctx_t
 Interrupt context for a UART device. More...


typedef void(* uart_rx_cb_t) (void *arg, uint8_t data)
 Signature for receive interrupt callback. More...


enum  {
 Possible UART return values. More...


int uart_init (uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg)
 Initialize a given UART device. More...
void uart_write (uart_t uart, const uint8_t *data, size_t len)
 Write data from the given buffer to the specified UART device. More...
void uart_poweron (uart_t uart)
 Power on the given UART device. More...
void uart_poweroff (uart_t uart)
 Power off the given UART device. More...
typedef unsigned int uart_t
 Define default UART type identifier.
 Default UART undefined value.
#define UART_DEV(x)   (x)
 Default UART device access macro.

Typedef Documentation

typedef void(* uart_rx_cb_t) (void *arg, uint8_t data)
[in]argcontext to the callback (optional)
[in]datathe byte that was received

Definition at line 93 of file uart.h.

Enumeration Type Documentation

anonymous enum

everything in order


invalid UART device given


given baudrate is not applicable


all other internal errors


given mode is not applicable

Definition at line 110 of file uart.h.

Function Documentation

int uart_init ( uart_t  uart,
uint32_t  baudrate,
uart_rx_cb_t  rx_cb,
void *  arg 

The UART device will be initialized with the following configuration:

  • 8 data bits
  • no parity
  • 1 stop bit
  • baudrate as given

If no callback parameter is given (rx_cb := NULL), the UART will be initialized in TX only mode.

[in]uartUART device to initialize
[in]baudratedesired baudrate in baud/s
[in]rx_cbreceive callback, executed in interrupt context once for every byte that is received (RX buffer filled), set to NULL for TX only mode
[in]argoptional context passed to the callback functions
UART_OK on success
UART_NODEV on invalid UART device
UART_NOBAUD on inapplicable baudrate
UART_INTERR on other errors
void uart_poweroff ( uart_t  uart)
[in]uartthe UART device to power off
void uart_poweron ( uart_t  uart)
[in]uartthe UART device to power on
void uart_write ( uart_t  uart,
const uint8_t *  data,
size_t  len 

This function is blocking, as it will only return after len bytes from the given buffer have been send. The way this data is send is up to the implementation: active waiting, interrupt driven, DMA, etc.

[in]uartUART device to use for transmission
[in]datadata buffer to send
[in]lennumber of bytes to send