Skip to content

Commit d44a093

Browse files
rfuestmvertescher
authored andcommitted
Add missing timer and implement Cancel
1 parent 1b00863 commit d44a093

File tree

1 file changed

+58
-14
lines changed

1 file changed

+58
-14
lines changed

src/timer.rs

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
11
//! Timers
22
3-
use crate::device::{TIM2, TIM3, TIM4, TIM5};
4-
use crate::hal::timer::{CountDown, Periodic};
5-
use crate::rcc::{Clocks, APB1};
3+
use crate::device::{
4+
TIM1, TIM10, TIM11, TIM12, TIM13, TIM14, TIM2, TIM3, TIM4, TIM5, TIM6, TIM7, TIM8, TIM9,
5+
};
6+
use crate::hal::timer::{Cancel, CountDown, Periodic};
7+
use crate::rcc::{Clocks, APB1, APB2};
68
use crate::time::Hertz;
79
use cast::{u16, u32};
810
use nb;
911
use void::Void;
1012

1113
/// Hardware timers
1214
pub struct Timer<TIM> {
13-
clocks: Clocks,
15+
clock: Hertz,
1416
tim: TIM,
1517
timeout: Hertz,
1618
}
1719

1820
/// Interrupt events
21+
#[derive(Debug, PartialEq)]
1922
pub enum Event {
2023
/// Timer timed out / count down ended
2124
TimeOut,
2225
}
2326

27+
/// Timer errors
28+
#[derive(Debug, PartialEq)]
29+
pub enum Error {
30+
/// Timer is disabled.
31+
Disabled,
32+
}
33+
2434
macro_rules! hal {
2535
($($TIM:ident: ($tim:ident, $timXen:ident, $timXrst:ident, $apb:ident, $timclk:ident),)+) => {
2636
$(
@@ -34,12 +44,11 @@ macro_rules! hal {
3444
where
3545
T: Into<Hertz>,
3646
{
37-
// pause
38-
self.tim.cr1.modify(|_, w| w.cen().clear_bit());
47+
self.disable();
3948

4049
self.timeout = timeout.into();
4150
let frequency = self.timeout.0;
42-
let ticks = self.clocks.$timclk().0 / frequency;
51+
let ticks = self.clock.0 / frequency;
4352
let psc = u16((ticks - 1) / (1 << 16)).unwrap();
4453

4554
self.tim.psc.write(|w| unsafe { w.psc().bits(psc) });
@@ -55,8 +64,7 @@ macro_rules! hal {
5564
// it should be cleared
5665
self.tim.sr.modify(|_, w| w.uif().clear_bit());
5766

58-
// start counter
59-
self.tim.cr1.modify(|_, w| w.cen().set_bit());
67+
self.enable();
6068
}
6169

6270
fn wait(&mut self) -> nb::Result<(), Void> {
@@ -69,6 +77,20 @@ macro_rules! hal {
6977
}
7078
}
7179

80+
impl Cancel for Timer<$TIM> {
81+
type Error = Error;
82+
83+
fn cancel(&mut self) -> Result<(), Self::Error> {
84+
if !self.tim.cr1.read().cen().is_enabled() {
85+
return Err(Error::Disabled);
86+
}
87+
88+
self.disable();
89+
90+
Ok(())
91+
}
92+
}
93+
7294
impl Timer<$TIM> {
7395
/// Configures a TIM peripheral as a periodic count down timer
7496
pub fn $tim<T>(tim: $TIM, timeout: T, clocks: Clocks, apb: &mut $apb) -> Self
@@ -80,8 +102,10 @@ macro_rules! hal {
80102
apb.rstr().modify(|_, w| w.$timXrst().set_bit());
81103
apb.rstr().modify(|_, w| w.$timXrst().clear_bit());
82104

105+
let clock = clocks.$timclk();
106+
83107
let mut timer = Timer {
84-
clocks,
108+
clock,
85109
tim,
86110
timeout: Hertz(0),
87111
};
@@ -124,20 +148,40 @@ macro_rules! hal {
124148
}
125149

126150
/// Releases the TIM peripheral
127-
pub fn free(self) -> $TIM {
128-
// pause counter
129-
self.tim.cr1.modify(|_, w| w.cen().clear_bit());
151+
pub fn free(mut self) -> $TIM {
152+
self.disable();
153+
130154
self.tim
131155
}
156+
157+
/// Enables the counter.
158+
fn enable(&mut self) {
159+
self.tim.cr1.modify(|_, w| w.cen().set_bit());
160+
}
161+
162+
/// Disables the counter.
163+
fn disable(&mut self) {
164+
self.tim.cr1.modify(|_, w| w.cen().clear_bit());
165+
}
132166
}
133167
)+
134168
}
135169
}
136170

137-
// TODO: Add support for missing timers
138171
hal! {
139172
TIM2: (tim2, tim2en, tim2rst, APB1, timclk1),
140173
TIM3: (tim3, tim3en, tim3rst, APB1, timclk1),
141174
TIM4: (tim4, tim4en, tim4rst, APB1, timclk1),
142175
TIM5: (tim5, tim5en, tim5rst, APB1, timclk1),
176+
TIM6: (tim6, tim6en, tim6rst, APB1, timclk1),
177+
TIM7: (tim7, tim7en, tim7rst, APB1, timclk1),
178+
TIM12: (tim12, tim12en, tim12rst, APB1, timclk1),
179+
TIM13: (tim13, tim13en, tim13rst, APB1, timclk1),
180+
TIM14: (tim14, tim14en, tim14rst, APB1, timclk1),
181+
182+
TIM1: (tim1, tim1en, tim1rst, APB2, timclk2),
183+
TIM8: (tim8, tim8en, tim8rst, APB2, timclk2),
184+
TIM9: (tim9, tim9en, tim9rst, APB2, timclk2),
185+
TIM10: (tim10, tim10en, tim10rst, APB2, timclk2),
186+
TIM11: (tim11, tim11en, tim11rst, APB2, timclk2),
143187
}

0 commit comments

Comments
 (0)