Merge pull request #1878 from xoviat/adc

stm32: fix adc f3 and example
This commit is contained in:
xoviat 2023-09-10 03:02:42 +00:00 committed by GitHub
commit a47fb42962
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 11 deletions

View file

@ -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<T: Instance> AdcPin<T> 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<T>) -> 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()
}

View file

@ -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;
}
}