2024-04-11 00:00:38 +02:00
|
|
|
//! [`Monotonic`](rtic_time::Monotonic) implementation for RP2040's Timer peripheral.
|
|
|
|
//!
|
|
|
|
//! Always runs at a fixed rate of 1 MHz.
|
2023-04-01 20:48:23 +02:00
|
|
|
//!
|
|
|
|
//! # Example
|
|
|
|
//!
|
|
|
|
//! ```
|
2024-04-11 00:00:38 +02:00
|
|
|
//! use rtic_monotonics::rp2040::prelude::*;
|
|
|
|
//!
|
|
|
|
//! rp2040_timer_monotonic!(Mono);
|
2023-04-01 20:48:23 +02:00
|
|
|
//!
|
2023-04-02 20:32:10 +02:00
|
|
|
//! fn init() {
|
|
|
|
//! # // This is normally provided by the selected PAC
|
|
|
|
//! # let timer = unsafe { core::mem::transmute(()) };
|
|
|
|
//! # let mut resets = unsafe { core::mem::transmute(()) };
|
2024-04-11 00:00:38 +02:00
|
|
|
//! #
|
2023-04-02 20:32:10 +02:00
|
|
|
//! // Start the monotonic
|
2024-04-11 00:00:38 +02:00
|
|
|
//! Mono::start(timer, &mut resets);
|
2023-04-02 20:32:10 +02:00
|
|
|
//! }
|
2023-04-01 20:48:23 +02:00
|
|
|
//!
|
2023-04-02 20:32:10 +02:00
|
|
|
//! async fn usage() {
|
|
|
|
//! loop {
|
|
|
|
//! // Use the monotonic
|
2024-04-11 00:00:38 +02:00
|
|
|
//! let timestamp = Mono::now();
|
|
|
|
//! Mono::delay(100.millis()).await;
|
2023-04-02 20:32:10 +02:00
|
|
|
//! }
|
2023-04-01 20:48:23 +02:00
|
|
|
//! }
|
|
|
|
//! ```
|
2023-02-10 07:52:50 +01:00
|
|
|
|
2024-04-11 00:00:38 +02:00
|
|
|
/// Common definitions and traits for using the RP2040 timer monotonic
|
|
|
|
pub mod prelude {
|
|
|
|
pub use crate::rp2040_timer_monotonic;
|
2023-04-23 16:34:18 +02:00
|
|
|
|
2024-04-11 00:00:38 +02:00
|
|
|
pub use crate::Monotonic;
|
2023-02-10 07:52:50 +01:00
|
|
|
|
2024-04-11 00:00:38 +02:00
|
|
|
pub use fugit::{self, ExtU64, ExtU64Ceil};
|
|
|
|
}
|
2023-02-10 07:52:50 +01:00
|
|
|
|
2024-04-11 00:00:38 +02:00
|
|
|
use crate::TimerQueueBackend;
|
|
|
|
use rp2040_pac::{timer, Interrupt, NVIC};
|
|
|
|
pub use rp2040_pac::{RESETS, TIMER};
|
|
|
|
use rtic_time::timer_queue::TimerQueue;
|
|
|
|
|
|
|
|
/// Timer implementing [`TimerQueueBackend`].
|
|
|
|
pub struct TimerBackend;
|
|
|
|
|
|
|
|
impl TimerBackend {
|
|
|
|
/// Starts the monotonic timer.
|
|
|
|
///
|
|
|
|
/// **Do not use this function directly.**
|
|
|
|
///
|
|
|
|
/// Use the prelude macros instead.
|
|
|
|
pub fn _start(timer: TIMER, resets: &RESETS) {
|
2024-04-21 12:46:29 +02:00
|
|
|
resets.reset().modify(|_, w| w.timer().clear_bit());
|
|
|
|
while resets.reset_done().read().timer().bit_is_clear() {}
|
|
|
|
timer.inte().modify(|_, w| w.alarm_0().bit(true));
|
2023-02-10 07:52:50 +01:00
|
|
|
|
|
|
|
TIMER_QUEUE.initialize(Self {});
|
2023-03-11 20:25:35 +01:00
|
|
|
|
2023-04-02 20:32:10 +02:00
|
|
|
unsafe {
|
|
|
|
crate::set_monotonic_prio(rp2040_pac::NVIC_PRIO_BITS, Interrupt::TIMER_IRQ_0);
|
|
|
|
NVIC::unmask(Interrupt::TIMER_IRQ_0);
|
|
|
|
}
|
2023-02-10 07:52:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn timer() -> &'static timer::RegisterBlock {
|
|
|
|
unsafe { &*TIMER::ptr() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-11 00:00:38 +02:00
|
|
|
static TIMER_QUEUE: TimerQueue<TimerBackend> = TimerQueue::new();
|
2023-02-10 07:52:50 +01:00
|
|
|
|
2024-04-11 00:00:38 +02:00
|
|
|
impl TimerQueueBackend for TimerBackend {
|
|
|
|
type Ticks = u64;
|
2023-02-10 07:52:50 +01:00
|
|
|
|
2024-04-11 00:00:38 +02:00
|
|
|
fn now() -> Self::Ticks {
|
2023-02-10 07:52:50 +01:00
|
|
|
let timer = Self::timer();
|
|
|
|
|
2024-04-21 12:46:29 +02:00
|
|
|
let mut hi0 = timer.timerawh().read().bits();
|
2023-02-10 07:52:50 +01:00
|
|
|
loop {
|
2024-04-21 12:46:29 +02:00
|
|
|
let low = timer.timerawl().read().bits();
|
|
|
|
let hi1 = timer.timerawh().read().bits();
|
2023-02-10 07:52:50 +01:00
|
|
|
if hi0 == hi1 {
|
2024-04-11 00:00:38 +02:00
|
|
|
break ((u64::from(hi0) << 32) | u64::from(low));
|
2023-02-10 07:52:50 +01:00
|
|
|
}
|
|
|
|
hi0 = hi1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-11 00:00:38 +02:00
|
|
|
fn set_compare(instant: Self::Ticks) {
|
2023-02-10 07:52:50 +01:00
|
|
|
let now = Self::now();
|
|
|
|
|
2024-04-11 00:00:38 +02:00
|
|
|
const MAX: u64 = u32::MAX as u64;
|
2023-02-10 07:52:50 +01:00
|
|
|
|
|
|
|
// Since the timer may or may not overflow based on the requested compare val, we check
|
|
|
|
// how many ticks are left.
|
2024-04-11 00:00:38 +02:00
|
|
|
// `wrapping_sup` takes care of the u64 integer overflow special case.
|
|
|
|
let val = if instant.wrapping_sub(now) <= MAX {
|
|
|
|
instant & MAX
|
|
|
|
} else {
|
|
|
|
0
|
2023-02-10 07:52:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Self::timer()
|
2024-04-21 12:46:29 +02:00
|
|
|
.alarm0()
|
2023-02-10 07:52:50 +01:00
|
|
|
.write(|w| unsafe { w.bits(val as u32) });
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clear_compare_flag() {
|
2024-04-21 12:46:29 +02:00
|
|
|
Self::timer().intr().modify(|_, w| w.alarm_0().bit(true));
|
2023-02-10 07:52:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn pend_interrupt() {
|
2023-02-11 07:55:08 +01:00
|
|
|
rp2040_pac::NVIC::pend(Interrupt::TIMER_IRQ_0);
|
2023-02-10 07:52:50 +01:00
|
|
|
}
|
|
|
|
|
2024-04-11 00:00:38 +02:00
|
|
|
fn timer_queue() -> &'static TimerQueue<Self> {
|
|
|
|
&TIMER_QUEUE
|
|
|
|
}
|
2023-02-10 07:52:50 +01:00
|
|
|
}
|
|
|
|
|
2024-04-11 00:00:38 +02:00
|
|
|
/// Create an RP2040 timer based monotonic and register the necessary interrupt for it.
|
|
|
|
///
|
|
|
|
/// See [`crate::rp2040`] for more details.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `name` - The name that the monotonic type will have.
|
2023-02-10 07:52:50 +01:00
|
|
|
#[macro_export]
|
2024-04-11 00:00:38 +02:00
|
|
|
macro_rules! rp2040_timer_monotonic {
|
|
|
|
($name:ident) => {
|
|
|
|
/// A `Monotonic` based on the RP2040 Timer peripheral.
|
|
|
|
struct $name;
|
|
|
|
|
|
|
|
impl $name {
|
|
|
|
/// Starts the `Monotonic`.
|
|
|
|
///
|
|
|
|
/// This method must be called only once.
|
|
|
|
pub fn start(timer: $crate::rp2040::TIMER, resets: &$crate::rp2040::RESETS) {
|
|
|
|
#[no_mangle]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
unsafe extern "C" fn TIMER_IRQ_0() {
|
|
|
|
use $crate::TimerQueueBackend;
|
|
|
|
$crate::rp2040::TimerBackend::timer_queue().on_monotonic_interrupt();
|
|
|
|
}
|
|
|
|
|
|
|
|
$crate::rp2040::TimerBackend::_start(timer, resets);
|
|
|
|
}
|
2023-02-10 07:52:50 +01:00
|
|
|
}
|
2023-03-11 20:47:39 +01:00
|
|
|
|
2024-04-11 00:00:38 +02:00
|
|
|
impl $crate::TimerQueueBasedMonotonic for $name {
|
|
|
|
type Backend = $crate::rp2040::TimerBackend;
|
|
|
|
type Instant = $crate::fugit::Instant<
|
|
|
|
<Self::Backend as $crate::TimerQueueBackend>::Ticks,
|
|
|
|
1,
|
|
|
|
1_000_000,
|
|
|
|
>;
|
|
|
|
type Duration = $crate::fugit::Duration<
|
|
|
|
<Self::Backend as $crate::TimerQueueBackend>::Ticks,
|
|
|
|
1,
|
|
|
|
1_000_000,
|
|
|
|
>;
|
|
|
|
}
|
2023-03-11 20:47:39 +01:00
|
|
|
|
2024-04-11 00:00:38 +02:00
|
|
|
$crate::rtic_time::impl_embedded_hal_delay_fugit!($name);
|
|
|
|
$crate::rtic_time::impl_embedded_hal_async_delay_fugit!($name);
|
|
|
|
};
|
2023-02-10 07:52:50 +01:00
|
|
|
}
|