From 660fc4f7602c0be689e2c6cb4b20e55a26127636 Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Wed, 26 Nov 2025 14:35:18 +0100 Subject: feat: support nrf54 GRTC as time-driver * Refactor GRTC peripheral splitting it into multiple channels * Reserve channel 1 for time-driver if enabled * Implement time-driver using GRTC (RTC peripheral is now removed). * Add timer example to nrf54l15 --- examples/nrf54l15/Cargo.toml | 2 +- examples/nrf54l15/src/bin/rtc.rs | 56 -------------------------------------- examples/nrf54l15/src/bin/timer.rs | 30 ++++++++++++++++++++ 3 files changed, 31 insertions(+), 57 deletions(-) delete mode 100644 examples/nrf54l15/src/bin/rtc.rs create mode 100644 examples/nrf54l15/src/bin/timer.rs (limited to 'examples') diff --git a/examples/nrf54l15/Cargo.toml b/examples/nrf54l15/Cargo.toml index 4ef77279f..f34df0f26 100644 --- a/examples/nrf54l15/Cargo.toml +++ b/examples/nrf54l15/Cargo.toml @@ -10,7 +10,7 @@ embassy-futures = { version = "0.1.2", path = "../../embassy-futures" } embassy-executor = { version = "0.9.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "executor-interrupt", "defmt"] } embassy-time = { version = "0.5.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] } embassy-sync = { version = "0.7.2", path = "../../embassy-sync", features = ["defmt"] } -embassy-nrf = { version = "0.8.0", path = "../../embassy-nrf", features = ["defmt", "nrf54l15-app-s", "time-driver-rtc1", "gpiote", "unstable-pac", "time"] } +embassy-nrf = { version = "0.8.0", path = "../../embassy-nrf", features = ["defmt", "nrf54l15-app-s", "time-driver-grtc", "gpiote", "unstable-pac", "time"] } embedded-io = { version = "0.6.0", features = ["defmt-03"] } embedded-io-async = { version = "0.6.1", features = ["defmt-03"] } diff --git a/examples/nrf54l15/src/bin/rtc.rs b/examples/nrf54l15/src/bin/rtc.rs deleted file mode 100644 index a45aaca52..000000000 --- a/examples/nrf54l15/src/bin/rtc.rs +++ /dev/null @@ -1,56 +0,0 @@ -#![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::Mutex; -use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; -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>>> = Mutex::new(RefCell::new(None)); - -#[embassy_executor::main] -async fn main(_spawner: Spawner) { - defmt::println!("nRF54L15 RTC example"); - let p = embassy_nrf::init(Default::default()); - let mut led = Output::new(p.P2_09, Level::High, OutputDrive::Standard); - // Counter resolution is 125 ms. - let mut rtc = Rtc::new(p.RTC10, (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 RTC10() { - // 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); - }); -} diff --git a/examples/nrf54l15/src/bin/timer.rs b/examples/nrf54l15/src/bin/timer.rs new file mode 100644 index 000000000..68acc91c1 --- /dev/null +++ b/examples/nrf54l15/src/bin/timer.rs @@ -0,0 +1,30 @@ +#![no_std] +#![no_main] + +use defmt::{info, unwrap}; +use embassy_executor::Spawner; +use embassy_time::Timer; +use {defmt_rtt as _, panic_probe as _}; + +#[embassy_executor::task] +async fn run1() { + loop { + info!("BIG INFREQUENT TICK"); + Timer::after_secs(10).await; + } +} + +#[embassy_executor::task] +async fn run2() { + loop { + info!("tick"); + Timer::after_secs(1).await; + } +} + +#[embassy_executor::main] +async fn main(spawner: Spawner) { + let _p = embassy_nrf::init(Default::default()); + spawner.spawn(unwrap!(run1())); + spawner.spawn(unwrap!(run2())); +} -- cgit