aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrant Miller <[email protected]>2022-10-26 02:04:52 -0500
committerGrant Miller <[email protected]>2022-10-26 17:07:58 -0500
commit7b38b95e1036d2a62e1d0abfc41b064839497242 (patch)
treefbf115357c6f11755b1233ec55d0446c8aa8bbb3
parent51426747863f5d73f5d8c55d39add1b1aeb3356a (diff)
Refactor: Factor out `Resolution`
-rw-r--r--embassy-stm32/src/adc/mod.rs5
-rw-r--r--embassy-stm32/src/adc/resolution.rs59
-rw-r--r--embassy-stm32/src/adc/v2.rs35
-rw-r--r--embassy-stm32/src/adc/v3.rs35
-rw-r--r--embassy-stm32/src/adc/v4.rs38
5 files changed, 67 insertions, 105 deletions
diff --git a/embassy-stm32/src/adc/mod.rs b/embassy-stm32/src/adc/mod.rs
index 6232678f0..35d84f976 100644
--- a/embassy-stm32/src/adc/mod.rs
+++ b/embassy-stm32/src/adc/mod.rs
@@ -11,8 +11,13 @@ mod _version;
11#[cfg(not(adc_v1))] 11#[cfg(not(adc_v1))]
12mod sample_time; 12mod sample_time;
13 13
14#[cfg(not(any(adc_f1, adc_v1)))]
15mod resolution;
16
14#[allow(unused)] 17#[allow(unused)]
15pub use _version::*; 18pub use _version::*;
19#[cfg(not(any(adc_f1, adc_v1)))]
20pub use resolution::Resolution;
16#[cfg(not(adc_v1))] 21#[cfg(not(adc_v1))]
17pub use sample_time::SampleTime; 22pub use sample_time::SampleTime;
18 23
diff --git a/embassy-stm32/src/adc/resolution.rs b/embassy-stm32/src/adc/resolution.rs
new file mode 100644
index 000000000..354cabfb3
--- /dev/null
+++ b/embassy-stm32/src/adc/resolution.rs
@@ -0,0 +1,59 @@
1#[cfg(any(adc_v2, adc_v3, adc_g0))]
2pub enum Resolution {
3 TwelveBit,
4 TenBit,
5 EightBit,
6 SixBit,
7}
8
9#[cfg(adc_v4)]
10pub enum Resolution {
11 SixteenBit,
12 FourteenBit,
13 TwelveBit,
14 TenBit,
15 EightBit,
16}
17
18impl Default for Resolution {
19 fn default() -> Self {
20 #[cfg(any(adc_v2, adc_v3, adc_g0))]
21 {
22 Self::TwelveBit
23 }
24 #[cfg(adc_v4)]
25 {
26 Self::SixteenBit
27 }
28 }
29}
30
31impl Resolution {
32 pub(super) fn res(&self) -> crate::pac::adc::vals::Res {
33 match self {
34 #[cfg(adc_v4)]
35 Resolution::SixteenBit => crate::pac::adc::vals::Res::SIXTEENBIT,
36 #[cfg(adc_v4)]
37 Resolution::FourteenBit => crate::pac::adc::vals::Res::FOURTEENBITV,
38 Resolution::TwelveBit => crate::pac::adc::vals::Res::TWELVEBIT,
39 Resolution::TenBit => crate::pac::adc::vals::Res::TENBIT,
40 Resolution::EightBit => crate::pac::adc::vals::Res::EIGHTBIT,
41 #[cfg(any(adc_v2, adc_v3, adc_g0))]
42 Resolution::SixBit => crate::pac::adc::vals::Res::SIXBIT,
43 }
44 }
45
46 pub fn to_max_count(&self) -> u32 {
47 match self {
48 #[cfg(adc_v4)]
49 Resolution::SixteenBit => (1 << 16) - 1,
50 #[cfg(adc_v4)]
51 Resolution::FourteenBit => (1 << 14) - 1,
52 Resolution::TwelveBit => (1 << 12) - 1,
53 Resolution::TenBit => (1 << 10) - 1,
54 Resolution::EightBit => (1 << 8) - 1,
55 #[cfg(any(adc_v2, adc_v3, adc_g0))]
56 Resolution::SixBit => (1 << 6) - 1,
57 }
58 }
59}
diff --git a/embassy-stm32/src/adc/v2.rs b/embassy-stm32/src/adc/v2.rs
index 00d9b3077..e2678fe4a 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;
4use embedded_hal_02::blocking::delay::DelayUs; 4use embedded_hal_02::blocking::delay::DelayUs;
5 5
6use super::InternalChannel; 6use super::InternalChannel;
7use crate::adc::{AdcPin, Instance, SampleTime}; 7use crate::adc::{AdcPin, Instance, Resolution, SampleTime};
8use crate::peripherals::ADC1; 8use crate::peripherals::ADC1;
9use crate::time::Hertz; 9use crate::time::Hertz;
10use crate::Peripheral; 10use crate::Peripheral;
@@ -17,39 +17,6 @@ pub const VREF_CALIB_MV: u32 = 3300;
17/// ADC turn-on time 17/// ADC turn-on time
18pub const ADC_POWERUP_TIME_US: u32 = 3; 18pub const ADC_POWERUP_TIME_US: u32 = 3;
19 19
20pub enum Resolution {
21 TwelveBit,
22 TenBit,
23 EightBit,
24 SixBit,
25}
26
27impl Default for Resolution {
28 fn default() -> Self {
29 Self::TwelveBit
30 }
31}
32
33impl Resolution {
34 fn res(&self) -> crate::pac::adc::vals::Res {
35 match self {
36 Resolution::TwelveBit => crate::pac::adc::vals::Res::TWELVEBIT,
37 Resolution::TenBit => crate::pac::adc::vals::Res::TENBIT,
38 Resolution::EightBit => crate::pac::adc::vals::Res::EIGHTBIT,
39 Resolution::SixBit => crate::pac::adc::vals::Res::SIXBIT,
40 }
41 }
42
43 pub fn to_max_count(&self) -> u32 {
44 match self {
45 Resolution::TwelveBit => (1 << 12) - 1,
46 Resolution::TenBit => (1 << 10) - 1,
47 Resolution::EightBit => (1 << 8) - 1,
48 Resolution::SixBit => (1 << 6) - 1,
49 }
50 }
51}
52
53pub struct VrefInt; 20pub struct VrefInt;
54impl InternalChannel<ADC1> for VrefInt {} 21impl InternalChannel<ADC1> for VrefInt {}
55impl super::sealed::InternalChannel<ADC1> for VrefInt { 22impl super::sealed::InternalChannel<ADC1> for VrefInt {
diff --git a/embassy-stm32/src/adc/v3.rs b/embassy-stm32/src/adc/v3.rs
index b638e8ca5..7bc130e55 100644
--- a/embassy-stm32/src/adc/v3.rs
+++ b/embassy-stm32/src/adc/v3.rs
@@ -3,7 +3,7 @@ use core::marker::PhantomData;
3use embassy_hal_common::into_ref; 3use embassy_hal_common::into_ref;
4use embedded_hal_02::blocking::delay::DelayUs; 4use embedded_hal_02::blocking::delay::DelayUs;
5 5
6use crate::adc::{AdcPin, Instance, SampleTime}; 6use crate::adc::{AdcPin, Instance, Resolution, SampleTime};
7use crate::Peripheral; 7use crate::Peripheral;
8 8
9/// Default VREF voltage used for sample conversion to millivolts. 9/// Default VREF voltage used for sample conversion to millivolts.
@@ -24,39 +24,6 @@ fn enable() {
24 }); 24 });
25} 25}
26 26
27pub enum Resolution {
28 TwelveBit,
29 TenBit,
30 EightBit,
31 SixBit,
32}
33
34impl Default for Resolution {
35 fn default() -> Self {
36 Self::TwelveBit
37 }
38}
39
40impl Resolution {
41 fn res(&self) -> crate::pac::adc::vals::Res {
42 match self {
43 Resolution::TwelveBit => crate::pac::adc::vals::Res::TWELVEBIT,
44 Resolution::TenBit => crate::pac::adc::vals::Res::TENBIT,
45 Resolution::EightBit => crate::pac::adc::vals::Res::EIGHTBIT,
46 Resolution::SixBit => crate::pac::adc::vals::Res::SIXBIT,
47 }
48 }
49
50 pub fn to_max_count(&self) -> u32 {
51 match self {
52 Resolution::TwelveBit => (1 << 12) - 1,
53 Resolution::TenBit => (1 << 10) - 1,
54 Resolution::EightBit => (1 << 8) - 1,
55 Resolution::SixBit => (1 << 6) - 1,
56 }
57 }
58}
59
60pub struct VrefInt; 27pub struct VrefInt;
61impl<T: Instance> AdcPin<T> for VrefInt {} 28impl<T: Instance> AdcPin<T> for VrefInt {}
62impl<T: Instance> super::sealed::AdcPin<T> for VrefInt { 29impl<T: Instance> super::sealed::AdcPin<T> for VrefInt {
diff --git a/embassy-stm32/src/adc/v4.rs b/embassy-stm32/src/adc/v4.rs
index b9222947b..9dc5318e2 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;
5use pac::adc::vals::{Adcaldif, Boost, Difsel, Exten, Pcsel}; 5use pac::adc::vals::{Adcaldif, Boost, Difsel, Exten, Pcsel};
6use pac::adccommon::vals::Presc; 6use pac::adccommon::vals::Presc;
7 7
8use super::{AdcPin, Instance, InternalChannel, SampleTime}; 8use super::{AdcPin, Instance, InternalChannel, Resolution, SampleTime};
9use crate::time::Hertz; 9use crate::time::Hertz;
10use crate::{pac, Peripheral}; 10use crate::{pac, Peripheral};
11 11
@@ -14,42 +14,6 @@ pub const VREF_DEFAULT_MV: u32 = 3300;
14/// VREF voltage used for factory calibration of VREFINTCAL register. 14/// VREF voltage used for factory calibration of VREFINTCAL register.
15pub const VREF_CALIB_MV: u32 = 3300; 15pub const VREF_CALIB_MV: u32 = 3300;
16 16
17pub enum Resolution {
18 SixteenBit,
19 FourteenBit,
20 TwelveBit,
21 TenBit,
22 EightBit,
23}
24
25impl Default for Resolution {
26 fn default() -> Self {
27 Self::SixteenBit
28 }
29}
30
31impl Resolution {
32 fn res(&self) -> pac::adc::vals::Res {
33 match self {
34 Resolution::SixteenBit => pac::adc::vals::Res::SIXTEENBIT,
35 Resolution::FourteenBit => pac::adc::vals::Res::FOURTEENBITV,
36 Resolution::TwelveBit => pac::adc::vals::Res::TWELVEBITV,
37 Resolution::TenBit => pac::adc::vals::Res::TENBIT,
38 Resolution::EightBit => pac::adc::vals::Res::EIGHTBIT,
39 }
40 }
41
42 pub fn to_max_count(&self) -> u32 {
43 match self {
44 Resolution::SixteenBit => (1 << 16) - 1,
45 Resolution::FourteenBit => (1 << 14) - 1,
46 Resolution::TwelveBit => (1 << 12) - 1,
47 Resolution::TenBit => (1 << 10) - 1,
48 Resolution::EightBit => (1 << 8) - 1,
49 }
50 }
51}
52
53// 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 17// 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
54pub struct VrefInt; 18pub struct VrefInt;
55impl<T: Instance> InternalChannel<T> for VrefInt {} 19impl<T: Instance> InternalChannel<T> for VrefInt {}