Skip to content

Commit 3b8bbb6

Browse files
committed
Update examples with calculations of frequencies
1 parent 5ed0deb commit 3b8bbb6

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

examples/stm32f3/hrtim.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ fn main() -> ! {
2727
let mut rcc = dp.RCC.constrain();
2828
let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);
2929

30-
//let mut rcc = Input to hrtim needs to be 128MHz when using HSI, 128-144 with HSE
31-
3230
// Set system frequency to 64MHz using PLL, PLLCLKx2 will thus be 128MHz which
3331
// feeds into the HRTIM. This and the HRTIM's DLL would lead to an effective
3432
// HRTIM frequency of 128MHz * 32 = 4.096GHz...
@@ -40,8 +38,8 @@ fn main() -> ! {
4038

4139
let mut delay = Delay::new(cp.SYST, clocks);
4240

43-
// ...with a prescaler of 4 this gives us a HrTimer with a tick rate of 960MHz
44-
// With max the max period set, this would be 960MHz/2^16 ~= 15kHz...
41+
// ...with a prescaler of 4 this gives us a HrTimer with a tick rate of 1024MHz
42+
// With max the max period set, this would be 1024MHz/2^16 ~= 15.6kHz...
4543
let prescaler = Pscl4;
4644

4745
let pin_a = gpioa.pa8;
@@ -96,8 +94,8 @@ fn main() -> ! {
9694
out2.enable();
9795

9896
loop {
99-
// Step frequency from 18kHz to about 180kHz(half of that when only looking at one pin)
100-
for i in 1..10 {
97+
// Step frequency from 15.6kHz to about 156kHz(half of that when only looking at one pin)
98+
for i in 1..=10 {
10199
let new_period = u16::MAX / i;
102100

103101
cr1.set_duty(new_period / 3);

examples/stm32g4/hrtim.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn main() -> ! {
4040
let mut delay = cp.SYST.delay(&rcc.clocks);
4141

4242
// ...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...
43+
// With max the max period set, this would be 960MHz/2^16 ~= 14.6kHz...
4444
let prescaler = Pscl4;
4545

4646
let gpioa = dp.GPIOA.split(&mut rcc);
@@ -88,8 +88,8 @@ fn main() -> ! {
8888
out2.enable();
8989

9090
loop {
91-
// Step frequency from 18kHz to about 180kHz(half of that when only looking at one pin)
92-
for i in 1..10 {
91+
// Step frequency from 14.6kHz to about 146kHz(half of that when only looking at one pin)
92+
for i in 1..=10 {
9393
let new_period = u16::MAX / i;
9494

9595
cr1.set_duty(new_period / 3);

examples/stm32h7/hrtim.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use cortex_m_rt::entry;
55
use panic_probe as _;
66
use stm32_hrtim::{
77
compare_register::HrCompareRegister, control::HrControltExt, output::HrOutput, timer::HrTimer,
8-
HrParts, HrPwmAdvExt, Pscl4,
8+
HrParts, HrPwmAdvExt, Pscl1,
99
};
1010
use stm32h7xx_hal::{
11-
prelude::_embedded_hal_blocking_delay_DelayMs,
1211
delay::DelayExt,
1312
gpio::GpioExt,
13+
prelude::_embedded_hal_blocking_delay_DelayMs,
1414
pwr::PwrExt,
1515
rcc::RccExt,
1616
stm32::{CorePeripherals, Peripherals},
@@ -31,7 +31,14 @@ fn main() -> ! {
3131

3232
// Constrain and Freeze clock
3333
let rcc = dp.RCC.constrain();
34-
let ccdr = rcc.sys_ck(320.MHz()).freeze(pwrcfg, &dp.SYSCFG);
34+
35+
// With a sys_ck of 240MHz and d1cpre of 1 if the HRTIM will be fed by 240MHz/1 = 240MHz
36+
// since HRTIMSEL is set to take the HRTIM's clock directly from the core clock. The
37+
// stm32h7 devices' HRTIM does not have a DLL, also leading to an effective HRTIM
38+
// frequency of 240MHz...
39+
let ccdr = rcc
40+
.sys_ck(240.MHz())
41+
.freeze(pwrcfg, &dp.SYSCFG);
3542

3643
// Acquire the GPIO peripherals. This also enables the clock for
3744
// the GPIOs in the RCC register.
@@ -40,9 +47,9 @@ fn main() -> ! {
4047
// Get the delay provider.
4148
let mut delay = cp.SYST.delay(ccdr.clocks);
4249

43-
// ...with a prescaler of 4 this gives us a HrTimer with a tick rate of 960MHz
44-
// With max the max period set, this would be 960MHz/2^16 ~= 15kHz...
45-
let prescaler = Pscl4;
50+
// ...with a prescaler of 1 this gives us a HrTimer with a tick rate of 240MHz
51+
// With max the max period set, this would be 240MHz/2^16 ~= 3.7kHz...
52+
let prescaler = Pscl1;
4653

4754
let pin_a = gpioc.pc6.into_input();
4855
let pin_b = gpioc.pc7.into_input();
@@ -61,7 +68,7 @@ fn main() -> ! {
6168
// . . . .
6269
let (hr_control, ..) = dp
6370
.HRTIM_COMMON
64-
.hr_control(ccdr.peripheral.HRTIM)
71+
.hr_control(&ccdr.clocks, ccdr.peripheral.HRTIM)
6572
.wait_for_calibration();
6673
let mut hr_control = hr_control.constrain();
6774

@@ -91,7 +98,7 @@ fn main() -> ! {
9198
out2.enable();
9299

93100
loop {
94-
// Step frequency from 18kHz to about 180kHz(half of that when only looking at one pin)
101+
// Step frequency from 3.7kHz to about 36.6kHz(half of that when only looking at one pin)
95102
for i in 1..10 {
96103
let new_period = u16::MAX / i;
97104

src/control.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub trait HrControltExt {
2626
#[cfg(feature = "stm32g4")]
2727
rcc: &mut hal::rcc::Rcc,
2828

29+
#[cfg(feature = "stm32h7")] _clocks: &hal::rcc::CoreClocks,
2930
#[cfg(feature = "stm32h7")] rcc: hal::rcc::rec::Hrtim,
3031
) -> HrTimOngoingCalibration;
3132
}
@@ -40,6 +41,7 @@ impl HrControltExt for HRTIM_COMMON {
4041
#[cfg(feature = "stm32g4")]
4142
rcc: &mut hal::rcc::Rcc,
4243

44+
#[cfg(feature = "stm32h7")] _clocks: &hal::rcc::CoreClocks,
4345
#[cfg(feature = "stm32h7")] rcc: hal::rcc::rec::Hrtim,
4446
) -> HrTimOngoingCalibration {
4547
let common = unsafe { &*HRTIM_COMMON::ptr() };
@@ -57,6 +59,11 @@ impl HrControltExt for HRTIM_COMMON {
5759

5860
#[cfg(feature = "stm32h7")]
5961
{
62+
{
63+
let rcc = unsafe { &*hal::stm32::RCC::ptr() };
64+
// Same clock source as CPU
65+
rcc.cfgr().modify(|_, w| w.hrtimsel().c_ck());
66+
}
6067
rcc
6168
}
6269
};

0 commit comments

Comments
 (0)