pub struct Adc<ADC, State = Enabled> { /* private fields */ }Expand description
Analog Digital Converter Peripheral
Implementations§
Source§impl<ADC> Adc<ADC, Enabled>where
ADC: Instance,
impl<ADC> Adc<ADC, Enabled>where
ADC: Instance,
Sourcepub fn new(
adc: ADC,
config: impl Into<Config>,
clocks: &Clocks,
common_adc: &CommonAdc<<ADC as Instance>::SharedInstance>,
) -> Adc<ADC, Enabled>
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.
Sourcepub fn data_register(&self) -> u16
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.
Sourcepub fn data_register_address(&self) -> u32
pub fn data_register_address(&self) -> u32
Returns the address of the ADC data register. Primarily useful for configuring DMA.
Sourcepub fn start_conversion(&mut self)
pub fn start_conversion(&mut self)
Manually start a conversion sequence.
- To automatically start a conversion consider setting
Adc::set_external_trigger. - The conversion mode is configured via
Adc::set_conversion_mode.
Sourcepub fn into_oneshot(self) -> Adc<ADC, OneShot>
pub fn into_oneshot(self) -> Adc<ADC, OneShot>
Sourcepub fn into_disabled(self) -> Adc<ADC, Disabled>
pub fn into_disabled(self) -> Adc<ADC, Disabled>
Disables the peripheral and convert it into Disabled mode.
Source§impl<ADC> Adc<ADC, Disabled>where
ADC: Instance,
impl<ADC> Adc<ADC, Disabled>where
ADC: Instance,
Sourcepub fn new_disabled(adc: ADC) -> Self
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§impl<ADC, State> Adc<ADC, State>where
ADC: Instance,
impl<ADC, State> Adc<ADC, State>where
ADC: Instance,
Sourcepub fn overrun_mode(&self) -> OverrunMode
pub fn overrun_mode(&self) -> OverrunMode
Get the current overrun mode.
Sourcepub fn resolution(&self) -> Resolution
pub fn resolution(&self) -> Resolution
Get the sampling resolution.
Sourcepub fn data_alignment(&self) -> DataAlignment
pub fn data_alignment(&self) -> DataAlignment
Get the current configured data alignment.
Sourcepub fn external_trigger(&self) -> Option<ExternalTrigger>
pub fn external_trigger(&self) -> Option<ExternalTrigger>
Get the current configured external trigger.
If no (valid) trigger is set, return None
Sourcepub fn conversion_mode(&self) -> ConversionMode
pub fn conversion_mode(&self) -> ConversionMode
Return the conversion mode the peripheral is currently operating in.
Sourcepub fn config(&self) -> Config
pub fn config(&self) -> Config
Get the current config::Config the peripheral is configured to.
Sourcepub fn is_conversion_ongoing(&self) -> bool
pub fn is_conversion_ongoing(&self) -> bool
Check if a conversion is ongoing.
- A conversion can not only be started manually via
Adc::start_conversion, but also by anconfig::ExternalTrigger. - Stopping a conversion might be needed to be able to configure the peripheral, which is not allowed during a conversion.
- Alternativly a conversino stop is signaled via the
Event::EndOfConversionevent.
Sourcepub fn channel_sequence(&self, sequence: Sequence) -> Option<Id>
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();Sourcepub fn sample_time<Pin>(&self, _pin: &Pin) -> SampleTime
pub fn sample_time<Pin>(&self, _pin: &Pin) -> SampleTime
Return the current configured sample time for the given pin.
Sourcepub fn sequence_length(&self) -> Sequence
pub fn sequence_length(&self) -> Sequence
Returns the total number of conversions in a regular channel sequence.
Sourcepub fn is_interrupt_configured(&self, event: Event) -> bool
pub fn is_interrupt_configured(&self, event: Event) -> bool
Check if an interrupt is configured for the Event
Sourcepub fn configured_interrupts(&mut self) -> EnumSet<Event>
Available on crate feature enumset only.
pub fn configured_interrupts(&mut self) -> EnumSet<Event>
enumset only.Check which interrupts are enabled for all Events
Sourcepub fn is_event_triggered(&self, event: Event) -> bool
pub fn is_event_triggered(&self, event: Event) -> bool
Check if an interrupt event happend.
Sourcepub fn triggered_events(&self) -> EnumSet<Event>
Available on crate feature enumset only.
pub fn triggered_events(&self) -> EnumSet<Event>
enumset only.Sourcepub unsafe fn peripheral(&mut self) -> &mut ADC
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,
impl<ADC, State> Adc<ADC, State>where
ADC: Instance,
State: Configurable,
Sourcepub fn set_config(&mut self, config: Config)
pub fn set_config(&mut self, config: Config)
Apply a whole new config::Config to the peripheral.
Sourcepub fn set_overrun_mode(&mut self, mode: OverrunMode)
pub fn set_overrun_mode(&mut self, mode: OverrunMode)
Set the overrun mode
Sourcepub fn set_resolution(&mut self, resolution: Resolution)
pub fn set_resolution(&mut self, resolution: Resolution)
Sets the sampling resolution.
Sourcepub fn set_data_alignment(&mut self, align: DataAlignment)
pub fn set_data_alignment(&mut self, align: DataAlignment)
Sets the data register alignment
Sourcepub fn set_external_trigger(&mut self, trigger: Option<ExternalTrigger>)
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)
Sourcepub fn set_conversion_mode(&mut self, conversion_mode: ConversionMode)
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).
Sourcepub fn set_dma_mode(&mut self, dma: DmaMode)
pub fn set_dma_mode(&mut self, dma: DmaMode)
Sets DMA to disabled, single or continuous
Sourcepub fn stop_conversion(&mut self)
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
Sourcepub fn set_pin_sequence_position<Pin>(
&mut self,
sequence: Sequence,
_pin: &mut Pin,
)
pub fn set_pin_sequence_position<Pin>( &mut self, sequence: Sequence, _pin: &mut Pin, )
Wraps Adc::set_channel_sequence_position to be able to take a pin.
Sourcepub unsafe fn set_channel_sequence_position(
&mut self,
sequence: Sequence,
channel: Id,
)
pub unsafe fn set_channel_sequence_position( &mut self, sequence: Sequence, channel: Id, )
Configure the sequence position of a channel.
§Note
- The
Adc::sequence_lengthwill be increased, ifSequence<Adc::sequence_length. - To configure a channel, any ongoing conversion will be stopped (
Adc::stop_conversion)
§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.
Sourcepub fn set_sample_time<Pin>(&mut self, _pin: &Pin, sample_time: SampleTime)
pub fn set_sample_time<Pin>(&mut self, _pin: &Pin, sample_time: SampleTime)
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.
Sourcepub fn set_sequence_length(&mut self, sequence: Sequence)
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.
Sourcepub fn enable_interrupt(&mut self, event: Event)
pub fn enable_interrupt(&mut self, event: Event)
Enable the interrupt for the specified Event.
Sourcepub fn disable_interrupt(&mut self, event: Event)
pub fn disable_interrupt(&mut self, event: Event)
Disable the interrupt for the specified Event.
Sourcepub fn configure_interrupt(&mut self, event: Event, enable: impl Into<Switch>)
pub fn configure_interrupt(&mut self, event: Event, enable: impl Into<Switch>)
Enable or disable the interrupt for the specified Event.
Sourcepub fn configure_interrupts(&mut self, events: EnumSet<Event>)
Available on crate feature enumset only.
pub fn configure_interrupts(&mut self, events: EnumSet<Event>)
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.
Sourcepub fn clear_event(&mut self, event: Event)
pub fn clear_event(&mut self, event: Event)
Clear the given interrupt event flag.
Sourcepub fn clear_events(&mut self)
pub fn clear_events(&mut self)
Clear all interrupt events.
Source§impl<ADC> Adc<ADC>where
ADC: InterruptNumber,
impl<ADC> Adc<ADC>where
ADC: InterruptNumber,
Sourcepub fn interrupt(&self) -> <ADC as InterruptNumber>::Interrupt
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, Word, Pin> OneShot<ADC, Word, Pin> for Adc<ADC>
impl<ADC, Word, Pin> OneShot<ADC, Word, Pin> for Adc<ADC>
Source§fn read(&mut self, pin: &mut Pin) -> Result<Word, Self::Error>
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.