Struct stm32f4xx_hal::adc::Adc

source ·
pub struct Adc<ADC> { /* private fields */ }
Expand description

Analog to Digital Converter

Implementations§

source§

impl Adc<ADC1>

source

pub fn calibrate(&mut self)

Calculates the system VDDA by sampling the internal VREF channel and comparing the result with the value stored at the factory.

source

pub fn enable_vbat(&self)

Enables the vbat internal channel

source

pub fn disable_vbat(&self)

Enables the vbat internal channel

source

pub fn enable_temperature_and_vref(&mut self)

Enables the temp and vref internal channels. They can’t work while vbat is also enabled so this method also disables vbat.

source

pub fn disable_temperature_and_vref(&mut self)

Disables the temp and vref internal channels

source

pub fn temperature_and_vref_enabled(&mut self) -> bool

Returns if the temp and vref internal channels are enabled

source

pub fn adc1(adc: ADC1, reset: bool, config: AdcConfig) -> Adc<ADC1>

Enables the ADC clock, resets the peripheral (optionally), runs calibration and applies the supplied config

Arguments
  • reset - should a reset be performed. This is provided because on some devices multiple ADCs share the same common reset
source

pub fn apply_config(&mut self, config: AdcConfig)

Applies all fields in AdcConfig

source

pub fn is_enabled(&self) -> bool

Returns if the adc is enabled

source

pub fn enable(&mut self)

Enables the adc

source

pub fn disable(&mut self)

Disables the adc

Note

The ADC in the f4 has few restrictions on what can be configured while the ADC is enabled. If any bugs are found where some settings aren’t “sticking” try disabling the ADC before changing them. The reference manual for the chip I’m using only states that the sequence registers are locked when they are being converted.

source

pub fn start_conversion(&mut self)

Starts conversion sequence. Waits for the hardware to indicate it’s actually started.

source

pub fn set_clock(&mut self, clock: Clock)

Sets the clock for the adc

source

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

Sets the sampling resolution

source

pub fn set_align(&mut self, align: Align)

Sets the DR register alignment to left or right

source

pub fn set_scan(&mut self, scan: Scan)

Enables and disables scan mode

source

pub fn set_external_trigger( &mut self, (edge, extsel): (TriggerMode, ExternalTrigger) )

Sets which external trigger to use and if it is disabled, rising, falling or both

source

pub fn set_continuous(&mut self, continuous: Continuous)

Enables and disables continuous mode

source

pub fn set_dma(&mut self, dma: Dma)

Sets DMA to disabled, single or continuous

source

pub fn set_end_of_conversion_interrupt(&mut self, eoc: Eoc)

Sets if the end-of-conversion behaviour. The end-of-conversion interrupt occur either per conversion or for the whole sequence.

source

pub fn clear_end_of_conversion_flag(&mut self)

Resets the end-of-conversion flag

source

pub fn set_default_sample_time(&mut self, sample_time: SampleTime)

Sets the default sample time that is used for one-shot conversions. configure_channel and start_conversion can be
used for configurations where different sampling times are required per channel.

source

pub fn sequence_length(&mut self) -> u8

Returns the current sequence length. Primarily useful for configuring DMA.

source

pub fn reset_sequence(&mut self)

Reset the sequence

source

pub fn data_register_address(&mut self) -> u32

Returns the address of the ADC data register. Primarily useful for configuring DMA.

source

pub fn configure_channel<CHANNEL>( &mut self, _channel: &CHANNEL, sequence: Sequence, sample_time: SampleTime )
where CHANNEL: Channel<ADC1, ID = u8>,

Configure a channel for sampling. It will make sure the sequence is at least as long as the sequence provided.

Arguments
  • channel - channel to configure
  • sequence - where in the sequence to sample the channel. Also called rank in some STM docs/code
  • sample_time - how long to sample for. See datasheet and ref manual to work out how long you need
    to sample for at a given ADC clock frequency
source

pub fn current_sample(&self) -> u16

Returns the current sample stored in the ADC data register

source

pub fn sample_to_millivolts(&self, sample: u16) -> u16

