Skip to content

Commit 8507004

Browse files
committed
generic impl od monotonic
1 parent b5aa071 commit 8507004

File tree

4 files changed

+51
-58
lines changed

4 files changed

+51
-58
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Changed
1111

12+
- Make `Monotonic` implementation generic for `u32` timers, fix `PwmExt` trait [#454]
1213
- Rename `Delay<SYST>` to SysDelay, remove old `Delay<TIM2>`
1314
- Extend timers to 32bit on `Delay`
1415
- Move `MonoTimer` from `timer` to dwt mode [#448]
@@ -65,6 +66,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
6566
[#447]: https://github.com/stm32-rs/stm32f4xx-hal/pull/447
6667
[#448]: https://github.com/stm32-rs/stm32f4xx-hal/pull/448
6768
[#449]: https://github.com/stm32-rs/stm32f4xx-hal/pull/449
69+
[#454]: https://github.com/stm32-rs/stm32f4xx-hal/pull/454
6870

6971
### Changed
7072

src/prelude.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ pub use crate::spi::SpiExt as _stm32f4xx_hal_spi_SpiExt;
7373
pub use crate::syscfg::SysCfgExt as _stm32f4xx_hal_syscfg_SysCfgExt;
7474
pub use crate::time::U32Ext as _stm32f4xx_hal_time_U32Ext;
7575
#[cfg(feature = "rtic")]
76-
#[cfg(not(feature = "stm32f410"))]
7776
pub use crate::timer::MonoTimerExt as _stm32f4xx_hal_timer_MonoTimerExt;
7877
pub use crate::timer::PwmExt as _stm32f4xx_hal_timer_PwmExt;
7978
pub use crate::timer::SysTimerExt as _stm32f4xx_hal_timer_SysCounterExt;

src/timer.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,11 @@ mod pins;
2222
pub use pins::*;
2323
pub mod pwm;
2424
pub use pwm::*;
25-
#[cfg(not(feature = "stm32f410"))]
2625
pub mod pwm_input;
27-
#[cfg(not(feature = "stm32f410"))]
2826
pub use pwm_input::PwmInput;
2927
#[cfg(feature = "rtic")]
30-
#[cfg(not(feature = "stm32f410"))]
3128
pub mod monotonic;
3229
#[cfg(feature = "rtic")]
33-
#[cfg(not(feature = "stm32f410"))]
3430
pub use monotonic::*;
3531

3632
mod hal_02;
@@ -233,6 +229,7 @@ mod sealed {
233229
fn get_interrupt_flag(&self) -> Event;
234230
fn read_count(&self) -> Self::Width;
235231
fn start_one_pulse(&mut self);
232+
fn start_no_update(&mut self);
236233
fn cr1_reset(&mut self);
237234
}
238235

@@ -353,6 +350,10 @@ macro_rules! hal {
353350
self.cr1.write(|w| unsafe { w.bits(1 << 3) }.cen().set_bit());
354351
}
355352
#[inline(always)]
353+
fn start_no_update(&mut self) {
354+
self.cr1.write(|w| w.cen().set_bit().udis().set_bit());
355+
}
356+
#[inline(always)]
356357
fn cr1_reset(&mut self) {
357358
self.cr1.reset();
358359
}

src/timer/monotonic.rs

Lines changed: 44 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RTIC Monotonic impl for the 32-bit timers
2-
use super::{FTimer, Instance};
2+
use super::{Channel, Event, FTimer, General, Instance, WithPwm};
33
use crate::rcc::Clocks;
44
use core::ops::{Deref, DerefMut};
55
pub use fugit::{self, ExtU32};
@@ -39,64 +39,55 @@ pub trait MonoTimerExt: Sized {
3939
}
4040
}
4141

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+
}
5650

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+
}
6664

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>;
7071

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+
}
7475

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+
}
7980

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+
}
8584

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+
}
8988

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)
9692
}
9793
}
98-
99-
mono!(crate::pac::TIM5,);
100-
101-
#[cfg(feature = "tim2")]
102-
mono!(crate::pac::TIM2,);

0 commit comments

Comments
 (0)