|
| 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::gpio::gpioa::PA8; |
| 8 | +use hal::gpio::Alternate; |
| 9 | +use hal::gpio::AF13; |
| 10 | +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::rcc; |
| 20 | +use hal::stm32; |
| 21 | +use stm32g4xx_hal as hal; |
| 22 | +//mod utils; |
| 23 | + |
| 24 | +use defmt_rtt as _; // global logger |
| 25 | +use panic_probe as _; |
| 26 | + |
| 27 | +#[entry] |
| 28 | +fn main() -> ! { |
| 29 | + let dp = stm32::Peripherals::take().expect("cannot take peripherals"); |
| 30 | + let cp = stm32::CorePeripherals::take().expect("cannot take core"); |
| 31 | + // Set system frequency to 16MHz * 75/4/2 = 150MHz |
| 32 | + // 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 | + })); |
| 40 | + |
| 41 | + let mut delay = cp.SYST.delay(&rcc.clocks); |
| 42 | + |
| 43 | + let gpioa = dp.GPIOA.split(&mut rcc); |
| 44 | + let gpiob = dp.GPIOB.split(&mut rcc); |
| 45 | + let (mut fault_control, flt_inputs, eev_inputs) = |
| 46 | + dp.HRTIM_COMMON.hr_control(&mut rcc).wait_for_calibration(); |
| 47 | + |
| 48 | + let eev_input3 = eev_inputs |
| 49 | + .eev_input3 |
| 50 | + .bind_pin(gpiob.pb7.into_pull_down_input()) |
| 51 | + .polarity(hal::pwm::Polarity::ActiveHigh) |
| 52 | + .finalize(&mut fault_control); |
| 53 | + |
| 54 | + // ...with a prescaler of 4 this gives us a HrTimer with a tick rate of 1.2GHz |
| 55 | + // With max the max period set, this would be 1.2GHz/2^16 ~= 18kHz... |
| 56 | + let prescaler = Pscl4; |
| 57 | + |
| 58 | + let pin_a: PA8<Alternate<AF13>> = gpioa.pa8.into_alternate(); |
| 59 | + |
| 60 | + // . . . * |
| 61 | + // . 33% . . * . . |
| 62 | + // .-----. .-----. .--. . . |
| 63 | + //out1 | | | | | | . . |
| 64 | + // | | | | | | . . |
| 65 | + // ------ ----------- ----------- ----------------------------------- |
| 66 | + // . . . * . . |
| 67 | + // . . . * . . |
| 68 | + // . . . *-------- . . |
| 69 | + //fault . . . | | . . |
| 70 | + // . . . | | . . |
| 71 | + // ----------------------------------------- -------------------------- |
| 72 | + // . . . * . . |
| 73 | + // . . . * . . |
| 74 | + let (timer, (mut cr1, _cr2, _cr3, _cr4), mut out1) = dp |
| 75 | + .HRTIM_TIMA |
| 76 | + .pwm_advanced(pin_a, &mut rcc) |
| 77 | + .prescaler(prescaler) |
| 78 | + .period(0xFFFF) |
| 79 | + //.with_fault_source(fault_source1) |
| 80 | + //.with_fault_source(fault_source2) |
| 81 | + .with_fault_source(fault_source3) // Set fault source |
| 82 | + //.with_fault_source(fault_source4) |
| 83 | + //.with_fault_source(fault_source5) |
| 84 | + //.with_fault_source(fault_source6) |
| 85 | + .fault_action1(FaultAction::ForceInactive) |
| 86 | + .fault_action2(FaultAction::ForceInactive) |
| 87 | + // alternated every period with one being |
| 88 | + // inactive and the other getting to output its wave form |
| 89 | + // as normal |
| 90 | + .finalize(&mut fault_control); |
| 91 | + |
| 92 | + out1.enable_rst_event(EventSource::Cr1); // Set low on compare match with cr1 |
| 93 | + out1.enable_set_event(EventSource::Period); // Set high at new period |
| 94 | + cr1.set_duty(timer.get_period() / 3); |
| 95 | + //unsafe {((HRTIM_COMMON::ptr() as *mut u8).offset(0x14) as *mut u32).write_volatile(1); } |
| 96 | + out1.enable(); |
| 97 | + |
| 98 | + defmt::info!("Started"); |
| 99 | + |
| 100 | + loop { |
| 101 | + for _ in 0..5 { |
| 102 | + delay.delay(500_u32.millis()); |
| 103 | + defmt::info!("State: {}", out1.get_state()); |
| 104 | + } |
| 105 | + if fault_control.fault_3.is_fault_active() { |
| 106 | + fault_control.fault_3.clear_fault(); // Clear fault every 5s |
| 107 | + out1.enable(); |
| 108 | + defmt::info!("failt cleared, and output reenabled"); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments