pub struct Adc<'d, T: Instance> { /* private fields */ }Expand description
Analog to Digital driver.
Implementations§
Source§impl<'d, T> Adc<'d, T>
impl<'d, T> Adc<'d, T>
pub fn new(adc: Peri<'d, T>) -> Self
pub fn new_with_config(adc: Peri<'d, T>, config: AdcConfig) -> Self
Sourcepub fn enable_vrefint(&self) -> VrefInt
pub fn enable_vrefint(&self) -> VrefInt
Enables internal voltage reference and returns VrefInt, which can be used in [Adc::read_internal()] to perform conversion.
Sourcepub fn enable_temperature(&self) -> Temperature
pub fn enable_temperature(&self) -> Temperature
Enables internal temperature sensor and returns Temperature, which can be used in [Adc::read_internal()] to perform conversion.
On STM32F42 and STM32F43 this can not be used together with Vbat. If both are enabled, temperature sensor will return vbat value.
Sourcepub fn enable_vbat(&self) -> Vbat
pub fn enable_vbat(&self) -> Vbat
Enables vbat input and returns Vbat, which can be used in [Adc::read_internal()] to perform conversion.
Sourcepub fn setup_injected_conversions<'a, const N: usize>(
self,
sequence: [(AnyAdcChannel<'a, T>, SampleTime); N],
trigger: impl InjectedTrigger<T>,
edge: Exten,
interrupt: bool,
) -> InjectedAdc<'a, T, N>
pub fn setup_injected_conversions<'a, const N: usize>( self, sequence: [(AnyAdcChannel<'a, T>, SampleTime); N], trigger: impl InjectedTrigger<T>, edge: Exten, interrupt: bool, ) -> InjectedAdc<'a, 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, 'b, const N: usize, D: RxDma<T>>(
self,
dma: Peri<'a, D>,
dma_buf: &'a mut [u16],
_irq: impl Binding<D::Interrupt, InterruptHandler<D>> + 'a,
regular_sequence: impl ExactSizeIterator<Item = (AnyAdcChannel<'b, T>, <T::Regs as BasicAdcRegs>::SampleTime)>,
regular_trigger: impl RegularTrigger<T>,
regular_edge: Exten,
injected_sequence: [(AnyAdcChannel<'b, T>, SampleTime); N],
injected_trigger: impl InjectedTrigger<T>,
injected_edge: Exten,
injected_interrupt: bool,
) -> (RingBufferedAdc<'a, T>, InjectedAdc<'b, T, N>)
pub fn into_ring_buffered_and_injected<'a, 'b, const N: usize, D: RxDma<T>>( self, dma: Peri<'a, D>, dma_buf: &'a mut [u16], _irq: impl Binding<D::Interrupt, InterruptHandler<D>> + 'a, regular_sequence: impl ExactSizeIterator<Item = (AnyAdcChannel<'b, T>, <T::Regs as BasicAdcRegs>::SampleTime)>, regular_trigger: impl RegularTrigger<T>, regular_edge: Exten, injected_sequence: [(AnyAdcChannel<'b, T>, SampleTime); N], injected_trigger: impl InjectedTrigger<T>, injected_edge: Exten, injected_interrupt: bool, ) -> (RingBufferedAdc<'a, T>, InjectedAdc<'b, 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.
Source§impl<'d, T: Instance> Adc<'d, T>
impl<'d, T: Instance> Adc<'d, T>
Sourcepub fn blocking_read(
&mut self,
channel: &mut impl AdcChannel<T>,
sample_time: <T::Regs as BasicAdcRegs>::SampleTime,
) -> u16
pub fn blocking_read( &mut self, channel: &mut impl AdcChannel<T>, sample_time: <T::Regs as BasicAdcRegs>::SampleTime, ) -> u16
Read an ADC pin.
Sourcepub fn into_ring_buffered<'a, 'b, D: RxDma<T>>(
self,
dma: Peri<'a, D>,
dma_buf: &'a mut [u16],
irq: impl Binding<D::Interrupt, InterruptHandler<D>> + 'a,
sequence: impl ExactSizeIterator<Item = (AnyAdcChannel<'b, T>, <T::Regs as BasicAdcRegs>::SampleTime)>,
_trigger: impl RegularTrigger<T>,
edge: Exten,
) -> RingBufferedAdc<'a, T>
pub fn into_ring_buffered<'a, 'b, D: RxDma<T>>( self, dma: Peri<'a, D>, dma_buf: &'a mut [u16], irq: impl Binding<D::Interrupt, InterruptHandler<D>> + 'a, sequence: impl ExactSizeIterator<Item = (AnyAdcChannel<'b, T>, <T::Regs as BasicAdcRegs>::SampleTime)>, _trigger: impl RegularTrigger<T>, edge: Exten, ) -> RingBufferedAdc<'a, T>
Configures the ADC to use a DMA ring buffer for continuous data acquisition.
Use the [Self::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.
Note: Depending on hardware limitations, this method may require channels to be passed in order or require the sequence to have the same sample time for all channnels, depending on the number and properties of the channels in the sequence. This method will panic if the hardware cannot deliver the requested configuration.