pub struct Adc<'d, T: Instance> { /* private fields */ }Expand description
Analog to Digital driver.
Implementations§
Source§impl<'d, T: Instance> Adc<'d, T>
impl<'d, T: Instance> Adc<'d, T>
Sourcepub fn enable_vrefint(&self) -> VrefIntwhere
T: VrefConverter,
pub fn enable_vrefint(&self) -> VrefIntwhere
T: VrefConverter,
Enable reading the voltage reference internal channel.
Sourcepub fn enable_temperature(&self) -> Temperaturewhere
T: TemperatureConverter,
pub fn enable_temperature(&self) -> Temperaturewhere
T: TemperatureConverter,
Enable reading the temperature internal channel.
Sourcepub fn enable_vbat(&self) -> Vbatwhere
T: VBatConverter,
pub fn enable_vbat(&self) -> Vbatwhere
T: VBatConverter,
Enable reading the vbat internal channel.
pub fn set_differential( &mut self, channel: &mut impl AdcChannel<T>, enable: bool, )
Sourcepub fn set_oversampling_shift(&mut self, shift: u8)
pub fn set_oversampling_shift(&mut self, shift: u8)
Set oversampling shift.
Sourcepub fn set_oversampling_ratio(&mut self, ratio: u8)
pub fn set_oversampling_ratio(&mut self, ratio: u8)
Set oversampling ratio.
Sourcepub fn enable_regular_oversampling_mode(
&mut self,
mode: Rovsm,
trig_mode: Trovs,
enable: bool,
)
pub fn enable_regular_oversampling_mode( &mut self, mode: Rovsm, trig_mode: Trovs, enable: bool, )
Enable oversampling in regular mode.
Sourcepub fn set_resolution(&mut self, resolution: Resolution)
pub fn set_resolution(&mut self, resolution: Resolution)
Set the ADC resolution.
Sourcepub fn blocking_read(
&mut self,
channel: &mut impl AdcChannel<T>,
sample_time: SampleTime,
) -> u16
pub fn blocking_read( &mut self, channel: &mut impl AdcChannel<T>, sample_time: SampleTime, ) -> u16
Read an ADC pin.
Sourcepub async fn read(
&mut self,
rx_dma: Peri<'_, impl RxDma<T>>,
sequence: impl ExactSizeIterator<Item = (&mut AnyAdcChannel<T>, SampleTime)>,
readings: &mut [u16],
)
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
pub fn configure_dual_mode(&mut self, val: Dual)
Sourcepub 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>
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.
Sourcepub fn setup_injected_conversions<'a, const N: usize>(
self,
sequence: [(AnyAdcChannel<T>, SampleTime); N],
trigger: ConversionTrigger,
interrupt: bool,
) -> InjectedAdc<T, N>
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 lengthNdetermines the number of injected ranks to configure (maximum 4 for STM32).trigger: The trigger source that starts the injected conversion sequence.interrupt: Iftrue, 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:
sequenceis empty.sequencelength 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
sequencedetermines the rank order in the injected sequence. - Accessing samples beyond
Nwill result in a panic; use the returned typeInjectedAdc<T, N>to enforce bounds at compile time.
Sourcepub 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>)
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 (lengthN).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:
RingBufferedAdc<'a, T>— the configured ADC for regular conversions using DMA.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.