-
Notifications
You must be signed in to change notification settings - Fork 171
/
uart_loopback.rs
44 lines (37 loc) · 1.1 KB
/
uart_loopback.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//! UART loopback test
//!
//! Folowing pins are used:
//! TX GPIO12
//! RX GPIO13
//!
//! 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_hal::sys::link_patches();
let peripherals = Peripherals::take()?;
let tx = peripherals.pins.gpio12;
let rx = peripherals.pins.gpio13;
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,
)?;
loop {
uart.write(&[0xaa])?;
let mut buf = [0_u8; 1];
uart.read(&mut buf, BLOCK)?;
println!("Written 0xaa, read 0x{:02x}", buf[0]);
}
}