@@ -15,8 +15,8 @@ use crate::stm32::{
15
15
} ;
16
16
17
17
use super :: {
18
- ActiveHigh , Alignment , ComplementaryImpossible , FaultMonitor , Pins , Pwm , PwmPinEnable ,
19
- TimerType ,
18
+ ActiveHigh , Alignment , ComplementaryImpossible , FaultMonitor , Pins , Polarity , Pwm ,
19
+ PwmPinEnable , TimerType ,
20
20
} ;
21
21
use crate :: rcc:: { Enable , GetBusFreq , Rcc , Reset } ;
22
22
use crate :: stm32:: RCC ;
@@ -267,6 +267,8 @@ pub struct HrPwmBuilder<TIM, PSCL, PS, OUT> {
267
267
repetition_counter : u8 ,
268
268
//deadtime: NanoSecond,
269
269
enable_repetition_interrupt : bool ,
270
+ out1_polarity : Polarity ,
271
+ out2_polarity : Polarity ,
270
272
}
271
273
272
274
pub enum PreloadSource {
@@ -310,6 +312,15 @@ pub trait HrTimer<TIM, PSCL> {
310
312
///
311
313
/// NOTE: This will affect the maximum duty usable for `HrCompareRegister::set_duty`
312
314
fn set_period ( & mut self , period : u16 ) ;
315
+
316
+ /// Start timer
317
+ fn start ( & mut self , _hr_control : & mut HrPwmControl ) ;
318
+
319
+ /// Stop timer
320
+ fn stop ( & mut self , _hr_control : & mut HrPwmControl ) ;
321
+
322
+ /// Stop timer and reset counter
323
+ fn stop_and_reset ( & mut self , _hr_control : & mut HrPwmControl ) ;
313
324
}
314
325
315
326
pub trait HrOutput {
@@ -796,10 +807,10 @@ macro_rules! hrtim_finalize_body {
796
807
} ) ;
797
808
798
809
$(
799
- tim. $timXcr2. modify( |_r, w| unsafe {
810
+ tim. $timXcr2. modify( |_r, w|
800
811
// Set counting direction
801
812
w. udm( ) . bit( $this. counting_direction == HrCountingDirection :: UpDown )
802
- } ) ;
813
+ ) ;
803
814
804
815
// Only available for timers with outputs(not HRTIM_MASTER)
805
816
let _ = tim. $outXr;
@@ -829,10 +840,14 @@ macro_rules! hrtim_finalize_body {
829
840
// ... and lock configuration
830
841
tim. $fltXr. modify( |_r, w| w. fltlck( ) . set_bit( ) ) ;
831
842
832
- // Set actions on fault for both outputs
833
843
tim. $outXr. modify( |_r, w| w
844
+ // Set actions on fault for both outputs
834
845
. fault1( ) . bits( $this. fault1_bits)
835
846
. fault2( ) . bits( $this. fault2_bits)
847
+
848
+ // Set output polarity for both outputs
849
+ . pol1( ) . bit( $this. out1_polarity == Polarity :: ActiveLow )
850
+ . pol2( ) . bit( $this. out2_polarity == Polarity :: ActiveLow )
836
851
) ;
837
852
} ) *
838
853
@@ -846,8 +861,8 @@ macro_rules! hrtim_finalize_body {
846
861
tim. $dier. modify( |_r, w| w. $repie( ) . bit( $this. enable_repetition_interrupt) ) ;
847
862
848
863
// Start timer
849
- let master = unsafe { & * HRTIM_MASTER :: ptr( ) } ;
850
- master. mcr. modify( |_r, w| { w. $tXcen( ) . set_bit( ) } ) ;
864
+ // let master = unsafe { &*HRTIM_MASTER::ptr() };
865
+ // master.mcr.modify(|_r, w| { w.$tXcen().set_bit() });
851
866
852
867
unsafe {
853
868
MaybeUninit :: uninit( ) . assume_init( )
@@ -923,6 +938,8 @@ macro_rules! hrtim_common_methods {
923
938
repetition_counter,
924
939
925
940
enable_repetition_interrupt,
941
+ out1_polarity,
942
+ out2_polarity,
926
943
} = self ;
927
944
928
945
let period = match count {
@@ -949,6 +966,8 @@ macro_rules! hrtim_common_methods {
949
966
repetition_counter,
950
967
//deadtime: 0.nanos(),
951
968
enable_repetition_interrupt,
969
+ out1_polarity,
970
+ out2_polarity,
952
971
}
953
972
}
954
973
@@ -1047,6 +1066,8 @@ macro_rules! hrtim_hal {
1047
1066
repetition_counter: 0 ,
1048
1067
//deadtime: 0.nanos(),
1049
1068
enable_repetition_interrupt: false ,
1069
+ out1_polarity: Polarity :: ActiveHigh ,
1070
+ out2_polarity: Polarity :: ActiveHigh ,
1050
1071
}
1051
1072
}
1052
1073
}
@@ -1082,6 +1103,18 @@ macro_rules! hrtim_hal {
1082
1103
self
1083
1104
}
1084
1105
1106
+ pub fn out1_polarity( mut self , polarity: Polarity ) -> Self {
1107
+ self . out1_polarity = polarity;
1108
+
1109
+ self
1110
+ }
1111
+
1112
+ pub fn out2_polarity( mut self , polarity: Polarity ) -> Self {
1113
+ self . out2_polarity = polarity;
1114
+
1115
+ self
1116
+ }
1117
+
1085
1118
/// Enable or disable Push-Pull mode
1086
1119
///
1087
1120
/// Enabling Push-Pull mode will make output 1 and 2
@@ -1106,6 +1139,15 @@ macro_rules! hrtim_hal {
1106
1139
self
1107
1140
}
1108
1141
1142
+ /// Set counting direction
1143
+ ///
1144
+ /// See [`HrCountingDirection`]
1145
+ pub fn counting_direction( mut self , counting_direction: HrCountingDirection ) -> Self {
1146
+ self . counting_direction = counting_direction;
1147
+
1148
+ self
1149
+ }
1150
+
1109
1151
/// Set interleaved or half modes
1110
1152
///
1111
1153
/// NOTE: Check [`InterleavedMode`] for more info about special cases
@@ -1157,6 +1199,8 @@ macro_rules! hrtim_hal_master {
1157
1199
repetition_counter: 0 ,
1158
1200
//deadtime: 0.nanos(),
1159
1201
enable_repetition_interrupt: false ,
1202
+ out1_polarity: Polarity :: ActiveHigh ,
1203
+ out2_polarity: Polarity :: ActiveHigh ,
1160
1204
}
1161
1205
}
1162
1206
}
@@ -1220,7 +1264,7 @@ macro_rules! hrtim_pin_hal {
1220
1264
1221
1265
/// Set duty cycle
1222
1266
///
1223
- /// NOTE: Please observe limits:
1267
+ /// NOTE: Please observe limits(RM0440 "Period and compare registers min and max values") :
1224
1268
/// | Prescaler | Min duty | Max duty |
1225
1269
/// |-----------|----------|----------|
1226
1270
/// | 1 | 0x0060 | 0xFFDF |
@@ -1232,7 +1276,12 @@ macro_rules! hrtim_pin_hal {
1232
1276
/// | 64 | 0x0003 | 0xFFFD |
1233
1277
/// | 128 | 0x0003 | 0xFFFD |
1234
1278
///
1235
- /// Also, writing 0 as duty is only valid for CR1 and CR3
1279
+ /// Also, writing 0 as duty is only valid for CR1 and CR3 during a set of
1280
+ /// specific conditions(see RM0440 "Null duty cycle exception case"):
1281
+ /// – the output SET event is generated by the PERIOD event
1282
+ /// – the output RESET if generated by the compare 1 (respectively compare 3) event
1283
+ /// – the compare 1 (compare 3) event is active within the timer unit itself, and not used
1284
+ /// for other timing units
1236
1285
fn set_duty( & mut self , duty: Self :: Duty ) {
1237
1286
let tim = unsafe { & * $TIMX:: ptr( ) } ;
1238
1287
@@ -1378,7 +1427,7 @@ macro_rules! hrtim_cr {
1378
1427
}
1379
1428
1380
1429
macro_rules! hrtim_timer {
1381
- ( $( $TIMX: ident: $perXr: ident, $perx: ident, $rep: ident, $repx: ident, $dier: ident, $repie: ident, $icr: ident, $repc: ident, $( [ $rstXr: ident, $TimerXResetEventSource: ident] , ) * ) +) => { $(
1430
+ ( $( $TIMX: ident: $cntXr : ident , $cntx : ident , $ perXr: ident , $tXcen : ident, $perx: ident, $rep: ident, $repx: ident, $dier: ident, $repie: ident, $icr: ident, $repc: ident, $( [ $rstXr: ident, $TimerXResetEventSource: ident] , ) * ) +) => { $(
1382
1431
impl <PSCL > HrTimer <$TIMX, PSCL > for HrTim <$TIMX, PSCL > {
1383
1432
fn get_period( & self ) -> u16 {
1384
1433
let tim = unsafe { & * $TIMX:: ptr( ) } ;
@@ -1390,6 +1439,32 @@ macro_rules! hrtim_timer {
1390
1439
1391
1440
tim. $perXr. write( |w| unsafe { w. $perx( ) . bits( period as u16 ) } ) ;
1392
1441
}
1442
+
1443
+ /// Start timer
1444
+ fn start( & mut self , _hr_control: & mut HrPwmControl ) {
1445
+ // Start timer
1446
+
1447
+ // SAFETY: Since we hold _hr_control there is no risk for a race condition
1448
+ let master = unsafe { & * HRTIM_MASTER :: ptr( ) } ;
1449
+ master. mcr. modify( |_r, w| { w. $tXcen( ) . set_bit( ) } ) ;
1450
+ }
1451
+
1452
+ /// Stop timer
1453
+ fn stop( & mut self , _hr_control: & mut HrPwmControl ) {
1454
+ // Stop counter
1455
+ // SAFETY: Since we hold _hr_control there is no risk for a race condition
1456
+ let master = unsafe { & * HRTIM_MASTER :: ptr( ) } ;
1457
+ master. mcr. modify( |_r, w| { w. $tXcen( ) . set_bit( ) } ) ;
1458
+ }
1459
+
1460
+ /// Stop timer and reset counter
1461
+ fn stop_and_reset( & mut self , _hr_control: & mut HrPwmControl ) {
1462
+ self . stop( _hr_control) ;
1463
+
1464
+ // Reset counter
1465
+ let tim = unsafe { & * $TIMX:: ptr( ) } ;
1466
+ unsafe { tim. $cntXr. write( |w| w. $cntx( ) . bits( 0 ) ) ; }
1467
+ }
1393
1468
}
1394
1469
1395
1470
impl <PSCL > HrTim <$TIMX, PSCL > {
@@ -1444,14 +1519,14 @@ macro_rules! hrtim_timer {
1444
1519
}
1445
1520
1446
1521
hrtim_timer ! {
1447
- HRTIM_MASTER : mper, mper, mrep, mrep, mdier, mrepie, micr, mrepc,
1522
+ HRTIM_MASTER : mcntr , mcnt , mper, mcen , mper, mrep, mrep, mdier, mrepie, micr, mrepc,
1448
1523
1449
- HRTIM_TIMA : perar, perx, repar, repx, timadier, repie, timaicr, repc, [ rstar, TimerAResetEventSource ] ,
1450
- HRTIM_TIMB : perbr, perx, repbr, repx, timbdier, repie, timbicr, repc, [ rstbr, TimerBResetEventSource ] ,
1451
- HRTIM_TIMC : percr, perx, repcr, repx, timcdier, repie, timcicr, repc, [ rstcr, TimerCResetEventSource ] ,
1452
- HRTIM_TIMD : perdr, perx, repdr, repx, timddier, repie, timdicr, repc, [ rstdr, TimerDResetEventSource ] ,
1453
- HRTIM_TIME : perer, perx, reper, repx, timedier, repie, timeicr, repc, [ rster, TimerEResetEventSource ] ,
1454
- HRTIM_TIMF : perfr, perx, repfr, repx, timfdier, repie, timficr, repc, [ rstfr, TimerFResetEventSource ] ,
1524
+ HRTIM_TIMA : cntar , cntx , perar, tacen , perx, repar, repx, timadier, repie, timaicr, repc, [ rstar, TimerAResetEventSource ] ,
1525
+ HRTIM_TIMB : cntr , cntx , perbr, tbcen , perx, repbr, repx, timbdier, repie, timbicr, repc, [ rstbr, TimerBResetEventSource ] ,
1526
+ HRTIM_TIMC : cntcr , cntx , percr, tccen , perx, repcr, repx, timcdier, repie, timcicr, repc, [ rstcr, TimerCResetEventSource ] ,
1527
+ HRTIM_TIMD : cntdr , cntx , perdr, tdcen , perx, repdr, repx, timddier, repie, timdicr, repc, [ rstdr, TimerDResetEventSource ] ,
1528
+ HRTIM_TIME : cnter , cntx , perer, tecen , perx, reper, repx, timedier, repie, timeicr, repc, [ rster, TimerEResetEventSource ] ,
1529
+ HRTIM_TIMF : cntfr , cntx , perfr, tfcen , perx, repfr, repx, timfdier, repie, timficr, repc, [ rstfr, TimerFResetEventSource ] ,
1455
1530
}
1456
1531
1457
1532
hrtim_cr ! {
@@ -1498,7 +1573,7 @@ hrtim_pin_hal! {
1498
1573
HRTIM_TIMF : ( CH2 , perfr, cmp3fr, cmp3x, cmp3, tf2oen, tf2odis) ,
1499
1574
}
1500
1575
1501
- pub unsafe trait HrtimPrescaler {
1576
+ pub unsafe trait HrtimPrescaler : Default {
1502
1577
const BITS : u8 ;
1503
1578
const VALUE : u8 ;
1504
1579
@@ -2325,8 +2400,9 @@ pub enum Adc13Trigger {
2325
2400
/// bit 29 ADCxTEC3 - Trigger on HRTIM_TIME compare match for compare register 3
2326
2401
TimECmp3 = 1 << 29 ,
2327
2402
2328
- // /// bit 28 ADCxTFRST
2329
- // _ = 1 << 28,
2403
+ /// bit 28 ADCxTFRST - Trigger on HRTIM_TIMF reset or counter roll-over
2404
+ TimFRst = 1 << 28 ,
2405
+
2330
2406
/// bit 27 ADCxTDPER - Trigger on HRTIM_TIMD period
2331
2407
TimDPeriod = 1 << 27 ,
2332
2408
@@ -2351,8 +2427,9 @@ pub enum Adc13Trigger {
2351
2427
/// bit 20 ADCxTFC4 - Trigger on HRTIM_TIMF compare match for compare register 4
2352
2428
TimFCmp4 = 1 << 20 ,
2353
2429
2354
- // /// bit 19 ADCxTBRST
2355
- // _ = 1 << 19,
2430
+ /// bit 19 ADCxTBRST - Trigger on HRTIM_TIMB reset or counter roll-over
2431
+ TimBRst = 1 << 19 ,
2432
+
2356
2433
/// bit 18 ADCxTBPER - Trigger on HRTIM_TIMB period
2357
2434
TimBPeriod = 1 << 18 ,
2358
2435
@@ -2365,8 +2442,9 @@ pub enum Adc13Trigger {
2365
2442
/// bit 15 ADCxTFC3 - Trigger on HRTIM_TIMF compare match for compare register 3
2366
2443
TimFCmp3 = 1 << 15 ,
2367
2444
2368
- // /// bit 14 ADCxTARST
2369
- // _ = 1 << 14,
2445
+ /// bit 14 ADCxTARST - Trigger on HRTIM_TIMA reset or counter roll-over
2446
+ TimARst = 1 << 14 ,
2447
+
2370
2448
/// bit 13 ADCxTAPER - Trigger on HRTIM_TIMA period
2371
2449
TimAPeriod = 1 << 13 ,
2372
2450
@@ -2445,8 +2523,9 @@ pub enum AdcTriggerPostscaler {
2445
2523
}
2446
2524
2447
2525
pub enum Adc24Trigger {
2448
- // /// bit 31 ADCxTERST
2449
- // _ = 1 << 31,
2526
+ /// bit 31 ADCxTERST - Trigger on HRTIM_TIME reset or counter roll-over
2527
+ TimERst = 1 << 31 ,
2528
+
2450
2529
/// bit 30 ADCxTEC4 - Trigger on HRTIM_TIME compare match for compare register 4
2451
2530
TimECmp4 = 1 << 30 ,
2452
2531
@@ -2456,8 +2535,9 @@ pub enum Adc24Trigger {
2456
2535
/// bit 28 ADCxTEC2 - Trigger on HRTIM_TIME compare match for compare register 2
2457
2536
TimECmp2 = 1 << 28 ,
2458
2537
2459
- // /// bit 27 ADCxTDRST
2460
- // _ = 1 << 27,
2538
+ /// bit 27 ADCxTDRST - Trigger on HRTIM_TIMD reset or counter roll-over
2539
+ TimDRst = 1 << 27 ,
2540
+
2461
2541
/// bit 26 ADCxTDPER - Trigger on HRTIM_TIMD period
2462
2542
TimDPeriod = 1 << 26 ,
2463
2543
@@ -2470,8 +2550,8 @@ pub enum Adc24Trigger {
2470
2550
/// bit 23 ADCxTDC2 - Trigger on HRTIM_TIMD compare match for compare register 2
2471
2551
TimDCmp2 = 1 << 23 ,
2472
2552
2473
- /// bit 22 ADCxTCRST
2474
- // _ = 1 << 22,
2553
+ /// bit 22 ADCxTCRST - Trigger on HRTIM_TIMC reset or counter roll-over
2554
+ TimCRst = 1 << 22 ,
2475
2555
2476
2556
/// bit 21 ADCxTCPER - Trigger on HRTIM_TIMC period
2477
2557
TimCPeriod = 1 << 21 ,
0 commit comments