aboutsummaryrefslogtreecommitdiff
path: root/embassy-stm32
diff options
context:
space:
mode:
authorklownfish <[email protected]>2024-12-27 02:54:38 +0100
committerklownfish <[email protected]>2024-12-27 02:54:38 +0100
commita5a90156ce2eeb09760075cecf0eea8f4d1a9e73 (patch)
tree97542b93761b86ff8549894b3d2a8de77de6d73a /embassy-stm32
parent0836392219f4c47c470267c7b51b51fa730784b8 (diff)
cleanup
Diffstat (limited to 'embassy-stm32')
-rw-r--r--embassy-stm32/src/adc/u5_adc4.rs74
-rw-r--r--embassy-stm32/src/lib.rs2
2 files changed, 49 insertions, 27 deletions
diff --git a/embassy-stm32/src/adc/u5_adc4.rs b/embassy-stm32/src/adc/u5_adc4.rs
index ddc1b58a2..468d16640 100644
--- a/embassy-stm32/src/adc/u5_adc4.rs
+++ b/embassy-stm32/src/adc/u5_adc4.rs
@@ -221,16 +221,16 @@ impl<'d, T: Instance> Adc4<'d, T> {
221 } 221 }
222 222
223 fn power_up(&mut self) { 223 fn power_up(&mut self) {
224 T::regs().isr().modify(|reg| { 224 T::regs().isr().modify(|w| {
225 reg.set_ldordy(true); 225 w.set_ldordy(true);
226 }); 226 });
227 T::regs().cr().modify(|reg| { 227 T::regs().cr().modify(|w| {
228 reg.set_advregen(true); 228 w.set_advregen(true);
229 }); 229 });
230 while !T::regs().isr().read().ldordy() { }; 230 while !T::regs().isr().read().ldordy() { };
231 231
232 T::regs().isr().modify(|reg| { 232 T::regs().isr().modify(|w| {
233 reg.set_ldordy(true); 233 w.set_ldordy(true);
234 }); 234 });
235 } 235 }
236 236
@@ -253,6 +253,7 @@ impl<'d, T: Instance> Adc4<'d, T> {
253 w.set_cont(false); 253 w.set_cont(false);
254 w.set_discen(false); 254 w.set_discen(false);
255 w.set_exten(Adc4Exten::DISABLED); 255 w.set_exten(Adc4Exten::DISABLED);
256 w.set_chselrmod(false);
256 }); 257 });
257 258
258 // only use one channel at the moment 259 // only use one channel at the moment
@@ -265,8 +266,8 @@ impl<'d, T: Instance> Adc4<'d, T> {
265 266
266 /// Enable reading the voltage reference internal channel. 267 /// Enable reading the voltage reference internal channel.
267 pub fn enable_vrefint(&self) -> VrefInt { 268 pub fn enable_vrefint(&self) -> VrefInt {
268 T::regs().ccr().modify(|reg| { 269 T::regs().ccr().modify(|w| {
269 reg.set_vrefen(true); 270 w.set_vrefen(true);
270 }); 271 });
271 272
272 VrefInt {} 273 VrefInt {}
@@ -274,8 +275,8 @@ impl<'d, T: Instance> Adc4<'d, T> {
274 275
275 /// Enable reading the temperature internal channel. 276 /// Enable reading the temperature internal channel.
276 pub fn enable_temperature(&self) -> Temperature { 277 pub fn enable_temperature(&self) -> Temperature {
277 T::regs().ccr().modify(|reg| { 278 T::regs().ccr().modify(|w| {
278 reg.set_vsensesel(true); 279 w.set_vsensesel(true);
279 }); 280 });
280 281
281 Temperature {} 282 Temperature {}
@@ -283,8 +284,8 @@ impl<'d, T: Instance> Adc4<'d, T> {
283 284
284 /// Enable reading the vbat internal channel. 285 /// Enable reading the vbat internal channel.
285 pub fn enable_vbat(&self) -> Vbat { 286 pub fn enable_vbat(&self) -> Vbat {
286 T::regs().ccr().modify(|reg| { 287 T::regs().ccr().modify(|w| {
287 reg.set_vbaten(true); 288 w.set_vbaten(true);
288 }); 289 });
289 290
290 Vbat {} 291 Vbat {}
@@ -320,7 +321,7 @@ impl<'d, T: Instance> Adc4<'d, T> {
320 321
321 /// Set the ADC resolution. 322 /// Set the ADC resolution.
322 pub fn set_resolution(&mut self, resolution: Resolution) { 323 pub fn set_resolution(&mut self, resolution: Resolution) {
323 T::regs().cfgr1().modify(|reg| reg.set_res(resolution.into())); 324 T::regs().cfgr1().modify(|w| w.set_res(resolution.into()));
324 } 325 }
325 326
326 /// Set hardware averaging. 327 /// Set hardware averaging.
@@ -337,25 +338,24 @@ impl<'d, T: Instance> Adc4<'d, T> {
337 Averaging::Samples256 => (true, Adc4OversamplingRatio::OVERSAMPLE256X, 8), 338 Averaging::Samples256 => (true, Adc4OversamplingRatio::OVERSAMPLE256X, 8),
338 }; 339 };
339 340
340 T::regs().cfgr2().modify(|reg| { 341 T::regs().cfgr2().modify(|w| {
341 reg.set_ovsr(samples); 342 w.set_ovsr(samples);
342 reg.set_ovss(right_shift); 343 w.set_ovss(right_shift);
343 reg.set_ovse(enable) 344 w.set_ovse(enable)
344 }) 345 })
345 } 346 }
346 347
347 /// Read an ADC channel. 348 /// Read an ADC channel.
348 pub fn blocking_read(&mut self, channel: &mut impl AdcChannel<T>) -> u16{ 349 pub fn blocking_read(&mut self, channel: &mut impl AdcChannel<T>) -> u16{
349 channel.setup(); 350 channel.setup();
350 T::regs().cfgr1().modify(|reg| {
351 reg.set_chselrmod(false);
352 });
353 351
352 // Select channel
354 T::regs().chselrmod0().write_value(Adc4Chselrmod0(0_u32)); 353 T::regs().chselrmod0().write_value(Adc4Chselrmod0(0_u32));
355 T::regs().chselrmod0().modify(|w| { 354 T::regs().chselrmod0().modify(|w| {
356 w.set_chsel(channel.channel() as usize, true); 355 w.set_chsel(channel.channel() as usize, true);
357 }); 356 });
358 357
358 // Reset interrupts
359 T::regs().isr().modify(|reg| { 359 T::regs().isr().modify(|reg| {
360 reg.set_eos(true); 360 reg.set_eos(true);
361 reg.set_eoc(true); 361 reg.set_eoc(true);
@@ -373,8 +373,33 @@ impl<'d, T: Instance> Adc4<'d, T> {
373 T::regs().dr().read().0 as u16 373 T::regs().dr().read().0 as u16
374 } 374 }
375 375
376 /// Channels can not be repeated and must be in ascending order! 376 /// Read one or multiple ADC channels using DMA.
377 /// TODO: broken 377 ///
378 /// `sequence` iterator and `readings` must have the same length.
379 /// The channels in `sequence` must be in ascending order.
380 ///
381 /// Example
382 /// ```rust,ignore
383 /// use embassy_stm32::adc::adc4;
384 /// use embassy_stm32::adc::AdcChannel;
385 ///
386 /// let mut adc4 = adc4::Adc4::new(p.ADC4);
387 /// let mut adc4_pin1 = p.PC1;
388 /// let mut adc4_pin2 = p.PC0;
389 /// let mut degraded41 = adc4_pin1.degrade_adc();
390 /// let mut degraded42 = adc4_pin2.degrade_adc();
391 /// let mut measurements = [0u16; 2];
392 /// // not that the channels must be in ascending order
393 /// adc4.read(
394 /// &mut p.GPDMA1_CH1,
395 /// [
396 /// &mut degraded42,
397 /// &mut degraded41,
398 /// ]
399 /// .into_iter(),
400 /// &mut measurements,
401 /// ).await.unwrap();
402 /// ```
378 pub async fn read( 403 pub async fn read(
379 &mut self, 404 &mut self,
380 rx_dma: &mut impl RxDma4<T>, 405 rx_dma: &mut impl RxDma4<T>,
@@ -402,7 +427,7 @@ impl<'d, T: Instance> Adc4<'d, T> {
402 reg.set_chselrmod(false); 427 reg.set_chselrmod(false);
403 }); 428 });
404 429
405 430 // Verify and activate sequence
406 let mut prev_channel: i16 = -1; 431 let mut prev_channel: i16 = -1;
407 T::regs().chselrmod0().write_value(Adc4Chselrmod0(0_u32)); 432 T::regs().chselrmod0().write_value(Adc4Chselrmod0(0_u32));
408 for channel in sequence { 433 for channel in sequence {
@@ -433,11 +458,8 @@ impl<'d, T: Instance> Adc4<'d, T> {
433 reg.set_adstart(true); 458 reg.set_adstart(true);
434 }); 459 });
435 460
436 // Wait for conversion sequence to finish.
437 transfer.await; 461 transfer.await;
438 462
439 blocking_delay_us(10);
440
441 // Ensure conversions are finished. 463 // Ensure conversions are finished.
442 Self::cancel_conversions(); 464 Self::cancel_conversions();
443 465
diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs
index fb10f2a5f..d04199d05 100644
--- a/embassy-stm32/src/lib.rs
+++ b/embassy-stm32/src/lib.rs
@@ -238,7 +238,7 @@ pub struct Config {
238 #[cfg(any(stm32l4, stm32l5, stm32u5))] 238 #[cfg(any(stm32l4, stm32l5, stm32u5))]
239 pub enable_independent_io_supply: bool, 239 pub enable_independent_io_supply: bool,
240 240
241 /// On the U5 series all analog peripherals are powere by a separate supply. 241 /// On the U5 series all analog peripherals are powered by a separate supply.
242 #[cfg(stm32u5)] 242 #[cfg(stm32u5)]
243 pub enable_independent_analog_supply: bool, 243 pub enable_independent_analog_supply: bool,
244 244