Skip to content

Commit

Permalink
wip: split by value
Browse files Browse the repository at this point in the history
  • Loading branch information
GrantM11235 committed Sep 7, 2024
1 parent b8beaba commit df06c2b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
9 changes: 9 additions & 0 deletions embassy-stm32/src/timer/low_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//!
//! The available functionality depends on the timer type.

use core::mem::ManuallyDrop;

use embassy_hal_internal::{into_ref, Peripheral, PeripheralRef};
// Re-export useful enums
pub use stm32_metapac::timer::vals::{FilterValue, Sms as SlaveMode, Ts as TriggerSource};
Expand Down Expand Up @@ -198,6 +200,13 @@ impl<'d, T: CoreInstance> Timer<'d, T> {
Self { tim }
}

pub(crate) unsafe fn clone_unchecked(&self) -> ManuallyDrop<Self> {
// this doesn't work for some reason
// let tim = unsafe { self.tim.clone_unchecked() };
let tim = todo!();
ManuallyDrop::new(Self { tim })
}

/// Get access to the virutal core 16bit timer registers.
///
/// Note: This works even if the timer is more capable, because registers
Expand Down
1 change: 1 addition & 0 deletions embassy-stm32/src/timer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use core::marker::PhantomData;

use embassy_hal_internal::Peripheral;
use embassy_sync::waitqueue::AtomicWaker;

#[cfg(not(stm32l0))]
Expand Down
17 changes: 11 additions & 6 deletions embassy-stm32/src/timer/simple_pwm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Simple PWM driver.

use core::marker::PhantomData;
use core::mem::ManuallyDrop;

use embassy_hal_internal::{into_ref, PeripheralRef};

Expand Down Expand Up @@ -57,7 +58,7 @@ channel_impl!(new_ch4, Ch4, Channel4Pin);
/// It is not possible to change the pwm frequency because
/// the frequency configuration is shared with all four channels.
pub struct SimplePwmChannel<'d, T: GeneralInstance4Channel> {
timer: &'d Timer<'d, T>,
timer: ManuallyDrop<Timer<'d, T>>,
channel: Channel,
}

Expand Down Expand Up @@ -199,7 +200,7 @@ impl<'d, T: GeneralInstance4Channel> SimplePwm<'d, T> {
/// If you need to use multiple channels, use [`Self::split`].
pub fn channel(&mut self, channel: Channel) -> SimplePwmChannel<'_, T> {
SimplePwmChannel {
timer: &self.inner,
timer: unsafe { self.inner.clone_unchecked() },
channel,
}
}
Expand Down Expand Up @@ -245,12 +246,16 @@ impl<'d, T: GeneralInstance4Channel> SimplePwm<'d, T> {
/// This returns all four channels, including channels that
/// aren't configured with a [`PwmPin`].
// TODO: I hate the name "split"
pub fn split(&mut self) -> SimplePwmChannels<'_, T> {
// TODO: pre-enable channels?
pub fn split(self) -> SimplePwmChannels<'static, T>
where
// must be static because the timer will never be dropped/disabled
'd: 'static,
{
// without this, the timer would be disabled at the end of this function
let timer = ManuallyDrop::new(self.inner);

// we can't use self.channel() because that takes &mut self
let ch = |channel| SimplePwmChannel {
timer: &self.inner,
timer: unsafe { timer.clone_unchecked() },
channel,
};

Expand Down

0 comments on commit df06c2b

Please sign in to comment.