Skip to content

Commit 6eea233

Browse files
committed
Update hrtim examples. TODO: Make sure timers are started
1 parent 9e28e28 commit 6eea233

File tree

14 files changed

+330
-242
lines changed

14 files changed

+330
-242
lines changed

examples/hrtim-adc-trigger.rs renamed to examples/hrtim/adc-trigger.rs

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,51 @@ use crate::hal::{
1010
AdcClaim, ClockSource, Temperature, Vref,
1111
},
1212
delay::SYSTDelayExt,
13-
dma::{config::DmaConfig, stream::DMAExt, TransferExt},
14-
gpio::GpioExt,
15-
rcc::{Config, RccExt},
13+
dma::{self, config::DmaConfig, stream::DMAExt, TransferExt},
14+
gpio::{gpioa::PA8, gpioa::PA9, Alternate, GpioExt, AF13},
15+
hrtim::control::HrControltExt,
16+
hrtim::output::HrOutput,
17+
hrtim::HrPwmAdvExt,
18+
hrtim::{control::Adc13Trigger, Pscl4},
19+
pwr::PwrExt,
20+
rcc::{self, RccExt},
1621
stm32::Peripherals,
1722
};
1823
use stm32g4xx_hal as hal;
1924

20-
use log::info;
25+
use defmt::info;
2126

22-
#[macro_use]
23-
mod utils;
27+
use defmt_rtt as _; // global logger
28+
use panic_probe as _;
2429

