From 171b764d8275b8da3af5650edfac0d5c4c3e3fe1 Mon Sep 17 00:00:00 2001 From: Grant Miller Date: Wed, 26 Oct 2022 18:36:04 -0500 Subject: [PATCH] Refactor: Use `PeripheralRef` --- embassy-stm32/src/adc/f1.rs | 8 +++----- embassy-stm32/src/adc/mod.rs | 7 ++++--- embassy-stm32/src/adc/v2.rs | 8 +++----- embassy-stm32/src/adc/v3.rs | 8 +++----- embassy-stm32/src/adc/v4.rs | 8 +++----- 5 files changed, 16 insertions(+), 23 deletions(-) diff --git a/embassy-stm32/src/adc/f1.rs b/embassy-stm32/src/adc/f1.rs index 4282a655..d30ec001 100644 --- a/embassy-stm32/src/adc/f1.rs +++ b/embassy-stm32/src/adc/f1.rs @@ -1,5 +1,3 @@ -use core::marker::PhantomData; - use embassy_hal_common::into_ref; use embedded_hal_02::blocking::delay::DelayUs; @@ -30,8 +28,8 @@ impl super::sealed::AdcPin for Temperature { } impl<'d, T: Instance> Adc<'d, T> { - pub fn new(_peri: impl Peripheral

+ 'd, delay: &mut impl DelayUs) -> Self { - into_ref!(_peri); + pub fn new(adc: impl Peripheral

+ 'd, delay: &mut impl DelayUs) -> Self { + into_ref!(adc); T::enable(); T::reset(); unsafe { @@ -60,8 +58,8 @@ impl<'d, T: Instance> Adc<'d, T> { delay.delay_us((1_000_000) / Self::freq().0 + 1); Self { + adc, sample_time: Default::default(), - phantom: PhantomData, } } diff --git a/embassy-stm32/src/adc/mod.rs b/embassy-stm32/src/adc/mod.rs index 6d8afea5..ec49dace 100644 --- a/embassy-stm32/src/adc/mod.rs +++ b/embassy-stm32/src/adc/mod.rs @@ -23,8 +23,9 @@ use crate::peripherals; #[cfg(not(adc_v1))] pub struct Adc<'d, T: Instance> { + #[allow(unused)] + adc: crate::PeripheralRef<'d, T>, sample_time: SampleTime, - phantom: core::marker::PhantomData<&'d mut T>, } pub(crate) mod sealed { @@ -44,9 +45,9 @@ pub(crate) mod sealed { } #[cfg(not(any(adc_f1, adc_v2, adc_v4)))] -pub trait Instance: sealed::Instance + 'static {} +pub trait Instance: sealed::Instance + crate::Peripheral

{} #[cfg(any(adc_f1, adc_v2, adc_v4))] -pub trait Instance: sealed::Instance + crate::rcc::RccPeripheral + 'static {} +pub trait Instance: sealed::Instance + crate::Peripheral

+ crate::rcc::RccPeripheral {} pub trait AdcPin: sealed::AdcPin {} pub trait InternalChannel: sealed::InternalChannel {} diff --git a/embassy-stm32/src/adc/v2.rs b/embassy-stm32/src/adc/v2.rs index f1e65dc5..11a51f99 100644 --- a/embassy-stm32/src/adc/v2.rs +++ b/embassy-stm32/src/adc/v2.rs @@ -1,5 +1,3 @@ -use core::marker::PhantomData; - use embassy_hal_common::into_ref; use embedded_hal_02::blocking::delay::DelayUs; @@ -96,8 +94,8 @@ impl<'d, T> Adc<'d, T> where T: Instance, { - pub fn new(_peri: impl Peripheral

+ 'd, delay: &mut impl DelayUs) -> Self { - into_ref!(_peri); + pub fn new(adc: impl Peripheral

+ 'd, delay: &mut impl DelayUs) -> Self { + into_ref!(adc); T::enable(); T::reset(); @@ -113,8 +111,8 @@ where delay.delay_us(ADC_POWERUP_TIME_US); Self { + adc, sample_time: Default::default(), - phantom: PhantomData, } } diff --git a/embassy-stm32/src/adc/v3.rs b/embassy-stm32/src/adc/v3.rs index ee5f278b..8f81cb7a 100644 --- a/embassy-stm32/src/adc/v3.rs +++ b/embassy-stm32/src/adc/v3.rs @@ -1,5 +1,3 @@ -use core::marker::PhantomData; - use embassy_hal_common::into_ref; use embedded_hal_02::blocking::delay::DelayUs; @@ -61,8 +59,8 @@ impl super::sealed::AdcPin for Vbat { } impl<'d, T: Instance> Adc<'d, T> { - pub fn new(_peri: impl Peripheral

+ 'd, delay: &mut impl DelayUs) -> Self { - into_ref!(_peri); + pub fn new(adc: impl Peripheral

+ 'd, delay: &mut impl DelayUs) -> Self { + into_ref!(adc); enable(); unsafe { T::regs().cr().modify(|reg| { @@ -92,8 +90,8 @@ impl<'d, T: Instance> Adc<'d, T> { delay.delay_us(1); Self { + adc, sample_time: Default::default(), - phantom: PhantomData, } } diff --git a/embassy-stm32/src/adc/v4.rs b/embassy-stm32/src/adc/v4.rs index 12f86885..f5aa0e63 100644 --- a/embassy-stm32/src/adc/v4.rs +++ b/embassy-stm32/src/adc/v4.rs @@ -1,5 +1,3 @@ -use core::marker::PhantomData; - use atomic_polyfill::{AtomicU8, Ordering}; use embedded_hal_02::blocking::delay::DelayUs; use pac::adc::vals::{Adcaldif, Boost, Difsel, Exten, Pcsel}; @@ -226,8 +224,8 @@ impl Prescaler { } impl<'d, T: Instance> Adc<'d, T> { - pub fn new(_peri: impl Peripheral

+ 'd, delay: &mut impl DelayUs) -> Self { - embassy_hal_common::into_ref!(_peri); + pub fn new(adc: impl Peripheral

+ 'd, delay: &mut impl DelayUs) -> Self { + embassy_hal_common::into_ref!(adc); T::enable(); T::reset(); @@ -257,8 +255,8 @@ impl<'d, T: Instance> Adc<'d, T> { } let mut s = Self { + adc, sample_time: Default::default(), - phantom: PhantomData, }; s.power_up(delay); s.configure_differential_inputs();