2021-12-30 09:53:39 +00:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
|
2022-04-02 02:35:06 +00:00
|
|
|
use defmt::*;
|
2022-08-17 21:40:16 +00:00
|
|
|
use embassy_executor::Spawner;
|
2021-12-30 09:53:39 +00:00
|
|
|
use embassy_stm32::adc::Adc;
|
2023-09-28 01:58:46 +00:00
|
|
|
use embassy_stm32::peripherals::ADC1;
|
|
|
|
use embassy_stm32::{adc, bind_interrupts};
|
2023-10-14 23:57:25 +00:00
|
|
|
use embassy_time::{Delay, Timer};
|
2022-06-12 20:15:44 +00:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
2021-12-30 09:53:39 +00:00
|
|
|
|
2023-09-28 01:58:46 +00:00
|
|
|
bind_interrupts!(struct Irqs {
|
|
|
|
ADC1_2 => adc::InterruptHandler<ADC1>;
|
|
|
|
});
|
|
|
|
|
2022-07-29 19:58:35 +00:00
|
|
|
#[embassy_executor::main]
|
2022-08-17 16:49:55 +00:00
|
|
|
async fn main(_spawner: Spawner) {
|
|
|
|
let p = embassy_stm32::init(Default::default());
|
2021-12-30 09:53:39 +00:00
|
|
|
info!("Hello World!");
|
|
|
|
|
|
|
|
let mut adc = Adc::new(p.ADC1, &mut Delay);
|
|
|
|
let mut pin = p.PB1;
|
|
|
|
|
2022-10-24 20:01:16 +00:00
|
|
|
let mut vrefint = adc.enable_vref(&mut Delay);
|
2023-09-28 01:58:46 +00:00
|
|
|
let vrefint_sample = adc.read(&mut vrefint).await;
|
2022-10-23 21:31:10 +00:00
|
|
|
let convert_to_millivolts = |sample| {
|
|
|
|
// From http://www.st.com/resource/en/datasheet/CD00161566.pdf
|
|
|
|
// 5.3.4 Embedded reference voltage
|
2022-10-24 20:01:16 +00:00
|
|
|
const VREFINT_MV: u32 = 1200; // mV
|
2022-10-23 21:31:10 +00:00
|
|
|
|
2022-10-24 20:01:16 +00:00
|
|
|
(u32::from(sample) * VREFINT_MV / u32::from(vrefint_sample)) as u16
|
2022-10-23 21:31:10 +00:00
|
|
|
};
|
|
|
|
|
2021-12-30 09:53:39 +00:00
|
|
|
loop {
|
2023-09-28 01:58:46 +00:00
|
|
|
let v = adc.read(&mut pin).await;
|
2022-10-23 21:31:10 +00:00
|
|
|
info!("--> {} - {} mV", v, convert_to_millivolts(v));
|
2023-10-14 23:57:25 +00:00
|
|
|
Timer::after_millis(100).await;
|
2021-12-30 09:53:39 +00:00
|
|
|
}
|
|
|
|
}
|