aboutsummaryrefslogtreecommitdiff
path: root/examples/nrf/src/bin/uart_split.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-12-16 07:44:40 +0000
committerGitHub <[email protected]>2021-12-16 07:44:40 +0000
commit5df16c6793a3730535ac91af8133ff744d76fad5 (patch)
treeefe5218718aa6115d472aa00b8611c8bb5510078 /examples/nrf/src/bin/uart_split.rs
parentd5a3064c2c20b4a9515e5322bb9a74724ebcf7c9 (diff)
parent0642eec01e23d9037e21186cfb0c169e56f9a161 (diff)
Merge #544
544: Introduces split on the nRF Uarte r=Dirbaio a=huntc A new `split` method is introduced such that the Uarte tx and rx can be used from separate tasks. An MPSC is used in an example to illustrate how data may be passed between these tasks. The approach taken within the `Uarte` struct is to split into tx and rx fields on calling `Uarte::new`. These fields are returned given a call to `Uarte::split`, but otherwise, if that call isn't made, then the API remains as it was before. Here's a snippet from a new example introduced: ```rust #[embassy::main] async fn main(spawner: Spawner, p: Peripherals) { // ... let uart = uarte::Uarte::new(p.UARTE0, irq, p.P0_08, p.P0_06, NoPin, NoPin, config); let (mut tx, rx) = uart.split(); // ... // Spawn a task responsible purely for reading unwrap!(spawner.spawn(reader(rx, s))); // ... // 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); } } ``` Co-authored-by: huntc <[email protected]>
Diffstat (limited to 'examples/nrf/src/bin/uart_split.rs')
-rw-r--r--examples/nrf/src/bin/uart_split.rs68
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"]
6mod example_common;
7use embassy::blocking_mutex::kind::Noop;
8use embassy::channel::mpsc::{self, Channel, Sender};
9use embassy::util::Forever;
10use embassy_nrf::peripherals::UARTE0;
11use embassy_nrf::uarte::UarteRx;
12use example_common::*;
13
14use embassy::executor::Spawner;
15use embassy::traits::uart::{Read, Write};
16use embassy_nrf::gpio::NoPin;
17use embassy_nrf::{interrupt, uarte, Peripherals};
18
19static CHANNEL: Forever<Channel<Noop, [u8; 8], 1>> = Forever::new();
20
21#[embassy::main]
22async 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]
61async 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}