1
1
//! Timers
2
2
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 } ;
6
8
use crate :: time:: Hertz ;
7
9
use cast:: { u16, u32} ;
8
10
use nb;
9
11
use void:: Void ;
10
12
11
13
/// Hardware timers
12
14
pub struct Timer < TIM > {
13
- clocks : Clocks ,
15
+ clock : Hertz ,
14
16
tim : TIM ,
15
17
timeout : Hertz ,
16
18
}
17
19
18
20
/// Interrupt events
21
+ #[ derive( Debug , PartialEq ) ]
19
22
pub enum Event {
20
23
/// Timer timed out / count down ended
21
24
TimeOut ,
22
25
}
23
26
27
+ /// Timer errors
28
+ #[ derive( Debug , PartialEq ) ]
29
+ pub enum Error {
30
+ /// Timer is disabled.
31
+ Disabled ,
32
+ }
33
+
24
34
macro_rules! hal {
25
35
( $( $TIM: ident: ( $tim: ident, $timXen: ident, $timXrst: ident, $apb: ident, $timclk: ident) , ) +) => {
26
36
$(
@@ -34,12 +44,11 @@ macro_rules! hal {
34
44
where
35
45
T : Into <Hertz >,
36
46
{
37
- // pause
38
- self . tim. cr1. modify( |_, w| w. cen( ) . clear_bit( ) ) ;
47
+ self . disable( ) ;
39
48
40
49
self . timeout = timeout. into( ) ;
41
50
let frequency = self . timeout. 0 ;
42
- let ticks = self . clocks . $timclk ( ) . 0 / frequency;
51
+ let ticks = self . clock . 0 / frequency;
43
52
let psc = u16 ( ( ticks - 1 ) / ( 1 << 16 ) ) . unwrap( ) ;
44
53
45
54
self . tim. psc. write( |w| unsafe { w. psc( ) . bits( psc) } ) ;
@@ -55,8 +64,7 @@ macro_rules! hal {
55
64
// it should be cleared
56
65
self . tim. sr. modify( |_, w| w. uif( ) . clear_bit( ) ) ;
57
66
58
- // start counter
59
- self . tim. cr1. modify( |_, w| w. cen( ) . set_bit( ) ) ;
67
+ self . enable( ) ;
60
68
}
61
69
62
70
fn wait( & mut self ) -> nb:: Result <( ) , Void > {
@@ -69,6 +77,20 @@ macro_rules! hal {
69
77
}
70
78
}
71
79
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
+
72
94
impl Timer <$TIM> {
73
95
/// Configures a TIM peripheral as a periodic count down timer
74
96
pub fn $tim<T >( tim: $TIM, timeout: T , clocks: Clocks , apb: & mut $apb) -> Self
@@ -80,8 +102,10 @@ macro_rules! hal {
80
102
apb. rstr( ) . modify( |_, w| w. $timXrst( ) . set_bit( ) ) ;
81
103
apb. rstr( ) . modify( |_, w| w. $timXrst( ) . clear_bit( ) ) ;
82
104
105
+ let clock = clocks. $timclk( ) ;
106
+
83
107
let mut timer = Timer {
84
- clocks ,
108
+ clock ,
85
109
tim,
86
110
timeout: Hertz ( 0 ) ,
87
111
} ;
@@ -124,20 +148,40 @@ macro_rules! hal {
124
148
}
125
149
126
150
/// 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
+
130
154
self . tim
131
155
}
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
+ }
132
166
}
133
167
) +
134
168
}
135
169
}
136
170
137
- // TODO: Add support for missing timers
138
171
hal ! {
139
172
TIM2 : ( tim2, tim2en, tim2rst, APB1 , timclk1) ,
140
173
TIM3 : ( tim3, tim3en, tim3rst, APB1 , timclk1) ,
141
174
TIM4 : ( tim4, tim4en, tim4rst, APB1 , timclk1) ,
142
175
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) ,
143
187
}
0 commit comments