@@ -32,6 +32,24 @@ pub struct C3;
3232pub struct C3N ;
3333pub struct C4 ;
3434
35+ pub enum DTInterval {
36+ DT_0 = 0 ,
37+ DT_1 = 1 ,
38+ DT_2 = 2 ,
39+ DT_3 = 3 ,
40+ DT_4 = 4 ,
41+ DT_5 = 5 ,
42+ DT_6 = 6 ,
43+ DT_7 = 7 ,
44+ }
45+
46+ pub trait ComplementaryPwm {
47+ fn disable ( & mut self ) ;
48+ fn enable ( & mut self ) ;
49+
50+ fn set_dead_time ( & mut self , duration : DTInterval ) ;
51+ }
52+
3553pub struct PwmChannels < TIM , CHANNELS > {
3654 _channel : PhantomData < CHANNELS > ,
3755 _tim : PhantomData < TIM > ,
@@ -321,7 +339,7 @@ macro_rules! pwm_4_channels_with_3_complementary_outputs {
321339 rcc. regs. $apbrstr. modify( |_, w| w. $timXrst( ) . set_bit( ) ) ;
322340 rcc. regs. $apbrstr. modify( |_, w| w. $timXrst( ) . clear_bit( ) ) ;
323341
324- if PINS :: C1N | PINS :: C1N | PINS :: C1N {
342+ if PINS :: C1N | PINS :: C2N | PINS :: C3N {
325343 tim. bdtr. modify( |_, w| w. ossr( ) . set_bit( ) ) ;
326344 }
327345 if PINS :: C1 {
@@ -406,9 +424,7 @@ macro_rules! pwm_4_channels_with_3_complementary_outputs {
406424 }
407425 }
408426
409- impl hal:: PwmPin for PwmChannels <$TIMX, C1N > {
410- type Duty = u16 ;
411-
427+ impl ComplementaryPwm for PwmChannels <$TIMX, C1N > {
412428 //NOTE(unsafe) atomic write with no side effects
413429 fn disable( & mut self ) {
414430 unsafe { ( * ( $TIMX:: ptr( ) ) ) . ccer. modify( |_, w| w. cc1ne( ) . clear_bit( ) ) } ;
@@ -419,19 +435,9 @@ macro_rules! pwm_4_channels_with_3_complementary_outputs {
419435 unsafe { ( * ( $TIMX:: ptr( ) ) ) . ccer. modify( |_, w| w. cc1ne( ) . set_bit( ) ) } ;
420436 }
421437
422- //NOTE(unsafe) atomic read with no side effects
423- fn get_duty( & self ) -> u16 {
424- unsafe { ( * $TIMX:: ptr( ) ) . ccr1. read( ) . ccr( ) . bits( ) as u16 }
425- }
426-
427- //NOTE(unsafe) atomic read with no side effects
428- fn get_max_duty( & self ) -> u16 {
429- unsafe { ( * $TIMX:: ptr( ) ) . arr. read( ) . arr( ) . bits( ) as u16 }
430- }
431-
432- //NOTE(unsafe) atomic write with no side effects
433- fn set_duty( & mut self , duty: u16 ) {
434- unsafe { ( * $TIMX:: ptr( ) ) . ccr1. write( |w| w. ccr( ) . bits( duty. into( ) ) ) }
438+ // NOTE: dead time insertion is per timer not per channel
439+ fn set_dead_time( & mut self , duration: DTInterval ) {
440+ unsafe { ( * ( $TIMX:: ptr( ) ) ) . bdtr. modify( |_, w| w. dtg( ) . bits( duration as u8 ) ) } ;
435441 }
436442 }
437443
@@ -464,9 +470,7 @@ macro_rules! pwm_4_channels_with_3_complementary_outputs {
464470 }
465471 }
466472
467- impl hal:: PwmPin for PwmChannels <$TIMX, C2N > {
468- type Duty = u16 ;
469-
473+ impl ComplementaryPwm for PwmChannels <$TIMX, C2N > {
470474 //NOTE(unsafe) atomic write with no side effects
471475 fn disable( & mut self ) {
472476 unsafe { ( * ( $TIMX:: ptr( ) ) ) . ccer. modify( |_, w| w. cc2ne( ) . clear_bit( ) ) } ;
@@ -477,19 +481,9 @@ macro_rules! pwm_4_channels_with_3_complementary_outputs {
477481 unsafe { ( * ( $TIMX:: ptr( ) ) ) . ccer. modify( |_, w| w. cc2ne( ) . set_bit( ) ) } ;
478482 }
479483
480- //NOTE(unsafe) atomic read with no side effects
481- fn get_duty( & self ) -> u16 {
482- unsafe { ( * $TIMX:: ptr( ) ) . ccr2. read( ) . ccr( ) . bits( ) as u16 }
483- }
484-
485- //NOTE(unsafe) atomic read with no side effects
486- fn get_max_duty( & self ) -> u16 {
487- unsafe { ( * $TIMX:: ptr( ) ) . arr. read( ) . arr( ) . bits( ) as u16 }
488- }
489-
490- //NOTE(unsafe) atomic write with no side effects
491- fn set_duty( & mut self , duty: u16 ) {
492- unsafe { ( * $TIMX:: ptr( ) ) . ccr2. write( |w| w. ccr( ) . bits( duty. into( ) ) ) }
484+ // NOTE: dead time insertion is per timer not per channel
485+ fn set_dead_time( & mut self , duration: DTInterval ) {
486+ unsafe { ( * ( $TIMX:: ptr( ) ) ) . bdtr. modify( |_, w| w. dtg( ) . bits( duration as u8 ) ) } ;
493487 }
494488 }
495489
@@ -522,9 +516,7 @@ macro_rules! pwm_4_channels_with_3_complementary_outputs {
522516 }
523517 }
524518
525- impl hal:: PwmPin for PwmChannels <$TIMX, C3N > {
526- type Duty = u16 ;
527-
519+ impl ComplementaryPwm for PwmChannels <$TIMX, C3N > {
528520 //NOTE(unsafe) atomic write with no side effects
529521 fn disable( & mut self ) {
530522 unsafe { ( * ( $TIMX:: ptr( ) ) ) . ccer. modify( |_, w| w. cc3ne( ) . clear_bit( ) ) } ;
@@ -535,19 +527,9 @@ macro_rules! pwm_4_channels_with_3_complementary_outputs {
535527 unsafe { ( * ( $TIMX:: ptr( ) ) ) . ccer. modify( |_, w| w. cc3ne( ) . set_bit( ) ) } ;
536528 }
537529
538- //NOTE(unsafe) atomic read with no side effects
539- fn get_duty( & self ) -> u16 {
540- unsafe { ( * $TIMX:: ptr( ) ) . ccr3. read( ) . ccr( ) . bits( ) as u16 }
541- }
542-
543- //NOTE(unsafe) atomic read with no side effects
544- fn get_max_duty( & self ) -> u16 {
545- unsafe { ( * $TIMX:: ptr( ) ) . arr. read( ) . arr( ) . bits( ) as u16 }
546- }
547-
548- //NOTE(unsafe) atomic write with no side effects
549- fn set_duty( & mut self , duty: u16 ) {
550- unsafe { ( * $TIMX:: ptr( ) ) . ccr3. write( |w| w. ccr( ) . bits( duty. into( ) ) ) }
530+ // NOTE: dead time insertion is per timer not per channel
531+ fn set_dead_time( & mut self , duration: DTInterval ) {
532+ unsafe { ( * ( $TIMX:: ptr( ) ) ) . bdtr. modify( |_, w| w. dtg( ) . bits( duration as u8 ) ) } ;
551533 }
552534 }
553535
0 commit comments