@@ -11,7 +11,7 @@ use void::Void;
11
11
12
12
use crate :: pac:: RCC ;
13
13
14
- use crate :: rcc:: { Clocks , Enable , Reset } ;
14
+ use crate :: rcc:: { self , Clocks } ;
15
15
use crate :: time:: Hertz ;
16
16
17
17
/// Timer wrapper
@@ -26,6 +26,24 @@ pub struct CountDownTimer<TIM> {
26
26
clk : Hertz ,
27
27
}
28
28
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
+
29
47
/// Interrupt events
30
48
pub enum Event {
31
49
/// CountDownTimer timed out / count down ended
@@ -48,17 +66,6 @@ impl Timer<SYST> {
48
66
}
49
67
}
50
68
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
-
62
69
pub fn release ( self ) -> SYST {
63
70
self . tim
64
71
}
@@ -118,8 +125,6 @@ impl Cancel for CountDownTimer<SYST> {
118
125
}
119
126
}
120
127
121
- impl Periodic for CountDownTimer < SYST > { }
122
-
123
128
/// A monotonic non-decreasing timer
124
129
///
125
130
/// This uses the timer in the debug watch trace peripheral. This means, that if the
@@ -171,40 +176,47 @@ impl Instant {
171
176
}
172
177
}
173
178
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
+
174
209
macro_rules! hal {
175
210
( $( $TIM: ty: ( $tim: ident, $pclk: ident, $ppre: ident) , ) +) => {
176
211
$(
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 {
188
215
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)
205
217
}
206
-
207
218
}
219
+
208
220
impl CountDownTimer <$TIM> {
209
221
/// Starts listening for an `event`
210
222
///
@@ -305,8 +317,6 @@ macro_rules! hal {
305
317
Ok ( ( ) )
306
318
}
307
319
}
308
-
309
- impl Periodic for CountDownTimer <$TIM> { }
310
320
) +
311
321
}
312
322
}
@@ -388,100 +398,21 @@ hal! {
388
398
crate :: pac:: TIM14 : ( tim14, pclk1, ppre1) ,
389
399
}
390
400
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" ) ]
431
403
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" ) ]
451
406
use crate :: gpio:: gpioe:: * ;
452
-
453
- #[ cfg( any ( feature = "stm32f412" , feature = "stm32f413" , feature = "stm32f423" ) ) ]
407
+ # [ allow ( unused ) ]
408
+ #[ cfg( feature = "gpiof" ) ]
454
409
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" ) ]
482
412
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:: * } ;
485
416
486
417
// Output channels marker traits
487
418
pub trait PinC1 < TIM > { }
0 commit comments