Module uart_hal

UART Hardware Abstraction Layer behavior.

Description

This module defines the behavior that platform-specific UART modules must implement. It provides a common interface for UART (Universal Asynchronous Receiver-Transmitter) operations across all supported platforms.

Currently, only ESP32 provides a UART implementation.

Lifecycle

A UART port is opened with open/1 or open/2 and closed with close/1. The open/2 variant is a convenience that takes the peripheral name as a separate argument. The returned handle is passed to all subsequent operations.

Reading and writing

  • read/1 - Non-blocking read. Returns {ok, Data} if data is available, or {error, timeout} if no data is ready.

  • read/2 - Blocking read with a timeout in milliseconds. Waits up to the specified time for data to arrive.

  • write/2 - Write data to the UART port.

Configuration parameters

The open/1 function accepts a proplist of configuration parameters. Common parameters include:

  • {tx, integer()} - Transmit pin number

  • {rx, integer()} - Receive pin number

  • {speed, pos_integer()} - Baud rate

  • {data_bits, 5..8} - Number of data bits (default: 8)

  • {stop_bits, 1 | 2} - Number of stop bits (default: 1)

  • {parity, none | even | odd} - Parity mode

  • {flow_control, none | hardware | software} - Flow control type

  • {rts, integer()} - RTS pin for hardware flow control

  • {cts, integer()} - CTS pin for hardware flow control

  • {peripheral, string() | binary()} - UART peripheral name (e.g. "UART0", "UART1", "UART2")

Example

  UART = uart:open([{tx, 17}, {rx, 16}, {speed, 115200}]),
  uart:write(UART, <<"Hello\r\n">>),
  case uart:read(UART, 5000) of
      {ok, Data} -> io:format("Received: ~p~n", [Data]);
      {error, timeout} -> io:format("No response~n")
  end,
  uart:close(UART).

Data Types

params()


params() = [term()]

Initialization parameters for the UART port. See the module documentation for common parameters.

peripheral()


peripheral() = string() | binary()

UART peripheral name (e.g. "UART0", "UART1").

uart()


uart() = port() | pid() | term()

Handle returned by open/1 or open/2.