aboutsummaryrefslogtreecommitdiff
path: root/embassy-stm32/src/adc
diff options
context:
space:
mode:
authorxoviat <[email protected]>2025-11-13 09:33:27 -0600
committerxoviat <[email protected]>2025-11-13 09:33:27 -0600
commit1a3da1c582b4a9fec1af561e2f4a7ed0352aff33 (patch)
treea2d8b4dc920589f4d916ee690216e4555f940ade /embassy-stm32/src/adc
parent84e0aefc77f7339717e9e880b08f6361f8b49722 (diff)
adc: extract v2 psc.
Diffstat (limited to 'embassy-stm32/src/adc')
-rw-r--r--embassy-stm32/src/adc/v2.rs51
1 files changed, 17 insertions, 34 deletions
diff --git a/embassy-stm32/src/adc/v2.rs b/embassy-stm32/src/adc/v2.rs
index 4065f89a7..07eaebf7c 100644
--- a/embassy-stm32/src/adc/v2.rs
+++ b/embassy-stm32/src/adc/v2.rs
@@ -3,6 +3,7 @@ use core::sync::atomic::{Ordering, compiler_fence};
3use super::{ConversionMode, Temperature, Vbat, VrefInt, blocking_delay_us}; 3use super::{ConversionMode, Temperature, Vbat, VrefInt, blocking_delay_us};
4use crate::adc::{Adc, Instance, Resolution, SampleTime}; 4use crate::adc::{Adc, Instance, Resolution, SampleTime};
5use crate::pac::adc::vals; 5use crate::pac::adc::vals;
6pub use crate::pac::adccommon::vals::Adcpre;
6use crate::time::Hertz; 7use crate::time::Hertz;
7use crate::{Peri, rcc}; 8use crate::{Peri, rcc};
8 9
@@ -50,38 +51,20 @@ impl Temperature {
50 } 51 }
51} 52}
52 53
53enum Prescaler { 54fn from_pclk2(freq: Hertz) -> Adcpre {
54 Div2, 55 // Datasheet for F2 specifies min frequency 0.6 MHz, and max 30 MHz (with VDDA 2.4-3.6V).
55 Div4, 56 #[cfg(stm32f2)]
56 Div6, 57 const MAX_FREQUENCY: Hertz = Hertz(30_000_000);
57 Div8, 58 // Datasheet for both F4 and F7 specifies min frequency 0.6 MHz, typ freq. 30 MHz and max 36 MHz.
58} 59 #[cfg(not(stm32f2))]
59 60 const MAX_FREQUENCY: Hertz = Hertz(36_000_000);
60impl Prescaler { 61 let raw_div = freq.0 / MAX_FREQUENCY.0;
61 fn from_pclk2(freq: Hertz) -> Self { 62 match raw_div {
62 // Datasheet for F2 specifies min frequency 0.6 MHz, and max 30 MHz (with VDDA 2.4-3.6V). 63 0..=1 => Adcpre::DIV2,
63 #[cfg(stm32f2)] 64 2..=3 => Adcpre::DIV4,
64 const MAX_FREQUENCY: Hertz = Hertz(30_000_000); 65 4..=5 => Adcpre::DIV6,
65 // Datasheet for both F4 and F7 specifies min frequency 0.6 MHz, typ freq. 30 MHz and max 36 MHz. 66 6..=7 => Adcpre::DIV8,
66 #[cfg(not(stm32f2))] 67 _ => panic!("Selected PCLK2 frequency is too high for ADC with largest possible prescaler."),
67 const MAX_FREQUENCY: Hertz = Hertz(36_000_000);
68 let raw_div = freq.0 / MAX_FREQUENCY.0;
69 match raw_div {
70 0..=1 => Self::Div2,
71 2..=3 => Self::Div4,
72 4..=5 => Self::Div6,
73 6..=7 => Self::Div8,
74 _ => panic!("Selected PCLK2 frequency is too high for ADC with largest possible prescaler."),
75 }
76 }
77
78 fn adcpre(&self) -> crate::pac::adccommon::vals::Adcpre {
79 match self {
80 Prescaler::Div2 => crate::pac::adccommon::vals::Adcpre::DIV2,
81 Prescaler::Div4 => crate::pac::adccommon::vals::Adcpre::DIV4,
82 Prescaler::Div6 => crate::pac::adccommon::vals::Adcpre::DIV6,
83 Prescaler::Div8 => crate::pac::adccommon::vals::Adcpre::DIV8,
84 }
85 } 68 }
86} 69}
87 70
@@ -224,8 +207,8 @@ where
224 pub fn new_with_config(adc: Peri<'d, T>, config: AdcConfig) -> Self { 207 pub fn new_with_config(adc: Peri<'d, T>, config: AdcConfig) -> Self {
225 rcc::enable_and_reset::<T>(); 208 rcc::enable_and_reset::<T>();
226 209
227 let presc = Prescaler::from_pclk2(T::frequency()); 210 let presc = from_pclk2(T::frequency());
228 T::common_regs().ccr().modify(|w| w.set_adcpre(presc.adcpre())); 211 T::common_regs().ccr().modify(|w| w.set_adcpre(presc));
229 T::regs().cr2().modify(|reg| { 212 T::regs().cr2().modify(|reg| {
230 reg.set_adon(true); 213 reg.set_adon(true);
231 }); 214 });