|
| 1 | +#![no_std] |
| 2 | +#![no_main] |
| 3 | + |
| 4 | +use cortex_m_rt::entry; |
| 5 | + |
| 6 | +use defmt_rtt as _; // global logger |
| 7 | +use panic_probe as _; |
| 8 | + |
| 9 | +#[entry] |
| 10 | +fn main() -> ! { |
| 11 | + use stm32g4xx_hal as hal; |
| 12 | + |
| 13 | + use defmt::info; |
| 14 | + use hal::{ |
| 15 | + gpio::{gpioa::PA8, Alternate, GpioExt, AF13}, |
| 16 | + hrtim::{ |
| 17 | + capture::HrCapture, compare_register::HrCompareRegister, control::HrControltExt, |
| 18 | + external_event, external_event::ToExternalEventSource, output::HrOutput, |
| 19 | + timer::HrTimer, HrPwmAdvExt, Pscl4, |
| 20 | + }, |
| 21 | + pwm, |
| 22 | + pwr::PwrExt, |
| 23 | + rcc::{self, RccExt}, |
| 24 | + stm32::Peripherals, |
| 25 | + }; |
| 26 | + |
| 27 | + info!("start"); |
| 28 | + |
| 29 | + let dp = Peripherals::take().unwrap(); |
| 30 | + |
| 31 | + // Set system frequency to 16MHz * 15/1/2 = 120MHz |
| 32 | + // This would lead to HrTim running at 120MHz * 32 = 3.84... |
| 33 | + info!("rcc"); |
| 34 | + let pwr = dp.PWR.constrain().freeze(); |
| 35 | + let mut rcc = dp.RCC.freeze( |
| 36 | + rcc::Config::pll().pll_cfg(rcc::PllConfig { |
| 37 | + mux: rcc::PLLSrc::HSI, |
| 38 | + n: rcc::PllNMul::MUL_15, |
| 39 | + m: rcc::PllMDiv::DIV_1, |
| 40 | + r: Some(rcc::PllRDiv::DIV_2), |
| 41 | + |
| 42 | + ..Default::default() |
| 43 | + }), |
| 44 | + pwr, |
| 45 | + ); |
| 46 | + |
| 47 | + info!("Setup Gpio"); |
| 48 | + let gpioa = dp.GPIOA.split(&mut rcc); |
| 49 | + let gpiob = dp.GPIOB.split(&mut rcc); |
| 50 | + let pin_a: PA8<Alternate<AF13>> = gpioa.pa8.into_alternate(); |
| 51 | + |
| 52 | + // ...with a prescaler of 4 this gives us a HrTimer with a tick rate of 960MHz |
| 53 | + // With max the max period set, this would be 960MHz/2^16 ~= 15kHz... |
| 54 | + let prescaler = Pscl4; |
| 55 | + |
| 56 | + // . . |
| 57 | + // . 50% . |
| 58 | + // ------ ------ |
| 59 | + //out1 | | | | |
| 60 | + // | | | | |
| 61 | + // -------- ---------- -------- |
| 62 | + // . ^ ^ |
| 63 | + // . | | |
| 64 | + //AD samlp pa0 temp |
| 65 | + let period = 0xFFFF; |
| 66 | + let (mut hr_control, _flt_inputs, eev_inputs) = |
| 67 | + dp.HRTIM_COMMON.hr_control(&mut rcc).wait_for_calibration(); |
| 68 | + |
| 69 | + let eev_input3 = eev_inputs |
| 70 | + .eev_input3 |
| 71 | + .bind(gpiob.pb7.into_pull_down_input()) |
| 72 | + .edge_or_polarity(external_event::EdgeOrPolarity::Polarity( |
| 73 | + pwm::Polarity::ActiveHigh, |
| 74 | + )) |
| 75 | + .finalize(&mut hr_control); |
| 76 | + |
| 77 | + let mut hr_control = hr_control.constrain(); |
| 78 | + let (mut timer, (mut cr1, _cr2, _cr3, _cr4), mut out1) = dp |
| 79 | + .HRTIM_TIMA |
| 80 | + .pwm_advanced(pin_a, &mut rcc) |
| 81 | + .prescaler(prescaler) |
| 82 | + .period(period) |
| 83 | + .finalize(&mut hr_control); |
| 84 | + |
| 85 | + out1.enable_rst_event(&cr1); // Set low on compare match with cr1 |
| 86 | + out1.enable_set_event(&timer); // Set high at new period |
| 87 | + |
| 88 | + cr1.set_duty(period / 2); |
| 89 | + timer.start(&mut hr_control); |
| 90 | + |
| 91 | + let capture = timer.capture_ch1(); |
| 92 | + capture.enable_interrupt(true, &mut hr_control); |
| 93 | + capture.add_event(&eev_input3); |
| 94 | + |
| 95 | + loop { |
| 96 | + if !capture.is_pending() { |
| 97 | + continue; |
| 98 | + } |
| 99 | + capture.clear_interrupt(); |
| 100 | + |
| 101 | + info!("Capture: {:?}", capture.get()); |
| 102 | + } |
| 103 | +} |
0 commit comments