From 1374ad2ab6a6f86a46c7c244568718a6bf8db041 Mon Sep 17 00:00:00 2001 From: huntc Date: Wed, 15 Dec 2021 17:51:26 +1100 Subject: Introduces split on the nRF Uarte A new `split` method is introduced such that the Uarte tx and rx can be used from separate tasks. An MPSC is used to illustrate how data may be passed between these tasks. --- examples/nrf/src/bin/uart_split.rs | 68 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 examples/nrf/src/bin/uart_split.rs (limited to 'examples') diff --git a/examples/nrf/src/bin/uart_split.rs b/examples/nrf/src/bin/uart_split.rs new file mode 100644 index 000000000..4b5dbb21f --- /dev/null +++ b/examples/nrf/src/bin/uart_split.rs @@ -0,0 +1,68 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +#[path = "../example_common.rs"] +mod example_common; +use embassy::blocking_mutex::kind::Noop; +use embassy::channel::mpsc::{self, Channel, Sender}; +use embassy::util::Forever; +use embassy_nrf::peripherals::UARTE0; +use embassy_nrf::uarte::UarteRx; +use example_common::*; + +use embassy::executor::Spawner; +use embassy::traits::uart::{Read, Write}; +use embassy_nrf::gpio::NoPin; +use embassy_nrf::{interrupt, uarte, Peripherals}; + +static CHANNEL: Forever> = Forever::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, NoPin, NoPin, config); + let (mut tx, rx) = uart.split(); + + let c = CHANNEL.put(Channel::new()); + let (s, mut r) = mpsc::split(c); + + info!("uarte initialized!"); + + // Spawn a task responsible purely for reading + + unwrap!(spawner.spawn(reader(rx, s))); + + // 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 { + if let Some(buf) = r.recv().await { + info!("writing..."); + unwrap!(tx.write(&buf).await); + } + } +} + +#[embassy::task] +async fn reader(mut rx: UarteRx<'static, UARTE0>, s: Sender<'static, Noop, [u8; 8], 1>) { + let mut buf = [0; 8]; + loop { + info!("reading..."); + unwrap!(rx.read(&mut buf).await); + unwrap!(s.send(buf).await); + } +} -- cgit