diff --git a/embassy-stm32/src/hrtim/mod.rs b/embassy-stm32/src/hrtim/mod.rs index 3a05719b0..9ed03a219 100644 --- a/embassy-stm32/src/hrtim/mod.rs +++ b/embassy-stm32/src/hrtim/mod.rs @@ -7,7 +7,7 @@ use embassy_hal_internal::{into_ref, PeripheralRef}; #[allow(unused_imports)] use crate::gpio::sealed::{AFType, Pin}; use crate::gpio::AnyPin; -use crate::hrtim::traits::HighResolutionCaptureCompare16bitInstance; +use crate::hrtim::traits::Instance; use crate::time::Hertz; use crate::Peripheral; @@ -20,37 +20,37 @@ pub enum Source { ChE, } -pub struct BurstController { +pub struct BurstController { phantom: PhantomData, } -pub struct Master { +pub struct Master { phantom: PhantomData, } -pub struct ChA { +pub struct ChA { phantom: PhantomData, } -pub struct ChB { +pub struct ChB { phantom: PhantomData, } -pub struct ChC { +pub struct ChC { phantom: PhantomData, } -pub struct ChD { +pub struct ChD { phantom: PhantomData, } -pub struct ChE { +pub struct ChE { phantom: PhantomData, } mod sealed { - use super::HighResolutionCaptureCompare16bitInstance; + use super::Instance; - pub trait AdvancedChannel { + pub trait AdvancedChannel { fn raw() -> usize; } } -pub trait AdvancedChannel: sealed::AdvancedChannel {} +pub trait AdvancedChannel: sealed::AdvancedChannel {} pub struct PwmPin<'d, Perip, Channel> { _pin: PeripheralRef<'d, AnyPin>, @@ -64,7 +64,7 @@ pub struct ComplementaryPwmPin<'d, Perip, Channel> { macro_rules! advanced_channel_impl { ($new_chx:ident, $channel:tt, $ch_num:expr, $pin_trait:ident, $complementary_pin_trait:ident) => { - impl<'d, Perip: HighResolutionCaptureCompare16bitInstance> PwmPin<'d, Perip, $channel> { + impl<'d, Perip: Instance> PwmPin<'d, Perip, $channel> { pub fn $new_chx(pin: impl Peripheral

> + 'd) -> Self { into_ref!(pin); critical_section::with(|_| { @@ -80,7 +80,7 @@ macro_rules! advanced_channel_impl { } } - impl<'d, Perip: HighResolutionCaptureCompare16bitInstance> ComplementaryPwmPin<'d, Perip, $channel> { + impl<'d, Perip: Instance> ComplementaryPwmPin<'d, Perip, $channel> { pub fn $new_chx(pin: impl Peripheral

