|
| 1 | +#![no_std] |
| 2 | +#![no_main] |
| 3 | + |
| 4 | +#[path = "../utils/mod.rs"] |
| 5 | +mod utils; |
| 6 | +use utils::logger::info; |
| 7 | + |
| 8 | +use cortex_m_rt::entry; |
| 9 | +use stm32_hrtim::{ |
| 10 | + compare_register::HrCompareRegister, deadtime::DeadtimeConfig, output::HrOutput, |
| 11 | + timer::HrTimer, HrParts, HrPwmAdvExt, Pscl1, |
| 12 | +}; |
| 13 | +use stm32g4xx_hal::{ |
| 14 | + delay::{DelayExt, SYSTDelayExt}, |
| 15 | + gpio::GpioExt, |
| 16 | + hrtim::{HrControltExt, HrPwmBuilderExt}, |
| 17 | + pwr::PwrExt, |
| 18 | + rcc::{self, RccExt}, |
| 19 | + stm32::{CorePeripherals, Peripherals}, |
| 20 | + time::ExtU32, |
| 21 | +}; |
| 22 | + |
| 23 | +#[entry] |
| 24 | +fn main() -> ! { |
| 25 | + info!("Initializing..."); |
| 26 | + |
| 27 | + let dp = Peripherals::take().expect("cannot take peripherals"); |
| 28 | + let cp = CorePeripherals::take().expect("cannot take core"); |
| 29 | + // Set system frequency to 16MHz * 15/1/2 = 120MHz |
| 30 | + // This would lead to HrTim running at 120MHz * 32 = 3.84GHz... |
| 31 | + let pwr = dp.PWR.constrain().freeze(); |
| 32 | + let mut rcc = dp.RCC.freeze( |
| 33 | + rcc::Config::pll().pll_cfg(rcc::PllConfig { |
| 34 | + mux: rcc::PllSrc::HSI, |
| 35 | + n: rcc::PllNMul::MUL_15, |
| 36 | + m: rcc::PllMDiv::DIV_1, |
| 37 | + r: Some(rcc::PllRDiv::DIV_2), |
| 38 | + |
| 39 | + ..Default::default() |
| 40 | + }), |
| 41 | + pwr, |
| 42 | + ); |
| 43 | + |
| 44 | + let mut delay = cp.SYST.delay(&rcc.clocks); |
| 45 | + |
| 46 | + // ...with a prescaler of 1 this gives us a HrTimer with a tick rate of 960MHz |
| 47 | + // With max the max period set, this would be 960MHz/2^16 ~= 14.6kHz... |
| 48 | + let prescaler = Pscl1; |
| 49 | + |
| 50 | + let gpioa = dp.GPIOA.split(&mut rcc); |
| 51 | + let pin_a = gpioa.pa8; // D7 On nucleo-G474 - LI Blue-White |
| 52 | + let pin_b = gpioa.pa9; // D8 On nucleo-G474 - HI Green |
| 53 | + |
| 54 | + // . . . . |
| 55 | + // . 30% . . . |
| 56 | + // ---- . .---- . |
| 57 | + //out1 | | . | | . |
| 58 | + // | | . | | . |
| 59 | + // -------- ---------------------------- -------------------- |
| 60 | + // . .---- . .---- |
| 61 | + //out2 . | | . | | |
| 62 | + // . | | . | | |
| 63 | + // ------------------------ ---------------------------- ---- |
| 64 | + // . . . . |
| 65 | + // . . . . |
| 66 | + let (hr_control, ..) = dp.HRTIM_COMMON.hr_control(&mut rcc).wait_for_calibration(); |
| 67 | + let mut hr_control = hr_control.constrain(); |
| 68 | + |
| 69 | + let deadtime = DeadtimeConfig::default() |
| 70 | + .prescaler(stm32_hrtim::deadtime::DeadtimePrescaler::ThrtimDiv8) |
| 71 | + .deadtime_rising_value(64) // 32 / (8 * 120MHz) = ~33.33ns |
| 72 | + .deadtime_falling_value(64); // 32 / (8 * 120MHz) = ~33.33ns |
| 73 | + let period = 0xF00; |
| 74 | + let HrParts { |
| 75 | + mut timer, |
| 76 | + mut cr1, |
| 77 | + out: (mut out1, mut out2), |
| 78 | + .. |
| 79 | + } = dp |
| 80 | + .HRTIM_TIMA |
| 81 | + .pwm_advanced((pin_a, pin_b)) |
| 82 | + .prescaler(prescaler) |
| 83 | + .period(period) // (120MHz * 32) / 0xF00 = 1MHz |
| 84 | + .deadtime(deadtime) |
| 85 | + .out1_polarity(stm32_hrtim::Polarity::ActiveHigh) |
| 86 | + .out2_polarity(stm32_hrtim::Polarity::ActiveHigh) |
| 87 | + .finalize(&mut hr_control); |
| 88 | + |
| 89 | + out1.enable_rst_event(&cr1); // Set low on compare match with cr1 |
| 90 | + out2.enable_rst_event(&cr1); |
| 91 | + |
| 92 | + out1.enable_set_event(&timer); // Set high at new period |
| 93 | + out2.enable_set_event(&timer); |
| 94 | + |
| 95 | + out1.enable(); |
| 96 | + out2.enable(); |
| 97 | + timer.start(&mut hr_control.control); |
| 98 | + |
| 99 | + loop { |
| 100 | + // Step frequency from 14.6kHz to about 146kHz(half of that when only looking at one pin) |
| 101 | + for i in 1..10 { |
| 102 | + let p = timer.get_period(); |
| 103 | + info!("p: {}", p); |
| 104 | + info!("Duty: {}%", i * 10); |
| 105 | + cr1.set_duty(i * period / 10); |
| 106 | + |
| 107 | + delay.delay(2000_u32.millis()); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments