mod traits;
pub mod bridge_converter;
pub mod resonant_converter;
use core::mem::MaybeUninit;
use embassy_hal_internal::Peri;
#[allow(unused_imports)] use stm32_hrtim::HrPwmAdvExt;
use stm32_hrtim::control::{HrPwmControl, HrTimOngoingCalibration};
use stm32_hrtim::output::{Output1Pin, Output2Pin};
#[cfg(hrtim_v2)]
use stm32_hrtim::pac::HRTIM_TIMF;
use stm32_hrtim::pac::{HRTIM_MASTER, HRTIM_TIMA, HRTIM_TIMB, HRTIM_TIMC, HRTIM_TIMD, HRTIM_TIME};
pub use stm32_hrtim::{self, Pscl1, Pscl2, Pscl4, Pscl8, Pscl16, Pscl32, Pscl64, Pscl128, PsclDefault};
use stm32_hrtim::{DacResetTrigger, DacStepTrigger, HrParts, HrPwmBuilder};
use traits::Instance;
use crate::gpio::{AfType, OutputType, Speed};
use crate::peripherals::HRTIM1;
use crate::rcc;
pub struct Parts {
pub control: HrTimOngoingCalibration,
pub master: HRTIM_MASTER,
pub tima: HRTIM_TIMA,
pub timb: HRTIM_TIMB,
pub timc: HRTIM_TIMC,
pub timd: HRTIM_TIMD,
pub time: HRTIM_TIME,
#[cfg(hrtim_v2)]
pub timf: HRTIM_TIMF,
}
pub trait HrControltExt {
fn hr_control(self) -> Parts;
}
impl<T: super::hrtim::Instance> HrControltExt for T {
fn hr_control(self) -> Parts {
rcc::enable_and_reset::<T>();
unsafe {
Parts {
control: HrTimOngoingCalibration::hr_control(),
master: HRTIM_MASTER::steal(),
tima: HRTIM_TIMA::steal(),
timb: HRTIM_TIMB::steal(),
timc: HRTIM_TIMC::steal(),
timd: HRTIM_TIMD::steal(),
time: HRTIM_TIME::steal(),
#[cfg(hrtim_v2)]
timf: HRTIM_TIMF::steal(),
}
}
}
}
pub trait HrPwmBuilderExt<TIM, PSCL, P1: Output1Pin<TIM>, P2: Output2Pin<TIM>> {
fn finalize(self, control: &mut HrPwmControl) -> HrParts<TIM, PSCL>;
}
macro_rules! impl_finalize {
($($TIMX:ident),+) => {$(
impl<PSCL, P1, P2, DacRst, DacStp> HrPwmBuilderExt<$TIMX, PSCL, P1, P2>
for HrPwmBuilder<$TIMX, PSCL, stm32_hrtim::PreloadSource, P1, P2, DacRst, DacStp>
where PSCL:
stm32_hrtim::HrtimPrescaler,
P1: Out1Pin<$TIMX>,
P2: Out2Pin<$TIMX>,
DacRst: DacResetTrigger,
DacStp: DacStepTrigger,
{
fn finalize(
self,
control: &mut HrPwmControl,
) -> HrParts<$TIMX, PSCL> {
let (pin1, pin2) = self._init(control);
pin1.connect_to_hrtim();
pin2.connect_to_hrtim();
unsafe { MaybeUninit::uninit().assume_init() }
}
}
)+};
}
pub struct Pin<P> {
pub speed: Speed,
pub pin: P,
}
impl_finalize! {
HRTIM_TIMA,
HRTIM_TIMB,
HRTIM_TIMC,
HRTIM_TIMD,
HRTIM_TIME
}
#[cfg(hrtim_v2)]
impl_finalize! {
HRTIM_TIMF
}
pub trait Out1Pin<TIM>: Output1Pin<TIM> {
fn connect_to_hrtim(self);
}
pub trait Out2Pin<TIM>: Output2Pin<TIM> {
fn connect_to_hrtim(self);
}
macro_rules! pins_helper {
($TIMX:ty, $ChannelXPin:ident, $OutYPin:ident, $OutputYPin:ident, $HrOutY:ident) => {
unsafe impl<'d, T: $ChannelXPin<HRTIM1>> $OutputYPin<$TIMX> for Pin<Peri<'d, T>> {}
impl<'d, T: $ChannelXPin<HRTIM1>> $OutYPin<$TIMX> for Pin<Peri<'d, T>> {
fn connect_to_hrtim(self) {
self.pin.set_as_af(
self.pin.af_num(),
AfType::output(OutputType::PushPull, self.speed),
);
}
}
};
}
pins_helper!(HRTIM_TIMA, ChannelAPin, Out1Pin, Output1Pin, HrOut1);
pins_helper!(HRTIM_TIMA, ChannelAComplementaryPin, Out2Pin, Output2Pin, HrOut2);
pins_helper!(HRTIM_TIMB, ChannelBPin, Out1Pin, Output1Pin, HrOut1);
pins_helper!(HRTIM_TIMB, ChannelBComplementaryPin, Out2Pin, Output2Pin, HrOut2);
pins_helper!(HRTIM_TIMC, ChannelCPin, Out1Pin, Output1Pin, HrOut1);
pins_helper!(HRTIM_TIMC, ChannelCComplementaryPin, Out2Pin, Output2Pin, HrOut2);
pins_helper!(HRTIM_TIMD, ChannelDPin, Out1Pin, Output1Pin, HrOut1);
pins_helper!(HRTIM_TIMD, ChannelDComplementaryPin, Out2Pin, Output2Pin, HrOut2);
pins_helper!(HRTIM_TIME, ChannelEPin, Out1Pin, Output1Pin, HrOut1);
pins_helper!(HRTIM_TIME, ChannelEComplementaryPin, Out2Pin, Output2Pin, HrOut2);
#[cfg(stm32g4)]
pins_helper!(HRTIM_TIMF, ChannelFPin, Out1Pin, Output1Pin, HrOut1);
#[cfg(stm32g4)]
pins_helper!(HRTIM_TIMF, ChannelFComplementaryPin, Out2Pin, Output2Pin, HrOut2);
pin_trait!(ChannelAPin, Instance);
pin_trait!(ChannelAComplementaryPin, Instance);
pin_trait!(ChannelBPin, Instance);
pin_trait!(ChannelBComplementaryPin, Instance);
pin_trait!(ChannelCPin, Instance);
pin_trait!(ChannelCComplementaryPin, Instance);
pin_trait!(ChannelDPin, Instance);
pin_trait!(ChannelDComplementaryPin, Instance);
pin_trait!(ChannelEPin, Instance);
pin_trait!(ChannelEComplementaryPin, Instance);
#[cfg(hrtim_v2)]
pin_trait!(ChannelFPin, Instance);
#[cfg(hrtim_v2)]
pin_trait!(ChannelFComplementaryPin, Instance);