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

55 lines
1.3 KiB
Rust
Raw Permalink Normal View History

2020-12-23 16:18:29 +01:00
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use defmt::*;
use embassy_executor::Spawner;
use embassy_nrf::buffered_uarte::{self, BufferedUarte};
use embassy_nrf::{bind_interrupts, peripherals, uarte};
2023-08-07 13:43:09 +02:00
use embedded_io_async::Write;
2022-06-12 22:15:44 +02:00
use {defmt_rtt as _, panic_probe as _};
bind_interrupts!(struct Irqs {
UARTE0_UART0 => buffered_uarte::InterruptHandler<peripherals::UARTE0>;
});
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_nrf::init(Default::default());
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];
let mut u = BufferedUarte::new(
p.UARTE0,
p.TIMER0,
p.PPI_CH0,
p.PPI_CH1,
p.PPI_GROUP0,
Irqs,
p.P0_08,
p.P0_06,
config,
&mut rx_buffer,
&mut tx_buffer,
);
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
}
}