diff options
| -rw-r--r-- | embassy-stm32/src/adc/f3.rs | 9 | ||||
| -rw-r--r-- | examples/stm32f334/src/bin/adc.rs | 23 |
2 files changed, 21 insertions, 11 deletions
diff --git a/embassy-stm32/src/adc/f3.rs b/embassy-stm32/src/adc/f3.rs index 458573c05..8f16c6abf 100644 --- a/embassy-stm32/src/adc/f3.rs +++ b/embassy-stm32/src/adc/f3.rs | |||
| @@ -7,8 +7,7 @@ use crate::Peripheral; | |||
| 7 | 7 | ||
| 8 | pub const VDDA_CALIB_MV: u32 = 3300; | 8 | pub const VDDA_CALIB_MV: u32 = 3300; |
| 9 | pub const ADC_MAX: u32 = (1 << 12) - 1; | 9 | pub const ADC_MAX: u32 = (1 << 12) - 1; |
| 10 | // No calibration data for F103, voltage should be 1.2v | 10 | pub const VREF_INT: u32 = 1230; |
| 11 | pub const VREF_INT: u32 = 1200; | ||
| 12 | 11 | ||
| 13 | pub struct Vref; | 12 | pub struct Vref; |
| 14 | impl<T: Instance> AdcPin<T> for Vref {} | 13 | impl<T: Instance> AdcPin<T> for Vref {} |
| @@ -102,16 +101,14 @@ impl<'d, T: Instance> Adc<'d, T> { | |||
| 102 | while !T::regs().isr().read().eoc() && !T::regs().isr().read().eos() {} | 101 | while !T::regs().isr().read().eoc() && !T::regs().isr().read().eos() {} |
| 103 | T::regs().isr().write(|_| {}); | 102 | T::regs().isr().write(|_| {}); |
| 104 | 103 | ||
| 105 | T::regs().dr().read().0 as u16 | 104 | T::regs().dr().read().rdata() |
| 106 | } | 105 | } |
| 107 | 106 | ||
| 108 | pub fn read(&mut self, pin: &mut impl AdcPin<T>) -> u16 { | 107 | pub fn read(&mut self, pin: &mut impl AdcPin<T>) -> u16 { |
| 109 | // pin.set_as_analog(); | ||
| 110 | |||
| 111 | Self::set_channel_sample_time(pin.channel(), self.sample_time); | 108 | Self::set_channel_sample_time(pin.channel(), self.sample_time); |
| 112 | 109 | ||
| 113 | // Configure the channel to sample | 110 | // Configure the channel to sample |
| 114 | T::regs().sqr3().write(|w| w.set_sq(0, pin.channel())); | 111 | T::regs().sqr1().write(|w| w.set_sq(0, pin.channel())); |
| 115 | self.convert() | 112 | self.convert() |
| 116 | } | 113 | } |
| 117 | 114 | ||
diff --git a/examples/stm32f334/src/bin/adc.rs b/examples/stm32f334/src/bin/adc.rs index 729497e82..6dffd6731 100644 --- a/examples/stm32f334/src/bin/adc.rs +++ b/examples/stm32f334/src/bin/adc.rs | |||
| @@ -4,7 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | use defmt::info; | 5 | use defmt::info; |
| 6 | use embassy_executor::Spawner; | 6 | use embassy_executor::Spawner; |
| 7 | use embassy_stm32::adc::Adc; | 7 | use embassy_stm32::adc::{Adc, VREF_INT}; |
| 8 | use embassy_stm32::rcc::{ADCClock, ADCPrescaler}; | 8 | use embassy_stm32::rcc::{ADCClock, ADCPrescaler}; |
| 9 | use embassy_stm32::time::Hertz; | 9 | use embassy_stm32::time::Hertz; |
| 10 | use embassy_stm32::Config; | 10 | use embassy_stm32::Config; |
| @@ -20,15 +20,28 @@ async fn main(_spawner: Spawner) -> ! { | |||
| 20 | 20 | ||
| 21 | let mut p = embassy_stm32::init(config); | 21 | let mut p = embassy_stm32::init(config); |
| 22 | 22 | ||
| 23 | info!("create adc..."); | ||
| 24 | |||
| 23 | let mut adc = Adc::new(p.ADC1, &mut Delay); | 25 | let mut adc = Adc::new(p.ADC1, &mut Delay); |
| 24 | 26 | ||
| 25 | let mut vrefint = adc.enable_vref(&mut Delay); | 27 | info!("enable vrefint..."); |
| 26 | 28 | ||
| 27 | let _vref = adc.read(&mut vrefint); | 29 | let mut vrefint = adc.enable_vref(&mut Delay); |
| 28 | let _pin = adc.read(&mut p.PA0); | 30 | let mut temperature = adc.enable_temperature(); |
| 29 | 31 | ||
| 30 | loop { | 32 | loop { |
| 31 | info!("Hello World!"); | 33 | let vref = adc.read(&mut vrefint); |
| 34 | info!("read vref: {}", vref); | ||
| 35 | |||
| 36 | let temp = adc.read(&mut temperature); | ||
| 37 | info!("read temperature: {}", temp); | ||
| 38 | |||
| 39 | let pin = adc.read(&mut p.PA0); | ||
| 40 | info!("read pin: {}", pin); | ||
| 41 | |||
| 42 | let pin_mv = pin as u32 * VREF_INT as u32 / vref as u32; | ||
| 43 | info!("computed pin mv: {}", pin_mv); | ||
| 44 | |||
| 32 | Timer::after(Duration::from_secs(1)).await; | 45 | Timer::after(Duration::from_secs(1)).await; |
| 33 | } | 46 | } |
| 34 | } | 47 | } |
