diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-04-06 00:00:29 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-04-06 01:34:08 +0200 |
| commit | 27a1b0ea7316be4687e7173a73861d276974d502 (patch) | |
| tree | 6edd9fd7c2d69a5ad130dc13ae7c0bbed442e640 /examples/nrf/src/bin/uart_split.rs | |
| parent | aee19185b7cf34466f7941784b55e639c925fae4 (diff) | |
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.
Diffstat (limited to 'examples/nrf/src/bin/uart_split.rs')
| -rw-r--r-- | examples/nrf/src/bin/uart_split.rs | 23 |
1 files changed, 9 insertions, 14 deletions
diff --git a/examples/nrf/src/bin/uart_split.rs b/examples/nrf/src/bin/uart_split.rs index 909429b1a..3fde2f0d8 100644 --- a/examples/nrf/src/bin/uart_split.rs +++ b/examples/nrf/src/bin/uart_split.rs | |||
| @@ -3,10 +3,9 @@ | |||
| 3 | #![feature(type_alias_impl_trait)] | 3 | #![feature(type_alias_impl_trait)] |
| 4 | 4 | ||
| 5 | use defmt::*; | 5 | use defmt::*; |
| 6 | use embassy::blocking_mutex::raw::NoopRawMutex; | 6 | use embassy::blocking_mutex::raw::ThreadModeRawMutex; |
| 7 | use embassy::channel::mpsc::{self, Channel, Sender}; | 7 | use embassy::channel::channel::Channel; |
| 8 | use embassy::executor::Spawner; | 8 | use embassy::executor::Spawner; |
| 9 | use embassy::util::Forever; | ||
| 10 | use embassy_nrf::peripherals::UARTE0; | 9 | use embassy_nrf::peripherals::UARTE0; |
| 11 | use embassy_nrf::uarte::UarteRx; | 10 | use embassy_nrf::uarte::UarteRx; |
| 12 | use embassy_nrf::{interrupt, uarte, Peripherals}; | 11 | use embassy_nrf::{interrupt, uarte, Peripherals}; |
| @@ -14,7 +13,7 @@ use embassy_nrf::{interrupt, uarte, Peripherals}; | |||
| 14 | use defmt_rtt as _; // global logger | 13 | use defmt_rtt as _; // global logger |
| 15 | use panic_probe as _; | 14 | use panic_probe as _; |
| 16 | 15 | ||
| 17 | static CHANNEL: Forever<Channel<NoopRawMutex, [u8; 8], 1>> = Forever::new(); | 16 | static CHANNEL: Channel<ThreadModeRawMutex, [u8; 8], 1> = Channel::new(); |
| 18 | 17 | ||
| 19 | #[embassy::main] | 18 | #[embassy::main] |
| 20 | async fn main(spawner: Spawner, p: Peripherals) { | 19 | async fn main(spawner: Spawner, p: Peripherals) { |
| @@ -26,14 +25,11 @@ async fn main(spawner: Spawner, p: Peripherals) { | |||
| 26 | let uart = uarte::Uarte::new(p.UARTE0, irq, p.P0_08, p.P0_06, config); | 25 | let uart = uarte::Uarte::new(p.UARTE0, irq, p.P0_08, p.P0_06, config); |
| 27 | let (mut tx, rx) = uart.split(); | 26 | let (mut tx, rx) = uart.split(); |
| 28 | 27 | ||
| 29 | let c = CHANNEL.put(Channel::new()); | ||
| 30 | let (s, mut r) = mpsc::split(c); | ||
| 31 | |||
| 32 | info!("uarte initialized!"); | 28 | info!("uarte initialized!"); |
| 33 | 29 | ||
| 34 | // Spawn a task responsible purely for reading | 30 | // Spawn a task responsible purely for reading |
| 35 | 31 | ||
| 36 | unwrap!(spawner.spawn(reader(rx, s))); | 32 | unwrap!(spawner.spawn(reader(rx))); |
| 37 | 33 | ||
| 38 | // Message must be in SRAM | 34 | // Message must be in SRAM |
| 39 | { | 35 | { |
| @@ -48,19 +44,18 @@ async fn main(spawner: Spawner, p: Peripherals) { | |||
| 48 | // back out the buffer we receive from the read | 44 | // back out the buffer we receive from the read |
| 49 | // task. | 45 | // task. |
| 50 | loop { | 46 | loop { |
| 51 | if let Some(buf) = r.recv().await { | 47 | let buf = CHANNEL.recv().await; |
| 52 | info!("writing..."); | 48 | info!("writing..."); |
| 53 | unwrap!(tx.write(&buf).await); | 49 | unwrap!(tx.write(&buf).await); |
| 54 | } | ||
| 55 | } | 50 | } |
| 56 | } | 51 | } |
| 57 | 52 | ||
| 58 | #[embassy::task] | 53 | #[embassy::task] |
| 59 | async fn reader(mut rx: UarteRx<'static, UARTE0>, s: Sender<'static, NoopRawMutex, [u8; 8], 1>) { | 54 | async fn reader(mut rx: UarteRx<'static, UARTE0>) { |
| 60 | let mut buf = [0; 8]; | 55 | let mut buf = [0; 8]; |
| 61 | loop { | 56 | loop { |
| 62 | info!("reading..."); | 57 | info!("reading..."); |
| 63 | unwrap!(rx.read(&mut buf).await); | 58 | unwrap!(rx.read(&mut buf).await); |
| 64 | unwrap!(s.send(buf).await); | 59 | CHANNEL.send(buf).await; |
| 65 | } | 60 | } |
| 66 | } | 61 | } |