Converts a sample value to millivolts using calibrated VDDA and configured resolution. Due to the ADC characteristics VDDA will never be reached as described in #362 and AN2834-How to get the best ADC accuracy in STM32 microcontrollers in section 3.1.2.

source

pub fn make_sample_to_millivolts(&self) -> impl Fn(u16) -> u16

Make a converter for samples to millivolts

source

pub fn reference_voltage(&self) -> u32

Returns the VDDA in millivolts calculated from the factory calibration and vrefint. Can be used to get calibration data from ADC1 and use it to configure ADCs that don’t support calibration.

source

pub fn wait_for_conversion_sequence(&self)

Block until the conversion is completed

Panics

Will panic if there is no conversion started and the end-of-conversion bit is not set

source

pub fn convert<PIN>(&mut self, pin: &PIN, sample_time: SampleTime) -> u16
where PIN: Channel<ADC1, ID = u8>,

Synchronously convert a single sample Note that it reconfigures the adc sequence and doesn’t restore it

source§

impl Adc<ADC2>

source

pub fn adc2(adc: ADC2, reset: bool, config: AdcConfig) -> Adc<ADC2>

Enables the ADC clock, resets the peripheral (optionally), runs calibration and applies the supplied config

Arguments
  • reset - should a reset be performed. This is provided because on some devices multiple ADCs share the same common reset
source

pub fn apply_config(&mut self, config: AdcConfig)

Applies all fields in AdcConfig

source

pub fn is_enabled(&self) -> bool

Returns if the adc is enabled

source

pub fn enable(&mut self)

Enables the adc

source

pub fn disable(&mut self)

Disables the adc

Note

The ADC in the f4 has few restrictions on what can be configured while the ADC is enabled. If any bugs are found where some settings aren’t “sticking” try disabling the ADC before changing them. The reference manual for the chip I’m using only states that the sequence registers are locked when they are being converted.

source

pub fn start_conversion(&mut self)

Starts conversion sequence. Waits for the hardware to indicate it’s actually started.

source

pub fn set_clock(&mut self, clock: Clock)

Sets the clock for the adc

source

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

Sets the sampling resolution

source

pub fn set_align(&mut self, align: Align)

Sets the DR register alignment to left or right

source

pub fn set_scan(&mut self, scan: Scan)

Enables and disables scan mode

source

pub fn set_external_trigger( &mut self, (edge, extsel): (TriggerMode, ExternalTrigger) )

Sets which external trigger to use and if it is disabled, rising, falling or both

source

pub fn set_continuous(&mut self, continuous: Continuous)

Enables and disables continuous mode

source

pub fn set_dma(&mut self, dma: Dma)

Sets DMA to disabled, single or continuous

source

pub fn set_end_of_conversion_interrupt(&mut self, eoc: Eoc)

Sets if the end-of-conversion behaviour. The end-of-conversion interrupt occur either per conversion or for the whole sequence.

source

pub fn clear_end_of_conversion_flag(&mut self)

Resets the end-of-conversion flag

source

pub fn set_default_sample_time(&mut self, sample_time: SampleTime)

Sets the default sample time that is used for one-shot conversions. configure_channel and start_conversion can be
used for configurations where different sampling times are required per channel.

source

pub fn sequence_length(&mut self) -> u8

Returns the current sequence length. Primarily useful for configuring DMA.

source

pub fn reset_sequence(&mut self)

Reset the sequence

source

pub fn data_register_address(&mut self) -> u32

Returns the address of the ADC data register. Primarily useful for configuring DMA.

source

pub fn configure_channel<CHANNEL>( &mut self, _channel: &CHANNEL, sequence: Sequence, sample_time: SampleTime )
where CHANNEL: Channel<ADC2, ID = u8>,

Configure a channel for sampling. It will make sure the sequence is at least as long as the sequence provided.

Arguments
  • channel - channel to configure
  • sequence - where in the sequence to sample the channel. Also called rank in some STM docs/code
  • sample_time - how long to sample for. See datasheet and ref manual to work out how long you need
    to sample for at a given ADC clock frequency
source

pub fn current_sample(&self) -> u16

