diff options
| author | Benjamin <[email protected]> | 2025-06-03 20:29:06 +0200 |
|---|---|---|
| committer | Benjamin <[email protected]> | 2025-06-03 20:29:06 +0200 |
| commit | 94080b38a5b8dec05210b5b1377d595b2640488c (patch) | |
| tree | ab4d674f17ac96c9d285ca57dcc8a3b6678c2484 | |
| parent | 141c170db426404444a454c063c2eec07c74a1c3 (diff) | |
Added Option to enable HW Oversampling in STM32 V3 ADCs. Copied from adc/v4.rs and adjusted to reflect 2 to 256x oversampling + adjusted bit shifting operations
| -rw-r--r-- | embassy-stm32/src/adc/v3.rs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/embassy-stm32/src/adc/v3.rs b/embassy-stm32/src/adc/v3.rs index 313244e19..f561f817c 100644 --- a/embassy-stm32/src/adc/v3.rs +++ b/embassy-stm32/src/adc/v3.rs | |||
| @@ -93,6 +93,18 @@ cfg_if! { | |||
| 93 | } | 93 | } |
| 94 | } | 94 | } |
| 95 | 95 | ||
| 96 | /// Number of samples used for averaging. | ||
| 97 | pub enum Averaging { | ||
| 98 | Disabled, | ||
| 99 | Samples2, | ||
| 100 | Samples4, | ||
| 101 | Samples8, | ||
| 102 | Samples16, | ||
| 103 | Samples32, | ||
| 104 | Samples64, | ||
| 105 | Samples128, | ||
| 106 | Samples256, | ||
| 107 | } | ||
| 96 | impl<'d, T: Instance> Adc<'d, T> { | 108 | impl<'d, T: Instance> Adc<'d, T> { |
| 97 | pub fn new(adc: Peri<'d, T>) -> Self { | 109 | pub fn new(adc: Peri<'d, T>) -> Self { |
| 98 | rcc::enable_and_reset::<T>(); | 110 | rcc::enable_and_reset::<T>(); |
| @@ -223,6 +235,25 @@ impl<'d, T: Instance> Adc<'d, T> { | |||
| 223 | T::regs().cfgr1().modify(|reg| reg.set_res(resolution.into())); | 235 | T::regs().cfgr1().modify(|reg| reg.set_res(resolution.into())); |
| 224 | } | 236 | } |
| 225 | 237 | ||
| 238 | pub fn set_averaging(&mut self, averaging: Averaging) { | ||
| 239 | let (enable, samples, right_shift) = match averaging { | ||
| 240 | Averaging::Disabled => (false, 0, 0), | ||
| 241 | Averaging::Samples2 => (true, 0, 1), | ||
| 242 | Averaging::Samples4 => (true, 1, 2), | ||
| 243 | Averaging::Samples8 => (true, 2, 3), | ||
| 244 | Averaging::Samples16 => (true, 3, 4), | ||
| 245 | Averaging::Samples32 => (true, 4, 5), | ||
| 246 | Averaging::Samples64 => (true, 5, 6), | ||
| 247 | Averaging::Samples128 => (true, 6, 7), | ||
| 248 | Averaging::Samples256 => (true, 7, 8), | ||
| 249 | }; | ||
| 250 | |||
| 251 | T::regs().cfgr2().modify(|reg| { | ||
| 252 | reg.set_rovse(enable); | ||
| 253 | reg.set_ovsr(samples); | ||
| 254 | reg.set_ovss(right_shift); | ||
| 255 | }) | ||
| 256 | } | ||
| 226 | /* | 257 | /* |
| 227 | /// Convert a raw sample from the `Temperature` to deg C | 258 | /// Convert a raw sample from the `Temperature` to deg C |
| 228 | pub fn to_degrees_centigrade(sample: u16) -> f32 { | 259 | pub fn to_degrees_centigrade(sample: u16) -> f32 { |
