aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2020-09-24 22:46:00 +0200
committerDario Nieuwenhuis <[email protected]>2020-09-24 22:46:00 +0200
commitafcf7255196c4362df72b9e675813cf40876750a (patch)
tree104054850f72a455ab858cfdf2c952804ee969ba /examples
parent82e5e3c45f16147863fae0187bab1d9122439280 (diff)
Add rtc_async example
Diffstat (limited to 'examples')
-rw-r--r--examples/Cargo.toml1
-rw-r--r--examples/src/bin/rtc_async.rs88
2 files changed, 89 insertions, 0 deletions
diff --git a/examples/Cargo.toml b/examples/Cargo.toml
index 5194e4f44..4e81d1daa 100644
--- a/examples/Cargo.toml
+++ b/examples/Cargo.toml
@@ -28,3 +28,4 @@ embassy = { version = "0.1.0", path = "../embassy" }
28embassy-nrf = { version = "0.1.0", path = "../embassy-nrf", features = ["defmt-trace", "52840"] } 28embassy-nrf = { version = "0.1.0", path = "../embassy-nrf", features = ["defmt-trace", "52840"] }
29static-executor = { version = "0.1.0", features=["defmt"]} 29static-executor = { version = "0.1.0", features=["defmt"]}
30futures = { version = "0.3.5", default-features = false } 30futures = { version = "0.3.5", default-features = false }
31futures-intrusive = { version = "0.3.1", default-features = false } \ No newline at end of file
diff --git a/examples/src/bin/rtc_async.rs b/examples/src/bin/rtc_async.rs
new file mode 100644
index 000000000..838a0faef
--- /dev/null
+++ b/examples/src/bin/rtc_async.rs
@@ -0,0 +1,88 @@
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 embassy_nrf::rtc;
12use futures_intrusive::timer::{Clock, LocalTimer, LocalTimerService};
13use nrf52840_hal::clocks;
14use static_executor::{task, Executor};
15
16struct RtcClock<T>(rtc::RTC<T>);
17
18impl<T: rtc::Instance> Clock for RtcClock<T> {
19 fn now(&self) -> u64 {
20 self.0.now()
21 }
22}
23
24#[task]
25async fn run1(rtc: &'static rtc::RTC<embassy_nrf::pac::RTC1>, timer: &'static LocalTimerService) {
26 loop {
27 info!("tick 1");
28 timer.deadline(rtc.now() + 64000).await;
29 }
30}
31
32#[task]
33async fn run2(rtc: &'static rtc::RTC<embassy_nrf::pac::RTC1>, timer: &'static LocalTimerService) {
34 loop {
35 info!("tick 2");
36 timer.deadline(rtc.now() + 23000).await;
37 }
38}
39
40static EXECUTOR: Executor = Executor::new(cortex_m::asm::sev);
41static mut RTC: MaybeUninit<RtcClock<embassy_nrf::pac::RTC1>> = MaybeUninit::uninit();
42static mut TIMER: MaybeUninit<LocalTimerService> = MaybeUninit::uninit();
43
44#[entry]
45fn main() -> ! {
46 info!("Hello World!");
47
48 let p = embassy_nrf::pac::Peripherals::take().dewrap();
49
50 clocks::Clocks::new(p.CLOCK)
51 .enable_ext_hfosc()
52 .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass)
53 .start_lfclk();
54
55 let rtc: &'static _ = unsafe {
56 let ptr = RTC.as_mut_ptr();
57 ptr.write(RtcClock(rtc::RTC::new(p.RTC1)));
58 &*ptr
59 };
60
61 rtc.0.start();
62
63 let timer: &'static _ = unsafe {
64 let ptr = TIMER.as_mut_ptr();
65 ptr.write(LocalTimerService::new(rtc));
66 &*ptr
67 };
68
69 unsafe {
70 EXECUTOR.spawn(run1(&rtc.0, timer)).dewrap();
71 EXECUTOR.spawn(run2(&rtc.0, timer)).dewrap();
72
73 loop {
74 timer.check_expirations();
75
76 EXECUTOR.run();
77
78 match timer.next_expiration() {
79 // If this is in the past, set_alarm will immediately trigger the alarm,
80 // which will make the wfe immediately return so we do another loop iteration.
81 Some(at) => rtc.0.set_alarm(at, cortex_m::asm::sev),
82 None => rtc.0.clear_alarm(),
83 }
84
85 cortex_m::asm::wfe();
86 }
87 }
88}