diff options
| author | xoviat <[email protected]> | 2023-04-05 17:50:23 -0500 |
|---|---|---|
| committer | xoviat <[email protected]> | 2023-04-05 17:50:23 -0500 |
| commit | 76772683191be15d32604ec5dd46fb5eb3949de8 (patch) | |
| tree | 610b84b73d52ac7586eb52b859c56474cb74a521 | |
| parent | 991b22b6a1e845dc15eca72bf9881e60f1803840 (diff) | |
stm32/pwm: cleanup and fix complementary pwm
| -rw-r--r-- | embassy-stm32/src/pwm/complementary_pwm.rs | 47 | ||||
| -rw-r--r-- | embassy-stm32/src/pwm/mod.rs | 18 |
2 files changed, 31 insertions, 34 deletions
diff --git a/embassy-stm32/src/pwm/complementary_pwm.rs b/embassy-stm32/src/pwm/complementary_pwm.rs index b8761724a..e4de1fb7a 100644 --- a/embassy-stm32/src/pwm/complementary_pwm.rs +++ b/embassy-stm32/src/pwm/complementary_pwm.rs | |||
| @@ -1,7 +1,9 @@ | |||
| 1 | use core::marker::PhantomData; | 1 | use core::marker::PhantomData; |
| 2 | 2 | ||
| 3 | use embassy_hal_common::{into_ref, PeripheralRef}; | 3 | use embassy_hal_common::{into_ref, PeripheralRef}; |
| 4 | use stm32_metapac::timer::vals::Ckd; | ||
| 4 | 5 | ||
| 6 | use super::simple_pwm::*; | ||
| 5 | use super::*; | 7 | use super::*; |
| 6 | #[allow(unused_imports)] | 8 | #[allow(unused_imports)] |
| 7 | use crate::gpio::sealed::{AFType, Pin}; | 9 | use crate::gpio::sealed::{AFType, Pin}; |
| @@ -9,39 +11,13 @@ use crate::gpio::AnyPin; | |||
| 9 | use crate::time::Hertz; | 11 | use crate::time::Hertz; |
| 10 | use crate::Peripheral; | 12 | use crate::Peripheral; |
| 11 | 13 | ||
| 12 | pub struct Ch1; | ||
| 13 | pub struct Ch2; | ||
| 14 | pub struct Ch3; | ||
| 15 | pub struct Ch4; | ||
| 16 | |||
| 17 | pub struct PwmPin<'d, Perip, Channel> { | ||
| 18 | _pin: PeripheralRef<'d, AnyPin>, | ||
| 19 | phantom: PhantomData<(Perip, Channel)>, | ||
| 20 | } | ||
| 21 | |||
| 22 | pub struct ComplementaryPwmPin<'d, Perip, Channel> { | 14 | pub struct ComplementaryPwmPin<'d, Perip, Channel> { |
| 23 | _pin: PeripheralRef<'d, AnyPin>, | 15 | _pin: PeripheralRef<'d, AnyPin>, |
| 24 | phantom: PhantomData<(Perip, Channel)>, | 16 | phantom: PhantomData<(Perip, Channel)>, |
| 25 | } | 17 | } |
| 26 | 18 | ||
| 27 | macro_rules! channel_impl { | 19 | macro_rules! complementary_channel_impl { |
| 28 | ($new_chx:ident, $channel:ident, $pin_trait:ident, $complementary_pin_trait:ident) => { | 20 | ($new_chx:ident, $channel:ident, $pin_trait:ident, $complementary_pin_trait:ident) => { |
| 29 | impl<'d, Perip: CaptureCompare16bitInstance> PwmPin<'d, Perip, $channel> { | ||
| 30 | pub fn $new_chx(pin: impl Peripheral<P = impl $pin_trait<Perip>> + 'd) -> Self { | ||
| 31 | into_ref!(pin); | ||
| 32 | critical_section::with(|_| unsafe { | ||
| 33 | pin.set_low(); | ||
| 34 | pin.set_as_af(pin.af_num(), AFType::OutputPushPull); | ||
| 35 | #[cfg(gpio_v2)] | ||
| 36 | pin.set_speed(crate::gpio::Speed::VeryHigh); | ||
| 37 | }); | ||
| 38 | PwmPin { | ||
| 39 | _pin: pin.map_into(), | ||
| 40 | phantom: PhantomData, | ||
| 41 | } | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | impl<'d, Perip: CaptureCompare16bitInstance> ComplementaryPwmPin<'d, Perip, $channel> { | 21 | impl<'d, Perip: CaptureCompare16bitInstance> ComplementaryPwmPin<'d, Perip, $channel> { |
| 46 | pub fn $new_chx(pin: impl Peripheral<P = impl $complementary_pin_trait<Perip>> + 'd) -> Self { | 22 | pub fn $new_chx(pin: impl Peripheral<P = impl $complementary_pin_trait<Perip>> + 'd) -> Self { |
| 47 | into_ref!(pin); | 23 | into_ref!(pin); |
| @@ -60,10 +36,10 @@ macro_rules! channel_impl { | |||
| 60 | }; | 36 | }; |
| 61 | } | 37 | } |
| 62 | 38 | ||
| 63 | channel_impl!(new_ch1, Ch1, Channel1Pin, Channel1ComplementaryPin); | 39 | complementary_channel_impl!(new_ch1, Ch1, Channel1Pin, Channel1ComplementaryPin); |
| 64 | channel_impl!(new_ch2, Ch2, Channel2Pin, Channel2ComplementaryPin); | 40 | complementary_channel_impl!(new_ch2, Ch2, Channel2Pin, Channel2ComplementaryPin); |
| 65 | channel_impl!(new_ch3, Ch3, Channel3Pin, Channel3ComplementaryPin); | 41 | complementary_channel_impl!(new_ch3, Ch3, Channel3Pin, Channel3ComplementaryPin); |
| 66 | channel_impl!(new_ch4, Ch4, Channel4Pin, Channel4ComplementaryPin); | 42 | complementary_channel_impl!(new_ch4, Ch4, Channel4Pin, Channel4ComplementaryPin); |
| 67 | 43 | ||
| 68 | pub struct ComplementaryPwm<'d, T> { | 44 | pub struct ComplementaryPwm<'d, T> { |
| 69 | inner: PeripheralRef<'d, T>, | 45 | inner: PeripheralRef<'d, T>, |
| @@ -114,11 +90,13 @@ impl<'d, T: ComplementaryCaptureCompare16bitInstance> ComplementaryPwm<'d, T> { | |||
| 114 | pub fn enable(&mut self, channel: Channel) { | 90 | pub fn enable(&mut self, channel: Channel) { |
| 115 | unsafe { | 91 | unsafe { |
| 116 | self.inner.enable_channel(channel, true); | 92 | self.inner.enable_channel(channel, true); |
| 93 | self.inner.enable_complementary_channel(channel, true); | ||
| 117 | } | 94 | } |
| 118 | } | 95 | } |
| 119 | 96 | ||
| 120 | pub fn disable(&mut self, channel: Channel) { | 97 | pub fn disable(&mut self, channel: Channel) { |
| 121 | unsafe { | 98 | unsafe { |
| 99 | self.inner.enable_complementary_channel(channel, false); | ||
| 122 | self.inner.enable_channel(channel, false); | 100 | self.inner.enable_channel(channel, false); |
| 123 | } | 101 | } |
| 124 | } | 102 | } |
| @@ -136,9 +114,10 @@ impl<'d, T: ComplementaryCaptureCompare16bitInstance> ComplementaryPwm<'d, T> { | |||
| 136 | unsafe { self.inner.set_compare_value(channel, duty) } | 114 | unsafe { self.inner.set_compare_value(channel, duty) } |
| 137 | } | 115 | } |
| 138 | 116 | ||
| 139 | /* | 117 | pub fn set_dead_time_clock_division(&mut self, value: Ckd) { |
| 140 | set the value of the dead-time register | 118 | unsafe { self.inner.set_dead_time_clock_division(value) } |
| 141 | */ | 119 | } |
| 120 | |||
| 142 | pub fn set_dead_time_value(&mut self, value: u8) { | 121 | pub fn set_dead_time_value(&mut self, value: u8) { |
| 143 | unsafe { self.inner.set_dead_time_value(value) } | 122 | unsafe { self.inner.set_dead_time_value(value) } |
| 144 | } | 123 | } |
diff --git a/embassy-stm32/src/pwm/mod.rs b/embassy-stm32/src/pwm/mod.rs index 6f3c12665..0bef07089 100644 --- a/embassy-stm32/src/pwm/mod.rs +++ b/embassy-stm32/src/pwm/mod.rs | |||
| @@ -1,6 +1,8 @@ | |||
| 1 | pub mod complementary_pwm; | 1 | pub mod complementary_pwm; |
| 2 | pub mod simple_pwm; | 2 | pub mod simple_pwm; |
| 3 | 3 | ||
| 4 | use stm32_metapac::timer::vals::Ckd; | ||
| 5 | |||
| 4 | #[cfg(feature = "unstable-pac")] | 6 | #[cfg(feature = "unstable-pac")] |
| 5 | pub mod low_level { | 7 | pub mod low_level { |
| 6 | pub use super::sealed::*; | 8 | pub use super::sealed::*; |
| @@ -69,7 +71,11 @@ pub(crate) mod sealed { | |||
| 69 | } | 71 | } |
| 70 | 72 | ||
| 71 | pub trait ComplementaryCaptureCompare16bitInstance: CaptureCompare16bitInstance { | 73 | pub trait ComplementaryCaptureCompare16bitInstance: CaptureCompare16bitInstance { |
| 74 | unsafe fn set_dead_time_clock_division(&mut self, value: Ckd); | ||
| 75 | |||
| 72 | unsafe fn set_dead_time_value(&mut self, value: u8); | 76 | unsafe fn set_dead_time_value(&mut self, value: u8); |
| 77 | |||
| 78 | unsafe fn enable_complementary_channel(&mut self, channel: Channel, enable: bool); | ||
| 73 | } | 79 | } |
| 74 | 80 | ||
| 75 | pub trait CaptureCompare32bitInstance: crate::timer::sealed::GeneralPurpose32bitInstance { | 81 | pub trait CaptureCompare32bitInstance: crate::timer::sealed::GeneralPurpose32bitInstance { |
| @@ -222,10 +228,22 @@ foreach_interrupt! { | |||
| 222 | } | 228 | } |
| 223 | 229 | ||
| 224 | impl crate::pwm::sealed::ComplementaryCaptureCompare16bitInstance for crate::peripherals::$inst { | 230 | impl crate::pwm::sealed::ComplementaryCaptureCompare16bitInstance for crate::peripherals::$inst { |
| 231 | unsafe fn set_dead_time_clock_division(&mut self, value: Ckd) { | ||
| 232 | use crate::timer::sealed::AdvancedControlInstance; | ||
| 233 | Self::regs_advanced().cr1().modify(|w| w.set_ckd(value)); | ||
| 234 | } | ||
| 235 | |||
| 225 | unsafe fn set_dead_time_value(&mut self, value: u8) { | 236 | unsafe fn set_dead_time_value(&mut self, value: u8) { |
| 226 | use crate::timer::sealed::AdvancedControlInstance; | 237 | use crate::timer::sealed::AdvancedControlInstance; |
| 227 | Self::regs_advanced().bdtr().modify(|w| w.set_dtg(value)); | 238 | Self::regs_advanced().bdtr().modify(|w| w.set_dtg(value)); |
| 228 | } | 239 | } |
| 240 | |||
| 241 | unsafe fn enable_complementary_channel(&mut self, channel: Channel, enable: bool) { | ||
| 242 | use crate::timer::sealed::AdvancedControlInstance; | ||
| 243 | Self::regs_advanced() | ||
| 244 | .ccer() | ||
| 245 | .modify(|w| w.set_ccne(channel.raw(), enable)); | ||
| 246 | } | ||
| 229 | } | 247 | } |
| 230 | 248 | ||
| 231 | impl ComplementaryCaptureCompare16bitInstance for crate::peripherals::$inst { | 249 | impl ComplementaryCaptureCompare16bitInstance for crate::peripherals::$inst { |
