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.rs107
1 files changed, 0 insertions, 107 deletions
diff --git a/examples/src/bin/uart.rs b/examples/src/bin/uart.rs
deleted file mode 100644
index 107936686..000000000
--- a/examples/src/bin/uart.rs
+++ /dev/null
@@ -1,107 +0,0 @@
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 defmt::panic;
11use embassy::executor::{task, Executor};
12use embassy::time::{Duration, Timer};
13use embassy::util::Forever;
14use embassy_nrf::{interrupt, pac, rtc, uarte};
15use futures::future::{select, Either};
16use nrf52840_hal::clocks;
17use nrf52840_hal::gpio;
18
19#[task]
20async fn run(mut uart: uarte::Uarte<pac::UARTE0>) {
21 info!("uarte initialized!");
22
23 // Message must be in SRAM
24 let mut buf = [0; 8];
25 buf.copy_from_slice(b"Hello!\r\n");
26
27 uart.send(&buf).await;
28 info!("wrote hello in uart!");
29
30 info!("reading...");
31 loop {
32 let received = match select(
33 uart.receive(&mut buf),
34 Timer::after(Duration::from_millis(10)),
35 )
36 .await
37 {
38 Either::Left((buf, _)) => buf,
39 Either::Right((_, read)) => {
40 let (buf, n) = read.stop().await;
41 &buf[..n]
42 }
43 };
44
45 if received.len() > 0 {
46 info!("read done, got {:[u8]}", received);
47
48 // Echo back received data
49 uart.send(received).await;
50 }
51 }
52}
53
54static RTC: Forever<rtc::RTC<pac::RTC1>> = Forever::new();
55static ALARM: Forever<rtc::Alarm<pac::RTC1>> = Forever::new();
56static EXECUTOR: Forever<Executor> = Forever::new();
57
58#[entry]
59fn main() -> ! {
60 info!("Hello World!");
61
62 let p = unwrap!(embassy_nrf::pac::Peripherals::take());
63
64 clocks::Clocks::new(p.CLOCK)
65 .enable_ext_hfosc()
66 .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass)
67 .start_lfclk();
68
69 let rtc = RTC.put(rtc::RTC::new(p.RTC1, interrupt::take!(RTC1)));
70 rtc.start();
71
72 unsafe { embassy::time::set_clock(rtc) };
73
74 let alarm = ALARM.put(rtc.alarm0());
75 let executor = EXECUTOR.put(Executor::new_with_alarm(alarm, cortex_m::asm::sev));
76
77 // Init UART
78 let port0 = gpio::p0::Parts::new(p.P0);
79
80 let pins = uarte::Pins {
81 rxd: port0.p0_08.into_floating_input().degrade(),
82 txd: port0
83 .p0_06
84 .into_push_pull_output(gpio::Level::Low)
85 .degrade(),
86 cts: None,
87 rts: None,
88 };
89
90 // NOTE(unsafe): Safe becasue we do not use `mem::forget` anywhere.
91 let uart = unsafe {
92 uarte::Uarte::new(
93 p.UARTE0,
94 interrupt::take!(UARTE0_UART0),
95 pins,
96 uarte::Parity::EXCLUDED,
97 uarte::Baudrate::BAUD115200,
98 )
99 };
100
101 unwrap!(executor.spawn(run(uart)));
102
103 loop {
104 executor.run();
105 cortex_m::asm::wfe();
106 }
107}