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//! ```
3233/// Common definitions and traits for using the STM32 monotonics
34pub mod prelude {
35#[cfg(feature = "stm32_tim2")]
36pub use crate::stm32_tim2_monotonic;
3738#[cfg(feature = "stm32_tim3")]
39pub use crate::stm32_tim3_monotonic;
4041#[cfg(feature = "stm32_tim4")]
42pub use crate::stm32_tim4_monotonic;
4344#[cfg(feature = "stm32_tim5")]
45pub use crate::stm32_tim5_monotonic;
4647#[cfg(feature = "stm32_tim15")]
48pub use crate::stm32_tim15_monotonic;
4950pub use crate::Monotonic;
51pub use fugit::{self, ExtU64, ExtU64Ceil};
52}
5354use portable_atomic::{AtomicU64, Ordering};
55use rtic_time::{
56 half_period_counter::calculate_now,
57 timer_queue::{TimerQueue, TimerQueueBackend},
58};
59use stm32_metapac as pac;
6061mod _generated {
62#![allow(dead_code)]
63 #![allow(unused_imports)]
64 #![allow(non_snake_case)]
6566include!(concat!(env!("OUT_DIR"), "/_generated.rs"));
67}
6869#[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)]
75unsafe extern "C" fn $interrupt_name() {
76use $crate::TimerQueueBackend;
77$crate::stm32::$mono_backend::timer_queue().on_monotonic_interrupt();
78 }
79 };
80}
8182#[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.
87pub struct $name;
8889impl $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.
98pub fn start(tim_clock_hz: u32) {
99$crate::__internal_create_stm32_timer_interrupt!($mono_backend, $timer);
100101$crate::stm32::$mono_backend::_start(tim_clock_hz, $tick_rate_hz);
102 }
103 }
104105impl $crate::TimerQueueBasedMonotonic for $name {
106type Backend = $crate::stm32::$mono_backend;
107type Instant = $crate::fugit::Instant<
108 <Self::Backend as $crate::TimerQueueBackend>::Ticks,
1091,
110 { $tick_rate_hz },
111 >;
112type Duration = $crate::fugit::Duration<
113 <Self::Backend as $crate::TimerQueueBackend>::Ticks,
1141,
115 { $tick_rate_hz },
116 >;
117 }
118119$crate::rtic_time::impl_embedded_hal_delay_fugit!($name);
120$crate::rtic_time::impl_embedded_hal_async_delay_fugit!($name);
121 };
122}
123124/// 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}
140141/// 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}
157158/// 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}
174175/// 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}
191192/// 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}
208209macro_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)?
215216pub struct $backend_name;
217218use pac::$timer;
219220static $overflow: AtomicU64 = AtomicU64::new(0);
221static $tq: TimerQueue<$backend_name> = TimerQueue::new();
222223impl $backend_name {
224/// Starts the timer.
225 ///
226 /// **Do not use this function directly.**
227 ///
228 /// Use the prelude macros instead.
229pub fn _start(tim_clock_hz: u32, timer_hz: u32) {
230 _generated::$timer::enable();
231 _generated::$timer::reset();
232233$timer.cr1().modify(|r| r.set_cen(false));
234235assert!((tim_clock_hz % timer_hz) == 0, "Unable to find suitable timer prescaler value!");
236let psc = tim_clock_hz / timer_hz - 1;
237$timer.psc().write(|r| r.set_psc(psc as u16));
238239// Enable full-period interrupt.
240$timer.dier().modify(|r| r.set_uie(true));
241242// 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));
245246// Trigger an update event to load the prescaler value to the clock.
247$timer.egr().write(|r| r.set_ug(true));
248249// Clear timer value so it is known that we are at the first half period
250$timer.cnt().write(|r| r.set_cnt(1));
251252// 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 });
260261$tq.initialize(Self {});
262$overflow.store(0, Ordering::SeqCst);
263264// Start the counter.
265$timer.cr1().modify(|r| {
266 r.set_cen(true);
267 });
268269// 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.
272unsafe {
273crate::set_monotonic_prio(_generated::NVIC_PRIO_BITS, pac::Interrupt::$timer);
274 cortex_m::peripheral::NVIC::unmask(pac::Interrupt::$timer);
275 }
276 }
277 }
278279impl TimerQueueBackend for $backend_name {
280type Ticks = u64;
281282fn now() -> Self::Ticks {
283 calculate_now(
284 || $overflow.load(Ordering::Relaxed),
285 || $timer.cnt().read().cnt()
286 )
287 }
288289fn set_compare(instant: Self::Ticks) {
290let now = Self::now();
291292// 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.
294let val = if instant.wrapping_sub(now) <= ($bits::MAX as u64) {
295 instant as $bits
296} else {
297// In the past or will overflow
2980
299};
300301$timer.ccr(1).write(|r| r.set_ccr(val.into()));
302 }
303304fn clear_compare_flag() {
305$timer.sr().write(|r| {
306 r.0 = !0;
307 r.set_ccif(1, false);
308 });
309 }
310311fn pend_interrupt() {
312 cortex_m::peripheral::NVIC::pend(pac::Interrupt::$timer);
313 }
314315fn enable_timer() {
316$timer.dier().modify(|r| r.set_ccie(1, true));
317 }
318319fn disable_timer() {
320$timer.dier().modify(|r| r.set_ccie(1, false));
321 }
322323fn on_interrupt() {
324// Full period
325if $timer.sr().read().uif() {
326$timer.sr().write(|r| {
327 r.0 = !0;
328 r.set_uif(false);
329 });
330let prev = $overflow.fetch_add(1, Ordering::Relaxed);
331assert!(prev % 2 == 1, "Monotonic must have missed an interrupt!");
332 }
333// Half period
334if $timer.sr().read().ccif(0) {
335$timer.sr().write(|r| {
336 r.0 = !0;
337 r.set_ccif(0, false);
338 });
339let prev = $overflow.fetch_add(1, Ordering::Relaxed);
340assert!(prev % 2 == 0, "Monotonic must have missed an interrupt!");
341 }
342 }
343344fn timer_queue() -> &'static TimerQueue<$backend_name> {
345&$tq
346}
347 }
348 };
349}
350351#[cfg(feature = "stm32_tim2")]
352make_timer!(Tim2Backend, TIM2, u32, TIMER2_OVERFLOWS, TIMER2_TQ);
353354#[cfg(feature = "stm32_tim3")]
355make_timer!(Tim3Backend, TIM3, u16, TIMER3_OVERFLOWS, TIMER3_TQ);
356357#[cfg(feature = "stm32_tim4")]
358make_timer!(Tim4Backend, TIM4, u16, TIMER4_OVERFLOWS, TIMER4_TQ);
359360#[cfg(feature = "stm32_tim5")]
361make_timer!(Tim5Backend, TIM5, u16, TIMER5_OVERFLOWS, TIMER5_TQ);
362363#[cfg(feature = "stm32_tim15")]
364make_timer!(Tim15Backend, TIM15, u16, TIMER15_OVERFLOWS, TIMER15_TQ);