esp-idf-hal/examples/uart_loopback.rs
Ronen Ulanovsky 3f532d7224
Overhaul UART to support single wire transmitters/receivers (#164)
* overhaul UART to support single wire trasimitters/receivers

* add missing methods for uart transmitter/receiver

* fix `uart_config_t`'s `source_clk` being part of a union in `esp-idf v4.x`

* add `esp32c2`'s `PLL F40M` `APB` clock

* revert `uart_wait_tx_done` to timeout of 0

* fix incorrect order in `uart_driver_install`, `rx_buffer` shou always exceed `UART_FIFO_SIZE`

* Refactor `Owner` enum

* Fix double drop on `into_split`

* Add missing `count` methods for the UART drivers, change their return to be able to fit the maximum allowed value, don't run `drop` on temporary internal borrows, update example
2022-11-19 15:10:43 +02:00

46 lines
1.1 KiB
Rust

//! UART loopback test
//!
//! Folowing pins are used:
//! TX GPIO5
//! RX GPIO6
//!
//! Depending on your target and the board you are using you have to change the pins.
//!
//! This example transfers data via UART.
//! Connect TX and RX pins to see the outgoing data is read as incoming data.
use esp_idf_hal::delay::BLOCK;
use esp_idf_hal::gpio;
use esp_idf_hal::peripherals::Peripherals;
use esp_idf_hal::prelude::*;
use esp_idf_hal::uart::*;
fn main() -> anyhow::Result<()> {
esp_idf_sys::link_patches();
let peripherals = Peripherals::take().unwrap();
let tx = peripherals.pins.gpio5;
let rx = peripherals.pins.gpio6;
println!("Starting UART loopback test");
let config = config::Config::new().baudrate(Hertz(115_200));
let uart = UartDriver::new(
peripherals.uart1,
tx,
rx,
Option::<gpio::Gpio0>::None,
Option::<gpio::Gpio1>::None,
&config,
)
.unwrap();
loop {
uart.write(&[0xaa])?;
let mut buf = [0_u8; 1];
uart.read(&mut buf, BLOCK)?;
println!("Written 0xaa, read 0x{:02x}", buf[0]);
}
}