Skip to content

Commit 1eb2b31

Browse files
committed
Add non working examples
1 parent b0dd45f commit 1eb2b31

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed

examples/stm32f3/hrtim.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#![no_std]
2+
#![no_main]
3+
4+
use cortex_m_rt::entry;
5+
use panic_probe as _;
6+
use stm32_hrtim::{
7+
compare_register::HrCompareRegister, control::HrControltExt, output::HrOutput, timer::HrTimer,
8+
HrParts, HrPwmAdvExt, Pscl4,
9+
};
10+
use stm32g4xx_hal::{
11+
delay::{DelayExt, SYSTDelayExt},
12+
gpio::GpioExt,
13+
pwr::PwrExt,
14+
rcc::{self, RccExt},
15+
stm32::{CorePeripherals, Peripherals},
16+
time::ExtU32,
17+
};
18+
19+
#[entry]
20+
fn main() -> ! {
21+
defmt::info!("Initializing...");
22+
23+
let dp = Peripherals::take().expect("cannot take peripherals");
24+
let cp = CorePeripherals::take().expect("cannot take core");
25+
// Set system frequency to 16MHz * 15/1/2 = 120MHz
26+
// This would lead to HrTim running at 120MHz * 32 = 3.84...
27+
let pwr = dp.PWR.constrain().freeze();
28+
let mut rcc = Input to hrtim needs to be 128MHz when using HSI, 128-144 with HSE
29+
30+
let mut delay = cp.SYST.delay(&rcc.clocks);
31+
32+
// ...with a prescaler of 4 this gives us a HrTimer with a tick rate of 960MHz
33+
// With max the max period set, this would be 960MHz/2^16 ~= 15kHz...
34+
let prescaler = Pscl4;
35+
36+
let gpioa = dp.GPIOA.split(&mut rcc);
37+
let pin_a = gpioa.pa8;
38+
let pin_b = gpioa.pa9;
39+
40+
// . . . .
41+
// . 30% . . .
42+
// ---- . .---- .
43+
//out1 | | . | | .
44+
// | | . | | .
45+
// -------- ---------------------------- --------------------
46+
// . .---- . .----
47+
//out2 . | | . | |
48+
// . | | . | |
49+
// ------------------------ ---------------------------- ----
50+
// . . . .
51+
// . . . .
52+
let (hr_control, ..) = dp.HRTIM_COMMON.hr_control(&mut rcc).wait_for_calibration();
53+
let mut hr_control = hr_control.constrain();
54+
55+
let HrParts {
56+
mut timer,
57+
mut cr1,
58+
out: (mut out1, mut out2),
59+
..
60+
} = dp
61+
.HRTIM_TIMA
62+
.pwm_advanced((pin_a, pin_b), &mut rcc)
63+
.prescaler(prescaler)
64+
.period(0xFFFF)
65+
.push_pull_mode(true) // Set push pull mode, out1 and out2 are
66+
// alternated every period with one being
67+
// inactive and the other getting to output its wave form
68+
// as normal
69+
.finalize(&mut hr_control);
70+
71+
out1.enable_rst_event(&cr1); // Set low on compare match with cr1
72+
out2.enable_rst_event(&cr1);
73+
74+
out1.enable_set_event(&timer); // Set high at new period
75+
out2.enable_set_event(&timer);
76+
77+
out1.enable();
78+
out2.enable();
79+
80+
loop {
81+
// Step frequency from 18kHz to about 180kHz(half of that when only looking at one pin)
82+
for i in 1..10 {
83+
let new_period = u16::MAX / i;
84+
85+
cr1.set_duty(new_period / 3);
86+
timer.set_period(new_period);
87+
88+
delay.delay(500_u32.millis());
89+
}
90+
}
91+
}

examples/stm32h7/hrtim.rs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#![no_std]
2+
#![no_main]
3+
4+
use cortex_m_rt::entry;
5+
use panic_probe as _;
6+
use stm32_hrtim::{
7+
compare_register::HrCompareRegister, control::HrControltExt, output::HrOutput, timer::HrTimer,
8+
HrParts, HrPwmAdvExt, Pscl4,
9+
};
10+
use stm32h7xx_hal::{
11+
delay::DelayExt,
12+
gpio::GpioExt,
13+
pwr::PwrExt,
14+
rcc::{self, RccExt},
15+
stm32::{CorePeripherals, Peripherals},
16+
};
17+
18+
use fugit::{ExtU32, RateExtU32 as _};
19+
20+
#[entry]
21+
fn main() -> ! {
22+
defmt::info!("Initializing...");
23+
24+
let dp = Peripherals::take().expect("cannot take peripherals");
25+
let cp = CorePeripherals::take().expect("cannot take core");
26+
27+
// Constrain and Freeze power
28+
let pwr = dp.PWR.constrain();
29+
let pwrcfg = pwr.freeze();
30+
31+
// Constrain and Freeze clock
32+
let rcc = dp.RCC.constrain();
33+
let ccdr = rcc.sys_ck(320.MHz()).freeze(pwrcfg, &dp.SYSCFG);
34+
35+
// Acquire the GPIO peripherals. This also enables the clock for
36+
// the GPIOs in the RCC register.
37+
let gpioa = dp.GPIOA.split(ccdr.peripheral.GPIOA);
38+
39+
// Get the delay provider.
40+
let mut delay = cp.SYST.delay(ccdr.clocks);
41+
42+
// ...with a prescaler of 4 this gives us a HrTimer with a tick rate of 960MHz
43+
// With max the max period set, this would be 960MHz/2^16 ~= 15kHz...
44+
let prescaler = Pscl4;
45+
46+
let pin_a = gpioa.pa8;
47+
let pin_b = gpioa.pa9;
48+
49+
// . . . .
50+
// . 30% . . .
51+
// ---- . .---- .
52+
//out1 | | . | | .
53+
// | | . | | .
54+
// -------- ---------------------------- --------------------
55+
// . .---- . .----
56+
//out2 . | | . | |
57+
// . | | . | |
58+
// ------------------------ ---------------------------- ----
59+
// . . . .
60+
// . . . .
61+
let (hr_control, ..) = dp
62+
.HRTIM_COMMON
63+
.hr_control(ccdr.peripheral.HRTIM)
64+
.wait_for_calibration();
65+
let mut hr_control = hr_control.constrain();
66+
67+
let HrParts {
68+
mut timer,
69+
mut cr1,
70+
out: (mut out1, mut out2),
71+
..
72+
} = dp
73+
.HRTIM_TIMA
74+
.pwm_advanced((pin_a, pin_b), &mut rcc)
75+
.prescaler(prescaler)
76+
.period(0xFFFF)
77+
.push_pull_mode(true) // Set push pull mode, out1 and out2 are
78+
// alternated every period with one being
79+
// inactive and the other getting to output its wave form
80+
// as normal
81+
.finalize(&mut hr_control);
82+
83+
out1.enable_rst_event(&cr1); // Set low on compare match with cr1
84+
out2.enable_rst_event(&cr1);
85+
86+
out1.enable_set_event(&timer); // Set high at new period
87+
out2.enable_set_event(&timer);
88+
89+
out1.enable();
90+
out2.enable();
91+
92+
loop {
93+
// Step frequency from 18kHz to about 180kHz(half of that when only looking at one pin)
94+
for i in 1..10 {
95+
let new_period = u16::MAX / i;
96+
97+
cr1.set_duty(new_period / 3);
98+
timer.set_period(new_period);
99+
100+
delay.delay(500_u32.millis());
101+
}
102+
}
103+
}

0 commit comments

Comments
 (0)