2022-02-10 23:21:40 +01:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
2022-10-07 13:31:55 +02:00
|
|
|
use cortex_m::prelude::_embedded_hal_blocking_delay_DelayUs;
|
2022-04-02 04:35:06 +02:00
|
|
|
use defmt::*;
|
2022-08-17 23:40:16 +02:00
|
|
|
use embassy_executor::Spawner;
|
2022-10-07 13:53:03 +02:00
|
|
|
use embassy_stm32::adc::{Adc, Temperature, VrefInt};
|
2023-10-15 01:57:25 +02:00
|
|
|
use embassy_time::{Delay, Timer};
|
2022-06-12 22:15:44 +02:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
2022-02-10 23:21:40 +01:00
|
|
|
|
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());
|
2022-02-10 23:21:40 +01:00
|
|
|
info!("Hello World!");
|
|
|
|
|
2022-10-07 13:31:55 +02:00
|
|
|
let mut delay = Delay;
|
|
|
|
let mut adc = Adc::new(p.ADC1, &mut delay);
|
2022-02-10 23:21:40 +01:00
|
|
|
let mut pin = p.PC1;
|
|
|
|
|
2022-10-07 13:31:55 +02:00
|
|
|
let mut vrefint = adc.enable_vrefint();
|
|
|
|
let mut temp = adc.enable_temperature();
|
|
|
|
|
|
|
|
// Startup delay can be combined to the maximum of either
|
|
|
|
delay.delay_us(Temperature::start_time_us().max(VrefInt::start_time_us()));
|
|
|
|
|
2023-09-28 03:58:46 +02:00
|
|
|
let vrefint_sample = adc.read(&mut vrefint);
|
2022-10-23 23:31:10 +02:00
|
|
|
|
|
|
|
let convert_to_millivolts = |sample| {
|
|
|
|
// From http://www.st.com/resource/en/datasheet/DM00071990.pdf
|
|
|
|
// 6.3.24 Reference voltage
|
2022-10-24 22:01:16 +02:00
|
|
|
const VREFINT_MV: u32 = 1210; // 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
|
|
|
};
|
|
|
|
|
|
|
|
let convert_to_celcius = |sample| {
|
|
|
|
// From http://www.st.com/resource/en/datasheet/DM00071990.pdf
|
|
|
|
// 6.3.22 Temperature sensor characteristics
|
|
|
|
const V25: i32 = 760; // mV
|
|
|
|
const AVG_SLOPE: f32 = 2.5; // mV/C
|
|
|
|
|
|
|
|
let sample_mv = convert_to_millivolts(sample) as i32;
|
|
|
|
|
|
|
|
(sample_mv - V25) as f32 / AVG_SLOPE + 25.0
|
|
|
|
};
|
|
|
|
|
2022-10-24 22:01:16 +02:00
|
|
|
info!("VrefInt: {}", vrefint_sample);
|
|
|
|
const MAX_ADC_SAMPLE: u16 = (1 << 12) - 1;
|
|
|
|
info!("VCCA: {} mV", convert_to_millivolts(MAX_ADC_SAMPLE));
|
|
|
|
|
2022-02-10 23:21:40 +01:00
|
|
|
loop {
|
2022-10-07 13:31:55 +02:00
|
|
|
// Read pin
|
2022-02-10 23:21:40 +01:00
|
|
|
let v = adc.read(&mut pin);
|
2022-10-23 23:31:10 +02:00
|
|
|
info!("PC1: {} ({} mV)", v, convert_to_millivolts(v));
|
2022-10-07 13:31:55 +02:00
|
|
|
|
|
|
|
// Read internal temperature
|
2023-09-28 03:58:46 +02:00
|
|
|
let v = adc.read(&mut temp);
|
2022-10-23 23:31:10 +02:00
|
|
|
let celcius = convert_to_celcius(v);
|
2022-10-07 13:31:55 +02:00
|
|
|
info!("Internal temp: {} ({} C)", v, celcius);
|
|
|
|
|
|
|
|
// Read internal voltage reference
|
2023-09-28 03:58:46 +02:00
|
|
|
let v = adc.read(&mut vrefint);
|
2022-10-24 22:01:16 +02:00
|
|
|
info!("VrefInt: {}", v);
|
2022-10-07 13:31:55 +02:00
|
|
|
|
2023-10-15 01:57:25 +02:00
|
|
|
Timer::after_millis(100).await;
|
2022-02-10 23:21:40 +01:00
|
|
|
}
|
|
|
|
}
|