aboutsummaryrefslogtreecommitdiff
path: root/examples/stm32wle5/src/bin/adc.rs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/stm32wle5/src/bin/adc.rs')
-rw-r--r--examples/stm32wle5/src/bin/adc.rs67
1 files changed, 67 insertions, 0 deletions
diff --git a/examples/stm32wle5/src/bin/adc.rs b/examples/stm32wle5/src/bin/adc.rs
new file mode 100644
index 000000000..8cc84ccdf
--- /dev/null
+++ b/examples/stm32wle5/src/bin/adc.rs
@@ -0,0 +1,67 @@
1#![no_std]
2#![no_main]
3
4use defmt::*;
5#[cfg(feature = "defmt-rtt")]
6use defmt_rtt as _;
7use embassy_executor::Spawner;
8use embassy_stm32::adc::{Adc, SampleTime};
9use embassy_stm32::low_power;
10use embassy_time::Timer;
11use panic_probe as _;
12use static_cell::StaticCell;
13
14#[embassy_executor::main(executor = "low_power::Executor")]
15async fn async_main(_spawner: Spawner) {
16 let mut config = embassy_stm32::Config::default();
17 // enable HSI clock
18 config.rcc.hsi = true;
19 // enable LSI clock for RTC
20 config.rcc.ls = embassy_stm32::rcc::LsConfig::default_lsi();
21 config.rcc.msi = Some(embassy_stm32::rcc::MSIRange::RANGE4M);
22 config.rcc.sys = embassy_stm32::rcc::Sysclk::MSI;
23 // enable ADC with HSI clock
24 config.rcc.mux.adcsel = embassy_stm32::pac::rcc::vals::Adcsel::HSI;
25 #[cfg(feature = "defmt-serial")]
26 {
27 // disable debug during sleep to reduce power consumption since we are
28 // using defmt-serial on LPUART1.
29 config.enable_debug_during_sleep = false;
30 // if we are using defmt-serial on LPUART1, we need to use HSI for the clock
31 // so that its registers are preserved during STOP modes.
32 config.rcc.mux.lpuart1sel = embassy_stm32::pac::rcc::vals::Lpuart1sel::HSI;
33 }
34 // Initialize STM32WL peripherals (use default config like wio-e5-async example)
35 let p = embassy_stm32::init(config);
36
37 #[cfg(feature = "defmt-serial")]
38 {
39 use embassy_stm32::mode::Blocking;
40 use embassy_stm32::usart::Uart;
41 let config = embassy_stm32::usart::Config::default();
42 let uart = Uart::new_blocking(p.LPUART1, p.PC0, p.PC1, config).expect("failed to configure UART!");
43 static SERIAL: StaticCell<Uart<'static, Blocking>> = StaticCell::new();
44 defmt_serial::defmt_serial(SERIAL.init(uart));
45 }
46
47 info!("Hello World!");
48
49 let mut adc = Adc::new(p.ADC1);
50 let mut pin = p.PA10;
51
52 let mut vrefint = adc.enable_vrefint();
53 let vrefint_sample = adc.blocking_read(&mut vrefint, SampleTime::CYCLES79_5);
54 let convert_to_millivolts = |sample| {
55 // From https://www.st.com/resource/en/datasheet/stm32g031g8.pdf
56 // 6.3.3 Embedded internal reference voltage
57 const VREFINT_MV: u32 = 1212; // mV
58
59 (u32::from(sample) * VREFINT_MV / u32::from(vrefint_sample)) as u16
60 };
61
62 loop {
63 let v = adc.blocking_read(&mut pin, SampleTime::CYCLES79_5);
64 info!("--> {} - {} mV", v, convert_to_millivolts(v));
65 Timer::after_secs(1).await;
66 }
67}