diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/nrf52840/Cargo.toml | 1 | ||||
| -rw-r--r-- | examples/nrf52840/src/bin/rtc.rs | 57 |
2 files changed, 58 insertions, 0 deletions
diff --git a/examples/nrf52840/Cargo.toml b/examples/nrf52840/Cargo.toml index 452e83b7e..ca3c6f863 100644 --- a/examples/nrf52840/Cargo.toml +++ b/examples/nrf52840/Cargo.toml | |||
| @@ -35,6 +35,7 @@ embedded-hal-async = { version = "1.0" } | |||
| 35 | embedded-hal-bus = { version = "0.1", features = ["async"] } | 35 | embedded-hal-bus = { version = "0.1", features = ["async"] } |
| 36 | num-integer = { version = "0.1.45", default-features = false } | 36 | num-integer = { version = "0.1.45", default-features = false } |
| 37 | microfft = "0.5.0" | 37 | microfft = "0.5.0" |
| 38 | portable-atomic = "1" | ||
| 38 | 39 | ||
| 39 | [profile.release] | 40 | [profile.release] |
| 40 | debug = 2 | 41 | debug = 2 |
diff --git a/examples/nrf52840/src/bin/rtc.rs b/examples/nrf52840/src/bin/rtc.rs new file mode 100644 index 000000000..a3df7da14 --- /dev/null +++ b/examples/nrf52840/src/bin/rtc.rs | |||
| @@ -0,0 +1,57 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use core::cell::RefCell; | ||
| 5 | |||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_nrf::gpio::{Level, Output, OutputDrive}; | ||
| 8 | use embassy_nrf::interrupt; | ||
| 9 | use embassy_nrf::rtc::Rtc; | ||
| 10 | use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; | ||
| 11 | use embassy_sync::blocking_mutex::Mutex; | ||
| 12 | use portable_atomic::AtomicU64; | ||
| 13 | use {defmt_rtt as _, panic_probe as _}; | ||
| 14 | |||
| 15 | // 64 bit counter which will never overflow. | ||
| 16 | static TICK_COUNTER: AtomicU64 = AtomicU64::new(0); | ||
| 17 | static RTC: Mutex<CriticalSectionRawMutex, RefCell<Option<Rtc<'static, embassy_nrf::peripherals::RTC0>>>> = | ||
| 18 | Mutex::new(RefCell::new(None)); | ||
| 19 | |||
| 20 | #[embassy_executor::main] | ||
| 21 | async fn main(_spawner: Spawner) { | ||
| 22 | defmt::println!("nRF52840 RTC example"); | ||
| 23 | let p = embassy_nrf::init(Default::default()); | ||
| 24 | let mut led = Output::new(p.P0_13, Level::High, OutputDrive::Standard); | ||
| 25 | // Counter resolution is 125 ms. | ||
| 26 | let mut rtc = Rtc::new(p.RTC0, (1 << 12) - 1).unwrap(); | ||
| 27 | rtc.enable_interrupt(embassy_nrf::rtc::Interrupt::Tick, true); | ||
| 28 | rtc.enable_event(embassy_nrf::rtc::Interrupt::Tick); | ||
| 29 | rtc.enable(); | ||
| 30 | RTC.lock(|r| { | ||
| 31 | let mut rtc_borrow = r.borrow_mut(); | ||
| 32 | *rtc_borrow = Some(rtc); | ||
| 33 | }); | ||
| 34 | |||
| 35 | let mut last_counter_val = 0; | ||
| 36 | loop { | ||
| 37 | let current = TICK_COUNTER.load(core::sync::atomic::Ordering::Relaxed); | ||
| 38 | if current != last_counter_val { | ||
| 39 | led.toggle(); | ||
| 40 | last_counter_val = current; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | #[interrupt] | ||
| 46 | fn RTC0() { | ||
| 47 | // For 64-bit, we do not need to worry about overflowing, at least not for realistic program | ||
| 48 | // lifetimes. | ||
| 49 | TICK_COUNTER.fetch_add(1, core::sync::atomic::Ordering::Relaxed); | ||
| 50 | RTC.lock(|r| { | ||
| 51 | let mut rtc_borrow = r.borrow_mut(); | ||
| 52 | rtc_borrow | ||
| 53 | .as_mut() | ||
| 54 | .unwrap() | ||
| 55 | .reset_event(embassy_nrf::rtc::Interrupt::Tick); | ||
| 56 | }); | ||
| 57 | } | ||
