embassy/examples/nrf/src/bin/uart_split.rs
Dario Nieuwenhuis 27a1b0ea73 Simpler Channel.
- Allow initializing in a static, without Forever.
- Remove ability to close, since in embedded enviromnents channels usually live forever and don't get closed.
- Remove MPSC restriction, it's MPMC now. Rename "mpsc" to "channel".
- `Sender` and `Receiver` are still available if you want to enforce a piece of code only has send/receive access, but are optional: you can send/receive directly into the Channel if you want.
2022-04-06 01:34:08 +02:00

61 lines
1.6 KiB
Rust

#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use defmt::*;
use embassy::blocking_mutex::raw::ThreadModeRawMutex;
use embassy::channel::channel::Channel;
use embassy::executor::Spawner;
use embassy_nrf::peripherals::UARTE0;
use embassy_nrf::uarte::UarteRx;
use embassy_nrf::{interrupt, uarte, Peripherals};
use defmt_rtt as _; // global logger
use panic_probe as _;
static CHANNEL: Channel<ThreadModeRawMutex, [u8; 8], 1> = Channel::new();
#[embassy::main]
async fn main(spawner: Spawner, p: Peripherals) {
let mut config = uarte::Config::default();
config.parity = uarte::Parity::EXCLUDED;
config.baudrate = uarte::Baudrate::BAUD115200;
let irq = interrupt::take!(UARTE0_UART0);
let uart = uarte::Uarte::new(p.UARTE0, irq, p.P0_08, p.P0_06, config);
let (mut tx, rx) = uart.split();
info!("uarte initialized!");
// Spawn a task responsible purely for reading
unwrap!(spawner.spawn(reader(rx)));
// Message must be in SRAM
{
let mut buf = [0; 23];
buf.copy_from_slice(b"Type 8 chars to echo!\r\n");
unwrap!(tx.write(&buf).await);
info!("wrote hello in uart!");
}
// Continue reading in this main task and write
// back out the buffer we receive from the read
// task.
loop {
let buf = CHANNEL.recv().await;
info!("writing...");
unwrap!(tx.write(&buf).await);
}
}
#[embassy::task]
async fn reader(mut rx: UarteRx<'static, UARTE0>) {
let mut buf = [0; 8];
loop {
info!("reading...");
unwrap!(rx.read(&mut buf).await);
CHANNEL.send(buf).await;
}
}