Skip to content

Commit 2cf638d

Browse files
Add support for using PLLQ with TIM1/TIM15 (#115)
1 parent 1a5705e commit 2cf638d

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/prelude.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub use crate::spi::SpiExt as _;
3939
pub use crate::time::U32Ext as _;
4040
pub use crate::timer::opm::OpmExt as _;
4141
pub use crate::timer::pwm::PwmExt as _;
42+
pub use crate::timer::pwm::PwmQExt as _;
4243
pub use crate::timer::qei::QeiExt as _;
4344
pub use crate::timer::stopwatch::StopwatchExt as _;
4445
pub use crate::timer::TimerExt as _;

src/timer/pwm.rs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,22 @@ pub struct PwmPin<TIM, CH> {
3434
channel: PhantomData<CH>,
3535
}
3636

37+
enum ClockSource {
38+
ApbTim,
39+
#[allow(dead_code)]
40+
Pllq,
41+
}
42+
3743
pub trait PwmExt: Sized {
3844
fn pwm(self, freq: Hertz, rcc: &mut Rcc) -> Pwm<Self>;
3945
}
4046

47+
pub trait PwmQExt: Sized {
48+
// Configures PWM using PLLQ as a clock source. Panics if PLLQ was not
49+
// enabled when RCC was configured.
50+
fn pwm_q(self, freq: Hertz, rcc: &mut Rcc) -> Pwm<Self>;
51+
}
52+
4153
pub trait PwmPinMode {
4254
fn set_compare_mode(&mut self, mode: OutputCompareMode);
4355
}
@@ -60,16 +72,27 @@ macro_rules! pwm {
6072
$(
6173
impl PwmExt for $TIMX {
6274
fn pwm(self, freq: Hertz, rcc: &mut Rcc) -> Pwm<Self> {
63-
$timX(self, freq, rcc)
75+
$timX(self, freq, rcc, ClockSource::ApbTim)
6476
}
6577
}
6678

67-
fn $timX(tim: $TIMX, freq: Hertz, rcc: &mut Rcc) -> Pwm<$TIMX> {
79+
fn $timX(tim: $TIMX, freq: Hertz, rcc: &mut Rcc, clock_source: ClockSource) -> Pwm<$TIMX> {
6880
$TIMX::enable(rcc);
6981
$TIMX::reset(rcc);
7082

83+
let clk = match clock_source {
84+
ClockSource::ApbTim => {
85+
rcc.ccipr.modify(|_, w| w.tim1sel().clear_bit());
86+
rcc.clocks.apb_tim_clk
87+
}
88+
ClockSource::Pllq => {
89+
rcc.ccipr.modify(|_, w| w.tim1sel().set_bit());
90+
rcc.clocks.pll_clk.q.unwrap()
91+
}
92+
};
93+
7194
let mut pwm = Pwm::<$TIMX> {
72-
clk: rcc.clocks.apb_tim_clk,
95+
clk,
7396
tim,
7497
};
7598
pwm.set_freq(freq);
@@ -114,6 +137,19 @@ macro_rules! pwm {
114137
}
115138
}
116139

140+
#[allow(unused_macros)]
141+
macro_rules! pwm_q {
142+
($($TIMX:ident: $timX:ident,)+) => {
143+
$(
144+
impl PwmQExt for $TIMX {
145+
fn pwm_q(self, freq: Hertz, rcc: &mut Rcc) -> Pwm<Self> {
146+
$timX(self, freq, rcc, ClockSource::Pllq)
147+
}
148+
}
149+
)+
150+
}
151+
}
152+
117153
#[cfg(any(feature = "stm32g0x1", feature = "stm32g070"))]
118154
macro_rules! pwm_hal {
119155
($($TIMX:ident:
@@ -270,3 +306,13 @@ pwm! {
270306
pwm! {
271307
TIM15: (tim15, arr),
272308
}
309+
310+
#[cfg(feature = "stm32g0x1")]
311+
pwm_q! {
312+
TIM1: tim1,
313+
}
314+
315+
#[cfg(any(feature = "stm32g071", feature = "stm32g081"))]
316+
pwm_q! {
317+
TIM15: tim15,
318+
}

0 commit comments

Comments
 (0)