From e640933e2f0c8fb0b5a6df645002f69b55f51d8a Mon Sep 17 00:00:00 2001 From: xoviat Date: Mon, 18 Sep 2023 16:31:20 -0500 Subject: [PATCH] stm32/adc: add async conversion --- embassy-stm32/src/adc/f3.rs | 65 +++++++++++++++++++++++++++---- embassy-stm32/src/adc/mod.rs | 39 ++++++++++++++++++- examples/stm32f334/src/bin/adc.rs | 23 ++++++----- 3 files changed, 109 insertions(+), 18 deletions(-) diff --git a/embassy-stm32/src/adc/f3.rs b/embassy-stm32/src/adc/f3.rs index b39d6ac8..5d2ea1da 100644 --- a/embassy-stm32/src/adc/f3.rs +++ b/embassy-stm32/src/adc/f3.rs @@ -1,14 +1,36 @@ +use core::future::poll_fn; +use core::marker::PhantomData; +use core::task::Poll; + use embassy_hal_internal::into_ref; use embedded_hal_02::blocking::delay::DelayUs; use crate::adc::{Adc, AdcPin, Instance, SampleTime}; +use crate::interrupt::typelevel::Interrupt; use crate::time::Hertz; -use crate::Peripheral; +use crate::{interrupt, Peripheral}; pub const VDDA_CALIB_MV: u32 = 3300; pub const ADC_MAX: u32 = (1 << 12) - 1; pub const VREF_INT: u32 = 1230; +/// Interrupt handler. +pub struct InterruptHandler { + _phantom: PhantomData, +} + +impl interrupt::typelevel::Handler for InterruptHandler { + unsafe fn on_interrupt() { + if T::regs().isr().read().eoc() { + T::regs().ier().modify(|w| w.set_eocie(false)); + } else { + return; + } + + T::state().waker.wake(); + } +} + pub struct Vref; impl AdcPin for Vref {} impl super::sealed::AdcPin for Vref { @@ -17,6 +39,13 @@ impl super::sealed::AdcPin for Vref { } } +impl Vref { + /// The value that vref would be if vdda was at 3300mv + pub fn value(&self) -> u16 { + crate::pac::VREFINTCAL.data().read().value() + } +} + pub struct Temperature; impl AdcPin for Temperature {} impl super::sealed::AdcPin for Temperature { @@ -26,7 +55,11 @@ impl super::sealed::AdcPin for Temperature { } impl<'d, T: Instance> Adc<'d, T> { - pub fn new(adc: impl Peripheral

+ 'd, delay: &mut impl DelayUs) -> Self { + pub fn new( + adc: impl Peripheral

