2021-12-30 10:53:39 +01:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
2022-04-02 04:35:06 +02:00
|
|
|
use defmt::*;
|
2022-08-17 23:40:16 +02:00
|
|
|
use embassy_executor::Spawner;
|
2021-12-30 10:53:39 +01:00
|
|
|
use embassy_stm32::adc::Adc;
|
2023-09-28 03:58:46 +02:00
|
|
|
use embassy_stm32::peripherals::ADC1;
|
|
|
|
use embassy_stm32::{adc, bind_interrupts};
|
2022-08-17 23:40:16 +02:00
|
|
|
use embassy_time::{Delay, Duration, Timer};
|
2022-06-12 22:15:44 +02:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
2021-12-30 10:53:39 +01:00
|
|
|
|
2023-09-28 03:58:46 +02:00
|
|
|
bind_interrupts!(struct Irqs {
|
|
|
|
ADC1_2 => adc::InterruptHandler<ADC1>;
|
|
|
|
});
|
|
|
|
|
2022-07-29 21:58:35 +02:00
|
|
|
#[embassy_executor::main]
|
2022-08-17 18:49:55 +02:00
|
|
|
async fn main(_spawner: Spawner) {
|
|
|
|
let p = embassy_stm32::init(Default::default());
|
2021-12-30 10:53:39 +01:00
|
|
|
info!("Hello World!");
|
|
|
|
|
|
|
|
let mut adc = Adc::new(p.ADC1, &mut Delay);
|
|
|
|
let mut pin = p.PB1;
|
|
|
|
|
2022-10-24 22:01:16 +02:00
|
|
|
let mut vrefint = adc.enable_vref(&mut Delay);
|
2023-09-28 03:58:46 +02:00
|
|
|
let vrefint_sample = adc.read(&mut vrefint).await;
|
2022-10-23 23:31:10 +02: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 22:01:16 +02:00
|
|
|
const VREFINT_MV: u32 = 1200; // mV
|
2022-10-23 23:31:10 +02:00
|
|
|
|
2022-10-24 22:01:16 +02:00
|
|
|
(u32::from(sample) * VREFINT_MV / u32::from(vrefint_sample)) as u16
|
2022-10-23 23:31:10 +02:00
|
|
|
};
|
|
|
|
|
2021-12-30 10:53:39 +01:00
|
|
|
loop {
|
2023-09-28 03:58:46 +02:00
|
|
|
let v = adc.read(&mut pin).await;
|
2022-10-23 23:31:10 +02:00
|
|
|
info!("--> {} - {} mV", v, convert_to_millivolts(v));
|
2022-01-27 00:08:02 +01:00
|
|
|
Timer::after(Duration::from_millis(100)).await;
|
2021-12-30 10:53:39 +01:00
|
|
|
}
|
|
|
|
}
|