diff options
| author | Chen Yuheng <[email protected]> | 2024-07-11 10:33:43 +0800 |
|---|---|---|
| committer | Chen Yuheng <[email protected]> | 2024-07-11 10:33:43 +0800 |
| commit | f01ffbcc12c40c68a57bc2daffdfad697bc28921 (patch) | |
| tree | ca32b8dd65d7abeb050eebdf72e4b13b765c2438 /embassy-stm32/src | |
| parent | 6636a5835b27f82abea9000c9f3e93b4455b29c8 (diff) | |
Add oversampling and differential for g4
Diffstat (limited to 'embassy-stm32/src')
| -rw-r--r-- | embassy-stm32/src/adc/g4.rs | 60 | ||||
| -rw-r--r-- | embassy-stm32/src/adc/mod.rs | 1 |
2 files changed, 61 insertions, 0 deletions
diff --git a/embassy-stm32/src/adc/g4.rs b/embassy-stm32/src/adc/g4.rs index c1e584f59..896a70578 100644 --- a/embassy-stm32/src/adc/g4.rs +++ b/embassy-stm32/src/adc/g4.rs | |||
| @@ -1,4 +1,8 @@ | |||
| 1 | #[allow(unused)] | 1 | #[allow(unused)] |
| 2 | |||
| 3 | #[cfg(stm32g4)] | ||
| 4 | use pac::adc::vals::{Adcaldif, Difsel, Exten, Rovsm, Trovs}; | ||
| 5 | #[cfg(stm32h7)] | ||
| 2 | use pac::adc::vals::{Adcaldif, Difsel, Exten}; | 6 | use pac::adc::vals::{Adcaldif, Difsel, Exten}; |
| 3 | use pac::adccommon::vals::Presc; | 7 | use pac::adccommon::vals::Presc; |
| 4 | 8 | ||
| @@ -228,6 +232,62 @@ 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(ch, if enable { Difsel::DIFFERENTIAL } else { Difsel::SINGLEENDED }); | ||
| 248 | }); | ||
| 249 | T::regs().cr().modify(|w| w.set_aden(true)); | ||
| 250 | } | ||
| 251 | |||
| 252 | #[cfg(stm32g4)] | ||
| 253 | pub fn set_differential(&mut self, channel: &mut impl AdcChannel<T>, enable: bool) { | ||
| 254 | self.set_differential_channel(channel.channel() as usize, enable); | ||
| 255 | } | ||
| 256 | |||
| 257 | /// Set oversampling shift. | ||
| 258 | #[cfg(stm32g4)] | ||
| 259 | pub fn set_oversampling_shift(&mut self, shift: u8) { | ||
| 260 | T::regs().cfgr2().modify(|reg| reg.set_ovss(shift)); | ||
| 261 | } | ||
| 262 | |||
| 263 | /// Set oversampling ratio. | ||
| 264 | #[cfg(stm32g4)] | ||
| 265 | pub fn set_oversampling_ratio(&mut self, ratio: u8) { | ||
| 266 | T::regs().cfgr2().modify(|reg| reg.set_ovsr(ratio)); | ||
| 267 | } | ||
| 268 | |||
| 269 | /// Enable oversampling in regular mode. | ||
| 270 | #[cfg(stm32g4)] | ||
| 271 | pub fn enable_regular_oversampling_mode(&mut self,mode:Rovsm,trig_mode:Trovs, enable: bool) { | ||
| 272 | T::regs().cfgr2().modify(|reg| reg.set_trovs(trig_mode)); | ||
| 273 | T::regs().cfgr2().modify(|reg| reg.set_rovsm(mode)); | ||
| 274 | T::regs().cfgr2().modify(|reg| reg.set_rovse(enable)); | ||
| 275 | } | ||
| 276 | |||
| 277 | // Reads that are not implemented as INJECTED in "blocking_read" | ||
| 278 | // #[cfg(stm32g4)] | ||
| 279 | // pub fn enalble_injected_oversampling_mode(&mut self, enable: bool) { | ||
| 280 | // T::regs().cfgr2().modify(|reg| reg.set_jovse(enable)); | ||
| 281 | // } | ||
| 282 | |||
| 283 | // #[cfg(stm32g4)] | ||
| 284 | // pub fn enable_oversampling_regular_injected_mode(&mut self, enable: bool) { | ||
| 285 | // // the regularoversampling mode is forced to resumed mode (ROVSM bit ignored), | ||
| 286 | // T::regs().cfgr2().modify(|reg| reg.set_rovse(enable)); | ||
| 287 | // T::regs().cfgr2().modify(|reg| reg.set_jovse(enable)); | ||
| 288 | // } | ||
| 289 | |||
| 290 | |||
| 231 | /// Set the ADC sample time. | 291 | /// Set the ADC sample time. |
| 232 | pub fn set_sample_time(&mut self, sample_time: SampleTime) { | 292 | pub fn set_sample_time(&mut self, sample_time: SampleTime) { |
| 233 | self.sample_time = sample_time; | 293 | self.sample_time = sample_time; |
diff --git a/embassy-stm32/src/adc/mod.rs b/embassy-stm32/src/adc/mod.rs index 7a7d7cd8e..b530bb4c8 100644 --- a/embassy-stm32/src/adc/mod.rs +++ b/embassy-stm32/src/adc/mod.rs | |||
| @@ -26,6 +26,7 @@ use embassy_sync::waitqueue::AtomicWaker; | |||
| 26 | #[cfg(not(any(adc_f1, adc_f3_v2)))] | 26 | #[cfg(not(any(adc_f1, adc_f3_v2)))] |
| 27 | pub use crate::pac::adc::vals::Res as Resolution; | 27 | pub use crate::pac::adc::vals::Res as Resolution; |
| 28 | pub use crate::pac::adc::vals::SampleTime; | 28 | pub use crate::pac::adc::vals::SampleTime; |
| 29 | pub use crate::pac::adc::vals ; | ||
| 29 | use crate::peripherals; | 30 | use crate::peripherals; |
| 30 | 31 | ||
| 31 | dma_trait!(RxDma, Instance); | 32 | dma_trait!(RxDma, Instance); |
