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