From 0b9961584b366e6d3cc83a218c8150800793cb1d Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Tue, 28 Sep 2021 20:29:46 -0400 Subject: [PATCH] stm32/adc: Ensure that clock is enabled Sadly due to the inconsistency in clocking configuration across devices we cannot use RccPeripheral. --- embassy-stm32/src/adc/v3.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/embassy-stm32/src/adc/v3.rs b/embassy-stm32/src/adc/v3.rs index 5fa5b9bc..ddf06dea 100644 --- a/embassy-stm32/src/adc/v3.rs +++ b/embassy-stm32/src/adc/v3.rs @@ -6,6 +6,17 @@ use embedded_hal::blocking::delay::DelayUs; pub const VDDA_CALIB_MV: u32 = 3000; +/// Sadly we cannot use `RccPeripheral::enable` since devices are quite inconsistent ADC clock +/// configuration. +unsafe fn enable() { + #[cfg(rcc_h7)] + crate::pac::RCC.apb2enr().modify(|w| w.set_adcen(true)); + #[cfg(rcc_g0)] + crate::pac::RCC.apbenr2().modify(|w| w.set_adcen(true)); + #[cfg(rcc_l4)] + crate::pac::RCC.ahb2enr().modify(|w| w.set_adcen(true)); +} + pub enum Resolution { TwelveBit, TenBit, @@ -196,6 +207,7 @@ impl<'d, T: Instance> Adc<'d, T> { pub fn new(_peri: impl Unborrow + 'd, delay: &mut impl DelayUs) -> Self { unborrow!(_peri); unsafe { + enable(); T::regs().cr().modify(|reg| { #[cfg(not(adc_g0))] reg.set_deeppwd(false);