-
Notifications
You must be signed in to change notification settings - Fork 1.2k
stm32/adc/v3: Add method to sample ADC without reconfiguring every time #4450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
clubby789
wants to merge
1
commit into
embassy-rs:main
Choose a base branch
from
clubby789:adc-v3-read-without-prepare
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+104
−47
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -266,8 +266,8 @@ impl<'d, T: Instance> Adc<'d, T> { | |
/// channel number and not the pin order in `sequence`. | ||
/// | ||
/// Example | ||
/// ```rust,ignore | ||
/// use embassy_stm32::adc::{Adc, AdcChannel} | ||
/// ```rust,no_run | ||
/// use embassy_stm32::adc::{Adc, AdcChannel, SampleTime} | ||
/// | ||
/// let mut adc = Adc::new(p.ADC1); | ||
/// let mut adc_pin0 = p.PA0.degrade_adc(); | ||
|
@@ -292,11 +292,112 @@ impl<'d, T: Instance> Adc<'d, T> { | |
sequence: impl ExactSizeIterator<Item = (&mut AnyAdcChannel<T>, SampleTime)>, | ||
readings: &mut [u16], | ||
) { | ||
assert!(sequence.len() != 0, "Asynchronous read sequence cannot be empty"); | ||
assert!( | ||
sequence.len() == readings.len(), | ||
"Sequence length must be equal to readings length" | ||
); | ||
self.prepare_read(sequence).await; | ||
|
||
let request = rx_dma.request(); | ||
let transfer = unsafe { | ||
Transfer::new_read( | ||
rx_dma, | ||
request, | ||
T::regs().dr().as_ptr() as *mut u16, | ||
readings, | ||
Default::default(), | ||
) | ||
}; | ||
|
||
// Start conversion | ||
T::regs().cr().modify(|reg| { | ||
reg.set_adstart(true); | ||
}); | ||
|
||
// Wait for conversion sequence to finish. | ||
transfer.await; | ||
|
||
// Ensure conversions are finished. | ||
Self::cancel_conversions(); | ||
|
||
// Reset configuration. | ||
#[cfg(not(any(adc_g0, adc_u0)))] | ||
T::regs().cfgr().modify(|reg| { | ||
reg.set_cont(false); | ||
}); | ||
#[cfg(any(adc_g0, adc_u0))] | ||
T::regs().cfgr1().modify(|reg| { | ||
reg.set_cont(false); | ||
}); | ||
} | ||
|
||
/// Read one or multiple ADC channels using DMA, after preparing for the read with [`Self::prepare_read`]; | ||
/// `sequence` iterator and `readings` must have the same length. | ||
/// | ||
/// Note: The order of values in `readings` is defined by the pin ADC | ||
/// channel number and not the pin order in `sequence`. | ||
/// | ||
/// Example | ||
/// ```rust,no_run | ||
/// use embassy_stm32::adc::{Adc, AdcChannel, SampleTime} | ||
/// | ||
/// let mut adc = Adc::new(p.ADC1); | ||
/// let mut adc_pin0 = p.PA0.degrade_adc(); | ||
/// let mut adc_pin1 = p.PA1.degrade_adc(); | ||
/// let mut measurements = [0u16; 2]; | ||
/// adc.prepare_read( | ||
/// [ | ||
/// (&mut *adc_pin0, SampleTime::CYCLES160_5), | ||
/// (&mut *adc_pin1, SampleTime::CYCLES160_5), | ||
/// ] | ||
/// ).await; | ||
/// for _ in 0..5 { | ||
/// adc.read_without_preparing( | ||
/// p.DMA1_CH2.reborrow(), | ||
/// &mut measurements, | ||
/// ).await; | ||
/// defmt::info!("measurements: {}", measurements); | ||
/// } | ||
/// ``` | ||
pub async fn read_without_preparing(&mut self, rx_dma: Peri<'_, impl RxDma<T>>, readings: &mut [u16]) { | ||
let request = rx_dma.request(); | ||
let transfer = unsafe { | ||
Transfer::new_read( | ||
rx_dma, | ||
request, | ||
T::regs().dr().as_ptr() as *mut u16, | ||
readings, | ||
Default::default(), | ||
) | ||
}; | ||
|
||
// Start conversion | ||
T::regs().cr().modify(|reg| { | ||
reg.set_adstart(true); | ||
}); | ||
|
||
// Wait for conversion sequence to finish. | ||
transfer.await; | ||
|
||
// Ensure conversions are finished. | ||
Self::cancel_conversions(); | ||
} | ||
|
||
fn configure_channel(channel: &mut impl AdcChannel<T>, sample_time: SampleTime) { | ||
// RM0492, RM0481, etc. | ||
// "This option bit must be set to 1 when ADCx_INP0 or ADCx_INN1 channel is selected." | ||
#[cfg(any(adc_h5, adc_h7rs))] | ||
if channel.channel() == 0 { | ||
T::regs().or().modify(|reg| reg.set_op0(true)); | ||
} | ||
|
||
// Configure channel | ||
Self::set_channel_sample_time(channel.channel(), sample_time); | ||
} | ||
|
||
/// Configure the ADC peripheral with the given channels for reading with DMA | ||
pub async fn prepare_read(&mut self, sequence: impl ExactSizeIterator<Item = (&mut AnyAdcChannel<T>, SampleTime)>) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest to rename this method to configure_regular_sequence to make the distinction between regular and injected more clear. |
||
assert!(sequence.len() != 0, "Asynchronous read sequence cannot be empty"); | ||
assert!( | ||
sequence.len() <= 16, | ||
"Asynchronous read sequence cannot be more than 16 in length" | ||
|
@@ -379,50 +480,6 @@ impl<'d, T: Instance> Adc<'d, T> { | |
reg.set_dmacfg(Dmacfg::ONE_SHOT); | ||
reg.set_dmaen(true); | ||
}); | ||
|
||
let request = rx_dma.request(); | ||
let transfer = unsafe { | ||
Transfer::new_read( | ||
rx_dma, | ||
request, | ||
T::regs().dr().as_ptr() as *mut u16, | ||
readings, | ||
Default::default(), | ||
) | ||
}; | ||
|
||
// Start conversion | ||
T::regs().cr().modify(|reg| { | ||
reg.set_adstart(true); | ||
}); | ||
|
||
// Wait for conversion sequence to finish. | ||
transfer.await; | ||
|
||
// Ensure conversions are finished. | ||
Self::cancel_conversions(); | ||
|
||
// Reset configuration. | ||
#[cfg(not(any(adc_g0, adc_u0)))] | ||
T::regs().cfgr().modify(|reg| { | ||
reg.set_cont(false); | ||
}); | ||
#[cfg(any(adc_g0, adc_u0))] | ||
T::regs().cfgr1().modify(|reg| { | ||
reg.set_cont(false); | ||
}); | ||
} | ||
|
||
fn configure_channel(channel: &mut impl AdcChannel<T>, sample_time: SampleTime) { | ||
// RM0492, RM0481, etc. | ||
// "This option bit must be set to 1 when ADCx_INP0 or ADCx_INN1 channel is selected." | ||
#[cfg(any(adc_h5, adc_h7rs))] | ||
if channel.channel() == 0 { | ||
T::regs().or().modify(|reg| reg.set_op0(true)); | ||
} | ||
|
||
// Configure channel | ||
Self::set_channel_sample_time(channel.channel(), sample_time); | ||
} | ||
|
||
fn read_channel(&mut self, channel: &mut impl AdcChannel<T>) -> u16 { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest to rename this to something like "trigger_regular_conversion_and_await_result" (maybe a bit long but I think you get the point).
We may also want to think about how we can add support for non-software triggered ADC measurements in the future by setting the EXTEN bit.