Struct Adc

Source
pub struct Adc<ADC, State = Enabled> { /* private fields */ }
Expand description

Analog Digital Converter Peripheral

Implementations§

Source§

impl<ADC> Adc<ADC, Enabled>
where ADC: Instance,

Source

pub fn new( adc: ADC, config: impl Into<Config>, clocks: &Clocks, common_adc: &CommonAdc<<ADC as Instance>::SharedInstance>, ) -> Adc<ADC, Enabled>

Initialize a new ADC peripheral.

Enables the clock, performs a calibration and enables the ADC.

Source

pub fn data_register(&self) -> u16

Returns the current sample stored in the ADC data register

At the end of each regular conversion channel (when Event::EndOfConversion event occurs), the result of the converted data is stored into the data register.

§Note

This function does no error handling or configuration of the peripheral. The value read might be invalid, because the device is not configured accordingly..

§Embedded HAL

There is a more managed way to read from the adc via the embedded_hal::adc::OneShot trait.

Source

pub fn data_register_address(&self) -> u32

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

Source

pub fn start_conversion(&mut self)

Manually start a conversion sequence.

Source

pub fn into_oneshot(self) -> Adc<ADC, OneShot>

Configure and convert the Adc instance into OneShot mode.

Source

pub fn into_disabled(self) -> Adc<ADC, Disabled>

Disables the peripheral and convert it into Disabled mode.

Source

pub fn free(self) -> ADC

Releases the ADC peripheral

Source§

impl<ADC> Adc<ADC, Disabled>
where ADC: Instance,

Source

pub fn new_disabled(adc: ADC) -> Self

Create an Adc instance in Disabled mode.

In this mode, the peripheral is not yet enabled, and neither guaranteed to be calibrated. To convert the Adc peripheral into Enabled mode, you can do the following:

let clocks = rcc.cfgr.freeze(/*... */);
let adc_common = CommonAdc::new(/*...*/);

let mut adc = adc::Adc::new_disabled(dp.ADC1);
adc.calibrate(&clocks, &adc_common); // Optional
adc.set_config(adc::config::Config::default()); // Optional
let adc: Adc<ADC1, Enabled> = adc2.into_enabled();
Source

pub fn into_enabled(self) -> Adc<ADC, Enabled>

Convert the Adc into Enabled mode.

Source

pub fn calibrate( &mut self, clocks: &Clocks, common_adc: &CommonAdc<<ADC as Instance>::SharedInstance>, )

Calibrate according to RM0316 15.3.8

The internal analog calibration is lost each time the power of the ADC is removed.

§Note

The ADC is calibrated, when created via Adc::new, so immidiate calibration after that should not be needed.

Source

pub fn free(self) -> ADC

Releases the ADC peripheral

Source§

impl<ADC, State> Adc<ADC, State>
where ADC: Instance,

Source

pub fn overrun_mode(&self) -> OverrunMode

Get the current overrun mode.

Source

pub fn resolution(&self) -> Resolution

Get the sampling resolution.

Source

pub fn data_alignment(&self) -> DataAlignment

Get the current configured data alignment.

Source

pub fn external_trigger(&self) -> Option<ExternalTrigger>

Get the current configured external trigger.

If no (valid) trigger is set, return None

Source

pub fn conversion_mode(&self) -> ConversionMode

Return the conversion mode the peripheral is currently operating in.

Source

pub fn dma_mode(&self) -> DmaMode

Get the current configured DMA mode.

Source

pub fn config(&self) -> Config

Get the current config::Config the peripheral is configured to.

Source

pub fn is_conversion_ongoing(&self) -> bool

Check if a conversion is ongoing.

Source

pub fn channel_sequence(&self, sequence: Sequence) -> Option<Id>

Get the channel::Id at a specific config::Sequence position.

Return None when no channel was set at the sequence position.

§Example

Getting the pin in a specific sequence position

let pin_id: u8 = adc.channel_sequence(adc::config::Sequence::Seven).unwrap().into();
Source

pub fn sample_time<Pin>(&self, _pin: &Pin) -> SampleTime
where Pin: Channel<ADC, ID = Id>,

Return the current configured sample time for the given pin.

Source

pub fn sequence_length(&self) -> Sequence

Returns the total number of conversions in a regular channel sequence.

Source

pub fn is_interrupt_configured(&self, event: Event) -> bool

Check if an interrupt is configured for the Event

Source

pub fn configured_interrupts(&mut self) -> EnumSet<Event>

Available on crate feature enumset only.

Check which interrupts are enabled for all Events

Source

pub fn is_event_triggered(&self, event: Event) -> bool

Check if an interrupt event happend.

Source

pub fn triggered_events(&self) -> EnumSet<Event>

Available on crate feature enumset only.

Get an EnumSet of all fired interrupt events.

§Examples

This allows disabling all fired event at once, via the enum set abstraction, like so

for event in adc.events() {
    adc.configure_interrupt(event, false);
}
Source

pub unsafe fn peripheral(&mut self) -> &mut ADC

Get access to the underlying register block.

§Safety

This function is not memory unsafe per se, but does not guarantee anything about assumptions of invariants made in this implementation.

Changing specific options can lead to un-expected behavior and nothing is guaranteed.

Source§

impl<ADC, State> Adc<ADC, State>
where ADC: Instance, State: Configurable,

Source

pub fn set_config(&mut self, config: Config)

Apply a whole new config::Config to the peripheral.

Source

pub fn set_overrun_mode(&mut self, mode: OverrunMode)

Set the overrun mode

Source

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

Sets the sampling resolution.

Source

pub fn set_data_alignment(&mut self, align: DataAlignment)

Sets the data register alignment

Source

pub fn set_external_trigger(&mut self, trigger: Option<ExternalTrigger>)

