Expand description
§Universal Asynchronous Receiver/Transmitter (UART)
§Overview
The UART is a hardware peripheral which handles communication using serial communication interfaces, such as RS232 and RS485. This peripheral provides a cheap and ubiquitous method for full- and half-duplex communication between devices.
Depending on your device, two or more UART controllers are available for use, all of which can be configured and used in the same way. All UART controllers are compatible with UART-enabled devices from various manufacturers, and can also support Infrared Data Association (IrDA) protocols.
§Configuration
Each UART controller is individually configurable, and the usual setting such as baud rate, data bits, parity, and stop bits can easily be configured. Additionally, the receive (RX) and transmit (TX) pins need to be specified.
let mut uart1 = Uart::new(
peripherals.UART1,
peripherals.GPIO1,
peripherals.GPIO2,
).unwrap();The UART controller can be configured to invert the polarity of the pins. This is achieved by inverting the desired pins, and then constructing the UART instance using the inverted pins.
§Usage
The UART driver implements a number of third-party traits, with the intention of making the HAL inter-compatible with various device drivers from the community. This includes, but is not limited to, the embedded-hal and embedded-io blocking traits, and the embedded-hal-async and embedded-io-async asynchronous traits.
In addition to the interfaces provided by these traits, native APIs are also available. See the examples below for more information on how to interact with this driver.
§Examples
§Sending and Receiving Data
// Write bytes out over the UART:
uart1.write_bytes(b"Hello, world!").expect("write error!");§Splitting the UART into RX and TX Components
// The UART can be split into separate Transmit and Receive components:
let (mut rx, mut tx) = uart1.split();
// Each component can be used individually to interact with the UART:
tx.write_bytes(&[42u8]).expect("write error!");
let byte = rx.read_byte().expect("read error!");§Inverting RX and TX Pins
let (rx, _) = peripherals.GPIO2.split();
let (_, tx) = peripherals.GPIO1.split();
let mut uart1 = Uart::new(
peripherals.UART1,
rx.inverted(),
tx.inverted(),
).unwrap();§Constructing RX and TX Components
let tx = UartTx::new(peripherals.UART0, peripherals.GPIO1).unwrap();
let rx = UartRx::new(peripherals.UART1, peripherals.GPIO2).unwrap();Modules§
- Low-power UART
Structs§
- Any UART peripheral.
- Configuration for the AT-CMD detection functionality
- UART Configuration
- Peripheral data describing a particular UART instance.
- Peripheral state for a UART instance.
- UART (Full-duplex)
- UART (Receive)
- UART (Transmit)
Enums§
- UART clock source
- Number of data bits
- UART Error
- Parity check
- Number of stop bits
- List of exposed UART events.
Traits§
- UART Peripheral Instance