3.4.9. uart — Universal Asynchronous Receiver/Transmitter

Source code: src/drivers/network/uart.h, src/drivers/network/uart.c

Test code: tst/drivers/hardware/network/uart/main.c


Defines

UART_FRAME_FORMAT_5N1
UART_FRAME_FORMAT_5N15
UART_FRAME_FORMAT_5N2
UART_FRAME_FORMAT_5O1
UART_FRAME_FORMAT_5O15
UART_FRAME_FORMAT_5O2
UART_FRAME_FORMAT_5E1
UART_FRAME_FORMAT_5E15
UART_FRAME_FORMAT_5E2
UART_FRAME_FORMAT_6N1
UART_FRAME_FORMAT_6N15
UART_FRAME_FORMAT_6N2
UART_FRAME_FORMAT_6O1
UART_FRAME_FORMAT_6O15
UART_FRAME_FORMAT_6O2
UART_FRAME_FORMAT_6E1
UART_FRAME_FORMAT_6E15
UART_FRAME_FORMAT_6E2
UART_FRAME_FORMAT_7N1
UART_FRAME_FORMAT_7N15
UART_FRAME_FORMAT_7N2
UART_FRAME_FORMAT_7O1
UART_FRAME_FORMAT_7O15
UART_FRAME_FORMAT_7O2
UART_FRAME_FORMAT_7E1
UART_FRAME_FORMAT_7E15
UART_FRAME_FORMAT_7E2
UART_FRAME_FORMAT_8N1
UART_FRAME_FORMAT_8N15
UART_FRAME_FORMAT_8N2
UART_FRAME_FORMAT_8O1
UART_FRAME_FORMAT_8O15
UART_FRAME_FORMAT_8O2
UART_FRAME_FORMAT_8E1
UART_FRAME_FORMAT_8E15
UART_FRAME_FORMAT_8E2
UART_FRAME_FORMAT_9N1
UART_FRAME_FORMAT_9N15
UART_FRAME_FORMAT_9N2
UART_FRAME_FORMAT_9O1
UART_FRAME_FORMAT_9O15
UART_FRAME_FORMAT_9O2
UART_FRAME_FORMAT_9E1
UART_FRAME_FORMAT_9E15
UART_FRAME_FORMAT_9E2
UART_FRAME_FORMAT_DEFAULT
uart_read(self_p, buf_p, size)

Read data from the UART.

Return
Number of received bytes or negative error code.
Parameters
  • self_p: Initialized driver object.
  • buf_p: Buffer to read into.
  • size: Number of bytes to receive.

uart_write(self_p, buf_p, size)

Write data to the UART.

Return
Number of written bytes or negative error code.
Parameters
  • self_p: Initialized driver object.
  • buf_p: Buffer to write.
  • size: Number of bytes to write.

Typedefs

typedef int (*uart_rx_filter_cb_t)(char c)

Functions

int uart_module_init(void)

Initialize UART module. This function must be called before calling any other function in this module.

The module will only be initialized once even if this function is called multiple times.

Return
zero(0) or negative error code.

int uart_init(struct uart_driver_t *self_p, struct uart_device_t *dev_p, long baudrate, void *rxbuf_p, size_t size)

Initialize driver object from given configuration.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object to be initialized.
  • dev_p: Device to use.
  • baudrate: Baudrate.
  • rxbuf_p: Reception buffer.
  • size: Reception buffer size.

int uart_set_rx_filter_cb(struct uart_driver_t *self_p, uart_rx_filter_cb_t rx_filter_cb)

Set the reception filter callback function.

Return
zero(0) or negative error code.
Parameters
  • self_p: Initialized driver object.
  • rx_filter_cb: Callback to set.

int uart_start(struct uart_driver_t *self_p)

Starts the UART device using given driver object.

Return
zero(0) or negative error code.
Parameters
  • self_p: Initialized driver object.

int uart_stop(struct uart_driver_t *self_p)

Stops the UART device referenced by driver object.

Return
zero(0) or negative error code.
Parameters
  • self_p: Initialized driver object.

int uart_set_frame_format(struct uart_driver_t *self_p, int format)

Set the UART frame format (number of data bits, parity and number of stop bits) in the driver object. This function must be called after uart_init(), and before uart_start().

Return
zero(0) or negative error code.
Parameters
  • self_p: Initialized driver object.
  • format: An UART_FRAME_FORMAT_* constant.

int uart_device_start(struct uart_device_t *dev_p, long baudrate)

Starts the UART device using given configration. The UART device group of functions does not use interrupts, but instead polls the hardware for events. The driver and device functions may not be used for the same UART device.

Return
zero(0) or negative error code.
Parameters
  • dev_p: UART device to start.
  • baudrate: Baudrate.

int uart_device_stop(struct uart_device_t *dev_p)

Stops given UART device.

Return
zero(0) or negative error code.
Parameters
  • dev_p: UART device to stop.

ssize_t uart_device_read(struct uart_device_t *dev_p, void *buf_p, size_t size)

Read data from the UART. This function does not wait for interrupts, but instead busy-waits for data by polling UART registers.

Return
Number of received bytes or negative error code.
Parameters
  • dev_p: UART device to read from.
  • buf_p: Buffer to read into.
  • size: Number of bytes to receive.

ssize_t uart_device_write(struct uart_device_t *dev_p, const void *buf_p, size_t size)

Write data to the UART. This function does not wait for interrupts, but instead busy-waits for data by polling UART registers.

Return
Number of written bytes or negative error code.
Parameters
  • dev_p: UART device to write to.
  • buf_p: Buffer to write.
  • size: Number of bytes to write.

Variables

struct uart_device_t uart_device[UART_DEVICE_MAX]