1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//! [`Monotonic`] implementation for RP2040's Timer peripheral.
//!
//! # Example
//!
//! ```
//! use rtic_monotonics::rp2040::*;
//!
//! fn init() {
//!     # // This is normally provided by the selected PAC
//!     # let timer = unsafe { core::mem::transmute(()) };
//!     # let mut resets = unsafe { core::mem::transmute(()) };
//!     // Generate the required token
//!     let token = rtic_monotonics::create_rp2040_monotonic_token!();
//!
//!     // Start the monotonic
//!     Timer::start(timer, &mut resets, token);
//! }
//!
//! async fn usage() {
//!     loop {
//!          // Use the monotonic
//!          Timer::delay(100.millis()).await;
//!     }
//! }
//! ```

use super::Monotonic;

pub use super::{TimeoutError, TimerQueue};
use core::future::Future;
pub use fugit::{self, ExtU64};
use rp2040_pac::{timer, Interrupt, NVIC, RESETS, TIMER};

/// Timer implementing `rtic_monotonic::Monotonic` which runs at 1 MHz.
pub struct Timer;

impl Timer {
    /// Start a `Monotonic` based on RP2040's Timer.
    pub fn start(
        timer: TIMER,
        resets: &RESETS,
        _interrupt_token: impl crate::InterruptToken<Self>,
    ) {
        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));

        TIMER_QUEUE.initialize(Self {});

        unsafe {
            crate::set_monotonic_prio(rp2040_pac::NVIC_PRIO_BITS, Interrupt::TIMER_IRQ_0);
            NVIC::unmask(Interrupt::TIMER_IRQ_0);
        }
    }

    fn timer() -> &'static timer::RegisterBlock {
        unsafe { &*TIMER::ptr() }
    }
}

static TIMER_QUEUE: TimerQueue<Timer> = TimerQueue::new();

// Forward timerqueue interface
impl Timer {
    /// Used to access the underlying timer queue
    #[doc(hidden)]
    pub fn __tq() -> &'static TimerQueue<Timer> {
        &TIMER_QUEUE
    }

    /// Timeout at a specific time.
    #[inline]
    pub async fn timeout_at<F: Future>(
        instant: <Self as Monotonic>::Instant,
        future: F,
    ) -> Result<F::Output, TimeoutError> {
        TIMER_QUEUE.timeout_at(instant, future).await
    }

    /// Timeout after a specific duration.
    #[inline]
    pub async fn timeout_after<F: Future>(
        duration: <Self as Monotonic>::Duration,
        future: F,
    ) -> Result<F::Output, TimeoutError> {
        TIMER_QUEUE.timeout_after(duration, future).await
    }

    /// Delay for some duration of time.
    #[inline]
    pub async fn delay(duration: <Self as Monotonic>::Duration) {
        TIMER_QUEUE.delay(duration).await;
    }

    /// Delay to some specific time instant.
    #[inline]
    pub async fn delay_until(instant: <Self as Monotonic>::Instant) {
        TIMER_QUEUE.delay_until(instant).await;
    }
}

impl Monotonic for Timer {
    type Instant = fugit::TimerInstantU64<1_000_000>;
    type Duration = fugit::TimerDurationU64<1_000_000>;

    const ZERO: Self::Instant = Self::Instant::from_ticks(0);

    fn now() -> Self::Instant {
        let timer = Self::timer();

        let mut hi0 = timer.timerawh.read().bits();
        loop {
            let low = timer.timerawl.read().bits();
            let hi1 = timer.timerawh.read().bits();
            if hi0 == hi1 {
                break Self::Instant::from_ticks((u64::from(hi0) << 32) | u64::from(low));
            }
            hi0 = hi1;
        }
    }

    fn set_compare(instant: Self::Instant) {
        let now = Self::now();

        let max = u32::MAX as u64;

        // Since the timer may or may not overflow based on the requested compare val, we check
        // how many ticks are left.
        let val = match instant.checked_duration_since(now) {
            Some(x) if x.ticks() <= max => instant.duration_since_epoch().ticks() & max, // Will not overflow
            _ => 0, // Will overflow or in the past, set the same value as after overflow to not get extra interrupts
        };

        Self::timer()
            .alarm0
            .write(|w| unsafe { w.bits(val as u32) });
    }

    fn clear_compare_flag() {
        Self::timer().intr.modify(|_, w| w.alarm_0().bit(true));
    }

    fn pend_interrupt() {
        rp2040_pac::NVIC::pend(Interrupt::TIMER_IRQ_0);
    }

    fn on_interrupt() {}

    fn enable_timer() {}

    fn disable_timer() {}
}

#[cfg(feature = "embedded-hal-async")]
impl embedded_hal_async::delay::DelayUs for Timer {
    async fn delay_us(&mut self, us: u32) {
        Self::delay((us as u64).micros()).await;
    }

    async fn delay_ms(&mut self, ms: u32) {
        Self::delay((ms as u64).millis()).await;
    }
}

impl embedded_hal::delay::DelayUs for Timer {
    fn delay_us(&mut self, us: u32) {
        let done = Self::now() + u64::from(us).micros();
        while Self::now() < done {}
    }
}

/// Register the Timer interrupt for the monotonic.
#[macro_export]
macro_rules! create_rp2040_monotonic_token {
    () => {{
        #[no_mangle]
        #[allow(non_snake_case)]
        unsafe extern "C" fn TIMER_IRQ_0() {
            $crate::rp2040::Timer::__tq().on_monotonic_interrupt();
        }

        pub struct Rp2040Token;

        unsafe impl $crate::InterruptToken<$crate::rp2040::Timer> for Rp2040Token {}

        Rp2040Token
    }};
}