pub struct RingBufferedAdc<'d, T: Instance> { /* private fields */ }Implementations§
Source§impl<'d, T: Instance> RingBufferedAdc<'d, T>
 
impl<'d, T: Instance> RingBufferedAdc<'d, T>
Sourcepub fn start(&mut self)
 
pub fn start(&mut self)
Turns on ADC if it is not already turned on and starts continuous DMA transfer.
pub fn stop(&mut self)
pub fn clear(&mut self)
Sourcepub async fn read(
    &mut self,
    measurements: &mut [u16],
) -> Result<usize, OverrunError>
 
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);
        }
    }
}Sourcepub fn blocking_read(&mut self, buf: &mut [u16]) -> Result<usize, OverrunError>
 
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().