embassy-stm32

Crates

git

Versions

stm32f722ze

Flavors

RingBufferedAdc

Struct RingBufferedAdc 

Source
pub struct RingBufferedAdc<'d, T: Instance> { /* private fields */ }

Implementations§

Source§

impl<'d, T: Instance> RingBufferedAdc<'d, T>

Source

pub fn start(&mut self)

Turns on ADC if it is not already turned on and starts continuous DMA transfer.

Source

pub fn stop(&mut self)

Source

pub fn clear(&mut self)

Source

pub async fn read( &mut self, measurements: &mut [u16], ) -> Result<usize, OverrunError>

Reads measurements from the DMA ring buffer.

This method fills the provided measurements array with ADC readings from the DMA buffer. The length of the measurements array should be exactly half of the DMA buffer length. Because interrupts are only generated if half or full DMA transfer completes.

Each call to read will populate the measurements array in the same order as the channels defined with sequence. There will be many sequences worth of measurements in this array because it only returns if at least half of the DMA buffer is filled. For example if 2 channels are sampled measurements contain: [sq0 sq1 sq0 sq1 sq0 sq1 ..].

Note that the ADC Datarate can be very fast, it is suggested to use DMA mode inside tightly running tasks Otherwise, you’ll see constant Overrun errors occuring, this means that you’re sampling too quickly for the task to handle, and you may need to increase the buffer size. Example:

const DMA_BUF_LEN: usize = 120;
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 adc_dma_buf = [0u16; DMA_BUF_LEN];

let mut ring_buffered_adc: RingBufferedAdc<embassy_stm32::peripherals::ADC1> = adc.into_ring_buffered(
    p.DMA2_CH0,
     adc_dma_buf, [
        (&mut *adc_pin0, SampleTime::CYCLES160_5),
        (&mut *adc_pin1, SampleTime::CYCLES160_5),
    ].into_iter());


let mut measurements = [0u16; DMA_BUF_LEN / 2];
loop {
    match ring_buffered_adc.read(&mut measurements).await {
        Ok(_) => {
            defmt::info!("adc1: {}", measurements);
        }
        Err(e) => {
            defmt::warn!("Error: {:?}", e);
        }
    }
}
Source

pub fn blocking_read(&mut self, buf: &mut [u16]) -> Result<usize, OverrunError>

Read bytes that are readily available in the ring buffer. If no bytes are currently available in the buffer the call waits until the some bytes are available (at least one byte and at most half the buffer size)

Background receive is started if start_continuous_sampling() has not been previously called.

Receive in the background is terminated if an error is returned. It must then manually be started again by calling start_continuous_sampling() or by re-calling blocking_read().

Trait Implementations§

Source§

impl<T: Instance> Drop for RingBufferedAdc<'_, T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'d, T> Freeze for RingBufferedAdc<'d, T>

§

impl<'d, T> RefUnwindSafe for RingBufferedAdc<'d, T>
where T: RefUnwindSafe,

§

impl<'d, T> Send for RingBufferedAdc<'d, T>
where T: Send,

§

impl<'d, T> Sync for RingBufferedAdc<'d, T>
where T: Sync,

§

impl<'d, T> Unpin for RingBufferedAdc<'d, T>
where T: Unpin,

§

impl<'d, T> !UnwindSafe for RingBufferedAdc<'d, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.