Skip to content

Commit b00758f

Browse files
committed
Timer::new
1 parent 493e96f commit b00758f

File tree

6 files changed

+70
-139
lines changed

6 files changed

+70
-139
lines changed

examples/analog-stopwatch-with-spi-ssd1306.rs

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

132132
// Create a 1ms periodic interrupt from TIM2
133-
let mut timer = Timer::tim2(dp.TIM2, &clocks).start_count_down(1.hz());
133+
let mut timer = Timer::new(dp.TIM2, &clocks).start_count_down(1.hz());
134134
timer.listen(Event::TimeOut);
135135

136136
free(|cs| {

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, &clocks).start_count_down(1.hz());
82+
let mut timer = Timer::new(dp.TIM2, &clocks).start_count_down(1.hz());
8383

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

examples/pwm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn main() -> ! {
1818
let gpioa = dp.GPIOA.split();
1919
let channels = (gpioa.pa8.into_alternate(), gpioa.pa9.into_alternate());
2020

21-
let pwm = Timer::tim1(dp.TIM1, &clocks).pwm(channels, 20u32.khz());
21+
let pwm = Timer::new(dp.TIM1, &clocks).pwm(channels, 20u32.khz());
2222
let (mut ch1, _ch2) = pwm;
2323
let max_duty = ch1.get_max_duty();
2424
ch1.set_duty(max_duty / 2);

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

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

9191
// Create a 1ms periodic interrupt from TIM2
92-
let mut timer = Timer::tim2(dp.TIM2, &clocks).start_count_down(1.hz());
92+
let mut timer = Timer::new(dp.TIM2, &clocks).start_count_down(1.hz());
9393
timer.listen(Event::TimeOut);
9494

9595
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, &clocks).start_count_down(1.hz());
32+
let mut timer = Timer::new(dp.TIM1, &clocks).start_count_down(1.hz());
3333

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

src/timer.rs

Lines changed: 65 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use void::Void;
1111

1212
use crate::pac::RCC;
1313

14-
use crate::rcc::{Clocks, Enable, Reset};
14+
use crate::rcc::{self, Clocks};
1515
use crate::time::Hertz;
1616

1717
/// Timer wrapper
@@ -26,6 +26,24 @@ pub struct CountDownTimer<TIM> {
2626
clk: Hertz,
2727
}
2828

29+
impl<TIM> Timer<TIM>
30+
where
31+
CountDownTimer<TIM>: CountDown<Time = Hertz>,
32+
{
33+
/// Starts timer in count down mode at a given frequency
34+
pub fn start_count_down<T>(self, timeout: T) -> CountDownTimer<TIM>
35+
where
36+
T: Into<Hertz>,
37+
{
38+
let Self { tim, clk } = self;
39+
let mut timer = CountDownTimer { tim, clk };
40+
timer.start(timeout);
41+
timer
42+
}
43+
}
44+
45+
impl<TIM> Periodic for CountDownTimer<TIM> {}
46+
2947
/// Interrupt events
3048
pub enum Event {
3149
/// CountDownTimer timed out / count down ended
@@ -48,17 +66,6 @@ impl Timer<SYST> {
4866
}
4967
}
5068

51-
/// Configures the SYST clock as a periodic count down timer
52-
pub fn start_count_down<T>(self, timeout: T) -> CountDownTimer<SYST>
53-
where
54-
T: Into<Hertz>,
55-
{
56-
let Self { tim, clk } = self;
57-
let mut timer = CountDownTimer { tim, clk };
58-
timer.start(timeout);
59-
timer
60-
}
61-
6269
pub fn release(self) -> SYST {
6370
self.tim
6471
}
@@ -118,8 +125,6 @@ impl Cancel for CountDownTimer<SYST> {
118125
}
119126
}
120127

121-
impl Periodic for CountDownTimer<SYST> {}
122-
123128
/// A monotonic non-decreasing timer
124129
///
125130
/// This uses the timer in the debug watch trace peripheral. This means, that if the
@@ -171,40 +176,47 @@ impl Instant {
171176
}
172177
}
173178