2530
#[entry]
2631
fn main() -> ! {
27-
utils::logger::init();
28-
2932
info!("start");
3033

3134
let dp = Peripherals::take().unwrap();
3235
let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
3336

37+
// Set system frequency to 16MHz * 15/1/2 = 120MHz
38+
// This would lead to HrTim running at 120MHz * 32 = 3.84...
3439
info!("rcc");
35-
let rcc = dp.RCC.constrain();
36-
let mut rcc = rcc.freeze(Config::hsi());
40+
let pwr = dp.PWR.constrain().freeze();
41+
let mut rcc = dp.RCC.freeze(
42+
rcc::Config::pll().pll_cfg(rcc::PllConfig {
43+
mux: rcc::PLLSrc::HSI,
44+
n: rcc::PllNMul::MUL_15,
45+
m: rcc::PllMDiv::DIV_1,
46+
r: Some(rcc::PllRDiv::DIV_2),
47+
48+
..Default::default()
49+
}),
50+
pwr,
51+
);
52+
53+
let mut delay = cp.SYST.delay(&rcc.clocks);
3754

38-
let streams = dp.DMA1.split(&rcc);
55+
let dma::stream::StreamsTuple(dma1ch1, ..) = dp.DMA1.split(&rcc);
3956
let config = DmaConfig::default()
40-
.transfer_complete_interrupt(false)
57+
.transfer_complete_interrupt(true)
4158
.circular_buffer(true)
4259
.memory_increment(true);
4360

@@ -46,7 +63,6 @@ fn main() -> ! {
4663
let pa0 = gpioa.pa0.into_analog();
4764

4865
info!("Setup Adc1");
49-
let mut delay = cp.SYST.delay(&rcc.clocks);
5066
let mut adc = dp
5167
.ADC1
5268
.claim(ClockSource::SystemClock, &rcc, &mut delay, true);
@@ -63,40 +79,50 @@ fn main() -> ! {
6379

6480
info!("Setup DMA");
6581
let first_buffer = cortex_m::singleton!(: [u16; 10] = [0; 10]).unwrap();
66-
let mut transfer = streams.0.into_circ_peripheral_to_memory_transfer(
82+
83+
let mut transfer = dma1ch1.into_circ_peripheral_to_memory_transfer(
6784
adc.enable_dma(AdcDma::Continuous),
6885
&mut first_buffer[..],
6986
config,
7087
);
7188

7289
transfer.start(|adc| adc.start_conversion());
7390

91+
let pin_a: PA8<Alternate<AF13>> = gpioa.pa8.into_alternate();
92+
let pin_b: PA9<Alternate<AF13>> = gpioa.pa9.into_alternate();
93+
94+
// ...with a prescaler of 4 this gives us a HrTimer with a tick rate of 960MHz
95+
// With max the max period set, this would be 960MHz/2^16 ~= 15kHz...
96+
let prescaler = Pscl4;
97+
7498
// . .
7599
// . 50% .
76100
// ------ ------
77101
//out1 | | | |
78102
// | | | |
79103
// -------- ---------- --------
80-
let (mut fault_control, _) = dp
104+
let (hr_control, ..) = dp
81105
.HRTIM_COMMON
82106
.hr_control(&mut rcc)
83-
.set_adc_trigger1(Adc13Trigger::)
107+
.enable_adc_trigger1_source(Adc13Trigger::TimACmp3)
108+
.enable_adc_trigger1_source(Adc13Trigger::TimACmp4)
84109
.wait_for_calibration();
85-
let (mut timer, (mut cr1, _cr2, _cr3, _cr4), (mut out1, mut out2)) = dp
110+
let mut hr_control = hr_control.constrain();
111+
let (timer, (cr1, _cr2, _cr3, _cr4), (mut out1, mut out2)) = dp
86112
.HRTIM_TIMA
87113
.pwm_advanced((pin_a, pin_b), &mut rcc)
88114
.prescaler(prescaler)
89115
.period(0xFFFF)
90116
// alternated every period with one being
91117
// inactive and the other getting to output its wave form
92118
// as normal
93-
.finalize(&mut fault_control);
119+
.finalize(&mut hr_control);
94120

95-
out1.enable_rst_event(EventSource::Cr1); // Set low on compare match with cr1
96-
out2.enable_rst_event(EventSource::Cr1);
121+
out1.enable_rst_event(&cr1); // Set low on compare match with cr1
122+
out2.enable_rst_event(&cr1);
97123

98-
out1.enable_set_event(EventSource::Period); // Set high at new period
99-
out2.enable_set_event(EventSource::Period);
124+
out1.enable_set_event(&timer); // Set high at new period
125+
out2.enable_set_event(&timer);
100126

101127
out1.enable();
102128
out2.enable();

examples/hrtim_eev_comp.rs renamed to examples/hrtim/eev-comp.rs

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,23 @@
33
#![no_std]
44

55
use cortex_m_rt::entry;
6-
use fugit::ExtU32;
76
use hal::comparator;
8-
use hal::comparator::{Hysteresis, ComparatorSplit, ComparatorExt};
7+
use hal::comparator::{ComparatorExt, ComparatorSplit, Hysteresis};
98
use hal::dac::{self, DacExt, DacOut};
109
use hal::gpio::gpioa::PA8;
1110
use hal::gpio::Alternate;
11+
use hal::gpio::SignalEdge;
1212
use hal::gpio::AF13;
1313
use hal::hrtim::compare_register::HrCompareRegister;
14-
use hal::hrtim::event::EventSource;
1514
use hal::hrtim::external_event::{self, ToExternalEventSource};
16-
use hal::hrtim::timer_eev_cfg::EevCfgs;
1715
use hal::hrtim::timer::HrTimer;
16+
use hal::hrtim::timer_eev_cfg::{EevCfg, EevCfgs};
1817
use hal::hrtim::HrPwmAdvExt;
1918
use hal::hrtim::Pscl4;
2019
use hal::hrtim::{control::HrControltExt, output::HrOutput};
2120
use hal::prelude::*;
2221
use hal::pwm;
23-
use hal::pwr::{self, PwrExt};
22+
use hal::pwr::PwrExt;
2423
use hal::rcc;
2524
use hal::stm32;
2625
use stm32g4xx_hal as hal;
@@ -34,13 +33,7 @@ fn main() -> ! {
3433
let cp = stm32::CorePeripherals::take().expect("cannot take core");
3534
// Set system frequency to 16MHz * 75/4/2 = 150MHz
3635
// 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();
36+
let pwr = dp.PWR.constrain().freeze();
4437

4538
let mut rcc = dp.RCC.freeze(
4639
rcc::Config::pll().pll_cfg(rcc::PllConfig {
@@ -53,34 +46,34 @@ fn main() -> ! {
5346
pwr,
5447
);
5548

49+
let exti = dp.EXTI;
50+
5651
let mut delay = cp.SYST.delay(&rcc.clocks);
5752

5853
let gpioa = dp.GPIOA.split(&mut rcc);
59-
let gpiob = dp.GPIOB.split(&mut rcc);
6054

6155
let input = gpioa.pa1.into_analog();
6256
let pin_a: PA8<Alternate<AF13>> = gpioa.pa8.into_alternate();
6357

64-
let dac3ch1 = dp.DAC3.constrain(dac::Dac3IntSig1, &mut rcc);
65-
let mut dac = dac3ch1.enable();
58+
let dac1ch1 = dp.DAC1.constrain(dac::Dac1IntSig1, &mut rcc);
59+
let mut dac = dac1ch1.calibrate_buffer(&mut delay).enable();
6660

6761
// Use dac to define the fault threshold
6862
// 2^12 / 2 = 2^11 for about half of VCC
69-
let fault_limit = 60;
70-
dac.set_value(fault_limit);
63+
let limit = 1 << 11;
64+
dac.set_value(limit);
7165

7266
let (comp1, ..) = dp.COMP.split(&mut rcc);
7367

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();
68+
let comp1 = comp1.comparator(
69+
&input,
70+
&dac,
71+
comparator::Config::default().hysteresis(Hysteresis::None),
72+
//.output_inverted(),
73+
&rcc.clocks,
74+
);
75+
comp1.listen(SignalEdge::Rising, &exti);
76+
let comp1 = comp1.enable().lock();
8477

8578
let (mut hr_control, _flt_inputs, eev_inputs) =
8679
dp.HRTIM_COMMON.hr_control(&mut rcc).wait_for_calibration();
@@ -117,19 +110,25 @@ fn main() -> ! {
117110
.HRTIM_TIMA
118111
.pwm_advanced(pin_a, &mut rcc)
119112
.prescaler(prescaler)
120-
.eev_cfg(EevCfgs::default())
113+
.eev_cfg(EevCfgs::default().eev4(EevCfg::default()))
121114
.period(0xFFFF)
122115
.finalize(&mut hr_control);
123116

124117
out1.enable_rst_event(&cr1); // Set low on compare match with cr1
125118
out1.enable_rst_event(eev_input4);
126119
out1.enable_set_event(&timer); // Set high at new period
127120
cr1.set_duty(timer.get_period() / 3);
128-
//unsafe {((HRTIM_COMMON::ptr() as *mut u8).offset(0x14) as *mut u32).write_volatile(1); }
121+
129122
out1.enable();
130123
timer.start(&mut hr_control);
131124

132125
defmt::info!("Started");
133126

134-
loop {}
127+
loop {
128+
defmt::info!(
129+
"Comp: {}, pending: {}",
130+
comp1.output(),
131+
comp1.is_pending(&exti)
132+
);
133+
}
135134
}

examples/hrtim_eev.rs renamed to examples/hrtim/eev.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@
33
#![no_std]
44

55
use cortex_m_rt::entry;
6-
use fugit::ExtU32;
76
use hal::gpio::gpioa::PA8;
87
use hal::gpio::Alternate;
98
use hal::gpio::AF13;
109
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;
10+
use hal::hrtim::external_event;
11+
use hal::hrtim::external_event::ToExternalEventSource;
1412
use hal::hrtim::timer::HrTimer;
13+
use hal::hrtim::timer_eev_cfg::EevCfgs;
1514
use hal::hrtim::HrPwmAdvExt;
1615
use hal::hrtim::Pscl4;
1716
use hal::hrtim::{control::HrControltExt, output::HrOutput};
1817
use hal::prelude::*;
1918
use hal::pwm;
20-
use hal::pwr::{self, PwrExt};
19+
use hal::pwr::PwrExt;
2120
use hal::rcc;
2221
use hal::stm32;
2322
use stm32g4xx_hal as hal;
@@ -29,16 +28,9 @@ use panic_probe as _;
2928
#[entry]
3029
fn main() -> ! {
3130
let dp = stm32::Peripherals::take().expect("cannot take peripherals");
32-
let cp = stm32::CorePeripherals::take().expect("cannot take core");
3331
// Set system frequency to 16MHz * 75/4/2 = 150MHz
3432
// This would lead to HrTim running at 150MHz * 32 = 4.8GHz...
35-
let pwr = dp
36-
.PWR
37-
.constrain()
38-
.vos(pwr::VoltageScale::Range1 {
39-
enable_boost: false,
40-
})
41-
.freeze();
33+
let pwr = dp.PWR.constrain().freeze();
4234

4335
let mut rcc = dp.RCC.freeze(
4436
rcc::Config::pll().pll_cfg(rcc::PllConfig {
@@ -51,8 +43,6 @@ fn main() -> ! {
5143
pwr,
5244
);
5345

54-
let mut delay = cp.SYST.delay(&rcc.clocks);
55-
5646
let gpioa = dp.GPIOA.split(&mut rcc);
5747
let gpiob = dp.GPIOB.split(&mut rcc);
5848

@@ -61,7 +51,7 @@ fn main() -> ! {
6151

6252
let eev_input3 = eev_inputs
6353
.eev_input3
64-
.bind_pin(gpiob.pb7.into_pull_down_input())
54+
.bind(gpiob.pb7.into_pull_down_input())
6555
.edge_or_polarity(external_event::EdgeOrPolarity::Polarity(
6656
pwm::Polarity::ActiveHigh,
6757
))
@@ -101,7 +91,7 @@ fn main() -> ! {
10191
out1.enable_rst_event(eev_input3);
10292
out1.enable_set_event(&timer); // Set high at new period
10393
cr1.set_duty(timer.get_period() / 3);
104-
//unsafe {((HRTIM_COMMON::ptr() as *mut u8).offset(0x14) as *mut u32).write_volatile(1); }
94+
10595
out1.enable();
10696
timer.start(&mut hr_control);
10797

0 commit comments

Comments
 (0)