+ 'd, + _irq: impl interrupt::typelevel::Binding> + 'd, + delay: &mut impl DelayUs, + ) -> Self { use crate::pac::adc::vals; into_ref!(adc); @@ -58,6 +91,11 @@ impl<'d, T: Instance> Adc<'d, T> { // Wait until the adc is ready while !T::regs().isr().read().adrdy() {} + T::Interrupt::unpend(); + unsafe { + T::Interrupt::enable(); + } + Self { adc, sample_time: Default::default(), @@ -97,30 +135,41 @@ impl<'d, T: Instance> Adc<'d, T> { } /// Perform a single conversion. - fn convert(&mut self) -> u16 { + async fn convert(&mut self) -> u16 { T::regs().isr().write(|_| {}); + T::regs().ier().modify(|w| w.set_eocie(true)); T::regs().cr().modify(|w| w.set_adstart(true)); - while !T::regs().isr().read().eoc() && !T::regs().isr().read().eos() {} + poll_fn(|cx| { + T::state().waker.register(cx.waker()); + + if T::regs().isr().read().eoc() { + Poll::Ready(()) + } else { + Poll::Pending + } + }) + .await; + T::regs().isr().write(|_| {}); T::regs().dr().read().rdata() } - pub fn read(&mut self, pin: &mut impl AdcPin) -> u16 { + pub async fn read(&mut self, pin: &mut impl AdcPin) -> u16 { Self::set_channel_sample_time(pin.channel(), self.sample_time); // Configure the channel to sample T::regs().sqr1().write(|w| w.set_sq(0, pin.channel())); - self.convert() + self.convert().await } fn set_channel_sample_time(ch: u8, sample_time: SampleTime) { let sample_time = sample_time.into(); if ch <= 9 { - T::regs().smpr2().modify(|reg| reg.set_smp(ch as _, sample_time)); + T::regs().smpr1().modify(|reg| reg.set_smp(ch as _, sample_time)); } else { - T::regs().smpr1().modify(|reg| reg.set_smp((ch - 10) as _, sample_time)); + T::regs().smpr2().modify(|reg| reg.set_smp((ch - 10) as _, sample_time)); } } } diff --git a/embassy-stm32/src/adc/mod.rs b/embassy-stm32/src/adc/mod.rs index 9334deac..e74913da 100644 --- a/embassy-stm32/src/adc/mod.rs +++ b/embassy-stm32/src/adc/mod.rs @@ -31,12 +31,35 @@ pub struct Adc<'d, T: Instance> { } pub(crate) mod sealed { - pub trait Instance { + #[cfg(adc_f3)] + use embassy_sync::waitqueue::AtomicWaker; + + #[cfg(adc_f3)] + pub struct State { + pub waker: AtomicWaker, + } + + #[cfg(adc_f3)] + impl State { + pub const fn new() -> Self { + Self { + waker: AtomicWaker::new(), + } + } + } + + pub trait InterruptableInstance { + type Interrupt: crate::interrupt::typelevel::Interrupt; + } + + pub trait Instance: InterruptableInstance { fn regs() -> crate::pac::adc::Adc; #[cfg(not(any(adc_f1, adc_v1, adc_f3_v2, adc_g0)))] fn common_regs() -> crate::pac::adccommon::AdcCommon; #[cfg(adc_f3)] fn frequency() -> crate::time::Hertz; + #[cfg(adc_f3)] + fn state() -> &'static State; } pub trait AdcPin { @@ -72,8 +95,22 @@ foreach_adc!( fn frequency() -> crate::time::Hertz { unsafe { crate::rcc::get_freqs() }.$clock.unwrap() } + + #[cfg(adc_f3)] + fn state() -> &'static sealed::State { + static STATE: sealed::State = sealed::State::new(); + &STATE + } } + foreach_interrupt!( + ($inst,adc,ADC,GLOBAL,$irq:ident) => { + impl sealed::InterruptableInstance for peripherals::$inst { + type Interrupt = crate::interrupt::typelevel::$irq; + } + }; + ); + impl crate::adc::Instance for peripherals::$inst {} }; ); diff --git a/examples/stm32f334/src/bin/adc.rs b/examples/stm32f334/src/bin/adc.rs index e8b2cd8a..ed246a7d 100644 --- a/examples/stm32f334/src/bin/adc.rs +++ b/examples/stm32f334/src/bin/adc.rs @@ -4,13 +4,18 @@ use defmt::info; use embassy_executor::Spawner; -use embassy_stm32::adc::{Adc, SampleTime, VREF_INT}; +use embassy_stm32::adc::{Adc, SampleTime}; +use embassy_stm32::peripherals::ADC1; use embassy_stm32::rcc::AdcClockSource; use embassy_stm32::time::mhz; -use embassy_stm32::Config; +use embassy_stm32::{adc, bind_interrupts, Config}; use embassy_time::{Delay, Duration, Timer}; use {defmt_rtt as _, panic_probe as _}; +bind_interrupts!(struct Irqs { + ADC1_2 => adc::InterruptHandler; +}); + #[embassy_executor::main] async fn main(_spawner: Spawner) -> ! { let mut config = Config::default(); @@ -24,7 +29,7 @@ async fn main(_spawner: Spawner) -> ! { info!("create adc..."); - let mut adc = Adc::new(p.ADC1, &mut Delay); + let mut adc = Adc::new(p.ADC1, Irqs, &mut Delay); adc.set_sample_time(SampleTime::Cycles601_5); @@ -34,18 +39,18 @@ async fn main(_spawner: Spawner) -> ! { let mut temperature = adc.enable_temperature(); loop { - let vref = adc.read(&mut vrefint); - info!("read vref: {}", vref); + let vref = adc.read(&mut vrefint).await; + info!("read vref: {} (should be {})", vref, vrefint.value()); - let temp = adc.read(&mut temperature); + let temp = adc.read(&mut temperature).await; info!("read temperature: {}", temp); - let pin = adc.read(&mut p.PA0); + let pin = adc.read(&mut p.PA0).await; info!("read pin: {}", pin); - let pin_mv = pin as u32 * VREF_INT as u32 / vref as u32; + let pin_mv = (pin as u32 * vrefint.value() as u32 / vref as u32) * 3300 / 4095; info!("computed pin mv: {}", pin_mv); - Timer::after(Duration::from_secs(1)).await; + Timer::after(Duration::from_millis(500)).await; } }