1
1
//! Timers
2
2
3
- use crate :: hal:: timer:: { CountDown , Periodic } ;
4
- // missing PAC support
5
- /*
3
+ use core:: convert:: Infallible ;
4
+
5
+ use crate :: hal:: timer:: { Cancel , CountDown , Periodic } ;
6
+
6
7
#[ cfg( any(
7
- feature = "stm32l451",
8
+ // feature = "stm32l451",
8
9
feature = "stm32l452" ,
9
10
feature = "stm32l462" ,
10
- feature = "stm32l471",
11
+ // feature = "stm32l471",
11
12
feature = "stm32l475" ,
12
13
feature = "stm32l476" ,
13
14
feature = "stm32l485" ,
@@ -20,11 +21,11 @@ use crate::hal::timer::{CountDown, Periodic};
20
21
// feature = "stm32l4s5",
21
22
// feature = "stm32l4r7",
22
23
// feature = "stm32l4s7",
23
- feature = "stm32l4r9",
24
- feature = "stm32l4s9",
24
+ // feature = "stm32l4r9",
25
+ // feature = "stm32l4s9",
25
26
) ) ]
26
27
use crate :: stm32:: TIM3 ;
27
- */
28
+
28
29
#[ cfg( not( any(
29
30
feature = "stm32l412" ,
30
31
feature = "stm32l422" ,
@@ -88,17 +89,16 @@ macro_rules! hal {
88
89
type Time = Hertz ;
89
90
90
91
// NOTE(allow) `w.psc().bits()` is safe for TIM{6,7} but not for TIM{2,3,4} due to
91
- // some SVD omission
92
+ // some SVD omission.
92
93
#[ allow( unused_unsafe) ]
93
94
fn start<T >( & mut self , timeout: T )
94
95
where
95
96
T : Into <Hertz >,
96
97
{
97
- // pause
98
- self . tim. cr1. modify( |_, w| w. cen( ) . clear_bit( ) ) ;
98
+ self . pause( ) ;
99
99
100
100
self . timeout = timeout. into( ) ;
101
- let ticks = self . clocks. pclk1( ) / self . timeout; // TODO check pclk that timer is on
101
+ let ticks = self . clocks. pclk1( ) / self . timeout; // TODO: Check pclk that timer is on.
102
102
let psc = u16 ( ( ticks - 1 ) / ( 1 << 16 ) ) . unwrap( ) ;
103
103
104
104
self . tim. psc. write( |w| unsafe { w. psc( ) . bits( psc) } ) ;
@@ -107,14 +107,15 @@ macro_rules! hal {
107
107
108
108
self . tim. arr. write( |w| unsafe { w. bits( u32 ( arr) ) } ) ;
109
109
110
- // Trigger an update event to load the prescaler value to the clock
110
+ // Trigger an update event to load the prescaler value to the clock.
111
111
self . tim. egr. write( |w| w. ug( ) . set_bit( ) ) ;
112
+
112
113
// The above line raises an update event which will indicate
113
114
// that the timer is already finished. Since this is not the case,
114
- // it should be cleared
115
+ // it should be cleared.
115
116
self . clear_update_interrupt_flag( ) ;
116
117
117
- // start counter
118
+ // Start counter.
118
119
self . tim. cr1. modify( |_, w| w. cen( ) . set_bit( ) ) ;
119
120
}
120
121
@@ -128,6 +129,17 @@ macro_rules! hal {
128
129
}
129
130
}
130
131
132
+ impl Cancel for Timer <$TIM> {
133
+ type Error = Infallible ;
134
+
135
+ fn cancel( & mut self ) -> Result <( ) , Self :: Error > {
136
+ self . pause( ) ;
137
+ self . reset( ) ;
138
+
139
+ Ok ( ( ) )
140
+ }
141
+ }
142
+
131
143
impl Timer <$TIM> {
132
144
// XXX(why not name this `new`?) bummer: constructors need to have different names
133
145
// even if the `$TIM` are non overlapping (compare to the `free` function below
@@ -171,7 +183,7 @@ macro_rules! hal {
171
183
let max = core:: $width:: MAX ;
172
184
tim. arr. write( |w| unsafe { w. bits( max. into( ) ) } ) ;
173
185
174
- // Trigger an update event to load the prescaler value to the clock
186
+ // Trigger an update event to load the prescaler value to the clock.
175
187
tim. egr. write( |w| w. ug( ) . set_bit( ) ) ;
176
188
177
189
@@ -180,7 +192,7 @@ macro_rules! hal {
180
192
// it should be cleared
181
193
tim. sr. modify( |_, w| w. uif( ) . clear_bit( ) ) ;
182
194
183
- // start counter
195
+ // Start counter.
184
196
tim. cr1. modify( |_, w| {
185
197
w. cen( ) . set_bit( ) ;
186
198
@@ -204,17 +216,16 @@ macro_rules! hal {
204
216
pub fn listen( & mut self , event: Event ) {
205
217
match event {
206
218
Event :: TimeOut => {
207
- // Enable update event interrupt
219
+ // Enable update event interrupt.
208
220
self . tim. dier. write( |w| w. uie( ) . set_bit( ) ) ;
209
221
}
210
222
}
211
223
}
212
224
213
-
214
225
/// Clears interrupt associated with `event`.
215
226
///
216
- /// If the interrupt is not cleared, it will immediately retrigger after
217
- /// the ISR has finished.
227
+ /// If the interrupt is not cleared, it will immediately
228
+ /// retrigger after the ISR has finished.
218
229
pub fn clear_interrupt( & mut self , event: Event ) {
219
230
match event {
220
231
Event :: TimeOut => {
@@ -224,8 +235,7 @@ macro_rules! hal {
224
235
}
225
236
}
226
237
227
-
228
- /// Stops listening for an `event`
238
+ /// Stops listening for an `event`.
229
239
pub fn unlisten( & mut self , event: Event ) {
230
240
match event {
231
241
Event :: TimeOut => {
@@ -235,7 +245,7 @@ macro_rules! hal {
235
245
}
236
246
}
237
247
238
- /// Clears Update Interrupt Flag
248
+ /// Clear the update interrupt flag.
239
249
pub fn clear_update_interrupt_flag( & mut self ) {
240
250
self . tim. sr. modify( |_, w| w. uif( ) . clear_bit( ) ) ;
241
251
}
@@ -246,10 +256,19 @@ macro_rules! hal {
246
256
cnt. cnt( ) . bits( )
247
257
}
248
258
249
- /// Releases the TIM peripheral
250
- pub fn free( self ) -> $TIM {
251
- // pause counter
259
+ /// Pause the counter.
260
+ pub fn pause( & mut self ) {
252
261
self . tim. cr1. modify( |_, w| w. cen( ) . clear_bit( ) ) ;
262
+ }
263
+
264
+ /// Reset the counter.
265
+ pub fn reset( & mut self ) {
266
+ self . tim. cnt. modify( |_, w| unsafe { w. bits( 0 ) } ) ;
267
+ }
268
+
269
+ /// Releases the TIM peripheral.
270
+ pub fn free( mut self ) -> $TIM {
271
+ self . pause( ) ;
253
272
self . tim
254
273
}
255
274
}
@@ -265,14 +284,11 @@ hal! {
265
284
TIM16 : ( tim16, free_running_tim16, APB2 , u16 ) ,
266
285
}
267
286
268
- // missing PAC support
269
- // RCC_APB1RSTR1->TIM3RST not defined
270
- /*
271
287
#[ cfg( any(
272
- feature = "stm32l451",
288
+ // feature = "stm32l451",
273
289
feature = "stm32l452" ,
274
290
feature = "stm32l462" ,
275
- feature = "stm32l471",
291
+ // feature = "stm32l471",
276
292
feature = "stm32l475" ,
277
293
feature = "stm32l476" ,
278
294
feature = "stm32l485" ,
@@ -285,13 +301,12 @@ hal! {
285
301
// feature = "stm32l4s5",
286
302
// feature = "stm32l4r7",
287
303
// feature = "stm32l4s7",
288
- feature = "stm32l4r9",
289
- feature = "stm32l4s9",
304
+ // feature = "stm32l4r9",
305
+ // feature = "stm32l4s9",
290
306
) ) ]
291
307
hal ! {
292
- TIM3: (tim3, free_running_tim3, tim3en, tim3rst, APB1R1, u32 ),
308
+ TIM3 : ( tim3, free_running_tim3, APB1R1 , u16 ) ,
293
309
}
294
- */
295
310
296
311
#[ cfg( not( any(
297
312
feature = "stm32l412" ,
0 commit comments