blob: 9d475df7f85a58466f312db63f79e76dd069ec1e (
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
|
#![no_std]
#![no_main]
use core::cell::RefCell;
use embassy_executor::Spawner;
use embassy_nrf::gpio::{Level, Output, OutputDrive};
use embassy_nrf::interrupt;
use embassy_nrf::rtc::Rtc;
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::blocking_mutex::Mutex;
use portable_atomic::AtomicU64;
use {defmt_rtt as _, panic_probe as _};
// 64 bit counter which will never overflow.
static TICK_COUNTER: AtomicU64 = AtomicU64::new(0);
static RTC: Mutex<CriticalSectionRawMutex, RefCell<Option<Rtc<'static>>>> = Mutex::new(RefCell::new(None));
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
defmt::println!("nRF52840 RTC example");
let p = embassy_nrf::init(Default::default());
let mut led = Output::new(p.P0_13, Level::High, OutputDrive::Standard);
// Counter resolution is 125 ms.
let mut rtc = Rtc::new(p.RTC0, (1 << 12) - 1).unwrap();
rtc.enable_interrupt(embassy_nrf::rtc::Interrupt::Tick, true);
rtc.enable_event(embassy_nrf::rtc::Interrupt::Tick);
rtc.enable();
RTC.lock(|r| {
let mut rtc_borrow = r.borrow_mut();
*rtc_borrow = Some(rtc);
});
let mut last_counter_val = 0;
loop {
let current = TICK_COUNTER.load(core::sync::atomic::Ordering::Relaxed);
if current != last_counter_val {
led.toggle();
last_counter_val = current;
}
}
}
#[interrupt]
fn RTC0() {
// For 64-bit, we do not need to worry about overflowing, at least not for realistic program
// lifetimes.
TICK_COUNTER.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
RTC.lock(|r| {
let mut rtc_borrow = r.borrow_mut();
rtc_borrow
.as_mut()
.unwrap()
.reset_event(embassy_nrf::rtc::Interrupt::Tick);
});
}
|