embassy/examples/nrf/src/bin/buffered_uart.rs

57 lines
1.5 KiB
Rust
Raw Normal View History

2020-12-23 16:18:29 +01:00
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use defmt::*;
2021-03-29 02:47:10 +02:00
use embassy::executor::Spawner;
2022-06-12 22:15:44 +02:00
use embassy_nrf::buffered_uarte::{BufferedUarte, State};
use embassy_nrf::{interrupt, uarte, Peripherals};
use embedded_io::asynch::{BufRead, Write};
2021-03-29 00:42:08 +02:00
use futures::pin_mut;
2022-06-12 22:15:44 +02:00
use {defmt_rtt as _, panic_probe as _};
2021-03-29 02:47:10 +02:00
#[embassy::main]
2021-05-17 11:48:58 +02:00
async fn main(_spawner: Spawner, p: Peripherals) {
let mut config = uarte::Config::default();
config.parity = uarte::Parity::EXCLUDED;
config.baudrate = uarte::Baudrate::BAUD115200;
let mut tx_buffer = [0u8; 4096];
let mut rx_buffer = [0u8; 4096];
2020-12-23 16:18:29 +01:00
let irq = interrupt::take!(UARTE0_UART0);
let mut state = State::new();
// Please note - important to have hardware flow control (https://github.com/embassy-rs/embassy/issues/536)
let u = BufferedUarte::new(
&mut state,
p.UARTE0,
p.TIMER0,
p.PPI_CH0,
p.PPI_CH1,
irq,
p.P0_08,
p.P0_06,
p.P0_07,
p.P0_05,
config,
&mut rx_buffer,
&mut tx_buffer,
);
2021-02-20 00:27:24 +01:00
pin_mut!(u);
2020-12-23 16:18:29 +01:00
info!("uarte initialized!");
unwrap!(u.write_all(b"Hello!\r\n").await);
info!("wrote hello in uart!");
loop {
info!("reading...");
let buf = unwrap!(u.fill_buf().await);
2021-02-24 08:57:06 +01:00
info!("read done, got {}", buf);
2020-12-23 16:18:29 +01:00
// Read bytes have to be explicitly consumed, otherwise fill_buf() will return them again
let n = buf.len();
u.consume(n);
2020-12-23 16:18:29 +01:00
}
}