Skip to content

Commit 41a5da8

Browse files
committed
Multiple hrtim changes
* Add output polarity * Add counting direction * Do not start timer directly when created, helps when used as AD trigger * Add methods for starting/stopping timers * Add some adc triggers
1 parent 97fdbe1 commit 41a5da8

File tree

1 file changed

+110
-30
lines changed

1 file changed

+110
-30
lines changed

src/pwm/hrtim.rs

Lines changed: 110 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use crate::stm32::{
1515
};
1616

1717
use super::{
18-
ActiveHigh, Alignment, ComplementaryImpossible, FaultMonitor, Pins, Pwm, PwmPinEnable,
19-
TimerType,
18+
ActiveHigh, Alignment, ComplementaryImpossible, FaultMonitor, Pins, Polarity, Pwm,
19+
PwmPinEnable, TimerType,
2020
};
2121
use crate::rcc::{Enable, GetBusFreq, Rcc, Reset};
2222
use crate::stm32::RCC;
@@ -267,6 +267,8 @@ pub struct HrPwmBuilder<TIM, PSCL, PS, OUT> {
267267
repetition_counter: u8,
268268
//deadtime: NanoSecond,
269269
enable_repetition_interrupt: bool,
270+
out1_polarity: Polarity,
271+
out2_polarity: Polarity,
270272
}
271273

272274
pub enum PreloadSource {
@@ -310,6 +312,15 @@ pub trait HrTimer<TIM, PSCL> {
310312
///
311313
/// NOTE: This will affect the maximum duty usable for `HrCompareRegister::set_duty`
312314
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);
313324
}
314325

315326
pub trait HrOutput {
@@ -796,10 +807,10 @@ macro_rules! hrtim_finalize_body {
796807
});
797808

798809
$(
799-
tim.$timXcr2.modify(|_r, w| unsafe {
810+
tim.$timXcr2.modify(|_r, w|
800811
// Set counting direction
801812
w.udm().bit($this.counting_direction == HrCountingDirection::UpDown)
802-
});
813+
);
803814

804815
// Only available for timers with outputs(not HRTIM_MASTER)
805816
let _ = tim.$outXr;
@@ -829,10 +840,14 @@ macro_rules! hrtim_finalize_body {
829840
// ... and lock configuration
830841
tim.$fltXr.modify(|_r, w| w.fltlck().set_bit());
831842

832-
// Set actions on fault for both outputs
833843
tim.$outXr.modify(|_r, w| w
844+
// Set actions on fault for both outputs
834845
.fault1().bits($this.fault1_bits)
835846
.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)
836851
);
837852
})*
838853

