Skip to content

Commit

Permalink
Implement ADC DMA source
Browse files Browse the repository at this point in the history
This commit makes the ADCs a possible source for DMA operations.
  • Loading branch information
lkolbly committed Mar 30, 2021
1 parent 8f457ed commit be6006e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
58 changes: 58 additions & 0 deletions imxrt-hal/src/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,64 @@ where
}
}

/// Internal trait for determining what DMA request signal to use for
/// ADC1 or ADC2.
#[doc(hidden)]
pub trait AdcDmaSource {
/// See table 4-3 of the iMXRT1060 Reference Manual (Rev 2)
const SOURCE_REQUEST_SIGNAL: u32;
}

impl AdcDmaSource for ADC1 {
const SOURCE_REQUEST_SIGNAL: u32 = 24;
}

impl AdcDmaSource for ADC2 {
const SOURCE_REQUEST_SIGNAL: u32 = 88;
}

/// Streaming DMA source for ADCs
pub struct AdcSource<ADCx, P> {
adc: ADC<ADCx>,
pin: AnalogInput<ADCx, P>,
}

impl<ADCx, P> AdcSource<ADCx, P>
where
ADCx: adc::ADC + AdcDmaSource,
P: Pin<ADCx>,
{
/// Create a DMA Source object tieing the ADC to the given pin.
pub fn new(adc: ADC<ADCx>, pin: AnalogInput<ADCx, P>) -> Self {
AdcSource { adc, pin }
}
}

impl<ADCx, P> crate::dma::peripheral::Source<u16> for AdcSource<ADCx, P>
where
ADCx: adc::ADC + AdcDmaSource,
P: Pin<ADCx>,
{
type Error = ();

const SOURCE_REQUEST_SIGNAL: u32 = ADCx::SOURCE_REQUEST_SIGNAL;

fn source(&self) -> *const u16 {
&self.adc.reg.R0 as *const _ as *const u16
}

fn enable_source(&mut self) -> Result<(), Self::Error> {
let channel = <P as Pin<ADCx>>::Input::U32;
ral::modify_reg!(ral::adc, self.adc.reg, GC, ADCO: 1, DMAEN: 1);
ral::modify_reg!(ral::adc, self.adc.reg, HC0, |_| channel);
Ok(())
}

fn disable_source(&mut self) {
ral::modify_reg!(ral::adc, self.adc.reg, GC, ADCO: 0, DMAEN: 0);
}
}

/// Unclocked ADC modules
///
/// The `Unclocked` struct represents both unconfigured ADC peripherals.
Expand Down
3 changes: 3 additions & 0 deletions imxrt-hal/src/dma/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ mod private {
impl<M> Sealed for uart::Rx<M> where M: crate::iomuxc::consts::Unsigned {}
impl<M> Sealed for uart::Tx<M> where M: crate::iomuxc::consts::Unsigned {}

use crate::adc;
impl<ADCx, P> Sealed for adc::AdcSource<ADCx, P> {}

use crate::spi;
impl<M> Sealed for spi::SPI<M> where M: crate::iomuxc::consts::Unsigned {}
}
Expand Down

0 comments on commit be6006e

Please sign in to comment.