From 9dca368c3dd1a8f00295b21c87d4fbb94afb60a5 Mon Sep 17 00:00:00 2001 From: chemicstry Date: Fri, 7 Oct 2022 13:29:56 +0300 Subject: Use RccPeripheral for adc_v2 --- embassy-stm32/src/adc/mod.rs | 4 ++-- embassy-stm32/src/adc/v2.rs | 26 +++++++++----------------- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/embassy-stm32/src/adc/mod.rs b/embassy-stm32/src/adc/mod.rs index 8da13073..fba016a7 100644 --- a/embassy-stm32/src/adc/mod.rs +++ b/embassy-stm32/src/adc/mod.rs @@ -30,9 +30,9 @@ pub(crate) mod sealed { } } -#[cfg(not(adc_f1))] +#[cfg(not(any(adc_f1, adc_v2)))] pub trait Instance: sealed::Instance + 'static {} -#[cfg(adc_f1)] +#[cfg(any(adc_f1, adc_v2))] pub trait Instance: sealed::Instance + crate::rcc::RccPeripheral + 'static {} #[cfg(all(not(adc_f1), not(adc_v1)))] pub trait Common: sealed::Common + 'static {} diff --git a/embassy-stm32/src/adc/v2.rs b/embassy-stm32/src/adc/v2.rs index 25b7ba96..70e3b73b 100644 --- a/embassy-stm32/src/adc/v2.rs +++ b/embassy-stm32/src/adc/v2.rs @@ -12,21 +12,6 @@ pub const VREF_DEFAULT_MV: u32 = 3300; /// VREF voltage used for factory calibration of VREFINTCAL register. pub const VREF_CALIB_MV: u32 = 3300; -#[cfg(not(any(rcc_f4, rcc_f7)))] -fn enable() { - todo!() -} - -#[cfg(any(rcc_f4, rcc_f7))] -fn enable() { - critical_section::with(|_| unsafe { - // TODO do not enable all adc clocks if not needed - crate::pac::RCC.apb2enr().modify(|w| w.set_adc1en(true)); - crate::pac::RCC.apb2enr().modify(|w| w.set_adc2en(true)); - crate::pac::RCC.apb2enr().modify(|w| w.set_adc3en(true)); - }); -} - pub enum Resolution { TwelveBit, TenBit, @@ -164,9 +149,10 @@ where { pub fn new(_peri: impl Peripheral

+ 'd, delay: &mut impl DelayUs) -> Self { into_ref!(_peri); - enable(); + T::enable(); + T::reset(); - let presc = unsafe { Prescaler::from_pclk2(crate::rcc::get_freqs().apb2) }; + let presc = unsafe { Prescaler::from_pclk2(T::frequency()) }; unsafe { T::common_regs().ccr().modify(|w| w.set_adcpre(presc.adcpre())); } @@ -288,3 +274,9 @@ where } } } + +impl<'d, T: Instance> Drop for Adc<'d, T> { + fn drop(&mut self) { + T::disable(); + } +} -- cgit v1.2.3