Returns the current sample stored in the ADC data register

source

pub fn sample_to_millivolts(&self, sample: u16) -> u16

Converts a sample value to millivolts using calibrated VDDA and configured resolution. Due to the ADC characteristics VDDA will never be reached as described in #362 and AN2834-How to get the best ADC accuracy in STM32 microcontrollers in section 3.1.2.

source

pub fn make_sample_to_millivolts(&self) -> impl Fn(u16) -> u16

Make a converter for samples to millivolts

source

pub fn reference_voltage(&self) -> u32

Returns the VDDA in millivolts calculated from the factory calibration and vrefint. Can be used to get calibration data from ADC1 and use it to configure ADCs that don’t support calibration.

source

pub fn wait_for_conversion_sequence(&self)

Block until the conversion is completed

Panics

Will panic if there is no conversion started and the end-of-conversion bit is not set

source

pub fn convert<PIN>(&mut self, pin: &PIN, sample_time: SampleTime) -> u16
where PIN: Channel<ADC2, ID = u8>,

Synchronously convert a single sample Note that it reconfigures the adc sequence and doesn’t restore it

source§

impl Adc<ADC3>

source

pub fn adc3(adc: ADC3, reset: bool, config: AdcConfig) -> Adc<ADC3>

Enables the ADC clock, resets the peripheral (optionally), runs calibration and applies the supplied config

Arguments
  • reset - should a reset be performed. This is provided because on some devices multiple ADCs share the same common reset
source

pub fn apply_config(&mut self, config: AdcConfig)

Applies all fields in AdcConfig

source

pub fn is_enabled(&self) -> bool

Returns if the adc is enabled

source

pub fn enable(&mut self)

Enables the adc

source

pub fn disable(&mut self)

Disables the adc

Note

The ADC in the f4 has few restrictions on what can be configured while the ADC is enabled. If any bugs are found where some settings aren’t “sticking” try disabling the ADC before changing them. The reference manual for the chip I’m using only states that the sequence registers are locked when they are being converted.

source

pub fn start_conversion(&mut self)

Starts conversion sequence. Waits for the hardware to indicate it’s actually started.

source

pub fn set_clock(&mut self, clock: Clock)

Sets the clock for the adc

source

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

Sets the sampling resolution

source

pub fn set_align(&mut self, align: Align)

Sets the DR register alignment to left or right

source

pub fn set_scan(&mut self, scan: Scan)

Enables and disables scan mode

source

pub fn set_external_trigger( &mut self, (edge, extsel): (TriggerMode, ExternalTrigger) )

Sets which external trigger to use and if it is disabled, rising, falling or both

source

pub fn set_continuous(&mut self, continuous: Continuous)

Enables and disables continuous mode

source

pub fn set_dma(&mut self, dma: Dma)

Sets DMA to disabled, single or continuous

source

pub fn set_end_of_conversion_interrupt(&mut self, eoc: Eoc)

Sets if the end-of-conversion behaviour. The end-of-conversion interrupt occur either per conversion or for the whole sequence.

source

pub fn clear_end_of_conversion_flag(&mut self)

Resets the end-of-conversion flag

source

pub fn set_default_sample_time(&mut self, sample_time: SampleTime)

Sets the default sample time that is used for one-shot conversions. configure_channel and start_conversion can be
used for configurations where different sampling times are required per channel.

source

pub fn sequence_length(&mut self) -> u8

Returns the current sequence length. Primarily useful for configuring DMA.

source

pub fn reset_sequence(&mut self)

Reset the sequence

source

pub fn data_register_address(&mut self) -> u32

Returns the address of the ADC data register. Primarily useful for configuring DMA.

source

pub fn configure_channel<CHANNEL>( &mut self, _channel: &CHANNEL, sequence: Sequence, sample_time: SampleTime )
where CHANNEL: Channel<ADC3, ID = u8>,

Configure a channel for sampling. It will make sure the sequence is at least as long as the sequence provided.

Arguments
  • channel - channel to configure
  • sequence - where in the sequence to sample the channel. Also called rank in some STM docs/code
  • sample_time - how long to sample for. See datasheet and ref manual to work out how long you need
    to sample for at a given ADC clock frequency
