diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-08-18 14:51:48 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-08-18 14:51:48 +0000 |
| commit | 97da34595cb9943affadb1ce8785556108b977df (patch) | |
| tree | c52cbc37ffa275e4da63e1a8d3c2762d01119ac7 | |
| parent | 8754a1d378defcd9496c6e01cfbb4cacc63163e7 (diff) | |
| parent | 2ea17d27836d98f55659e23502c91a33e2c06d29 (diff) | |
Merge pull request #1798 from aurelj/stm32_pwm_polarity
stm32: allow setting the PWM output polarity
| -rw-r--r-- | embassy-stm32/src/timer/complementary_pwm.rs | 5 | ||||
| -rw-r--r-- | embassy-stm32/src/timer/mod.rs | 49 | ||||
| -rw-r--r-- | embassy-stm32/src/timer/simple_pwm.rs | 4 |
3 files changed, 58 insertions, 0 deletions
diff --git a/embassy-stm32/src/timer/complementary_pwm.rs b/embassy-stm32/src/timer/complementary_pwm.rs index 533267cf7..4f033e3a2 100644 --- a/embassy-stm32/src/timer/complementary_pwm.rs +++ b/embassy-stm32/src/timer/complementary_pwm.rs | |||
| @@ -108,6 +108,11 @@ impl<'d, T: ComplementaryCaptureCompare16bitInstance> ComplementaryPwm<'d, T> { | |||
| 108 | self.inner.set_compare_value(channel, duty) | 108 | self.inner.set_compare_value(channel, duty) |
| 109 | } | 109 | } |
| 110 | 110 | ||
| 111 | pub fn set_polarity(&mut self, channel: Channel, polarity: OutputPolarity) { | ||
| 112 | self.inner.set_output_polarity(channel, polarity); | ||
| 113 | self.inner.set_complementary_output_polarity(channel, polarity); | ||
| 114 | } | ||
| 115 | |||
| 111 | /// Set the dead time as a proportion of max_duty | 116 | /// Set the dead time as a proportion of max_duty |
| 112 | pub fn set_dead_time(&mut self, value: u16) { | 117 | pub fn set_dead_time(&mut self, value: u16) { |
| 113 | let (ckd, value) = compute_dead_time_value(value); | 118 | let (ckd, value) = compute_dead_time_value(value); |
diff --git a/embassy-stm32/src/timer/mod.rs b/embassy-stm32/src/timer/mod.rs index 6c2d6d827..4ffb2a289 100644 --- a/embassy-stm32/src/timer/mod.rs +++ b/embassy-stm32/src/timer/mod.rs | |||
| @@ -53,6 +53,8 @@ pub(crate) mod sealed { | |||
| 53 | 53 | ||
| 54 | fn set_output_compare_mode(&mut self, channel: Channel, mode: OutputCompareMode); | 54 | fn set_output_compare_mode(&mut self, channel: Channel, mode: OutputCompareMode); |
| 55 | 55 | ||
| 56 | fn set_output_polarity(&mut self, channel: Channel, polarity: OutputPolarity); | ||
| 57 | |||
| 56 | fn enable_channel(&mut self, channel: Channel, enable: bool); | 58 | fn enable_channel(&mut self, channel: Channel, enable: bool); |
| 57 | 59 | ||
| 58 | fn set_compare_value(&mut self, channel: Channel, value: u16); | 60 | fn set_compare_value(&mut self, channel: Channel, value: u16); |
| @@ -61,6 +63,8 @@ pub(crate) mod sealed { | |||
| 61 | } | 63 | } |
| 62 | 64 | ||
| 63 | pub trait ComplementaryCaptureCompare16bitInstance: CaptureCompare16bitInstance { | 65 | pub trait ComplementaryCaptureCompare16bitInstance: CaptureCompare16bitInstance { |
| 66 | fn set_complementary_output_polarity(&mut self, channel: Channel, polarity: OutputPolarity); | ||
| 67 | |||
| 64 | fn set_dead_time_clock_division(&mut self, value: vals::Ckd); | 68 | fn set_dead_time_clock_division(&mut self, value: vals::Ckd); |
| 65 | 69 | ||
| 66 | fn set_dead_time_value(&mut self, value: u8); | 70 | fn set_dead_time_value(&mut self, value: u8); |
| @@ -71,6 +75,8 @@ pub(crate) mod sealed { | |||
| 71 | pub trait CaptureCompare32bitInstance: GeneralPurpose32bitInstance { | 75 | pub trait CaptureCompare32bitInstance: GeneralPurpose32bitInstance { |
| 72 | fn set_output_compare_mode(&mut self, channel: Channel, mode: OutputCompareMode); | 76 | fn set_output_compare_mode(&mut self, channel: Channel, mode: OutputCompareMode); |
| 73 | 77 | ||
| 78 | fn set_output_polarity(&mut self, channel: Channel, polarity: OutputPolarity); | ||
| 79 | |||
| 74 | fn enable_channel(&mut self, channel: Channel, enable: bool); | 80 | fn enable_channel(&mut self, channel: Channel, enable: bool); |
| 75 | 81 | ||
| 76 | fn set_compare_value(&mut self, channel: Channel, value: u32); | 82 | fn set_compare_value(&mut self, channel: Channel, value: u32); |
| @@ -125,6 +131,21 @@ impl From<OutputCompareMode> for stm32_metapac::timer::vals::Ocm { | |||
| 125 | } | 131 | } |
| 126 | } | 132 | } |
| 127 | 133 | ||
| 134 | #[derive(Clone, Copy)] | ||
| 135 | pub enum OutputPolarity { | ||
| 136 | ActiveHigh, | ||
| 137 | ActiveLow, | ||
| 138 | } | ||
| 139 | |||
| 140 | impl From<OutputPolarity> for bool { | ||
| 141 | fn from(mode: OutputPolarity) -> Self { | ||
| 142 | match mode { | ||
| 143 | OutputPolarity::ActiveHigh => false, | ||
| 144 | OutputPolarity::ActiveLow => true, | ||
| 145 | } | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 128 | pub trait Basic16bitInstance: sealed::Basic16bitInstance + 'static {} | 149 | pub trait Basic16bitInstance: sealed::Basic16bitInstance + 'static {} |
| 129 | 150 | ||
| 130 | pub trait GeneralPurpose16bitInstance: sealed::GeneralPurpose16bitInstance + 'static {} | 151 | pub trait GeneralPurpose16bitInstance: sealed::GeneralPurpose16bitInstance + 'static {} |
| @@ -265,6 +286,13 @@ macro_rules! impl_compare_capable_16bit { | |||
| 265 | .modify(|w| w.set_ocm(raw_channel % 2, mode.into())); | 286 | .modify(|w| w.set_ocm(raw_channel % 2, mode.into())); |
| 266 | } | 287 | } |
| 267 | 288 | ||
| 289 | fn set_output_polarity(&mut self, channel: Channel, polarity: OutputPolarity) { | ||
| 290 | use sealed::GeneralPurpose16bitInstance; | ||
| 291 | Self::regs_gp16() | ||
| 292 | .ccer() | ||
| 293 | .modify(|w| w.set_ccp(channel.raw(), polarity.into())); | ||
| 294 | } | ||
| 295 | |||
| 268 | fn enable_channel(&mut self, channel: Channel, enable: bool) { | 296 | fn enable_channel(&mut self, channel: Channel, enable: bool) { |
| 269 | use sealed::GeneralPurpose16bitInstance; | 297 | use sealed::GeneralPurpose16bitInstance; |
| 270 | Self::regs_gp16() | 298 | Self::regs_gp16() |
| @@ -325,6 +353,13 @@ foreach_interrupt! { | |||
| 325 | Self::regs_gp32().ccmr_output(raw_channel / 2).modify(|w| w.set_ocm(raw_channel % 2, mode.into())); | 353 | Self::regs_gp32().ccmr_output(raw_channel / 2).modify(|w| w.set_ocm(raw_channel % 2, mode.into())); |
| 326 | } | 354 | } |
| 327 | 355 | ||
| 356 | fn set_output_polarity(&mut self, channel: Channel, polarity: OutputPolarity) { | ||
| 357 | use crate::timer::sealed::GeneralPurpose32bitInstance; | ||
| 358 | Self::regs_gp32() | ||
| 359 | .ccer() | ||
| 360 | .modify(|w| w.set_ccp(channel.raw(), polarity.into())); | ||
| 361 | } | ||
| 362 | |||
| 328 | fn enable_channel(&mut self, channel: Channel, enable: bool) { | 363 | fn enable_channel(&mut self, channel: Channel, enable: bool) { |
| 329 | use crate::timer::sealed::GeneralPurpose32bitInstance; | 364 | use crate::timer::sealed::GeneralPurpose32bitInstance; |
| 330 | Self::regs_gp32().ccer().modify(|w| w.set_cce(channel.raw(), enable)); | 365 | Self::regs_gp32().ccer().modify(|w| w.set_cce(channel.raw(), enable)); |
| @@ -388,6 +423,13 @@ foreach_interrupt! { | |||
| 388 | .modify(|w| w.set_ocm(raw_channel % 2, mode.into())); | 423 | .modify(|w| w.set_ocm(raw_channel % 2, mode.into())); |
| 389 | } | 424 | } |
| 390 | 425 | ||
| 426 | fn set_output_polarity(&mut self, channel: Channel, polarity: OutputPolarity) { | ||
| 427 | use crate::timer::sealed::AdvancedControlInstance; | ||
| 428 | Self::regs_advanced() | ||
| 429 | .ccer() | ||
| 430 | .modify(|w| w.set_ccp(channel.raw(), polarity.into())); | ||
| 431 | } | ||
| 432 | |||
| 391 | fn enable_channel(&mut self, channel: Channel, enable: bool) { | 433 | fn enable_channel(&mut self, channel: Channel, enable: bool) { |
| 392 | use crate::timer::sealed::AdvancedControlInstance; | 434 | use crate::timer::sealed::AdvancedControlInstance; |
| 393 | Self::regs_advanced() | 435 | Self::regs_advanced() |
| @@ -409,6 +451,13 @@ foreach_interrupt! { | |||
| 409 | } | 451 | } |
| 410 | 452 | ||
| 411 | impl sealed::ComplementaryCaptureCompare16bitInstance for crate::peripherals::$inst { | 453 | impl sealed::ComplementaryCaptureCompare16bitInstance for crate::peripherals::$inst { |
| 454 | fn set_complementary_output_polarity(&mut self, channel: Channel, polarity: OutputPolarity) { | ||
| 455 | use crate::timer::sealed::AdvancedControlInstance; | ||
| 456 | Self::regs_advanced() | ||
| 457 | .ccer() | ||
| 458 | .modify(|w| w.set_ccnp(channel.raw(), polarity.into())); | ||
| 459 | } | ||
| 460 | |||
| 412 | fn set_dead_time_clock_division(&mut self, value: vals::Ckd) { | 461 | fn set_dead_time_clock_division(&mut self, value: vals::Ckd) { |
| 413 | use crate::timer::sealed::AdvancedControlInstance; | 462 | use crate::timer::sealed::AdvancedControlInstance; |
| 414 | Self::regs_advanced().cr1().modify(|w| w.set_ckd(value)); | 463 | Self::regs_advanced().cr1().modify(|w| w.set_ckd(value)); |
diff --git a/embassy-stm32/src/timer/simple_pwm.rs b/embassy-stm32/src/timer/simple_pwm.rs index 40e3dd1bd..9e28878b1 100644 --- a/embassy-stm32/src/timer/simple_pwm.rs +++ b/embassy-stm32/src/timer/simple_pwm.rs | |||
| @@ -104,4 +104,8 @@ impl<'d, T: CaptureCompare16bitInstance> SimplePwm<'d, T> { | |||
| 104 | assert!(duty <= self.get_max_duty()); | 104 | assert!(duty <= self.get_max_duty()); |
| 105 | self.inner.set_compare_value(channel, duty) | 105 | self.inner.set_compare_value(channel, duty) |
| 106 | } | 106 | } |
| 107 | |||
| 108 | pub fn set_polarity(&mut self, channel: Channel, polarity: OutputPolarity) { | ||
| 109 | self.inner.set_output_polarity(channel, polarity); | ||
| 110 | } | ||
| 107 | } | 111 | } |
