mirror of
https://github.com/embassy-rs/embassy.git
synced 2025-10-02 14:44:32 +00:00
Merge pull request #3566 from IvanLi-CN/feat/adc-read-async-stm32g4
STM32: Implement Asynchronous ADC Reading for G4
This commit is contained in:
commit
bc5e0d60b3
@ -5,8 +5,11 @@ use pac::adc::vals::{Adcaldif, Difsel, Exten};
|
||||
#[cfg(stm32g4)]
|
||||
use pac::adc::vals::{Adcaldif, Difsel, Exten, Rovsm, Trovs};
|
||||
use pac::adccommon::vals::Presc;
|
||||
use stm32_metapac::adc::vals::{Adstp, Dmacfg, Dmaen};
|
||||
|
||||
use super::{blocking_delay_us, Adc, AdcChannel, Instance, Resolution, SampleTime};
|
||||
use super::{blocking_delay_us, Adc, AdcChannel, AnyAdcChannel, Instance, Resolution, RxDma, SampleTime};
|
||||
use crate::adc::SealedAdcChannel;
|
||||
use crate::dma::Transfer;
|
||||
use crate::time::Hertz;
|
||||
use crate::{pac, rcc, Peripheral};
|
||||
|
||||
@ -191,10 +194,24 @@ impl<'d, T: Instance> Adc<'d, T> {
|
||||
}
|
||||
|
||||
fn enable(&mut self) {
|
||||
T::regs().isr().write(|w| w.set_adrdy(true));
|
||||
T::regs().cr().modify(|w| w.set_aden(true));
|
||||
while !T::regs().isr().read().adrdy() {}
|
||||
T::regs().isr().write(|w| w.set_adrdy(true));
|
||||
// Make sure bits are off
|
||||
while T::regs().cr().read().addis() {
|
||||
// spin
|
||||
}
|
||||
|
||||
if !T::regs().cr().read().aden() {
|
||||
// Enable ADC
|
||||
T::regs().isr().modify(|reg| {
|
||||
reg.set_adrdy(true);
|
||||
});
|
||||
T::regs().cr().modify(|reg| {
|
||||
reg.set_aden(true);
|
||||
});
|
||||
|
||||
while !T::regs().isr().read().adrdy() {
|
||||
// spin
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn configure(&mut self) {
|
||||
@ -327,23 +344,146 @@ impl<'d, T: Instance> Adc<'d, T> {
|
||||
pub fn blocking_read(&mut self, channel: &mut impl AdcChannel<T>) -> u16 {
|
||||
channel.setup();
|
||||
|
||||
self.read_channel(channel.channel())
|
||||
self.read_channel(channel)
|
||||
}
|
||||
|
||||
fn read_channel(&mut self, channel: u8) -> u16 {
|
||||
// Configure channel
|
||||
Self::set_channel_sample_time(channel, self.sample_time);
|
||||
/// Read one or multiple ADC channels using DMA.
|
||||
///
|
||||
/// `sequence` iterator and `readings` must have the same length.
|
||||
///
|
||||
/// Example
|
||||
/// ```rust,ignore
|
||||
/// use embassy_stm32::adc::{Adc, AdcChannel}
|
||||
///
|
||||
/// let mut adc = Adc::new(p.ADC1);
|
||||
/// let mut adc_pin0 = p.PA0.degrade_adc();
|
||||
/// let mut adc_pin1 = p.PA1.degrade_adc();
|
||||
/// let mut measurements = [0u16; 2];
|
||||
///
|
||||
/// adc.read_async(
|
||||
/// p.DMA1_CH2,
|
||||
/// [
|
||||
/// (&mut *adc_pin0, SampleTime::CYCLES160_5),
|
||||
/// (&mut *adc_pin1, SampleTime::CYCLES160_5),
|
||||
/// ]
|
||||
/// .into_iter(),
|
||||
/// &mut measurements,
|
||||
/// )
|
||||
/// .await;
|
||||
/// defmt::info!("measurements: {}", measurements);
|
||||
/// ```
|
||||
pub async fn read(
|
||||
&mut self,
|
||||
rx_dma: &mut impl RxDma<T>,
|
||||
sequence: impl ExactSizeIterator<Item = (&mut AnyAdcChannel<T>, SampleTime)>,
|
||||
readings: &mut [u16],
|
||||
) {
|
||||
assert!(sequence.len() != 0, "Asynchronous read sequence cannot be empty");
|
||||
assert!(
|
||||
sequence.len() == readings.len(),
|
||||
"Sequence length must be equal to readings length"
|
||||
);
|
||||
assert!(
|
||||
sequence.len() <= 16,
|
||||
"Asynchronous read sequence cannot be more than 16 in length"
|
||||
);
|
||||
|
||||
// Ensure no conversions are ongoing and ADC is enabled.
|
||||
Self::cancel_conversions();
|
||||
self.enable();
|
||||
|
||||
// Set sequence length
|
||||
T::regs().sqr1().modify(|w| {
|
||||
w.set_l(sequence.len() as u8 - 1);
|
||||
});
|
||||
|
||||
// Configure channels and ranks
|
||||
for (_i, (channel, sample_time)) in sequence.enumerate() {
|
||||
Self::configure_channel(channel, sample_time);
|
||||
|
||||
match _i {
|
||||
0..=3 => {
|
||||
T::regs().sqr1().modify(|w| {
|
||||
w.set_sq(_i, channel.channel());
|
||||
});
|
||||
}
|
||||
4..=8 => {
|
||||
T::regs().sqr2().modify(|w| {
|
||||
w.set_sq(_i - 4, channel.channel());
|
||||
});
|
||||
}
|
||||
9..=13 => {
|
||||
T::regs().sqr3().modify(|w| {
|
||||
w.set_sq(_i - 9, channel.channel());
|
||||
});
|
||||
}
|
||||
14..=15 => {
|
||||
T::regs().sqr4().modify(|w| {
|
||||
w.set_sq(_i - 14, channel.channel());
|
||||
});
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
// Set continuous mode with oneshot dma.
|
||||
// Clear overrun flag before starting transfer.
|
||||
T::regs().isr().modify(|reg| {
|
||||
reg.set_ovr(true);
|
||||
});
|
||||
|
||||
T::regs().cfgr().modify(|reg| {
|
||||
reg.set_discen(false);
|
||||
reg.set_cont(true);
|
||||
reg.set_dmacfg(Dmacfg::ONESHOT);
|
||||
reg.set_dmaen(Dmaen::ENABLE);
|
||||
});
|
||||
|
||||
let request = rx_dma.request();
|
||||
let transfer = unsafe {
|
||||
Transfer::new_read(
|
||||
rx_dma,
|
||||
request,
|
||||
T::regs().dr().as_ptr() as *mut u16,
|
||||
readings,
|
||||
Default::default(),
|
||||
)
|
||||
};
|
||||
|
||||
// Start conversion
|
||||
T::regs().cr().modify(|reg| {
|
||||
reg.set_adstart(true);
|
||||
});
|
||||
|
||||
// Wait for conversion sequence to finish.
|
||||
transfer.await;
|
||||
|
||||
// Ensure conversions are finished.
|
||||
Self::cancel_conversions();
|
||||
|
||||
// Reset configuration.
|
||||
T::regs().cfgr().modify(|reg| {
|
||||
reg.set_cont(false);
|
||||
});
|
||||
}
|
||||
|
||||
fn configure_channel(channel: &mut impl AdcChannel<T>, sample_time: SampleTime) {
|
||||
// Configure channel
|
||||
Self::set_channel_sample_time(channel.channel(), sample_time);
|
||||
}
|
||||
|
||||
fn read_channel(&mut self, channel: &mut impl AdcChannel<T>) -> u16 {
|
||||
Self::configure_channel(channel, self.sample_time);
|
||||
#[cfg(stm32h7)]
|
||||
{
|
||||
T::regs().cfgr2().modify(|w| w.set_lshift(0));
|
||||
T::regs()
|
||||
.pcsel()
|
||||
.write(|w| w.set_pcsel(channel as _, Pcsel::PRESELECTED));
|
||||
.write(|w| w.set_pcsel(channel.channel() as _, Pcsel::PRESELECTED));
|
||||
}
|
||||
|
||||
T::regs().sqr1().write(|reg| {
|
||||
reg.set_sq(0, channel);
|
||||
reg.set_sq(0, channel.channel());
|
||||
reg.set_l(0);
|
||||
});
|
||||
|
||||
@ -358,4 +498,13 @@ impl<'d, T: Instance> Adc<'d, T> {
|
||||
T::regs().smpr2().modify(|reg| reg.set_smp((ch - 10) as _, sample_time));
|
||||
}
|
||||
}
|
||||
|
||||
fn cancel_conversions() {
|
||||
if T::regs().cr().read().adstart() && !T::regs().cr().read().addis() {
|
||||
T::regs().cr().modify(|reg| {
|
||||
reg.set_adstp(Adstp::STOP);
|
||||
});
|
||||
while T::regs().cr().read().adstart() {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
60
examples/stm32g4/src/bin/adc_dma.rs
Normal file
60
examples/stm32g4/src/bin/adc_dma.rs
Normal file
@ -0,0 +1,60 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use defmt::*;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_stm32::adc::{Adc, AdcChannel as _, SampleTime};
|
||||
use embassy_stm32::Config;
|
||||
use embassy_time::Timer;
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
static mut DMA_BUF: [u16; 2] = [0; 2];
|
||||
|
||||
#[embassy_executor::main]
|
||||
async fn main(_spawner: Spawner) {
|
||||
let mut read_buffer = unsafe { &mut DMA_BUF[..] };
|
||||
|
||||
let mut config = Config::default();
|
||||
{
|
||||
use embassy_stm32::rcc::*;
|
||||
config.rcc.pll = Some(Pll {
|
||||
source: PllSource::HSI,
|
||||
prediv: PllPreDiv::DIV4,
|
||||
mul: PllMul::MUL85,
|
||||
divp: None,
|
||||
divq: None,
|
||||
// Main system clock at 170 MHz
|
||||
divr: Some(PllRDiv::DIV2),
|
||||
});
|
||||
config.rcc.mux.adc12sel = mux::Adcsel::SYS;
|
||||
config.rcc.sys = Sysclk::PLL1_R;
|
||||
}
|
||||
let p = embassy_stm32::init(config);
|
||||
|
||||
info!("Hello World!");
|
||||
|
||||
let mut adc = Adc::new(p.ADC1);
|
||||
|
||||
let mut dma = p.DMA1_CH1;
|
||||
let mut vrefint_channel = adc.enable_vrefint().degrade_adc();
|
||||
let mut pa0 = p.PA0.degrade_adc();
|
||||
|
||||
loop {
|
||||
adc.read(
|
||||
&mut dma,
|
||||
[
|
||||
(&mut vrefint_channel, SampleTime::CYCLES247_5),
|
||||
(&mut pa0, SampleTime::CYCLES247_5),
|
||||
]
|
||||
.into_iter(),
|
||||
&mut read_buffer,
|
||||
)
|
||||
.await;
|
||||
|
||||
let vrefint = read_buffer[0];
|
||||
let measured = read_buffer[1];
|
||||
info!("vrefint: {}", vrefint);
|
||||
info!("measured: {}", measured);
|
||||
Timer::after_millis(500).await;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user