aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxoviat <[email protected]>2023-04-05 17:50:23 -0500
committerxoviat <[email protected]>2023-04-05 17:50:23 -0500
commit76772683191be15d32604ec5dd46fb5eb3949de8 (patch)
tree610b84b73d52ac7586eb52b859c56474cb74a521
parent991b22b6a1e845dc15eca72bf9881e60f1803840 (diff)
stm32/pwm: cleanup and fix complementary pwm
-rw-r--r--embassy-stm32/src/pwm/complementary_pwm.rs47
-rw-r--r--embassy-stm32/src/pwm/mod.rs18
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 @@
1use core::marker::PhantomData; 1use core::marker::PhantomData;
2 2
3use embassy_hal_common::{into_ref, PeripheralRef}; 3use embassy_hal_common::{into_ref, PeripheralRef};
4use stm32_metapac::timer::vals::Ckd;
4 5
6use super::simple_pwm::*;
5use super::*; 7use super::*;
6#[allow(unused_imports)] 8#[allow(unused_imports)]
7use crate::gpio::sealed::{AFType, Pin}; 9use crate::gpio::sealed::{AFType, Pin};
@@ -9,39 +11,13 @@ use crate::gpio::AnyPin;
9use crate::time::Hertz; 11use crate::time::Hertz;
10use crate::Peripheral; 12use crate::Peripheral;
11 13
12pub struct Ch1;
13pub struct Ch2;
14pub struct Ch3;
15pub struct Ch4;
16
17pub struct PwmPin<'d, Perip, Channel> {
18 _pin: PeripheralRef<'d, AnyPin>,
19 phantom: PhantomData<(Perip, Channel)>,
20}
21
22pub struct ComplementaryPwmPin<'d, Perip, Channel> { 14pub 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
27macro_rules! channel_impl { 19macro_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
63channel_impl!(new_ch1, Ch1, Channel1Pin, Channel1ComplementaryPin); 39complementary_channel_impl!(new_ch1, Ch1, Channel1Pin, Channel1ComplementaryPin);
64channel_impl!(new_ch2, Ch2, Channel2Pin, Channel2ComplementaryPin); 40complementary_channel_impl!(new_ch2, Ch2, Channel2Pin, Channel2ComplementaryPin);
65channel_impl!(new_ch3, Ch3, Channel3Pin, Channel3ComplementaryPin); 41complementary_channel_impl!(new_ch3, Ch3, Channel3Pin, Channel3ComplementaryPin);
66channel_impl!(new_ch4, Ch4, Channel4Pin, Channel4ComplementaryPin); 42complementary_channel_impl!(new_ch4, Ch4, Channel4Pin, Channel4ComplementaryPin);
67 43
68pub struct ComplementaryPwm<'d, T> { 44pub 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 @@
1pub mod complementary_pwm; 1pub mod complementary_pwm;
2pub mod simple_pwm; 2pub mod simple_pwm;
3 3
4use stm32_metapac::timer::vals::Ckd;
5
4#[cfg(feature = "unstable-pac")] 6#[cfg(feature = "unstable-pac")]
5pub mod low_level { 7pub 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 {