|
| 1 | +//! [`Monotonic`](rtic_time::Monotonic) implementation for RP235x's Timer peripheral |
| 2 | +//! |
| 3 | +//! |
| 4 | +//! Always runs at a fixed rate of 1 MHz. |
| 5 | +//! |
| 6 | +//! # Example |
| 7 | +//! |
| 8 | +//! ``` |
| 9 | +//! use rtic_monotonics::rp235x::prelude::*; |
| 10 | +//! |
| 11 | +//! rp235x_timer_monotonic!(Mono); |
| 12 | +//! |
| 13 | +//! fn init() { |
| 14 | +//! # // This is normally provided by the selected PAC |
| 15 | +//! # let timer = unsafe { core::mem::transmute(()) }; |
| 16 | +//! # let mut resets = unsafe { core::mem::transmute(()) }; |
| 17 | +//! # |
| 18 | +//! // Start the monotonic |
| 19 | +//! Mono::start(timer, &mut resets); |
| 20 | +//! } |
| 21 | +//! |
| 22 | +//! async fn usage() { |
| 23 | +//! loop { |
| 24 | +//! // Use the monotonic |
| 25 | +//! let timestamp = Mono::now(); |
| 26 | +//! Mono::delay(100.millis()).await; |
| 27 | +//! } |
| 28 | +//! } |
| 29 | +//! ``` |
| 30 | +
|
| 31 | +/// Common definitions and traits for using the RP235x timer monotonic |
| 32 | +pub mod prelude { |
| 33 | + pub use crate::rp235x_timer_monotonic; |
| 34 | + |
| 35 | + pub use crate::Monotonic; |
| 36 | + |
| 37 | + pub use fugit::{self, ExtU64, ExtU64Ceil}; |
| 38 | +} |
| 39 | + |
| 40 | +use crate::TimerQueueBackend; |
| 41 | +use cortex_m::peripheral::NVIC; |
| 42 | +use rp235x_pac::Interrupt; |
| 43 | +pub use rp235x_pac::{timer0, RESETS, TIMER0}; |
| 44 | +use rtic_time::timer_queue::TimerQueue; |
| 45 | + |
| 46 | +/// Timer implementing [`TimerQueueBackend`]. |
| 47 | +pub struct TimerBackend; |
| 48 | + |
| 49 | +impl TimerBackend { |
| 50 | + /// Starts the monotonic timer. |
| 51 | + /// |
| 52 | + /// **Do not use this function directly.** |
| 53 | + /// |
| 54 | + /// Use the prelude macros instead. |
| 55 | + pub fn _start(timer: TIMER0, resets: &RESETS) { |
| 56 | + resets.reset().modify(|_, w| w.timer0().clear_bit()); |
| 57 | + while resets.reset_done().read().timer0().bit_is_clear() {} |
| 58 | + timer.inte().modify(|_, w| w.alarm_0().bit(true)); |
| 59 | + |
| 60 | + TIMER_QUEUE.initialize(Self {}); |
| 61 | + |
| 62 | + unsafe { |
| 63 | + crate::set_monotonic_prio(rp235x_pac::NVIC_PRIO_BITS, Interrupt::TIMER0_IRQ_0); |
| 64 | + NVIC::unmask(Interrupt::TIMER0_IRQ_0); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + fn timer() -> &'static timer0::RegisterBlock { |
| 69 | + unsafe { &*TIMER0::ptr() } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +static TIMER_QUEUE: TimerQueue<TimerBackend> = TimerQueue::new(); |
| 74 | + |
| 75 | +impl TimerQueueBackend for TimerBackend { |
| 76 | + type Ticks = u64; |
| 77 | + |
| 78 | + fn now() -> Self::Ticks { |
| 79 | + let timer = Self::timer(); |
| 80 | + |
| 81 | + let mut hi0 = timer.timerawh().read().bits(); |
| 82 | + loop { |
| 83 | + let low = timer.timerawl().read().bits(); |
| 84 | + let hi1 = timer.timerawh().read().bits(); |
| 85 | + if hi0 == hi1 { |
| 86 | + break ((u64::from(hi0) << 32) | u64::from(low)); |
| 87 | + } |
| 88 | + hi0 = hi1; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + fn set_compare(instant: Self::Ticks) { |
| 93 | + let now = Self::now(); |
| 94 | + |
| 95 | + const MAX: u64 = u32::MAX as u64; |
| 96 | + |
| 97 | + // Since the timer may or may not overflow based on the requested compare val, we check |
| 98 | + // how many ticks are left. |
| 99 | + // `wrapping_sub` takes care of the u64 integer overflow special case. |
| 100 | + let val = if instant.wrapping_sub(now) <= MAX { |
| 101 | + instant & MAX |
| 102 | + } else { |
| 103 | + 0 |
| 104 | + }; |
| 105 | + |
| 106 | + Self::timer() |
| 107 | + .alarm0() |
| 108 | + .write(|w| unsafe { w.bits(val as u32) }); |
| 109 | + } |
| 110 | + |
| 111 | + fn clear_compare_flag() { |
| 112 | + Self::timer().intr().modify(|_, w| w.alarm_0().bit(true)); |
| 113 | + } |
| 114 | + |
| 115 | + fn pend_interrupt() { |
| 116 | + NVIC::pend(Interrupt::TIMER0_IRQ_0); |
| 117 | + } |
| 118 | + |
| 119 | + fn timer_queue() -> &'static TimerQueue<Self> { |
| 120 | + &TIMER_QUEUE |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +/// Create an RP235x timer based monotonic and register the necessary interrupt for it. |
| 125 | +/// |
| 126 | +/// See [`crate::rp235x`] for more details. |
| 127 | +/// |
| 128 | +/// # Arguments |
| 129 | +/// |
| 130 | +/// * `name` - The name that the monotonic type will have. |
| 131 | +#[macro_export] |
| 132 | +macro_rules! rp235x_timer_monotonic { |
| 133 | + ($name:ident) => { |
| 134 | + /// A `Monotonic` based on the RP235x Timer peripheral. |
| 135 | + pub struct $name; |
| 136 | + |
| 137 | + impl $name { |
| 138 | + /// Starts the `Monotonic`. |
| 139 | + /// |
| 140 | + /// This method must be called only once. |
| 141 | + pub fn start(timer: $crate::rp235x::TIMER0, resets: &$crate::rp235x::RESETS) { |
| 142 | + #[no_mangle] |
| 143 | + #[allow(non_snake_case)] |
| 144 | + unsafe extern "C" fn TIMER0_IRQ_0() { |
| 145 | + use $crate::TimerQueueBackend; |
| 146 | + $crate::rp235x::TimerBackend::timer_queue().on_monotonic_interrupt(); |
| 147 | + } |
| 148 | + |
| 149 | + $crate::rp235x::TimerBackend::_start(timer, resets); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + impl $crate::TimerQueueBasedMonotonic for $name { |
| 154 | + type Backend = $crate::rp235x::TimerBackend; |
| 155 | + type Instant = $crate::fugit::Instant< |
| 156 | + <Self::Backend as $crate::TimerQueueBackend>::Ticks, |
| 157 | + 1, |
| 158 | + 1_000_000, |
| 159 | + >; |
| 160 | + type Duration = $crate::fugit::Duration< |
| 161 | + <Self::Backend as $crate::TimerQueueBackend>::Ticks, |
| 162 | + 1, |
| 163 | + 1_000_000, |
| 164 | + >; |
| 165 | + } |
| 166 | + |
| 167 | + $crate::rtic_time::impl_embedded_hal_delay_fugit!($name); |
| 168 | + $crate::rtic_time::impl_embedded_hal_async_delay_fugit!($name); |
| 169 | + }; |
| 170 | +} |
0 commit comments