diff options
| -rw-r--r-- | embassy-stm32/src/adc/g4.rs | 66 | ||||
| -rw-r--r-- | embassy-stm32/src/adc/mod.rs | 1 | ||||
| -rw-r--r-- | examples/stm32g4/src/bin/adc_differential.rs | 47 | ||||
| -rw-r--r-- | examples/stm32g4/src/bin/adc_oversampling.rs | 57 |
4 files changed, 171 insertions, 0 deletions
diff --git a/embassy-stm32/src/adc/g4.rs b/embassy-stm32/src/adc/g4.rs index c1e584f59..3e9ba8ae2 100644 --- a/embassy-stm32/src/adc/g4.rs +++ b/embassy-stm32/src/adc/g4.rs | |||
| @@ -1,5 +1,9 @@ | |||
| 1 | #[allow(unused)] | 1 | #[allow(unused)] |
| 2 | #[cfg(stm32h7)] | ||
| 2 | use pac::adc::vals::{Adcaldif, Difsel, Exten}; | 3 | use pac::adc::vals::{Adcaldif, Difsel, Exten}; |
| 4 | #[allow(unused)] | ||
| 5 | #[cfg(stm32g4)] | ||
| 6 | use pac::adc::vals::{Adcaldif, Difsel, Exten, Rovsm, Trovs}; | ||
| 3 | use pac::adccommon::vals::Presc; | 7 | use pac::adccommon::vals::Presc; |
| 4 | 8 | ||
| 5 | use super::{blocking_delay_us, Adc, AdcChannel, Instance, Resolution, SampleTime}; | 9 | use super::{blocking_delay_us, Adc, AdcChannel, Instance, Resolution, SampleTime}; |
| @@ -228,6 +232,68 @@ impl<'d, T: Instance> Adc<'d, T> { | |||
| 228 | Vbat {} | 232 | Vbat {} |
| 229 | } | 233 | } |
| 230 | 234 | ||
| 235 | /// Enable differential channel. | ||
| 236 | /// Caution: | ||
| 237 | /// : When configuring the channel āiā in differential input mode, its negative input voltage VINN[i] | ||
| 238 | /// is connected to another channel. As a consequence, this channel is no longer usable in | ||
| 239 | /// single-ended mode or in differential mode and must never be configured to be converted. | ||
| 240 | /// Some channels are shared between ADC1/ADC2/ADC3/ADC4/ADC5: this can make the | ||
| 241 | /// channel on the other ADC unusable. The only exception is when ADC master and the slave | ||
| 242 | /// operate in interleaved mode. | ||
| 243 | #[cfg(stm32g4)] | ||
| 244 | pub fn set_differential_channel(&mut self, ch: usize, enable: bool) { | ||
| 245 | T::regs().cr().modify(|w| w.set_aden(false)); // disable adc | ||
| 246 | T::regs().difsel().modify(|w| { | ||
| 247 | w.set_difsel( | ||
| 248 | ch, | ||
| 249 | if enable { | ||
| 250 | Difsel::DIFFERENTIAL | ||
| 251 | } else { | ||
| 252 | Difsel::SINGLEENDED | ||
| 253 | }, | ||
| 254 | ); | ||
| 255 | }); | ||
| 256 | T::regs().cr().modify(|w| w.set_aden(true)); | ||
| 257 | } | ||
| 258 | |||
| 259 | #[cfg(stm32g4)] | ||
| 260 | pub fn set_differential(&mut self, channel: &mut impl AdcChannel<T>, enable: bool) { | ||
| 261 | self.set_differential_channel(channel.channel() as usize, enable); | ||
| 262 | } | ||
| 263 | |||
| 264 | /// Set oversampling shift. | ||
| 265 | #[cfg(stm32g4)] | ||
| 266 | pub fn set_oversampling_shift(&mut self, shift: u8) { | ||
| 267 | T::regs().cfgr2().modify(|reg| reg.set_ovss(shift)); | ||
| 268 | } | ||
| 269 | |||
| 270 | /// Set oversampling ratio. | ||
| 271 | #[cfg(stm32g4)] | ||
| 272 | pub fn set_oversampling_ratio(&mut self, ratio: u8) { | ||
| 273 | T::regs().cfgr2().modify(|reg| reg.set_ovsr(ratio)); | ||
| 274 | } | ||
| 275 | |||
| 276 | /// Enable oversampling in regular mode. | ||
| 277 | #[cfg(stm32g4)] | ||
| 278 | pub fn enable_regular_oversampling_mode(&mut self, mode: Rovsm, trig_mode: Trovs, enable: bool) { | ||
| 279 | T::regs().cfgr2().modify(|reg| reg.set_trovs(trig_mode)); | ||
| 280 | T::regs().cfgr2().modify(|reg| reg.set_rovsm(mode)); | ||
| 281 | T::regs().cfgr2().modify(|reg| reg.set_rovse(enable)); | ||
| 282 | } | ||
| 283 | |||
| 284 | // Reads that are not implemented as INJECTED in "blocking_read" | ||
| 285 | // #[cfg(stm32g4)] | ||
| 286 | // pub fn enalble_injected_oversampling_mode(&mut self, enable: bool) { | ||
| 287 | // T::regs().cfgr2().modify(|reg| reg.set_jovse(enable)); | ||
| 288 | // } | ||
| 289 | |||
| 290 | // #[cfg(stm32g4)] | ||
| 291 | // pub fn enable_oversampling_regular_injected_mode(&mut self, enable: bool) { | ||
| 292 | // // the regularoversampling mode is forced to resumed mode (ROVSM bit ignored), | ||
| 293 | // T::regs().cfgr2().modify(|reg| reg.set_rovse(enable)); | ||
| 294 | // T::regs().cfgr2().modify(|reg| reg.set_jovse(enable)); | ||
| 295 | // } | ||
| 296 | |||
| 231 | /// Set the ADC sample time. | 297 | /// Set the ADC sample time. |
| 232 | pub fn set_sample_time(&mut self, sample_time: SampleTime) { | 298 | pub fn set_sample_time(&mut self, sample_time: SampleTime) { |
| 233 | self.sample_time = sample_time; | 299 | self.sample_time = sample_time; |
diff --git a/embassy-stm32/src/adc/mod.rs b/embassy-stm32/src/adc/mod.rs index 2f36df240..2ac0c0083 100644 --- a/embassy-stm32/src/adc/mod.rs +++ b/embassy-stm32/src/adc/mod.rs | |||
| @@ -24,6 +24,7 @@ pub use _version::*; | |||
| 24 | #[cfg(any(adc_f1, adc_f3, adc_v1, adc_l0, adc_f3_v1_1))] | 24 | #[cfg(any(adc_f1, adc_f3, adc_v1, adc_l0, adc_f3_v1_1))] |
| 25 | use embassy_sync::waitqueue::AtomicWaker; | 25 | use embassy_sync::waitqueue::AtomicWaker; |
| 26 | 26 | ||
| 27 | pub use crate::pac::adc::vals; | ||
| 27 | #[cfg(not(any(adc_f1, adc_f3_v2)))] | 28 | #[cfg(not(any(adc_f1, adc_f3_v2)))] |
| 28 | pub use crate::pac::adc::vals::Res as Resolution; | 29 | pub use crate::pac::adc::vals::Res as Resolution; |
| 29 | pub use crate::pac::adc::vals::SampleTime; | 30 | pub use crate::pac::adc::vals::SampleTime; |
diff --git a/examples/stm32g4/src/bin/adc_differential.rs b/examples/stm32g4/src/bin/adc_differential.rs new file mode 100644 index 000000000..78d071d45 --- /dev/null +++ b/examples/stm32g4/src/bin/adc_differential.rs | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | //! adc differential mode example | ||
| 2 | //! | ||
| 3 | //! This example uses adc1 in differential mode | ||
| 4 | //! p:pa0 n:pa1 | ||
| 5 | |||
| 6 | #![no_std] | ||
| 7 | #![no_main] | ||
| 8 | |||
| 9 | use defmt::*; | ||
| 10 | use embassy_executor::Spawner; | ||
| 11 | use embassy_stm32::adc::{Adc, SampleTime}; | ||
| 12 | use embassy_stm32::Config; | ||
| 13 | use embassy_time::Timer; | ||
| 14 | use {defmt_rtt as _, panic_probe as _}; | ||
| 15 | |||
| 16 | #[embassy_executor::main] | ||
| 17 | async fn main(_spawner: Spawner) { | ||
| 18 | let mut config = Config::default(); | ||
| 19 | { | ||
| 20 | use embassy_stm32::rcc::*; | ||
| 21 | config.rcc.pll = Some(Pll { | ||
| 22 | source: PllSource::HSI, | ||
| 23 | prediv: PllPreDiv::DIV4, | ||
| 24 | mul: PllMul::MUL85, | ||
| 25 | divp: None, | ||
| 26 | divq: None, | ||
| 27 | // Main system clock at 170 MHz | ||
| 28 | divr: Some(PllRDiv::DIV2), | ||
| 29 | }); | ||
| 30 | config.rcc.mux.adc12sel = mux::Adcsel::SYS; | ||
| 31 | config.rcc.sys = Sysclk::PLL1_R; | ||
| 32 | } | ||
| 33 | let mut p = embassy_stm32::init(config); | ||
| 34 | |||
| 35 | let mut adc = Adc::new(p.ADC1); | ||
| 36 | adc.set_sample_time(SampleTime::CYCLES247_5); | ||
| 37 | adc.set_differential(&mut p.PA0, true); //p:pa0,n:pa1 | ||
| 38 | |||
| 39 | // can also use | ||
| 40 | // adc.set_differential_channel(1, true); | ||
| 41 | info!("adc initialized"); | ||
| 42 | loop { | ||
| 43 | let measured = adc.blocking_read(&mut p.PA0); | ||
| 44 | info!("data: {}", measured); | ||
| 45 | Timer::after_millis(500).await; | ||
| 46 | } | ||
| 47 | } | ||
diff --git a/examples/stm32g4/src/bin/adc_oversampling.rs b/examples/stm32g4/src/bin/adc_oversampling.rs new file mode 100644 index 000000000..d31eb20f8 --- /dev/null +++ b/examples/stm32g4/src/bin/adc_oversampling.rs | |||
| @@ -0,0 +1,57 @@ | |||
| 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::vals::{Rovsm, Trovs}; | ||
| 11 | use embassy_stm32::adc::{Adc, SampleTime}; | ||
| 12 | use embassy_stm32::Config; | ||
| 13 | use embassy_time::Timer; | ||
| 14 | use {defmt_rtt as _, panic_probe as _}; | ||
| 15 | |||
| 16 | #[embassy_executor::main] | ||
| 17 | async fn main(_spawner: Spawner) { | ||
| 18 | let mut config = Config::default(); | ||
| 19 | { | ||
| 20 | use embassy_stm32::rcc::*; | ||
| 21 | config.rcc.pll = Some(Pll { | ||
| 22 | source: PllSource::HSI, | ||
| 23 | prediv: PllPreDiv::DIV4, | ||
| 24 | mul: PllMul::MUL85, | ||
| 25 | divp: None, | ||
| 26 | divq: None, | ||
| 27 | // Main system clock at 170 MHz | ||
| 28 | divr: Some(PllRDiv::DIV2), | ||
| 29 | }); | ||
| 30 | config.rcc.mux.adc12sel = mux::Adcsel::SYS; | ||
| 31 | config.rcc.sys = Sysclk::PLL1_R; | ||
| 32 | } | ||
| 33 | let mut p = embassy_stm32::init(config); | ||
| 34 | |||
| 35 | let mut adc = Adc::new(p.ADC1); | ||
| 36 | adc.set_sample_time(SampleTime::CYCLES6_5); | ||
| 37 | // From https://www.st.com/resource/en/reference_manual/rm0440-stm32g4-series-advanced-armbased-32bit-mcus-stmicroelectronics.pdf | ||
| 38 | // page652 Oversampler | ||
| 39 | // Table 172. Maximum output results vs N and M. Grayed values indicates truncation | ||
| 40 | // 0x00 oversampling ratio X2 | ||
| 41 | // 0x01 oversampling ratio X4 | ||
| 42 | // 0x02 oversampling ratio X8 | ||
| 43 | // 0x03 oversampling ratio X16 | ||
| 44 | // 0x04 oversampling ratio X32 | ||
| 45 | // 0x05 oversampling ratio X64 | ||
| 46 | // 0x06 oversampling ratio X128 | ||
| 47 | // 0x07 oversampling ratio X256 | ||
| 48 | adc.set_oversampling_ratio(0x03); // ratio X3 | ||
| 49 | adc.set_oversampling_shift(0b0000); // no shift | ||
| 50 | adc.enable_regular_oversampling_mode(Rovsm::RESUMED, Trovs::AUTOMATIC, true); | ||
| 51 | |||
| 52 | loop { | ||
| 53 | let measured = adc.blocking_read(&mut p.PA0); | ||
| 54 | info!("data: 0x{:X}", measured); //max 0xFFF0 -> 65520 | ||
| 55 | Timer::after_millis(500).await; | ||
| 56 | } | ||
| 57 | } | ||
