nrf/saadc: do not use dyn

This commit is contained in:
Dario Nieuwenhuis 2021-05-22 15:42:14 +02:00
parent 13524080d3
commit 55c3ba2a5f

View File

@ -32,7 +32,7 @@ pub enum Error {}
/// One-shot saadc. Continuous sample mode TODO.
pub struct OneShot<'d> {
irq: interrupt::SAADC,
phantom: PhantomData<(&'d mut peripherals::SAADC)>,
phantom: PhantomData<&'d mut peripherals::SAADC>,
}
/// Used to configure the SAADC peripheral.
@ -117,34 +117,12 @@ impl<'d> OneShot<'d> {
fn regs(&self) -> &saadc::RegisterBlock {
unsafe { &*SAADC::ptr() }
}
}
impl<'d> Drop for OneShot<'d> {
fn drop(&mut self) {
let r = self.regs();
r.enable.write(|w| w.enable().disabled());
}
}
pub trait Sample {
type SampleFuture<'a>: Future<Output = i16> + 'a
where
Self: 'a;
fn sample<'a>(&'a mut self, pin: &mut dyn PositivePin) -> Self::SampleFuture<'a>;
}
impl<'d> Sample for OneShot<'d> {
#[rustfmt::skip]
type SampleFuture<'a> where Self: 'a = impl Future<Output = i16> + 'a;
fn sample<'a>(&'a mut self, pin: &mut dyn PositivePin) -> Self::SampleFuture<'a> {
let channel = pin.channel();
async move {
async fn sample_inner(&mut self, pin: PositiveChannel) -> i16 {
let r = self.regs();
// Set positive channel
r.ch[0].pselp.write(|w| w.pselp().variant(channel));
r.ch[0].pselp.write(|w| w.pselp().variant(pin));
// Set up the DMA
let mut val: i16 = 0;
@ -182,6 +160,29 @@ impl<'d> Sample for OneShot<'d> {
// The DMA wrote the sampled value to `val`.
val
}
}
impl<'d> Drop for OneShot<'d> {
fn drop(&mut self) {
let r = self.regs();
r.enable.write(|w| w.enable().disabled());
}
}
pub trait Sample {
type SampleFuture<'a>: Future<Output = i16> + 'a
where
Self: 'a;
fn sample<'a, T: PositivePin>(&'a mut self, pin: &mut T) -> Self::SampleFuture<'a>;
}
impl<'d> Sample for OneShot<'d> {
#[rustfmt::skip]
type SampleFuture<'a> where Self: 'a = impl Future<Output = i16> + 'a;
fn sample<'a, T: PositivePin>(&'a mut self, pin: &mut T) -> Self::SampleFuture<'a> {
self.sample_inner(pin.channel())
}
}