rtic_monotonics/
stm32.rs

1//! [`Monotonic`](rtic_time::Monotonic) implementations for STM32 chips.
2//!
3//! Not all timers are available on all parts. Ensure that only available
4//! timers are exposed by having the correct `stm32*` feature enabled for `rtic-monotonics`.
5//!
6//! # Example
7//!
8//! ```
9//! use rtic_monotonics::stm32::prelude::*;
10//!
11//! // Define the monotonic and set it to 1MHz tick rate
12//! stm32_tim2_monotonic!(Mono, 1_000_000);
13//!
14//! fn init() {
15//!     // If using `embassy-stm32` HAL, timer clock can be read out like this:
16//!     let timer_clock_hz = embassy_stm32::peripherals::TIM2::frequency();
17//!     // Or define it manually if you are using other HAL or know correct frequency:
18//!     let timer_clock_hz = 64_000_000;
19//!
20//!     // Start the monotonic
21//!     Mono::start(timer_clock_hz);
22//! }
23//!
24//! async fn usage() {
25//!     loop {
26//!          // Use the monotonic
27//!          let timestamp = Mono::now();
28//!          Mono::delay(100.millis()).await;
29//!     }
30//! }
31//! ```
32
33/// Common definitions and traits for using the STM32 monotonics
34pub mod prelude {
35    #[cfg(feature = "stm32_tim2")]
36    pub use crate::stm32_tim2_monotonic;
37
38    #[cfg(feature = "stm32_tim3")]
39    pub use crate::stm32_tim3_monotonic;
40
41    #[cfg(feature = "stm32_tim4")]
42    pub use crate::stm32_tim4_monotonic;
43
44    #[cfg(feature = "stm32_tim5")]
45    pub use crate::stm32_tim5_monotonic;
46
47    #[cfg(feature = "stm32_tim15")]
48    pub use crate::stm32_tim15_monotonic;
49
50    pub use crate::Monotonic;
51    pub use fugit::{self, ExtU64, ExtU64Ceil};
52}
53
54use portable_atomic::{AtomicU64, Ordering};
55use rtic_time::{
56    half_period_counter::calculate_now,
57    timer_queue::{TimerQueue, TimerQueueBackend},
58};
59use stm32_metapac as pac;
60
61mod _generated {
62    #![allow(dead_code)]
63    #![allow(unused_imports)]
64    #![allow(non_snake_case)]
65
66    include!(concat!(env!("OUT_DIR"), "/_generated.rs"));
67}
68
69#[doc(hidden)]
70#[macro_export]
71macro_rules! __internal_create_stm32_timer_interrupt {
72    ($mono_backend:ident, $interrupt_name:ident) => {
73        #[no_mangle]
74        #[allow(non_snake_case)]
75        unsafe extern "C" fn $interrupt_name() {
76            use $crate::TimerQueueBackend;
77            $crate::stm32::$mono_backend::timer_queue().on_monotonic_interrupt();
78        }
79    };
80}
81
82#[doc(hidden)]
83#[macro_export]
84macro_rules! __internal_create_stm32_timer_struct {
85    ($name:ident, $mono_backend:ident, $timer:ident, $tick_rate_hz:expr) => {
86        /// A `Monotonic` based on an STM32 timer peripheral.
87        pub struct $name;
88
89        impl $name {
90            /// Starts the `Monotonic`.
91            ///
92            /// - `tim_clock_hz`: `TIMx` peripheral clock frequency.
93            ///
94            /// Panics if it is impossible to achieve the desired monotonic tick rate based
95            /// on the given `tim_clock_hz` parameter. If that happens, adjust the desired monotonic tick rate.
96            ///
97            /// This method must be called only once.
98            pub fn start(tim_clock_hz: u32) {
99                $crate::__internal_create_stm32_timer_interrupt!($mono_backend, $timer);
100
101                $crate::stm32::$mono_backend::_start(tim_clock_hz, $tick_rate_hz);
102            }
103        }
104
105        impl $crate::TimerQueueBasedMonotonic for $name {
106            type Backend = $crate::stm32::$mono_backend;
107            type Instant = $crate::fugit::Instant<
108                <Self::Backend as $crate::TimerQueueBackend>::Ticks,
109                1,
110                { $tick_rate_hz },
111            >;
112            type Duration = $crate::fugit::Duration<
113                <Self::Backend as $crate::TimerQueueBackend>::Ticks,
114                1,
115                { $tick_rate_hz },
116            >;
117        }
118
119        $crate::rtic_time::impl_embedded_hal_delay_fugit!($name);
120        $crate::rtic_time::impl_embedded_hal_async_delay_fugit!($name);
121    };
122}
123
124/// Create a TIM2 based monotonic and register the TIM2 interrupt for it.
125///
126/// See [`crate::stm32`] for more details.
127///
128/// # Arguments
129///
130/// * `name` - The name that the monotonic type will have.
131/// * `tick_rate_hz` - The tick rate of the timer peripheral.
132///
133#[cfg(feature = "stm32_tim2")]
134#[macro_export]
135macro_rules! stm32_tim2_monotonic {
136    ($name:ident, $tick_rate_hz:expr) => {
137        $crate::__internal_create_stm32_timer_struct!($name, Tim2Backend, TIM2, $tick_rate_hz);
138    };
139}
140
141/// Create a TIM3 based monotonic and register the TIM3 interrupt for it.
142///
143/// See [`crate::stm32`] for more details.
144///
145/// # Arguments
146///
147/// * `name` - The name that the monotonic type will have.
148/// * `tick_rate_hz` - The tick rate of the timer peripheral.
149///
150#[cfg(feature = "stm32_tim3")]
151#[macro_export]
152macro_rules! stm32_tim3_monotonic {
153    ($name:ident, $tick_rate_hz:expr) => {
154        $crate::__internal_create_stm32_timer_struct!($name, Tim3Backend, TIM3, $tick_rate_hz);
155    };
156}
157
158/// Create a TIM4 based monotonic and register the TIM4 interrupt for it.
159///
160/// See [`crate::stm32`] for more details.
161///
162/// # Arguments
163///
164/// * `name` - The name that the monotonic type will have.
165/// * `tick_rate_hz` - The tick rate of the timer peripheral.
166///
167#[cfg(feature = "stm32_tim4")]
168#[macro_export]
169macro_rules! stm32_tim4_monotonic {
170    ($name:ident, $tick_rate_hz:expr) => {
171        $crate::__internal_create_stm32_timer_struct!($name, Tim4Backend, TIM4, $tick_rate_hz);
172    };
173}
174
175/// Create a TIM5 based monotonic and register the TIM5 interrupt for it.
176///
177/// See [`crate::stm32`] for more details.
178///
179/// # Arguments
180///
181/// * `name` - The name that the monotonic type will have.
182/// * `tick_rate_hz` - The tick rate of the timer peripheral.
183///
184#[cfg(feature = "stm32_tim5")]
185#[macro_export]
186macro_rules! stm32_tim5_monotonic {
187    ($name:ident, $tick_rate_hz:expr) => {
188        $crate::__internal_create_stm32_timer_struct!($name, Tim5Backend, TIM5, $tick_rate_hz);
189    };
190}
191
192/// Create a TIM15 based monotonic and register the TIM15 interrupt for it.
193///
194/// See [`crate::stm32`] for more details.
195///
196/// # Arguments
197///
198/// * `name` - The name that the monotonic type will have.
199/// * `tick_rate_hz` - The tick rate of the timer peripheral.
200///
201#[cfg(feature = "stm32_tim15")]
202#[macro_export]
203macro_rules! stm32_tim15_monotonic {
204    ($name:ident, $tick_rate_hz:expr) => {
205        $crate::__internal_create_stm32_timer_struct!($name, Tim15Backend, TIM15, $tick_rate_hz);
206    };
207}
208
209macro_rules! make_timer {
210    ($backend_name:ident, $timer:ident, $bits:ident, $overflow:ident, $tq:ident$(, doc: ($($doc:tt)*))?) => {
211        /// Monotonic timer backend implementation.
212        $(
213            #[cfg_attr(docsrs, doc(cfg($($doc)*)))]
214        )?
215
216        pub struct $backend_name;
217
218        use pac::$timer;
219
220        static $overflow: AtomicU64 = AtomicU64::new(0);
221        static $tq: TimerQueue<$backend_name> = TimerQueue::new();
222
223        impl $backend_name {
224            /// Starts the timer.
225            ///
226            /// **Do not use this function directly.**
227            ///
228            /// Use the prelude macros instead.
229            pub fn _start(tim_clock_hz: u32, timer_hz: u32) {
230                _generated::$timer::enable();
231                _generated::$timer::reset();
232
233                $timer.cr1().modify(|r| r.set_cen(false));
234
235                assert!((tim_clock_hz % timer_hz) == 0, "Unable to find suitable timer prescaler value!");
236                let psc = tim_clock_hz / timer_hz - 1;
237                $timer.psc().write(|r| r.set_psc(psc as u16));
238
239                // Enable full-period interrupt.
240                $timer.dier().modify(|r| r.set_uie(true));
241
242                // Configure and enable half-period interrupt
243                $timer.ccr(0).write(|r| r.set_ccr(($bits::MAX - ($bits::MAX >> 1)).into()));
244                $timer.dier().modify(|r| r.set_ccie(0, true));
245
246                // Trigger an update event to load the prescaler value to the clock.
247                $timer.egr().write(|r| r.set_ug(true));
248
249                // Clear timer value so it is known that we are at the first half period
250                $timer.cnt().write(|r| r.set_cnt(1));
251
252                // Triggering the update event might have raised overflow interrupts.
253                // Clear them to return to a known state.
254                $timer.sr().write(|r| {
255                    r.0 = !0;
256                    r.set_uif(false);
257                    r.set_ccif(0, false);
258                    r.set_ccif(1, false);
259                });
260
261                $tq.initialize(Self {});
262                $overflow.store(0, Ordering::SeqCst);
263
264                // Start the counter.
265                $timer.cr1().modify(|r| {
266                    r.set_cen(true);
267                });
268
269                // SAFETY: We take full ownership of the peripheral and interrupt vector,
270                // plus we are not using any external shared resources so we won't impact
271                // basepri/source masking based critical sections.
272                unsafe {
273                    crate::set_monotonic_prio(_generated::NVIC_PRIO_BITS, pac::Interrupt::$timer);
274                    cortex_m::peripheral::NVIC::unmask(pac::Interrupt::$timer);
275                }
276            }
277        }
278
279        impl TimerQueueBackend for $backend_name {
280            type Ticks = u64;
281
282            fn now() -> Self::Ticks {
283                calculate_now(
284                    || $overflow.load(Ordering::Relaxed),
285                    || $timer.cnt().read().cnt()
286                )
287            }
288
289            fn set_compare(instant: Self::Ticks) {
290                let now = Self::now();
291
292                // Since the timer may or may not overflow based on the requested compare val, we check how many ticks are left.
293                // `wrapping_sub` takes care of the u64 integer overflow special case.
294                let val = if instant.wrapping_sub(now) <= ($bits::MAX as u64) {
295                    instant as $bits
296                } else {
297                    // In the past or will overflow
298                    0
299                };
300
301                $timer.ccr(1).write(|r| r.set_ccr(val.into()));
302            }
303
304            fn clear_compare_flag() {
305                $timer.sr().write(|r| {
306                    r.0 = !0;
307                    r.set_ccif(1, false);
308                });
309            }
310
311            fn pend_interrupt() {
312                cortex_m::peripheral::NVIC::pend(pac::Interrupt::$timer);
313            }
314
315            fn enable_timer() {
316                $timer.dier().modify(|r| r.set_ccie(1, true));
317            }
318
319            fn disable_timer() {
320                $timer.dier().modify(|r| r.set_ccie(1, false));
321            }
322
323            fn on_interrupt() {
324                // Full period
325                if $timer.sr().read().uif() {
326                    $timer.sr().write(|r| {
327                        r.0 = !0;
328                        r.set_uif(false);
329                    });
330                    let prev = $overflow.fetch_add(1, Ordering::Relaxed);
331                    assert!(prev % 2 == 1, "Monotonic must have missed an interrupt!");
332                }
333                // Half period
334                if $timer.sr().read().ccif(0) {
335                    $timer.sr().write(|r| {
336                        r.0 = !0;
337                        r.set_ccif(0, false);
338                    });
339                    let prev = $overflow.fetch_add(1, Ordering::Relaxed);
340                    assert!(prev % 2 == 0, "Monotonic must have missed an interrupt!");
341                }
342            }
343
344            fn timer_queue() -> &'static TimerQueue<$backend_name> {
345                &$tq
346            }
347        }
348    };
349}
350
351#[cfg(feature = "stm32_tim2")]
352make_timer!(Tim2Backend, TIM2, u32, TIMER2_OVERFLOWS, TIMER2_TQ);
353
354#[cfg(feature = "stm32_tim3")]
355make_timer!(Tim3Backend, TIM3, u16, TIMER3_OVERFLOWS, TIMER3_TQ);
356
357#[cfg(feature = "stm32_tim4")]
358make_timer!(Tim4Backend, TIM4, u16, TIMER4_OVERFLOWS, TIMER4_TQ);
359
360#[cfg(feature = "stm32_tim5")]
361make_timer!(Tim5Backend, TIM5, u16, TIMER5_OVERFLOWS, TIMER5_TQ);
362
363#[cfg(feature = "stm32_tim15")]
364make_timer!(Tim15Backend, TIM15, u16, TIMER15_OVERFLOWS, TIMER15_TQ);