1
1
//! Timers
2
2
use crate :: hal:: timer:: { CountDown , Periodic } ;
3
- use crate :: pac:: { TIM2 , TIM21 , TIM22 , TIM3 } ;
3
+ use crate :: pac:: { TIM2 , TIM21 , TIM22 , TIM3 , tim2 , tim21 , tim22 } ;
4
4
use crate :: rcc:: { Clocks , Rcc } ;
5
5
use crate :: time:: Hertz ;
6
6
use cast:: { u16, u32} ;
@@ -83,7 +83,7 @@ impl TimerExt<SYST> for SYST {
83
83
impl Periodic for Timer < SYST > { }
84
84
85
85
macro_rules! timers {
86
- ( $( $TIM: ident: ( $tim: ident, $timXen: ident, $timXrst: ident, $apbenr: ident, $apbrstr: ident, $timclk: ident) , ) +) => {
86
+ ( $( $TIM: ident: ( $tim: ident, $timXen: ident, $timXrst: ident, $apbenr: ident, $apbrstr: ident, $timclk: ident, $mms : ty ) , ) +) => {
87
87
$(
88
88
impl TimerExt <$TIM> for $TIM {
89
89
fn timer<T >( self , timeout: T , rcc: & mut Rcc ) -> Timer <$TIM>
@@ -94,7 +94,7 @@ macro_rules! timers {
94
94
}
95
95
}
96
96
97
- impl Timer <$TIM> {
97
+ impl Timer <$TIM> where $TIM : GeneralPurposeTimer {
98
98
/// Configures a TIM peripheral as a periodic count down timer
99
99
pub fn $tim<T >( tim: $TIM, timeout: T , rcc: & mut Rcc ) -> Self
100
100
where
@@ -142,6 +142,13 @@ macro_rules! timers {
142
142
// continue
143
143
self . tim. cr1. modify( |_, w| w. cen( ) . set_bit( ) ) ;
144
144
}
145
+
146
+ /// Select master mode
147
+ pub fn select_master_mode( & mut self ,
148
+ variant: <$TIM as GeneralPurposeTimer >:: MasterMode ,
149
+ ) {
150
+ self . tim. select_master_mode( variant) ;
151
+ }
145
152
}
146
153
147
154
impl CountDown for Timer <$TIM> {
@@ -188,13 +195,32 @@ macro_rules! timers {
188
195
}
189
196
190
197
impl Periodic for Timer <$TIM> { }
198
+
199
+ impl GeneralPurposeTimer for $TIM {
200
+ type MasterMode = $mms;
201
+
202
+ fn select_master_mode( & mut self , variant: Self :: MasterMode ) {
203
+ self . cr2. modify( |_, w| w. mms( ) . variant( variant) ) ;
204
+ }
205
+ }
191
206
) +
192
207
}
193
208
}
194
209
195
210
timers ! {
196
- TIM2 : ( tim2, tim2en, tim2rst, apb1enr, apb1rstr, apb1_tim_clk) ,
197
- TIM3 : ( tim3, tim3en, tim3rst, apb1enr, apb1rstr, apb1_tim_clk) ,
198
- TIM21 : ( tim21, tim21en, tim21rst, apb2enr, apb2rstr, apb2_tim_clk) ,
199
- TIM22 : ( tim22, tim22en, tim22rst, apb2enr, apb2rstr, apb2_tim_clk) ,
211
+ TIM2 : ( tim2, tim2en, tim2rst, apb1enr, apb1rstr, apb1_tim_clk,
212
+ tim2:: cr2:: MMS_A ) ,
213
+ TIM3 : ( tim3, tim3en, tim3rst, apb1enr, apb1rstr, apb1_tim_clk,
214
+ tim2:: cr2:: MMS_A ) ,
215
+ TIM21 : ( tim21, tim21en, tim21rst, apb2enr, apb2rstr, apb2_tim_clk,
216
+ tim21:: cr2:: MMS_A ) ,
217
+ TIM22 : ( tim22, tim22en, tim22rst, apb2enr, apb2rstr, apb2_tim_clk,
218
+ tim22:: cr2:: MMS_A ) ,
219
+ }
220
+
221
+
222
+ pub trait GeneralPurposeTimer {
223
+ type MasterMode ;
224
+
225
+ fn select_master_mode ( & mut self , variant : Self :: MasterMode ) ;
200
226
}
0 commit comments