|
| 1 | +//! RTIC Monotonic implementation |
| 2 | +
|
| 3 | +use core::convert::TryInto; |
| 4 | + |
| 5 | +use crate::{rcc::Clocks, timer::Timer}; |
| 6 | +pub use fugit::{self, ExtU32}; |
| 7 | +use rtic_monotonic::Monotonic; |
| 8 | + |
| 9 | +pub struct MonoTimer<T, const FREQ: u32> { |
| 10 | + tim: T, |
| 11 | + ovf: u32, |
| 12 | +} |
| 13 | + |
| 14 | +macro_rules! mono { |
| 15 | + ($($TIM:ty,)+) => { |
| 16 | + $( |
| 17 | + impl Timer<$TIM> { |
| 18 | + pub fn monotonic<const FREQ: u32>(self) -> MonoTimer<$TIM, FREQ> { |
| 19 | + MonoTimer::<$TIM, FREQ>::_new(self) |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + impl<const FREQ: u32> MonoTimer<$TIM, FREQ> { |
| 24 | + pub fn new(timer: $TIM, clocks: &Clocks) -> Self { |
| 25 | + Timer::<$TIM>::new(timer, clocks).monotonic() |
| 26 | + } |
| 27 | + |
| 28 | + fn _new(timer: Timer<$TIM>) -> Self { |
| 29 | + let Timer { tim, clk } = timer; |
| 30 | + // Configure timer. If the u16 conversion panics, try increasing FREQ. |
| 31 | + let psc: u16 = (clk.0 / FREQ - 1).try_into().unwrap(); |
| 32 | + tim.psc.write(|w| w.psc().bits(psc)); // Set prescaler. |
| 33 | + tim.arr.write(|w| w.arr().bits(u16::MAX)); // Set auto-reload value. |
| 34 | + tim.egr.write(|w| w.ug().set_bit()); // Generate interrupt on overflow. |
| 35 | + |
| 36 | + // Start timer. |
| 37 | + tim.sr.modify(|_, w| w.uif().clear_bit()); // Clear interrupt flag. |
| 38 | + tim.cr1.modify(|_, w| { |
| 39 | + w.cen() |
| 40 | + .set_bit() // Enable counter. |
| 41 | + .udis() |
| 42 | + .clear_bit() // Overflow should trigger update event. |
| 43 | + .urs() |
| 44 | + .set_bit() // Only overflow triggers interrupt. |
| 45 | + }); |
| 46 | + |
| 47 | + Self { tim, ovf: 0 } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + impl<const FREQ: u32> Monotonic for MonoTimer<$TIM, FREQ> { |
| 52 | + type Instant = fugit::TimerInstantU32<FREQ>; |
| 53 | + type Duration = fugit::TimerDurationU32<FREQ>; |
| 54 | + |
| 55 | + unsafe fn reset(&mut self) { |
| 56 | + self.tim.dier.modify(|_, w| w.cc1ie().set_bit()); |
| 57 | + } |
| 58 | + |
| 59 | + #[inline(always)] |
| 60 | + fn now(&mut self) -> Self::Instant { |
| 61 | + let cnt = self.tim.cnt.read().cnt().bits() as u32; |
| 62 | + |
| 63 | + // If the overflow bit is set, we add this to the timer value. It means the `on_interrupt` |
| 64 | + // has not yet happened, and we need to compensate here. |
| 65 | + let ovf = if self.tim.sr.read().uif().bit_is_set() { |
| 66 | + 0x10000 |
| 67 | + } else { |
| 68 | + 0 |
| 69 | + }; |
| 70 | + |
| 71 | + Self::Instant::from_ticks(cnt.wrapping_add(ovf).wrapping_add(self.ovf)) |
| 72 | + } |
| 73 | + |
| 74 | + fn set_compare(&mut self, instant: Self::Instant) { |
| 75 | + let now = self.now(); |
| 76 | + let cnt = self.tim.cnt.read().cnt().bits(); |
| 77 | + |
| 78 | + // Since the timer may or may not overflow based on the requested compare val, we check |
| 79 | + // how many ticks are left. |
| 80 | + let val = match instant.checked_duration_since(now) { |
| 81 | + None => cnt.wrapping_add(0xffff), // In the past, RTIC will handle this |
| 82 | + Some(x) if x.ticks() <= 0xffff => instant.duration_since_epoch().ticks() as u16, // Will not overflow |
| 83 | + Some(_) => cnt.wrapping_add(0xffff), // Will overflow, run for as long as possible |
| 84 | + }; |
| 85 | + |
| 86 | + self.tim.ccr1.write(|w| w.ccr().bits(val)); |
| 87 | + } |
| 88 | + |
| 89 | + fn clear_compare_flag(&mut self) { |
| 90 | + self.tim.sr.modify(|_, w| w.cc1if().clear_bit()); |
| 91 | + } |
| 92 | + |
| 93 | + fn on_interrupt(&mut self) { |
| 94 | + // If there was an overflow, increment the overflow counter. |
| 95 | + if self.tim.sr.read().uif().bit_is_set() { |
| 96 | + self.tim.sr.modify(|_, w| w.uif().clear_bit()); |
| 97 | + |
| 98 | + self.ovf += 0x10000; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + #[inline(always)] |
| 103 | + fn zero() -> Self::Instant { |
| 104 | + Self::Instant::from_ticks(0) |
| 105 | + } |
| 106 | + } |
| 107 | + )+ |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +mono!(crate::pac::TIM2, crate::pac::TIM3,); |
| 112 | + |
| 113 | +#[cfg(any(feature = "stm32f100", feature = "stm32f103", feature = "connectivity",))] |
| 114 | +mono!(crate::pac::TIM1,); |
| 115 | + |
| 116 | +#[cfg(feature = "medium")] |
| 117 | +mono!(crate::pac::TIM4,); |
| 118 | + |
| 119 | +#[cfg(any(feature = "high", feature = "connectivity"))] |
| 120 | +mono!(crate::pac::TIM5,); |
| 121 | + |
| 122 | +#[cfg(all(feature = "stm32f103", feature = "high",))] |
| 123 | +mono!(crate::pac::TIM8,); |
0 commit comments