diff options
| -rw-r--r-- | embassy-stm32/src/adc/v3.rs | 15 | ||||
| -rw-r--r-- | examples/stm32g0/src/bin/adc_oversampling.rs | 43 |
2 files changed, 58 insertions, 0 deletions
diff --git a/embassy-stm32/src/adc/v3.rs b/embassy-stm32/src/adc/v3.rs index 398c57a92..bf7ad242b 100644 --- a/embassy-stm32/src/adc/v3.rs +++ b/embassy-stm32/src/adc/v3.rs | |||
| @@ -277,6 +277,21 @@ impl<'d, T: Instance> Adc<'d, T> { | |||
| 277 | val | 277 | val |
| 278 | } | 278 | } |
| 279 | 279 | ||
| 280 | #[cfg(any(adc_g0, adc_u0))] | ||
| 281 | pub fn set_oversampling_shift(&mut self, shift: u8) { | ||
| 282 | T::regs().cfgr2().modify(|reg| reg.set_ovss(shift)); | ||
| 283 | } | ||
| 284 | |||
| 285 | #[cfg(any(adc_g0, adc_u0))] | ||
| 286 | pub fn set_oversampling_ratio(&mut self, ratio: u8) { | ||
| 287 | T::regs().cfgr2().modify(|reg| reg.set_ovsr(ratio)); | ||
| 288 | } | ||
| 289 | |||
| 290 | #[cfg(any(adc_g0, adc_u0))] | ||
| 291 | pub fn oversampling_enable(&mut self, enable: bool) { | ||
| 292 | T::regs().cfgr2().modify(|reg| reg.set_ovse(enable)); | ||
| 293 | } | ||
| 294 | |||
| 280 | fn set_channel_sample_time(_ch: u8, sample_time: SampleTime) { | 295 | fn set_channel_sample_time(_ch: u8, sample_time: SampleTime) { |
| 281 | cfg_if! { | 296 | cfg_if! { |
| 282 | if #[cfg(any(adc_g0, adc_u0))] { | 297 | if #[cfg(any(adc_g0, adc_u0))] { |
diff --git a/examples/stm32g0/src/bin/adc_oversampling.rs b/examples/stm32g0/src/bin/adc_oversampling.rs new file mode 100644 index 000000000..3c31eb206 --- /dev/null +++ b/examples/stm32g0/src/bin/adc_oversampling.rs | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | //! adc oversampling example | ||
| 2 | //! | ||
| 3 | //! This example uses adc oversampling to achieve 16bit data | ||
| 4 | |||
| 5 | #![no_std] | ||
| 6 | #![no_main] | ||
| 7 | |||
| 8 | use defmt::*; | ||
| 9 | use embassy_executor::Spawner; | ||
| 10 | use embassy_stm32::adc::{Adc, SampleTime}; | ||
| 11 | use embassy_time::Timer; | ||
| 12 | use {defmt_rtt as _, panic_probe as _}; | ||
| 13 | |||
| 14 | #[embassy_executor::main] | ||
| 15 | async fn main(_spawner: Spawner) { | ||
| 16 | let p = embassy_stm32::init(Default::default()); | ||
| 17 | info!("Adc oversample test"); | ||
| 18 | |||
| 19 | let mut adc = Adc::new(p.ADC1); | ||
| 20 | adc.set_sample_time(SampleTime::CYCLES1_5); | ||
| 21 | let mut pin = p.PA1; | ||
| 22 | |||
| 23 | // From https://www.st.com/resource/en/reference_manual/rm0444-stm32g0x1-advanced-armbased-32bit-mcus-stmicroelectronics.pdf | ||
| 24 | // page373 15.8 Oversampler | ||
| 25 | // Table 76. Maximum output results vs N and M. Grayed values indicates truncation | ||
| 26 | // 0x00 oversampling ratio X2 | ||
| 27 | // 0x01 oversampling ratio X4 | ||
| 28 | // 0x02 oversampling ratio X8 | ||
| 29 | // 0x03 oversampling ratio X16 | ||
| 30 | // 0x04 oversampling ratio X32 | ||
| 31 | // 0x05 oversampling ratio X64 | ||
| 32 | // 0x06 oversampling ratio X128 | ||
| 33 | // 0x07 oversampling ratio X256 | ||
| 34 | adc.set_oversampling_ratio(0x03); | ||
| 35 | adc.set_oversampling_shift(0b0000); | ||
| 36 | adc.oversampling_enable(true); | ||
| 37 | |||
| 38 | loop { | ||
| 39 | let v = adc.read(&mut pin); | ||
| 40 | info!("--> {} ", v); //max 65520 = 0xFFF0 | ||
| 41 | Timer::after_millis(100).await; | ||
| 42 | } | ||
| 43 | } | ||
