UART (Universal Asynchronous Receiver-Transmitter)
LibXR::UART provides an abstract base class for general asynchronous serial communication. It supports configuration of baud rate, data bits, stop bits, and parity, and encapsulates unified read/write interfaces for easy cross-platform adaptation.
Interface Overview
Enum Type
enum class Parity : uint8_t {
NO_PARITY = 0, // No parity
EVEN = 1, // Even parity
ODD = 2 // Odd parity
};
Configuration Structure
struct Configuration {
uint32_t baudrate; // Baud rate
Parity parity; // Parity mode
uint8_t data_bits; // Valid data bits; for byte-wise RX only the low data_bits bits are valid
uint8_t stop_bits; // Stop bit length
};
Construction and Configuration
template <typename ReadPortType = ReadPort, typename WritePortType = WritePort>
UART(ReadPortType* read_port, WritePortType* write_port);
virtual ErrorCode SetConfig(Configuration config, bool in_isr = false) = 0;
The constructor takes read/write port pointers (derived types of ReadPort / WritePort are allowed). Internally it stores:
ReadPort* read_port_WritePort* write_port_
When the configuration takes effect and from which contexts SetConfig() may be called depend on the backend.
Data Transmission Interfaces
template <typename OperationType>
ErrorCode Write(ConstRawData data, OperationType&& op, bool in_isr = false);
template <typename OperationType>
ErrorCode Read(RawData data, OperationType&& op, bool in_isr = false);
Write and Read interfaces are based on the unified Port + Operation abstraction, supporting blocking, callback, and polling models. They are suitable for both main loop and asynchronous environments.
opmust be a namedWriteOperation/ReadOperationobject (or a derived type); a temporary does not compile. The port copiesop; the callback, polling status, or semaphore it refers to must stay valid until completion (until the call returns for BLOCK).in_isrindicates whether the call happens in ISR context (forwarded to the portoperator()).
Completion modes and return values are described in IO Read/Write Abstraction, Operation Model, and BLOCK Timeout and Completion Handoff; backend implementation is covered in UART Driver Design.
Notes
UART::ParitysupportsNO_PARITY,EVEN, andODD.stop_bitsis auint8_tstop-bit count; the accepted values depend on the backend.- The
UARTbase class storesread_port_/write_port_pointers and forwardsRead()/Write(); the port objects are created and managed by the caller or the platform implementation.
Feature Summary
- Full configuration support for baud rate, data bits, stop bits, and parity;
- Unified read/write interfaces supporting multiple I/O operation models;
- Platform-independent, facilitating cross-platform adaptation and abstraction;
- Typically implemented using underlying hardware drivers for TX/RX logic, with users not needing to handle subclass details.