Skip to content

Commit 9e28e28

Browse files
committed
more eev hrtim
1 parent 851c67f commit 9e28e28

File tree

8 files changed

+461
-150
lines changed

8 files changed

+461
-150
lines changed

examples/hrtim_eev.rs

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ use fugit::ExtU32;
77
use hal::gpio::gpioa::PA8;
88
use hal::gpio::Alternate;
99
use hal::gpio::AF13;
10+
use hal::hrtim::compare_register::HrCompareRegister;
11+
use hal::hrtim::event::EventSource;
12+
use hal::hrtim::external_event::{self, ToExternalEventSource};
13+
use hal::hrtim::timer_eev_cfg::EevCfgs;
14+
use hal::hrtim::timer::HrTimer;
15+
use hal::hrtim::HrPwmAdvExt;
16+
use hal::hrtim::Pscl4;
17+
use hal::hrtim::{control::HrControltExt, output::HrOutput};
1018
use hal::prelude::*;
11-
use hal::pwm::hrtim::EventSource;
12-
use hal::pwm::hrtim::FaultAction;
13-
use hal::pwm::hrtim::HrCompareRegister;
14-
use hal::pwm::hrtim::HrPwmAdvExt;
15-
use hal::pwm::hrtim::HrTimer;
16-
use hal::pwm::hrtim::Pscl4;
17-
use hal::pwm::hrtim::{HrControltExt, HrOutput};
18-
use hal::pwm::FaultMonitor;
19+
use hal::pwm;
20+
use hal::pwr::{self, PwrExt};
1921
use hal::rcc;
2022
use hal::stm32;
2123
use stm32g4xx_hal as hal;
@@ -30,26 +32,42 @@ fn main() -> ! {
3032
let cp = stm32::CorePeripherals::take().expect("cannot take core");
3133
// Set system frequency to 16MHz * 75/4/2 = 150MHz
3234
// This would lead to HrTim running at 150MHz * 32 = 4.8GHz...
33-
let mut rcc = dp.RCC.freeze(rcc::Config::pll().pll_cfg(rcc::PllConfig {
34-
mux: rcc::PLLSrc::HSI,
35-
n: rcc::PllNMul::MUL_75,
36-
m: rcc::PllMDiv::DIV_4,
37-
r: Some(rcc::PllRDiv::DIV_2),
38-
..Default::default()
39-
}));
35+
let pwr = dp
36+
.PWR
37+
.constrain()
38+
.vos(pwr::VoltageScale::Range1 {
39+
enable_boost: false,
40+
})
41+
.freeze();
42+
43+
let mut rcc = dp.RCC.freeze(
44+
rcc::Config::pll().pll_cfg(rcc::PllConfig {
45+
mux: rcc::PLLSrc::HSI,
46+
n: rcc::PllNMul::MUL_75,
47+
m: rcc::PllMDiv::DIV_4,
48+
r: Some(rcc::PllRDiv::DIV_2),
49+
..Default::default()
50+
}),
51+
pwr,
52+
);
4053

4154
let mut delay = cp.SYST.delay(&rcc.clocks);
4255

4356
let gpioa = dp.GPIOA.split(&mut rcc);
4457
let gpiob = dp.GPIOB.split(&mut rcc);
45-
let (mut fault_control, flt_inputs, eev_inputs) =
58+
59+
let (mut hr_control, _flt_inputs, eev_inputs) =
4660
dp.HRTIM_COMMON.hr_control(&mut rcc).wait_for_calibration();
4761

4862
let eev_input3 = eev_inputs
4963
.eev_input3
5064
.bind_pin(gpiob.pb7.into_pull_down_input())
51-
.polarity(hal::pwm::Polarity::ActiveHigh)
52-
.finalize(&mut fault_control);
65+
.edge_or_polarity(external_event::EdgeOrPolarity::Polarity(
66+
pwm::Polarity::ActiveHigh,
67+
))
68+
.finalize(&mut hr_control);
69+
70+
let mut hr_control = hr_control.constrain();
5371

5472
// ...with a prescaler of 4 this gives us a HrTimer with a tick rate of 1.2GHz
5573
// With max the max period set, this would be 1.2GHz/2^16 ~= 18kHz...
@@ -71,31 +89,23 @@ fn main() -> ! {
7189
// ------------------------- ------------------------------------------
7290
// . . * . . .
7391
// . . * . . .
74-
let (timer, (mut cr1, _cr2, _cr3, _cr4), mut out1) = dp
92+
let (mut timer, (mut cr1, _cr2, _cr3, _cr4), mut out1) = dp
7593
.HRTIM_TIMA
7694
.pwm_advanced(pin_a, &mut rcc)
7795
.prescaler(prescaler)
96+
.eev_cfg(EevCfgs::default())
7897
.period(0xFFFF)
79-
.finalize(&mut fault_control);
98+
.finalize(&mut hr_control);
8099

81-
out1.enable_rst_event(EventSource::Cr1); // Set low on compare match with cr1
82-
out1.enable_rst_event(&eev_input3);
83-
out1.enable_set_event(EventSource::Period); // Set high at new period
100+
out1.enable_rst_event(&cr1); // Set low on compare match with cr1
101+
out1.enable_rst_event(eev_input3);
102+
out1.enable_set_event(&timer); // Set high at new period
84103
cr1.set_duty(timer.get_period() / 3);
85104
//unsafe {((HRTIM_COMMON::ptr() as *mut u8).offset(0x14) as *mut u32).write_volatile(1); }
86105
out1.enable();
106+
timer.start(&mut hr_control);
87107

88108
defmt::info!("Started");
89109

90-
loop {
91-
for _ in 0..5 {
92-
delay.delay(500_u32.millis());
93-
defmt::info!("State: {}", out1.get_state());
94-
}
95-
if fault_control.fault_3.is_fault_active() {
96-
fault_control.fault_3.clear_fault(); // Clear fault every 5s
97-
out1.enable();
98-
defmt::info!("failt cleared, and output reenabled");
99-
}
100-
}
110+
loop {}
101111
}

examples/hrtim_eev_comp.rs

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
//This example puts the timer in PWM mode using the specified pin with a frequency of 100Hz and a duty cycle of 50%.
2+
#![no_main]
3+
#![no_std]
4+
5+
use cortex_m_rt::entry;
6+
use fugit::ExtU32;
7+
use hal::comparator;
8+
use hal::comparator::{Hysteresis, ComparatorSplit, ComparatorExt};
9+
use hal::dac::{self, DacExt, DacOut};
10+
use hal::gpio::gpioa::PA8;
11+
use hal::gpio::Alternate;
12+
use hal::gpio::AF13;
13+
use hal::hrtim::compare_register::HrCompareRegister;
14+
use hal::hrtim::event::EventSource;
15+
use hal::hrtim::external_event::{self, ToExternalEventSource};
16+
use hal::hrtim::timer_eev_cfg::EevCfgs;
17+
use hal::hrtim::timer::HrTimer;
18+
use hal::hrtim::HrPwmAdvExt;
19+
use hal::hrtim::Pscl4;
20+
use hal::hrtim::{control::HrControltExt, output::HrOutput};
21+
use hal::prelude::*;
22+
use hal::pwm;
23+
use hal::pwr::{self, PwrExt};
24+
use hal::rcc;
25+
use hal::stm32;
26+
use stm32g4xx_hal as hal;
27+
28+
use defmt_rtt as _; // global logger
29+
use panic_probe as _;
30+
31+
#[entry]
32+
fn main() -> ! {
33+
let dp = stm32::Peripherals::take().expect("cannot take peripherals");
34+
let cp = stm32::CorePeripherals::take().expect("cannot take core");
35+
// Set system frequency to 16MHz * 75/4/2 = 150MHz
36+
// This would lead to HrTim running at 150MHz * 32 = 4.8GHz...
37+
let pwr = dp
38+
.PWR
39+
.constrain()
40+
.vos(pwr::VoltageScale::Range1 {
41+
enable_boost: false,
42+
})
43+
.freeze();
44+
45+
let mut rcc = dp.RCC.freeze(
46+
rcc::Config::pll().pll_cfg(rcc::PllConfig {
47+
mux: rcc::PLLSrc::HSI,
48+
n: rcc::PllNMul::MUL_75,
49+
m: rcc::PllMDiv::DIV_4,
50+
r: Some(rcc::PllRDiv::DIV_2),
51+
..Default::default()
52+
}),
53+
pwr,
54+
);
55+
56+
let mut delay = cp.SYST.delay(&rcc.clocks);
57+
58+
let gpioa = dp.GPIOA.split(&mut rcc);
59+
let gpiob = dp.GPIOB.split(&mut rcc);
60+
61+
let input = gpioa.pa1.into_analog();
62+
let pin_a: PA8<Alternate<AF13>> = gpioa.pa8.into_alternate();
63+
64+
let dac3ch1 = dp.DAC3.constrain(dac::Dac3IntSig1, &mut rcc);
65+
let mut dac = dac3ch1.enable();
66+
67+
// Use dac to define the fault threshold
68+
// 2^12 / 2 = 2^11 for about half of VCC
69+
let fault_limit = 60;
70+
dac.set_value(fault_limit);
71+
72+
let (comp1, ..) = dp.COMP.split(&mut rcc);
73+
74+
let comp1 = comp1
75+
.comparator(
76+
&input,
77+
&dac,
78+
comparator::Config::default()
79+
.hysteresis(Hysteresis::None),
80+
//.output_inverted(),
81+
&rcc.clocks,
82+
)
83+
.enable();
84+
85+
let (mut hr_control, _flt_inputs, eev_inputs) =
86+
dp.HRTIM_COMMON.hr_control(&mut rcc).wait_for_calibration();
87+
88+
let eev_input4 = eev_inputs
89+
.eev_input4
90+
.bind(&comp1)
91+
.edge_or_polarity(external_event::EdgeOrPolarity::Polarity(
92+
pwm::Polarity::ActiveHigh,
93+
))
94+
.finalize(&mut hr_control);
95+
96+
let mut hr_control = hr_control.constrain();
97+
98+
// ...with a prescaler of 4 this gives us a HrTimer with a tick rate of 1.2GHz
99+
// With max the max period set, this would be 1.2GHz/2^16 ~= 18kHz...
100+
let prescaler = Pscl4;
101+
102+
// . . * .
103+
// . 33% . * . . .
104+
// .-----. .--* . .-----. .-----
105+
//out1 | | | | . | | |
106+
// | | | * . | | |
107+
// ------ ----------- ------------------------------ -----------
108+
// . . * . . .
109+
// . . * . . .
110+
// . . *-------------* . .
111+
//eev . . | .| . .
112+
// . . | .| . .
113+
// ------------------------- .--------------------------------------
114+
// . . * . . .
115+
// . . * . . .
116+
let (mut timer, (mut cr1, _cr2, _cr3, _cr4), mut out1) = dp
117+
.HRTIM_TIMA
118+
.pwm_advanced(pin_a, &mut rcc)
119+
.prescaler(prescaler)
120+
.eev_cfg(EevCfgs::default())
121+
.period(0xFFFF)
122+
.finalize(&mut hr_control);
123+
124+
out1.enable_rst_event(&cr1); // Set low on compare match with cr1
125+
out1.enable_rst_event(eev_input4);
126+
out1.enable_set_event(&timer); // Set high at new period
127+
cr1.set_duty(timer.get_period() / 3);
128+
//unsafe {((HRTIM_COMMON::ptr() as *mut u8).offset(0x14) as *mut u32).write_volatile(1); }
129+
out1.enable();
130+
timer.start(&mut hr_control);
131+
132+
defmt::info!("Started");
133+
134+
loop {}
135+
}

src/hrtim/control.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ use core::marker::PhantomData;
22

33
use crate::{
44
hrtim::fault::{
5-
FaultInput1, FaultInput2, FaultInput3, FaultInput4, FaultInput5, FaultInput6, FltMonitor1,
6-
FltMonitor2, FltMonitor3, FltMonitor4, FltMonitor5, FltMonitor6, FltMonitorSys,
5+
FltMonitor1, FltMonitor2, FltMonitor3, FltMonitor4, FltMonitor5, FltMonitor6, FltMonitorSys,
76
},
87
rcc::{Enable, Rcc, Reset},
98
stm32::{HRTIM_COMMON, RCC},
109
};
1110

12-
use super::{fault::FaultInputs, external_event::EevInputs};
11+
use super::{external_event::EevInputs, fault::FaultInputs};
1312

1413
pub trait HrControltExt {
1514
fn hr_control(self, _rcc: &mut Rcc) -> HrTimOngoingCalibration;

src/hrtim/event.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,18 @@ pub enum EventSource<PSCL, DST> {
159159
/// On complete master period
160160
MasterPeriod { _x: PhantomData<(PSCL, DST)> },
161161

162-
ExternalEvent(ExternalEventSource), // This is fine
162+
ExternalEvent(EevFastOrNormal), // This is fine
163163

164164
NeighborTimer {
165165
n: NeighborTimerEventSource<PSCL, DST>,
166166
},
167167
}
168168

169+
pub enum EevFastOrNormal {
170+
Fast(ExternalEventSource<true>),
171+
Normal(ExternalEventSource<false>),
172+
}
173+
169174
/// Compare events from neighbor timers
170175
///
171176
/// See RM0440 Table 218. 'Events mapping across timer A to F'

0 commit comments

Comments
 (0)