@@ -99,18 +99,16 @@ impl<'d, T: GeneralInstance4Channel> SimplePwmChannel<'d, T> {
9999 /// Get max duty value.
100100 ///
101101 /// This value depends on the configured frequency and the timer's clock rate from RCC.
102- pub fn max_duty_cycle ( & self ) -> u16 {
103- let max: u32 = self . timer . get_max_compare_value ( ) . into ( ) ;
104- assert ! ( max < u16 :: MAX as u32 ) ;
105- max as u16 + 1
102+ pub fn max_duty_cycle ( & self ) -> u32 {
103+ self . timer . get_max_compare_value ( ) . into ( ) + 1
106104 }
107105
108106 /// Set the duty for a given channel.
109107 ///
110108 /// The value ranges from 0 for 0% duty, to [`max_duty_cycle`](Self::max_duty_cycle) for 100% duty, both included.
111- pub fn set_duty_cycle ( & mut self , duty : u16 ) {
109+ pub fn set_duty_cycle ( & mut self , duty : u32 ) {
112110 assert ! ( duty <= ( * self ) . max_duty_cycle( ) ) ;
113- self . timer . set_compare_value ( self . channel , duty. into ( ) )
111+ self . timer . set_compare_value ( self . channel , unwrap ! ( duty. try_into ( ) ) )
114112 }
115113
116114 /// Set the duty cycle to 0%, or always inactive.
@@ -127,21 +125,21 @@ impl<'d, T: GeneralInstance4Channel> SimplePwmChannel<'d, T> {
127125 ///
128126 /// The caller is responsible for ensuring that `num` is less than or equal to `denom`,
129127 /// and that `denom` is not zero.
130- pub fn set_duty_cycle_fraction ( & mut self , num : u16 , denom : u16 ) {
128+ pub fn set_duty_cycle_fraction ( & mut self , num : u32 , denom : u32 ) {
131129 assert ! ( denom != 0 ) ;
132130 assert ! ( num <= denom) ;
133131 let duty = u32:: from ( num) * u32:: from ( self . max_duty_cycle ( ) ) / u32:: from ( denom) ;
134132
135133 // This is safe because we know that `num <= denom`, so `duty <= self.max_duty_cycle()` (u16)
136134 #[ allow( clippy:: cast_possible_truncation) ]
137- self . set_duty_cycle ( duty as u16 ) ;
135+ self . set_duty_cycle ( unwrap ! ( duty. try_into ( ) ) ) ;
138136 }
139137
140138 /// Set the duty cycle to `percent / 100`
141139 ///
142140 /// The caller is responsible for ensuring that `percent` is less than or equal to 100.
143141 pub fn set_duty_cycle_percent ( & mut self , percent : u8 ) {
144- self . set_duty_cycle_fraction ( u16 :: from ( percent) , 100 )
142+ self . set_duty_cycle_fraction ( percent as u32 , 100 )
145143 }
146144
147145 /// Get the duty for a given channel.
@@ -334,10 +332,8 @@ impl<'d, T: GeneralInstance4Channel> SimplePwm<'d, T> {
334332 /// Get max duty value.
335333 ///
336334 /// This value depends on the configured frequency and the timer's clock rate from RCC.
337- pub fn max_duty_cycle ( & self ) -> u16 {
338- let max: u32 = self . inner . get_max_compare_value ( ) . into ( ) ;
339- assert ! ( max < u16 :: MAX as u32 ) ;
340- max as u16 + 1
335+ pub fn max_duty_cycle ( & self ) -> u32 {
336+ self . inner . get_max_compare_value ( ) . into ( ) + 1
341337 }
342338
343339 /// Generate a sequence of PWM waveform
@@ -417,11 +413,11 @@ impl<'d, T: GeneralInstance4Channel> embedded_hal_1::pwm::ErrorType for SimplePw
417413
418414impl < ' d , T : GeneralInstance4Channel > embedded_hal_1:: pwm:: SetDutyCycle for SimplePwmChannel < ' d , T > {
419415 fn max_duty_cycle ( & self ) -> u16 {
420- self . max_duty_cycle ( )
416+ unwrap ! ( self . max_duty_cycle( ) . try_into ( ) )
421417 }
422418
423419 fn set_duty_cycle ( & mut self , duty : u16 ) -> Result < ( ) , Self :: Error > {
424- self . set_duty_cycle ( duty) ;
420+ self . set_duty_cycle ( duty. into ( ) ) ;
425421 Ok ( ( ) )
426422 }
427423
@@ -436,7 +432,7 @@ impl<'d, T: GeneralInstance4Channel> embedded_hal_1::pwm::SetDutyCycle for Simpl
436432 }
437433
438434 fn set_duty_cycle_fraction ( & mut self , num : u16 , denom : u16 ) -> Result < ( ) , Self :: Error > {
439- self . set_duty_cycle_fraction ( num, denom) ;
435+ self . set_duty_cycle_fraction ( num. into ( ) , denom. into ( ) ) ;
440436 Ok ( ( ) )
441437 }
442438
0 commit comments