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 psc = tim_clock_hz / timer_hz - 1;
242                $timer.psc().write(|r| r.set_psc(psc as u16));
243
244                // Enable full-period interrupt.
245                $timer.dier().modify(|r| r.set_uie(true));
246
247                // Configure and enable half-period interrupt
248                $timer.ccr(0).write(|r| r.set_ccr(($bits::MAX - ($bits::MAX >> 1)).into()));
249                $timer.dier().modify(|r| r.set_ccie(0, true));
250
251                // Trigger an update event to load the prescaler value to the clock.
252                $timer.egr().write(|r| r.set_ug(true));
253
254                // Clear timer value so it is known that we are at the first half period
255                $timer.cnt().write(|r| r.set_cnt(1));
256
257                // Triggering the update event might have raised overflow interrupts.
258                // Clear them to return to a known state.
259                $timer.sr().write(|r| {
260                    r.0 = !0;
261                    r.set_uif(false);
262                    r.set_ccif(0, false);
263                    r.set_ccif(1, false);
264                });
265
266                $tq.initialize(Self {});
267                $overflow.store(0, Ordering::SeqCst);
268
269                // Start the counter.
270                $timer.cr1().modify(|r| {
271                    r.set_cen(true);
272                });
273
274                // SAFETY: We take full ownership of the peripheral and interrupt vector,
275                // plus we are not using any external shared resources so we won't impact
276                // basepri/source masking based critical sections.
277                unsafe {
278                    crate::set_monotonic_prio(_generated::NVIC_PRIO_BITS, pac::Interrupt::$timer);
279                    cortex_m::peripheral::NVIC::unmask(pac::Interrupt::$timer);
280                }
281            }
282        }
283
284        impl TimerQueueBackend for $backend_name {
285            type Ticks = u64;
286
287            fn now() -> Self::Ticks {
288                calculate_now(
289                    || $overflow.load(Ordering::Relaxed),
290                    || $timer.cnt().read().cnt()
291                )
292            }
293
294            fn set_compare(instant: Self::Ticks) {
295                let now = Self::now();
296
297                // Since the timer may or may not overflow based on the requested compare val, we check how many ticks are left.
298                // `wrapping_sub` takes care of the u64 integer overflow special case.
299                let val = if instant.wrapping_sub(now) <= ($bits::MAX as u64) {
300                    instant as $bits
301                } else {
302                    // In the past or will overflow
303                    0
304                };
305
306                $timer.ccr(1).write(|r| r.set_ccr(val.into()));
307            }
308
309            fn clear_compare_flag() {
310                $timer.sr().write(|r| {
311                    r.0 = !0;
312                    r.set_ccif(1, false);
313                });
314            }
315
316            fn pend_interrupt() {
317                cortex_m::peripheral::NVIC::pend(pac::Interrupt::$timer);
318            }
319
320            fn enable_timer() {
321                $timer.dier().modify(|r| r.set_ccie(1, true));
322            }
323
324            fn disable_timer() {
325                $timer.dier().modify(|r| r.set_ccie(1, false));
326            }
327
328            fn on_interrupt() {
329                // Full period
330                if $timer.sr().read().uif() {
331                    $timer.sr().write(|r| {
332                        r.0 = !0;
333                        r.set_uif(false);
334                    });
335                    let prev = $overflow.fetch_add(1, Ordering::Relaxed);
336                    assert!(prev % 2 == 1, "Monotonic must have missed an interrupt!");
337                }
338                // Half period
339                if $timer.sr().read().ccif(0) {
340                    $timer.sr().write(|r| {
341                        r.0 = !0;
342                        r.set_ccif(0, false);
343                    });
344                    let prev = $overflow.fetch_add(1, Ordering::Relaxed);
345                    assert!(prev % 2 == 0, "Monotonic must have missed an interrupt!");
346                }
347            }
348
349            fn timer_queue() -> &'static TimerQueue<$backend_name> {
350                &$tq
351            }
352        }
353    };
354}
355
356#[cfg(feature = "stm32_tim2")]
357make_timer!(Tim2Backend, TIM2, u32, TIMER2_OVERFLOWS, TIMER2_TQ);
358
359#[cfg(feature = "stm32_tim3")]
360make_timer!(Tim3Backend, TIM3, u16, TIMER3_OVERFLOWS, TIMER3_TQ);
361
362#[cfg(feature = "stm32_tim4")]
363make_timer!(Tim4Backend, TIM4, u16, TIMER4_OVERFLOWS, TIMER4_TQ);
364
365#[cfg(feature = "stm32_tim5")]
366make_timer!(Tim5Backend, TIM5, u16, TIMER5_OVERFLOWS, TIMER5_TQ);
367
368#[cfg(feature = "stm32_tim15")]
369make_timer!(Tim15Backend, TIM15, u16, TIMER15_OVERFLOWS, TIMER15_TQ);