aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxoviat <[email protected]>2025-11-15 10:20:36 -0600
committerxoviat <[email protected]>2025-11-15 10:20:36 -0600
commit8e9ec797f255c7addcf43b390c234493a0913f92 (patch)
tree6316db3e3011746ed301c035f42029321432c954
parent435267941c5e585c0de714e3251f3d28426bcdca (diff)
adc: move enable after configure_sequence
-rw-r--r--embassy-stm32/src/adc/g4.rs50
-rw-r--r--embassy-stm32/src/adc/mod.rs38
-rw-r--r--embassy-stm32/src/adc/v3.rs16
3 files changed, 70 insertions, 34 deletions
diff --git a/embassy-stm32/src/adc/g4.rs b/embassy-stm32/src/adc/g4.rs
index bd8ccbf17..4957123a1 100644
--- a/embassy-stm32/src/adc/g4.rs
+++ b/embassy-stm32/src/adc/g4.rs
@@ -1,14 +1,12 @@
1#[cfg(stm32g4)] 1#[cfg(stm32g4)]
2use pac::adc::regs::Difsel as DifselReg; 2use pac::adc::regs::Difsel as DifselReg;
3#[allow(unused)] 3#[allow(unused)]
4#[cfg(stm32g4)]
5pub use pac::adc::vals::{Adcaldif, Adstp, Difsel, Dmacfg, Dmaen, Exten, Rovsm, Trovs};
6#[allow(unused)]
4#[cfg(stm32h7)] 7#[cfg(stm32h7)]
5use pac::adc::vals::{Adcaldif, Difsel, Exten}; 8use pac::adc::vals::{Adcaldif, Difsel, Exten};
6#[allow(unused)] 9pub use pac::adccommon::vals::{Dual, Presc};
7#[cfg(stm32g4)]
8pub use pac::adc::vals::{Adcaldif, Difsel, Exten, Rovsm, Trovs};
9pub use pac::adccommon::vals::Presc;
10pub use stm32_metapac::adc::vals::{Adstp, Dmacfg, Dmaen};
11pub use stm32_metapac::adccommon::vals::Dual;
12 10
13use super::{ 11use super::{
14 Adc, AnyAdcChannel, ConversionMode, Instance, RegularConversionMode, Resolution, RxDma, SampleTime, 12 Adc, AnyAdcChannel, ConversionMode, Instance, RegularConversionMode, Resolution, RxDma, SampleTime,
@@ -176,6 +174,8 @@ impl<T: Instance> super::SealedAnyInstance for T {
176 } 174 }
177 175
178 fn configure_sequence(sequence: impl ExactSizeIterator<Item = ((u8, bool), SampleTime)>) { 176 fn configure_sequence(sequence: impl ExactSizeIterator<Item = ((u8, bool), SampleTime)>) {
177 T::regs().cr().modify(|w| w.set_aden(false));
178
179 // Set sequence length 179 // Set sequence length
180 T::regs().sqr1().modify(|w| { 180 T::regs().sqr1().modify(|w| {
181 w.set_l(sequence.len() as u8 - 1); 181 w.set_l(sequence.len() as u8 - 1);
@@ -183,36 +183,34 @@ impl<T: Instance> super::SealedAnyInstance for T {
183 183
184 #[cfg(stm32g4)] 184 #[cfg(stm32g4)]
185 let mut difsel = DifselReg::default(); 185 let mut difsel = DifselReg::default();
186 let mut smpr = T::regs().smpr().read();
187 let mut smpr2 = T::regs().smpr2().read();
188 let mut sqr1 = T::regs().sqr1().read();
189 let mut sqr2 = T::regs().sqr2().read();
190 let mut sqr3 = T::regs().sqr3().read();
191 let mut sqr4 = T::regs().sqr4().read();
186 192
187 // Configure channels and ranks 193 // Configure channels and ranks
188 for (_i, ((ch, is_differential), sample_time)) in sequence.enumerate() { 194 for (_i, ((ch, is_differential), sample_time)) in sequence.enumerate() {
189 let sample_time = sample_time.into(); 195 let sample_time = sample_time.into();
190 if ch <= 9 { 196 if ch <= 9 {
191 T::regs().smpr().modify(|reg| reg.set_smp(ch as _, sample_time)); 197 smpr.set_smp(ch as _, sample_time);
192 } else { 198 } else {
193 T::regs().smpr2().modify(|reg| reg.set_smp((ch - 10) as _, sample_time)); 199 smpr2.set_smp((ch - 10) as _, sample_time);
194 } 200 }
195 201
196 match _i { 202 match _i {
197 0..=3 => { 203 0..=3 => {
198 T::regs().sqr1().modify(|w| { 204 sqr1.set_sq(_i, ch);
199 w.set_sq(_i, ch);
200 });
201 } 205 }
202 4..=8 => { 206 4..=8 => {
203 T::regs().sqr2().modify(|w| { 207 sqr2.set_sq(_i - 4, ch);
204 w.set_sq(_i - 4, ch);
205 });
206 } 208 }
207 9..=13 => { 209 9..=13 => {
208 T::regs().sqr3().modify(|w| { 210 sqr3.set_sq(_i - 9, ch);
209 w.set_sq(_i - 9, ch);
210 });
211 } 211 }
212 14..=15 => { 212 14..=15 => {
213 T::regs().sqr4().modify(|w| { 213 sqr4.set_sq(_i - 14, ch);
214 w.set_sq(_i - 14, ch);
215 });
216 } 214 }
217 _ => unreachable!(), 215 _ => unreachable!(),
218 } 216 }
@@ -232,12 +230,14 @@ impl<T: Instance> super::SealedAnyInstance for T {
232 } 230 }
233 } 231 }
234 232
233 T::regs().smpr().write_value(smpr);
234 T::regs().smpr2().write_value(smpr2);
235 T::regs().sqr1().write_value(sqr1);
236 T::regs().sqr2().write_value(sqr2);
237 T::regs().sqr3().write_value(sqr3);
238 T::regs().sqr4().write_value(sqr4);
235 #[cfg(stm32g4)] 239 #[cfg(stm32g4)]
236 { 240 T::regs().difsel().write_value(difsel);
237 T::regs().cr().modify(|w| w.set_aden(false));
238 T::regs().difsel().write_value(difsel);
239 T::enable();
240 }
241 } 241 }
242} 242}
243 243
diff --git a/embassy-stm32/src/adc/mod.rs b/embassy-stm32/src/adc/mod.rs
index 13f8a1544..74648cc21 100644
--- a/embassy-stm32/src/adc/mod.rs
+++ b/embassy-stm32/src/adc/mod.rs
@@ -192,10 +192,16 @@ impl<'d, T: AnyInstance> Adc<'d, T> {
192 #[cfg(any(adc_v1, adc_c0, adc_l0, adc_v2, adc_g4, adc_v4, adc_u5, adc_wba))] 192 #[cfg(any(adc_v1, adc_c0, adc_l0, adc_v2, adc_g4, adc_v4, adc_u5, adc_wba))]
193 channel.setup(); 193 channel.setup();
194 194
195 #[cfg(not(adc_v4))] 195 #[cfg(any(adc_v2, adc_v3, adc_g0, adc_h7rs, adc_u0, adc_u5, adc_wba, adc_c0))]
196 T::enable(); 196 T::enable();
197 T::configure_sequence([((channel.channel(), channel.is_differential()), sample_time)].into_iter()); 197 T::configure_sequence([((channel.channel(), channel.is_differential()), sample_time)].into_iter());
198 198
199 // On chips with differential channels, enable after configure_sequence to allow setting differential channels
200 //
201 // TODO: If hardware allows, enable after configure_sequence on all chips
202 #[cfg(any(adc_g4, adc_h5))]
203 T::enable();
204
199 T::convert() 205 T::convert()
200 } 206 }
201 207
@@ -229,10 +235,10 @@ impl<'d, T: AnyInstance> Adc<'d, T> {
229 /// Note: This is not very efficient as the ADC needs to be reconfigured for each read. Use 235 /// Note: This is not very efficient as the ADC needs to be reconfigured for each read. Use
230 /// `into_ring_buffered`, `into_ring_buffered_and_injected` 236 /// `into_ring_buffered`, `into_ring_buffered_and_injected`
231 /// 237 ///
232 /// In STM32C0, channels bigger than 14 cannot be read using sequencer, so you have to use 238 /// Note: Depending on hardware limitations, this method may require channels to be passed
233 /// either blocking reads or use the mechanism to read in HW order (CHSELRMOD=0). 239 /// in order or require the sequence to have the same sample time for all channnels, depending
234 /// 240 /// on the number and properties of the channels in the sequence. This method will panic if
235 /// In addtion, on STM320, this method will panic if the channels are not passed in order 241 /// the hardware cannot deliver the requested configuration.
236 pub async fn read( 242 pub async fn read(
237 &mut self, 243 &mut self,
238 rx_dma: embassy_hal_internal::Peri<'_, impl RxDma<T>>, 244 rx_dma: embassy_hal_internal::Peri<'_, impl RxDma<T>>,
@@ -249,14 +255,20 @@ impl<'d, T: AnyInstance> Adc<'d, T> {
249 "Asynchronous read sequence cannot be more than 16 in length" 255 "Asynchronous read sequence cannot be more than 16 in length"
250 ); 256 );
251 257
252 // Ensure no conversions are ongoing and ADC is enabled. 258 // Ensure no conversions are ongoing
253 T::stop(); 259 T::stop();
260 #[cfg(any(adc_g0, adc_v3, adc_h7rs, adc_u0, adc_v4, adc_u5, adc_wba, adc_c0))]
254 T::enable(); 261 T::enable();
255 262
256 T::configure_sequence( 263 T::configure_sequence(
257 sequence.map(|(channel, sample_time)| ((channel.channel, channel.is_differential), sample_time)), 264 sequence.map(|(channel, sample_time)| ((channel.channel, channel.is_differential), sample_time)),
258 ); 265 );
259 266
267 // On chips with differential channels, enable after configure_sequence to allow setting differential channels
268 //
269 // TODO: If hardware allows, enable after configure_sequence on all chips
270 #[cfg(any(adc_g4, adc_h5))]
271 T::enable();
260 T::configure_dma(ConversionMode::Singular); 272 T::configure_dma(ConversionMode::Singular);
261 273
262 let request = rx_dma.request(); 274 let request = rx_dma.request();
@@ -294,6 +306,11 @@ impl<'d, T: AnyInstance> Adc<'d, T> {
294 /// 306 ///
295 /// # Returns 307 /// # Returns
296 /// A `RingBufferedAdc<'a, T>` instance configured for continuous DMA-based sampling. 308 /// A `RingBufferedAdc<'a, T>` instance configured for continuous DMA-based sampling.
309 ///
310 /// Note: Depending on hardware limitations, this method may require channels to be passed
311 /// in order or require the sequence to have the same sample time for all channnels, depending
312 /// on the number and properties of the channels in the sequence. This method will panic if
313 /// the hardware cannot deliver the requested configuration.
297 pub fn into_ring_buffered<'a>( 314 pub fn into_ring_buffered<'a>(
298 self, 315 self,
299 dma: embassy_hal_internal::Peri<'a, impl RxDma<T>>, 316 dma: embassy_hal_internal::Peri<'a, impl RxDma<T>>,
@@ -307,15 +324,20 @@ impl<'d, T: AnyInstance> Adc<'d, T> {
307 sequence.len() <= 16, 324 sequence.len() <= 16,
308 "Asynchronous read sequence cannot be more than 16 in length" 325 "Asynchronous read sequence cannot be more than 16 in length"
309 ); 326 );
310 // reset conversions and enable the adc 327 // Ensure no conversions are ongoing
311 T::stop(); 328 T::stop();
329 #[cfg(any(adc_g0, adc_v3, adc_h7rs, adc_u0, adc_v4, adc_u5, adc_wba, adc_c0))]
312 T::enable(); 330 T::enable();
313 331
314 //adc side setup
315 T::configure_sequence( 332 T::configure_sequence(
316 sequence.map(|(channel, sample_time)| ((channel.channel, channel.is_differential), sample_time)), 333 sequence.map(|(channel, sample_time)| ((channel.channel, channel.is_differential), sample_time)),
317 ); 334 );
318 335
336 // On chips with differential channels, enable after configure_sequence to allow setting differential channels
337 //
338 // TODO: If hardware allows, enable after configure_sequence on all chips
339 #[cfg(any(adc_g4, adc_h5))]
340 T::enable();
319 T::configure_dma(ConversionMode::Repeated(mode)); 341 T::configure_dma(ConversionMode::Repeated(mode));
320 342
321 core::mem::forget(self); 343 core::mem::forget(self);
diff --git a/embassy-stm32/src/adc/v3.rs b/embassy-stm32/src/adc/v3.rs
index 81eb1e3ee..b270588c4 100644
--- a/embassy-stm32/src/adc/v3.rs
+++ b/embassy-stm32/src/adc/v3.rs
@@ -260,6 +260,9 @@ impl<T: Instance> super::SealedAnyInstance for T {
260 } 260 }
261 261
262 fn configure_sequence(sequence: impl ExactSizeIterator<Item = ((u8, bool), SampleTime)>) { 262 fn configure_sequence(sequence: impl ExactSizeIterator<Item = ((u8, bool), SampleTime)>) {
263 #[cfg(adc_h5)]
264 T::regs().cr().modify(|w| w.set_aden(false));
265
263 // Set sequence length 266 // Set sequence length
264 #[cfg(not(any(adc_g0, adc_u0)))] 267 #[cfg(not(any(adc_g0, adc_u0)))]
265 T::regs().sqr1().modify(|w| { 268 T::regs().sqr1().modify(|w| {
@@ -294,8 +297,11 @@ impl<T: Instance> super::SealedAnyInstance for T {
294 #[cfg(adc_u0)] 297 #[cfg(adc_u0)]
295 let mut channel_mask = 0; 298 let mut channel_mask = 0;
296 299
300 #[cfg(adc_h5)]
301 let mut difsel = 0u32;
302
297 // Configure channels and ranks 303 // Configure channels and ranks
298 for (_i, ((channel, _), sample_time)) in sequence.enumerate() { 304 for (_i, ((channel, _is_differential), sample_time)) in sequence.enumerate() {
299 // RM0492, RM0481, etc. 305 // RM0492, RM0481, etc.
300 // "This option bit must be set to 1 when ADCx_INP0 or ADCx_INN1 channel is selected." 306 // "This option bit must be set to 1 when ADCx_INP0 or ADCx_INN1 channel is selected."
301 #[cfg(any(adc_h5, adc_h7rs))] 307 #[cfg(any(adc_h5, adc_h7rs))]
@@ -357,12 +363,20 @@ impl<T: Instance> super::SealedAnyInstance for T {
357 _ => unreachable!(), 363 _ => unreachable!(),
358 } 364 }
359 365
366 #[cfg(adc_h5)]
367 {
368 difsel |= (_is_differential as u32) << channel;
369 }
370
360 #[cfg(adc_u0)] 371 #[cfg(adc_u0)]
361 { 372 {
362 channel_mask |= 1 << channel; 373 channel_mask |= 1 << channel;
363 } 374 }
364 } 375 }
365 376
377 #[cfg(adc_h5)]
378 T::regs().difsel().write(|w| w.set_difsel(difsel));
379
366 // On G0 and U0 enabled channels are sampled from 0 to last channel. 380 // On G0 and U0 enabled channels are sampled from 0 to last channel.
367 // It is possible to add up to 8 sequences if CHSELRMOD = 1. 381 // It is possible to add up to 8 sequences if CHSELRMOD = 1.
368 // However for supporting more than 8 channels alternative CHSELRMOD = 0 approach is used. 382 // However for supporting more than 8 channels alternative CHSELRMOD = 0 approach is used.