diff options
| author | huntc <[email protected]> | 2021-12-15 17:51:26 +1100 |
|---|---|---|
| committer | huntc <[email protected]> | 2021-12-15 18:31:52 +1100 |
| commit | 1374ad2ab6a6f86a46c7c244568718a6bf8db041 (patch) | |
| tree | 531694c1560d8d83c714438d5b49a8ca7036e815 /examples/nrf/src/bin/uart_split.rs | |
| parent | 052abc918a987acb35d44dcd91d9db8a8aaf8ece (diff) | |
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.
Diffstat (limited to 'examples/nrf/src/bin/uart_split.rs')
| -rw-r--r-- | examples/nrf/src/bin/uart_split.rs | 68 |
1 files changed, 68 insertions, 0 deletions
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 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | #[path = "../example_common.rs"] | ||
| 6 | mod example_common; | ||
| 7 | use embassy::blocking_mutex::kind::Noop; | ||
| 8 | use embassy::channel::mpsc::{self, Channel, Sender}; | ||
| 9 | use embassy::util::Forever; | ||
| 10 | use embassy_nrf::peripherals::UARTE0; | ||
| 11 | use embassy_nrf::uarte::UarteRx; | ||
| 12 | use example_common::*; | ||
| 13 | |||
| 14 | use embassy::executor::Spawner; | ||
| 15 | use embassy::traits::uart::{Read, Write}; | ||
| 16 | use embassy_nrf::gpio::NoPin; | ||
| 17 | use embassy_nrf::{interrupt, uarte, Peripherals}; | ||
| 18 | |||
| 19 | static CHANNEL: Forever<Channel<Noop, [u8; 8], 1>> = Forever::new(); | ||
| 20 | |||
| 21 | #[embassy::main] | ||
| 22 | async fn main(spawner: Spawner, p: Peripherals) { | ||
| 23 | let mut config = uarte::Config::default(); | ||
| 24 | config.parity = uarte::Parity::EXCLUDED; | ||
| 25 | config.baudrate = uarte::Baudrate::BAUD115200; | ||
| 26 | |||
| 27 | let irq = interrupt::take!(UARTE0_UART0); | ||
| 28 | let uart = uarte::Uarte::new(p.UARTE0, irq, p.P0_08, p.P0_06, NoPin, NoPin, config); | ||
| 29 | let (mut tx, rx) = uart.split(); | ||
| 30 | |||
| 31 | let c = CHANNEL.put(Channel::new()); | ||
| 32 | let (s, mut r) = mpsc::split(c); | ||
| 33 | |||
| 34 | info!("uarte initialized!"); | ||
| 35 | |||
| 36 | // Spawn a task responsible purely for reading | ||
| 37 | |||
| 38 | unwrap!(spawner.spawn(reader(rx, s))); | ||
| 39 | |||
| 40 | // Message must be in SRAM | ||
| 41 | { | ||
| 42 | let mut buf = [0; 23]; | ||
| 43 | buf.copy_from_slice(b"Type 8 chars to echo!\r\n"); | ||
| 44 | |||
| 45 | unwrap!(tx.write(&buf).await); | ||
| 46 | info!("wrote hello in uart!"); | ||
| 47 | } | ||
| 48 | |||
| 49 | // Continue reading in this main task and write | ||
| 50 | // back out the buffer we receive from the read | ||
| 51 | // task. | ||
| 52 | loop { | ||
| 53 | if let Some(buf) = r.recv().await { | ||
| 54 | info!("writing..."); | ||
| 55 | unwrap!(tx.write(&buf).await); | ||
| 56 | } | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | #[embassy::task] | ||
| 61 | async fn reader(mut rx: UarteRx<'static, UARTE0>, s: Sender<'static, Noop, [u8; 8], 1>) { | ||
| 62 | let mut buf = [0; 8]; | ||
| 63 | loop { | ||
| 64 | info!("reading..."); | ||
| 65 | unwrap!(rx.read(&mut buf).await); | ||
| 66 | unwrap!(s.send(buf).await); | ||
| 67 | } | ||
| 68 | } | ||
