Skip to content

Commit 7e68786

Browse files
authored
Merge pull request #43 from mattico/stopwatch
Stopwatch: add set_prescaler
2 parents fc6748e + 82728fa commit 7e68786

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/timer/stopwatch.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ macro_rules! stopwatches {
2020
rcc.rb.$apbenr.modify(|_, w| w.$timXen().set_bit());
2121
rcc.rb.$apbrstr.modify(|_, w| w.$timXrst().set_bit());
2222
rcc.rb.$apbrstr.modify(|_, w| w.$timXrst().clear_bit());
23-
tim.cr1.modify(|_, w| w.urs().set_bit());
2423
tim.cr1.modify(|_, w| w.cen().set_bit());
2524
Stopwatch {
2625
tim,
2726
clk: rcc.clocks.apb_tim_clk,
2827
}
2928
}
3029

30+
/// Overrides the counter clock input frequency
31+
///
32+
/// Useful if the APB Timer Clock changes after the `Stopwatch` is created or
33+
/// to deliberately speed up or slow down the `Stopwatch` from actual measured time.
3134
pub fn set_clock<T>(&mut self, clk: T)
3235
where
3336
T: Into<Hertz>,
@@ -37,6 +40,14 @@ macro_rules! stopwatches {
3740
self.clk = clk;
3841
}
3942

43+
/// Set the prescaler which divides the input clock frequency before the counter
44+
///
45+
/// The counter frequency is equal to the input clock divided by the prescaler + 1.
46+
pub fn set_prescaler(&mut self, prescaler: u16) {
47+
self.tim.psc.write(|w| unsafe { w.psc().bits(prescaler) });
48+
self.tim.egr.write(|w| w.ug().set_bit());
49+
}
50+
4051
pub fn reset(&mut self) {
4152
self.tim.cnt.reset();
4253
}

0 commit comments

Comments
 (0)