aboutsummaryrefslogtreecommitdiff
path: root/examples/src/bin
diff options
context:
space:
mode:
Diffstat (limited to 'examples/src/bin')
-rw-r--r--examples/src/bin/buffered_uart.rs84
-rw-r--r--examples/src/bin/executor_fairness_test.rs74
-rw-r--r--examples/src/bin/gpiote.rs83
-rw-r--r--examples/src/bin/gpiote_port.rs62
-rw-r--r--examples/src/bin/multiprio.rs176
-rw-r--r--examples/src/bin/qspi.rs134
-rw-r--r--examples/src/bin/rtc_async.rs65
-rw-r--r--examples/src/bin/rtc_raw.rs63
-rw-r--r--examples/src/bin/uart.rs107
9 files changed, 0 insertions, 848 deletions
diff --git a/examples/src/bin/buffered_uart.rs b/examples/src/bin/buffered_uart.rs
deleted file mode 100644
index 6e15fbcfa..000000000
--- a/examples/src/bin/buffered_uart.rs
+++ /dev/null
@@ -1,84 +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 futures::pin_mut;
12use nrf52840_hal::gpio;
13
14use embassy::executor::{task, Executor};
15use embassy::io::{AsyncBufRead, AsyncBufReadExt, AsyncWrite, AsyncWriteExt};
16use embassy::util::Forever;
17use embassy_nrf::buffered_uarte;
18use embassy_nrf::interrupt;
19
20#[task]
21async fn run() {
22 let p = unwrap!(embassy_nrf::pac::Peripherals::take());
23
24 let port0 = gpio::p0::Parts::new(p.P0);
25
26 let pins = buffered_uarte::Pins {
27 rxd: port0.p0_08.into_floating_input().degrade(),
28 txd: port0
29 .p0_06
30 .into_push_pull_output(gpio::Level::Low)
31 .degrade(),
32 cts: None,
33 rts: None,
34 };
35
36 let irq = interrupt::take!(UARTE0_UART0);
37 let u = buffered_uarte::BufferedUarte::new(
38 p.UARTE0,
39 irq,
40 pins,
41 buffered_uarte::Parity::EXCLUDED,
42 buffered_uarte::Baudrate::BAUD115200,
43 );
44 pin_mut!(u);
45
46 info!("uarte initialized!");
47
48 unwrap!(u.write_all(b"Hello!\r\n").await);
49 info!("wrote hello in uart!");
50
51 // Simple demo, reading 8-char chunks and echoing them back reversed.
52 loop {
53 info!("reading...");
54 let mut buf = [0u8; 8];
55 unwrap!(u.read_exact(&mut buf).await);
56 info!("read done, got {:[u8]}", buf);
57
58 // Reverse buf
59 for i in 0..4 {
60 let tmp = buf[i];
61 buf[i] = buf[7 - i];
62 buf[7 - i] = tmp;
63 }
64
65 info!("writing...");
66 unwrap!(u.write_all(&buf).await);
67 info!("write done");
68 }
69}
70
71static EXECUTOR: Forever<Executor> = Forever::new();
72
73#[entry]
74fn main() -> ! {
75 info!("Hello World!");
76
77 let executor = EXECUTOR.put(Executor::new(cortex_m::asm::sev));
78 unwrap!(executor.spawn(run()));
79
80 loop {
81 executor.run();
82 cortex_m::asm::wfe();
83 }
84}
diff --git a/examples/src/bin/executor_fairness_test.rs b/examples/src/bin/executor_fairness_test.rs
deleted file mode 100644
index 9b2c1bd26..000000000
--- a/examples/src/bin/executor_fairness_test.rs
+++ /dev/null
@@ -1,74 +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 core::task::Poll;
10use cortex_m_rt::entry;
11use defmt::panic;
12use embassy::executor::{task, Executor};
13use embassy::time::{Duration, Instant, Timer};
14use embassy::util::Forever;
15use embassy_nrf::pac;
16use embassy_nrf::{interrupt, rtc};
17use nrf52840_hal::clocks;
18
19#[task]
20async fn run1() {
21 loop {
22 info!("DING DONG");
23 Timer::after(Duration::from_ticks(16000)).await;
24 }
25}
26
27#[task]
28async fn run2() {
29 loop {
30 Timer::at(Instant::from_ticks(0)).await;
31 }
32}
33
34#[task]
35async fn run3() {
36 futures::future::poll_fn(|cx| {
37 cx.waker().wake_by_ref();
38 Poll::<()>::Pending
39 })
40 .await;
41}
42
43static RTC: Forever<rtc::RTC<pac::RTC1>> = Forever::new();
44static ALARM: Forever<rtc::Alarm<pac::RTC1>> = Forever::new();
45static EXECUTOR: Forever<Executor> = Forever::new();
46
47#[entry]
48fn main() -> ! {
49 info!("Hello World!");
50
51 let p = unwrap!(embassy_nrf::pac::Peripherals::take());
52
53 clocks::Clocks::new(p.CLOCK)
54 .enable_ext_hfosc()
55 .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass)
56 .start_lfclk();
57
58 let rtc = RTC.put(rtc::RTC::new(p.RTC1, interrupt::take!(RTC1)));
59 rtc.start();
60
61 unsafe { embassy::time::set_clock(rtc) };
62
63 let alarm = ALARM.put(rtc.alarm0());
64 let executor = EXECUTOR.put(Executor::new_with_alarm(alarm, cortex_m::asm::sev));
65
66 unwrap!(executor.spawn(run1()));
67 unwrap!(executor.spawn(run2()));
68 unwrap!(executor.spawn(run3()));
69
70 loop {
71 executor.run();
72 cortex_m::asm::wfe();
73 }
74}
diff --git a/examples/src/bin/gpiote.rs b/examples/src/bin/gpiote.rs
deleted file mode 100644
index afa1b85d5..000000000
--- a/examples/src/bin/gpiote.rs
+++ /dev/null
@@ -1,83 +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 nrf52840_hal::gpio;
12
13use embassy::executor::{task, Executor};
14use embassy::util::Forever;
15use embassy_nrf::gpiote;
16use embassy_nrf::interrupt;
17
18#[task]
19async fn run() {
20 let p = unwrap!(embassy_nrf::pac::Peripherals::take());
21 let port0 = gpio::p0::Parts::new(p.P0);
22
23 let g = gpiote::Gpiote::new(p.GPIOTE, interrupt::take!(GPIOTE));
24
25 info!("Starting!");
26
27 let pin1 = port0.p0_11.into_pullup_input().degrade();
28 let button1 = async {
29 let ch = unwrap!(g.new_input_channel(pin1, gpiote::InputChannelPolarity::HiToLo));
30
31 loop {
32 ch.wait().await;
33 info!("Button 1 pressed")
34 }
35 };
36
37 let pin2 = port0.p0_12.into_pullup_input().degrade();
38 let button2 = async {
39 let ch = unwrap!(g.new_input_channel(pin2, gpiote::InputChannelPolarity::LoToHi));
40
41 loop {
42 ch.wait().await;
43 info!("Button 2 released")
44 }
45 };
46
47 let pin3 = port0.p0_24.into_pullup_input().degrade();
48 let button3 = async {
49 let ch = unwrap!(g.new_input_channel(pin3, gpiote::InputChannelPolarity::Toggle));
50
51 loop {
52 ch.wait().await;
53 info!("Button 3 toggled")
54 }
55 };
56
57 let pin4 = port0.p0_25.into_pullup_input().degrade();
58 let button4 = async {
59 let ch = unwrap!(g.new_input_channel(pin4, gpiote::InputChannelPolarity::Toggle));
60
61 loop {
62 ch.wait().await;
63 info!("Button 4 toggled")
64 }
65 };
66
67 futures::join!(button1, button2, button3, button4);
68}
69
70static EXECUTOR: Forever<Executor> = Forever::new();
71
72#[entry]
73fn main() -> ! {
74 info!("Hello World!");
75
76 let executor = EXECUTOR.put(Executor::new(cortex_m::asm::sev));
77 unwrap!(executor.spawn(run()));
78
79 loop {
80 executor.run();
81 cortex_m::asm::wfe();
82 }
83}
diff --git a/examples/src/bin/gpiote_port.rs b/examples/src/bin/gpiote_port.rs
deleted file mode 100644
index f5aa81322..000000000
--- a/examples/src/bin/gpiote_port.rs
+++ /dev/null
@@ -1,62 +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 core::mem;
10use cortex_m_rt::entry;
11use defmt::panic;
12use nrf52840_hal::gpio;
13
14use embassy::executor::{task, Executor};
15use embassy::util::Forever;
16use embassy_nrf::gpiote::{Gpiote, PortInputPolarity};
17use embassy_nrf::interrupt;
18
19async fn button(g: &Gpiote, n: usize, pin: gpio::Pin<gpio::Input<gpio::PullUp>>) {
20 loop {
21 g.wait_port_input(&pin, PortInputPolarity::Low).await;
22 info!("Button {:?} pressed!", n);
23 g.wait_port_input(&pin, PortInputPolarity::High).await;
24 info!("Button {:?} released!", n);
25 }
26}
27
28#[task]
29async fn run() {
30 let p = unwrap!(embassy_nrf::pac::Peripherals::take());
31 let port0 = gpio::p0::Parts::new(p.P0);
32
33 let g = Gpiote::new(p.GPIOTE, interrupt::take!(GPIOTE));
34 info!(
35 "sizeof Signal<()> = {:usize}",
36 mem::size_of::<embassy::util::Signal<()>>()
37 );
38 info!("sizeof gpiote = {:usize}", mem::size_of::<Gpiote>());
39
40 info!("Starting!");
41
42 let button1 = button(&g, 1, port0.p0_11.into_pullup_input().degrade());
43 let button2 = button(&g, 2, port0.p0_12.into_pullup_input().degrade());
44 let button3 = button(&g, 3, port0.p0_24.into_pullup_input().degrade());
45 let button4 = button(&g, 4, port0.p0_25.into_pullup_input().degrade());
46 futures::join!(button1, button2, button3, button4);
47}
48
49static EXECUTOR: Forever<Executor> = Forever::new();
50
51#[entry]
52fn main() -> ! {
53 info!("Hello World!");
54
55 let executor = EXECUTOR.put(Executor::new(cortex_m::asm::sev));
56 unwrap!(executor.spawn(run()));
57
58 loop {
59 executor.run();
60 cortex_m::asm::wfe();
61 }
62}
diff --git a/examples/src/bin/multiprio.rs b/examples/src/bin/multiprio.rs
deleted file mode 100644
index c821e3dba..000000000
--- a/examples/src/bin/multiprio.rs
+++ /dev/null
@@ -1,176 +0,0 @@
1//! This example showcases how to create multiple Executor instances to run tasks at
2//! different priority levels.
3//!
4//! Low priority executor runs in thread mode (not interrupt), and uses `sev` for signaling
5//! there's work in the queue, and `wfe` for waiting for work.
6//!
7//! Medium and high priority executors run in two interrupts with different priorities.
8//! Signaling work is done by pending the interrupt. No "waiting" needs to be done explicitly, since
9//! when there's work the interrupt will trigger and run the executor.
10//!
11//! Sample output below. Note that high priority ticks can interrupt everything else, and
12//! medium priority computations can interrupt low priority computations, making them to appear
13//! to take significantly longer time.
14//!
15//! ```not_rust
16//! [med] Starting long computation
17//! [med] done in 992 ms
18//! [high] tick!
19//! [low] Starting long computation
20//! [med] Starting long computation
21//! [high] tick!
22//! [high] tick!
23//! [med] done in 993 ms
24//! [med] Starting long computation
25//! [high] tick!
26//! [high] tick!
27//! [med] done in 993 ms
28//! [low] done in 3972 ms
29//! [med] Starting long computation
30//! [high] tick!
31//! [high] tick!
32//! [med] done in 993 ms
33//! ```
34//!
35//! For comparison, try changing the code so all 3 tasks get spawned on the low priority executor.
36//! You will get an output like the following. Note that no computation is ever interrupted.
37//!
38//! ```not_rust
39//! [high] tick!
40//! [med] Starting long computation
41//! [med] done in 496 ms
42//! [low] Starting long computation
43//! [low] done in 992 ms
44//! [med] Starting long computation
45//! [med] done in 496 ms
46//! [high] tick!
47//! [low] Starting long computation
48//! [low] done in 992 ms
49//! [high] tick!
50//! [med] Starting long computation
51//! [med] done in 496 ms
52//! [high] tick!
53//! ```
54//!
55
56#![no_std]
57#![no_main]
58#![feature(type_alias_impl_trait)]
59
60#[path = "../example_common.rs"]
61mod example_common;
62use example_common::*;
63
64use cortex_m::peripheral::NVIC;
65use cortex_m_rt::entry;
66use defmt::panic;
67use nrf52840_hal::clocks;
68
69use embassy::executor::{task, Executor};
70use embassy::time::{Duration, Instant, Timer};
71use embassy::util::Forever;
72use embassy_nrf::{interrupt, pac, rtc};
73
74#[task]
75async fn run_high() {
76 loop {
77 info!(" [high] tick!");
78 Timer::after(Duration::from_ticks(27374)).await;
79 }
80}
81
82#[task]
83async fn run_med() {
84 loop {
85 let start = Instant::now();
86 info!(" [med] Starting long computation");
87
88 // Spin-wait to simulate a long CPU computation
89 cortex_m::asm::delay(32_000_000); // ~1 second
90
91 let end = Instant::now();
92 let ms = end.duration_since(start).as_ticks() / 33;
93 info!(" [med] done in {:u64} ms", ms);
94
95 Timer::after(Duration::from_ticks(23421)).await;
96 }
97}
98
99#[task]
100async fn run_low() {
101 loop {
102 let start = Instant::now();
103 info!("[low] Starting long computation");
104
105 // Spin-wait to simulate a long CPU computation
106 cortex_m::asm::delay(64_000_000); // ~2 seconds
107
108 let end = Instant::now();
109 let ms = end.duration_since(start).as_ticks() / 33;
110 info!("[low] done in {:u64} ms", ms);
111
112 Timer::after(Duration::from_ticks(32983)).await;
113 }
114}
115
116static RTC: Forever<rtc::RTC<pac::RTC1>> = Forever::new();
117static ALARM_LOW: Forever<rtc::Alarm<pac::RTC1>> = Forever::new();
118static EXECUTOR_LOW: Forever<Executor> = Forever::new();
119static ALARM_MED: Forever<rtc::Alarm<pac::RTC1>> = Forever::new();
120static EXECUTOR_MED: Forever<Executor> = Forever::new();
121static ALARM_HIGH: Forever<rtc::Alarm<pac::RTC1>> = Forever::new();
122static EXECUTOR_HIGH: Forever<Executor> = Forever::new();
123
124#[entry]
125fn main() -> ! {
126 info!("Hello World!");
127
128 let p = unwrap!(embassy_nrf::pac::Peripherals::take());
129
130 clocks::Clocks::new(p.CLOCK)
131 .enable_ext_hfosc()
132 .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass)
133 .start_lfclk();
134
135 let rtc = RTC.put(rtc::RTC::new(p.RTC1, interrupt::take!(RTC1)));
136 rtc.start();
137 unsafe { embassy::time::set_clock(rtc) };
138
139 let alarm_low = ALARM_LOW.put(rtc.alarm0());
140 let executor_low = EXECUTOR_LOW.put(Executor::new_with_alarm(alarm_low, cortex_m::asm::sev));
141 let alarm_med = ALARM_MED.put(rtc.alarm1());
142 let executor_med = EXECUTOR_MED.put(Executor::new_with_alarm(alarm_med, || {
143 NVIC::pend(interrupt::SWI0_EGU0)
144 }));
145 let alarm_high = ALARM_HIGH.put(rtc.alarm2());
146 let executor_high = EXECUTOR_HIGH.put(Executor::new_with_alarm(alarm_high, || {
147 NVIC::pend(interrupt::SWI1_EGU1)
148 }));
149
150 unsafe {
151 let mut nvic: NVIC = core::mem::transmute(());
152 nvic.set_priority(interrupt::SWI0_EGU0, 7 << 5);
153 nvic.set_priority(interrupt::SWI1_EGU1, 6 << 5);
154 NVIC::unmask(interrupt::SWI0_EGU0);
155 NVIC::unmask(interrupt::SWI1_EGU1);
156 }
157
158 unwrap!(executor_low.spawn(run_low()));
159 unwrap!(executor_med.spawn(run_med()));
160 unwrap!(executor_high.spawn(run_high()));
161
162 loop {
163 executor_low.run();
164 cortex_m::asm::wfe();
165 }
166}
167
168#[interrupt]
169unsafe fn SWI0_EGU0() {
170 EXECUTOR_MED.steal().run()
171}
172
173#[interrupt]
174unsafe fn SWI1_EGU1() {
175 EXECUTOR_HIGH.steal().run()
176}
diff --git a/examples/src/bin/qspi.rs b/examples/src/bin/qspi.rs
deleted file mode 100644
index a7d47f79c..000000000
--- a/examples/src/bin/qspi.rs
+++ /dev/null
@@ -1,134 +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::{assert_eq, panic, *};
11use nrf52840_hal::gpio;
12
13use embassy::executor::{task, Executor};
14use embassy::flash::Flash;
15use embassy::util::Forever;
16use embassy_nrf::{interrupt, qspi};
17
18const PAGE_SIZE: usize = 4096;
19
20// Workaround for alignment requirements.
21// Nicer API will probably come in the future.
22#[repr(C, align(4))]
23struct AlignedBuf([u8; 4096]);
24
25#[task]
26async fn run() {
27 let p = unwrap!(embassy_nrf::pac::Peripherals::take());
28
29 let port0 = gpio::p0::Parts::new(p.P0);
30
31 let pins = qspi::Pins {
32 csn: port0
33 .p0_17
34 .into_push_pull_output(gpio::Level::High)
35 .degrade(),
36 sck: port0
37 .p0_19
38 .into_push_pull_output(gpio::Level::High)
39 .degrade(),
40 io0: port0
41 .p0_20
42 .into_push_pull_output(gpio::Level::High)
43 .degrade(),
44 io1: port0
45 .p0_21
46 .into_push_pull_output(gpio::Level::High)
47 .degrade(),
48 io2: Some(
49 port0
50 .p0_22
51 .into_push_pull_output(gpio::Level::High)
52 .degrade(),
53 ),
54 io3: Some(
55 port0
56 .p0_23
57 .into_push_pull_output(gpio::Level::High)
58 .degrade(),
59 ),
60 };
61
62 let config = qspi::Config {
63 pins,
64 read_opcode: qspi::ReadOpcode::READ4IO,
65 write_opcode: qspi::WriteOpcode::PP4IO,
66 xip_offset: 0,
67 write_page_size: qspi::WritePageSize::_256BYTES,
68 deep_power_down: None,
69 };
70
71 let irq = interrupt::take!(QSPI);
72 let mut q = qspi::Qspi::new(p.QSPI, irq, config);
73
74 let mut id = [1; 3];
75 q.custom_instruction(0x9F, &[], &mut id).await.unwrap();
76 info!("id: {:[u8]}", id);
77
78 // Read status register
79 let mut status = [0; 1];
80 q.custom_instruction(0x05, &[], &mut status).await.unwrap();
81
82 info!("status: {:?}", status[0]);
83
84 if status[0] & 0x40 == 0 {
85 status[0] |= 0x40;
86
87 q.custom_instruction(0x01, &status, &mut []).await.unwrap();
88
89 info!("enabled quad in status");
90 }
91
92 let mut buf = AlignedBuf([0u8; PAGE_SIZE]);
93
94 let pattern = |a: u32| (a ^ (a >> 8) ^ (a >> 16) ^ (a >> 24)) as u8;
95
96 for i in 0..8 {
97 info!("page {:?}: erasing... ", i);
98 q.erase(i * PAGE_SIZE).await.unwrap();
99
100 for j in 0..PAGE_SIZE {
101 buf.0[j] = pattern((j + i * PAGE_SIZE) as u32);
102 }
103
104 info!("programming...");
105 q.write(i * PAGE_SIZE, &buf.0).await.unwrap();
106 }
107
108 for i in 0..8 {
109 info!("page {:?}: reading... ", i);
110 q.read(i * PAGE_SIZE, &mut buf.0).await.unwrap();
111
112 info!("verifying...");
113 for j in 0..PAGE_SIZE {
114 assert_eq!(buf.0[j], pattern((j + i * PAGE_SIZE) as u32));
115 }
116 }
117
118 info!("done!")
119}
120
121static EXECUTOR: Forever<Executor> = Forever::new();
122
123#[entry]
124fn main() -> ! {
125 info!("Hello World!");
126
127 let executor = EXECUTOR.put(Executor::new(cortex_m::asm::sev));
128 unwrap!(executor.spawn(run()));
129
130 loop {
131 executor.run();
132 cortex_m::asm::wfe();
133 }
134}
diff --git a/examples/src/bin/rtc_async.rs b/examples/src/bin/rtc_async.rs
deleted file mode 100644
index dcdeb7049..000000000
--- a/examples/src/bin/rtc_async.rs
+++ /dev/null
@@ -1,65 +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 core::mem::MaybeUninit;
10use cortex_m_rt::entry;
11use defmt::panic;
12use embassy::executor::{task, Executor};
13use embassy::time::{Clock, Duration, Timer};
14use embassy::util::Forever;
15use embassy_nrf::pac;
16use embassy_nrf::{interrupt, rtc};
17use nrf52840_hal::clocks;
18
19#[task]
20async fn run1() {
21 loop {
22 info!("BIG INFREQUENT TICK");
23 Timer::after(Duration::from_ticks(64000)).await;
24 }
25}
26
27#[task]
28async fn run2() {
29 loop {
30 info!("tick");
31 Timer::after(Duration::from_ticks(13000)).await;
32 }
33}
34
35static RTC: Forever<rtc::RTC<pac::RTC1>> = Forever::new();
36static ALARM: Forever<rtc::Alarm<pac::RTC1>> = Forever::new();
37static EXECUTOR: Forever<Executor> = Forever::new();
38
39#[entry]
40fn main() -> ! {
41 info!("Hello World!");
42
43 let p = unwrap!(embassy_nrf::pac::Peripherals::take());
44
45 clocks::Clocks::new(p.CLOCK)
46 .enable_ext_hfosc()
47 .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass)
48 .start_lfclk();
49
50 let rtc = RTC.put(rtc::RTC::new(p.RTC1, interrupt::take!(RTC1)));
51 rtc.start();
52
53 unsafe { embassy::time::set_clock(rtc) };
54
55 let alarm = ALARM.put(rtc.alarm0());
56 let executor = EXECUTOR.put(Executor::new_with_alarm(alarm, cortex_m::asm::sev));
57
58 unwrap!(executor.spawn(run1()));
59 unwrap!(executor.spawn(run2()));
60
61 loop {
62 executor.run();
63 cortex_m::asm::wfe();
64 }
65}
diff --git a/examples/src/bin/rtc_raw.rs b/examples/src/bin/rtc_raw.rs
deleted file mode 100644
index 438585460..000000000
--- a/examples/src/bin/rtc_raw.rs
+++ /dev/null
@@ -1,63 +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 core::mem::MaybeUninit;
10use cortex_m_rt::entry;
11use defmt::panic;
12use embassy::time::{Alarm, Clock};
13use embassy_nrf::{interrupt, rtc};
14use nrf52840_hal::clocks;
15
16static mut RTC: MaybeUninit<rtc::RTC<embassy_nrf::pac::RTC1>> = MaybeUninit::uninit();
17
18#[entry]
19fn main() -> ! {
20 info!("Hello World!");
21
22 let p = unwrap!(embassy_nrf::pac::Peripherals::take());
23
24 clocks::Clocks::new(p.CLOCK)
25 .enable_ext_hfosc()
26 .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass)
27 .start_lfclk();
28
29 let irq = interrupt::take!(RTC1);
30
31 let rtc: &'static _ = unsafe {
32 let ptr = RTC.as_mut_ptr();
33 ptr.write(rtc::RTC::new(p.RTC1, irq));
34 &*ptr
35 };
36
37 let alarm = rtc.alarm0();
38
39 rtc.start();
40
41 alarm.set_callback(|| info!("ALARM TRIGGERED"));
42 alarm.set(53719);
43
44 info!("initialized!");
45
46 let mut val = 0;
47 let mut printval = 0;
48 loop {
49 let val2 = rtc.now();
50 if val2 < val {
51 info!(
52 "timer ran backwards! {:u32} -> {:u32}",
53 val as u32, val2 as u32
54 );
55 }
56 val = val2;
57
58 if val > printval + 32768 {
59 info!("tick {:u32}", val as u32);
60 printval = val;
61 }
62 }
63}
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}