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 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 36 additions & 4 deletions embassy-stm32/src/adc/v2.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use core::future::poll_fn;
use core::task::Poll;

use embassy_hal_internal::into_ref;

use super::blocking_delay_us;
Expand Down Expand Up @@ -156,8 +159,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 +169,28 @@ where
T::regs().cr2().modify(|reg| {
reg.set_swstart(true);
});
}

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

// wait for actual start and then finish
poll_fn(|_| {
Christopher-06 marked this conversation as resolved.
Show resolved Hide resolved
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 +202,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 +213,16 @@ where

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

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

self.convert()
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
Loading