aboutsummaryrefslogtreecommitdiff
path: root/examples/stm32u5
diff options
context:
space:
mode:
authorxoviat <[email protected]>2025-11-12 20:06:00 -0600
committerxoviat <[email protected]>2025-11-12 20:06:00 -0600
commitf0506252e21c96ce3b03e0d1c061a831d7dfe3c3 (patch)
tree839f8dac139321665abc293f907ad13d5721aede /examples/stm32u5
parent07c918023c1b233a2f9f16eb0b654169c0379f79 (diff)
stm32: extract adc4
extract adc4 into common adc system and add anyInstance trait to cover adc4 and not adc4
Diffstat (limited to 'examples/stm32u5')
-rw-r--r--examples/stm32u5/src/bin/adc.rs21
1 files changed, 12 insertions, 9 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
4use defmt::*; 4use defmt::*;
5use embassy_stm32::adc::{self, AdcChannel, AdcConfig, SampleTime, adc4}; 5use embassy_stm32::adc::{self, Adc, AdcChannel, AdcConfig, SampleTime, adc4};
6use {defmt_rtt as _, panic_probe as _}; 6use {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);