Skip to content

Commit 22f1e8f

Browse files
bors[bot]burrbull
andauthored
Merge #444
444: master mode selection r=burrbull a=burrbull Co-authored-by: Andrey Zgarbul <[email protected]>
2 parents 22026e9 + d85cd4c commit 22f1e8f

File tree

3 files changed

+52
-22
lines changed

3 files changed

+52
-22
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Changed
1111

12+
- Add possibility to select Timer master mode
1213
- Add inherent impl of `embedded_hal::Pwm` methods on `Pwm`s [#439]
1314
- Use `embedded-dma` v0.2 [#440]
1415
- Add LSI support for `Rtc` [#438]

src/fugit/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
use crate::pac::RCC;
44
use crate::rcc::Clocks;
5-
use crate::timer::WithPwm;
65
pub use crate::timer::{Channel, Error, Event, Instance, Ocm, SysEvent};
6+
use crate::timer::{MasterTimer, WithPwm};
77
use cast::u16;
88

99
pub mod delay;
@@ -148,3 +148,9 @@ impl<TIM: Instance, const FREQ: u32> Timer<TIM, FREQ> {
148148
self.tim.listen_interrupt(event, false);
149149
}
150150
}
151+
152+
impl<TIM: Instance + MasterTimer, const FREQ: u32> Timer<TIM, FREQ> {
153+
pub fn set_master_mode(&mut self, mode: TIM::Mms) {
154+
self.tim.master_mode(mode)
155+
}
156+
}

src/timer.rs

+44-21
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,13 @@ mod sealed {
252252
fn start_pwm(&mut self);
253253
fn enable_channel(channel: u8, b: bool);
254254
}
255+
256+
pub trait MasterTimer: General {
257+
type Mms;
258+
fn master_mode(&mut self, mode: Self::Mms);
259+
}
255260
}
256-
pub(crate) use sealed::{General, WithPwm};
261+
pub(crate) use sealed::{General, MasterTimer, WithPwm};
257262

258263
pub trait Instance:
259264
crate::Sealed + rcc::Enable + rcc::Reset + rcc::BusTimerClock + General
@@ -282,7 +287,12 @@ where
282287
}
283288

284289
macro_rules! hal {
285-
($($TIM:ty: [$Timer:ident, $bits:ty, $($cnum:ident $(, $aoe:ident)?)?],)+) => {
290+
($($TIM:ty: [
291+
$Timer:ident,
292+
$bits:ty,
293+
$(c: ($cnum:ident $(, $aoe:ident)?),)?
294+
$(m: $timbase:ident,)?
295+
],)+) => {
286296
$(
287297
impl Instance for $TIM { }
288298
pub type $Timer = Timer<$TIM>;
@@ -373,6 +383,13 @@ macro_rules! hal {
373383
}
374384
}
375385
$(with_pwm!($TIM: $cnum $(, $aoe)?);)?
386+
387+
$(impl MasterTimer for $TIM {
388+
type Mms = pac::$timbase::cr2::MMS_A;
389+
fn master_mode(&mut self, mode: Self::Mms) {
390+
self.cr2.modify(|_,w| w.mms().variant(mode));
391+
}
392+
})?
376393
)+
377394
}
378395
}
@@ -587,10 +604,7 @@ macro_rules! with_pwm {
587604
}
588605
}
589606

590-
impl<TIM> CountDownTimer<TIM>
591-
where
592-
TIM: General,
593-
{
607+
impl<TIM: General> CountDownTimer<TIM> {
594608
/// Starts listening for an `event`
595609
///
596610
/// Note, you will also have to enable the TIM2 interrupt in the NVIC to start
@@ -620,6 +634,12 @@ where
620634
}
621635
}
622636

637+
impl<TIM: General + MasterTimer> CountDownTimer<TIM> {
638+
pub fn set_master_mode(&mut self, mode: TIM::Mms) {
639+
self.tim.master_mode(mode)
640+
}
641+
}
642+
623643
#[inline(always)]
624644
pub(crate) const fn compute_arr_presc(freq: u32, clock: u32) -> (u16, u32) {
625645
let ticks = clock / freq;
@@ -688,35 +708,38 @@ where
688708

689709
// All F4xx parts have these timers.
690710
hal!(
691-
pac::TIM1: [Timer1, u16, CH4, _aoe],
692-
pac::TIM9: [Timer9, u16, CH2],
693-
pac::TIM11: [Timer11, u16, CH1],
711+
pac::TIM9: [Timer9, u16, c: (CH2),],
712+
pac::TIM11: [Timer11, u16, c: (CH1),],
694713
);
695714

696715
// All parts except for F410 add these timers.
697716
#[cfg(not(feature = "stm32f410"))]
698717
hal!(
699-
pac::TIM5: [Timer5, u32, CH4],
700-
pac::TIM2: [Timer2, u32, CH4],
701-
pac::TIM3: [Timer3, u16, CH4],
702-
pac::TIM4: [Timer4, u16, CH4],
703-
pac::TIM10: [Timer10, u16, CH1],
718+
pac::TIM1: [Timer1, u16, c: (CH4, _aoe), m: tim1,],
719+
pac::TIM5: [Timer5, u32, c: (CH4), m: tim5,],
720+
pac::TIM2: [Timer2, u32, c: (CH4), m: tim2,],
721+
pac::TIM3: [Timer3, u16, c: (CH4), m: tim3,],
722+
pac::TIM4: [Timer4, u16, c: (CH4), m: tim3,],
723+
pac::TIM10: [Timer10, u16, c: (CH1),],
704724
);
705725

706726
// TIM5 on F410 is 16-bit
707727
#[cfg(feature = "stm32f410")]
708-
hal!(pac::TIM5: [Timer5, u16, CH4],);
728+
hal!(
729+
pac::TIM1: [Timer1, u16, c: (CH4, _aoe), /*m: tim1,*/], // TODO: fix SVD
730+
pac::TIM5: [Timer5, u16, c: (CH4), /*m: tim5,*/], // TODO: fix SVD
731+
);
709732

710733
// All parts except F401 and F411.
711734
#[cfg(not(any(feature = "stm32f401", feature = "stm32f411",)))]
712-
hal!(pac::TIM6: [Timer6, u16,],);
735+
hal!(pac::TIM6: [Timer6, u16, /*m: tim7,*/],); // TODO: fix SVD
713736

714737
// All parts except F401, F410, F411.
715738
#[cfg(not(any(feature = "stm32f401", feature = "stm32f410", feature = "stm32f411",)))]
716739
hal!(
717-
pac::TIM7: [Timer7, u16,],
718-
pac::TIM8: [Timer8, u16, CH4, _aoe],
719-
pac::TIM12: [Timer12, u16, CH2],
720-
pac::TIM13: [Timer13, u16, CH1],
721-
pac::TIM14: [Timer14, u16, CH1],
740+
pac::TIM7: [Timer7, u16, /*m: tim7,*/], // TODO: fix SVD
741+
pac::TIM8: [Timer8, u16, c: (CH4, _aoe), m: tim1,],
742+
pac::TIM12: [Timer12, u16, c: (CH2),],
743+
pac::TIM13: [Timer13, u16, c: (CH1),],
744+
pac::TIM14: [Timer14, u16, c: (CH1),],
722745
);

0 commit comments

Comments
 (0)