Skip to content

Commit e2ddb4b

Browse files
committed
Add support for master mode selection to timer
1 parent e138a62 commit e2ddb4b

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

src/timer.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Timers
22
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};
44
use crate::rcc::{Clocks, Rcc};
55
use crate::time::Hertz;
66
use cast::{u16, u32};
@@ -83,7 +83,7 @@ impl TimerExt<SYST> for SYST {
8383
impl Periodic for Timer<SYST> {}
8484

8585
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),)+) => {
8787
$(
8888
impl TimerExt<$TIM> for $TIM {
8989
fn timer<T>(self, timeout: T, rcc: &mut Rcc) -> Timer<$TIM>
@@ -94,7 +94,7 @@ macro_rules! timers {
9494
}
9595
}
9696

97-
impl Timer<$TIM> {
97+
impl Timer<$TIM> where $TIM: GeneralPurposeTimer {
9898
/// Configures a TIM peripheral as a periodic count down timer
9999
pub fn $tim<T>(tim: $TIM, timeout: T, rcc: &mut Rcc) -> Self
100100
where
@@ -142,6 +142,13 @@ macro_rules! timers {
142142
// continue
143143
self.tim.cr1.modify(|_, w| w.cen().set_bit());
144144
}
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+
}
145152
}
146153

147154
impl CountDown for Timer<$TIM> {
@@ -188,13 +195,32 @@ macro_rules! timers {
188195
}
189196

190197
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+
}
191206
)+
192207
}
193208
}
194209

195210
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);
200226
}

0 commit comments

Comments
 (0)