Skip to content

Commit 49bd07c

Browse files
committed
More hrtim refactor
1 parent 1bff83d commit 49bd07c

File tree

13 files changed

+2422
-2191
lines changed

13 files changed

+2422
-2191
lines changed

src/hrtim/compare_register.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use core::marker::PhantomData;
2+
3+
use crate::stm32::{
4+
HRTIM_MASTER, HRTIM_TIMA, HRTIM_TIMB, HRTIM_TIMC, HRTIM_TIMD, HRTIM_TIME, HRTIM_TIMF,
5+
};
6+
7+
pub trait HrCompareRegister {
8+
fn get_duty(&self) -> u16;
9+
fn set_duty(&mut self, duty: u16);
10+
}
11+
12+
pub struct HrCr1<TIM, PSCL>(PhantomData<(TIM, PSCL)>);
13+
pub struct HrCr2<TIM, PSCL>(PhantomData<(TIM, PSCL)>);
14+
pub struct HrCr3<TIM, PSCL>(PhantomData<(TIM, PSCL)>);
15+
pub struct HrCr4<TIM, PSCL>(PhantomData<(TIM, PSCL)>);
16+
17+
macro_rules! hrtim_cr_helper {
18+
($TIMX:ident: $cr_type:ident: $cmpXYr:ident, $cmpYx:ident) => {
19+
impl<PSCL> HrCompareRegister for $cr_type<$TIMX, PSCL> {
20+
fn get_duty(&self) -> u16 {
21+
let tim = unsafe { &*$TIMX::ptr() };
22+
23+
tim.$cmpXYr.read().$cmpYx().bits()
24+
}
25+
fn set_duty(&mut self, duty: u16) {
26+
let tim = unsafe { &*$TIMX::ptr() };
27+
28+
tim.$cmpXYr.write(|w| unsafe { w.$cmpYx().bits(duty) });
29+
}
30+
}
31+
};
32+
}
33+
34+
macro_rules! hrtim_cr {
35+
($($TIMX:ident: [
36+
$cmpX1r:ident, $cmpX2r:ident, $cmpX3r:ident, $cmpX4r:ident,
37+
$cmp1x:ident, $cmp2x:ident, $cmp3x:ident, $cmp4x:ident
38+
],)+) => {$(
39+
hrtim_cr_helper!($TIMX: HrCr1: $cmpX1r, $cmp1x);
40+
hrtim_cr_helper!($TIMX: HrCr2: $cmpX2r, $cmp2x);
41+
hrtim_cr_helper!($TIMX: HrCr3: $cmpX3r, $cmp3x);
42+
hrtim_cr_helper!($TIMX: HrCr4: $cmpX4r, $cmp4x);
43+
)+};
44+
}
45+
46+
hrtim_cr! {
47+
HRTIM_MASTER: [mcmp1r, mcmp2r, mcmp3r, mcmp4r, mcmp1, mcmp2, mcmp3, mcmp4],
48+
49+
HRTIM_TIMA: [cmp1ar, cmp2ar, cmp3ar, cmp4ar, cmp1x, cmp2x, cmp3x, cmp4x],
50+
HRTIM_TIMB: [cmp1br, cmp2br, cmp3br, cmp4br, cmp1x, cmp2x, cmp3x, cmp4x],
51+
HRTIM_TIMC: [cmp1cr, cmp2cr, cmp3cr, cmp4cr, cmp1x, cmp2x, cmp3x, cmp4x],
52+
HRTIM_TIMD: [cmp1dr, cmp2dr, cmp3dr, cmp4dr, cmp1x, cmp2x, cmp3x, cmp4x],
53+
HRTIM_TIME: [cmp1er, cmp2er, cmp3er, cmp4er, cmp1x, cmp2x, cmp3x, cmp4x],
54+
HRTIM_TIMF: [cmp1fr, cmp2fr, cmp3fr, cmp4fr, cmp1x, cmp2x, cmp3x, cmp4x],
55+
}

