diff options
| author | Alexandros Liarokapis <[email protected]> | 2024-06-23 12:43:24 +0300 |
|---|---|---|
| committer | Alexandros Liarokapis <[email protected]> | 2024-06-23 12:43:24 +0300 |
| commit | 3883a5b2de75d60301307bffb64181f755c397aa (patch) | |
| tree | 703c44db1953ec3eed0a8557e8cddb15c1919455 | |
| parent | cfe8561550e10d145eb6ef14423a49f78d7ac38e (diff) | |
Enables adc v4 averaging support.
The Adc v4 peripheral includes a hardware oversampler.
This PR adds an averaging interface that keeps most of the current
interface backwards compatible while allowing for the common use-case
of hardware-averaging. A more comprehensive oversampler interface may
be exposed in the future.
| -rw-r--r-- | embassy-stm32/src/adc/v4.rs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/embassy-stm32/src/adc/v4.rs b/embassy-stm32/src/adc/v4.rs index 50db646fe..b15aebc10 100644 --- a/embassy-stm32/src/adc/v4.rs +++ b/embassy-stm32/src/adc/v4.rs | |||
| @@ -126,6 +126,21 @@ impl Prescaler { | |||
| 126 | } | 126 | } |
| 127 | } | 127 | } |
| 128 | 128 | ||
| 129 | /// Number of samples used for averaging. | ||
| 130 | pub enum Averaging { | ||
| 131 | Disabled, | ||
| 132 | Samples2, | ||
| 133 | Samples4, | ||
| 134 | Samples8, | ||
| 135 | Samples16, | ||
| 136 | Samples32, | ||
| 137 | Samples64, | ||
| 138 | Samples128, | ||
| 139 | Samples256, | ||
| 140 | Samples512, | ||
| 141 | Samples1024, | ||
| 142 | } | ||
| 143 | |||
| 129 | impl<'d, T: Instance> Adc<'d, T> { | 144 | impl<'d, T: Instance> Adc<'d, T> { |
| 130 | /// Create a new ADC driver. | 145 | /// Create a new ADC driver. |
| 131 | pub fn new(adc: impl Peripheral<P = T> + 'd) -> Self { | 146 | pub fn new(adc: impl Peripheral<P = T> + 'd) -> Self { |
| @@ -252,6 +267,29 @@ impl<'d, T: Instance> Adc<'d, T> { | |||
| 252 | T::regs().cfgr().modify(|reg| reg.set_res(resolution.into())); | 267 | T::regs().cfgr().modify(|reg| reg.set_res(resolution.into())); |
| 253 | } | 268 | } |
| 254 | 269 | ||
| 270 | /// Set hardware averaging. | ||
| 271 | pub fn set_averaging(&mut self, averaging: Averaging) { | ||
| 272 | let (enable, samples, right_shift) = match averaging { | ||
| 273 | Averaging::Disabled => (false, 0, 0), | ||
| 274 | Averaging::Samples2 => (true, 1, 1), | ||
| 275 | Averaging::Samples4 => (true, 3, 2), | ||
| 276 | Averaging::Samples8 => (true, 7, 3), | ||
| 277 | Averaging::Samples16 => (true, 15, 4), | ||
| 278 | Averaging::Samples32 => (true, 31, 5), | ||
| 279 | Averaging::Samples64 => (true, 63, 6), | ||
| 280 | Averaging::Samples128 => (true, 127, 7), | ||
| 281 | Averaging::Samples256 => (true, 255, 8), | ||
| 282 | Averaging::Samples512 => (true, 511, 9), | ||
| 283 | Averaging::Samples1024 => (true, 1023, 10), | ||
| 284 | }; | ||
| 285 | |||
| 286 | T::regs().cfgr2().modify(|reg| { | ||
| 287 | reg.set_rovse(enable); | ||
| 288 | reg.set_osvr(samples); | ||
| 289 | reg.set_ovss(right_shift); | ||
| 290 | }) | ||
| 291 | } | ||
| 292 | |||
| 255 | /// Perform a single conversion. | 293 | /// Perform a single conversion. |
| 256 | fn convert(&mut self) -> u16 { | 294 | fn convert(&mut self) -> u16 { |
| 257 | T::regs().isr().modify(|reg| { | 295 | T::regs().isr().modify(|reg| { |