source

pub fn current_sample(&self) -> u16

Returns the current sample stored in the ADC data register

source

pub fn sample_to_millivolts(&self, sample: u16) -> u16

Converts a sample value to millivolts using calibrated VDDA and configured resolution. Due to the ADC characteristics VDDA will never be reached as described in #362 and AN2834-How to get the best ADC accuracy in STM32 microcontrollers in section 3.1.2.

source

pub fn make_sample_to_millivolts(&self) -> impl Fn(u16) -> u16

Make a converter for samples to millivolts

source

pub fn reference_voltage(&self) -> u32

Returns the VDDA in millivolts calculated from the factory calibration and vrefint. Can be used to get calibration data from ADC1 and use it to configure ADCs that don’t support calibration.

source

pub fn wait_for_conversion_sequence(&self)

Block until the conversion is completed

Panics

Will panic if there is no conversion started and the end-of-conversion bit is not set

source

pub fn convert<PIN>(&mut self, pin: &PIN, sample_time: SampleTime) -> u16
where PIN: Channel<ADC3, ID = u8>,

Synchronously convert a single sample Note that it reconfigures the adc sequence and doesn’t restore it

Trait Implementations§

source§

impl<ADC: Clone> Clone for Adc<ADC>

source§

fn clone(&self) -> Adc<ADC>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<ADC> Debug for Adc<ADC>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<PIN> OneShot<ADC1, u16, PIN> for Adc<ADC1>
where PIN: Channel<ADC1, ID = u8>,

§

type Error = ()

Error type returned by ADC methods
source§

fn read(&mut self, pin: &mut PIN) -> Result<u16, Self::Error>

Request that the ADC begin a conversion on the specified pin Read more
source§

impl<PIN> OneShot<ADC2, u16, PIN> for Adc<ADC2>
where PIN: Channel<ADC2, ID = u8>,

§

type Error = ()

Error type returned by ADC methods
source§

fn read(&mut self, pin: &mut PIN) -> Result<u16, Self::Error>

Request that the ADC begin a conversion on the specified pin Read more
source§

impl<PIN> OneShot<ADC3, u16, PIN> for Adc<ADC3>
where PIN: Channel<ADC3, ID = u8>,

§

type Error = ()

Error type returned by ADC methods
source§

fn read(&mut self, pin: &mut PIN) -> Result<u16, Self::Error>

Request that the ADC begin a conversion on the specified pin Read more
source§

impl PeriAddress for Adc<ADC1>

source§

fn address(&self) -> u32

Returns the address to be used by the DMA stream.
§

type MemSize = u16

Memory size of the peripheral.
source§

impl PeriAddress for Adc<ADC2>

source§

fn address(&self) -> u32

Returns the address to be used by the DMA stream.
§

type MemSize = u16

Memory size of the peripheral.
source§

impl PeriAddress for Adc<ADC3>

source§

fn address(&self) -> u32

Returns the address to be used by the DMA stream.
§

type MemSize = u16

Memory size of the peripheral.
source§

impl<ADC: Copy> Copy for Adc<ADC>

source§

impl<ADC, STREAM, const CHANNEL: u8> DMASet<STREAM, CHANNEL, PeripheralToMemory> for Adc<ADC>
where ADC: DMASet<STREAM, CHANNEL, PeripheralToMemory>,

source§

impl SafePeripheralRead for Adc<ADC1>

source§

impl SafePeripheralRead for Adc<ADC2>

source§

impl SafePeripheralRead for Adc<ADC3>

Auto Trait Implementations§

§

impl<ADC> RefUnwindSafe for Adc<ADC>
where ADC: RefUnwindSafe,

§

impl<ADC> Send for Adc<ADC>
where ADC: Send,

§

impl<ADC> Sync for Adc<ADC>
where ADC: Sync,

§

impl<ADC> Unpin for Adc<ADC>
where ADC: Unpin,

§

impl<ADC> UnwindSafe for Adc<ADC>
where ADC: UnwindSafe,

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>,

§

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>,

§

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.