src/pwm/hrtim/calibration.rs renamed to src/hrtim/control.rs

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1-
use crate::stm32::{HRTIM_COMMON, RCC};
1+
use core::marker::PhantomData;
22

3-
use super::HrControltExt;
3+
use crate::{
4+
hrtim::fault::{
5+
FaultInput1, FaultInput2, FaultInput3, FaultInput4, FaultInput5, FaultInput6, FltMonitor1,
6+
FltMonitor2, FltMonitor3, FltMonitor4, FltMonitor5, FltMonitor6, FltMonitorSys,
7+
},
8+
rcc::{Enable, Rcc, Reset},
9+
stm32::{HRTIM_COMMON, RCC},
10+
};
11+
12+
use super::fault::FaultInputs;
413

514
pub trait HrControltExt {
615
fn hr_control(self, _rcc: &mut Rcc) -> HrTimOngoingCalibration;
@@ -84,7 +93,7 @@ pub struct HrTimOngoingCalibration {
8493

8594
impl HrTimOngoingCalibration {
8695
/// SAFETY: Calibration needs to be done before calling this
87-
unsafe fn init(self) -> (HrPwmControl, FaultInputs) {
96+
unsafe fn init(self) {
8897
use Adc13Trigger as Ad13T;
8998
use Adc24Trigger as Ad24T;
9099

@@ -410,37 +419,18 @@ impl HrTimOngoingCalibration {
410419

411420
// TODO: Adc trigger 5-10
412421
}
413-
414-
(
415-
HrPwmControl {
416-
_x: PhantomData,
417-
fault_sys: FltMonitorSys { _x: PhantomData },
418-
fault_1: FltMonitor1 { _x: PhantomData },
419-
fault_2: FltMonitor2 { _x: PhantomData },
420-
fault_3: FltMonitor3 { _x: PhantomData },
421-
fault_4: FltMonitor4 { _x: PhantomData },
422-
fault_5: FltMonitor5 { _x: PhantomData },
423-
fault_6: FltMonitor6 { _x: PhantomData },
424-
},
425-
FaultInputs {
426-
fault_input1: FaultInput1 { _x: PhantomData },
427-
fault_input2: FaultInput2 { _x: PhantomData },
428-
fault_input3: FaultInput3 { _x: PhantomData },
429-
fault_input4: FaultInput4 { _x: PhantomData },
430-
fault_input5: FaultInput5 { _x: PhantomData },
431-
fault_input6: FaultInput6 { _x: PhantomData },
432-
},
433-
)
434422
}
435423

436-
pub fn wait_for_calibration(self) -> (HrPwmControl, FaultInputs) {
424+
pub fn wait_for_calibration(self) -> HrTimCalibrated {
437425
let common = unsafe { &*HRTIM_COMMON::ptr() };
438426
while common.isr.read().dllrdy().bit_is_clear() {
439427
// Wait until ready
440428
}
441429

442430
// Calibration is now done, it is safe to continue
443-
unsafe { self.init() }
431+
unsafe { self.init() };
432+
433+
HrTimCalibrated { _x: PhantomData }
444434
}
445435

446436
pub fn enable_adc_trigger1_source(mut self, trigger: Adc13Trigger) -> Self {
@@ -526,6 +516,38 @@ impl HrTimOngoingCalibration {
526516
// TODO: Adc trigger 5-10
527517
}
528518

519+
/// This object may be used for things that needs to be done before any timers have been started but after the calibration has been completed. Its existence is proof that no timers have started.
520+
///
521+
/// Once done with setup, use the `constrain` to get a `HrPwmControl` which can be used to start the timers.
522+
pub struct HrTimCalibrated {
523+
_x: PhantomData<()>,
524+
}
525+
526+
impl HrTimCalibrated {
527+
pub fn constrain(self) -> (HrPwmControl, FaultInputs) {
528+
(
529+
HrPwmControl {
530+
_x: PhantomData,
531+
fault_sys: FltMonitorSys { _x: PhantomData },
532+
fault_1: FltMonitor1 { _x: PhantomData },
533+
fault_2: FltMonitor2 { _x: PhantomData },
534+
fault_3: FltMonitor3 { _x: PhantomData },
535+
fault_4: FltMonitor4 { _x: PhantomData },
536+
fault_5: FltMonitor5 { _x: PhantomData },
537+
fault_6: FltMonitor6 { _x: PhantomData },
538+
},
539+
FaultInputs {
540+
fault_input1: FaultInput1 { _x: PhantomData },
541+
fault_input2: FaultInput2 { _x: PhantomData },
542+
fault_input3: FaultInput3 { _x: PhantomData },
543+
fault_input4: FaultInput4 { _x: PhantomData },
544+
fault_input5: FaultInput5 { _x: PhantomData },
545+
fault_input6: FaultInput6 { _x: PhantomData },
546+
},
547+
)
548+
}
549+
}
550+
529551
pub struct HrPwmControl {
530552
_x: PhantomData<()>,
531553

src/hrtim/deadtime.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#[derive(Copy, Clone, Debug)]
2+
pub struct DeadtimeConfig {
3+
/// Prescaler for both rising and falling deadtime
4+
pub(crate) prescaler: DeadtimePrescaler,
5+
6+
/// 9-bits
7+
pub(crate) deadtime_rising_value: u16,
8+
9+
/// Is deadtime negative
10+
pub(crate) deadtime_rising_sign: bool,
11+
12+
/// 9-bits
13+
pub(crate) deadtime_falling_value: u16,
14+
15+
/// Is deadtime negative
16+
pub(crate) deadtime_falling_sign: bool,
17+
}
18+
19+
impl DeadtimeConfig {
20+
/// See RM0440 Table 221 'Deadtime resolution and max absolute values'
21+
pub fn prescaler(mut self, value: DeadtimePrescaler) -> Self {
22+
self.prescaler = value;
23+
self
24+
}
25+
26+
/// Panic if value can not fit in 9 bits
27+
pub fn deadtime_rising_value(mut self, value: u16) -> Self {
28+
// 9 bits
29+
assert!(value < (1 << 9));
30+
31+
self.deadtime_rising_value = value;
32+
33+
self
34+
}
35+
36+
pub fn deadtime_rising_sign(mut self, is_negative: bool) -> Self {
37+
self.deadtime_rising_sign = is_negative;
38+
self
39+
}
40+
41+
/// Panic if value can not fit in 9 bits
42+
pub fn deadtime_falling_value(mut self, value: u16) -> Self {
43+
// 9 bits
44+
assert!(value < (1 << 9));
45+
46+
self.deadtime_falling_value = value;
47+
48+
self
49+
}
50+
51+
pub fn deadtime_falling_sign(mut self, is_negative: bool) -> Self {
52+
self.deadtime_falling_sign = is_negative;
53+
self
54+
}
55+
}
56+
57+
impl Default for DeadtimeConfig {
58+
fn default() -> Self {
59+
Self {
60+
prescaler: DeadtimePrescaler::Thrtim,
61+
deadtime_rising_value: 170, // about 1us when f_sys = 170MHz
62+
deadtime_rising_sign: false,
63+
deadtime_falling_value: 170, // about 1us when f_sys = 170MHz
64+
deadtime_falling_sign: false,
65+
}
66+
}
67+
}
68+
69+
#[derive(Copy, Clone, Debug)]
70+
pub enum DeadtimePrescaler {
71+
ThrtimDiv8 = 0b000,
72+
ThrtimDiv4 = 0b001,
73+
ThrtimDiv2 = 0b010,
74+
Thrtim = 0b011,
75+
ThrtimMul2 = 0b100,
76+
ThrtimMul4 = 0b101,
77+
ThrtimMul8 = 0b110,
78+
ThrtimMul16 = 0b111,
79+
}

0 commit comments

Comments
 (0)