|
| 1 | +pub mod adc_trigger; |
| 2 | +pub mod capture; |
| 3 | +pub mod external_event; |
| 4 | +pub mod fault; |
| 5 | + |
| 6 | +use core::mem::MaybeUninit; |
| 7 | + |
| 8 | +use crate::{ |
| 9 | + gpio, |
| 10 | + rcc::{Enable, Reset}, |
| 11 | + stm32::{HRTIM_COMMON, HRTIM_TIMA, HRTIM_TIMB, HRTIM_TIMC, HRTIM_TIMD, HRTIM_TIME, HRTIM_TIMF}, |
| 12 | +}; |
| 13 | +use stm32_hrtim::{ |
| 14 | + control::{HrPwmControl, HrTimOngoingCalibration}, |
| 15 | + output::{HrOut1, HrOut2, ToHrOut}, |
| 16 | + HrParts, HrPwmBuilder, |
| 17 | +}; |
| 18 | + |
| 19 | +pub use stm32_hrtim; |
| 20 | + |
| 21 | +pub trait HrControltExt { |
| 22 | + fn hr_control(self, rcc: &mut crate::rcc::Rcc) -> HrTimOngoingCalibration; |
| 23 | +} |
| 24 | + |
| 25 | +impl HrControltExt for crate::stm32::HRTIM_COMMON { |
| 26 | + fn hr_control(self, rcc: &mut crate::rcc::Rcc) -> HrTimOngoingCalibration { |
| 27 | + HRTIM_COMMON::enable(rcc); |
| 28 | + HRTIM_COMMON::reset(rcc); |
| 29 | + |
| 30 | + // TODO: Verify that the HRTIM gets a clock of 100-170MHz as input |
| 31 | + // SAFETY: We have enabled the rcc |
| 32 | + unsafe { HrTimOngoingCalibration::hr_control() } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +pub trait HrPwmBuilderExt<TIM, PSCL, PINS: ToHrOut<TIM>> { |
| 37 | + fn finalize(self, control: &mut HrPwmControl) -> HrParts<TIM, PSCL, PINS::Out<PSCL>>; |
| 38 | +} |
| 39 | +macro_rules! impl_finalize { |
| 40 | + ($($TIMX:ident),+) => {$( |
| 41 | + impl<PSCL: stm32_hrtim::HrtimPrescaler, PINS: HrtimPin<$TIMX>> HrPwmBuilderExt<$TIMX, PSCL, PINS> |
| 42 | + for HrPwmBuilder<$TIMX, PSCL, stm32_hrtim::PreloadSource, PINS> |
| 43 | + { |
| 44 | + fn finalize( |
| 45 | + self, |
| 46 | + control: &mut HrPwmControl, |
| 47 | + ) -> HrParts<$TIMX, PSCL, <PINS as ToHrOut<$TIMX>>::Out<PSCL>> { |
| 48 | + let pins = self._init(control); |
| 49 | + pins.connect_to_hrtim(); |
| 50 | + unsafe { MaybeUninit::uninit().assume_init() } |
| 51 | + } |
| 52 | + } |
| 53 | + )+}; |
| 54 | +} |
| 55 | + |
| 56 | +impl_finalize! { |
| 57 | + HRTIM_TIMA, |
| 58 | + HRTIM_TIMB, |
| 59 | + HRTIM_TIMC, |
| 60 | + HRTIM_TIMD, |
| 61 | + HRTIM_TIME, |
| 62 | + HRTIM_TIMF |
| 63 | +} |
| 64 | + |
| 65 | +use gpio::{ |
| 66 | + gpioa::{PA10, PA11, PA8, PA9}, |
| 67 | + gpioc::PC8, |
| 68 | +}; |
| 69 | + |
| 70 | +use gpio::{ |
| 71 | + gpiob::{PB12, PB13, PB14, PB15}, |
| 72 | + gpioc::PC9, |
| 73 | +}; |
| 74 | + |
| 75 | +use gpio::gpioc::{PC6, PC7}; |
| 76 | + |
| 77 | +pub trait HrtimPin<TIM>: ToHrOut<TIM> { |
| 78 | + fn connect_to_hrtim(self); |
| 79 | +} |
| 80 | + |
| 81 | +impl<TIM, PA, PB> HrtimPin<TIM> for (PA, PB) |
| 82 | +where |
| 83 | + PA: HrtimPin<TIM>, |
| 84 | + PB: HrtimPin<TIM>, |
| 85 | +{ |
| 86 | + fn connect_to_hrtim(self) { |
| 87 | + self.0.connect_to_hrtim(); |
| 88 | + self.1.connect_to_hrtim(); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +macro_rules! pins_helper { |
| 93 | + ($TIMX:ty, $HrOutY:ident, $CHY:ident<$CHY_AF:literal>) => { |
| 94 | + //impl sealed::Sealed<$TIMX> for $CHY<GpioInputMode> {} |
| 95 | + |
| 96 | + unsafe impl ToHrOut<$TIMX> for $CHY<gpio::DefaultMode> { |
| 97 | + type Out<PSCL> = $HrOutY<$TIMX, PSCL>; |
| 98 | + } |
| 99 | + |
| 100 | + impl HrtimPin<$TIMX> for $CHY<gpio::DefaultMode> { |
| 101 | + // Pin<Gpio, Index, Alternate<PushPull, AF>> |
| 102 | + fn connect_to_hrtim(self) { |
| 103 | + let _: $CHY<gpio::Alternate<{ $CHY_AF }>> = self.into_alternate(); |
| 104 | + } |
| 105 | + } |
| 106 | + }; |
| 107 | +} |
| 108 | + |
| 109 | +macro_rules! pins { |
| 110 | + ($($TIMX:ty: CH1: $CH1:ident<$CH1_AF:literal>, CH2: $CH2:ident<$CH2_AF:literal>),+) => {$( |
| 111 | + pins_helper!($TIMX, HrOut1, $CH1<$CH1_AF>); |
| 112 | + pins_helper!($TIMX, HrOut2, $CH2<$CH2_AF>); |
| 113 | + )+}; |
| 114 | +} |
| 115 | + |
| 116 | +pins! { |
| 117 | + HRTIM_TIMA: CH1: PA8<13>, CH2: PA9<13>, |
| 118 | + HRTIM_TIMB: CH1: PA10<13>, CH2: PA11<13>, |
| 119 | + HRTIM_TIMC: CH1: PB12<13>, CH2: PB13<13>, |
| 120 | + HRTIM_TIMD: CH1: PB14<13>, CH2: PB15<13>, |
| 121 | + HRTIM_TIME: CH1: PC8<3>, CH2: PC9<3>, |
| 122 | + HRTIM_TIMF: CH1: PC6<13>, CH2: PC7<13> |
| 123 | +} |
0 commit comments