embassy-stm32

Crates

git

Versions

stm32g473pe

Flavors

Adc

Struct Adc 

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

Analog to Digital driver.

Implementations§

Source§

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

Source

pub fn new(adc: Peri<'d, T>) -> Self

Create a new ADC driver.

Source

pub fn enable_vrefint(&self) -> VrefInt
where T: VrefConverter,

Enable reading the voltage reference internal channel.

Source

pub fn enable_temperature(&self) -> Temperature

Enable reading the temperature internal channel.

Source

pub fn enable_vbat(&self) -> Vbat
where T: VBatConverter,

Enable reading the vbat internal channel.

Source

pub fn set_differential( &mut self, channel: &mut impl AdcChannel<T>, enable: bool, )

Source

pub fn set_oversampling_shift(&mut self, shift: u8)

Set oversampling shift.

Source

pub fn set_oversampling_ratio(&mut self, ratio: u8)

Set oversampling ratio.

Source

pub fn enable_regular_oversampling_mode( &mut self, mode: Rovsm, trig_mode: Trovs, enable: bool, )

Enable oversampling in regular mode.

Source

pub fn set_resolution(&mut self, resolution: Resolution)

Set the ADC resolution.

Source

pub fn blocking_read( &mut self, channel: &mut impl AdcChannel<T>, sample_time: SampleTime, ) -> u16

Read an ADC pin.

Source

pub async fn read( &mut self, rx_dma: Peri<'_, impl RxDma<T>>, sequence: impl ExactSizeIterator<Item = (&mut AnyAdcChannel<T>, SampleTime)>, readings: &mut [u16], )

Read one or multiple ADC regular channels using DMA.

sequence iterator and readings must have the same length.

Example

use embassy_stm32::adc::{Adc, AdcChannel}

let mut adc = Adc::new(p.ADC1);
let mut adc_pin0 = p.PA0.into();
let mut adc_pin1 = p.PA1.into();
let mut measurements = [0u16; 2];

adc.read(
    p.DMA1_CH2.reborrow(),
    [
        (&mut *adc_pin0, SampleTime::CYCLES160_5),
        (&mut *adc_pin1, SampleTime::CYCLES160_5),
    ]
    .into_iter(),
    &mut measurements,
)
.await;
defmt::info!("measurements: {}", measurements);

Note: This is not very efficient as the ADC needs to be reconfigured for each read. Use into_ring_buffered, into_ring_buffered_and_injected

Source

pub fn configure_dual_mode(&mut self, val: Dual)

Source

pub fn into_ring_buffered<'a>( self, dma: Peri<'a, impl RxDma<T>>, dma_buf: &'a mut [u16], sequence: impl ExactSizeIterator<Item = (AnyAdcChannel<T>, SampleTime)>, mode: RegularConversionMode, ) -> RingBufferedAdc<'a, T>

Configures the ADC to use a DMA ring buffer for continuous data acquisition.

Use the [read] method to retrieve measurements from the DMA ring buffer. The read buffer should be exactly half the size of dma_buf. When using triggered mode, it is recommended to configure dma_buf as a double buffer so that one half can be read while the other half is being filled by the DMA, preventing data loss. The trigger period of the ADC effectively defines the period at which the buffer should be read.

If continous conversion mode is selected, the provided dma_buf must be large enough to prevent DMA buffer overruns. Its length should be a multiple of the number of ADC channels being measured. For example, if 3 channels are measured and you want to store 40 samples per channel, the buffer length should be 3 * 40 = 120.

§Parameters
  • dma: The DMA peripheral used to transfer ADC data into the buffer.
  • dma_buf: The buffer where DMA stores ADC samples.
  • regular_sequence: Sequence of channels and sample times for regular ADC conversions.
  • regular_conversion_mode: Mode for regular conversions (continuous or triggered).
§Returns

A RingBufferedAdc<'a, T> instance configured for continuous DMA-based sampling.

Source

pub fn setup_injected_conversions<'a, const N: usize>( self, sequence: [(AnyAdcChannel<T>, SampleTime); N], trigger: ConversionTrigger, interrupt: bool, ) -> InjectedAdc<T, N>

Configures the ADC for injected conversions.

Injected conversions are separate from the regular conversion sequence and are typically triggered by software or an external event. This method sets up a fixed-length sequence of injected channels with specified sample times, the trigger source, and whether the end-of-sequence interrupt should be enabled.

§Parameters
  • sequence: An array of tuples containing the ADC channels and their sample times. The length N determines the number of injected ranks to configure (maximum 4 for STM32).
  • trigger: The trigger source that starts the injected conversion sequence.
  • interrupt: If true, enables the end-of-sequence (JEOS) interrupt for injected conversions.
§Returns

An InjectedAdc<T, N> instance that represents the configured injected sequence. The returned type encodes the sequence length N in its type, ensuring that reads return exactly N samples.

§Panics

This function will panic if:

  • sequence is empty.
  • sequence length exceeds the maximum number of injected ranks (NR_INJECTED_RANKS).
§Notes
  • Injected conversions can run independently of regular ADC conversions.
  • The order of channels in sequence determines the rank order in the injected sequence.
  • Accessing samples beyond N will result in a panic; use the returned type InjectedAdc<T, N> to enforce bounds at compile time.
Source

pub fn into_ring_buffered_and_injected<'a, const N: usize>( self, dma: Peri<'a, impl RxDma<T>>, dma_buf: &'a mut [u16], regular_sequence: impl ExactSizeIterator<Item = (AnyAdcChannel<T>, SampleTime)>, regular_conversion_mode: RegularConversionMode, injected_sequence: [(AnyAdcChannel<T>, SampleTime); N], injected_trigger: ConversionTrigger, injected_interrupt: bool, ) -> (RingBufferedAdc<'a, T>, InjectedAdc<T, N>)

Configures ADC for both regular conversions with a ring-buffered DMA and injected conversions.

§Parameters
  • dma: The DMA peripheral to use for the ring-buffered ADC transfers.
  • dma_buf: The buffer to store DMA-transferred samples for regular conversions.
  • regular_sequence: The sequence of channels and their sample times for regular conversions.
  • regular_conversion_mode: The mode for regular conversions (e.g., continuous or triggered).
  • injected_sequence: An array of channels and sample times for injected conversions (length N).
  • injected_trigger: The trigger source for injected conversions.
  • injected_interrupt: Whether to enable the end-of-sequence interrupt for injected conversions.

Injected conversions are typically used with interrupts. If ADC1 and ADC2 are used in dual mode, it is recommended to enable interrupts only for the ADC whose sequence takes the longest to complete.

§Returns

A tuple containing:

  1. RingBufferedAdc<'a, T> — the configured ADC for regular conversions using DMA.
  2. InjectedAdc<T, N> — the configured ADC for injected conversions.
§Safety

This function is unsafe because it clones the ADC peripheral handle unchecked. Both the RingBufferedAdc and InjectedAdc take ownership of the handle and drop it independently. Ensure no other code concurrently accesses the same ADC instance in a conflicting way.

Auto Trait Implementations§

§

impl<'d, T> Freeze for Adc<'d, T>
where T: Freeze,

§

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

§

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

§

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

§

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

§

impl<'d, T> !UnwindSafe for Adc<'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.