> + 'd) -> Self { into_ref!(pin); critical_section::with(|_| { @@ -96,12 +96,12 @@ macro_rules! advanced_channel_impl { } } - impl sealed::AdvancedChannel for $channel { + impl sealed::AdvancedChannel for $channel { fn raw() -> usize { $ch_num } } - impl AdvancedChannel for $channel {} + impl AdvancedChannel for $channel {} }; } @@ -112,7 +112,7 @@ advanced_channel_impl!(new_chd, ChD, 3, ChannelDPin, ChannelDComplementaryPin); advanced_channel_impl!(new_che, ChE, 4, ChannelEPin, ChannelEComplementaryPin); /// Struct used to divide a high resolution timer into multiple channels -pub struct AdvancedPwm<'d, T: HighResolutionCaptureCompare16bitInstance> { +pub struct AdvancedPwm<'d, T: Instance> { _inner: PeripheralRef<'d, T>, pub master: Master, pub burst_controller: BurstController, @@ -123,7 +123,7 @@ pub struct AdvancedPwm<'d, T: HighResolutionCaptureCompare16bitInstance> { pub ch_e: ChE, } -impl<'d, T: HighResolutionCaptureCompare16bitInstance> AdvancedPwm<'d, T> { +impl<'d, T: Instance> AdvancedPwm<'d, T> { pub fn new( tim: impl Peripheral

+ 'd, _cha: Option>>, @@ -171,7 +171,7 @@ impl<'d, T: HighResolutionCaptureCompare16bitInstance> AdvancedPwm<'d, T> { } } -impl BurstController { +impl BurstController { pub fn set_source(&mut self, _source: Source) { todo!("burst mode control registers not implemented") } @@ -186,7 +186,7 @@ impl BurstController { /// It is important to remember that in synchronous topologies, energy can flow in reverse during /// light loading conditions, and that the low-side switch must be active for a short time to drive /// a bootstrapped high-side switch. -pub struct BridgeConverter> { +pub struct BridgeConverter> { timer: PhantomData, channel: PhantomData, dead_time: u16, @@ -195,7 +195,7 @@ pub struct BridgeConverter> BridgeConverter { +impl> BridgeConverter { pub fn new(_channel: C, frequency: Hertz) -> Self { use crate::pac::hrtim::vals::{Activeeffect, Inactiveeffect}; @@ -333,14 +333,14 @@ impl> Bridge /// This implementation of a resonsant converter is appropriate for a half or full bridge, /// but does not include secondary rectification, which is appropriate for applications /// with a low-voltage on the secondary side. -pub struct ResonantConverter> { +pub struct ResonantConverter> { timer: PhantomData, channel: PhantomData, min_period: u16, max_period: u16, } -impl> ResonantConverter { +impl> ResonantConverter { pub fn new(_channel: C, min_frequency: Hertz, max_frequency: Hertz) -> Self { T::set_channel_frequency(C::raw(), min_frequency); @@ -397,13 +397,13 @@ impl> Resona } } -pin_trait!(ChannelAPin, HighResolutionCaptureCompare16bitInstance); -pin_trait!(ChannelAComplementaryPin, HighResolutionCaptureCompare16bitInstance); -pin_trait!(ChannelBPin, HighResolutionCaptureCompare16bitInstance); -pin_trait!(ChannelBComplementaryPin, HighResolutionCaptureCompare16bitInstance); -pin_trait!(ChannelCPin, HighResolutionCaptureCompare16bitInstance); -pin_trait!(ChannelCComplementaryPin, HighResolutionCaptureCompare16bitInstance); -pin_trait!(ChannelDPin, HighResolutionCaptureCompare16bitInstance); -pin_trait!(ChannelDComplementaryPin, HighResolutionCaptureCompare16bitInstance); -pin_trait!(ChannelEPin, HighResolutionCaptureCompare16bitInstance); -pin_trait!(ChannelEComplementaryPin, HighResolutionCaptureCompare16bitInstance); +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); diff --git a/embassy-stm32/src/hrtim/traits.rs b/embassy-stm32/src/hrtim/traits.rs index 7f2cedda5..158a68862 100644 --- a/embassy-stm32/src/hrtim/traits.rs +++ b/embassy-stm32/src/hrtim/traits.rs @@ -2,7 +2,7 @@ use crate::rcc::sealed::RccPeripheral; use crate::time::Hertz; #[derive(Clone, Copy)] -pub(crate) enum HighResolutionControlPrescaler { +pub(crate) enum Prescaler { Div1, Div2, Div4, @@ -13,87 +13,83 @@ pub(crate) enum HighResolutionControlPrescaler { Div128, } -impl From for u32 { - fn from(val: HighResolutionControlPrescaler) -> Self { +impl From for u32 { + fn from(val: Prescaler) -> Self { match val { - HighResolutionControlPrescaler::Div1 => 1, - HighResolutionControlPrescaler::Div2 => 2, - HighResolutionControlPrescaler::Div4 => 4, - HighResolutionControlPrescaler::Div8 => 8, - HighResolutionControlPrescaler::Div16 => 16, - HighResolutionControlPrescaler::Div32 => 32, - HighResolutionControlPrescaler::Div64 => 64, - HighResolutionControlPrescaler::Div128 => 128, + Prescaler::Div1 => 1, + Prescaler::Div2 => 2, + Prescaler::Div4 => 4, + Prescaler::Div8 => 8, + Prescaler::Div16 => 16, + Prescaler::Div32 => 32, + Prescaler::Div64 => 64, + Prescaler::Div128 => 128, } } } -impl From for u8 { - fn from(val: HighResolutionControlPrescaler) -> Self { +impl From for u8 { + fn from(val: Prescaler) -> Self { match val { - HighResolutionControlPrescaler::Div1 => 0b000, - HighResolutionControlPrescaler::Div2 => 0b001, - HighResolutionControlPrescaler::Div4 => 0b010, - HighResolutionControlPrescaler::Div8 => 0b011, - HighResolutionControlPrescaler::Div16 => 0b100, - HighResolutionControlPrescaler::Div32 => 0b101, - HighResolutionControlPrescaler::Div64 => 0b110, - HighResolutionControlPrescaler::Div128 => 0b111, + Prescaler::Div1 => 0b000, + Prescaler::Div2 => 0b001, + Prescaler::Div4 => 0b010, + Prescaler::Div8 => 0b011, + Prescaler::Div16 => 0b100, + Prescaler::Div32 => 0b101, + Prescaler::Div64 => 0b110, + Prescaler::Div128 => 0b111, } } } -impl From for HighResolutionControlPrescaler { +impl From for Prescaler { fn from(val: u8) -> Self { match val { - 0b000 => HighResolutionControlPrescaler::Div1, - 0b001 => HighResolutionControlPrescaler::Div2, - 0b010 => HighResolutionControlPrescaler::Div4, - 0b011 => HighResolutionControlPrescaler::Div8, - 0b100 => HighResolutionControlPrescaler::Div16, - 0b101 => HighResolutionControlPrescaler::Div32, - 0b110 => HighResolutionControlPrescaler::Div64, - 0b111 => HighResolutionControlPrescaler::Div128, + 0b000 => Prescaler::Div1, + 0b001 => Prescaler::Div2, + 0b010 => Prescaler::Div4, + 0b011 => Prescaler::Div8, + 0b100 => Prescaler::Div16, + 0b101 => Prescaler::Div32, + 0b110 => Prescaler::Div64, + 0b111 => Prescaler::Div128, _ => unreachable!(), } } } -impl HighResolutionControlPrescaler { +impl Prescaler { pub fn compute_min_high_res(val: u32) -> Self { *[ - HighResolutionControlPrescaler::Div1, - HighResolutionControlPrescaler::Div2, - HighResolutionControlPrescaler::Div4, - HighResolutionControlPrescaler::Div8, - HighResolutionControlPrescaler::Div16, - HighResolutionControlPrescaler::Div32, - HighResolutionControlPrescaler::Div64, - HighResolutionControlPrescaler::Div128, + Prescaler::Div1, + Prescaler::Div2, + Prescaler::Div4, + Prescaler::Div8, + Prescaler::Div16, + Prescaler::Div32, + Prescaler::Div64, + Prescaler::Div128, ] .iter() - .skip_while(|psc| >::into(**psc) <= val) + .skip_while(|psc| >::into(**psc) <= val) .next() .unwrap() } pub fn compute_min_low_res(val: u32) -> Self { - *[ - HighResolutionControlPrescaler::Div32, - HighResolutionControlPrescaler::Div64, - HighResolutionControlPrescaler::Div128, - ] - .iter() - .skip_while(|psc| >::into(**psc) <= val) - .next() - .unwrap() + *[Prescaler::Div32, Prescaler::Div64, Prescaler::Div128] + .iter() + .skip_while(|psc| >::into(**psc) <= val) + .next() + .unwrap() } } pub(crate) mod sealed { use super::*; - pub trait HighResolutionCaptureCompare16bitInstance: RccPeripheral { + pub trait Instance: RccPeripheral { fn regs() -> crate::pac::hrtim::Hrtim; fn set_master_frequency(frequency: Hertz); @@ -109,14 +105,11 @@ pub(crate) mod sealed { } } -pub trait HighResolutionCaptureCompare16bitInstance: - sealed::HighResolutionCaptureCompare16bitInstance + 'static -{ -} +pub trait Instance: sealed::Instance + 'static {} foreach_interrupt! { ($inst:ident, hrtim, HRTIM, MASTER, $irq:ident) => { - impl sealed::HighResolutionCaptureCompare16bitInstance for crate::peripherals::$inst { + impl sealed::Instance for crate::peripherals::$inst { fn regs() -> crate::pac::hrtim::Hrtim { crate::pac::$inst } @@ -128,9 +121,9 @@ foreach_interrupt! { let timer_f = Self::frequency().0; let psc_min = (timer_f / f) / (u16::MAX as u32 / 32); let psc = if Self::regs().isr().read().dllrdy() { - HighResolutionControlPrescaler::compute_min_high_res(psc_min) + Prescaler::compute_min_high_res(psc_min) } else { - HighResolutionControlPrescaler::compute_min_low_res(psc_min) + Prescaler::compute_min_low_res(psc_min) }; let psc_val: u32 = psc.into(); @@ -150,9 +143,9 @@ foreach_interrupt! { let timer_f = Self::frequency().0; let psc_min = (timer_f / f) / (u16::MAX as u32 / 32); let psc = if Self::regs().isr().read().dllrdy() { - HighResolutionControlPrescaler::compute_min_high_res(psc_min) + Prescaler::compute_min_high_res(psc_min) } else { - HighResolutionControlPrescaler::compute_min_low_res(psc_min) + Prescaler::compute_min_low_res(psc_min) }; let psc_val: u32 = psc.into(); @@ -169,7 +162,7 @@ foreach_interrupt! { let regs = Self::regs(); - let channel_psc: HighResolutionControlPrescaler = regs.tim(channel).cr().read().ckpsc().into(); + let channel_psc: Prescaler = regs.tim(channel).cr().read().ckpsc().into(); let psc_val: u32 = channel_psc.into(); @@ -177,9 +170,9 @@ foreach_interrupt! { // u9::MAX = 511 let psc_min = (psc_val * dead_time as u32) / (4 * 511); let psc = if Self::regs().isr().read().dllrdy() { - HighResolutionControlPrescaler::compute_min_high_res(psc_min) + Prescaler::compute_min_high_res(psc_min) } else { - HighResolutionControlPrescaler::compute_min_low_res(psc_min) + Prescaler::compute_min_low_res(psc_min) }; let dt_psc_val: u32 = psc.into(); @@ -193,7 +186,7 @@ foreach_interrupt! { } } - impl HighResolutionCaptureCompare16bitInstance for crate::peripherals::$inst { + impl Instance for crate::peripherals::$inst { } };