aboutsummaryrefslogtreecommitdiff
path: root/examples/src/bin/rtc_async.rs
blob: 838a0faef4e57d039b14ac895cfa79a3854fd436 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]

#[path = "../example_common.rs"]
mod example_common;
use example_common::*;

use core::mem::MaybeUninit;
use cortex_m_rt::entry;
use embassy_nrf::rtc;
use futures_intrusive::timer::{Clock, LocalTimer, LocalTimerService};
use nrf52840_hal::clocks;
use static_executor::{task, Executor};

struct RtcClock<T>(rtc::RTC<T>);

impl<T: rtc::Instance> Clock for RtcClock<T> {
    fn now(&self) -> u64 {
        self.0.now()
    }
}

#[task]
async fn run1(rtc: &'static rtc::RTC<embassy_nrf::pac::RTC1>, timer: &'static LocalTimerService) {
    loop {
        info!("tick 1");
        timer.deadline(rtc.now() + 64000).await;
    }
}

#[task]
async fn run2(rtc: &'static rtc::RTC<embassy_nrf::pac::RTC1>, timer: &'static LocalTimerService) {
    loop {
        info!("tick 2");
        timer.deadline(rtc.now() + 23000).await;
    }
}

static EXECUTOR: Executor = Executor::new(cortex_m::asm::sev);
static mut RTC: MaybeUninit<RtcClock<embassy_nrf::pac::RTC1>> = MaybeUninit::uninit();
static mut TIMER: MaybeUninit<LocalTimerService> = MaybeUninit::uninit();

#[entry]
fn main() -> ! {
    info!("Hello World!");

    let p = embassy_nrf::pac::Peripherals::take().dewrap();

    clocks::Clocks::new(p.CLOCK)
        .enable_ext_hfosc()
        .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass)
        .start_lfclk();

    let rtc: &'static _ = unsafe {
        let ptr = RTC.as_mut_ptr();
        ptr.write(RtcClock(rtc::RTC::new(p.RTC1)));
        &*ptr
    };

    rtc.0.start();

    let timer: &'static _ = unsafe {
        let ptr = TIMER.as_mut_ptr();
        ptr.write(LocalTimerService::new(rtc));
        &*ptr
    };

    unsafe {
        EXECUTOR.spawn(run1(&rtc.0, timer)).dewrap();
        EXECUTOR.spawn(run2(&rtc.0, timer)).dewrap();

        loop {
            timer.check_expirations();

            EXECUTOR.run();

            match timer.next_expiration() {
                // If this is in the past, set_alarm will immediately trigger the alarm,
                // which will make the wfe immediately return so we do another loop iteration.
                Some(at) => rtc.0.set_alarm(at, cortex_m::asm::sev),
                None => rtc.0.clear_alarm(),
            }

            cortex_m::asm::wfe();
        }
    }
}