Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stm32f4] [adc] feat: Add async single adc readings #3742

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions embassy-stm32/src/adc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use core::marker::PhantomData;
#[allow(unused)]
#[cfg(not(any(adc_f3_v2)))]
pub use _version::*;
#[cfg(any(adc_f1, adc_f3, adc_v1, adc_l0, adc_f3_v1_1))]
#[cfg(any(adc_f1, adc_v2, adc_f3, adc_v1, adc_l0, adc_f3_v1_1))]
use embassy_sync::waitqueue::AtomicWaker;

#[cfg(adc_u5)]
Expand All @@ -46,12 +46,12 @@ pub struct Adc<'d, T: Instance> {
sample_time: SampleTime,
}

#[cfg(any(adc_f1, adc_f3, adc_v1, adc_l0, adc_f3_v1_1))]
#[cfg(any(adc_f1, adc_v2, adc_f3, adc_v1, adc_l0, adc_f3_v1_1))]
pub struct State {
pub waker: AtomicWaker,
}

#[cfg(any(adc_f1, adc_f3, adc_v1, adc_l0, adc_f3_v1_1))]
#[cfg(any(adc_f1, adc_v2, adc_f3, adc_v1, adc_l0, adc_f3_v1_1))]
impl State {
pub const fn new() -> Self {
Self {
Expand All @@ -66,7 +66,7 @@ trait SealedInstance {
#[cfg(not(any(adc_f1, adc_v1, adc_l0, adc_f3_v2, adc_f3_v1_1, adc_g0)))]
#[allow(unused)]
fn common_regs() -> crate::pac::adccommon::AdcCommon;
#[cfg(any(adc_f1, adc_f3, adc_v1, adc_l0, adc_f3_v1_1))]
#[cfg(any(adc_f1, adc_v2, adc_f3, adc_v1, adc_l0, adc_f3_v1_1))]
fn state() -> &'static State;
}

Expand Down Expand Up @@ -208,7 +208,7 @@ foreach_adc!(
return crate::pac::$common_inst
}

#[cfg(any(adc_f1, adc_f3, adc_v1, adc_l0, adc_f3_v1_1))]
#[cfg(any(adc_f1, adc_v2, adc_f3, adc_v1, adc_l0, adc_f3_v1_1))]
fn state() -> &'static State {
static STATE: State = State::new();
&STATE
Expand Down
64 changes: 59 additions & 5 deletions embassy-stm32/src/adc/v2.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use core::future::poll_fn;
use core::marker::PhantomData;
use core::task::Poll;

use embassy_hal_internal::into_ref;

use super::blocking_delay_us;
use crate::adc::{Adc, AdcChannel, Instance, Resolution, SampleTime};
use crate::peripherals::ADC1;
use crate::time::Hertz;
use crate::{rcc, Peripheral};
use crate::{interrupt, rcc, Peripheral};

mod ringbuffered_v2;
pub use ringbuffered_v2::{RingBufferedAdc, Sequence};
Expand All @@ -14,6 +18,23 @@ pub const VREF_DEFAULT_MV: u32 = 3300;
/// VREF voltage used for factory calibration of VREFINTCAL register.
pub const VREF_CALIB_MV: u32 = 3300;

/// Interrupt handler.
pub struct InterruptHandler<T: Instance> {
_phantom: PhantomData<T>,
}

impl<T: Instance> interrupt::typelevel::Handler<T::Interrupt> for InterruptHandler<T> {
unsafe fn on_interrupt() {
if T::regs().sr().read().eoc() {
T::regs().cr1().modify(|w| w.set_eocie(false));
} else {
return;
}

T::state().waker.wake();
}
}

pub struct VrefInt;
impl AdcChannel<ADC1> for VrefInt {}
impl super::SealedAdcChannel<ADC1> for VrefInt {
Expand Down Expand Up @@ -156,8 +177,7 @@ where
Vbat {}
}

/// Perform a single conversion.
fn convert(&mut self) -> u16 {
fn start_conversion(&mut self) {
// clear end of conversion flag
T::regs().sr().modify(|reg| {
reg.set_eoc(false);
Expand All @@ -167,6 +187,32 @@ where
T::regs().cr2().modify(|reg| {
reg.set_swstart(true);
});
}

/// Perform a single conversion asynchronously.
async fn convert(&mut self) -> u16 {
self.start_conversion();

T::regs().cr1().modify(|w| w.set_eocie(true));

// wait for actual start and finish
poll_fn(|cx| {
T::state().waker.register(cx.waker());

if T::regs().sr().read().strt() && T::regs().sr().read().eoc() {
Poll::Ready(())
} else {
Poll::Pending
}
})
.await;

T::regs().dr().read().0 as u16
}

/// Perform a single conversion.
fn blocking_convert(&mut self) -> u16 {
self.start_conversion();

while T::regs().sr().read().strt() == false {
// spin //wait for actual start
Expand All @@ -178,7 +224,7 @@ where
T::regs().dr().read().0 as u16
}

pub fn blocking_read(&mut self, channel: &mut impl AdcChannel<T>) -> u16 {
fn configure_channel_reading(&mut self, channel: &mut impl AdcChannel<T>) {
channel.setup();

// Configure ADC
Expand All @@ -189,8 +235,16 @@ where

// Configure channel
Self::set_channel_sample_time(channel, self.sample_time);
}

self.convert()
pub async fn read(&mut self, channel: &mut impl AdcChannel<T>) -> u16 {
self.configure_channel_reading(channel);
self.convert().await
}

pub fn blocking_read(&mut self, channel: &mut impl AdcChannel<T>) -> u16 {
self.configure_channel_reading(channel);
self.blocking_convert()
}

fn set_channel_sample_time(ch: u8, sample_time: SampleTime) {
Expand Down
14 changes: 7 additions & 7 deletions examples/stm32f4/src/bin/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async fn main(_spawner: Spawner) {
delay.delay_us(Temperature::start_time_us().max(VrefInt::start_time_us()));

let vrefint_sample = adc.blocking_read(&mut vrefint);
info!("VrefInt: {}", vrefint_sample);

let convert_to_millivolts = |sample| {
// From http://www.st.com/resource/en/datasheet/DM00071990.pdf
Expand All @@ -44,22 +45,21 @@ async fn main(_spawner: Spawner) {
(sample_mv - V25) as f32 / AVG_SLOPE + 25.0
};

info!("VrefInt: {}", vrefint_sample);
const MAX_ADC_SAMPLE: u16 = (1 << 12) - 1;
info!("VCCA: {} mV", convert_to_millivolts(MAX_ADC_SAMPLE));

loop {
// Read pin
let v = adc.blocking_read(&mut pin);
// Read pin asynchronously
let v = adc.read(&mut pin).await;
info!("PC1: {} ({} mV)", v, convert_to_millivolts(v));

// Read internal temperature
let v = adc.blocking_read(&mut temp);
// Read internal temperature asynchronously
let v = adc.read(&mut temp).await;
let celcius = convert_to_celcius(v);
info!("Internal temp: {} ({} C)", v, celcius);

// Read internal voltage reference
let v = adc.blocking_read(&mut vrefint);
// Read internal voltage reference asynchronously
let v = adc.read(&mut vrefint).await;
info!("VrefInt: {}", v);

Timer::after_millis(100).await;
Expand Down