added ADC example running on L0

This commit is contained in:
shufps 2024-02-08 11:07:30 +01:00
parent ab8f25fd78
commit 8dff89bfd9

View File

@ -0,0 +1,46 @@
#![no_std]
#![no_main]
use defmt::*;
use embassy_stm32::Config;
use embassy_executor::Spawner;
use embassy_stm32::adc::{Adc, SampleTime};
use embassy_stm32::peripherals::ADC;
use embassy_stm32::{adc, bind_interrupts};
use embassy_time::{Delay, Timer};
use {defmt_rtt as _, panic_probe as _};
bind_interrupts!(struct Irqs {
ADC1_COMP => adc::InterruptHandler<ADC>;
});
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
// enable HSI because default is MSI but ADC doesn't support
// this as clock source on L0s and uses HSI by default
let mut config = Config::default();
config.rcc.hsi = true;
let p = embassy_stm32::init(config);
info!("Hello World!");
let mut adc = Adc::new(p.ADC, Irqs, &mut Delay);
adc.set_sample_time(SampleTime::Cycles79_5);
let mut pin = p.PA1;
let mut vrefint = adc.enable_vref(&mut Delay);
let vrefint_sample = adc.read(&mut vrefint).await;
let convert_to_millivolts = |sample| {
// From https://www.st.com/resource/en/datasheet/stm32l051c6.pdf
// 6.3.3 Embedded internal reference voltage
const VREFINT_MV: u32 = 1224; // mV
(u32::from(sample) * VREFINT_MV / u32::from(vrefint_sample)) as u16
};
loop {
let v = adc.read(&mut pin).await;
info!("--> {} - {} mV", v, convert_to_millivolts(v));
Timer::after_millis(100).await;
}
}