@@ -57,6 +57,13 @@ pub enum Channel {
57
57
C4 = 3 ,
58
58
}
59
59
60
+ /// Enum for IO polarity
61
+ #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
62
+ pub enum Polarity {
63
+ ActiveHigh ,
64
+ ActiveLow ,
65
+ }
66
+
60
67
/// Interrupt events
61
68
#[ derive( Clone , Copy , PartialEq , Eq ) ]
62
69
#[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
@@ -232,7 +239,7 @@ pub type CCR4<T> = CCR<T, 3>;
232
239
pub struct DMAR < T > ( T ) ;
233
240
234
241
mod sealed {
235
- use super :: { Channel , Event , Ocm } ;
242
+ use super :: { Channel , Event , Ocm , Polarity } ;
236
243
pub trait General {
237
244
type Width : Into < u32 > + From < u16 > ;
238
245
fn max_auto_reload ( ) -> u32 ;
@@ -257,21 +264,30 @@ mod sealed {
257
264
fn cnt_reset ( & mut self ) ;
258
265
}
259
266
260
- pub trait WithPwm : General {
267
+ pub trait WithPwmCommon : General {
261
268
const CH_NUMBER : u8 ;
262
269
fn read_cc_value ( channel : u8 ) -> u32 ;
263
270
fn set_cc_value ( channel : u8 , value : u32 ) ;
271
+ fn enable_channel ( channel : u8 , b : bool ) ;
272
+ fn set_channel_polarity ( channel : u8 , p : Polarity ) ;
273
+ fn set_nchannel_polarity ( channel : u8 , p : Polarity ) ;
274
+ }
275
+
276
+ pub trait Advanced : WithPwmCommon {
277
+ fn enable_nchannel ( channel : u8 , b : bool ) ;
278
+ }
279
+
280
+ pub trait WithPwm : WithPwmCommon {
264
281
fn preload_output_channel_in_mode ( & mut self , channel : Channel , mode : Ocm ) ;
265
282
fn start_pwm ( & mut self ) ;
266
- fn enable_channel ( channel : u8 , b : bool ) ;
267
283
}
268
284
269
285
pub trait MasterTimer : General {
270
286
type Mms ;
271
287
fn master_mode ( & mut self , mode : Self :: Mms ) ;
272
288
}
273
289
}
274
- pub ( crate ) use sealed:: { General , MasterTimer , WithPwm } ;
290
+ pub ( crate ) use sealed:: { Advanced , General , MasterTimer , WithPwm , WithPwmCommon } ;
275
291
276
292
pub trait Instance :
277
293
crate :: Sealed + rcc:: Enable + rcc:: Reset + rcc:: BusTimerClock + General
@@ -283,7 +299,7 @@ macro_rules! hal {
283
299
$Timer: ident,
284
300
$bits: ty,
285
301
$( dmar: $memsize: ty, ) ?
286
- $( c: ( $cnum : ident $( , $aoe: ident) ?) , ) ?
302
+ $( c: ( $CNUM : ident, $cnum : literal $( , $aoe: ident) ?) , ) ?
287
303
$( m: $timbase: ident, ) ?
288
304
] , ) +) => {
289
305
$(
@@ -391,7 +407,66 @@ macro_rules! hal {
391
407
$( with_dmar!( $TIM, $memsize) ; ) ?
392
408
393
409
$(
394
- with_pwm!( $TIM: $cnum $( , $aoe) ?) ;
410
+ impl WithPwmCommon for $TIM {
411
+ const CH_NUMBER : u8 = $cnum;
412
+
413
+ #[ inline( always) ]
414
+ fn read_cc_value( c: u8 ) -> u32 {
415
+ let tim = unsafe { & * <$TIM>:: ptr( ) } ;
416
+ if c < Self :: CH_NUMBER {
417
+ tim. ccr[ c as usize ] . read( ) . bits( )
418
+ } else {
419
+ 0
420
+ }
421
+ }
422
+
423
+ #[ inline( always) ]
424
+ fn set_cc_value( c: u8 , value: u32 ) {
425
+ let tim = unsafe { & * <$TIM>:: ptr( ) } ;
426
+ if c < Self :: CH_NUMBER {
427
+ #[ allow( unused_unsafe) ]
428
+ tim. ccr[ c as usize ] . write( |w| unsafe { w. bits( value) } )
429
+ }
430
+ }
431
+
432
+ #[ inline( always) ]
433
+ fn enable_channel( c: u8 , b: bool ) {
434
+ let tim = unsafe { & * <$TIM>:: ptr( ) } ;
435
+ if c < Self :: CH_NUMBER {
436
+ unsafe { bb:: write( & tim. ccer, c* 4 , b) ; }
437
+ }
438
+ }
439
+
440
+ #[ inline( always) ]
441
+ fn set_channel_polarity( c: u8 , p: Polarity ) {
442
+ let tim = unsafe { & * <$TIM>:: ptr( ) } ;
443
+ if c < Self :: CH_NUMBER {
444
+ unsafe { bb:: write( & tim. ccer, c* 4 + 1 , p == Polarity :: ActiveLow ) ; }
445
+ }
446
+ }
447
+
448
+ #[ inline( always) ]
449
+ fn set_nchannel_polarity( c: u8 , p: Polarity ) {
450
+ let tim = unsafe { & * <$TIM>:: ptr( ) } ;
451
+ if c < Self :: CH_NUMBER {
452
+ unsafe { bb:: write( & tim. ccer, c* 4 + 3 , p == Polarity :: ActiveLow ) ; }
453
+ }
454
+ }
455
+ }
456
+
457
+ $(
458
+ impl Advanced for $TIM {
459
+ fn enable_nchannel( c: u8 , b: bool ) {
460
+ let $aoe = ( ) ;
461
+ let tim = unsafe { & * <$TIM>:: ptr( ) } ;
462
+ if c < Self :: CH_NUMBER {
463
+ unsafe { bb:: write( & tim. ccer, c* 4 + 2 , b) ; }
464
+ }
465
+ }
466
+ }
467
+ ) ?
468
+
469
+ with_pwm!( $TIM: $CNUM $( , $aoe) ?) ;
395
470
unsafe impl <const C : u8 > PeriAddress for CCR <$TIM, C > {
396
471
#[ inline( always) ]
397
472
fn address( & self ) -> u32 {
@@ -428,21 +503,6 @@ macro_rules! with_dmar {
428
503
macro_rules! with_pwm {
429
504
( $TIM: ty: CH1 ) => {
430
505
impl WithPwm for $TIM {
431
- const CH_NUMBER : u8 = 1 ;
432
-
433
- #[ inline( always) ]
434
- fn read_cc_value( channel: u8 ) -> u32 {
435
- let tim = unsafe { & * <$TIM>:: ptr( ) } ;
436
- tim. ccr[ channel as usize ] . read( ) . bits( )
437
- }
438
-
439
- #[ inline( always) ]
440
- fn set_cc_value( channel: u8 , value: u32 ) {
441
- let tim = unsafe { & * <$TIM>:: ptr( ) } ;
442
- #[ allow( unused_unsafe) ]
443
- tim. ccr[ channel as usize ] . write( |w| unsafe { w. bits( value) } )
444
- }
445
-
446
506
#[ inline( always) ]
447
507
fn preload_output_channel_in_mode( & mut self , channel: Channel , mode: Ocm ) {
448
508
match channel {
@@ -458,33 +518,10 @@ macro_rules! with_pwm {
458
518
fn start_pwm( & mut self ) {
459
519
self . cr1. modify( |_, w| w. cen( ) . set_bit( ) ) ;
460
520
}
461
-
462
- #[ inline( always) ]
463
- fn enable_channel( c: u8 , b: bool ) {
464
- let tim = unsafe { & * <$TIM>:: ptr( ) } ;
465
- if c < Self :: CH_NUMBER {
466
- unsafe { bb:: write( & tim. ccer, c* 4 , b) ; }
467
- }
468
- }
469
521
}
470
522
} ;
471
523
( $TIM: ty: CH2 ) => {
472
524
impl WithPwm for $TIM {
473
- const CH_NUMBER : u8 = 2 ;
474
-
475
- #[ inline( always) ]
476
- fn read_cc_value( channel: u8 ) -> u32 {
477
- let tim = unsafe { & * <$TIM>:: ptr( ) } ;
478
- tim. ccr[ channel as usize ] . read( ) . bits( )
479
- }
480
-
481
- #[ inline( always) ]
482
- fn set_cc_value( channel: u8 , value: u32 ) {
483
- let tim = unsafe { & * <$TIM>:: ptr( ) } ;
484
- #[ allow( unused_unsafe) ]
485
- tim. ccr[ channel as usize ] . write( |w| unsafe { w. bits( value) } ) ;
486
- }
487
-
488
525
#[ inline( always) ]
489
526
fn preload_output_channel_in_mode( & mut self , channel: Channel , mode: Ocm ) {
490
527
match channel {
@@ -504,33 +541,10 @@ macro_rules! with_pwm {
504
541
fn start_pwm( & mut self ) {
505
542
self . cr1. modify( |_, w| w. cen( ) . set_bit( ) ) ;
506
543
}
507
-
508
- #[ inline( always) ]
509
- fn enable_channel( c: u8 , b: bool ) {
510
- let tim = unsafe { & * <$TIM>:: ptr( ) } ;
511
- if c < Self :: CH_NUMBER {
512
- unsafe { bb:: write( & tim. ccer, c* 4 , b) ; }
513
- }
514
- }
515
544
}
516
545
} ;
517
546
( $TIM: ty: CH4 $( , $aoe: ident) ?) => {
518
547
impl WithPwm for $TIM {
519
- const CH_NUMBER : u8 = 4 ;
520
-
521
- #[ inline( always) ]
522
- fn read_cc_value( channel: u8 ) -> u32 {
523
- let tim = unsafe { & * <$TIM>:: ptr( ) } ;
524
- tim. ccr[ channel as usize ] . read( ) . bits( )
525
- }
526
-
527
- #[ inline( always) ]
528
- fn set_cc_value( channel: u8 , value: u32 ) {
529
- let tim = unsafe { & * <$TIM>:: ptr( ) } ;
530
- #[ allow( unused_unsafe) ]
531
- tim. ccr[ channel as usize ] . write( |w| unsafe { w. bits( value) } )
532
- }
533
-
534
548
#[ inline( always) ]
535
549
fn preload_output_channel_in_mode( & mut self , channel: Channel , mode: Ocm ) {
536
550
match channel {
@@ -558,16 +572,8 @@ macro_rules! with_pwm {
558
572
$( let $aoe = self . bdtr. modify( |_, w| w. aoe( ) . set_bit( ) ) ; ) ?
559
573
self . cr1. modify( |_, w| w. cen( ) . set_bit( ) ) ;
560
574
}
561
-
562
- #[ inline( always) ]
563
- fn enable_channel( c: u8 , b: bool ) {
564
- let tim = unsafe { & * <$TIM>:: ptr( ) } ;
565
- if c < Self :: CH_NUMBER {
566
- unsafe { bb:: write( & tim. ccer, c* 4 , b) ; }
567
- }
568
- }
569
575
}
570
- }
576
+ } ;
571
577
}
572
578
573
579
impl < TIM : Instance > Timer < TIM > {
@@ -723,26 +729,26 @@ pub(crate) const fn compute_arr_presc(freq: u32, clock: u32) -> (u16, u32) {
723
729
724
730
// All F4xx parts have these timers.
725
731
hal ! (
726
- pac:: TIM9 : [ Timer9 , u16 , c: ( CH2 ) , ] ,
727
- pac:: TIM11 : [ Timer11 , u16 , c: ( CH1 ) , ] ,
732
+ pac:: TIM9 : [ Timer9 , u16 , c: ( CH2 , 2 ) , ] ,
733
+ pac:: TIM11 : [ Timer11 , u16 , c: ( CH1 , 1 ) , ] ,
728
734
) ;
729
735
730
736
// All parts except for F410 add these timers.
731
737
#[ cfg( not( feature = "stm32f410" ) ) ]
732
738
hal ! (
733
- pac:: TIM1 : [ Timer1 , u16 , dmar: u32 , c: ( CH4 , _aoe) , m: tim1, ] ,
734
- pac:: TIM5 : [ Timer5 , u32 , dmar: u16 , c: ( CH4 ) , m: tim5, ] ,
735
- pac:: TIM2 : [ Timer2 , u32 , dmar: u16 , c: ( CH4 ) , m: tim2, ] ,
736
- pac:: TIM3 : [ Timer3 , u16 , dmar: u16 , c: ( CH4 ) , m: tim3, ] ,
737
- pac:: TIM4 : [ Timer4 , u16 , dmar: u16 , c: ( CH4 ) , m: tim3, ] ,
738
- pac:: TIM10 : [ Timer10 , u16 , c: ( CH1 ) , ] ,
739
+ pac:: TIM1 : [ Timer1 , u16 , dmar: u32 , c: ( CH4 , 4 , _aoe) , m: tim1, ] ,
740
+ pac:: TIM5 : [ Timer5 , u32 , dmar: u16 , c: ( CH4 , 4 ) , m: tim5, ] ,
741
+ pac:: TIM2 : [ Timer2 , u32 , dmar: u16 , c: ( CH4 , 4 ) , m: tim2, ] ,
742
+ pac:: TIM3 : [ Timer3 , u16 , dmar: u16 , c: ( CH4 , 4 ) , m: tim3, ] ,
743
+ pac:: TIM4 : [ Timer4 , u16 , dmar: u16 , c: ( CH4 , 4 ) , m: tim3, ] ,
744
+ pac:: TIM10 : [ Timer10 , u16 , c: ( CH1 , 1 ) , ] ,
739
745
) ;
740
746
741
747
// TIM5 on F410 is 16-bit
742
748
#[ cfg( feature = "stm32f410" ) ]
743
749
hal ! (
744
- pac:: TIM1 : [ Timer1 , u16 , dmar: u16 , c: ( CH4 , _aoe) , m: tim1, ] ,
745
- pac:: TIM5 : [ Timer5 , u16 , dmar: u16 , c: ( CH4 ) , m: tim5, ] ,
750
+ pac:: TIM1 : [ Timer1 , u16 , dmar: u16 , c: ( CH4 , 4 , _aoe) , m: tim1, ] ,
751
+ pac:: TIM5 : [ Timer5 , u16 , dmar: u16 , c: ( CH4 , 4 ) , m: tim5, ] ,
746
752
) ;
747
753
748
754
// All parts except F401 and F411.
@@ -753,8 +759,8 @@ hal!(pac::TIM6: [Timer6, u16, m: tim6,],);
753
759
#[ cfg( not( any( feature = "stm32f401" , feature = "stm32f410" , feature = "stm32f411" , ) ) ) ]
754
760
hal ! (
755
761
pac:: TIM7 : [ Timer7 , u16 , m: tim7, ] ,
756
- pac:: TIM8 : [ Timer8 , u16 , dmar: u32 , c: ( CH4 , _aoe) , m: tim8, ] ,
757
- pac:: TIM12 : [ Timer12 , u16 , c: ( CH2 ) , ] ,
758
- pac:: TIM13 : [ Timer13 , u16 , c: ( CH1 ) , ] ,
759
- pac:: TIM14 : [ Timer14 , u16 , c: ( CH1 ) , ] ,
762
+ pac:: TIM8 : [ Timer8 , u16 , dmar: u32 , c: ( CH4 , 4 , _aoe) , m: tim8, ] ,
763
+ pac:: TIM12 : [ Timer12 , u16 , c: ( CH2 , 2 ) , ] ,
764
+ pac:: TIM13 : [ Timer13 , u16 , c: ( CH1 , 1 ) , ] ,
765
+ pac:: TIM14 : [ Timer14 , u16 , c: ( CH1 , 1 ) , ] ,
760
766
) ;
0 commit comments