aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorxoviat <[email protected]>2023-09-18 16:31:20 -0500
committerxoviat <[email protected]>2023-09-18 16:31:20 -0500
commite640933e2f0c8fb0b5a6df645002f69b55f51d8a (patch)
tree3f2d8c4e4e338c96097d2b85a93244cc74b2be5e /examples
parentdaeb497045d47db3afa926b4e0e9e17973a544d5 (diff)
stm32/adc: add async conversion
Diffstat (limited to 'examples')
-rw-r--r--examples/stm32f334/src/bin/adc.rs23
1 files changed, 14 insertions, 9 deletions
diff --git a/examples/stm32f334/src/bin/adc.rs b/examples/stm32f334/src/bin/adc.rs
index e8b2cd8a7..ed246a7db 100644
--- a/examples/stm32f334/src/bin/adc.rs
+++ b/examples/stm32f334/src/bin/adc.rs
@@ -4,13 +4,18 @@
4 4
5use defmt::info; 5use defmt::info;
6use embassy_executor::Spawner; 6use embassy_executor::Spawner;
7use embassy_stm32::adc::{Adc, SampleTime, VREF_INT}; 7use embassy_stm32::adc::{Adc, SampleTime};
8use embassy_stm32::peripherals::ADC1;
8use embassy_stm32::rcc::AdcClockSource; 9use embassy_stm32::rcc::AdcClockSource;
9use embassy_stm32::time::mhz; 10use embassy_stm32::time::mhz;
10use embassy_stm32::Config; 11use embassy_stm32::{adc, bind_interrupts, Config};
11use embassy_time::{Delay, Duration, Timer}; 12use embassy_time::{Delay, Duration, Timer};
12use {defmt_rtt as _, panic_probe as _}; 13use {defmt_rtt as _, panic_probe as _};
13 14
15bind_interrupts!(struct Irqs {
16 ADC1_2 => adc::InterruptHandler<ADC1>;
17});
18
14#[embassy_executor::main] 19#[embassy_executor::main]
15async fn main(_spawner: Spawner) -> ! { 20async fn main(_spawner: Spawner) -> ! {
16 let mut config = Config::default(); 21 let mut config = Config::default();
@@ -24,7 +29,7 @@ async fn main(_spawner: Spawner) -> ! {
24 29
25 info!("create adc..."); 30 info!("create adc...");
26 31
27 let mut adc = Adc::new(p.ADC1, &mut Delay); 32 let mut adc = Adc::new(p.ADC1, Irqs, &mut Delay);
28 33
29 adc.set_sample_time(SampleTime::Cycles601_5); 34 adc.set_sample_time(SampleTime::Cycles601_5);
30 35
@@ -34,18 +39,18 @@ async fn main(_spawner: Spawner) -> ! {
34 let mut temperature = adc.enable_temperature(); 39 let mut temperature = adc.enable_temperature();
35 40
36 loop { 41 loop {
37 let vref = adc.read(&mut vrefint); 42 let vref = adc.read(&mut vrefint).await;
38 info!("read vref: {}", vref); 43 info!("read vref: {} (should be {})", vref, vrefint.value());
39 44
40 let temp = adc.read(&mut temperature); 45 let temp = adc.read(&mut temperature).await;
41 info!("read temperature: {}", temp); 46 info!("read temperature: {}", temp);
42 47
43 let pin = adc.read(&mut p.PA0); 48 let pin = adc.read(&mut p.PA0).await;
44 info!("read pin: {}", pin); 49 info!("read pin: {}", pin);
45 50
46 let pin_mv = pin as u32 * VREF_INT as u32 / vref as u32; 51 let pin_mv = (pin as u32 * vrefint.value() as u32 / vref as u32) * 3300 / 4095;
47 info!("computed pin mv: {}", pin_mv); 52 info!("computed pin mv: {}", pin_mv);
48 53
49 Timer::after(Duration::from_secs(1)).await; 54 Timer::after(Duration::from_millis(500)).await;
50 } 55 }
51} 56}