mirror of
https://github.com/embassy-rs/embassy.git
synced 2024-11-21 22:32:29 +00:00
rp: add single-channel dma from adc
with uniform treatment of adc inputs it's easy enough to add a new sampling method. dma sampling only supports one channel at the moment, though round-robin sampling would be a simple extension (probably a new trait that's implemented for Channel and &[Channel]). continuous dma as proposed in #1608 also isn't done here, we'd expect that to be a compound dma::Channel that internally splits a buffer in half and dispatches callbacks or something like that.
This commit is contained in:
parent
b166ed6b78
commit
a6b8f3d994
@ -1,5 +1,6 @@
|
||||
use core::future::poll_fn;
|
||||
use core::marker::PhantomData;
|
||||
use core::mem;
|
||||
use core::sync::atomic::{compiler_fence, Ordering};
|
||||
use core::task::Poll;
|
||||
|
||||
@ -11,7 +12,7 @@ use crate::gpio::{self, AnyPin, Pull};
|
||||
use crate::interrupt::typelevel::Binding;
|
||||
use crate::interrupt::InterruptExt;
|
||||
use crate::peripherals::{ADC, ADC_TEMP_SENSOR};
|
||||
use crate::{interrupt, pac, peripherals, Peripheral, RegExt};
|
||||
use crate::{dma, interrupt, pac, peripherals, Peripheral, RegExt};
|
||||
|
||||
static WAKER: AtomicWaker = AtomicWaker::new();
|
||||
|
||||
@ -48,7 +49,7 @@ impl<'p> Channel<'p> {
|
||||
Self(Source::Pin(pin.map_into()))
|
||||
}
|
||||
|
||||
pub fn new_sensor(s: impl Peripheral<P = ADC_TEMP_SENSOR> + 'p) -> Self {
|
||||
pub fn new_temp_sensor(s: impl Peripheral<P = ADC_TEMP_SENSOR> + 'p) -> Self {
|
||||
let r = pac::ADC;
|
||||
r.cs().write_set(|w| w.set_ts_en(true));
|
||||
Self(Source::TempSensor(s.into_ref()))
|
||||
@ -82,6 +83,21 @@ impl<'p> Drop for Source<'p> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Default)]
|
||||
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||
#[repr(transparent)]
|
||||
pub struct Sample(u16);
|
||||
|
||||
impl Sample {
|
||||
pub fn good(&self) -> bool {
|
||||
self.0 < 0x8000
|
||||
}
|
||||
|
||||
pub fn value(&self) -> u16 {
|
||||
self.0 & !0x8000
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
|
||||
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||
pub enum Error {
|
||||
@ -191,6 +207,91 @@ impl<'d> Adc<'d, Async> {
|
||||
false => Ok(r.result().read().result().into()),
|
||||
}
|
||||
}
|
||||
|
||||
async fn read_many_inner<W: dma::Word>(
|
||||
&mut self,
|
||||
ch: &mut Channel<'_>,
|
||||
buf: &mut [W],
|
||||
fcs_err: bool,
|
||||
dma: impl Peripheral<P = impl dma::Channel>,
|
||||
) -> Result<(), Error> {
|
||||
let r = Self::regs();
|
||||
// clear previous errors and set channel
|
||||
r.cs().modify(|w| {
|
||||
w.set_ainsel(ch.channel());
|
||||
w.set_err_sticky(true); // clear previous errors
|
||||
w.set_start_many(false);
|
||||
});
|
||||
// wait for previous conversions and drain fifo. an earlier batch read may have
|
||||
// been cancelled, leaving the adc running.
|
||||
while !r.cs().read().ready() {}
|
||||
while !r.fcs().read().empty() {
|
||||
r.fifo().read();
|
||||
}
|
||||
|
||||
// set up fifo for dma
|
||||
r.fcs().write(|w| {
|
||||
w.set_thresh(1);
|
||||
w.set_dreq_en(true);
|
||||
w.set_shift(mem::size_of::<W>() == 1);
|
||||
w.set_en(true);
|
||||
w.set_err(fcs_err);
|
||||
});
|
||||
|
||||
// reset dma config on drop, regardless of whether it was a future being cancelled
|
||||
// or the method returning normally.
|
||||
struct ResetDmaConfig;
|
||||
impl Drop for ResetDmaConfig {
|
||||
fn drop(&mut self) {
|
||||
pac::ADC.cs().write_clear(|w| w.set_start_many(true));
|
||||
while !pac::ADC.cs().read().ready() {}
|
||||
pac::ADC.fcs().write_clear(|w| {
|
||||
w.set_dreq_en(true);
|
||||
w.set_shift(true);
|
||||
w.set_en(true);
|
||||
});
|
||||
}
|
||||
}
|
||||
let auto_reset = ResetDmaConfig;
|
||||
|
||||
let dma = unsafe { dma::read(dma, r.fifo().as_ptr() as *const W, buf as *mut [W], 36) };
|
||||
// start conversions and wait for dma to finish. we can't report errors early
|
||||
// because there's no interrupt to signal them, and inspecting every element
|
||||
// of the fifo is too costly to do here.
|
||||
r.cs().write_set(|w| w.set_start_many(true));
|
||||
dma.await;
|
||||
mem::drop(auto_reset);
|
||||
// we can't report errors before the conversions have ended since no interrupt
|
||||
// exists to report them early, and since they're exceedingly rare we probably don't
|
||||
// want to anyway.
|
||||
match r.cs().read().err_sticky() {
|
||||
false => Ok(()),
|
||||
true => Err(Error::ConversionFailed),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub async fn read_many<S: AdcSample>(
|
||||
&mut self,
|
||||
ch: &mut Channel<'_>,
|
||||
buf: &mut [S],
|
||||
dma: impl Peripheral<P = impl dma::Channel>,
|
||||
) -> Result<(), Error> {
|
||||
self.read_many_inner(ch, buf, false, dma).await
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub async fn read_many_raw(
|
||||
&mut self,
|
||||
ch: &mut Channel<'_>,
|
||||
buf: &mut [Sample],
|
||||
dma: impl Peripheral<P = impl dma::Channel>,
|
||||
) {
|
||||
// errors are reported in individual samples
|
||||
let _ = self
|
||||
.read_many_inner(ch, unsafe { mem::transmute::<_, &mut [u16]>(buf) }, true, dma)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d> Adc<'d, Blocking> {
|
||||
@ -214,9 +315,19 @@ impl interrupt::typelevel::Handler<interrupt::typelevel::ADC_IRQ_FIFO> for Inter
|
||||
}
|
||||
|
||||
mod sealed {
|
||||
pub trait AdcSample: crate::dma::Word {}
|
||||
|
||||
pub trait AdcChannel {}
|
||||
}
|
||||
|
||||
pub trait AdcSample: sealed::AdcSample {}
|
||||
|
||||
impl sealed::AdcSample for u16 {}
|
||||
impl AdcSample for u16 {}
|
||||
|
||||
impl sealed::AdcSample for u8 {}
|
||||
impl AdcSample for u8 {}
|
||||
|
||||
pub trait AdcChannel: sealed::AdcChannel {}
|
||||
pub trait AdcPin: AdcChannel + gpio::Pin {}
|
||||
|
||||
|
@ -25,7 +25,7 @@ async fn main(_spawner: Spawner) {
|
||||
let mut p26 = Channel::new_pin(p.PIN_26, Pull::None);
|
||||
let mut p27 = Channel::new_pin(p.PIN_27, Pull::None);
|
||||
let mut p28 = Channel::new_pin(p.PIN_28, Pull::None);
|
||||
let mut ts = Channel::new_sensor(p.ADC_TEMP_SENSOR);
|
||||
let mut ts = Channel::new_temp_sensor(p.ADC_TEMP_SENSOR);
|
||||
|
||||
loop {
|
||||
let level = adc.read(&mut p26).await.unwrap();
|
||||
|
@ -6,7 +6,7 @@ mod common;
|
||||
|
||||
use defmt::*;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_rp::adc::{Adc, Channel, Config, InterruptHandler};
|
||||
use embassy_rp::adc::{Adc, Channel, Config, InterruptHandler, Sample};
|
||||
use embassy_rp::bind_interrupts;
|
||||
use embassy_rp::gpio::Pull;
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
@ -71,10 +71,57 @@ async fn main(_spawner: Spawner) {
|
||||
defmt::assert!(low < none);
|
||||
defmt::assert!(none < up);
|
||||
}
|
||||
|
||||
let temp = convert_to_celsius(adc.read(&mut Channel::new_sensor(p.ADC_TEMP_SENSOR)).await.unwrap());
|
||||
{
|
||||
let temp = convert_to_celsius(
|
||||
adc.read(&mut Channel::new_temp_sensor(&mut p.ADC_TEMP_SENSOR))
|
||||
.await
|
||||
.unwrap(),
|
||||
);
|
||||
defmt::assert!(temp > 0.0);
|
||||
defmt::assert!(temp < 60.0);
|
||||
}
|
||||
|
||||
// run a bunch of conversions. we'll only check gp29 and the temp
|
||||
// sensor here for brevity, if those two work the rest will too.
|
||||
{
|
||||
// gp29 is connected to vsys through a 200k/100k divider,
|
||||
// adding pulls should change the value
|
||||
let mut low = [0u16; 16];
|
||||
let mut none = [0u8; 16];
|
||||
let mut up = [Sample::default(); 16];
|
||||
adc.read_many(
|
||||
&mut Channel::new_pin(&mut p.PIN_29, Pull::Down),
|
||||
&mut low,
|
||||
&mut p.DMA_CH0,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
adc.read_many(
|
||||
&mut Channel::new_pin(&mut p.PIN_29, Pull::None),
|
||||
&mut none,
|
||||
&mut p.DMA_CH0,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
adc.read_many_raw(&mut Channel::new_pin(&mut p.PIN_29, Pull::Up), &mut up, &mut p.DMA_CH0)
|
||||
.await;
|
||||
defmt::assert!(low.iter().zip(none.iter()).all(|(l, n)| *l >> 4 < *n as u16));
|
||||
defmt::assert!(up.iter().all(|s| s.good()));
|
||||
defmt::assert!(none.iter().zip(up.iter()).all(|(n, u)| (*n as u16) < u.value()));
|
||||
}
|
||||
{
|
||||
let mut temp = [0u16; 16];
|
||||
adc.read_many(
|
||||
&mut Channel::new_temp_sensor(&mut p.ADC_TEMP_SENSOR),
|
||||
&mut temp,
|
||||
&mut p.DMA_CH0,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let temp = temp.map(convert_to_celsius);
|
||||
defmt::assert!(temp.iter().all(|t| *t > 0.0));
|
||||
defmt::assert!(temp.iter().all(|t| *t < 60.0));
|
||||
}
|
||||
|
||||
info!("Test OK");
|
||||
cortex_m::asm::bkpt();
|
||||
|
Loading…
Reference in New Issue
Block a user