179+
mod private {
180+
pub trait Sealed {}
181+
}
182+
183+
pub trait Instance: private::Sealed + rcc::Enable + rcc::Reset {
184+
#[doc(hidden)]
185+
fn pclk_freq(clocks: &Clocks) -> Hertz;
186+
}
187+
188+
impl<TIM> Timer<TIM>
189+
where
190+
TIM: Instance,
191+
{
192+
/// Initialize timer
193+
pub fn new(tim: TIM, clocks: &Clocks) -> Self {
194+
unsafe {
195+
//NOTE(unsafe) this reference will only be used for atomic writes with no side effects
196+
let rcc = &(*RCC::ptr());
197+
// Enable and reset the timer peripheral
198+
TIM::enable(rcc);
199+
TIM::reset(rcc);
200+
}
201+
202+
Self {
203+
clk: TIM::pclk_freq(clocks),
204+
tim,
205+
}
206+
}
207+
}
208+
174209
macro_rules! hal {
175210
($($TIM:ty: ($tim:ident, $pclk:ident, $ppre:ident),)+) => {
176211
$(
177-
impl Timer<$TIM> {
178-
/// Initialize timer
179-
pub fn $tim(tim: $TIM, clocks: &Clocks) -> Self {
180-
unsafe {
181-
//NOTE(unsafe) this reference will only be used for atomic writes with no side effects
182-
let rcc = &(*RCC::ptr());
183-
// Enable and reset the timer peripheral
184-
<$TIM>::enable(rcc);
185-
<$TIM>::reset(rcc);
186-
}
187-
212+
impl private::Sealed for $TIM {}
213+
impl Instance for $TIM {
214+
fn pclk_freq(clocks: &Clocks) -> Hertz {
188215
let pclk_mul = if clocks.$ppre() == 1 { 1 } else { 2 };
189-
190-
Self {
191-
clk: Hertz(clocks.$pclk().0 * pclk_mul),
192-
tim,
193-
}
194-
}
195-
196-
/// Starts timer in count down mode at a given frequency
197-
pub fn start_count_down<T>(self, timeout: T) -> CountDownTimer<$TIM>
198-
where
199-
T: Into<Hertz>,
200-
{
201-
let Self { tim, clk } = self;
202-
let mut timer = CountDownTimer { tim, clk };
203-
timer.start(timeout);
204-
timer
216+
Hertz(clocks.$pclk().0 * pclk_mul)
205217
}
206-
207218
}
219+
208220
impl CountDownTimer<$TIM> {
209221
/// Starts listening for an `event`
210222
///
@@ -305,8 +317,6 @@ macro_rules! hal {
305317
Ok(())
306318
}
307319
}
308-
309-
impl Periodic for CountDownTimer<$TIM> {}
310320
)+
311321
}
312322
}
@@ -388,100 +398,21 @@ hal! {
388398
crate::pac::TIM14: (tim14, pclk1, ppre1),
389399
}
390400

391-
use crate::gpio::gpiob::*;
392-
393-
#[cfg(any(
394-
feature = "stm32f401",
395-
feature = "stm32f405",
396-
feature = "stm32f407",
397-
feature = "stm32f411",
398-
feature = "stm32f412",
399-
feature = "stm32f413",
400-
feature = "stm32f415",
401-
feature = "stm32f417",
402-
feature = "stm32f423",
403-
feature = "stm32f427",
404-
feature = "stm32f429",
405-
feature = "stm32f437",
406-
feature = "stm32f439",
407-
feature = "stm32f446",
408-
feature = "stm32f469",
409-
feature = "stm32f479"
410-
))]
411-
use crate::gpio::gpioc::*;
412-
413-
#[cfg(any(
414-
feature = "stm32f401",
415-
feature = "stm32f405",
416-
feature = "stm32f407",
417-
feature = "stm32f411",
418-
feature = "stm32f412",
419-
feature = "stm32f413",
420-
feature = "stm32f415",
421-
feature = "stm32f417",
422-
feature = "stm32f423",
423-
feature = "stm32f427",
424-
feature = "stm32f429",
425-
feature = "stm32f437",
426-
feature = "stm32f439",
427-
feature = "stm32f446",
428-
feature = "stm32f469",
429-
feature = "stm32f479"
430-
))]
401+
#[allow(unused)]
402+
#[cfg(feature = "gpiod")]
431403
use crate::gpio::gpiod::*;
432-
433-
#[cfg(any(
434-
feature = "stm32f401",
435-
feature = "stm32f405",
436-
feature = "stm32f407",
437-
feature = "stm32f411",
438-
feature = "stm32f412",
439-
feature = "stm32f413",
440-
feature = "stm32f415",
441-
feature = "stm32f417",
442-
feature = "stm32f423",
443-
feature = "stm32f427",
444-
feature = "stm32f429",
445-
feature = "stm32f437",
446-
feature = "stm32f439",
447-
feature = "stm32f446",
448-
feature = "stm32f469",
449-
feature = "stm32f479"
450-
))]
404+
#[allow(unused)]
405+
#[cfg(feature = "gpioe")]
451406
use crate::gpio::gpioe::*;
452-
453-
#[cfg(any(feature = "stm32f412", feature = "stm32f413", feature = "stm32f423"))]
407+
#[allow(unused)]
408+
#[cfg(feature = "gpiof")]
454409
use crate::gpio::gpiof::*;
455-
456-
#[cfg(any(
457-
feature = "stm32f405",
458-
feature = "stm32f407",
459-
feature = "stm32f415",
460-
feature = "stm32f417",
461-
feature = "stm32f427",
462-
feature = "stm32f429",
463-
feature = "stm32f437",
464-
feature = "stm32f439",
465-
feature = "stm32f469",
466-
feature = "stm32f479"
467-
))]
468-
use crate::gpio::gpioh::*;
469-
470-
#[cfg(any(
471-
feature = "stm32f405",
472-
feature = "stm32f407",
473-
feature = "stm32f415",
474-
feature = "stm32f417",
475-
feature = "stm32f427",
476-
feature = "stm32f429",
477-
feature = "stm32f437",
478-
feature = "stm32f439",
479-
feature = "stm32f469",
480-
feature = "stm32f479"
481-
))]
410+
#[allow(unused)]
411+
#[cfg(feature = "gpioi")]
482412
use crate::gpio::gpioi::*;
483-
484-
use crate::gpio::{gpioa::*, Alternate, AlternateOD};
413+
use crate::gpio::{gpioa::*, gpiob::*, Alternate, AlternateOD};
414+
#[allow(unused)]
415+
use crate::gpio::{gpioc::*, gpioh::*};
485416

486417
// Output channels marker traits
487418
pub trait PinC1<TIM> {}

0 commit comments

Comments
 (0)