diff options
| author | xoviat <[email protected]> | 2025-11-12 20:06:00 -0600 |
|---|---|---|
| committer | xoviat <[email protected]> | 2025-11-12 20:06:00 -0600 |
| commit | f0506252e21c96ce3b03e0d1c061a831d7dfe3c3 (patch) | |
| tree | 839f8dac139321665abc293f907ad13d5721aede /examples | |
| parent | 07c918023c1b233a2f9f16eb0b654169c0379f79 (diff) | |
stm32: extract adc4
extract adc4 into common adc system and add anyInstance trait to cover adc4 and not adc4
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32u5/src/bin/adc.rs | 21 | ||||
| -rw-r--r-- | examples/stm32wba/src/bin/adc.rs | 17 | ||||
| -rw-r--r-- | examples/stm32wba6/src/bin/adc.rs | 17 |
3 files changed, 32 insertions, 23 deletions
diff --git a/examples/stm32u5/src/bin/adc.rs b/examples/stm32u5/src/bin/adc.rs index 6b9a91d6e..ad59c0bea 100644 --- a/examples/stm32u5/src/bin/adc.rs +++ b/examples/stm32u5/src/bin/adc.rs | |||
| @@ -2,7 +2,7 @@ | |||
| 2 | #![no_main] | 2 | #![no_main] |
| 3 | 3 | ||
| 4 | use defmt::*; | 4 | use defmt::*; |
| 5 | use embassy_stm32::adc::{self, AdcChannel, AdcConfig, SampleTime, adc4}; | 5 | use embassy_stm32::adc::{self, Adc, AdcChannel, AdcConfig, SampleTime, adc4}; |
| 6 | use {defmt_rtt as _, panic_probe as _}; | 6 | use {defmt_rtt as _, panic_probe as _}; |
| 7 | 7 | ||
| 8 | #[embassy_executor::main] | 8 | #[embassy_executor::main] |
| @@ -15,7 +15,7 @@ async fn main(_spawner: embassy_executor::Spawner) { | |||
| 15 | let mut config = AdcConfig::default(); | 15 | let mut config = AdcConfig::default(); |
| 16 | config.averaging = Some(adc::Averaging::Samples1024); | 16 | config.averaging = Some(adc::Averaging::Samples1024); |
| 17 | config.resolution = Some(adc::Resolution::BITS14); | 17 | config.resolution = Some(adc::Resolution::BITS14); |
| 18 | let mut adc1 = adc::Adc::new_with_config(p.ADC1, config); | 18 | let mut adc1 = Adc::new_with_config(p.ADC1, config); |
| 19 | let mut adc1_pin1 = p.PA3; // A0 on nucleo u5a5 | 19 | let mut adc1_pin1 = p.PA3; // A0 on nucleo u5a5 |
| 20 | let mut adc1_pin2 = p.PA2; // A1 | 20 | let mut adc1_pin2 = p.PA2; // A1 |
| 21 | let max1 = adc::resolution_to_max_count(adc::Resolution::BITS14); | 21 | let max1 = adc::resolution_to_max_count(adc::Resolution::BITS14); |
| @@ -24,17 +24,17 @@ async fn main(_spawner: embassy_executor::Spawner) { | |||
| 24 | let mut config = AdcConfig::default(); | 24 | let mut config = AdcConfig::default(); |
| 25 | config.averaging = Some(adc::Averaging::Samples1024); | 25 | config.averaging = Some(adc::Averaging::Samples1024); |
| 26 | config.resolution = Some(adc::Resolution::BITS14); | 26 | config.resolution = Some(adc::Resolution::BITS14); |
| 27 | let mut adc2 = adc::Adc::new_with_config(p.ADC2, config); | 27 | let mut adc2 = Adc::new_with_config(p.ADC2, config); |
| 28 | let mut adc2_pin1 = p.PC3; // A2 | 28 | let mut adc2_pin1 = p.PC3; // A2 |
| 29 | let mut adc2_pin2 = p.PB0; // A3 | 29 | let mut adc2_pin2 = p.PB0; // A3 |
| 30 | let max2 = adc::resolution_to_max_count(adc::Resolution::BITS14); | 30 | let max2 = adc::resolution_to_max_count(adc::Resolution::BITS14); |
| 31 | 31 | ||
| 32 | // **** ADC4 init **** | 32 | // **** ADC4 init **** |
| 33 | let mut adc4 = adc4::Adc4::new(p.ADC4); | 33 | let mut adc4 = Adc::new_adc4(p.ADC4); |
| 34 | let mut adc4_pin1 = p.PC1; // A4 | 34 | let mut adc4_pin1 = p.PC1; // A4 |
| 35 | let mut adc4_pin2 = p.PC0; // A5 | 35 | let mut adc4_pin2 = p.PC0; // A5 |
| 36 | adc4.set_resolution(adc4::Resolution::BITS12); | 36 | adc4.set_resolution_adc4(adc4::Resolution::BITS12); |
| 37 | adc4.set_averaging(adc4::Averaging::Samples256); | 37 | adc4.set_averaging_adc4(adc4::Averaging::Samples256); |
| 38 | let max4 = adc4::resolution_to_max_count(adc4::Resolution::BITS12); | 38 | let max4 = adc4::resolution_to_max_count(adc4::Resolution::BITS12); |
| 39 | 39 | ||
| 40 | // **** ADC1 blocking read **** | 40 | // **** ADC1 blocking read **** |
| @@ -95,11 +95,14 @@ async fn main(_spawner: embassy_executor::Spawner) { | |||
| 95 | // The channels must be in ascending order and can't repeat for ADC4 | 95 | // The channels must be in ascending order and can't repeat for ADC4 |
| 96 | adc4.read( | 96 | adc4.read( |
| 97 | p.GPDMA1_CH1.reborrow(), | 97 | p.GPDMA1_CH1.reborrow(), |
| 98 | [&mut degraded42, &mut degraded41].into_iter(), | 98 | [ |
| 99 | (&mut degraded42, adc4::SampleTime::CYCLES1_5), | ||
| 100 | (&mut degraded41, adc4::SampleTime::CYCLES1_5), | ||
| 101 | ] | ||
| 102 | .into_iter(), | ||
| 99 | &mut measurements, | 103 | &mut measurements, |
| 100 | ) | 104 | ) |
| 101 | .await | 105 | .await; |
| 102 | .unwrap(); | ||
| 103 | let volt2: f32 = 3.3 * measurements[0] as f32 / max4 as f32; | 106 | let volt2: f32 = 3.3 * measurements[0] as f32 / max4 as f32; |
| 104 | let volt1: f32 = 3.3 * measurements[1] as f32 / max4 as f32; | 107 | let volt1: f32 = 3.3 * measurements[1] as f32 / max4 as f32; |
| 105 | info!("Async read 4 pin 1 {}", volt1); | 108 | info!("Async read 4 pin 1 {}", volt1); |
diff --git a/examples/stm32wba/src/bin/adc.rs b/examples/stm32wba/src/bin/adc.rs index 177aab3f3..ade3f5d6a 100644 --- a/examples/stm32wba/src/bin/adc.rs +++ b/examples/stm32wba/src/bin/adc.rs | |||
| @@ -2,7 +2,7 @@ | |||
| 2 | #![no_main] | 2 | #![no_main] |
| 3 | 3 | ||
| 4 | use defmt::*; | 4 | use defmt::*; |
| 5 | use embassy_stm32::adc::{AdcChannel, adc4}; | 5 | use embassy_stm32::adc::{Adc, AdcChannel, SampleTime, adc4}; |
| 6 | use {defmt_rtt as _, panic_probe as _}; | 6 | use {defmt_rtt as _, panic_probe as _}; |
| 7 | 7 | ||
| 8 | #[embassy_executor::main] | 8 | #[embassy_executor::main] |
| @@ -12,11 +12,11 @@ async fn main(_spawner: embassy_executor::Spawner) { | |||
| 12 | let mut p = embassy_stm32::init(config); | 12 | let mut p = embassy_stm32::init(config); |
| 13 | 13 | ||
| 14 | // **** ADC4 init **** | 14 | // **** ADC4 init **** |
| 15 | let mut adc4 = adc4::Adc4::new(p.ADC4); | 15 | let mut adc4 = Adc::new_adc4(p.ADC4); |
| 16 | let mut adc4_pin1 = p.PA0; // A4 | 16 | let mut adc4_pin1 = p.PA0; // A4 |
| 17 | let mut adc4_pin2 = p.PA1; // A5 | 17 | let mut adc4_pin2 = p.PA1; // A5 |
| 18 | adc4.set_resolution(adc4::Resolution::BITS12); | 18 | adc4.set_resolution_adc4(adc4::Resolution::BITS12); |
| 19 | adc4.set_averaging(adc4::Averaging::Samples256); | 19 | adc4.set_averaging_adc4(adc4::Averaging::Samples256); |
| 20 | 20 | ||
| 21 | let max4 = adc4::resolution_to_max_count(adc4::Resolution::BITS12); | 21 | let max4 = adc4::resolution_to_max_count(adc4::Resolution::BITS12); |
| 22 | 22 | ||
| @@ -37,11 +37,14 @@ async fn main(_spawner: embassy_executor::Spawner) { | |||
| 37 | // The channels must be in ascending order and can't repeat for ADC4 | 37 | // The channels must be in ascending order and can't repeat for ADC4 |
| 38 | adc4.read( | 38 | adc4.read( |
| 39 | p.GPDMA1_CH1.reborrow(), | 39 | p.GPDMA1_CH1.reborrow(), |
| 40 | [&mut degraded42, &mut degraded41].into_iter(), | 40 | [ |
| 41 | (&mut degraded42, SampleTime::CYCLES12_5), | ||
| 42 | (&mut degraded41, SampleTime::CYCLES12_5), | ||
| 43 | ] | ||
| 44 | .into_iter(), | ||
| 41 | &mut measurements, | 45 | &mut measurements, |
| 42 | ) | 46 | ) |
| 43 | .await | 47 | .await; |
| 44 | .unwrap(); | ||
| 45 | let volt2: f32 = 3.3 * measurements[0] as f32 / max4 as f32; | 48 | let volt2: f32 = 3.3 * measurements[0] as f32 / max4 as f32; |
| 46 | let volt1: f32 = 3.0 * measurements[1] as f32 / max4 as f32; | 49 | let volt1: f32 = 3.0 * measurements[1] as f32 / max4 as f32; |
| 47 | info!("Async read 4 pin 1 {}", volt1); | 50 | info!("Async read 4 pin 1 {}", volt1); |
diff --git a/examples/stm32wba6/src/bin/adc.rs b/examples/stm32wba6/src/bin/adc.rs index 0887e124c..9d1f39419 100644 --- a/examples/stm32wba6/src/bin/adc.rs +++ b/examples/stm32wba6/src/bin/adc.rs | |||
| @@ -2,7 +2,7 @@ | |||
| 2 | #![no_main] | 2 | #![no_main] |
| 3 | 3 | ||
| 4 | use defmt::*; | 4 | use defmt::*; |
| 5 | use embassy_stm32::adc::{AdcChannel, adc4}; | 5 | use embassy_stm32::adc::{Adc, AdcChannel, SampleTime, adc4}; |
| 6 | use {defmt_rtt as _, panic_probe as _}; | 6 | use {defmt_rtt as _, panic_probe as _}; |
| 7 | 7 | ||
| 8 | #[embassy_executor::main] | 8 | #[embassy_executor::main] |
| @@ -12,11 +12,11 @@ async fn main(_spawner: embassy_executor::Spawner) { | |||
| 12 | let mut p = embassy_stm32::init(config); | 12 | let mut p = embassy_stm32::init(config); |
| 13 | 13 | ||
| 14 | // **** ADC4 init **** | 14 | // **** ADC4 init **** |
| 15 | let mut adc4 = adc4::Adc4::new(p.ADC4); | 15 | let mut adc4 = Adc::new_adc4(p.ADC4); |
| 16 | let mut adc4_pin1 = p.PA0; // A4 | 16 | let mut adc4_pin1 = p.PA0; // A4 |
| 17 | let mut adc4_pin2 = p.PA1; // A5 | 17 | let mut adc4_pin2 = p.PA1; // A5 |
| 18 | adc4.set_resolution(adc4::Resolution::BITS12); | 18 | adc4.set_resolution_adc4(adc4::Resolution::BITS12); |
| 19 | adc4.set_averaging(adc4::Averaging::Samples256); | 19 | adc4.set_averaging_adc4(adc4::Averaging::Samples256); |
| 20 | let max4 = adc4::resolution_to_max_count(adc4::Resolution::BITS12); | 20 | let max4 = adc4::resolution_to_max_count(adc4::Resolution::BITS12); |
| 21 | 21 | ||
| 22 | // **** ADC4 blocking read **** | 22 | // **** ADC4 blocking read **** |
| @@ -36,11 +36,14 @@ async fn main(_spawner: embassy_executor::Spawner) { | |||
| 36 | // The channels must be in ascending order and can't repeat for ADC4 | 36 | // The channels must be in ascending order and can't repeat for ADC4 |
| 37 | adc4.read( | 37 | adc4.read( |
| 38 | p.GPDMA1_CH1.reborrow(), | 38 | p.GPDMA1_CH1.reborrow(), |
| 39 | [&mut degraded42, &mut degraded41].into_iter(), | 39 | [ |
| 40 | (&mut degraded42, SampleTime::CYCLES12_5), | ||
| 41 | (&mut degraded41, SampleTime::CYCLES12_5), | ||
| 42 | ] | ||
| 43 | .into_iter(), | ||
| 40 | &mut measurements, | 44 | &mut measurements, |
| 41 | ) | 45 | ) |
| 42 | .await | 46 | .await; |
| 43 | .unwrap(); | ||
| 44 | let volt2: f32 = 3.3 * measurements[0] as f32 / max4 as f32; | 47 | let volt2: f32 = 3.3 * measurements[0] as f32 / max4 as f32; |
| 45 | let volt1: f32 = 3.0 * measurements[1] as f32 / max4 as f32; | 48 | let volt1: f32 = 3.0 * measurements[1] as f32 / max4 as f32; |
| 46 | info!("Async read 4 pin 1 {}", volt1); | 49 | info!("Async read 4 pin 1 {}", volt1); |
