aboutsummaryrefslogtreecommitdiff
path: root/examples/src/bin/uart.rs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/src/bin/uart.rs')
-rw-r--r--examples/src/bin/uart.rs72
1 files changed, 72 insertions, 0 deletions
diff --git a/examples/src/bin/uart.rs b/examples/src/bin/uart.rs
new file mode 100644
index 000000000..21e26e3ad
--- /dev/null
+++ b/examples/src/bin/uart.rs
@@ -0,0 +1,72 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5#[path = "../example_common.rs"]
6mod example_common;
7use example_common::*;
8
9use cortex_m_rt::entry;
10use embassy::io::{AsyncBufRead, AsyncBufReadExt, AsyncWrite, AsyncWriteExt};
11use embassy_nrf::uarte;
12use futures::pin_mut;
13use nrf52840_hal::gpio;
14
15#[static_executor::task]
16async fn run() {
17 let p = embassy_nrf::pac::Peripherals::take().dewrap();
18
19 let port0 = gpio::p0::Parts::new(p.P0);
20
21 let pins = uarte::Pins {
22 rxd: port0.p0_08.into_floating_input().degrade(),
23 txd: port0
24 .p0_06
25 .into_push_pull_output(gpio::Level::Low)
26 .degrade(),
27 cts: None,
28 rts: None,
29 };
30
31 let u = uarte::Uarte::new(
32 p.UARTE0,
33 pins,
34 uarte::Parity::EXCLUDED,
35 uarte::Baudrate::BAUD115200,
36 );
37 pin_mut!(u);
38
39 info!("uarte initialized!");
40
41 u.write_all(b"Hello!\r\n").await.dewrap();
42 info!("wrote hello in uart!");
43
44 // Simple demo, reading 8-char chunks and echoing them back reversed.
45 loop {
46 info!("reading...");
47 let mut buf = [0u8; 8];
48 u.read_exact(&mut buf).await.dewrap();
49 info!("read done, got {:[u8]}", buf);
50
51 // Reverse buf
52 for i in 0..4 {
53 let tmp = buf[i];
54 buf[i] = buf[7 - i];
55 buf[7 - i] = tmp;
56 }
57
58 info!("writing...");
59 u.write_all(&buf).await.dewrap();
60 info!("write done");
61 }
62}
63
64#[entry]
65fn main() -> ! {
66 info!("Hello World!");
67
68 unsafe {
69 run.spawn().dewrap();
70 static_executor::run();
71 }
72}