Set an external trigger for a conversion.

This is an alternative to starting a conversion by software (Adc::start_conversion)

Source

pub fn set_conversion_mode(&mut self, conversion_mode: ConversionMode)

Set the conversion mode the ADC peripheral is operating in.

This mode is the heart of how the ADC is operating and what it is used for. For a better explenatnion of the modes, read the documentation of config::ConversionMode.

E.g. for the embedded_hal::adc::OneShot trait this peripheral is operating in config::ConversionMode::Single, while, when using the ADC for DMA or similar, config::ConversionMode::Continuous makes more sense. (DMA itself is configured via Adc::set_dma_mode).

Source

pub fn set_dma_mode(&mut self, dma: DmaMode)

Sets DMA to disabled, single or continuous

Source

pub fn stop_conversion(&mut self)

Signal the peripheral to stop any ongoing conversion

This function will block until the conversion is stopped.

For further details how stop is working, see RM0316 15.3.17

Source

pub fn set_pin_sequence_position<Pin>( &mut self, sequence: Sequence, _pin: &mut Pin, )
where Pin: Channel<ADC, ID = Id>,

Wraps Adc::set_channel_sequence_position to be able to take a pin.

Source

pub unsafe fn set_channel_sequence_position( &mut self, sequence: Sequence, channel: Id, )

Configure the sequence position of a channel.

§Note
§Safety

This function can not guarantee, that one channel is not used by multiple ADCs at similar times. For example channel::Id::Eighteen, which is the VoltageInternalReference.

It can also not guarantee that the corresponding GPIO pin was set into analog mode.

For the safe variant use Adc::set_pin_sequence_position.

Source

pub fn set_sample_time<Pin>(&mut self, _pin: &Pin, sample_time: SampleTime)
where Pin: Channel<ADC, ID = Id>,

Each channel can be sampled with a different sampling time.

§Note

Before starting a conversion, the ADC must establish a direct connection between the voltage source under measurement and the embedded sampling capacitor of the ADC. This sampling time must be enough for the input voltage source to charge the embedded capacitor to the input voltage level.

Source

pub fn set_sequence_length(&mut self, sequence: Sequence)

Define the total number of conversions in the regular channel conversion sequence.

The end of the whole sequence is notfied via the Event::EndOfSequence, while the end of a single conversion of a “slot” is notfied via Event::EndOfConversion.

Source

pub fn enable_interrupt(&mut self, event: Event)

Enable the interrupt for the specified Event.

Source

pub fn disable_interrupt(&mut self, event: Event)

Disable the interrupt for the specified Event.

Source

pub fn configure_interrupt(&mut self, event: Event, enable: impl Into<Switch>)

Enable or disable the interrupt for the specified Event.

Source

pub fn configure_interrupts(&mut self, events: EnumSet<Event>)

Available on crate feature enumset only.

Enable or disable interrupt for the specified Events.

Like Adc::configure_interrupt, but instead using an enumset. The corresponding interrupt for every Event in the set will be enabled, every other interrupt will be disabled.

Source

pub fn clear_event(&mut self, event: Event)

Clear the given interrupt event flag.

Source

pub fn clear_events(&mut self)

Clear all interrupt events.

Source§

impl<ADC> Adc<ADC, OneShot>
where ADC: Instance,

Source

pub fn into_enabled(self) -> Adc<ADC, Enabled>

Converts the Adc in OneShot mode back to Enabled mode, while still staying in the confiugration for OneShot mode.

Source

pub fn free(self) -> ADC

Releases the ADC peripheral

Source§

impl<ADC> Adc<ADC>
where ADC: InterruptNumber,

Source

pub fn interrupt(&self) -> <ADC as InterruptNumber>::Interrupt

Obtain the associated interrupt number for the serial peripheral.

Used to unmask / enable the interrupt with cortex_m::peripheral::NVIC::unmask(). This is useful for all cortex_m::peripheral::INTERRUPT functions.

§Note

This is the easier alternative to obatain the interrupt for:

use cortex_m::peripheral::INTERRUPT;
use stm32f3xx_hal::pac::ADC1;
use stm32f3xx_hal::interrupt::InterruptNumber;

const INTERRUPT: Interrupt = <ADC1 as InterruptNumber>::INTERRUPT;

though this function can not be used in a const context.

Trait Implementations§

Source§

impl<ADC: Debug, State: Debug> Debug for Adc<ADC, State>

Source§

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

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

impl<ADC, Word, Pin> OneShot<ADC, Word, Pin> for Adc<ADC>
where ADC: Instance, Word: From<u16>, Pin: Channel<ADC, ID = Id>,

Source§

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

Synchronously record a single sample of pin.

§Note

This function needs to reconfigure the peripheral to read a single sample, but no such reconfiguration should be visible to the user. If this is the case, consider it a bug!

This function should not trigger any interrupt.

To have a more efficient implementation of this trait, configure the device to OneShot mode via Adc::into_oneshot.

Source§

type Error = ()

Error type returned by ADC methods
Source§

impl<ADC, Word, Pin> OneShot<ADC, Word, Pin> for Adc<ADC, OneShot>
where ADC: Instance, Word: From<u16>, Pin: Channel<ADC, ID = Id>,

Source§

fn read(&mut self, _pin: &mut Pin) -> Result<Word, Self::Error>

Synchronously record a single sample of pin.

Source§

type Error = ()

Error type returned by ADC methods

Auto Trait Implementations§

§

impl<ADC, State> Freeze for Adc<ADC, State>
where ADC: Freeze,

§

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

§

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

§

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

§

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

§

impl<ADC, State> UnwindSafe for Adc<ADC, State>
where ADC: UnwindSafe, State: 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>,

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.