@@ -70,83 +70,61 @@ impl<TIM> Pwm<TIM> {
70
70
}
71
71
}
72
72
73
- macro_rules! pwm {
74
- ( $( $TIMX: ident: ( $timX: ident, $arr: ident $( , $arr_h: ident) * ) , ) +) => {
75
- $(
76
- impl PwmExt for $TIMX {
77
- fn pwm( self , freq: Hertz , rcc: & mut Rcc ) -> Pwm <Self > {
78
- $timX( self , freq, rcc, ClockSource :: ApbTim )
79
- }
80
- }
73
+ impl < T : super :: private:: TimerBase > PwmExt for T {
74
+ fn pwm ( self , freq : Hertz , rcc : & mut Rcc ) -> Pwm < Self > {
75
+ Pwm :: new ( self , freq, rcc, ClockSource :: ApbTim )
76
+ }
77
+ }
81
78
82
- fn $timX ( tim : $TIMX , freq : Hertz , rcc : & mut Rcc , clock_source : ClockSource ) - > Pwm <$TIMX > {
83
- $TIMX :: enable ( rcc ) ;
84
- $TIMX :: reset ( rcc) ;
79
+ impl < T : super :: private :: TimerBase > Pwm < T > {
80
+ fn new ( mut tim : T , freq : Hertz , rcc : & mut Rcc , clock_source : ClockSource ) -> Pwm < T > {
81
+ tim . init ( rcc) ;
85
82
86
- let clk = match clock_source {
87
- ClockSource :: ApbTim => {
88
- rcc. ccipr( ) . modify( |_, w| w. tim1sel( ) . clear_bit( ) ) ;
89
- rcc. clocks. apb_tim_clk
90
- }
91
- ClockSource :: Pllq => {
92
- rcc. ccipr( ) . modify( |_, w| w. tim1sel( ) . set_bit( ) ) ;
93
- rcc. clocks. pll_clk. q. unwrap( )
94
- }
95
- } ;
96
-
97
- let mut pwm = Pwm :: <$TIMX> {
98
- clk,
99
- tim,
100
- } ;
101
- pwm. set_freq( freq) ;
102
- pwm
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 ( )
103
91
}
92
+ } ;
104
93
105
- impl Pwm <$TIMX> {
106
- /// Set the PWM frequency. Actual frequency may differ from
107
- /// requested due to precision of input clock. To check actual
108
- /// frequency, call freq.
109
- pub fn set_freq( & mut self , freq: Hertz ) {
110
- let ratio = self . clk / freq;
111
- let psc = ( ratio - 1 ) / 0xffff ;
94
+ tim. set_freq ( freq, clk) ;
95
+ tim. resume ( ) ;
112
96
113
- unsafe {
114
- let arr = ratio / ( psc + 1 ) - 1 ;
115
- self . tim. psc( ) . write( |w| w. psc( ) . bits( psc as u16 ) ) ;
116
- self . tim. arr( ) . write( |w| w. $arr( ) . bits( ( arr as u16 ) . into( ) ) ) ;
117
- $(
118
- self . tim. arr( ) . modify( |_, w| w. $arr_h( ) . bits( ( arr >> 16 ) as u16 ) ) ;
119
- ) *
120
- self . tim. cr1( ) . write( |w| w. cen( ) . set_bit( ) ) ;
121
- }
122
- }
123
- /// Starts listening
124
- pub fn listen( & mut self ) {
125
- self . tim. dier( ) . write( |w| w. uie( ) . set_bit( ) ) ;
126
- }
97
+ Self { clk, tim }
98
+ }
127
99
128
- /// Stops listening
129
- pub fn unlisten( & mut self ) {
130
- self . tim. dier( ) . write( |w| w. uie( ) . clear_bit( ) ) ;
131
- }
132
- /// Clears interrupt flag
133
- pub fn clear_irq( & mut self ) {
134
- self . tim. sr( ) . modify( |_, w| w. uif( ) . clear_bit( ) ) ;
135
- }
100
+ /// Set the PWM frequency. Actual frequency may differ from
101
+ /// requested due to precision of input clock. To check actual
102
+ /// frequency, call freq.
103
+ pub fn set_freq ( & mut self , freq : Hertz ) {
104
+ self . tim . set_freq ( freq, self . clk ) ;
105
+ }
106
+ /// Starts listening
107
+ pub fn listen ( & mut self ) {
108
+ self . tim . listen ( ) ;
109
+ }
136
110
137
- /// Resets counter value
138
- pub fn reset( & mut self ) {
139
- self . tim. cnt( ) . reset( ) ;
140
- }
111
+ /// Stops listening
112
+ pub fn unlisten ( & mut self ) {
113
+ self . tim . unlisten ( ) ;
114
+ }
115
+ /// Clears interrupt flag
116
+ pub fn clear_irq ( & mut self ) {
117
+ self . tim . clear_irq ( ) ;
118
+ }
141
119
142
- /// Returns the currently configured frequency
143
- pub fn freq ( & self ) -> Hertz {
144
- Hertz :: from_raw ( self . clk . raw ( )
145
- / ( self . tim . psc ( ) . read ( ) . bits ( ) + 1 )
146
- / ( self . tim . arr ( ) . read ( ) . bits ( ) + 1 ) )
147
- }
148
- }
149
- ) +
120
+ /// Resets counter value
121
+ pub fn reset ( & mut self ) {
122
+ self . tim . reset ( ) ;
123
+ }
124
+
125
+ /// Returns the currently configured frequency
126
+ pub fn freq ( & self ) -> Hertz {
127
+ self . tim . freq ( self . clk )
150
128
}
151
129
}
152
130
@@ -156,7 +134,7 @@ macro_rules! pwm_q {
156
134
$(
157
135
impl PwmQExt for $TIMX {
158
136
fn pwm_q( self , freq: Hertz , rcc: & mut Rcc ) -> Pwm <Self > {
159
- $timX ( self , freq, rcc, ClockSource :: Pllq )
137
+ Pwm :: new ( self , freq, rcc, ClockSource :: Pllq )
160
138
}
161
139
}
162
140
) +
@@ -324,31 +302,3 @@ pwm_hal! {
324
302
TIM3 : ( Channel3 , cc3e, ccmr2_output, oc3pe, oc3m, ccr3, ccr3_l, ccr3_h) ,
325
303
TIM3 : ( Channel4 , cc4e, ccmr2_output, oc4pe, oc4m, ccr4, ccr4_l, ccr4_h) ,
326
304
}
327
-
328
- pwm ! {
329
- TIM1 : ( tim1, arr) ,
330
- TIM3 : ( tim3, arr) ,
331
- TIM14 : ( tim14, arr) ,
332
- TIM16 : ( tim16, arr) ,
333
- TIM17 : ( tim17, arr) ,
334
- }
335
-
336
- #[ cfg( feature = "stm32g0x1" ) ]
337
- pwm ! {
338
- TIM2 : ( tim2, arr) ,
339
- }
340
-
341
- #[ cfg( any( feature = "stm32g070" , feature = "stm32g071" , feature = "stm32g081" ) ) ]
342
- pwm ! {
343
- TIM15 : ( tim15, arr) ,
344
- }
345
-
346
- #[ cfg( feature = "stm32g0x1" ) ]
347
- pwm_q ! {
348
- TIM1 : tim1,
349
- }
350
-
351
- #[ cfg( any( feature = "stm32g071" , feature = "stm32g081" ) ) ]
352
- pwm_q ! {
353
- TIM15 : tim15,
354
- }
0 commit comments