aboutsummaryrefslogtreecommitdiff
path: root/rtic-monotonics/src/stm32.rs
blob: e496e56b845bf0a28007ed5c9517ba17fe37ff24 (plain) (blame)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//! [`Monotonic`] impl for the STM32.
//!
//! Not all timers are available on all parts. Ensure that only available
//! timers are exposed by having the correct `stm32*` feature enabled for `rtic-monotonic`.
//!
//! # Example
//!
//! ```
//! use rtic_monotonics::stm32::*;
//! use rtic_monotonics::stm32::Tim2 as Mono;
//! use rtic_monotonics::Monotonic;
//! use embassy_stm32::peripherals::TIM2;
//! use embassy_stm32::rcc::low_level::RccPeripheral;
//!
//! fn init() {
//!     // Generate timer token to ensure correct timer interrupt handler is used.
//!     let token = rtic_monotonics::create_stm32_tim2_monotonic_token!();
//!
//!     // If using `embassy-stm32` HAL, timer clock can be read out like this:
//!     let timer_clock_hz = TIM2::frequency();
//!     // Or define it manually if you are using other HAL or know correct frequency:
//!     let timer_clock_hz = 64_000_000;
//!
//!     // Start the monotonic
//!     Mono::start(timer_clock_hz, token);
//! }
//!
//! async fn usage() {
//!     loop {
//!          // Use the monotonic
//!          let timestamp = Mono::now().ticks();
//!          Mono::delay(100.millis()).await;
//!     }
//! }
//! ```

use crate::{Monotonic, TimeoutError, TimerQueue};
use atomic_polyfill::{AtomicU64, Ordering};
pub use fugit::{self, ExtU64};
use pac::metadata::METADATA;
use stm32_metapac as pac;

const TIMER_HZ: u32 = 1_000_000;

#[doc(hidden)]
#[macro_export]
macro_rules! __internal_create_stm32_timer_interrupt {
    ($mono_timer:ident, $timer:ident, $timer_token:ident) => {{
        #[no_mangle]
        #[allow(non_snake_case)]
        unsafe extern "C" fn $timer() {
            $crate::stm32::$mono_timer::__tq().on_monotonic_interrupt();
        }

        pub struct $timer_token;

        unsafe impl $crate::InterruptToken<$crate::stm32::$mono_timer> for $timer_token {}

        $timer_token
    }};
}

/// Register TIM2 interrupt for the monotonic.
#[macro_export]
macro_rules! create_stm32_tim2_monotonic_token {
    () => {{
        $crate::__internal_create_stm32_timer_interrupt!(Tim2, TIM2, Tim2Token)
    }};
}

/// Register TIM3 interrupt for the monotonic.
#[macro_export]
macro_rules! create_stm32_tim3_monotonic_token {
    () => {{
        $crate::__internal_create_stm32_timer_interrupt!(Tim3, TIM3, Tim3Token)
    }};
}