@@ -846,8 +861,8 @@ macro_rules! hrtim_finalize_body {
846861
tim.$dier.modify(|_r, w| w.$repie().bit($this.enable_repetition_interrupt));
847862

848863
// 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() });
851866

852867
unsafe {
853868
MaybeUninit::uninit().assume_init()
@@ -923,6 +938,8 @@ macro_rules! hrtim_common_methods {
923938
repetition_counter,
924939

925940
enable_repetition_interrupt,
941+
out1_polarity,
942+
out2_polarity,
926943
} = self;
927944

928945
let period = match count {
@@ -949,6 +966,8 @@ macro_rules! hrtim_common_methods {
949966
repetition_counter,
950967
//deadtime: 0.nanos(),
951968
enable_repetition_interrupt,
969+
out1_polarity,
970+
out2_polarity,
952971
}
953972
}
954973

@@ -1047,6 +1066,8 @@ macro_rules! hrtim_hal {
10471066
repetition_counter: 0,
10481067
//deadtime: 0.nanos(),
10491068
enable_repetition_interrupt: false,
1069+
out1_polarity: Polarity::ActiveHigh,
1070+
out2_polarity: Polarity::ActiveHigh,
10501071
}
10511072
}
10521073
}
@@ -1082,6 +1103,18 @@ macro_rules! hrtim_hal {
10821103
self
10831104
}
10841105

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+
10851118
/// Enable or disable Push-Pull mode
10861119
///
10871120
/// Enabling Push-Pull mode will make output 1 and 2
@@ -1106,6 +1139,15 @@ macro_rules! hrtim_hal {
11061139
self
11071140
}
11081141

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+
11091151
/// Set interleaved or half modes
11101152
///
11111153
/// NOTE: Check [`InterleavedMode`] for more info about special cases
@@ -1157,6 +1199,8 @@ macro_rules! hrtim_hal_master {
11571199
repetition_counter: 0,
11581200
//deadtime: 0.nanos(),
11591201
enable_repetition_interrupt: false,
1202+
out1_polarity: Polarity::ActiveHigh,
1203+
out2_polarity: Polarity::ActiveHigh,
11601204
}
11611205
}
11621206
}
@@ -1220,7 +1264,7 @@ macro_rules! hrtim_pin_hal {
12201264

12211265
/// Set duty cycle
12221266
///
1223-
/// NOTE: Please observe limits:
1267+
/// NOTE: Please observe limits(RM0440 "Period and compare registers min and max values"):
12241268
/// | Prescaler | Min duty | Max duty |
12251269
/// |-----------|----------|----------|
12261270
/// | 1 | 0x0060 | 0xFFDF |
@@ -1232,7 +1276,12 @@ macro_rules! hrtim_pin_hal {
12321276
/// | 64 | 0x0003 | 0xFFFD |
12331277
/// | 128 | 0x0003 | 0xFFFD |
12341278
///
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
12361285
fn set_duty(&mut self, duty: Self::Duty) {
12371286
let tim = unsafe { &*$TIMX::ptr() };
12381287

@@ -1378,7 +1427,7 @@ macro_rules! hrtim_cr {
13781427
}
13791428

13801429
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],)*)+) => {$(
13821431
impl<PSCL> HrTimer<$TIMX, PSCL> for HrTim<$TIMX, PSCL> {
13831432
fn get_period(&self) -> u16 {
13841433
let tim = unsafe { &*$TIMX::ptr() };
@@ -1390,6 +1439,32 @@ macro_rules! hrtim_timer {
13901439

13911440
tim.$perXr.write(|w| unsafe { w.$perx().bits(period as u16) });
13921441
}
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+
}
13931468
}
13941469

13951470
impl<PSCL> HrTim<$TIMX, PSCL> {
@@ -1444,14 +1519,14 @@ macro_rules! hrtim_timer {
14441519
}
14451520

14461521
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,
14481523

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],
14551530
}
14561531

14571532
hrtim_cr! {
@@ -1498,7 +1573,7 @@ hrtim_pin_hal! {
14981573
HRTIM_TIMF: (CH2, perfr, cmp3fr, cmp3x, cmp3, tf2oen, tf2odis),
14991574
}
15001575

1501-
pub unsafe trait HrtimPrescaler {
1576+
pub unsafe trait HrtimPrescaler: Default {
15021577
const BITS: u8;
15031578
const VALUE: u8;
15041579

@@ -2325,8 +2400,9 @@ pub enum Adc13Trigger {
23252400
/// bit 29 ADCxTEC3 - Trigger on HRTIM_TIME compare match for compare register 3
23262401
TimECmp3 = 1 << 29,
23272402

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+
23302406
/// bit 27 ADCxTDPER - Trigger on HRTIM_TIMD period
23312407
TimDPeriod = 1 << 27,
23322408

@@ -2351,8 +2427,9 @@ pub enum Adc13Trigger {
23512427
/// bit 20 ADCxTFC4 - Trigger on HRTIM_TIMF compare match for compare register 4
23522428
TimFCmp4 = 1 << 20,
23532429

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+
23562433
/// bit 18 ADCxTBPER - Trigger on HRTIM_TIMB period
23572434
TimBPeriod = 1 << 18,
23582435

@@ -2365,8 +2442,9 @@ pub enum Adc13Trigger {
23652442
/// bit 15 ADCxTFC3 - Trigger on HRTIM_TIMF compare match for compare register 3
23662443
TimFCmp3 = 1 << 15,
23672444

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+
23702448
/// bit 13 ADCxTAPER - Trigger on HRTIM_TIMA period
23712449
TimAPeriod = 1 << 13,
23722450

@@ -2445,8 +2523,9 @@ pub enum AdcTriggerPostscaler {
24452523
}
24462524

24472525
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+
24502529
/// bit 30 ADCxTEC4 - Trigger on HRTIM_TIME compare match for compare register 4
24512530
TimECmp4 = 1 << 30,
24522531

@@ -2456,8 +2535,9 @@ pub enum Adc24Trigger {
24562535
/// bit 28 ADCxTEC2 - Trigger on HRTIM_TIME compare match for compare register 2
24572536
TimECmp2 = 1 << 28,
24582537

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+
24612541
/// bit 26 ADCxTDPER - Trigger on HRTIM_TIMD period
24622542
TimDPeriod = 1 << 26,
24632543

@@ -2470,8 +2550,8 @@ pub enum Adc24Trigger {
24702550
/// bit 23 ADCxTDC2 - Trigger on HRTIM_TIMD compare match for compare register 2
24712551
TimDCmp2 = 1 << 23,
24722552

2473-
/// bit 22 ADCxTCRST
2474-
// _ = 1 << 22,
2553+
/// bit 22 ADCxTCRST - Trigger on HRTIM_TIMC reset or counter roll-over
2554+
TimCRst = 1 << 22,
24752555

24762556
/// bit 21 ADCxTCPER - Trigger on HRTIM_TIMC period
24772557
TimCPeriod = 1 << 21,

0 commit comments

Comments
 (0)