diff options
| author | Matt Allen <[email protected]> | 2025-03-05 22:10:00 -0500 |
|---|---|---|
| committer | Matt Allen <[email protected]> | 2025-03-05 22:10:00 -0500 |
| commit | f22649e0082a9e9886d7fc2f1f8986682b4cc921 (patch) | |
| tree | e3bf723784037ed6d8e3e7fce52cc1cb51d6b8af | |
| parent | 0edd45e61013539f0d8379bf38177f6cb03f5fc0 (diff) | |
Added function to channel_impl to allow full configuration of the pin
| -rw-r--r-- | embassy-stm32/src/lptim/pwm.rs | 29 | ||||
| -rw-r--r-- | embassy-stm32/src/timer/simple_pwm.rs | 29 |
2 files changed, 56 insertions, 2 deletions
diff --git a/embassy-stm32/src/lptim/pwm.rs b/embassy-stm32/src/lptim/pwm.rs index 1f43eb6ee..9f3803cd6 100644 --- a/embassy-stm32/src/lptim/pwm.rs +++ b/embassy-stm32/src/lptim/pwm.rs | |||
| @@ -10,7 +10,7 @@ use super::OutputPin; | |||
| 10 | #[cfg(any(lptim_v2a, lptim_v2b))] | 10 | #[cfg(any(lptim_v2a, lptim_v2b))] |
| 11 | use super::{channel::Channel, timer::ChannelDirection, Channel1Pin, Channel2Pin}; | 11 | use super::{channel::Channel, timer::ChannelDirection, Channel1Pin, Channel2Pin}; |
| 12 | use super::{BasicInstance, Instance}; | 12 | use super::{BasicInstance, Instance}; |
| 13 | use crate::gpio::{AfType, AnyPin, OutputType, Speed}; | 13 | use crate::gpio::{AfType, AnyPin, OutputType, Pull, Speed}; |
| 14 | use crate::time::Hertz; | 14 | use crate::time::Hertz; |
| 15 | use crate::Peripheral; | 15 | use crate::Peripheral; |
| 16 | 16 | ||
| @@ -29,6 +29,15 @@ pub struct PwmPin<'d, T, C> { | |||
| 29 | phantom: PhantomData<(T, C)>, | 29 | phantom: PhantomData<(T, C)>, |
| 30 | } | 30 | } |
| 31 | 31 | ||
| 32 | /// PWM pin config | ||
| 33 | /// | ||
| 34 | /// This configures the pwm pin settings | ||
| 35 | pub struct PwmPinConfig { | ||
| 36 | pub output_type: OutputType, | ||
| 37 | pub speed: Speed, | ||
| 38 | pub pull: Pull, | ||
| 39 | } | ||
| 40 | |||
| 32 | macro_rules! channel_impl { | 41 | macro_rules! channel_impl { |
| 33 | ($new_chx:ident, $channel:ident, $pin_trait:ident) => { | 42 | ($new_chx:ident, $channel:ident, $pin_trait:ident) => { |
| 34 | impl<'d, T: BasicInstance> PwmPin<'d, T, $channel> { | 43 | impl<'d, T: BasicInstance> PwmPin<'d, T, $channel> { |
| @@ -47,6 +56,24 @@ macro_rules! channel_impl { | |||
| 47 | phantom: PhantomData, | 56 | phantom: PhantomData, |
| 48 | } | 57 | } |
| 49 | } | 58 | } |
| 59 | #[doc = concat!("Create a new ", stringify!($channel), "_with_config PWM pin instance.")] | ||
| 60 | pub fn $new_chx_with_config( | ||
| 61 | pin: impl Peripheral<P = impl $pin_trait<T>> + 'd, | ||
| 62 | pin_config: PwmPinConfig, | ||
| 63 | ) -> Self { | ||
| 64 | into_ref!(pin); | ||
| 65 | critical_section::with(|_| { | ||
| 66 | pin.set_low(); | ||
| 67 | pin.set_as_af( | ||
| 68 | pin.af_num(), | ||
| 69 | AfType::output_pull(pin_config.output_type, pin_config.speed, pin_config.pull), | ||
| 70 | ); | ||
| 71 | }); | ||
| 72 | PwmPin { | ||
| 73 | _pin: pin.map_into(), | ||
| 74 | phantom: PhantomData, | ||
| 75 | } | ||
| 76 | } | ||
| 50 | } | 77 | } |
| 51 | }; | 78 | }; |
| 52 | } | 79 | } |
diff --git a/embassy-stm32/src/timer/simple_pwm.rs b/embassy-stm32/src/timer/simple_pwm.rs index 1dff3f9ae..7664a4dc0 100644 --- a/embassy-stm32/src/timer/simple_pwm.rs +++ b/embassy-stm32/src/timer/simple_pwm.rs | |||
| @@ -7,7 +7,7 @@ use embassy_hal_internal::{into_ref, PeripheralRef}; | |||
| 7 | 7 | ||
| 8 | use super::low_level::{CountingMode, OutputCompareMode, OutputPolarity, Timer}; | 8 | use super::low_level::{CountingMode, OutputCompareMode, OutputPolarity, Timer}; |
| 9 | use super::{Channel, Channel1Pin, Channel2Pin, Channel3Pin, Channel4Pin, GeneralInstance4Channel, TimerBits}; | 9 | use super::{Channel, Channel1Pin, Channel2Pin, Channel3Pin, Channel4Pin, GeneralInstance4Channel, TimerBits}; |
| 10 | use crate::gpio::{AfType, AnyPin, OutputType, Speed}; | 10 | use crate::gpio::{AfType, AnyPin, OutputType, Pull, Speed}; |
| 11 | use crate::time::Hertz; | 11 | use crate::time::Hertz; |
| 12 | use crate::Peripheral; | 12 | use crate::Peripheral; |
| 13 | 13 | ||
| @@ -28,6 +28,15 @@ pub struct PwmPin<'d, T, C> { | |||
| 28 | phantom: PhantomData<(T, C)>, | 28 | phantom: PhantomData<(T, C)>, |
| 29 | } | 29 | } |
| 30 | 30 | ||
| 31 | /// PWM pin config | ||
| 32 | /// | ||
| 33 | /// This configures the pwm pin settings | ||
| 34 | pub struct PwmPinConfig { | ||
| 35 | pub output_type: OutputType, | ||
| 36 | pub speed: Speed, | ||
| 37 | pub pull: Pull, | ||
| 38 | } | ||
| 39 | |||
| 31 | macro_rules! channel_impl { | 40 | macro_rules! channel_impl { |
| 32 | ($new_chx:ident, $channel:ident, $pin_trait:ident) => { | 41 | ($new_chx:ident, $channel:ident, $pin_trait:ident) => { |
| 33 | impl<'d, T: GeneralInstance4Channel> PwmPin<'d, T, $channel> { | 42 | impl<'d, T: GeneralInstance4Channel> PwmPin<'d, T, $channel> { |
| @@ -43,6 +52,24 @@ macro_rules! channel_impl { | |||
| 43 | phantom: PhantomData, | 52 | phantom: PhantomData, |
| 44 | } | 53 | } |
| 45 | } | 54 | } |
| 55 | #[doc = concat!("Create a new ", stringify!($channel), "_with_config PWM pin instance.")] | ||
| 56 | pub fn $new_chx_with_config( | ||
| 57 | pin: impl Peripheral<P = impl $pin_trait<T>> + 'd, | ||
| 58 | pin_config: PwmPinConfig, | ||
| 59 | ) -> Self { | ||
| 60 | into_ref!(pin); | ||
| 61 | critical_section::with(|_| { | ||
| 62 | pin.set_low(); | ||
| 63 | pin.set_as_af( | ||
| 64 | pin.af_num(), | ||
| 65 | AfType::output_pull(pin_config.output_type, pin_config.speed, pin_config.pull), | ||
| 66 | ); | ||
| 67 | }); | ||
| 68 | PwmPin { | ||
| 69 | _pin: pin.map_into(), | ||
| 70 | phantom: PhantomData, | ||
| 71 | } | ||
| 72 | } | ||
| 46 | } | 73 | } |
| 47 | }; | 74 | }; |
| 48 | } | 75 | } |
