embassy/examples/stm32f334/src/bin/adc.rs

57 lines
1.6 KiB
Rust
Raw Normal View History

2023-09-05 23:46:57 +02:00
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use defmt::info;
use embassy_executor::Spawner;
2023-09-18 23:31:20 +02:00
use embassy_stm32::adc::{Adc, SampleTime};
use embassy_stm32::peripherals::ADC1;
use embassy_stm32::rcc::{AdcClockSource, Adcpres};
use embassy_stm32::time::mhz;
2023-09-18 23:31:20 +02:00
use embassy_stm32::{adc, bind_interrupts, Config};
use embassy_time::{Delay, Timer};
2023-09-05 23:46:57 +02:00
use {defmt_rtt as _, panic_probe as _};
2023-09-18 23:31:20 +02:00
bind_interrupts!(struct Irqs {
ADC1_2 => adc::InterruptHandler<ADC1>;
});
2023-09-05 23:46:57 +02:00
#[embassy_executor::main]
async fn main(_spawner: Spawner) -> ! {
let mut config = Config::default();
config.rcc.sysclk = Some(mhz(64));
config.rcc.hclk = Some(mhz(64));
config.rcc.pclk1 = Some(mhz(32));
config.rcc.pclk2 = Some(mhz(64));
config.rcc.adc = Some(AdcClockSource::Pll(Adcpres::DIV1));
2023-09-05 23:46:57 +02:00
let mut p = embassy_stm32::init(config);
2023-09-10 05:01:51 +02:00
info!("create adc...");
2023-09-18 23:31:20 +02:00
let mut adc = Adc::new(p.ADC1, Irqs, &mut Delay);
2023-09-05 23:46:57 +02:00
adc.set_sample_time(SampleTime::Cycles601_5);
2023-09-10 05:01:51 +02:00
info!("enable vrefint...");
2023-09-05 23:46:57 +02:00
2023-09-10 05:01:51 +02:00
let mut vrefint = adc.enable_vref(&mut Delay);
let mut temperature = adc.enable_temperature();
2023-09-05 23:46:57 +02:00
loop {
2023-09-18 23:31:20 +02:00
let vref = adc.read(&mut vrefint).await;
info!("read vref: {} (should be {})", vref, vrefint.value());
2023-09-10 05:01:51 +02:00
2023-09-18 23:31:20 +02:00
let temp = adc.read(&mut temperature).await;
2023-09-10 05:01:51 +02:00
info!("read temperature: {}", temp);
2023-09-18 23:31:20 +02:00
let pin = adc.read(&mut p.PA0).await;
2023-09-10 05:01:51 +02:00
info!("read pin: {}", pin);
2023-09-18 23:31:20 +02:00
let pin_mv = (pin as u32 * vrefint.value() as u32 / vref as u32) * 3300 / 4095;
2023-09-10 05:01:51 +02:00
info!("computed pin mv: {}", pin_mv);
Timer::after_millis(500).await;
2023-09-05 23:46:57 +02:00
}
}