diff options
| author | Quentin Smith <[email protected]> | 2023-07-17 21:31:43 -0400 |
|---|---|---|
| committer | Quentin Smith <[email protected]> | 2023-07-17 21:31:43 -0400 |
| commit | 6f02403184eb7fb7990fb88fc9df9c4328a690a3 (patch) | |
| tree | 748f510e190bb2724750507a6e69ed1a8e08cb20 /examples/nrf52840/src/bin/uart_split.rs | |
| parent | d896f80405aa8963877049ed999e4aba25d6e2bb (diff) | |
| parent | 6b5df4523aa1c4902f02e803450ae4b418e0e3ca (diff) | |
Merge remote-tracking branch 'origin/main' into nrf-pdm
Diffstat (limited to 'examples/nrf52840/src/bin/uart_split.rs')
| -rw-r--r-- | examples/nrf52840/src/bin/uart_split.rs | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/examples/nrf52840/src/bin/uart_split.rs b/examples/nrf52840/src/bin/uart_split.rs new file mode 100644 index 000000000..9979a1d53 --- /dev/null +++ b/examples/nrf52840/src/bin/uart_split.rs | |||
| @@ -0,0 +1,63 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::*; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_nrf::peripherals::UARTE0; | ||
| 8 | use embassy_nrf::uarte::UarteRx; | ||
| 9 | use embassy_nrf::{bind_interrupts, uarte}; | ||
| 10 | use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex; | ||
| 11 | use embassy_sync::channel::Channel; | ||
| 12 | use {defmt_rtt as _, panic_probe as _}; | ||
| 13 | |||
| 14 | static CHANNEL: Channel<ThreadModeRawMutex, [u8; 8], 1> = Channel::new(); | ||
| 15 | |||
| 16 | bind_interrupts!(struct Irqs { | ||
| 17 | UARTE0_UART0 => uarte::InterruptHandler<UARTE0>; | ||
| 18 | }); | ||
| 19 | |||
| 20 | #[embassy_executor::main] | ||
| 21 | async fn main(spawner: Spawner) { | ||
| 22 | let p = embassy_nrf::init(Default::default()); | ||
| 23 | let mut config = uarte::Config::default(); | ||
| 24 | config.parity = uarte::Parity::EXCLUDED; | ||
| 25 | config.baudrate = uarte::Baudrate::BAUD115200; | ||
| 26 | |||
| 27 | let uart = uarte::Uarte::new(p.UARTE0, Irqs, p.P0_08, p.P0_06, config); | ||
| 28 | let (mut tx, rx) = uart.split(); | ||
| 29 | |||
| 30 | info!("uarte initialized!"); | ||
| 31 | |||
| 32 | // Spawn a task responsible purely for reading | ||
| 33 | |||
| 34 | unwrap!(spawner.spawn(reader(rx))); | ||
| 35 | |||
| 36 | // Message must be in SRAM | ||
| 37 | { | ||
| 38 | let mut buf = [0; 23]; | ||
| 39 | buf.copy_from_slice(b"Type 8 chars to echo!\r\n"); | ||
| 40 | |||
| 41 | unwrap!(tx.write(&buf).await); | ||
| 42 | info!("wrote hello in uart!"); | ||
| 43 | } | ||
| 44 | |||
| 45 | // Continue reading in this main task and write | ||
| 46 | // back out the buffer we receive from the read | ||
| 47 | // task. | ||
| 48 | loop { | ||
| 49 | let buf = CHANNEL.recv().await; | ||
| 50 | info!("writing..."); | ||
| 51 | unwrap!(tx.write(&buf).await); | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | #[embassy_executor::task] | ||
| 56 | async fn reader(mut rx: UarteRx<'static, UARTE0>) { | ||
| 57 | let mut buf = [0; 8]; | ||
| 58 | loop { | ||
| 59 | info!("reading..."); | ||
| 60 | unwrap!(rx.read(&mut buf).await); | ||
| 61 | CHANNEL.send(buf).await; | ||
| 62 | } | ||
| 63 | } | ||
