diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-07-30 12:15:47 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-07-30 12:15:47 +0000 |
| commit | 2a004251a780ba03d38ad0a09408c104f3412e7e (patch) | |
| tree | a73e71c04629d91950b308970f8e8219e4bf2ca3 | |
| parent | fcbfd224a729c38d5ff94d94a25321a819254630 (diff) | |
| parent | a9f6e30bcdd3d288f1fba71311c9ae1f67f2d25a (diff) | |
Merge pull request #1712 from xoviat/pwm
stm32/pwm: add output type control
| -rw-r--r-- | embassy-stm32/src/gpio.rs | 14 | ||||
| -rw-r--r-- | embassy-stm32/src/timer/complementary_pwm.rs | 16 | ||||
| -rw-r--r-- | embassy-stm32/src/timer/simple_pwm.rs | 6 | ||||
| -rw-r--r-- | examples/stm32f4/src/bin/pwm.rs | 3 | ||||
| -rw-r--r-- | examples/stm32f4/src/bin/pwm_complementary.rs | 5 | ||||
| -rw-r--r-- | examples/stm32g4/src/bin/pwm.rs | 3 | ||||
| -rw-r--r-- | examples/stm32h7/src/bin/pwm.rs | 3 |
7 files changed, 34 insertions, 16 deletions
diff --git a/embassy-stm32/src/gpio.rs b/embassy-stm32/src/gpio.rs index cda597145..0cc269cfd 100644 --- a/embassy-stm32/src/gpio.rs +++ b/embassy-stm32/src/gpio.rs | |||
| @@ -502,6 +502,20 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { | |||
| 502 | } | 502 | } |
| 503 | } | 503 | } |
| 504 | 504 | ||
| 505 | pub enum OutputType { | ||
| 506 | PushPull, | ||
| 507 | OpenDrain, | ||
| 508 | } | ||
| 509 | |||
| 510 | impl From<OutputType> for sealed::AFType { | ||
| 511 | fn from(value: OutputType) -> Self { | ||
| 512 | match value { | ||
| 513 | OutputType::OpenDrain => sealed::AFType::OutputOpenDrain, | ||
| 514 | OutputType::PushPull => sealed::AFType::OutputPushPull, | ||
| 515 | } | ||
| 516 | } | ||
| 517 | } | ||
| 518 | |||
| 505 | pub(crate) mod sealed { | 519 | pub(crate) mod sealed { |
| 506 | use super::*; | 520 | use super::*; |
| 507 | 521 | ||
diff --git a/embassy-stm32/src/timer/complementary_pwm.rs b/embassy-stm32/src/timer/complementary_pwm.rs index 64bb32c39..48cb610f1 100644 --- a/embassy-stm32/src/timer/complementary_pwm.rs +++ b/embassy-stm32/src/timer/complementary_pwm.rs | |||
| @@ -7,7 +7,7 @@ use super::simple_pwm::*; | |||
| 7 | use super::*; | 7 | use super::*; |
| 8 | #[allow(unused_imports)] | 8 | #[allow(unused_imports)] |
| 9 | use crate::gpio::sealed::{AFType, Pin}; | 9 | use crate::gpio::sealed::{AFType, Pin}; |
| 10 | use crate::gpio::AnyPin; | 10 | use crate::gpio::{AnyPin, OutputType}; |
| 11 | use crate::time::Hertz; | 11 | use crate::time::Hertz; |
| 12 | use crate::Peripheral; | 12 | use crate::Peripheral; |
| 13 | 13 | ||
| @@ -17,13 +17,13 @@ pub struct ComplementaryPwmPin<'d, Perip, Channel> { | |||
| 17 | } | 17 | } |
| 18 | 18 | ||
| 19 | macro_rules! complementary_channel_impl { | 19 | macro_rules! complementary_channel_impl { |
| 20 | ($new_chx:ident, $channel:ident, $pin_trait:ident, $complementary_pin_trait:ident) => { | 20 | ($new_chx:ident, $channel:ident, $pin_trait:ident) => { |
| 21 | impl<'d, Perip: CaptureCompare16bitInstance> ComplementaryPwmPin<'d, Perip, $channel> { | 21 | impl<'d, Perip: CaptureCompare16bitInstance> ComplementaryPwmPin<'d, Perip, $channel> { |
| 22 | 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 $pin_trait<Perip>> + 'd, output_type: OutputType) -> Self { |
| 23 | into_ref!(pin); | 23 | into_ref!(pin); |
| 24 | critical_section::with(|_| { | 24 | critical_section::with(|_| { |
| 25 | pin.set_low(); | 25 | pin.set_low(); |
| 26 | pin.set_as_af(pin.af_num(), AFType::OutputPushPull); | 26 | pin.set_as_af(pin.af_num(), output_type.into()); |
| 27 | #[cfg(gpio_v2)] | 27 | #[cfg(gpio_v2)] |
| 28 | pin.set_speed(crate::gpio::Speed::VeryHigh); | 28 | pin.set_speed(crate::gpio::Speed::VeryHigh); |
| 29 | }); | 29 | }); |
| @@ -36,10 +36,10 @@ macro_rules! complementary_channel_impl { | |||
| 36 | }; | 36 | }; |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | complementary_channel_impl!(new_ch1, Ch1, Channel1Pin, Channel1ComplementaryPin); | 39 | complementary_channel_impl!(new_ch1, Ch1, Channel1ComplementaryPin); |
| 40 | complementary_channel_impl!(new_ch2, Ch2, Channel2Pin, Channel2ComplementaryPin); | 40 | complementary_channel_impl!(new_ch2, Ch2, Channel2ComplementaryPin); |
| 41 | complementary_channel_impl!(new_ch3, Ch3, Channel3Pin, Channel3ComplementaryPin); | 41 | complementary_channel_impl!(new_ch3, Ch3, Channel3ComplementaryPin); |
| 42 | complementary_channel_impl!(new_ch4, Ch4, Channel4Pin, Channel4ComplementaryPin); | 42 | complementary_channel_impl!(new_ch4, Ch4, Channel4ComplementaryPin); |
| 43 | 43 | ||
| 44 | pub struct ComplementaryPwm<'d, T> { | 44 | pub struct ComplementaryPwm<'d, T> { |
| 45 | inner: PeripheralRef<'d, T>, | 45 | inner: PeripheralRef<'d, T>, |
diff --git a/embassy-stm32/src/timer/simple_pwm.rs b/embassy-stm32/src/timer/simple_pwm.rs index 514796930..e0a817929 100644 --- a/embassy-stm32/src/timer/simple_pwm.rs +++ b/embassy-stm32/src/timer/simple_pwm.rs | |||
| @@ -5,7 +5,7 @@ use embassy_hal_internal::{into_ref, PeripheralRef}; | |||
| 5 | use super::*; | 5 | use super::*; |
| 6 | #[allow(unused_imports)] | 6 | #[allow(unused_imports)] |
| 7 | use crate::gpio::sealed::{AFType, Pin}; | 7 | use crate::gpio::sealed::{AFType, Pin}; |
| 8 | use crate::gpio::AnyPin; | 8 | use crate::gpio::{AnyPin, OutputType}; |
| 9 | use crate::time::Hertz; | 9 | use crate::time::Hertz; |
| 10 | use crate::Peripheral; | 10 | use crate::Peripheral; |
| 11 | 11 | ||
| @@ -22,11 +22,11 @@ pub struct PwmPin<'d, Perip, Channel> { | |||
| 22 | macro_rules! channel_impl { | 22 | macro_rules! channel_impl { |
| 23 | ($new_chx:ident, $channel:ident, $pin_trait:ident) => { | 23 | ($new_chx:ident, $channel:ident, $pin_trait:ident) => { |
| 24 | impl<'d, Perip: CaptureCompare16bitInstance> PwmPin<'d, Perip, $channel> { | 24 | impl<'d, Perip: CaptureCompare16bitInstance> PwmPin<'d, Perip, $channel> { |
| 25 | pub fn $new_chx(pin: impl Peripheral<P = impl $pin_trait<Perip>> + 'd) -> Self { | 25 | pub fn $new_chx(pin: impl Peripheral<P = impl $pin_trait<Perip>> + 'd, output_type: OutputType) -> Self { |
| 26 | into_ref!(pin); | 26 | into_ref!(pin); |
| 27 | critical_section::with(|_| { | 27 | critical_section::with(|_| { |
| 28 | pin.set_low(); | 28 | pin.set_low(); |
| 29 | pin.set_as_af(pin.af_num(), AFType::OutputPushPull); | 29 | pin.set_as_af(pin.af_num(), output_type.into()); |
| 30 | #[cfg(gpio_v2)] | 30 | #[cfg(gpio_v2)] |
| 31 | pin.set_speed(crate::gpio::Speed::VeryHigh); | 31 | pin.set_speed(crate::gpio::Speed::VeryHigh); |
| 32 | }); | 32 | }); |
diff --git a/examples/stm32f4/src/bin/pwm.rs b/examples/stm32f4/src/bin/pwm.rs index 4f130c26b..1013a844e 100644 --- a/examples/stm32f4/src/bin/pwm.rs +++ b/examples/stm32f4/src/bin/pwm.rs | |||
| @@ -4,6 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | use defmt::*; | 5 | use defmt::*; |
| 6 | use embassy_executor::Spawner; | 6 | use embassy_executor::Spawner; |
| 7 | use embassy_stm32::gpio::OutputType; | ||
| 7 | use embassy_stm32::time::khz; | 8 | use embassy_stm32::time::khz; |
| 8 | use embassy_stm32::timer::simple_pwm::{PwmPin, SimplePwm}; | 9 | use embassy_stm32::timer::simple_pwm::{PwmPin, SimplePwm}; |
| 9 | use embassy_stm32::timer::Channel; | 10 | use embassy_stm32::timer::Channel; |
| @@ -15,7 +16,7 @@ async fn main(_spawner: Spawner) { | |||
| 15 | let p = embassy_stm32::init(Default::default()); | 16 | let p = embassy_stm32::init(Default::default()); |
| 16 | info!("Hello World!"); | 17 | info!("Hello World!"); |
| 17 | 18 | ||
| 18 | let ch1 = PwmPin::new_ch1(p.PE9); | 19 | let ch1 = PwmPin::new_ch1(p.PE9, OutputType::PushPull); |
| 19 | let mut pwm = SimplePwm::new(p.TIM1, Some(ch1), None, None, None, khz(10)); | 20 | let mut pwm = SimplePwm::new(p.TIM1, Some(ch1), None, None, None, khz(10)); |
| 20 | let max = pwm.get_max_duty(); | 21 | let max = pwm.get_max_duty(); |
| 21 | pwm.enable(Channel::Ch1); | 22 | pwm.enable(Channel::Ch1); |
diff --git a/examples/stm32f4/src/bin/pwm_complementary.rs b/examples/stm32f4/src/bin/pwm_complementary.rs index 8cc2a4117..83a3c7537 100644 --- a/examples/stm32f4/src/bin/pwm_complementary.rs +++ b/examples/stm32f4/src/bin/pwm_complementary.rs | |||
| @@ -4,6 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | use defmt::*; | 5 | use defmt::*; |
| 6 | use embassy_executor::Spawner; | 6 | use embassy_executor::Spawner; |
| 7 | use embassy_stm32::gpio::OutputType; | ||
| 7 | use embassy_stm32::time::khz; | 8 | use embassy_stm32::time::khz; |
| 8 | use embassy_stm32::timer::complementary_pwm::{ComplementaryPwm, ComplementaryPwmPin}; | 9 | use embassy_stm32::timer::complementary_pwm::{ComplementaryPwm, ComplementaryPwmPin}; |
| 9 | use embassy_stm32::timer::simple_pwm::PwmPin; | 10 | use embassy_stm32::timer::simple_pwm::PwmPin; |
| @@ -16,8 +17,8 @@ async fn main(_spawner: Spawner) { | |||
| 16 | let p = embassy_stm32::init(Default::default()); | 17 | let p = embassy_stm32::init(Default::default()); |
| 17 | info!("Hello World!"); | 18 | info!("Hello World!"); |
| 18 | 19 | ||
| 19 | let ch1 = PwmPin::new_ch1(p.PE9); | 20 | let ch1 = PwmPin::new_ch1(p.PE9, OutputType::PushPull); |
| 20 | let ch1n = ComplementaryPwmPin::new_ch1(p.PA7); | 21 | let ch1n = ComplementaryPwmPin::new_ch1(p.PA7, OutputType::PushPull); |
| 21 | let mut pwm = ComplementaryPwm::new( | 22 | let mut pwm = ComplementaryPwm::new( |
| 22 | p.TIM1, | 23 | p.TIM1, |
| 23 | Some(ch1), | 24 | Some(ch1), |
diff --git a/examples/stm32g4/src/bin/pwm.rs b/examples/stm32g4/src/bin/pwm.rs index b5a9b9952..01e9cb476 100644 --- a/examples/stm32g4/src/bin/pwm.rs +++ b/examples/stm32g4/src/bin/pwm.rs | |||
| @@ -4,6 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | use defmt::*; | 5 | use defmt::*; |
| 6 | use embassy_executor::Spawner; | 6 | use embassy_executor::Spawner; |
| 7 | use embassy_stm32::gpio::OutputType; | ||
| 7 | use embassy_stm32::time::khz; | 8 | use embassy_stm32::time::khz; |
| 8 | use embassy_stm32::timer::simple_pwm::{PwmPin, SimplePwm}; | 9 | use embassy_stm32::timer::simple_pwm::{PwmPin, SimplePwm}; |
| 9 | use embassy_stm32::timer::Channel; | 10 | use embassy_stm32::timer::Channel; |
| @@ -15,7 +16,7 @@ async fn main(_spawner: Spawner) { | |||
| 15 | let p = embassy_stm32::init(Default::default()); | 16 | let p = embassy_stm32::init(Default::default()); |
| 16 | info!("Hello World!"); | 17 | info!("Hello World!"); |
| 17 | 18 | ||
| 18 | let ch1 = PwmPin::new_ch1(p.PC0); | 19 | let ch1 = PwmPin::new_ch1(p.PC0, OutputType::PushPull); |
| 19 | let mut pwm = SimplePwm::new(p.TIM1, Some(ch1), None, None, None, khz(10)); | 20 | let mut pwm = SimplePwm::new(p.TIM1, Some(ch1), None, None, None, khz(10)); |
| 20 | let max = pwm.get_max_duty(); | 21 | let max = pwm.get_max_duty(); |
| 21 | pwm.enable(Channel::Ch1); | 22 | pwm.enable(Channel::Ch1); |
diff --git a/examples/stm32h7/src/bin/pwm.rs b/examples/stm32h7/src/bin/pwm.rs index adf2ea9ce..aa5ec1bcf 100644 --- a/examples/stm32h7/src/bin/pwm.rs +++ b/examples/stm32h7/src/bin/pwm.rs | |||
| @@ -4,6 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | use defmt::*; | 5 | use defmt::*; |
| 6 | use embassy_executor::Spawner; | 6 | use embassy_executor::Spawner; |
| 7 | use embassy_stm32::gpio::OutputType; | ||
| 7 | use embassy_stm32::time::{khz, mhz}; | 8 | use embassy_stm32::time::{khz, mhz}; |
| 8 | use embassy_stm32::timer::simple_pwm::{PwmPin, SimplePwm}; | 9 | use embassy_stm32::timer::simple_pwm::{PwmPin, SimplePwm}; |
| 9 | use embassy_stm32::timer::Channel; | 10 | use embassy_stm32::timer::Channel; |
| @@ -24,7 +25,7 @@ async fn main(_spawner: Spawner) { | |||
| 24 | let p = embassy_stm32::init(config); | 25 | let p = embassy_stm32::init(config); |
| 25 | info!("Hello World!"); | 26 | info!("Hello World!"); |
| 26 | 27 | ||
| 27 | let ch1 = PwmPin::new_ch1(p.PA6); | 28 | let ch1 = PwmPin::new_ch1(p.PA6, OutputType::PushPull); |
| 28 | let mut pwm = SimplePwm::new(p.TIM3, Some(ch1), None, None, None, khz(10)); | 29 | let mut pwm = SimplePwm::new(p.TIM3, Some(ch1), None, None, None, khz(10)); |
| 29 | let max = pwm.get_max_duty(); | 30 | let max = pwm.get_max_duty(); |
| 30 | pwm.enable(Channel::Ch1); | 31 | pwm.enable(Channel::Ch1); |
