1024: stm32/adc: Remove voltage and temperature conversions r=Dirbaio a=GrantM11235

The current conversion utilities are confusing and a bit of a footgun. (Two out of the three examples got it wrong! They didn't measure vref at all, so all the conversions are completely wrong if vcca isn't 3.3v)

I think we should eventually have some sort of conversion utilities in the HAL, but for now I think it is best to just remove it and let the users do their own math.

cc `@chemicstry` 

Co-authored-by: Grant Miller <GrantM11235@gmail.com>
This commit is contained in:
bors[bot]
2022-10-26 19:44:06 +00:00
committed by GitHub
7 changed files with 50 additions and 104 deletions

View File

@ -16,11 +16,19 @@ async fn main(_spawner: Spawner) {
let mut adc = Adc::new(p.ADC1, &mut Delay);
let mut pin = p.PB1;
let mut vref = adc.enable_vref(&mut Delay);
adc.calibrate(&mut vref);
let mut vrefint = adc.enable_vref(&mut Delay);
let vrefint_sample = adc.read(&mut vrefint);
let convert_to_millivolts = |sample| {
// From http://www.st.com/resource/en/datasheet/CD00161566.pdf
// 5.3.4 Embedded reference voltage
const VREFINT_MV: u32 = 1200; // mV
(u32::from(sample) * VREFINT_MV / u32::from(vrefint_sample)) as u16
};
loop {
let v = adc.read(&mut pin);
info!("--> {} - {} mV", v, adc.to_millivolts(v));
info!("--> {} - {} mV", v, convert_to_millivolts(v));
Timer::after(Duration::from_millis(100)).await;
}
}

View File

@ -24,19 +24,44 @@ async fn main(_spawner: Spawner) {
// Startup delay can be combined to the maximum of either
delay.delay_us(Temperature::start_time_us().max(VrefInt::start_time_us()));
let vrefint_sample = adc.read_internal(&mut vrefint);
let convert_to_millivolts = |sample| {
// From http://www.st.com/resource/en/datasheet/DM00071990.pdf
// 6.3.24 Reference voltage
const VREFINT_MV: u32 = 1210; // mV
(u32::from(sample) * VREFINT_MV / u32::from(vrefint_sample)) as u16
};
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
};
info!("VrefInt: {}", vrefint_sample);
const MAX_ADC_SAMPLE: u16 = (1 << 12) - 1;
info!("VCCA: {} mV", convert_to_millivolts(MAX_ADC_SAMPLE));
loop {
// Read pin
let v = adc.read(&mut pin);
info!("PC1: {} ({} mV)", v, adc.to_millivolts(v));
info!("PC1: {} ({} mV)", v, convert_to_millivolts(v));
// Read internal temperature
let v = adc.read_internal(&mut temp);
let celcius = Temperature::to_celcius(adc.to_millivolts(v));
let celcius = convert_to_celcius(v);
info!("Internal temp: {} ({} C)", v, celcius);
// Read internal voltage reference
let v = adc.read_internal(&mut vrefint);
info!("VrefInt: {} ({} mV)", v, adc.to_millivolts(v));
info!("VrefInt: {}", v);
Timer::after(Duration::from_millis(100)).await;
}

View File

@ -16,9 +16,19 @@ async fn main(_spawner: Spawner) {
let mut adc = Adc::new(p.ADC1, &mut Delay);
let mut pin = p.PA3;
let mut vrefint = adc.enable_vrefint();
let vrefint_sample = adc.read_internal(&mut vrefint);
let convert_to_millivolts = |sample| {
// From http://www.st.com/resource/en/datasheet/DM00273119.pdf
// 6.3.27 Reference voltage
const VREFINT_MV: u32 = 1210; // mV
(u32::from(sample) * VREFINT_MV / u32::from(vrefint_sample)) as u16
};
loop {
let v = adc.read(&mut pin);
info!("--> {} - {} mV", v, adc.to_millivolts(v));
info!("--> {} - {} mV", v, convert_to_millivolts(v));
Timer::after(Duration::from_millis(100)).await;
}
}