|
1 | 1 | // RTIC Monotonic impl for the 32-bit timers |
2 | | -use super::{FTimer, Instance}; |
| 2 | +use super::{Channel, Event, FTimer, General, Instance, WithPwm}; |
3 | 3 | use crate::rcc::Clocks; |
4 | 4 | use core::ops::{Deref, DerefMut}; |
5 | 5 | pub use fugit::{self, ExtU32}; |
@@ -39,64 +39,55 @@ pub trait MonoTimerExt: Sized { |
39 | 39 | } |
40 | 40 | } |
41 | 41 |
|
42 | | -macro_rules! mono { |
43 | | - ($($TIM:ty,)+) => { |
44 | | - $( |
45 | | - impl MonoTimerExt for $TIM { |
46 | | - fn monotonic<const FREQ: u32>(self, clocks: &Clocks) -> MonoTimer<Self, FREQ> { |
47 | | - FTimer::new(self, clocks).monotonic() |
48 | | - } |
49 | | - } |
50 | | - |
51 | | - impl<const FREQ: u32> FTimer<$TIM, FREQ> { |
52 | | - pub fn monotonic(self) -> MonoTimer<$TIM, FREQ> { |
53 | | - MonoTimer::<$TIM, FREQ>::_new(self) |
54 | | - } |
55 | | - } |
| 42 | +impl<TIM> MonoTimerExt for TIM |
| 43 | +where |
| 44 | + Self: Instance + General<Width = u32> + WithPwm, |
| 45 | +{ |
| 46 | + fn monotonic<const FREQ: u32>(self, clocks: &Clocks) -> MonoTimer<Self, FREQ> { |
| 47 | + FTimer::new(self, clocks).monotonic() |
| 48 | + } |
| 49 | +} |
56 | 50 |
|
57 | | - impl<const FREQ: u32> MonoTimer<$TIM, FREQ> { |
58 | | - fn _new(timer: FTimer<$TIM, FREQ>) -> Self { |
59 | | - timer.tim.arr.write(|w| unsafe { w.bits(u32::MAX) }); |
60 | | - timer.tim.egr.write(|w| w.ug().set_bit()); |
61 | | - timer.tim.sr.modify(|_, w| w.uif().clear_bit()); |
62 | | - timer.tim.cr1.modify(|_, w| w.cen().set_bit().udis().set_bit()); |
63 | | - Self(timer) |
64 | | - } |
65 | | - } |
| 51 | +impl<TIM, const FREQ: u32> FTimer<TIM, FREQ> |
| 52 | +where |
| 53 | + TIM: Instance + General<Width = u32> + WithPwm, |
| 54 | +{ |
| 55 | + pub fn monotonic(mut self) -> MonoTimer<TIM, FREQ> { |
| 56 | + unsafe { |
| 57 | + self.tim.set_auto_reload_unchecked(TIM::max_auto_reload()); |
| 58 | + } |
| 59 | + self.tim.trigger_update(); |
| 60 | + self.tim.start_no_update(); |
| 61 | + MonoTimer(self) |
| 62 | + } |
| 63 | +} |
66 | 64 |
|
67 | | - impl<const FREQ: u32> Monotonic for MonoTimer<$TIM, FREQ> { |
68 | | - type Instant = fugit::TimerInstantU32<FREQ>; |
69 | | - type Duration = fugit::TimerDurationU32<FREQ>; |
| 65 | +impl<TIM, const FREQ: u32> Monotonic for MonoTimer<TIM, FREQ> |
| 66 | +where |
| 67 | + TIM: Instance + General<Width = u32> + WithPwm, |
| 68 | +{ |
| 69 | + type Instant = fugit::TimerInstantU32<FREQ>; |
| 70 | + type Duration = fugit::TimerDurationU32<FREQ>; |
70 | 71 |
|
71 | | - unsafe fn reset(&mut self) { |
72 | | - self.tim.dier.modify(|_, w| w.cc1ie().set_bit()); |
73 | | - } |
| 72 | + unsafe fn reset(&mut self) { |
| 73 | + self.tim.listen_interrupt(Event::C1, true); |
| 74 | + } |
74 | 75 |
|
75 | | - #[inline(always)] |
76 | | - fn now(&mut self) -> Self::Instant { |
77 | | - Self::Instant::from_ticks(self.tim.cnt.read().cnt().bits()) |
78 | | - } |
| 76 | + #[inline(always)] |
| 77 | + fn now(&mut self) -> Self::Instant { |
| 78 | + Self::Instant::from_ticks(self.tim.read_count()) |
| 79 | + } |
79 | 80 |
|
80 | | - fn set_compare(&mut self, instant: Self::Instant) { |
81 | | - self.tim |
82 | | - .ccr1 |
83 | | - .write(|w| w.ccr().bits(instant.duration_since_epoch().ticks())); |
84 | | - } |
| 81 | + fn set_compare(&mut self, instant: Self::Instant) { |
| 82 | + TIM::set_cc_value(Channel::C1 as u8, instant.duration_since_epoch().ticks()); |
| 83 | + } |
85 | 84 |
|
86 | | - fn clear_compare_flag(&mut self) { |
87 | | - self.tim.sr.modify(|_, w| w.cc1if().clear_bit()); |
88 | | - } |
| 85 | + fn clear_compare_flag(&mut self) { |
| 86 | + self.tim.clear_interrupt_flag(Event::C1); |
| 87 | + } |
89 | 88 |
|
90 | | - #[inline(always)] |
91 | | - fn zero() -> Self::Instant { |
92 | | - Self::Instant::from_ticks(0) |
93 | | - } |
94 | | - } |
95 | | - )+ |
| 89 | + #[inline(always)] |
| 90 | + fn zero() -> Self::Instant { |
| 91 | + Self::Instant::from_ticks(0) |
96 | 92 | } |
97 | 93 | } |
98 | | - |
99 | | -mono!(crate::pac::TIM5,); |
100 | | - |
101 | | -#[cfg(feature = "tim2")] |
102 | | -mono!(crate::pac::TIM2,); |
0 commit comments