macro_rules! make_timer {
    ($mono_name:ident, $timer:ident, $bits:ident, $set_tim_en:ident, $set_tim_rst:ident, $overflow:ident, $tq:ident$(, doc: ($($doc:tt)*))?) => {
        /// Monotonic timer queue implementation.
        $(
            #[cfg_attr(docsrs, doc(cfg($($doc)*)))]
        )?

        pub struct $mono_name;

        use pac::$timer;

        static $overflow: AtomicU64 = AtomicU64::new(0);
        static $tq: TimerQueue<$mono_name> = TimerQueue::new();

        impl $mono_name {
            /// Start monotonic timer. Must be called only once.
            /// `tim_clock_hz` shows to which frequency `TIMx` clock source is configured.
            pub fn start(tim_clock_hz: u32, _interrupt_token: impl crate::InterruptToken<Self>) {
                pac::RCC.apbenr1().modify(|r| r.$set_tim_en(true));
                pac::RCC.apbrstr1().modify(|r| r.$set_tim_rst(true));
                pac::RCC.apbrstr1().modify(|r| r.$set_tim_rst(false));

                $timer.cr1().modify(|r| r.set_cen(false));

                let psc = tim_clock_hz / TIMER_HZ - 1;
                $timer.psc().write(|r| r.set_psc(psc as u16));

                // Enable update event interrupt.
                $timer.dier().modify(|r| r.set_uie(true));

                // Trigger an update event to load the prescaler value to the clock.
                $timer.egr().write(|r| r.set_ug(true));

                // The above line raises an update event which will indicate that the timer is already finished.
                // Since this is not the case, it should be cleared.
                $timer.sr().modify(|r| r.set_uif(false));

                // Start the counter.
                $timer.cr1().modify(|r| {
                    r.set_cen(true);
                });

                $tq.initialize(Self {});

                // SAFETY: We take full ownership of the peripheral and interrupt vector,
                // plus we are not using any external shared resources so we won't impact
                // basepri/source masking based critical sections.
                unsafe {
                    crate::set_monotonic_prio(METADATA.nvic_priority_bits.unwrap(), pac::Interrupt::$timer);
                    cortex_m::peripheral::NVIC::unmask(pac::Interrupt::$timer);
                }
            }

            /// Used to access the underlying timer queue
            #[doc(hidden)]
            pub fn __tq() -> &'static TimerQueue<$mono_name> {
                &$tq
            }

            fn is_overflow() -> bool {
                $timer.sr().read().uif()
            }

            /// Delay for some duration of time.
            #[inline]
            pub async fn delay(duration: <Self as Monotonic>::Duration) {
                $tq.delay(duration).await;
            }
            /// Timeout at a specific time.
            pub async fn timeout_at<F: core::future::Future>(
                instant: <Self as rtic_time::Monotonic>::Instant,
                future: F,
            ) -> Result<F::Output, TimeoutError> {
                $tq.timeout_at(instant, future).await
            }

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

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

        impl Monotonic for $mono_name {
            type Instant = fugit::TimerInstantU64<TIMER_HZ>;
            type Duration = fugit::TimerDurationU64<TIMER_HZ>;

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

            fn now() -> Self::Instant {
                let cnt = $timer.cnt().read().cnt();

                // If the overflow bit is set, we add this to the timer value. It means the `on_interrupt`
                // has not yet happened, and we need to compensate here.
                let ovf: u64 = if Self::is_overflow() {
                    $bits::MAX as u64 + 1
                } else {
                    0
                };

                Self::Instant::from_ticks(cnt as u64 + ovf + $overflow.load(Ordering::SeqCst))
            }

            fn set_compare(instant: Self::Instant) {
                let now = Self::now();
                let max_ticks = $bits::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) {
                    None => 0, // In the past
                    Some(x) if x.ticks() <= max_ticks => instant.duration_since_epoch().ticks() as $bits, // Will not overflow
                    Some(_x) => $timer.cnt().read().cnt().wrapping_add($bits::MAX - 1), // Will overflow
                };

                $timer.ccr(1).write(|r| r.set_ccr(val));
            }

            fn clear_compare_flag() {
                $timer.sr().modify(|r| r.set_ccif(1, false));
            }

            fn pend_interrupt() {
                cortex_m::peripheral::NVIC::pend(pac::Interrupt::$timer);
            }

            fn enable_timer() {
                $timer.dier().modify(|r| r.set_ccie(1, true));
            }

            fn disable_timer() {
                $timer.dier().modify(|r| r.set_ccie(1, false));
            }

            fn on_interrupt() {
                if Self::is_overflow() {
                    $timer.sr().modify(|r| r.set_uif(false));

                    $overflow.fetch_add($bits::MAX as u64 + 1, Ordering::SeqCst);
                }
            }
        }
    };
}

make_timer!(
    Tim2,
    TIM2,
    u32,
    set_tim2en,
    set_tim2rst,
    TIMER2_OVERFLOWS,
    TIMER2_TQ
);
make_timer!(
    Tim3,
    TIM3,
    u16,
    set_tim3en,
    set_tim3rst,
    TIMER3_OVERFLOWS,
    TIMER3_TQ
);