Skip to content

Commit 31e42e3

Browse files
committed
Use duration::Generic instead of Hertz for Timer impl
1 parent 6d2ded4 commit 31e42e3

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/timer.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use void::Void;
1616
use crate::hal::timer::{CountDown, Periodic};
1717
use crate::pac::{Interrupt, RCC};
1818
use crate::rcc::{Clocks, APB1, APB2};
19-
use crate::time::rate::*;
19+
use crate::time::{duration, fixed_point::FixedPoint, rate::Hertz};
2020

2121
pub mod interrupts;
2222

@@ -224,21 +224,23 @@ impl<TIM> CountDown for Timer<TIM>
224224
where
225225
TIM: Instance,
226226
{
227-
type Time = Hertz;
227+
type Time = duration::Generic<u32>;
228228

229229
fn start<T>(&mut self, timeout: T)
230230
where
231231
T: Into<Self::Time>,
232232
{
233233
self.stop();
234234

235-
let ticks = TIM::clock(&self.clocks).0 / timeout.into().0;
236-
let psc = crate::unwrap!(u16::try_from((ticks - 1) / (1 << 16)).ok());
235+
let timeout: Self::Time = timeout.into();
236+
let clock = TIM::clock(&self.clocks);
237+
238+
let ticks = clock.integer() * *timeout.scaling_factor() * timeout.integer();
237239

240+
let psc = crate::unwrap!(u16::try_from((ticks - 1) / (1 << 16)).ok());
238241
self.tim.set_psc(psc);
239242

240243
let arr = crate::unwrap!(u16::try_from(ticks / u32::from(psc + 1)).ok());
241-
242244
self.tim.set_arr(arr);
243245

244246
// Ensure that the below procedure does not create an unexpected interrupt.

testsuite/tests/timer.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,20 @@ mod tests {
7171
let freqcyc = state.mono_timer.frequency().integer();
7272

7373
let instant = state.mono_timer.now();
74-
timer.start(1.Hz());
74+
timer.start(1.seconds());
7575
assert!(!timer.is_event_triggered(Event::Update));
7676
unwrap!(nb::block!(timer.wait()).ok());
7777
let elapsed = instant.elapsed();
7878

7979
defmt::info!("elapsed: {}", elapsed);
8080
// TODO: Test multiple frequencies and change the cycle counter window to a percentage
8181
// to measure the overhead of start?
82-
assert!(((freqcyc - 1000)..(freqcyc + 1000)).contains(&elapsed));
82+
83+
// Differentiate between releas and debug build.
84+
#[cfg(not(debug_assertions))]
85+
assert!(((freqcyc - 500)..(freqcyc + 500)).contains(&elapsed));
86+
#[cfg(debug_assertions)]
87+
assert!(((freqcyc - 2000)..(freqcyc + 2000)).contains(&elapsed));
8388

8489
state.timer = Some(timer);
8590
}
@@ -96,7 +101,7 @@ mod tests {
96101

97102
assert!(!INTERRUPT_FIRED.load(Ordering::SeqCst));
98103
timer.enable_interrupt(Event::Update);
99-
timer.start(100_000.Hz());
104+
timer.start(1.milliseconds());
100105

101106
while !INTERRUPT_FIRED.load(Ordering::SeqCst) {}
102107

0 commit comments

Comments
 (0)