34
34
//! and change their polarity with `set_polarity` and `set_complementary_polarity`.
35
35
36
36
use super :: {
37
- compute_arr_presc, Advanced , Channel , FTimer , Instance , Ocm , Polarity , Timer , WithPwm ,
37
+ compute_arr_presc, Advanced , Channel , FTimer , IdleState , Instance , Ocm , Polarity , Timer ,
38
+ WithPwm ,
38
39
} ;
39
40
use crate :: rcc:: Clocks ;
40
41
use core:: marker:: PhantomData ;
@@ -263,6 +264,16 @@ impl<TIM: Instance + WithPwm + Advanced, const C: u8> PwmChannel<TIM, C, true> {
263
264
pub fn enable_complementary ( & mut self ) {
264
265
TIM :: enable_nchannel ( C , true ) ;
265
266
}
267
+
268
+ #[ inline]
269
+ pub fn set_idle_state ( & mut self , s : IdleState ) {
270
+ TIM :: idle_state ( C , false , s) ;
271
+ }
272
+
273
+ #[ inline]
274
+ pub fn set_complementary_idle_state ( & mut self , s : IdleState ) {
275
+ TIM :: idle_state ( C , true , s) ;
276
+ }
266
277
}
267
278
268
279
pub struct PwmHz < TIM , P , PINS >
@@ -359,22 +370,27 @@ where
359
370
TIM : Instance + WithPwm ,
360
371
PINS : Pins < TIM , P > ,
361
372
{
373
+ #[ inline]
362
374
pub fn enable ( & mut self , channel : Channel ) {
363
375
TIM :: enable_channel ( PINS :: check_used ( channel) as u8 , true )
364
376
}
365
377
378
+ #[ inline]
366
379
pub fn disable ( & mut self , channel : Channel ) {
367
380
TIM :: enable_channel ( PINS :: check_used ( channel) as u8 , false )
368
381
}
369
382
383
+ #[ inline]
370
384
pub fn set_polarity ( & mut self , channel : Channel , p : Polarity ) {
371
385
TIM :: set_channel_polarity ( PINS :: check_used ( channel) as u8 , p) ;
372
386
}
373
387
388
+ #[ inline]
374
389
pub fn get_duty ( & self , channel : Channel ) -> u16 {
375
390
TIM :: read_cc_value ( PINS :: check_used ( channel) as u8 ) as u16
376
391
}
377
392
393
+ #[ inline]
378
394
pub fn set_duty ( & mut self , channel : Channel , duty : u16 ) {
379
395
TIM :: set_cc_value ( PINS :: check_used ( channel) as u8 , duty as u32 )
380
396
}
@@ -402,6 +418,7 @@ where
402
418
self . tim . cnt_reset ( ) ;
403
419
}
404
420
421
+ #[ inline]
405
422
pub fn set_complementary_polarity ( & mut self , channel : Channel , p : Polarity ) {
406
423
TIM :: set_channel_polarity ( PINS :: check_complementary_used ( channel) as u8 , p) ;
407
424
}
@@ -412,13 +429,50 @@ where
412
429
TIM : Instance + WithPwm + Advanced ,
413
430
PINS : Pins < TIM , P > ,
414
431
{
432
+ #[ inline]
415
433
pub fn enable_complementary ( & mut self , channel : Channel ) {
416
434
TIM :: enable_nchannel ( PINS :: check_complementary_used ( channel) as u8 , true )
417
435
}
418
436
437
+ #[ inline]
419
438
pub fn disable_complementary ( & mut self , channel : Channel ) {
420
439
TIM :: enable_nchannel ( PINS :: check_complementary_used ( channel) as u8 , false )
421
440
}
441
+
442
+ /// Set number DTS ticks during that complementary pin is `dead`
443
+ #[ inline]
444
+ pub fn set_dead_time ( & mut self , dts_ticks : u16 ) {
445
+ let bits = pack_ceil_dead_time ( dts_ticks) ;
446
+ TIM :: set_dtg_value ( bits) ;
447
+ }
448
+
449
+ /// Set raw dead time (DTG) bits
450
+ #[ inline]
451
+ pub fn set_dead_time_bits ( & mut self , bits : u8 ) {
452
+ TIM :: set_dtg_value ( bits) ;
453
+ }
454
+
455
+ /// Return dead time for complementary pins in DTS ticks
456
+ #[ inline]
457
+ pub fn get_dead_time ( & self ) -> u16 {
458
+ unpack_dead_time ( TIM :: read_dtg_value ( ) )
459
+ }
460
+
461
+ /// Get raw dead time (DTG) bits
462
+ #[ inline]
463
+ pub fn get_dead_time_bits ( & self ) -> u8 {
464
+ TIM :: read_dtg_value ( )
465
+ }
466
+
467
+ #[ inline]
468
+ pub fn set_idle_state ( & mut self , channel : Channel , s : IdleState ) {
469
+ TIM :: idle_state ( PINS :: check_used ( channel) as u8 , false , s) ;
470
+ }
471
+
472
+ #[ inline]
473
+ pub fn set_complementary_idle_state ( & mut self , channel : Channel , s : IdleState ) {
474
+ TIM :: idle_state ( PINS :: check_complementary_used ( channel) as u8 , true , s) ;
475
+ }
422
476
}
423
477
424
478
pub struct Pwm < TIM , P , PINS , const FREQ : u32 >
@@ -517,30 +571,37 @@ where
517
571
TIM : Instance + WithPwm ,
518
572
PINS : Pins < TIM , P > ,
519
573
{
574
+ #[ inline]
520
575
pub fn enable ( & mut self , channel : Channel ) {
521
576
TIM :: enable_channel ( PINS :: check_used ( channel) as u8 , true )
522
577
}
523
578
579
+ #[ inline]
524
580
pub fn disable ( & mut self , channel : Channel ) {
525
581
TIM :: enable_channel ( PINS :: check_used ( channel) as u8 , false )
526
582
}
527
583
584
+ #[ inline]
528
585
pub fn set_polarity ( & mut self , channel : Channel , p : Polarity ) {
529
586
TIM :: set_channel_polarity ( PINS :: check_used ( channel) as u8 , p) ;
530
587
}
531
588
589
+ #[ inline]
532
590
pub fn get_duty ( & self , channel : Channel ) -> u16 {
533
591
TIM :: read_cc_value ( PINS :: check_used ( channel) as u8 ) as u16
534
592
}
535
593
594
+ #[ inline]
536
595
pub fn get_duty_time ( & self , channel : Channel ) -> TimerDurationU32 < FREQ > {
537
596
TimerDurationU32 :: from_ticks ( TIM :: read_cc_value ( PINS :: check_used ( channel) as u8 ) )
538
597
}
539
598
599
+ #[ inline]
540
600
pub fn set_duty ( & mut self , channel : Channel , duty : u16 ) {
541
601
TIM :: set_cc_value ( PINS :: check_used ( channel) as u8 , duty. into ( ) )
542
602
}
543
603
604
+ #[ inline]
544
605
pub fn set_duty_time ( & mut self , channel : Channel , duty : TimerDurationU32 < FREQ > ) {
545
606
TIM :: set_cc_value ( PINS :: check_used ( channel) as u8 , duty. ticks ( ) )
546
607
}
@@ -559,6 +620,7 @@ where
559
620
self . tim . cnt_reset ( ) ;
560
621
}
561
622
623
+ #[ inline]
562
624
pub fn set_complementary_polarity ( & mut self , channel : Channel , p : Polarity ) {
563
625
TIM :: set_channel_polarity ( PINS :: check_complementary_used ( channel) as u8 , p) ;
564
626
}
@@ -569,11 +631,70 @@ where
569
631
TIM : Instance + WithPwm + Advanced ,
570
632
PINS : Pins < TIM , P > ,
571
633
{
634
+ #[ inline]
572
635
pub fn enable_complementary ( & mut self , channel : Channel ) {
573
636
TIM :: enable_nchannel ( PINS :: check_complementary_used ( channel) as u8 , true )
574
637
}
575
638
639
+ #[ inline]
576
640
pub fn disable_complementary ( & mut self , channel : Channel ) {
577
641
TIM :: enable_nchannel ( PINS :: check_complementary_used ( channel) as u8 , false )
578
642
}
643
+
644
+ /// Set number DTS ticks during that complementary pin is `dead`
645
+ #[ inline]
646
+ pub fn set_dead_time ( & mut self , dts_ticks : u16 ) {
647
+ let bits = pack_ceil_dead_time ( dts_ticks) ;
648
+ TIM :: set_dtg_value ( bits) ;
649
+ }
650
+
651
+ /// Set raw dead time (DTG) bits
652
+ #[ inline]
653
+ pub fn set_dead_time_bits ( & mut self , bits : u8 ) {
654
+ TIM :: set_dtg_value ( bits) ;
655
+ }
656
+
657
+ /// Return dead time for complementary pins in DTS ticks
658
+ #[ inline]
659
+ pub fn get_dead_time ( & self ) -> u16 {
660
+ unpack_dead_time ( TIM :: read_dtg_value ( ) )
661
+ }
662
+
663
+ /// Get raw dead time (DTG) bits
664
+ #[ inline]
665
+ pub fn get_dead_time_bits ( & self ) -> u8 {
666
+ TIM :: read_dtg_value ( )
667
+ }
668
+
669
+ #[ inline]
670
+ pub fn set_idle_state ( & mut self , channel : Channel , s : IdleState ) {
671
+ TIM :: idle_state ( PINS :: check_used ( channel) as u8 , false , s) ;
672
+ }
673
+
674
+ #[ inline]
675
+ pub fn set_complementary_idle_state ( & mut self , channel : Channel , s : IdleState ) {
676
+ TIM :: idle_state ( PINS :: check_complementary_used ( channel) as u8 , true , s) ;
677
+ }
678
+ }
679
+
680
+ const fn pack_ceil_dead_time ( dts_ticks : u16 ) -> u8 {
681
+ match dts_ticks {
682
+ 0 ..=127 => dts_ticks as u8 ,
683
+ 128 ..=254 => ( ( ( ( dts_ticks + 1 ) >> 1 ) - 64 ) as u8 ) | 0b_1000_0000 ,
684
+ 255 ..=504 => ( ( ( ( dts_ticks + 7 ) >> 3 ) - 32 ) as u8 ) | 0b_1100_0000 ,
685
+ 505 ..=1008 => ( ( ( ( dts_ticks + 15 ) >> 4 ) - 32 ) as u8 ) | 0b_1110_0000 ,
686
+ 1009 .. => 0xff ,
687
+ }
688
+ }
689
+
690
+ const fn unpack_dead_time ( bits : u8 ) -> u16 {
691
+ if bits & 0b_1000_0000 == 0 {
692
+ bits as u16
693
+ } else if bits & 0b_0100_0000 == 0 {
694
+ ( ( ( bits & !0b_1000_0000 ) as u16 ) + 64 ) * 2
695
+ } else if bits & 0b_0010_0000 == 0 {
696
+ ( ( ( bits & !0b_1100_0000 ) as u16 ) + 32 ) * 8
697
+ } else {
698
+ ( ( ( bits & !0b_1110_0000 ) as u16 ) + 32 ) * 16
699
+ }
579
700
}
0 commit comments