@@ -32,6 +32,24 @@ pub struct C3;
32
32
pub struct C3N ;
33
33
pub struct C4 ;
34
34
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
+
35
53
pub struct PwmChannels < TIM , CHANNELS > {
36
54
_channel : PhantomData < CHANNELS > ,
37
55
_tim : PhantomData < TIM > ,
@@ -321,7 +339,7 @@ macro_rules! pwm_4_channels_with_3_complementary_outputs {
321
339
rcc. regs. $apbrstr. modify( |_, w| w. $timXrst( ) . set_bit( ) ) ;
322
340
rcc. regs. $apbrstr. modify( |_, w| w. $timXrst( ) . clear_bit( ) ) ;
323
341
324
- if PINS :: C1N | PINS :: C1N | PINS :: C1N {
342
+ if PINS :: C1N | PINS :: C2N | PINS :: C3N {
325
343
tim. bdtr. modify( |_, w| w. ossr( ) . set_bit( ) ) ;
326
344
}
327
345
if PINS :: C1 {
@@ -406,9 +424,7 @@ macro_rules! pwm_4_channels_with_3_complementary_outputs {
406
424
}
407
425
}
408
426
409
- impl hal:: PwmPin for PwmChannels <$TIMX, C1N > {
410
- type Duty = u16 ;
411
-
427
+ impl ComplementaryPwm for PwmChannels <$TIMX, C1N > {
412
428
//NOTE(unsafe) atomic write with no side effects
413
429
fn disable( & mut self ) {
414
430
unsafe { ( * ( $TIMX:: ptr( ) ) ) . ccer. modify( |_, w| w. cc1ne( ) . clear_bit( ) ) } ;
@@ -419,19 +435,9 @@ macro_rules! pwm_4_channels_with_3_complementary_outputs {
419
435
unsafe { ( * ( $TIMX:: ptr( ) ) ) . ccer. modify( |_, w| w. cc1ne( ) . set_bit( ) ) } ;
420
436
}
421
437
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 ) ) } ;
435
441
}
436
442
}
437
443
@@ -464,9 +470,7 @@ macro_rules! pwm_4_channels_with_3_complementary_outputs {
464
470
}
465
471
}
466
472
467
- impl hal:: PwmPin for PwmChannels <$TIMX, C2N > {
468
- type Duty = u16 ;
469
-
473
+ impl ComplementaryPwm for PwmChannels <$TIMX, C2N > {
470
474
//NOTE(unsafe) atomic write with no side effects
471
475
fn disable( & mut self ) {
472
476
unsafe { ( * ( $TIMX:: ptr( ) ) ) . ccer. modify( |_, w| w. cc2ne( ) . clear_bit( ) ) } ;
@@ -477,19 +481,9 @@ macro_rules! pwm_4_channels_with_3_complementary_outputs {
477
481
unsafe { ( * ( $TIMX:: ptr( ) ) ) . ccer. modify( |_, w| w. cc2ne( ) . set_bit( ) ) } ;
478
482
}
479
483
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 ) ) } ;
493
487
}
494
488
}
495
489
@@ -522,9 +516,7 @@ macro_rules! pwm_4_channels_with_3_complementary_outputs {
522
516
}
523
517
}
524
518
525
- impl hal:: PwmPin for PwmChannels <$TIMX, C3N > {
526
- type Duty = u16 ;
527
-
519
+ impl ComplementaryPwm for PwmChannels <$TIMX, C3N > {
528
520
//NOTE(unsafe) atomic write with no side effects
529
521
fn disable( & mut self ) {
530
522
unsafe { ( * ( $TIMX:: ptr( ) ) ) . ccer. modify( |_, w| w. cc3ne( ) . clear_bit( ) ) } ;
@@ -535,19 +527,9 @@ macro_rules! pwm_4_channels_with_3_complementary_outputs {
535
527
unsafe { ( * ( $TIMX:: ptr( ) ) ) . ccer. modify( |_, w| w. cc3ne( ) . set_bit( ) ) } ;
536
528
}
537
529
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 ) ) } ;
551
533
}
552
534
}
553
535
0 commit comments