@@ -34,10 +34,22 @@ pub struct PwmPin<TIM, CH> {
34
34
channel : PhantomData < CH > ,
35
35
}
36
36
37
+ enum ClockSource {
38
+ ApbTim ,
39
+ #[ allow( dead_code) ]
40
+ Pllq ,
41
+ }
42
+
37
43
pub trait PwmExt : Sized {
38
44
fn pwm ( self , freq : Hertz , rcc : & mut Rcc ) -> Pwm < Self > ;
39
45
}
40
46
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
+
41
53
pub trait PwmPinMode {
42
54
fn set_compare_mode ( & mut self , mode : OutputCompareMode ) ;
43
55
}
@@ -60,16 +72,27 @@ macro_rules! pwm {
60
72
$(
61
73
impl PwmExt for $TIMX {
62
74
fn pwm( self , freq: Hertz , rcc: & mut Rcc ) -> Pwm <Self > {
63
- $timX( self , freq, rcc)
75
+ $timX( self , freq, rcc, ClockSource :: ApbTim )
64
76
}
65
77
}
66
78
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> {
68
80
$TIMX:: enable( rcc) ;
69
81
$TIMX:: reset( rcc) ;
70
82
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
+
71
94
let mut pwm = Pwm :: <$TIMX> {
72
- clk: rcc . clocks . apb_tim_clk ,
95
+ clk,
73
96
tim,
74
97
} ;
75
98
pwm. set_freq( freq) ;
@@ -114,6 +137,19 @@ macro_rules! pwm {
114
137
}
115
138
}
116
139
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
+
117
153
#[ cfg( any( feature = "stm32g0x1" , feature = "stm32g070" ) ) ]
118
154
macro_rules! pwm_hal {
119
155
( $( $TIMX: ident:
@@ -270,3 +306,13 @@ pwm! {
270
306
pwm ! {
271
307
TIM15 : ( tim15, arr) ,
272
308
}
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