3.25. uart — Universal Asynchronous Receiver/Transmitter

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

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


Defines

uart_read(self_p, buf_p, size) queue_read(&(self_p)->chin, 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) (self_p)->chout.write(&(self_p)->chout, 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_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]