@@ -8,6 +8,7 @@ use super::low_level::{CountingMode, OutputPolarity, Timer};
88use super :: simple_pwm:: PwmPin ;
99use super :: { AdvancedInstance4Channel , Ch1 , Ch2 , Ch3 , Ch4 , Channel , TimerComplementaryPin } ;
1010use crate :: Peri ;
11+ use crate :: dma:: word:: Word ;
1112use crate :: gpio:: { AnyPin , OutputType } ;
1213use crate :: time:: Hertz ;
1314use crate :: timer:: TimerChannel ;
@@ -176,20 +177,20 @@ impl<'d, T: AdvancedInstance4Channel> ComplementaryPwm<'d, T> {
176177 /// Get max duty value.
177178 ///
178179 /// This value depends on the configured frequency and the timer's clock rate from RCC.
179- pub fn get_max_duty ( & self ) -> u16 {
180+ pub fn get_max_duty ( & self ) -> u32 {
180181 if self . inner . get_counting_mode ( ) . is_center_aligned ( ) {
181- self . inner . get_max_compare_value ( ) as u16
182+ self . inner . get_max_compare_value ( ) . into ( )
182183 } else {
183- self . inner . get_max_compare_value ( ) as u16 + 1
184+ self . inner . get_max_compare_value ( ) . into ( ) + 1
184185 }
185186 }
186187
187188 /// Set the duty for a given channel.
188189 ///
189190 /// The value ranges from 0 for 0% duty, to [`get_max_duty`](Self::get_max_duty) for 100% duty, both included.
190- pub fn set_duty ( & mut self , channel : Channel , duty : u16 ) {
191+ pub fn set_duty ( & mut self , channel : Channel , duty : u32 ) {
191192 assert ! ( duty <= self . get_max_duty( ) ) ;
192- self . inner . set_compare_value ( channel, duty as _ )
193+ self . inner . set_compare_value ( channel, unwrap ! ( duty. try_into ( ) ) )
193194 }
194195
195196 /// Set the output polarity for a given channel.
@@ -220,8 +221,14 @@ impl<'d, T: AdvancedInstance4Channel> ComplementaryPwm<'d, T> {
220221 ///
221222 /// Note:
222223 /// you will need to provide corresponding TIMx_UP DMA channel to use this method.
223- pub async fn waveform_up ( & mut self , dma : Peri < ' _ , impl super :: UpDma < T > > , channel : Channel , duty : & [ u16 ] ) {
224+ pub async fn waveform_up < W : Word + Into < T :: Word > > (
225+ & mut self ,
226+ dma : Peri < ' _ , impl super :: UpDma < T > > ,
227+ channel : Channel ,
228+ duty : & [ W ] ,
229+ ) {
224230 self . inner . enable_channel ( channel, true ) ;
231+ self . inner . clamp_compare_value :: < W > ( channel) ;
225232 self . inner . enable_update_dma ( true ) ;
226233 self . inner . setup_update_dma ( dma, channel, duty) . await ;
227234 self . inner . enable_update_dma ( false ) ;
@@ -256,13 +263,21 @@ impl<'d, T: AdvancedInstance4Channel> ComplementaryPwm<'d, T> {
256263 /// Also be aware that embassy timers use one of timers internally. It is possible to
257264 /// switch this timer by using `time-driver-timX` feature.
258265 ///
259- pub async fn waveform_up_multi_channel (
266+ pub async fn waveform_up_multi_channel < W : Word + Into < T :: Word > > (
260267 & mut self ,
261268 dma : Peri < ' _ , impl super :: UpDma < T > > ,
262269 starting_channel : Channel ,
263270 ending_channel : Channel ,
264- duty : & [ u16 ] ,
271+ duty : & [ W ] ,
265272 ) {
273+ [ Channel :: Ch1 , Channel :: Ch2 , Channel :: Ch3 , Channel :: Ch4 ]
274+ . iter ( )
275+ . filter ( |ch| ch. index ( ) >= starting_channel. index ( ) )
276+ . filter ( |ch| ch. index ( ) <= ending_channel. index ( ) )
277+ . for_each ( |ch| {
278+ self . inner . enable_channel ( * ch, true ) ;
279+ self . inner . clamp_compare_value :: < W > ( * ch) ;
280+ } ) ;
266281 self . inner . enable_update_dma ( true ) ;
267282 self . inner
268283 . setup_update_dma_burst ( dma, starting_channel, ending_channel, duty)
@@ -291,20 +306,20 @@ impl<'d, T: AdvancedInstance4Channel> embedded_hal_02::Pwm for ComplementaryPwm<
291306 }
292307
293308 fn get_duty ( & self , channel : Self :: Channel ) -> Self :: Duty {
294- self . inner . get_compare_value ( channel) as u16
309+ unwrap ! ( self . inner. get_compare_value( channel) . try_into ( ) )
295310 }
296311
297312 fn get_max_duty ( & self ) -> Self :: Duty {
298313 if self . inner . get_counting_mode ( ) . is_center_aligned ( ) {
299- self . inner . get_max_compare_value ( ) as u16
314+ unwrap ! ( self . inner. get_max_compare_value( ) . try_into ( ) )
300315 } else {
301- self . inner . get_max_compare_value ( ) as u16 + 1
316+ unwrap ! ( self . inner. get_max_compare_value( ) . try_into ( ) ) + 1
302317 }
303318 }
304319
305320 fn set_duty ( & mut self , channel : Self :: Channel , duty : Self :: Duty ) {
306- assert ! ( duty <= self . get_max_duty( ) ) ;
307- self . inner . set_compare_value ( channel, duty as u32 )
321+ assert ! ( duty <= unwrap! ( self . get_max_duty( ) . try_into ( ) ) ) ;
322+ self . inner . set_compare_value ( channel, unwrap ! ( duty. try_into ( ) ) )
308323 }
309324
310325 fn set_period < P > ( & mut self , period : P )
0 commit comments