From 70a4a193c51876b994cbeea547747b6cfcaf2e9a Mon Sep 17 00:00:00 2001 From: xoviat Date: Sat, 9 Sep 2023 22:01:51 -0500 Subject: [PATCH] stm32: fix adc f3 and example --- embassy-stm32/src/adc/f3.rs | 9 +++------ 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; pub const VDDA_CALIB_MV: u32 = 3300; pub const ADC_MAX: u32 = (1 << 12) - 1; -// No calibration data for F103, voltage should be 1.2v -pub const VREF_INT: u32 = 1200; +pub const VREF_INT: u32 = 1230; pub struct Vref; impl AdcPin for Vref {} @@ -102,16 +101,14 @@ impl<'d, T: Instance> Adc<'d, T> { while !T::regs().isr().read().eoc() && !T::regs().isr().read().eos() {} T::regs().isr().write(|_| {}); - T::regs().dr().read().0 as u16 + T::regs().dr().read().rdata() } pub fn read(&mut self, pin: &mut impl AdcPin) -> u16 { - // pin.set_as_analog(); - Self::set_channel_sample_time(pin.channel(), self.sample_time); // Configure the channel to sample - T::regs().sqr3().write(|w| w.set_sq(0, pin.channel())); + T::regs().sqr1().write(|w| w.set_sq(0, pin.channel())); self.convert() } 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 @@ use defmt::info; use embassy_executor::Spawner; -use embassy_stm32::adc::Adc; +use embassy_stm32::adc::{Adc, VREF_INT}; use embassy_stm32::rcc::{ADCClock, ADCPrescaler}; use embassy_stm32::time::Hertz; use embassy_stm32::Config; @@ -20,15 +20,28 @@ async fn main(_spawner: Spawner) -> ! { let mut p = embassy_stm32::init(config); + info!("create adc..."); + let mut adc = Adc::new(p.ADC1, &mut Delay); - let mut vrefint = adc.enable_vref(&mut Delay); + info!("enable vrefint..."); - let _vref = adc.read(&mut vrefint); - let _pin = adc.read(&mut p.PA0); + let mut vrefint = adc.enable_vref(&mut Delay); + let mut temperature = adc.enable_temperature(); loop { - info!("Hello World!"); + let vref = adc.read(&mut vrefint); + info!("read vref: {}", vref); + + let temp = adc.read(&mut temperature); + info!("read temperature: {}", temp); + + let pin = adc.read(&mut p.PA0); + info!("read pin: {}", pin); + + let pin_mv = pin as u32 * VREF_INT as u32 / vref as u32; + info!("computed pin mv: {}", pin_mv); + Timer::after(Duration::from_secs(1)).await; } }