From 7b38b95e1036d2a62e1d0abfc41b064839497242 Mon Sep 17 00:00:00 2001 From: Grant Miller Date: Wed, 26 Oct 2022 02:04:52 -0500 Subject: [PATCH] Refactor: Factor out `Resolution` --- embassy-stm32/src/adc/mod.rs | 5 +++ embassy-stm32/src/adc/resolution.rs | 59 +++++++++++++++++++++++++++++ embassy-stm32/src/adc/v2.rs | 35 +---------------- embassy-stm32/src/adc/v3.rs | 35 +---------------- embassy-stm32/src/adc/v4.rs | 38 +------------------ 5 files changed, 67 insertions(+), 105 deletions(-) create mode 100644 embassy-stm32/src/adc/resolution.rs diff --git a/embassy-stm32/src/adc/mod.rs b/embassy-stm32/src/adc/mod.rs index 6232678f..35d84f97 100644 --- a/embassy-stm32/src/adc/mod.rs +++ b/embassy-stm32/src/adc/mod.rs @@ -11,8 +11,13 @@ mod _version; #[cfg(not(adc_v1))] mod sample_time; +#[cfg(not(any(adc_f1, adc_v1)))] +mod resolution; + #[allow(unused)] pub use _version::*; +#[cfg(not(any(adc_f1, adc_v1)))] +pub use resolution::Resolution; #[cfg(not(adc_v1))] pub use sample_time::SampleTime; diff --git a/embassy-stm32/src/adc/resolution.rs b/embassy-stm32/src/adc/resolution.rs new file mode 100644 index 00000000..354cabfb --- /dev/null +++ b/embassy-stm32/src/adc/resolution.rs @@ -0,0 +1,59 @@ +#[cfg(any(adc_v2, adc_v3, adc_g0))] +pub enum Resolution { + TwelveBit, + TenBit, + EightBit, + SixBit, +} + +#[cfg(adc_v4)] +pub enum Resolution { + SixteenBit, + FourteenBit, + TwelveBit, + TenBit, + EightBit, +} + +impl Default for Resolution { + fn default() -> Self { + #[cfg(any(adc_v2, adc_v3, adc_g0))] + { + Self::TwelveBit + } + #[cfg(adc_v4)] + { + Self::SixteenBit + } + } +} + +impl Resolution { + pub(super) fn res(&self) -> crate::pac::adc::vals::Res { + match self { + #[cfg(adc_v4)] + Resolution::SixteenBit => crate::pac::adc::vals::Res::SIXTEENBIT, + #[cfg(adc_v4)] + Resolution::FourteenBit => crate::pac::adc::vals::Res::FOURTEENBITV, + Resolution::TwelveBit => crate::pac::adc::vals::Res::TWELVEBIT, + Resolution::TenBit => crate::pac::adc::vals::Res::TENBIT, + Resolution::EightBit => crate::pac::adc::vals::Res::EIGHTBIT, + #[cfg(any(adc_v2, adc_v3, adc_g0))] + Resolution::SixBit => crate::pac::adc::vals::Res::SIXBIT, + } + } + + pub fn to_max_count(&self) -> u32 { + match self { + #[cfg(adc_v4)] + Resolution::SixteenBit => (1 << 16) - 1, + #[cfg(adc_v4)] + Resolution::FourteenBit => (1 << 14) - 1, + Resolution::TwelveBit => (1 << 12) - 1, + Resolution::TenBit => (1 << 10) - 1, + Resolution::EightBit => (1 << 8) - 1, + #[cfg(any(adc_v2, adc_v3, adc_g0))] + Resolution::SixBit => (1 << 6) - 1, + } + } +} diff --git a/embassy-stm32/src/adc/v2.rs b/embassy-stm32/src/adc/v2.rs index 00d9b307..e2678fe4 100644 --- a/embassy-stm32/src/adc/v2.rs +++ b/embassy-stm32/src/adc/v2.rs @@ -4,7 +4,7 @@ use embassy_hal_common::into_ref; use embedded_hal_02::blocking::delay::DelayUs; use super::InternalChannel; -use crate::adc::{AdcPin, Instance, SampleTime}; +use crate::adc::{AdcPin, Instance, Resolution, SampleTime}; use crate::peripherals::ADC1; use crate::time::Hertz; use crate::Peripheral; @@ -17,39 +17,6 @@ pub const VREF_CALIB_MV: u32 = 3300; /// ADC turn-on time pub const ADC_POWERUP_TIME_US: u32 = 3; -pub enum Resolution { - TwelveBit, - TenBit, - EightBit, - SixBit, -} - -impl Default for Resolution { - fn default() -> Self { - Self::TwelveBit - } -} - -impl Resolution { - fn res(&self) -> crate::pac::adc::vals::Res { - match self { - Resolution::TwelveBit => crate::pac::adc::vals::Res::TWELVEBIT, - Resolution::TenBit => crate::pac::adc::vals::Res::TENBIT, - Resolution::EightBit => crate::pac::adc::vals::Res::EIGHTBIT, - Resolution::SixBit => crate::pac::adc::vals::Res::SIXBIT, - } - } - - pub fn to_max_count(&self) -> u32 { - match self { - Resolution::TwelveBit => (1 << 12) - 1, - Resolution::TenBit => (1 << 10) - 1, - Resolution::EightBit => (1 << 8) - 1, - Resolution::SixBit => (1 << 6) - 1, - } - } -} - pub struct VrefInt; impl InternalChannel for VrefInt {} impl super::sealed::InternalChannel for VrefInt { diff --git a/embassy-stm32/src/adc/v3.rs b/embassy-stm32/src/adc/v3.rs index b638e8ca..7bc130e5 100644 --- a/embassy-stm32/src/adc/v3.rs +++ b/embassy-stm32/src/adc/v3.rs @@ -3,7 +3,7 @@ use core::marker::PhantomData; use embassy_hal_common::into_ref; use embedded_hal_02::blocking::delay::DelayUs; -use crate::adc::{AdcPin, Instance, SampleTime}; +use crate::adc::{AdcPin, Instance, Resolution, SampleTime}; use crate::Peripheral; /// Default VREF voltage used for sample conversion to millivolts. @@ -24,39 +24,6 @@ fn enable() { }); } -pub enum Resolution { - TwelveBit, - TenBit, - EightBit, - SixBit, -} - -impl Default for Resolution { - fn default() -> Self { - Self::TwelveBit - } -} - -impl Resolution { - fn res(&self) -> crate::pac::adc::vals::Res { - match self { - Resolution::TwelveBit => crate::pac::adc::vals::Res::TWELVEBIT, - Resolution::TenBit => crate::pac::adc::vals::Res::TENBIT, - Resolution::EightBit => crate::pac::adc::vals::Res::EIGHTBIT, - Resolution::SixBit => crate::pac::adc::vals::Res::SIXBIT, - } - } - - pub fn to_max_count(&self) -> u32 { - match self { - Resolution::TwelveBit => (1 << 12) - 1, - Resolution::TenBit => (1 << 10) - 1, - Resolution::EightBit => (1 << 8) - 1, - Resolution::SixBit => (1 << 6) - 1, - } - } -} - pub struct VrefInt; impl AdcPin for VrefInt {} impl super::sealed::AdcPin for VrefInt { diff --git a/embassy-stm32/src/adc/v4.rs b/embassy-stm32/src/adc/v4.rs index b9222947..9dc5318e 100644 --- a/embassy-stm32/src/adc/v4.rs +++ b/embassy-stm32/src/adc/v4.rs @@ -5,7 +5,7 @@ use embedded_hal_02::blocking::delay::DelayUs; use pac::adc::vals::{Adcaldif, Boost, Difsel, Exten, Pcsel}; use pac::adccommon::vals::Presc; -use super::{AdcPin, Instance, InternalChannel, SampleTime}; +use super::{AdcPin, Instance, InternalChannel, Resolution, SampleTime}; use crate::time::Hertz; use crate::{pac, Peripheral}; @@ -14,42 +14,6 @@ pub const VREF_DEFAULT_MV: u32 = 3300; /// VREF voltage used for factory calibration of VREFINTCAL register. pub const VREF_CALIB_MV: u32 = 3300; -pub enum Resolution { - SixteenBit, - FourteenBit, - TwelveBit, - TenBit, - EightBit, -} - -impl Default for Resolution { - fn default() -> Self { - Self::SixteenBit - } -} - -impl Resolution { - fn res(&self) -> pac::adc::vals::Res { - match self { - Resolution::SixteenBit => pac::adc::vals::Res::SIXTEENBIT, - Resolution::FourteenBit => pac::adc::vals::Res::FOURTEENBITV, - Resolution::TwelveBit => pac::adc::vals::Res::TWELVEBITV, - Resolution::TenBit => pac::adc::vals::Res::TENBIT, - Resolution::EightBit => pac::adc::vals::Res::EIGHTBIT, - } - } - - pub fn to_max_count(&self) -> u32 { - match self { - Resolution::SixteenBit => (1 << 16) - 1, - Resolution::FourteenBit => (1 << 14) - 1, - Resolution::TwelveBit => (1 << 12) - 1, - Resolution::TenBit => (1 << 10) - 1, - Resolution::EightBit => (1 << 8) - 1, - } - } -} - // NOTE: Vrefint/Temperature/Vbat are only available on ADC3 on H7, this currently cannot be modeled with stm32-data, so these are available from the software on all ADCs pub struct VrefInt; impl InternalChannel for VrefInt {}