diff options
| author | Grant Miller <[email protected]> | 2024-09-07 11:13:18 -0500 |
|---|---|---|
| committer | Grant Miller <[email protected]> | 2024-09-07 11:17:13 -0500 |
| commit | df06c2bbfe51e22e0d3eda3d760839f617ffbd96 (patch) | |
| tree | 9650a486f89c43e994971b4845edc3ba34fcce01 | |
| parent | b8beaba6df08c4455f55780a6e13191d95ad9eec (diff) | |
wip: split by value
| -rw-r--r-- | embassy-stm32/src/timer/low_level.rs | 9 | ||||
| -rw-r--r-- | embassy-stm32/src/timer/mod.rs | 1 | ||||
| -rw-r--r-- | embassy-stm32/src/timer/simple_pwm.rs | 17 |
3 files changed, 21 insertions, 6 deletions
diff --git a/embassy-stm32/src/timer/low_level.rs b/embassy-stm32/src/timer/low_level.rs index e643722aa..6377054c5 100644 --- a/embassy-stm32/src/timer/low_level.rs +++ b/embassy-stm32/src/timer/low_level.rs | |||
| @@ -6,6 +6,8 @@ | |||
| 6 | //! | 6 | //! |
| 7 | //! The available functionality depends on the timer type. | 7 | //! The available functionality depends on the timer type. |
| 8 | 8 | ||
| 9 | use core::mem::ManuallyDrop; | ||
| 10 | |||
| 9 | use embassy_hal_internal::{into_ref, Peripheral, PeripheralRef}; | 11 | use embassy_hal_internal::{into_ref, Peripheral, PeripheralRef}; |
| 10 | // Re-export useful enums | 12 | // Re-export useful enums |
| 11 | pub use stm32_metapac::timer::vals::{FilterValue, Sms as SlaveMode, Ts as TriggerSource}; | 13 | pub use stm32_metapac::timer::vals::{FilterValue, Sms as SlaveMode, Ts as TriggerSource}; |
| @@ -198,6 +200,13 @@ impl<'d, T: CoreInstance> Timer<'d, T> { | |||
| 198 | Self { tim } | 200 | Self { tim } |
| 199 | } | 201 | } |
| 200 | 202 | ||
| 203 | pub(crate) unsafe fn clone_unchecked(&self) -> ManuallyDrop<Self> { | ||
| 204 | // this doesn't work for some reason | ||
| 205 | // let tim = unsafe { self.tim.clone_unchecked() }; | ||
| 206 | let tim = todo!(); | ||
| 207 | ManuallyDrop::new(Self { tim }) | ||
| 208 | } | ||
| 209 | |||
| 201 | /// Get access to the virutal core 16bit timer registers. | 210 | /// Get access to the virutal core 16bit timer registers. |
| 202 | /// | 211 | /// |
| 203 | /// Note: This works even if the timer is more capable, because registers | 212 | /// Note: This works even if the timer is more capable, because registers |
diff --git a/embassy-stm32/src/timer/mod.rs b/embassy-stm32/src/timer/mod.rs index 25782ee13..6cf22689b 100644 --- a/embassy-stm32/src/timer/mod.rs +++ b/embassy-stm32/src/timer/mod.rs | |||
| @@ -2,6 +2,7 @@ | |||
| 2 | 2 | ||
| 3 | use core::marker::PhantomData; | 3 | use core::marker::PhantomData; |
| 4 | 4 | ||
| 5 | use embassy_hal_internal::Peripheral; | ||
| 5 | use embassy_sync::waitqueue::AtomicWaker; | 6 | use embassy_sync::waitqueue::AtomicWaker; |
| 6 | 7 | ||
| 7 | #[cfg(not(stm32l0))] | 8 | #[cfg(not(stm32l0))] |
diff --git a/embassy-stm32/src/timer/simple_pwm.rs b/embassy-stm32/src/timer/simple_pwm.rs index 7e2e9c202..9e4a09095 100644 --- a/embassy-stm32/src/timer/simple_pwm.rs +++ b/embassy-stm32/src/timer/simple_pwm.rs | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | //! Simple PWM driver. | 1 | //! Simple PWM driver. |
| 2 | 2 | ||
| 3 | use core::marker::PhantomData; | 3 | use core::marker::PhantomData; |
| 4 | use core::mem::ManuallyDrop; | ||
| 4 | 5 | ||
| 5 | use embassy_hal_internal::{into_ref, PeripheralRef}; | 6 | use embassy_hal_internal::{into_ref, PeripheralRef}; |
| 6 | 7 | ||
| @@ -57,7 +58,7 @@ channel_impl!(new_ch4, Ch4, Channel4Pin); | |||
| 57 | /// It is not possible to change the pwm frequency because | 58 | /// It is not possible to change the pwm frequency because |
| 58 | /// the frequency configuration is shared with all four channels. | 59 | /// the frequency configuration is shared with all four channels. |
| 59 | pub struct SimplePwmChannel<'d, T: GeneralInstance4Channel> { | 60 | pub struct SimplePwmChannel<'d, T: GeneralInstance4Channel> { |
| 60 | timer: &'d Timer<'d, T>, | 61 | timer: ManuallyDrop<Timer<'d, T>>, |
| 61 | channel: Channel, | 62 | channel: Channel, |
| 62 | } | 63 | } |
| 63 | 64 | ||
| @@ -199,7 +200,7 @@ impl<'d, T: GeneralInstance4Channel> SimplePwm<'d, T> { | |||
| 199 | /// If you need to use multiple channels, use [`Self::split`]. | 200 | /// If you need to use multiple channels, use [`Self::split`]. |
| 200 | pub fn channel(&mut self, channel: Channel) -> SimplePwmChannel<'_, T> { | 201 | pub fn channel(&mut self, channel: Channel) -> SimplePwmChannel<'_, T> { |
| 201 | SimplePwmChannel { | 202 | SimplePwmChannel { |
| 202 | timer: &self.inner, | 203 | timer: unsafe { self.inner.clone_unchecked() }, |
| 203 | channel, | 204 | channel, |
| 204 | } | 205 | } |
| 205 | } | 206 | } |
| @@ -245,12 +246,16 @@ impl<'d, T: GeneralInstance4Channel> SimplePwm<'d, T> { | |||
| 245 | /// This returns all four channels, including channels that | 246 | /// This returns all four channels, including channels that |
| 246 | /// aren't configured with a [`PwmPin`]. | 247 | /// aren't configured with a [`PwmPin`]. |
| 247 | // TODO: I hate the name "split" | 248 | // TODO: I hate the name "split" |
| 248 | pub fn split(&mut self) -> SimplePwmChannels<'_, T> { | 249 | pub fn split(self) -> SimplePwmChannels<'static, T> |
| 249 | // TODO: pre-enable channels? | 250 | where |
| 251 | // must be static because the timer will never be dropped/disabled | ||
| 252 | 'd: 'static, | ||
| 253 | { | ||
| 254 | // without this, the timer would be disabled at the end of this function | ||
| 255 | let timer = ManuallyDrop::new(self.inner); | ||
| 250 | 256 | ||
| 251 | // we can't use self.channel() because that takes &mut self | ||
| 252 | let ch = |channel| SimplePwmChannel { | 257 | let ch = |channel| SimplePwmChannel { |
| 253 | timer: &self.inner, | 258 | timer: unsafe { timer.clone_unchecked() }, |
| 254 | channel, | 259 | channel, |
| 255 | }; | 260 | }; |
| 256 | 261 | ||
