diff options
| author | Dario Nieuwenhuis <[email protected]> | 2024-12-31 11:33:35 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-12-31 11:33:35 +0100 |
| commit | 18773c377a1eca726f928426249c899afddb1877 (patch) | |
| tree | 323e26f58f860cdcc3dd2a1eb35da1067b6bd76b /examples | |
| parent | dc9a83b9b3110e601a268d5135fe9d89a046c9de (diff) | |
| parent | 41c8bf867bc185507e1b9eadbf5645e57004cd4f (diff) | |
Merge pull request #3688 from klownfish/u5_adc
STM32U5: Add ADC drivers
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32u5/src/bin/adc.rs | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/examples/stm32u5/src/bin/adc.rs b/examples/stm32u5/src/bin/adc.rs new file mode 100644 index 000000000..6ba21cc63 --- /dev/null +++ b/examples/stm32u5/src/bin/adc.rs | |||
| @@ -0,0 +1,109 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_stm32::adc; | ||
| 6 | use embassy_stm32::adc::{adc4, AdcChannel}; | ||
| 7 | use {defmt_rtt as _, panic_probe as _}; | ||
| 8 | |||
| 9 | #[embassy_executor::main] | ||
| 10 | async fn main(_spawner: embassy_executor::Spawner) { | ||
| 11 | let config = embassy_stm32::Config::default(); | ||
| 12 | |||
| 13 | let mut p = embassy_stm32::init(config); | ||
| 14 | |||
| 15 | // **** ADC1 init **** | ||
| 16 | let mut adc1 = adc::Adc::new(p.ADC1); | ||
| 17 | let mut adc1_pin1 = p.PA3; // A0 on nucleo u5a5 | ||
| 18 | let mut adc1_pin2 = p.PA2; // A1 | ||
| 19 | adc1.set_resolution(adc::Resolution::BITS14); | ||
| 20 | adc1.set_averaging(adc::Averaging::Samples1024); | ||
| 21 | adc1.set_sample_time(adc::SampleTime::CYCLES160_5); | ||
| 22 | let max1 = adc::resolution_to_max_count(adc::Resolution::BITS14); | ||
| 23 | |||
| 24 | // **** ADC2 init **** | ||
| 25 | let mut adc2 = adc::Adc::new(p.ADC2); | ||
| 26 | let mut adc2_pin1 = p.PC3; // A2 | ||
| 27 | let mut adc2_pin2 = p.PB0; // A3 | ||
| 28 | adc2.set_resolution(adc::Resolution::BITS14); | ||
| 29 | adc2.set_averaging(adc::Averaging::Samples1024); | ||
| 30 | adc2.set_sample_time(adc::SampleTime::CYCLES160_5); | ||
| 31 | let max2 = adc::resolution_to_max_count(adc::Resolution::BITS14); | ||
| 32 | |||
| 33 | // **** ADC4 init **** | ||
| 34 | let mut adc4 = adc4::Adc4::new(p.ADC4); | ||
| 35 | let mut adc4_pin1 = p.PC1; // A4 | ||
| 36 | let mut adc4_pin2 = p.PC0; // A5 | ||
| 37 | adc4.set_resolution(adc4::Resolution::BITS12); | ||
| 38 | adc4.set_averaging(adc4::Averaging::Samples256); | ||
| 39 | adc4.set_sample_time(adc4::SampleTime::CYCLES1_5); | ||
| 40 | let max4 = adc4::resolution_to_max_count(adc4::Resolution::BITS12); | ||
| 41 | |||
| 42 | // **** ADC1 blocking read **** | ||
| 43 | let raw: u16 = adc1.blocking_read(&mut adc1_pin1); | ||
| 44 | let volt: f32 = 3.3 * raw as f32 / max1 as f32; | ||
| 45 | info!("Read adc1 pin 1 {}", volt); | ||
| 46 | |||
| 47 | let raw: u16 = adc1.blocking_read(&mut adc1_pin2); | ||
| 48 | let volt: f32 = 3.3 * raw as f32 / max1 as f32; | ||
| 49 | info!("Read adc1 pin 2 {}", volt); | ||
| 50 | |||
| 51 | // **** ADC2 blocking read **** | ||
| 52 | let raw: u16 = adc2.blocking_read(&mut adc2_pin1); | ||
| 53 | let volt: f32 = 3.3 * raw as f32 / max2 as f32; | ||
| 54 | info!("Read adc2 pin 1 {}", volt); | ||
| 55 | |||
| 56 | let raw: u16 = adc2.blocking_read(&mut adc2_pin2); | ||
| 57 | let volt: f32 = 3.3 * raw as f32 / max2 as f32; | ||
| 58 | info!("Read adc2 pin 2 {}", volt); | ||
| 59 | |||
| 60 | // **** ADC4 blocking read **** | ||
| 61 | let raw: u16 = adc4.blocking_read(&mut adc4_pin1); | ||
| 62 | let volt: f32 = 3.3 * raw as f32 / max4 as f32; | ||
| 63 | info!("Read adc4 pin 1 {}", volt); | ||
| 64 | |||
| 65 | let raw: u16 = adc4.blocking_read(&mut adc4_pin2); | ||
| 66 | let volt: f32 = 3.3 * raw as f32 / max4 as f32; | ||
| 67 | info!("Read adc4 pin 2 {}", volt); | ||
| 68 | |||
| 69 | // **** ADC1 async read **** | ||
| 70 | let mut degraded11 = adc1_pin1.degrade_adc(); | ||
| 71 | let mut degraded12 = adc1_pin2.degrade_adc(); | ||
| 72 | let mut measurements = [0u16; 2]; | ||
| 73 | |||
| 74 | adc1.read( | ||
| 75 | &mut p.GPDMA1_CH0, | ||
| 76 | [ | ||
| 77 | (&mut degraded11, adc::SampleTime::CYCLES160_5), | ||
| 78 | (&mut degraded12, adc::SampleTime::CYCLES160_5), | ||
| 79 | ] | ||
| 80 | .into_iter(), | ||
| 81 | &mut measurements, | ||
| 82 | ) | ||
| 83 | .await; | ||
| 84 | let volt1: f32 = 3.3 * measurements[0] as f32 / max1 as f32; | ||
| 85 | let volt2: f32 = 3.3 * measurements[1] as f32 / max1 as f32; | ||
| 86 | |||
| 87 | info!("Async read 1 pin 1 {}", volt1); | ||
| 88 | info!("Async read 1 pin 2 {}", volt2); | ||
| 89 | |||
| 90 | // **** ADC2 does not support async read **** | ||
| 91 | |||
| 92 | // **** ADC4 async read **** | ||
| 93 | let mut degraded41 = adc4_pin1.degrade_adc(); | ||
| 94 | let mut degraded42 = adc4_pin2.degrade_adc(); | ||
| 95 | let mut measurements = [0u16; 2]; | ||
| 96 | |||
| 97 | // The channels must be in ascending order and can't repeat for ADC4 | ||
| 98 | adc4.read( | ||
| 99 | &mut p.GPDMA1_CH1, | ||
| 100 | [&mut degraded42, &mut degraded41].into_iter(), | ||
| 101 | &mut measurements, | ||
| 102 | ) | ||
| 103 | .await | ||
| 104 | .unwrap(); | ||
| 105 | let volt2: f32 = 3.3 * measurements[0] as f32 / max4 as f32; | ||
| 106 | let volt1: f32 = 3.3 * measurements[1] as f32 / max4 as f32; | ||
| 107 | info!("Async read 4 pin 1 {}", volt1); | ||
| 108 | info!("Async read 4 pin 2 {}", volt2); | ||
| 109 | } | ||
