diff --git a/embassy-stm32/src/pwm/mod.rs b/embassy-stm32/src/pwm/mod.rs index e8938ffa..c6edbc6c 100644 --- a/embassy-stm32/src/pwm/mod.rs +++ b/embassy-stm32/src/pwm/mod.rs @@ -99,7 +99,7 @@ macro_rules! impl_compare_capable_16bit { mode: OutputCompareMode, ) { use crate::timer::sealed::GeneralPurpose16bitInstance; - let r = self.regs_gp16(); + let r = Self::regs_gp16(); let raw_channel: usize = channel.raw(); r.ccmr_output(raw_channel / 2) .modify(|w| w.set_ocm(raw_channel % 2, mode.into())); @@ -107,21 +107,21 @@ macro_rules! impl_compare_capable_16bit { unsafe fn enable_channel(&mut self, channel: Channel, enable: bool) { use crate::timer::sealed::GeneralPurpose16bitInstance; - self.regs_gp16() + Self::regs_gp16() .ccer() .modify(|w| w.set_cce(channel.raw(), enable)); } unsafe fn set_compare_value(&mut self, channel: Channel, value: u16) { use crate::timer::sealed::GeneralPurpose16bitInstance; - self.regs_gp16() + Self::regs_gp16() .ccr(channel.raw()) .modify(|w| w.set_ccr(value)); } unsafe fn get_max_compare_value(&self) -> u16 { use crate::timer::sealed::GeneralPurpose16bitInstance; - self.regs_gp16().arr().read().arr() + Self::regs_gp16().arr().read().arr() } } }; @@ -136,7 +136,7 @@ foreach_interrupt! { mode: OutputCompareMode, ) { use crate::timer::sealed::GeneralPurpose16bitInstance; - let r = self.regs_gp16(); + let r = Self::regs_gp16(); let raw_channel: usize = channel.raw(); r.ccmr_output(raw_channel / 2) .modify(|w| w.set_ocm(raw_channel % 2, mode.into())); @@ -144,21 +144,21 @@ foreach_interrupt! { unsafe fn enable_channel(&mut self, channel: Channel, enable: bool) { use crate::timer::sealed::GeneralPurpose16bitInstance; - self.regs_gp16() + Self::regs_gp16() .ccer() .modify(|w| w.set_cce(channel.raw(), enable)); } unsafe fn set_compare_value(&mut self, channel: Channel, value: u16) { use crate::timer::sealed::GeneralPurpose16bitInstance; - self.regs_gp16() + Self::regs_gp16() .ccr(channel.raw()) .modify(|w| w.set_ccr(value)); } unsafe fn get_max_compare_value(&self) -> u16 { use crate::timer::sealed::GeneralPurpose16bitInstance; - self.regs_gp16().arr().read().arr() + Self::regs_gp16().arr().read().arr() } } @@ -177,22 +177,22 @@ foreach_interrupt! { ) { use crate::timer::sealed::GeneralPurpose32bitInstance; let raw_channel = channel.raw(); - self.regs_gp32().ccmr_output(raw_channel / 2).modify(|w| w.set_ocm(raw_channel % 2, mode.into())); + Self::regs_gp32().ccmr_output(raw_channel / 2).modify(|w| w.set_ocm(raw_channel % 2, mode.into())); } unsafe fn enable_channel(&mut self, channel: Channel, enable: bool) { use crate::timer::sealed::GeneralPurpose32bitInstance; - self.regs_gp32().ccer().modify(|w| w.set_cce(channel.raw(), enable)); + Self::regs_gp32().ccer().modify(|w| w.set_cce(channel.raw(), enable)); } unsafe fn set_compare_value(&mut self, channel: Channel, value: u32) { use crate::timer::sealed::GeneralPurpose32bitInstance; - self.regs_gp32().ccr(channel.raw()).modify(|w| w.set_ccr(value)); + Self::regs_gp32().ccr(channel.raw()).modify(|w| w.set_ccr(value)); } unsafe fn get_max_compare_value(&self) -> u32 { use crate::timer::sealed::GeneralPurpose32bitInstance; - self.regs_gp32().arr().read().arr() as u32 + Self::regs_gp32().arr().read().arr() as u32 } } impl CaptureCompare16bitInstance for crate::peripherals::$inst { @@ -211,7 +211,7 @@ foreach_interrupt! { mode: OutputCompareMode, ) { use crate::timer::sealed::AdvancedControlInstance; - let r = self.regs_advanced(); + let r = Self::regs_advanced(); let raw_channel: usize = channel.raw(); r.ccmr_output(raw_channel / 2) .modify(|w| w.set_ocm(raw_channel % 2, mode.into())); @@ -219,21 +219,21 @@ foreach_interrupt! { unsafe fn enable_channel(&mut self, channel: Channel, enable: bool) { use crate::timer::sealed::AdvancedControlInstance; - self.regs_advanced() + Self::regs_advanced() .ccer() .modify(|w| w.set_cce(channel.raw(), enable)); } unsafe fn set_compare_value(&mut self, channel: Channel, value: u16) { use crate::timer::sealed::AdvancedControlInstance; - self.regs_advanced() + Self::regs_advanced() .ccr(channel.raw()) .modify(|w| w.set_ccr(value)); } unsafe fn get_max_compare_value(&self) -> u16 { use crate::timer::sealed::AdvancedControlInstance; - self.regs_advanced().arr().read().arr() + Self::regs_advanced().arr().read().arr() } } diff --git a/embassy-stm32/src/time_driver.rs b/embassy-stm32/src/time_driver.rs index 009c6203..08796acd 100644 --- a/embassy-stm32/src/time_driver.rs +++ b/embassy-stm32/src/time_driver.rs @@ -103,7 +103,6 @@ impl AlarmState { } struct RtcDriver { - timer: T, /// Number of 2^15 periods elapsed since boot. period: AtomicU32, alarm_count: AtomicU8, @@ -114,7 +113,6 @@ struct RtcDriver { const ALARM_STATE_NEW: AlarmState = AlarmState::new(); embassy::time_driver_impl!(static DRIVER: RtcDriver = RtcDriver { - timer: unsafe { core::mem::transmute(()) }, // steal is not const period: AtomicU32::new(0), alarm_count: AtomicU8::new(0), alarms: Mutex::const_new(CriticalSectionRawMutex::new(), [ALARM_STATE_NEW; ALARM_COUNT]), @@ -122,7 +120,7 @@ embassy::time_driver_impl!(static DRIVER: RtcDriver = RtcDriver { impl RtcDriver { fn init(&'static self) { - let r = self.timer.regs_gp16(); + let r = T::regs_gp16(); ::enable(); ::reset(); @@ -163,7 +161,7 @@ impl RtcDriver { } fn on_interrupt(&self) { - let r = self.timer.regs_gp16(); + let r = T::regs_gp16(); // NOTE(unsafe) Use critical section to access the methods // XXX: reduce the size of this critical section ? @@ -194,7 +192,7 @@ impl RtcDriver { } fn next_period(&self) { - let r = self.timer.regs_gp16(); + let r = T::regs_gp16(); let period = self.period.fetch_add(1, Ordering::Relaxed) + 1; let t = (period as u64) << 15; @@ -236,7 +234,7 @@ impl RtcDriver { impl Driver for RtcDriver { fn now(&self) -> u64 { - let r = self.timer.regs_gp16(); + let r = T::regs_gp16(); let period = self.period.load(Ordering::Relaxed); compiler_fence(Ordering::Acquire); @@ -273,7 +271,7 @@ impl Driver for RtcDriver { fn set_alarm(&self, alarm: AlarmHandle, timestamp: u64) { critical_section::with(|cs| { - let r = self.timer.regs_gp16(); + let r = T::regs_gp16(); let n = alarm.id() as _; let alarm = self.get_alarm(cs, alarm); diff --git a/embassy-stm32/src/timer/mod.rs b/embassy-stm32/src/timer/mod.rs index 4c1eb946..f9fefdf7 100644 --- a/embassy-stm32/src/timer/mod.rs +++ b/embassy-stm32/src/timer/mod.rs @@ -14,7 +14,7 @@ pub(crate) mod sealed { pub trait Basic16bitInstance: RccPeripheral { type Interrupt: Interrupt; - fn regs(&self) -> crate::pac::timer::TimBasic; + fn regs() -> crate::pac::timer::TimBasic; fn start(&mut self); @@ -30,17 +30,17 @@ pub(crate) mod sealed { } pub trait GeneralPurpose16bitInstance: Basic16bitInstance { - fn regs_gp16(&self) -> crate::pac::timer::TimGp16; + fn regs_gp16() -> crate::pac::timer::TimGp16; } pub trait GeneralPurpose32bitInstance: GeneralPurpose16bitInstance { - fn regs_gp32(&self) -> crate::pac::timer::TimGp32; + fn regs_gp32() -> crate::pac::timer::TimGp32; fn set_frequency>(&mut self, frequency: F); } pub trait AdvancedControlInstance: Basic16bitInstance { - fn regs_advanced(&self) -> crate::pac::timer::TimAdv; + fn regs_advanced() -> crate::pac::timer::TimAdv; } } @@ -58,26 +58,25 @@ macro_rules! impl_basic_16bit_timer { impl sealed::Basic16bitInstance for crate::peripherals::$inst { type Interrupt = crate::interrupt::$irq; - fn regs(&self) -> crate::pac::timer::TimBasic { + fn regs() -> crate::pac::timer::TimBasic { crate::pac::timer::TimBasic(crate::pac::$inst.0) } fn start(&mut self) { unsafe { - self.regs().cr1().modify(|r| r.set_cen(true)); + Self::regs().cr1().modify(|r| r.set_cen(true)); } } fn stop(&mut self) { - let regs = self.regs(); unsafe { - regs.cr1().modify(|r| r.set_cen(false)); + Self::regs().cr1().modify(|r| r.set_cen(false)); } } fn reset(&mut self) { unsafe { - self.regs().cnt().write(|r| r.set_cnt(0)); + Self::regs().cnt().write(|r| r.set_cnt(0)); } } @@ -90,7 +89,7 @@ macro_rules! impl_basic_16bit_timer { let arr: u16 = unwrap!((pclk_ticks_per_timer_period / (u32::from(psc) + 1)).try_into()); - let regs = self.regs(); + let regs = Self::regs(); unsafe { regs.psc().write(|r| r.set_psc(psc)); regs.arr().write(|r| r.set_arr(arr)); @@ -102,10 +101,11 @@ macro_rules! impl_basic_16bit_timer { } fn clear_update_interrupt(&mut self) -> bool { + let regs = Self::regs(); unsafe { - let sr = self.regs().sr().read(); + let sr = regs.sr().read(); if sr.uif() { - self.regs().sr().modify(|r| { + regs.sr().modify(|r| { r.set_uif(false); }); true @@ -117,7 +117,7 @@ macro_rules! impl_basic_16bit_timer { fn enable_update_interrupt(&mut self, enable: bool) { unsafe { - self.regs().dier().write(|r| r.set_uie(enable)); + Self::regs().dier().write(|r| r.set_uie(enable)); } } } @@ -128,7 +128,7 @@ macro_rules! impl_basic_16bit_timer { macro_rules! impl_32bit_timer { ($inst:ident) => { impl sealed::GeneralPurpose32bitInstance for crate::peripherals::$inst { - fn regs_gp32(&self) -> crate::pac::timer::TimGp32 { + fn regs_gp32() -> crate::pac::timer::TimGp32 { crate::pac::$inst } @@ -141,7 +141,7 @@ macro_rules! impl_32bit_timer { let arr: u32 = unwrap!(((pclk_ticks_per_timer_period / (psc as u64 + 1)).try_into())); - let regs = self.regs_gp32(); + let regs = Self::regs_gp32(); unsafe { regs.psc().write(|r| r.set_psc(psc)); regs.arr().write(|r| r.set_arr(arr)); @@ -169,7 +169,7 @@ foreach_interrupt! { } impl sealed::GeneralPurpose16bitInstance for crate::peripherals::$inst { - fn regs_gp16(&self) -> crate::pac::timer::TimGp16 { + fn regs_gp16() -> crate::pac::timer::TimGp16 { crate::pac::$inst } } @@ -185,7 +185,7 @@ foreach_interrupt! { } impl sealed::GeneralPurpose16bitInstance for crate::peripherals::$inst { - fn regs_gp16(&self) -> crate::pac::timer::TimGp16 { + fn regs_gp16() -> crate::pac::timer::TimGp16 { crate::pac::timer::TimGp16(crate::pac::$inst.0) } } @@ -206,7 +206,7 @@ foreach_interrupt! { } impl sealed::AdvancedControlInstance for crate::peripherals::$inst { - fn regs_advanced(&self) -> crate::pac::timer::TimAdv { + fn regs_advanced() -> crate::pac::timer::TimAdv { crate::pac::$inst } } diff --git a/examples/stm32h7/src/bin/low_level_timer_api.rs b/examples/stm32h7/src/bin/low_level_timer_api.rs index 0c99b094..3a47dc22 100644 --- a/examples/stm32h7/src/bin/low_level_timer_api.rs +++ b/examples/stm32h7/src/bin/low_level_timer_api.rs @@ -90,20 +90,16 @@ impl<'d, T: CaptureCompare32bitInstance> SimplePwm32<'d, T> { this.inner.start(); unsafe { - this.inner - .regs_gp32() + T::regs_gp32() .ccmr_output(0) .modify(|w| w.set_ocm(0, OutputCompareMode::PwmMode1.into())); - this.inner - .regs_gp32() + T::regs_gp32() .ccmr_output(0) .modify(|w| w.set_ocm(1, OutputCompareMode::PwmMode1.into())); - this.inner - .regs_gp32() + T::regs_gp32() .ccmr_output(1) .modify(|w| w.set_ocm(0, OutputCompareMode::PwmMode1.into())); - this.inner - .regs_gp32() + T::regs_gp32() .ccmr_output(1) .modify(|w| w.set_ocm(1, OutputCompareMode::PwmMode1.into())); } @@ -112,8 +108,7 @@ impl<'d, T: CaptureCompare32bitInstance> SimplePwm32<'d, T> { pub fn enable(&mut self, channel: Channel) { unsafe { - self.inner - .regs_gp32() + T::regs_gp32() .ccer() .modify(|w| w.set_cce(channel.raw(), true)); } @@ -121,8 +116,7 @@ impl<'d, T: CaptureCompare32bitInstance> SimplePwm32<'d, T> { pub fn disable(&mut self, channel: Channel) { unsafe { - self.inner - .regs_gp32() + T::regs_gp32() .ccer() .modify(|w| w.set_cce(channel.raw(), false)); } @@ -136,14 +130,13 @@ impl<'d, T: CaptureCompare32bitInstance> SimplePwm32<'d, T> { } pub fn get_max_duty(&self) -> u32 { - unsafe { self.inner.regs_gp32().arr().read().arr() } + unsafe { T::regs_gp32().arr().read().arr() } } pub fn set_duty(&mut self, channel: Channel, duty: u32) { defmt::assert!(duty < self.get_max_duty()); unsafe { - self.inner - .regs_gp32() + T::regs_gp32() .ccr(channel.raw()) .modify(|w| w.set_ccr(duty)) }