Skip to content

Commit 6710c45

Browse files
committed
&Clocks
1 parent 3669202 commit 6710c45

File tree

5 files changed

+18
-14
lines changed

5 files changed

+18
-14
lines changed

examples/blinky-timer-irq.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn main() -> ! {
7979
cortex_m::interrupt::free(|cs| *G_LED.borrow(cs).borrow_mut() = Some(led));
8080

8181
// Set up a timer expiring after 1s
82-
let mut timer = Timer::tim2(dp.TIM2, 1.hz(), clocks);
82+
let mut timer = Timer::tim2(dp.TIM2, 1.hz(), &clocks);
8383

8484
// Generate an interrupt when the timer expires
8585
timer.listen(Event::TimeOut);

examples/stopwatch-with-ssd1306-and-interrupts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ fn main() -> ! {
8888
disp.flush().unwrap();
8989

9090
// Create a 1ms periodic interrupt from TIM2
91-
let mut timer = Timer::tim2(dp.TIM2, 1.khz(), clocks);
91+
let mut timer = Timer::tim2(dp.TIM2, 1.khz(), &clocks);
9292
timer.listen(Event::TimeOut);
9393

9494
free(|cs| {

examples/timer-periph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn main() -> ! {
2929
let clocks = rcc.cfgr.sysclk(24.mhz()).freeze();
3030

3131
// Create a timer based on SysTick
32-
let mut timer = Timer::tim1(dp.TIM1, 1.hz(), clocks);
32+
let mut timer = Timer::tim1(dp.TIM1, 1.hz(), &clocks);
3333

3434
hprintln!("hello!").unwrap();
3535
// wait until timer expires

examples/timer-syst.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn main() -> ! {
3030
let clocks = rcc.cfgr.sysclk(24.mhz()).freeze();
3131

3232
// Create a timer based on SysTick
33-
let mut timer = Timer::syst(cp.SYST, 24.hz(), clocks);
33+
let mut timer = Timer::syst(cp.SYST, 24.hz(), &clocks);
3434

3535
hprintln!("hello!").unwrap();
3636
// wait until timer expires

src/timer.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::time::Hertz;
1616

1717
/// Hardware timers
1818
pub struct Timer<TIM> {
19-
clocks: Clocks,
19+
clk: Hertz,
2020
tim: TIM,
2121
}
2222

@@ -34,12 +34,15 @@ pub enum Error {
3434

3535
impl Timer<SYST> {
3636
/// Configures the SYST clock as a periodic count down timer
37-
pub fn syst<T>(mut syst: SYST, timeout: T, clocks: Clocks) -> Self
37+
pub fn syst<T>(mut syst: SYST, timeout: T, clocks: &Clocks) -> Self
3838
where
3939
T: Into<Hertz>,
4040
{
4141
syst.set_clock_source(SystClkSource::Core);
42-
let mut timer = Timer { tim: syst, clocks };
42+
let mut timer = Self {
43+
tim: syst,
44+
clk: clocks.sysclk(),
45+
};
4346
timer.start(timeout);
4447
timer
4548
}
@@ -66,7 +69,7 @@ impl CountDown for Timer<SYST> {
6669
where
6770
T: Into<Hertz>,
6871
{
69-
let rvr = self.clocks.sysclk().0 / timeout.into().0 - 1;
72+
let rvr = self.clk.0 / timeout.into().0 - 1;
7073

7174
assert!(rvr < (1 << 24));
7275

@@ -112,7 +115,7 @@ pub struct MonoTimer {
112115

113116
impl MonoTimer {
114117
/// Creates a new `Monotonic` timer
115-
pub fn new(mut dwt: DWT, mut dcb: DCB, clocks: Clocks) -> Self {
118+
pub fn new(mut dwt: DWT, mut dcb: DCB, clocks: &Clocks) -> Self {
116119
dcb.enable_trace();
117120
dwt.enable_cycle_counter();
118121

@@ -155,7 +158,7 @@ macro_rules! hal {
155158
$(
156159
impl Timer<$TIM> {
157160
/// Configures a TIM peripheral as a periodic count down timer
158-
pub fn $tim<T>(tim: $TIM, timeout: T, clocks: Clocks) -> Self
161+
pub fn $tim<T>(tim: $TIM, timeout: T, clocks: &Clocks) -> Self
159162
where
160163
T: Into<Hertz>,
161164
{
@@ -167,8 +170,10 @@ macro_rules! hal {
167170
<$TIM>::reset(rcc);
168171
}
169172

170-
let mut timer = Timer {
171-
clocks,
173+
let pclk_mul = if clocks.$ppre() == 1 { 1 } else { 2 };
174+
175+
let mut timer = Self {
176+
clk: Hertz(clocks.$pclk().0 * pclk_mul),
172177
tim,
173178
};
174179
timer.start(timeout);
@@ -233,8 +238,7 @@ macro_rules! hal {
233238
self.tim.cnt.reset();
234239

235240
let frequency = timeout.into().0;
236-
let pclk_mul = if self.clocks.$ppre() == 1 { 1 } else { 2 };
237-
let ticks = self.clocks.$pclk().0 * pclk_mul / frequency;
241+
let ticks = self.clk.0 / frequency;
238242

239243
let psc = u16((ticks - 1) / (1 << 16)).unwrap();
240244
self.tim.psc.write(|w| w.psc().bits(psc) );

0 commit comments

Comments
 (0)