@@ -217,59 +217,71 @@ impl<TIM: Instance + WithPwm, const C: u8> PwmChannel<TIM, C> {
217
217
}
218
218
219
219
impl < TIM : Instance + WithPwm , const C : u8 , const COMP : bool > PwmChannel < TIM , C , COMP > {
220
+ /// Disable PWM channel
220
221
#[ inline]
221
222
pub fn disable ( & mut self ) {
222
223
TIM :: enable_channel ( C , false ) ;
223
224
}
224
225
226
+ /// Enable PWM channel
225
227
#[ inline]
226
228
pub fn enable ( & mut self ) {
227
229
TIM :: enable_channel ( C , true ) ;
228
230
}
229
231
232
+ /// Set PWM channel polarity
230
233
#[ inline]
231
234
pub fn set_polarity ( & mut self , p : Polarity ) {
232
235
TIM :: set_channel_polarity ( C , p) ;
233
236
}
234
237
238
+ /// Get PWM channel duty cycle
235
239
#[ inline]
236
240
pub fn get_duty ( & self ) -> u16 {
237
241
TIM :: read_cc_value ( C ) as u16
238
242
}
239
243
244
+ /// Get the maximum duty cycle value of the PWM channel
245
+ ///
240
246
/// If `0` returned means max_duty is 2^16
241
247
#[ inline]
242
248
pub fn get_max_duty ( & self ) -> u16 {
243
249
( TIM :: read_auto_reload ( ) as u16 ) . wrapping_add ( 1 )
244
250
}
245
251
252
+ /// Set PWM channel duty cycle
246
253
#[ inline]
247
254
pub fn set_duty ( & mut self , duty : u16 ) {
248
255
TIM :: set_cc_value ( C , duty as u32 )
249
256
}
250
257
258
+ /// Set complementary PWM channel polarity
251
259
#[ inline]
252
260
pub fn set_complementary_polarity ( & mut self , p : Polarity ) {
253
261
TIM :: set_nchannel_polarity ( C , p) ;
254
262
}
255
263
}
256
264
257
265
impl < TIM : Instance + WithPwm + Advanced , const C : u8 > PwmChannel < TIM , C , true > {
266
+ /// Disable complementary PWM channel
258
267
#[ inline]
259
268
pub fn disable_complementary ( & mut self ) {
260
269
TIM :: enable_nchannel ( C , false ) ;
261
270
}
262
271
272
+ /// Enable complementary PWM channel
263
273
#[ inline]
264
274
pub fn enable_complementary ( & mut self ) {
265
275
TIM :: enable_nchannel ( C , true ) ;
266
276
}
267
277
278
+ /// Set PWM channel idle state
268
279
#[ inline]
269
280
pub fn set_idle_state ( & mut self , s : IdleState ) {
270
281
TIM :: idle_state ( C , false , s) ;
271
282
}
272
283
284
+ /// Set complementary PWM channel idle state
273
285
#[ inline]
274
286
pub fn set_complementary_idle_state ( & mut self , s : IdleState ) {
275
287
TIM :: idle_state ( C , true , s) ;
@@ -592,55 +604,66 @@ where
592
604
TIM : Instance + WithPwm ,
593
605
PINS : Pins < TIM , P > ,
594
606
{
607
+ /// Enable PWM output of the timer on channel `channel`
595
608
#[ inline]
596
609
pub fn enable ( & mut self , channel : Channel ) {
597
610
TIM :: enable_channel ( PINS :: check_used ( channel) as u8 , true )
598
611
}
599
612
613
+ /// Disable PWM output of the timer on channel `channel`
600
614
#[ inline]
601
615
pub fn disable ( & mut self , channel : Channel ) {
602
616
TIM :: enable_channel ( PINS :: check_used ( channel) as u8 , false )
603
617
}
604
618
619
+ /// Set the polarity of the active state for the primary PWM output of the timer on channel `channel`
605
620
#[ inline]
606
621
pub fn set_polarity ( & mut self , channel : Channel , p : Polarity ) {
607
622
TIM :: set_channel_polarity ( PINS :: check_used ( channel) as u8 , p) ;
608
623
}
609
624
625
+ /// Get the current duty cycle of the timer on channel `channel`
610
626
#[ inline]
611
627
pub fn get_duty ( & self , channel : Channel ) -> u16 {
612
628
TIM :: read_cc_value ( PINS :: check_used ( channel) as u8 ) as u16
613
629
}
614
-
630
+ /// Get the current duty cycle of the timer on channel `channel` and convert to a duration
615
631
#[ inline]
616
632
pub fn get_duty_time ( & self , channel : Channel ) -> TimerDurationU32 < FREQ > {
617
633
TimerDurationU32 :: from_ticks ( TIM :: read_cc_value ( PINS :: check_used ( channel) as u8 ) )
618
634
}
619
635
636
+ /// Set the duty cycle of the timer on channel `channel`
620
637
#[ inline]
621
638
pub fn set_duty ( & mut self , channel : Channel , duty : u16 ) {
622
639
TIM :: set_cc_value ( PINS :: check_used ( channel) as u8 , duty. into ( ) )
623
640
}
624
641
642
+ /// Set the duty cycle of the timer on channel `channel` from a duration
625
643
#[ inline]
626
644
pub fn set_duty_time ( & mut self , channel : Channel , duty : TimerDurationU32 < FREQ > ) {
627
645
TIM :: set_cc_value ( PINS :: check_used ( channel) as u8 , duty. ticks ( ) )
628
646
}
629
647
648
+ /// Get the maximum duty cycle value of the timer
649
+ ///
630
650
/// If `0` returned means max_duty is 2^16
631
651
pub fn get_max_duty ( & self ) -> u16 {
632
652
( TIM :: read_auto_reload ( ) as u16 ) . wrapping_add ( 1 )
633
653
}
634
654
655
+ /// Get the PWM frequency of the timer as a duration
635
656
pub fn get_period ( & self ) -> TimerDurationU32 < FREQ > {
636
657
TimerDurationU32 :: from_ticks ( TIM :: read_auto_reload ( ) + 1 )
637
658
}
638
659
660
+ /// Set the PWM frequency for the timer from a duration
639
661
pub fn set_period ( & mut self , period : TimerDurationU32 < FREQ > ) {
640
662
self . tim . set_auto_reload ( period. ticks ( ) - 1 ) . unwrap ( ) ;
641
663
self . tim . cnt_reset ( ) ;
642
664
}
643
665
666
+ /// Set the polarity of the active state for the complementary PWM output of the advanced timer on channel `channel`
644
667
#[ inline]
645
668
pub fn set_complementary_polarity ( & mut self , channel : Channel , p : Polarity ) {
646
669
TIM :: set_channel_polarity ( PINS :: check_complementary_used ( channel) as u8 , p) ;
@@ -652,30 +675,39 @@ where
652
675
TIM : Instance + WithPwm + Advanced ,
653
676
PINS : Pins < TIM , P > ,
654
677
{
678
+ /// Enable complementary PWM output of the timer on channel `channel`
655
679
#[ inline]
656
680
pub fn enable_complementary ( & mut self , channel : Channel ) {
657
681
TIM :: enable_nchannel ( PINS :: check_complementary_used ( channel) as u8 , true )
658
682
}
659
683
684
+ /// Disable complementary PWM output of the timer on channel `channel`
660
685
#[ inline]
661
686
pub fn disable_complementary ( & mut self , channel : Channel ) {
662
687
TIM :: enable_nchannel ( PINS :: check_complementary_used ( channel) as u8 , false )
663
688
}
664
689
665
- /// Set number DTS ticks during that complementary pin is `dead`
690
+ /// Set number DTS ticks during that the primary and complementary PWM pins are simultaneously forced to their inactive states
691
+ /// ( see [`Polarity`] setting ) when changing PWM state. This duration when both channels are in an 'off' state is called 'dead time'.
692
+ ///
693
+ /// This is necessary in applications like motor control or power converters to prevent the destruction of the switching elements by
694
+ /// short circuit in the moment of switching.
666
695
#[ inline]
667
696
pub fn set_dead_time ( & mut self , dts_ticks : u16 ) {
668
697
let bits = pack_ceil_dead_time ( dts_ticks) ;
669
698
TIM :: set_dtg_value ( bits) ;
670
699
}
671
700
672
701
/// Set raw dead time (DTG) bits
702
+ ///
703
+ /// The dead time generation is nonlinear and constrained by the DTS tick duration. DTG register configuration and calculation of
704
+ /// the actual resulting dead time is described in the application note RM0368 from ST Microelectronics
673
705
#[ inline]
674
706
pub fn set_dead_time_bits ( & mut self , bits : u8 ) {
675
707
TIM :: set_dtg_value ( bits) ;
676
708
}
677
709
678
- /// Return dead time for complementary pins in DTS ticks
710
+ /// Return dead time for complementary pins in the unit of DTS ticks
679
711
#[ inline]
680
712
pub fn get_dead_time ( & self ) -> u16 {
681
713
unpack_dead_time ( TIM :: read_dtg_value ( ) )
@@ -687,17 +719,21 @@ where
687
719
TIM :: read_dtg_value ( )
688
720
}
689
721
722
+ /// Set the pin idle state
690
723
#[ inline]
691
724
pub fn set_idle_state ( & mut self , channel : Channel , s : IdleState ) {
692
725
TIM :: idle_state ( PINS :: check_used ( channel) as u8 , false , s) ;
693
726
}
694
727
728
+ /// Set the complementary pin idle state
695
729
#[ inline]
696
730
pub fn set_complementary_idle_state ( & mut self , channel : Channel , s : IdleState ) {
697
731
TIM :: idle_state ( PINS :: check_complementary_used ( channel) as u8 , true , s) ;
698
732
}
699
733
}
700
734
735
+ /// Convert number dead time ticks to raw DTG register bits.
736
+ /// Values greater than 1009 result in maximum dead time of 126 us
701
737
const fn pack_ceil_dead_time ( dts_ticks : u16 ) -> u8 {
702
738
match dts_ticks {
703
739
0 ..=127 => dts_ticks as u8 ,
@@ -708,6 +744,7 @@ const fn pack_ceil_dead_time(dts_ticks: u16) -> u8 {
708
744
}
709
745
}
710
746
747
+ /// Convert raw DTG register bits value to number of dead time ticks
711
748
const fn unpack_dead_time ( bits : u8 ) -> u16 {
712
749
if bits & 0b_1000_0000 == 0 {
713
750
bits as u16
0 commit comments