diff options
| author | chemicstry <[email protected]> | 2022-07-27 01:17:26 +0300 |
|---|---|---|
| committer | chemicstry <[email protected]> | 2022-07-27 01:17:26 +0300 |
| commit | 046778fc53fb996445948e0cb98aaa4253fe8201 (patch) | |
| tree | b566a8a0623ddcd098bb57e939b789de7b7e57cf | |
| parent | 84cffc751ac0a38e6736959e68d441b99138d6d0 (diff) | |
Improve ADC configuration options
| -rw-r--r-- | embassy-stm32/src/adc/v2.rs | 26 | ||||
| -rw-r--r-- | embassy-stm32/src/adc/v3.rs | 42 | ||||
| -rw-r--r-- | embassy-stm32/src/adc/v4.rs | 28 | ||||
| -rw-r--r-- | examples/stm32h7/src/bin/adc.rs | 6 |
4 files changed, 68 insertions, 34 deletions
diff --git a/embassy-stm32/src/adc/v2.rs b/embassy-stm32/src/adc/v2.rs index 5c608451b..a74c00979 100644 --- a/embassy-stm32/src/adc/v2.rs +++ b/embassy-stm32/src/adc/v2.rs | |||
| @@ -7,7 +7,10 @@ use crate::adc::{AdcPin, Instance}; | |||
| 7 | use crate::time::Hertz; | 7 | use crate::time::Hertz; |
| 8 | use crate::Peripheral; | 8 | use crate::Peripheral; |
| 9 | 9 | ||
| 10 | pub const VDDA_CALIB_MV: u32 = 3000; | 10 | /// Default VREF voltage used for sample conversion to millivolts. |
| 11 | pub const VREF_DEFAULT_MV: u32 = 3300; | ||
| 12 | /// VREF voltage used for factory calibration of VREFINTCAL register. | ||
| 13 | pub const VREF_CALIB_MV: u32 = 3300; | ||
| 11 | 14 | ||
| 12 | #[cfg(not(any(rcc_f4, rcc_f7)))] | 15 | #[cfg(not(any(rcc_f4, rcc_f7)))] |
| 13 | fn enable() { | 16 | fn enable() { |
| @@ -47,7 +50,7 @@ impl Resolution { | |||
| 47 | } | 50 | } |
| 48 | } | 51 | } |
| 49 | 52 | ||
| 50 | fn to_max_count(&self) -> u32 { | 53 | pub fn to_max_count(&self) -> u32 { |
| 51 | match self { | 54 | match self { |
| 52 | Resolution::TwelveBit => (1 << 12) - 1, | 55 | Resolution::TwelveBit => (1 << 12) - 1, |
| 53 | Resolution::TenBit => (1 << 10) - 1, | 56 | Resolution::TenBit => (1 << 10) - 1, |
| @@ -57,9 +60,9 @@ impl Resolution { | |||
| 57 | } | 60 | } |
| 58 | } | 61 | } |
| 59 | 62 | ||
| 60 | pub struct Vref; | 63 | pub struct VrefInt; |
| 61 | impl<T: Instance> AdcPin<T> for Vref {} | 64 | impl<T: Instance> AdcPin<T> for VrefInt {} |
| 62 | impl<T: Instance> super::sealed::AdcPin<T> for Vref { | 65 | impl<T: Instance> super::sealed::AdcPin<T> for VrefInt { |
| 63 | fn channel(&self) -> u8 { | 66 | fn channel(&self) -> u8 { |
| 64 | 17 | 67 | 17 |
| 65 | } | 68 | } |
| @@ -150,7 +153,7 @@ impl Prescaler { | |||
| 150 | 153 | ||
| 151 | pub struct Adc<'d, T: Instance> { | 154 | pub struct Adc<'d, T: Instance> { |
| 152 | sample_time: SampleTime, | 155 | sample_time: SampleTime, |
| 153 | calibrated_vdda: u32, | 156 | vref: u32, |
| 154 | resolution: Resolution, | 157 | resolution: Resolution, |
| 155 | phantom: PhantomData<&'d mut T>, | 158 | phantom: PhantomData<&'d mut T>, |
| 156 | } | 159 | } |
| @@ -180,7 +183,7 @@ where | |||
| 180 | Self { | 183 | Self { |
| 181 | sample_time: Default::default(), | 184 | sample_time: Default::default(), |
| 182 | resolution: Resolution::default(), | 185 | resolution: Resolution::default(), |
| 183 | calibrated_vdda: VDDA_CALIB_MV, | 186 | vref: VREF_DEFAULT_MV, |
| 184 | phantom: PhantomData, | 187 | phantom: PhantomData, |
| 185 | } | 188 | } |
| 186 | } | 189 | } |
| @@ -193,9 +196,16 @@ where | |||
| 193 | self.resolution = resolution; | 196 | self.resolution = resolution; |
| 194 | } | 197 | } |
| 195 | 198 | ||
| 199 | /// Set VREF, which is used for [to_millivolts()] conversion. | ||
| 200 | /// | ||
| 201 | /// Use this if you have a known precise VREF (VDDA) pin reference voltage. | ||
| 202 | pub fn set_vref(&mut self, vref: u32) { | ||
| 203 | self.vref = vref; | ||
| 204 | } | ||
| 205 | |||
| 196 | /// Convert a measurement to millivolts | 206 | /// Convert a measurement to millivolts |
| 197 | pub fn to_millivolts(&self, sample: u16) -> u16 { | 207 | pub fn to_millivolts(&self, sample: u16) -> u16 { |
| 198 | ((u32::from(sample) * self.calibrated_vdda) / self.resolution.to_max_count()) as u16 | 208 | ((u32::from(sample) * self.vref) / self.resolution.to_max_count()) as u16 |
| 199 | } | 209 | } |
| 200 | 210 | ||
| 201 | /// Perform a single conversion. | 211 | /// Perform a single conversion. |
diff --git a/embassy-stm32/src/adc/v3.rs b/embassy-stm32/src/adc/v3.rs index dbfd18810..32e115fa1 100644 --- a/embassy-stm32/src/adc/v3.rs +++ b/embassy-stm32/src/adc/v3.rs | |||
| @@ -6,7 +6,10 @@ use embedded_hal_02::blocking::delay::DelayUs; | |||
| 6 | use crate::adc::{AdcPin, Instance}; | 6 | use crate::adc::{AdcPin, Instance}; |
| 7 | use crate::Peripheral; | 7 | use crate::Peripheral; |
| 8 | 8 | ||
| 9 | pub const VDDA_CALIB_MV: u32 = 3000; | 9 | /// Default VREF voltage used for sample conversion to millivolts. |
| 10 | pub const VREF_DEFAULT_MV: u32 = 3300; | ||
| 11 | /// VREF voltage used for factory calibration of VREFINTCAL register. | ||
| 12 | pub const VREF_CALIB_MV: u32 = 3000; | ||
| 10 | 13 | ||
| 11 | /// Sadly we cannot use `RccPeripheral::enable` since devices are quite inconsistent ADC clock | 14 | /// Sadly we cannot use `RccPeripheral::enable` since devices are quite inconsistent ADC clock |
| 12 | /// configuration. | 15 | /// configuration. |
| @@ -44,7 +47,7 @@ impl Resolution { | |||
| 44 | } | 47 | } |
| 45 | } | 48 | } |
| 46 | 49 | ||
| 47 | fn to_max_count(&self) -> u32 { | 50 | pub fn to_max_count(&self) -> u32 { |
| 48 | match self { | 51 | match self { |
| 49 | Resolution::TwelveBit => (1 << 12) - 1, | 52 | Resolution::TwelveBit => (1 << 12) - 1, |
| 50 | Resolution::TenBit => (1 << 10) - 1, | 53 | Resolution::TenBit => (1 << 10) - 1, |
| @@ -54,9 +57,9 @@ impl Resolution { | |||
| 54 | } | 57 | } |
| 55 | } | 58 | } |
| 56 | 59 | ||
| 57 | pub struct Vref; | 60 | pub struct VrefInt; |
| 58 | impl<T: Instance> AdcPin<T> for Vref {} | 61 | impl<T: Instance> AdcPin<T> for VrefInt {} |
| 59 | impl<T: Instance> super::sealed::AdcPin<T> for Vref { | 62 | impl<T: Instance> super::sealed::AdcPin<T> for VrefInt { |
| 60 | fn channel(&self) -> u8 { | 63 | fn channel(&self) -> u8 { |
| 61 | #[cfg(not(stm32g0))] | 64 | #[cfg(not(stm32g0))] |
| 62 | let val = 0; | 65 | let val = 0; |
| @@ -202,7 +205,7 @@ pub use sample_time::SampleTime; | |||
| 202 | 205 | ||
| 203 | pub struct Adc<'d, T: Instance> { | 206 | pub struct Adc<'d, T: Instance> { |
| 204 | sample_time: SampleTime, | 207 | sample_time: SampleTime, |
| 205 | calibrated_vdda: u32, | 208 | vref: u32, |
| 206 | resolution: Resolution, | 209 | resolution: Resolution, |
| 207 | phantom: PhantomData<&'d mut T>, | 210 | phantom: PhantomData<&'d mut T>, |
| 208 | } | 211 | } |
| @@ -241,12 +244,12 @@ impl<'d, T: Instance> Adc<'d, T> { | |||
| 241 | Self { | 244 | Self { |
| 242 | sample_time: Default::default(), | 245 | sample_time: Default::default(), |
| 243 | resolution: Resolution::default(), | 246 | resolution: Resolution::default(), |
| 244 | calibrated_vdda: VDDA_CALIB_MV, | 247 | vref: VREF_DEFAULT_MV, |
| 245 | phantom: PhantomData, | 248 | phantom: PhantomData, |
| 246 | } | 249 | } |
| 247 | } | 250 | } |
| 248 | 251 | ||
| 249 | pub fn enable_vref(&self, delay: &mut impl DelayUs<u32>) -> Vref { | 252 | pub fn enable_vrefint(&self, delay: &mut impl DelayUs<u32>) -> VrefInt { |
| 250 | unsafe { | 253 | unsafe { |
| 251 | T::common_regs().ccr().modify(|reg| { | 254 | T::common_regs().ccr().modify(|reg| { |
| 252 | reg.set_vrefen(true); | 255 | reg.set_vrefen(true); |
| @@ -259,7 +262,7 @@ impl<'d, T: Instance> Adc<'d, T> { | |||
| 259 | //cortex_m::asm::delay(20_000_000); | 262 | //cortex_m::asm::delay(20_000_000); |
| 260 | delay.delay_us(15); | 263 | delay.delay_us(15); |
| 261 | 264 | ||
| 262 | Vref {} | 265 | VrefInt {} |
| 263 | } | 266 | } |
| 264 | 267 | ||
| 265 | pub fn enable_temperature(&self) -> Temperature { | 268 | pub fn enable_temperature(&self) -> Temperature { |
| @@ -282,16 +285,16 @@ impl<'d, T: Instance> Adc<'d, T> { | |||
| 282 | Vbat {} | 285 | Vbat {} |
| 283 | } | 286 | } |
| 284 | 287 | ||
| 285 | /// Calculates the system VDDA by sampling the internal VREF channel and comparing | 288 | /// Calculates the system VDDA by sampling the internal VREFINT channel and comparing |
| 286 | /// the result with the value stored at the factory. If the chip's VDDA is not stable, run | 289 | /// the result with the value stored at the factory. If the chip's VDDA is not stable, run |
| 287 | /// this before each ADC conversion. | 290 | /// this before each ADC conversion. |
| 288 | #[cfg(not(stm32g0))] // TODO is this supposed to be public? | 291 | #[cfg(not(stm32g0))] // TODO is this supposed to be public? |
| 289 | #[allow(unused)] // TODO is this supposed to be public? | 292 | #[allow(unused)] // TODO is this supposed to be public? |
| 290 | fn calibrate(&mut self, vref: &mut Vref) { | 293 | fn calibrate(&mut self, vrefint: &mut VrefInt) { |
| 291 | #[cfg(stm32l5)] | 294 | #[cfg(stm32l5)] |
| 292 | let vref_cal: u32 = todo!(); | 295 | let vrefint_cal: u32 = todo!(); |
| 293 | #[cfg(not(stm32l5))] | 296 | #[cfg(not(stm32l5))] |
| 294 | let vref_cal = unsafe { crate::pac::VREFINTCAL.data().read().value() }; | 297 | let vrefint_cal = unsafe { crate::pac::VREFINTCAL.data().read().value() }; |
| 295 | let old_sample_time = self.sample_time; | 298 | let old_sample_time = self.sample_time; |
| 296 | 299 | ||
| 297 | // "Table 24. Embedded internal voltage reference" states that the sample time needs to be | 300 | // "Table 24. Embedded internal voltage reference" states that the sample time needs to be |
| @@ -300,11 +303,11 @@ impl<'d, T: Instance> Adc<'d, T> { | |||
| 300 | self.sample_time = SampleTime::Cycles640_5; | 303 | self.sample_time = SampleTime::Cycles640_5; |
| 301 | 304 | ||
| 302 | // This can't actually fail, it's just in a result to satisfy hal trait | 305 | // This can't actually fail, it's just in a result to satisfy hal trait |
| 303 | let vref_samp = self.read(vref); | 306 | let vrefint_samp = self.read(vrefint); |
| 304 | 307 | ||
| 305 | self.sample_time = old_sample_time; | 308 | self.sample_time = old_sample_time; |
| 306 | 309 | ||
| 307 | self.calibrated_vdda = (VDDA_CALIB_MV * u32::from(vref_cal)) / u32::from(vref_samp); | 310 | self.vref = (VREF_CALIB_MV * u32::from(vrefint_cal)) / u32::from(vrefint_samp); |
| 308 | } | 311 | } |
| 309 | 312 | ||
| 310 | pub fn set_sample_time(&mut self, sample_time: SampleTime) { | 313 | pub fn set_sample_time(&mut self, sample_time: SampleTime) { |
| @@ -315,9 +318,16 @@ impl<'d, T: Instance> Adc<'d, T> { | |||
| 315 | self.resolution = resolution; | 318 | self.resolution = resolution; |
| 316 | } | 319 | } |
| 317 | 320 | ||
| 321 | /// Set VREF used for [to_millivolts()] conversion. | ||
| 322 | /// | ||
| 323 | /// Use this if you have a known precise VREF (VDDA) pin reference voltage. | ||
| 324 | pub fn set_vref(&mut self, vref: u32) { | ||
| 325 | self.vref = vref; | ||
| 326 | } | ||
| 327 | |||
| 318 | /// Convert a measurement to millivolts | 328 | /// Convert a measurement to millivolts |
| 319 | pub fn to_millivolts(&self, sample: u16) -> u16 { | 329 | pub fn to_millivolts(&self, sample: u16) -> u16 { |
| 320 | ((u32::from(sample) * self.calibrated_vdda) / self.resolution.to_max_count()) as u16 | 330 | ((u32::from(sample) * self.vref) / self.resolution.to_max_count()) as u16 |
| 321 | } | 331 | } |
| 322 | 332 | ||
| 323 | /* | 333 | /* |
diff --git a/embassy-stm32/src/adc/v4.rs b/embassy-stm32/src/adc/v4.rs index 92e8ac369..0950b4661 100644 --- a/embassy-stm32/src/adc/v4.rs +++ b/embassy-stm32/src/adc/v4.rs | |||
| @@ -9,6 +9,11 @@ use super::{AdcPin, Instance}; | |||
| 9 | use crate::time::Hertz; | 9 | use crate::time::Hertz; |
| 10 | use crate::{pac, Peripheral}; | 10 | use crate::{pac, Peripheral}; |
| 11 | 11 | ||
| 12 | /// Default VREF voltage used for sample conversion to millivolts. | ||
| 13 | pub const VREF_DEFAULT_MV: u32 = 3300; | ||
| 14 | /// VREF voltage used for factory calibration of VREFINTCAL register. | ||
| 15 | pub const VREF_CALIB_MV: u32 = 3300; | ||
| 16 | |||
| 12 | pub enum Resolution { | 17 | pub enum Resolution { |
| 13 | SixteenBit, | 18 | SixteenBit, |
| 14 | FourteenBit, | 19 | FourteenBit, |
| @@ -53,10 +58,10 @@ mod sealed { | |||
| 53 | } | 58 | } |
| 54 | } | 59 | } |
| 55 | 60 | ||
| 56 | // NOTE: Vref/Temperature/Vbat are only available on ADC3 on H7, this currently cannot be modeled with stm32-data, so these are available from the software on all ADCs | 61 | // NOTE: Vrefint/Temperature/Vbat are only available on ADC3 on H7, this currently cannot be modeled with stm32-data, so these are available from the software on all ADCs |
| 57 | pub struct Vref; | 62 | pub struct VrefInt; |
| 58 | impl<T: Instance> InternalChannel<T> for Vref {} | 63 | impl<T: Instance> InternalChannel<T> for VrefInt {} |
| 59 | impl<T: Instance> sealed::InternalChannel<T> for Vref { | 64 | impl<T: Instance> sealed::InternalChannel<T> for VrefInt { |
| 60 | fn channel(&self) -> u8 { | 65 | fn channel(&self) -> u8 { |
| 61 | 19 | 66 | 19 |
| 62 | } | 67 | } |
| @@ -317,6 +322,7 @@ impl Prescaler { | |||
| 317 | 322 | ||
| 318 | pub struct Adc<'d, T: Instance> { | 323 | pub struct Adc<'d, T: Instance> { |
| 319 | sample_time: SampleTime, | 324 | sample_time: SampleTime, |
| 325 | vref: u32, | ||
| 320 | resolution: Resolution, | 326 | resolution: Resolution, |
| 321 | phantom: PhantomData<&'d mut T>, | 327 | phantom: PhantomData<&'d mut T>, |
| 322 | } | 328 | } |
| @@ -354,6 +360,7 @@ impl<'d, T: Instance + crate::rcc::RccPeripheral> Adc<'d, T> { | |||
| 354 | 360 | ||
| 355 | let mut s = Self { | 361 | let mut s = Self { |
| 356 | sample_time: Default::default(), | 362 | sample_time: Default::default(), |
| 363 | vref: VREF_DEFAULT_MV, | ||
| 357 | resolution: Resolution::default(), | 364 | resolution: Resolution::default(), |
| 358 | phantom: PhantomData, | 365 | phantom: PhantomData, |
| 359 | }; | 366 | }; |
| @@ -422,14 +429,14 @@ impl<'d, T: Instance + crate::rcc::RccPeripheral> Adc<'d, T> { | |||
| 422 | } | 429 | } |
| 423 | } | 430 | } |
| 424 | 431 | ||
| 425 | pub fn enable_vref(&self) -> Vref { | 432 | pub fn enable_vrefint(&self) -> VrefInt { |
| 426 | unsafe { | 433 | unsafe { |
| 427 | T::common_regs().ccr().modify(|reg| { | 434 | T::common_regs().ccr().modify(|reg| { |
| 428 | reg.set_vrefen(true); | 435 | reg.set_vrefen(true); |
| 429 | }); | 436 | }); |
| 430 | } | 437 | } |
| 431 | 438 | ||
| 432 | Vref {} | 439 | VrefInt {} |
| 433 | } | 440 | } |
| 434 | 441 | ||
| 435 | pub fn enable_temperature(&self) -> Temperature { | 442 | pub fn enable_temperature(&self) -> Temperature { |
| @@ -460,9 +467,16 @@ impl<'d, T: Instance + crate::rcc::RccPeripheral> Adc<'d, T> { | |||
| 460 | self.resolution = resolution; | 467 | self.resolution = resolution; |
| 461 | } | 468 | } |
| 462 | 469 | ||
| 470 | /// Set VREF used for [to_millivolts()] conversion. | ||
| 471 | /// | ||
| 472 | /// Use this if you have a known precise VREF (VDDA) pin reference voltage. | ||
| 473 | pub fn set_vref(&mut self, vref: u32) { | ||
| 474 | self.vref = vref; | ||
| 475 | } | ||
| 476 | |||
| 463 | /// Convert a measurement to millivolts | 477 | /// Convert a measurement to millivolts |
| 464 | pub fn to_millivolts(&self, sample: u16) -> u16 { | 478 | pub fn to_millivolts(&self, sample: u16) -> u16 { |
| 465 | ((u32::from(sample) * 3300) / self.resolution.to_max_count()) as u16 | 479 | ((u32::from(sample) * self.vref) / self.resolution.to_max_count()) as u16 |
| 466 | } | 480 | } |
| 467 | 481 | ||
| 468 | /// Perform a single conversion. | 482 | /// Perform a single conversion. |
diff --git a/examples/stm32h7/src/bin/adc.rs b/examples/stm32h7/src/bin/adc.rs index ce73364c0..d8a5d23d7 100644 --- a/examples/stm32h7/src/bin/adc.rs +++ b/examples/stm32h7/src/bin/adc.rs | |||
| @@ -28,11 +28,11 @@ async fn main(_spawner: Spawner, mut p: Peripherals) { | |||
| 28 | 28 | ||
| 29 | adc.set_sample_time(SampleTime::Cycles32_5); | 29 | adc.set_sample_time(SampleTime::Cycles32_5); |
| 30 | 30 | ||
| 31 | let mut vref_channel = adc.enable_vref(); | 31 | let mut vrefint_channel = adc.enable_vrefint(); |
| 32 | 32 | ||
| 33 | loop { | 33 | loop { |
| 34 | let vref = adc.read_internal(&mut vref_channel); | 34 | let vrefint = adc.read_internal(&mut vrefint_channel); |
| 35 | info!("vref: {}", vref); | 35 | info!("vrefint: {}", vrefint); |
| 36 | let measured = adc.read(&mut p.PC0); | 36 | let measured = adc.read(&mut p.PC0); |
| 37 | info!("measured: {}", measured); | 37 | info!("measured: {}", measured); |
| 38 | Timer::after(Duration::from_millis(500)).await; | 38 | Timer::after(Duration::from_millis(500)).await; |
