diff options
| author | Dario Nieuwenhuis <[email protected]> | 2025-07-26 11:01:18 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-07-26 11:01:18 +0000 |
| commit | 945529282aee6a9702bb56e368c6e4d2a4dd4a3c (patch) | |
| tree | dd9996f4ed4a64759f2142324992d7b3ec876933 /embassy-stm32 | |
| parent | e27ccd26e56e3a037a8be514ed0c1852149e6d2b (diff) | |
| parent | 777e0c71c99fde779cf91c364849ac6906cb3d97 (diff) | |
Merge pull request #4279 from benjaminschlegel87/stm32_adc_v3_hw_oversampling_support
Added Option to enable HW Oversampling in STM32 V3 ADCs
Diffstat (limited to 'embassy-stm32')
| -rw-r--r-- | embassy-stm32/src/adc/v3.rs | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/embassy-stm32/src/adc/v3.rs b/embassy-stm32/src/adc/v3.rs index fd74d5318..a2e42fe52 100644 --- a/embassy-stm32/src/adc/v3.rs +++ b/embassy-stm32/src/adc/v3.rs | |||
| @@ -95,6 +95,18 @@ cfg_if! { | |||
| 95 | } | 95 | } |
| 96 | } | 96 | } |
| 97 | 97 | ||
| 98 | /// Number of samples used for averaging. | ||
| 99 | pub enum Averaging { | ||
| 100 | Disabled, | ||
| 101 | Samples2, | ||
| 102 | Samples4, | ||
| 103 | Samples8, | ||
| 104 | Samples16, | ||
| 105 | Samples32, | ||
| 106 | Samples64, | ||
| 107 | Samples128, | ||
| 108 | Samples256, | ||
| 109 | } | ||
| 98 | impl<'d, T: Instance> Adc<'d, T> { | 110 | impl<'d, T: Instance> Adc<'d, T> { |
| 99 | pub fn new(adc: Peri<'d, T>) -> Self { | 111 | pub fn new(adc: Peri<'d, T>) -> Self { |
| 100 | rcc::enable_and_reset::<T>(); | 112 | rcc::enable_and_reset::<T>(); |
| @@ -225,6 +237,30 @@ impl<'d, T: Instance> Adc<'d, T> { | |||
| 225 | T::regs().cfgr1().modify(|reg| reg.set_res(resolution.into())); | 237 | T::regs().cfgr1().modify(|reg| reg.set_res(resolution.into())); |
| 226 | } | 238 | } |
| 227 | 239 | ||
| 240 | pub fn set_averaging(&mut self, averaging: Averaging) { | ||
| 241 | let (enable, samples, right_shift) = match averaging { | ||
| 242 | Averaging::Disabled => (false, 0, 0), | ||
| 243 | Averaging::Samples2 => (true, 0, 1), | ||
| 244 | Averaging::Samples4 => (true, 1, 2), | ||
| 245 | Averaging::Samples8 => (true, 2, 3), | ||
| 246 | Averaging::Samples16 => (true, 3, 4), | ||
| 247 | Averaging::Samples32 => (true, 4, 5), | ||
| 248 | Averaging::Samples64 => (true, 5, 6), | ||
| 249 | Averaging::Samples128 => (true, 6, 7), | ||
| 250 | Averaging::Samples256 => (true, 7, 8), | ||
| 251 | }; | ||
| 252 | T::regs().cfgr2().modify(|reg| { | ||
| 253 | #[cfg(not(any(adc_g0, adc_u0)))] | ||
| 254 | reg.set_rovse(enable); | ||
| 255 | #[cfg(any(adc_g0, adc_u0))] | ||
| 256 | reg.set_ovse(enable); | ||
| 257 | #[cfg(any(adc_h5, adc_h7rs))] | ||
| 258 | reg.set_ovsr(samples.into()); | ||
| 259 | #[cfg(not(any(adc_h5, adc_h7rs)))] | ||
| 260 | reg.set_ovsr(samples.into()); | ||
| 261 | reg.set_ovss(right_shift.into()); | ||
| 262 | }) | ||
| 263 | } | ||
| 228 | /* | 264 | /* |
| 229 | /// Convert a raw sample from the `Temperature` to deg C | 265 | /// Convert a raw sample from the `Temperature` to deg C |
| 230 | pub fn to_degrees_centigrade(sample: u16) -> f32 { | 266 | pub fn to_degrees_centigrade(sample: u16) -> f32 { |
