From 539188affe8a9f9cf79890002cac726b54b4a819 Mon Sep 17 00:00:00 2001 From: mschnell Date: Wed, 5 Feb 2025 15:30:18 +0100 Subject: [PATCH 1/8] enable timer1 and aot --- embassy-rp/src/time_driver.rs | 268 ++++++++++++++++++++++++++++++++-- 1 file changed, 259 insertions(+), 9 deletions(-) diff --git a/embassy-rp/src/time_driver.rs b/embassy-rp/src/time_driver.rs index d598287a91..e5ab054724 100644 --- a/embassy-rp/src/time_driver.rs +++ b/embassy-rp/src/time_driver.rs @@ -6,10 +6,17 @@ use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; use embassy_sync::blocking_mutex::Mutex; use embassy_time_driver::Driver; use embassy_time_queue_utils::Queue; + +#[cfg(all(feature = "_rp235x", feature = "time-driver-timer1"))] +use embassy_rp::clocks; +#[cfg(all(feature = "_rp235x", feature = "time-driver-aot"))] +use pac::POWMAN as TIMER; #[cfg(feature = "rp2040")] use pac::TIMER; -#[cfg(feature = "_rp235x")] +#[cfg(all(feature = "_rp235x", feature = "time-driver-timer0"))] use pac::TIMER0 as TIMER; +#[cfg(all(feature = "_rp235x", feature = "time-driver-timer1"))] +use pac::TIMER1 as TIMER; use crate::interrupt::InterruptExt; use crate::{interrupt, pac}; @@ -32,6 +39,7 @@ embassy_time_driver::time_driver_impl!(static DRIVER: TimerDriver = TimerDriver{ }); impl Driver for TimerDriver { + #[cfg(not(feature = "time-driver-aot"))] fn now(&self) -> u64 { loop { let hi = TIMER.timerawh().read(); @@ -43,6 +51,20 @@ impl Driver for TimerDriver { } } + #[cfg(all(feature = "_rp235x", feature = "time-driver-aot"))] + fn now(&self) -> u64 { + use timer_aon::TICKS_PER_LPOSC_TICK; + let now_lpo = loop { + let hi = TIMER.read_time_upper().read(); + let lo = TIMER.read_time_lower().read(); + let hi2 = TIMER.read_time_upper().read(); + if hi == hi2 { + break (hi as u64) << 32 | (lo as u64); + } + }; + now_lpo * TICKS_PER_LPOSC_TICK + } + fn schedule_wake(&self, at: u64, waker: &core::task::Waker) { critical_section::with(|cs| { let mut queue = self.queue.borrow(cs).borrow_mut(); @@ -58,6 +80,7 @@ impl Driver for TimerDriver { } impl TimerDriver { + #[cfg(not(feature = "time-driver-aot"))] fn set_alarm(&self, cs: CriticalSection, timestamp: u64) -> bool { let n = 0; let alarm = &self.alarms.borrow(cs); @@ -83,6 +106,7 @@ impl TimerDriver { } } + #[cfg(not(feature = "time-driver-aot"))] fn check_alarm(&self) { let n = 0; critical_section::with(|cs| { @@ -102,32 +126,77 @@ impl TimerDriver { } fn trigger_alarm(&self, cs: CriticalSection) { - let mut next = self.queue.borrow(cs).borrow_mut().next_expiration(self.now()); + let mut next = self + .queue + .borrow(cs) + .borrow_mut() + .next_expiration(self.now()); while !self.set_alarm(cs, next) { - next = self.queue.borrow(cs).borrow_mut().next_expiration(self.now()); + next = self + .queue + .borrow(cs) + .borrow_mut() + .next_expiration(self.now()); } } } /// safety: must be called exactly once at bootup pub unsafe fn init() { + #[cfg(all(feature = "_rp235x", feature = "time-driver-timer1"))] + { + let timer1_cycles = clocks::clk_ref_freq() / embassy_time_driver::TICK_HZ as u32; + assert!(timer1_cycles < 512); + assert!(timer1_cycles > 0); + assert!(timer1_cycles * embassy_time_driver::TICK_HZ as u32 == clocks::clk_ref_freq()); + pac::TICKS.timer1_cycles().write(|w| w.0 = timer1_cycles); + pac::TICKS.timer1_ctrl().write(|w| w.set_enable(true)); + }; + + #[cfg(all(feature = "_rp235x", feature = "time-driver-aot"))] + timer_aon::initialize_aon_timer(); + // init alarms critical_section::with(|cs| { let alarm = DRIVER.alarms.borrow(cs); alarm.timestamp.set(u64::MAX); }); - // enable irq - TIMER.inte().write(|w| { - w.set_alarm(0, true); - }); #[cfg(feature = "rp2040")] { + // enable irq + TIMER.inte().write(|w| { + w.set_alarm(0, true); + }); interrupt::TIMER_IRQ_0.enable(); } #[cfg(feature = "_rp235x")] { - interrupt::TIMER0_IRQ_0.enable(); + #[cfg(feature = "time-driver-timer0")] + { + // enable irq + TIMER.inte().write(|w| { + w.set_alarm(0, true); + }); + interrupt::TIMER0_IRQ_0.enable(); + } + #[cfg(feature = "time-driver-timer1")] + { + // enable irq + TIMER.inte().write(|w| { + w.set_alarm(0, true); + }); + interrupt::TIMER1_IRQ_0.enable(); + } + #[cfg(feature = "time-driver-aot")] + { + use timer_aon::PowmanIntValue; + let inte = pac::POWMAN.inte(); + let mut inte_value = inte.read(); + inte_value.set_timer(true); + TIMER.inte().write_value_key(inte_value); + interrupt::POWMAN_IRQ_TIMER.enable(); + } } } @@ -137,8 +206,189 @@ fn TIMER_IRQ_0() { DRIVER.check_alarm() } -#[cfg(all(feature = "rt", feature = "_rp235x"))] +#[cfg(all(feature = "rt", feature = "_rp235x", feature = "time-driver-timer0"))] #[interrupt] fn TIMER0_IRQ_0() { DRIVER.check_alarm() } + +#[cfg(all(feature = "rt", feature = "_rp235x", feature = "time-driver-timer1"))] +#[interrupt] +fn TIMER1_IRQ_0() { + DRIVER.check_alarm() +} + +#[cfg(all(feature = "rt", feature = "_rp235x", feature = "time-driver-aot"))] +#[interrupt] +fn POWMAN_IRQ_TIMER() { + DRIVER.check_alarm() +} + +#[cfg(all(feature = "_rp235x", feature = "time-driver-aot"))] +mod timer_aon { + use super::TIMER; + use embassy_rp::pac::{powman, POWMAN}; + use powman::regs; + use regs::{AlarmTime15to0, AlarmTime31to16, AlarmTime47to32, AlarmTime63to48, Int, Timer}; + + const POWMAN_KEY: u32 = 0x5afeu32 << 16; + pub const LPOSC_TICK: u64 = 1000; + pub const TICKS_PER_LPOSC_TICK: u64 = embassy_time_driver::TICK_HZ / LPOSC_TICK; + + /// [static_assert]: http://en.cppreference.com/w/cpp/language/static_assert + /// macro taken from crate "static assertion" v1.1.0 (copied to avoid dependancy) + macro_rules! const_assert { + ($x:expr $(,)?) => { + #[allow(unknown_lints, clippy::eq_op)] + const _: [(); 0 - !{ + const ASSERT: bool = $x; + ASSERT + } as usize] = []; + }; + } + // The Embassy system tick needs to be an integer multiple of the Low Power oscillator tick + const_assert!(TICKS_PER_LPOSC_TICK * LPOSC_TICK == embassy_time_driver::TICK_HZ); + + pub fn initialize_aon_timer() { + let timer_reg = POWMAN.timer(); + let mut reset_timer = regs::Timer(0); + reset_timer.set_nonsec_write(true); + timer_reg.write_value_key(reset_timer); + + let mut clear_timer = Timer(0); + clear_timer.set_nonsec_write(true); + clear_timer.set_clear(true); + timer_reg.write_value_key(clear_timer); + + let mut timer_reg_value = timer_reg.read(); + timer_reg_value.set_nonsec_write(true); + timer_reg_value.set_use_lposc(true); + timer_reg.write_value_key(timer_reg_value); + timer_reg_value.set_run(true); + timer_reg_value.set_alarm_enab(true); // enable alarm + timer_reg_value.set_pwrup_on_alarm(true); + timer_reg.write_value_key(timer_reg_value); + } + + use super::TimerDriver; + use critical_section::CriticalSection; + use embassy_rp::pac; + use embassy_time_driver::Driver; + impl TimerDriver { + pub fn set_aon_alarm_value(&self, timestamp_ticks: u64) { + let timestamp_lposc_ticks = timestamp_ticks / TICKS_PER_LPOSC_TICK; + + let timer_reg = TIMER.timer(); + let mut timer_reg_value = timer_reg.read(); + timer_reg_value.set_nonsec_write(true); + let timer_reg_value_o = timer_reg_value; + timer_reg_value.set_alarm_enab(false); // disable alarm + timer_reg.write_value_key(timer_reg_value); + + let timestamp_15to0 = ((timestamp_lposc_ticks >> 0) & 0xFFFF) as u16; + let timestamp_31to16 = ((timestamp_lposc_ticks >> 16) & 0xFFFF) as u16; + let timestamp_47to32 = ((timestamp_lposc_ticks >> 32) & 0xFFFF) as u16; + let timestamp_64to48 = ((timestamp_lposc_ticks >> 48) & 0xFFFF) as u16; + let mut a = AlarmTime15to0(POWMAN_KEY); + a.set_alarm_time_15to0(timestamp_15to0); + TIMER.alarm_time_15to0().write_value(a); + let mut a = AlarmTime31to16(POWMAN_KEY); + a.set_alarm_time_31to16(timestamp_31to16); + TIMER.alarm_time_31to16().write_value(a); + let mut a = AlarmTime47to32(POWMAN_KEY); + a.set_alarm_time_47to32(timestamp_47to32); + TIMER.alarm_time_47to32().write_value(a); + let mut a = AlarmTime63to48(POWMAN_KEY); + a.set_alarm_time_63to48(timestamp_64to48); + TIMER.alarm_time_63to48().write_value(a); + + timer_reg.write_value_key(timer_reg_value_o); + } + + pub fn check_alarm(&self) { + critical_section::with(|cs| { + let alarm = &self.alarms.borrow(cs); + let timestamp = alarm.timestamp.get(); + if timestamp <= self.now() { + self.trigger_alarm(cs) + } else { + // Not elapsed, arm it again. + self.set_aon_alarm_value(timestamp); + } + }); + + // clear the irq + let timer_reg = TIMER.timer(); + let mut timer_reg_value = timer_reg.read(); + timer_reg_value.set_nonsec_write(true); + timer_reg_value.set_alarm_enab(true); // enable alarm + timer_reg_value.set_alarm(true); // reset alarm + timer_reg.write_value_key(timer_reg_value); + + let inte = pac::POWMAN.inte(); + let mut inte_value = inte.read(); + inte_value.set_timer(true); + TIMER.inte().write_value_key(inte_value); + } + + pub fn set_alarm(&self, cs: CriticalSection, timestamp: u64) -> bool { + // timestamp in µSec due to the definition of the constant TICK_HZ: u64 = 1_000_000; in ticks.rs + + let alarm = &self.alarms.borrow(cs); + alarm.timestamp.set(timestamp); + + // Arm it. + self.set_aon_alarm_value(timestamp); + + let now = self.now(); + if timestamp <= now { + // If alarm timestamp has passed the alarm will not fire. + let timer_reg = TIMER.timer(); + // Disarm the alarm and return `false` to indicate that. + let mut timer_reg_value = timer_reg.read(); + timer_reg_value.set_nonsec_write(true); + // timer_reg_value.set_clear(); + timer_reg_value.set_alarm_enab(true); // enable alarm + timer_reg_value.set_pwrup_on_alarm(true); + timer_reg_value.set_alarm(true); // reset alarm (just in case) + timer_reg.write_value_key(timer_reg_value); + alarm.timestamp.set(u64::MAX); + false + } else { + true + } + } + } + + pub trait PowmanTimerValue { + fn write_value_key(&self, val: Timer); + } + + impl PowmanTimerValue for rp_pac::common::Reg + // where T: Copy, A: rp_pac::common::Write + where + A: rp_pac::common::Write, + { + #[inline(always)] + fn write_value_key(&self, val: Timer) { + let timer_val = rp_pac::powman::regs::Timer(val.0 | POWMAN_KEY); + self.write_value(timer_val); + } + } + + pub trait PowmanIntValue { + fn write_value_key(&self, val: Int); + } + + impl PowmanIntValue for rp_pac::common::Reg + // where T: Copy, A: rp_pac::common::Write + where + A: rp_pac::common::Write, + { + #[inline(always)] + fn write_value_key(&self, val: Int) { + let int_val = rp_pac::powman::regs::Int(val.0 | POWMAN_KEY); + self.write_value(int_val); + } + } +} From 147205b3a790fc741b3b977c90870d980462afeb Mon Sep 17 00:00:00 2001 From: mschnell Date: Thu, 6 Feb 2025 15:13:32 +0100 Subject: [PATCH 2/8] TODO: "time-driver-mtime" --- embassy-rp/src/time_driver.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/embassy-rp/src/time_driver.rs b/embassy-rp/src/time_driver.rs index e5ab054724..c9f14606b1 100644 --- a/embassy-rp/src/time_driver.rs +++ b/embassy-rp/src/time_driver.rs @@ -392,3 +392,5 @@ mod timer_aon { } } } + +// TODO add a feature "time-driver-mtime" to use the RISC-V Platform Timer to free all other timers for the user From 7e90697d56c06af6ef83738fa13339159db3d325 Mon Sep 17 00:00:00 2001 From: mschnell Date: Mon, 10 Feb 2025 17:16:45 +0100 Subject: [PATCH 3/8] enable using the Risk V System Timer --- embassy-rp/src/time_driver.rs | 109 +++++++++++++++++++++++++++++++--- 1 file changed, 101 insertions(+), 8 deletions(-) diff --git a/embassy-rp/src/time_driver.rs b/embassy-rp/src/time_driver.rs index c9f14606b1..bcb9c8de6d 100644 --- a/embassy-rp/src/time_driver.rs +++ b/embassy-rp/src/time_driver.rs @@ -1,5 +1,5 @@ -//! Timer driver. use core::cell::{Cell, RefCell}; +// Timer driver. use critical_section::CriticalSection; use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; @@ -9,8 +9,12 @@ use embassy_time_queue_utils::Queue; #[cfg(all(feature = "_rp235x", feature = "time-driver-timer1"))] use embassy_rp::clocks; +#[cfg(all(feature = "_rp235x", feature = "time-driver-mtime"))] +use embassy_rp::clocks; #[cfg(all(feature = "_rp235x", feature = "time-driver-aot"))] use pac::POWMAN as TIMER; +#[cfg(all(feature = "_rp235x", feature = "time-driver-mtime"))] +use pac::SIO as TIMER; #[cfg(feature = "rp2040")] use pac::TIMER; #[cfg(all(feature = "_rp235x", feature = "time-driver-timer0"))] @@ -39,7 +43,7 @@ embassy_time_driver::time_driver_impl!(static DRIVER: TimerDriver = TimerDriver{ }); impl Driver for TimerDriver { - #[cfg(not(feature = "time-driver-aot"))] + #[cfg(not(any(feature = "time-driver-aot", feature = "time-driver-mtime")))] fn now(&self) -> u64 { loop { let hi = TIMER.timerawh().read(); @@ -65,6 +69,21 @@ impl Driver for TimerDriver { now_lpo * TICKS_PER_LPOSC_TICK } + #[cfg(all(feature = "_rp235x", feature = "time-driver-mtime"))] + fn now(&self) -> u64 { + let now_mtime = loop { + let timehi = TIMER.mtimeh(); + let timelo = TIMER.mtime(); + let hi2 = timehi.read(); + let lo = timelo.read(); + let hi = timehi.read(); + if hi == hi2 { + break (hi as u64) << 32 | (lo as u64); + } + }; + now_mtime + } + fn schedule_wake(&self, at: u64, waker: &core::task::Waker) { critical_section::with(|cs| { let mut queue = self.queue.borrow(cs).borrow_mut(); @@ -80,7 +99,7 @@ impl Driver for TimerDriver { } impl TimerDriver { - #[cfg(not(feature = "time-driver-aot"))] + #[cfg(not(any(feature = "time-driver-aot", feature = "time-driver-mtime")))] fn set_alarm(&self, cs: CriticalSection, timestamp: u64) -> bool { let n = 0; let alarm = &self.alarms.borrow(cs); @@ -106,13 +125,42 @@ impl TimerDriver { } } - #[cfg(not(feature = "time-driver-aot"))] + #[cfg(feature = "time-driver-mtime")] + fn set_alarm(&self, cs: CriticalSection, timestamp: u64) -> bool { + let alarm = &self.alarms.borrow(cs); + alarm.timestamp.set(timestamp); + + // Arm it. + + let mtime_cmp = TIMER.mtimecmp(); + let mtime_cmp_h = TIMER.mtimecmph(); + mtime_cmp.write_value(u32::MAX); + mtime_cmp_h.write_value((timestamp >> 32) as u32); + mtime_cmp.write_value(timestamp as u32); + + let now = self.now(); + if timestamp <= now { + // If alarm timestamp has passed the alarm will not fire. + // Disarm the alarm and return `false` to indicate that. + + //TIMER.armed().write(|w| w.set_armed(1 << n)); + + // TIMER.mtime_ctrl() ??????? + + alarm.timestamp.set(u64::MAX); + + false + } else { + true + } + } + + #[cfg(not(any(feature = "time-driver-aot", feature = "time-driver-mtime")))] fn check_alarm(&self) { let n = 0; critical_section::with(|cs| { // clear the irq TIMER.intr().write(|w| w.set_alarm(n, true)); - let alarm = &self.alarms.borrow(cs); let timestamp = alarm.timestamp.get(); if timestamp <= self.now() { @@ -125,6 +173,29 @@ impl TimerDriver { }); } + #[cfg(feature = "time-driver-mtime")] + fn check_alarm(&self) { + critical_section::with(|cs| { + // clear the irq + // TIMER.intr().write(|w| w.set_alarm(n, true)); + let riscv_softirq = TIMER.riscv_softirq(); + let mut riscv_softirq_value = riscv_softirq.read(); + riscv_softirq_value.set_core0_set(true); + riscv_softirq.write_value(riscv_softirq_value); + let alarm = &self.alarms.borrow(cs); + let timestamp = alarm.timestamp.get(); + if timestamp <= self.now() { + self.trigger_alarm(cs) + } else { + // Not elapsed, arm it again. + let riscv_softirq = TIMER.riscv_softirq(); + let mut riscv_softirq_value = riscv_softirq.read(); + riscv_softirq_value.set_core0_set(true); + riscv_softirq.write_value(riscv_softirq_value); + } + }); + } + fn trigger_alarm(&self, cs: CriticalSection) { let mut next = self .queue @@ -142,6 +213,7 @@ impl TimerDriver { } /// safety: must be called exactly once at bootup +// init alarms pub unsafe fn init() { #[cfg(all(feature = "_rp235x", feature = "time-driver-timer1"))] { @@ -156,7 +228,15 @@ pub unsafe fn init() { #[cfg(all(feature = "_rp235x", feature = "time-driver-aot"))] timer_aon::initialize_aon_timer(); - // init alarms + #[cfg(all(feature = "_rp235x", feature = "time-driver-mtime"))] + { + let timer_cycles = clocks::clk_ref_freq() / embassy_time_driver::TICK_HZ as u32; + pac::TICKS.riscv_cycles().write(|w| w.0 = timer_cycles); + pac::TICKS.riscv_ctrl().write(|w| w.0 = 1); + TIMER.mtime().write_value(0); + TIMER.mtimeh().write_value(0); + } + critical_section::with(|cs| { let alarm = DRIVER.alarms.borrow(cs); alarm.timestamp.set(u64::MAX); @@ -197,6 +277,15 @@ pub unsafe fn init() { TIMER.inte().write_value_key(inte_value); interrupt::POWMAN_IRQ_TIMER.enable(); } + #[cfg(feature = "time-driver-mtime")] + { + // enable irq + let riscv_softirq = TIMER.riscv_softirq(); + let mut riscv_softirq_value = riscv_softirq.read(); + riscv_softirq_value.set_core0_set(true); + riscv_softirq.write_value(riscv_softirq_value); + interrupt::SIO_IRQ_MTIMECMP.enable(); + } } } @@ -224,6 +313,12 @@ fn POWMAN_IRQ_TIMER() { DRIVER.check_alarm() } +#[cfg(all(feature = "_rp235x", feature = "time-driver-mtime"))] +#[interrupt] +fn SIO_IRQ_MTIMECMP() { + DRIVER.check_alarm() +} + #[cfg(all(feature = "_rp235x", feature = "time-driver-aot"))] mod timer_aon { use super::TIMER; @@ -392,5 +487,3 @@ mod timer_aon { } } } - -// TODO add a feature "time-driver-mtime" to use the RISC-V Platform Timer to free all other timers for the user From 368e5b6ccda47297f1ec20cb3ea0c23f175e34d3 Mon Sep 17 00:00:00 2001 From: mschnell Date: Mon, 10 Feb 2025 17:20:12 +0100 Subject: [PATCH 4/8] Enable Risk V System Timer --- embassy-rp/src/time_driver.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/embassy-rp/src/time_driver.rs b/embassy-rp/src/time_driver.rs index bcb9c8de6d..25cf9498c7 100644 --- a/embassy-rp/src/time_driver.rs +++ b/embassy-rp/src/time_driver.rs @@ -1,6 +1,5 @@ +//! Timer driver. use core::cell::{Cell, RefCell}; -// Timer driver. - use critical_section::CriticalSection; use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; use embassy_sync::blocking_mutex::Mutex; From caa36505524ca9f02ff282b4ff75b3e48b10e279 Mon Sep 17 00:00:00 2001 From: mschnell Date: Mon, 10 Feb 2025 17:27:12 +0100 Subject: [PATCH 5/8] delete some nonsense --- embassy-rp/src/time_driver.rs | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/embassy-rp/src/time_driver.rs b/embassy-rp/src/time_driver.rs index 25cf9498c7..975c28b51d 100644 --- a/embassy-rp/src/time_driver.rs +++ b/embassy-rp/src/time_driver.rs @@ -1,4 +1,5 @@ //! Timer driver. + use core::cell::{Cell, RefCell}; use critical_section::CriticalSection; use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; @@ -21,8 +22,11 @@ use pac::TIMER0 as TIMER; #[cfg(all(feature = "_rp235x", feature = "time-driver-timer1"))] use pac::TIMER1 as TIMER; +/* use crate::interrupt::InterruptExt; use crate::{interrupt, pac}; + */ +use embassy_rp::{interrupt, interrupt::InterruptExt, pac}; struct AlarmState { timestamp: Cell, @@ -176,21 +180,12 @@ impl TimerDriver { fn check_alarm(&self) { critical_section::with(|cs| { // clear the irq - // TIMER.intr().write(|w| w.set_alarm(n, true)); - let riscv_softirq = TIMER.riscv_softirq(); - let mut riscv_softirq_value = riscv_softirq.read(); - riscv_softirq_value.set_core0_set(true); - riscv_softirq.write_value(riscv_softirq_value); let alarm = &self.alarms.borrow(cs); let timestamp = alarm.timestamp.get(); if timestamp <= self.now() { self.trigger_alarm(cs) } else { // Not elapsed, arm it again. - let riscv_softirq = TIMER.riscv_softirq(); - let mut riscv_softirq_value = riscv_softirq.read(); - riscv_softirq_value.set_core0_set(true); - riscv_softirq.write_value(riscv_softirq_value); } }); } @@ -279,10 +274,6 @@ pub unsafe fn init() { #[cfg(feature = "time-driver-mtime")] { // enable irq - let riscv_softirq = TIMER.riscv_softirq(); - let mut riscv_softirq_value = riscv_softirq.read(); - riscv_softirq_value.set_core0_set(true); - riscv_softirq.write_value(riscv_softirq_value); interrupt::SIO_IRQ_MTIMECMP.enable(); } } From e2830bb4d56ae2e091341d0cb10bfccf3e8bf29e Mon Sep 17 00:00:00 2001 From: mschnell Date: Mon, 10 Feb 2025 17:28:21 +0100 Subject: [PATCH 6/8] use crate:: --- embassy-rp/src/time_driver.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/embassy-rp/src/time_driver.rs b/embassy-rp/src/time_driver.rs index 975c28b51d..ff3c4db982 100644 --- a/embassy-rp/src/time_driver.rs +++ b/embassy-rp/src/time_driver.rs @@ -22,11 +22,9 @@ use pac::TIMER0 as TIMER; #[cfg(all(feature = "_rp235x", feature = "time-driver-timer1"))] use pac::TIMER1 as TIMER; -/* + use crate::interrupt::InterruptExt; use crate::{interrupt, pac}; - */ -use embassy_rp::{interrupt, interrupt::InterruptExt, pac}; struct AlarmState { timestamp: Cell, From 04bbffad6bed821f44d36b3ac77f8513dabd25f1 Mon Sep 17 00:00:00 2001 From: mschnell Date: Tue, 11 Feb 2025 15:14:21 +0100 Subject: [PATCH 7/8] use TIMER0 if no "time-driver-..." feature is set --- embassy-rp/src/time_driver.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/embassy-rp/src/time_driver.rs b/embassy-rp/src/time_driver.rs index ff3c4db982..5da42f6f69 100644 --- a/embassy-rp/src/time_driver.rs +++ b/embassy-rp/src/time_driver.rs @@ -17,12 +17,11 @@ use pac::POWMAN as TIMER; use pac::SIO as TIMER; #[cfg(feature = "rp2040")] use pac::TIMER; -#[cfg(all(feature = "_rp235x", feature = "time-driver-timer0"))] +#[cfg(all(feature = "_rp235x", not(any(feature = "time-driver-timer1", feature = "time-driver-mtime", feature = "time-driver-aot"))))] use pac::TIMER0 as TIMER; #[cfg(all(feature = "_rp235x", feature = "time-driver-timer1"))] use pac::TIMER1 as TIMER; - use crate::interrupt::InterruptExt; use crate::{interrupt, pac}; @@ -223,6 +222,9 @@ pub unsafe fn init() { #[cfg(all(feature = "_rp235x", feature = "time-driver-mtime"))] { let timer_cycles = clocks::clk_ref_freq() / embassy_time_driver::TICK_HZ as u32; + assert!(timer_cycles < 512); + assert!(timer_cycles > 0); + assert!(timer_cycles * embassy_time_driver::TICK_HZ as u32 == clocks::clk_ref_freq()); pac::TICKS.riscv_cycles().write(|w| w.0 = timer_cycles); pac::TICKS.riscv_ctrl().write(|w| w.0 = 1); TIMER.mtime().write_value(0); @@ -244,7 +246,7 @@ pub unsafe fn init() { } #[cfg(feature = "_rp235x")] { - #[cfg(feature = "time-driver-timer0")] + #[cfg(all(feature = "_rp235x", not(any(feature = "time-driver-timer1", feature = "time-driver-mtime", feature = "time-driver-aot"))))] { // enable irq TIMER.inte().write(|w| { @@ -283,7 +285,7 @@ fn TIMER_IRQ_0() { DRIVER.check_alarm() } -#[cfg(all(feature = "rt", feature = "_rp235x", feature = "time-driver-timer0"))] +#[cfg(all(feature = "_rp235x", not(any(feature = "time-driver-timer1", feature = "time-driver-mtime", feature = "time-driver-aot"))))] #[interrupt] fn TIMER0_IRQ_0() { DRIVER.check_alarm() From 2de35bd33c62c3fb68bf4ddf03351cecf955642b Mon Sep 17 00:00:00 2001 From: mschnell Date: Tue, 11 Feb 2025 15:28:45 +0100 Subject: [PATCH 8/8] defining optionalo feature --- embassy-rp/Cargo.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/embassy-rp/Cargo.toml b/embassy-rp/Cargo.toml index 4e5c66feb4..7cc77eb022 100644 --- a/embassy-rp/Cargo.toml +++ b/embassy-rp/Cargo.toml @@ -28,6 +28,10 @@ features = ["defmt", "unstable-pac", "time-driver", "rp2040"] default = [ "rt" ] ## Enable the rt feature of [`rp-pac`](https://docs.rs/rp-pac). This brings in the [`cortex-m-rt`](https://docs.rs/cortex-m-rt) crate, which adds startup code and minimal runtime initialization. rt = [ "rp-pac/rt" ] +time-driver-timer1 = [] +time-driver-aot = [] +time-driver-mtime = [] + ## Enable [defmt support](https://docs.rs/defmt) and enables `defmt` debug-log messages and formatting in embassy drivers. defmt = ["dep:defmt", "embassy-usb-driver/defmt", "embassy-hal-internal/defmt"]