test: tests for temperature without linker error

This commit is contained in:
cscherr 2025-04-30 16:41:46 +02:00
parent c82d194206
commit b69e9e3fd4
Signed by: cscherrNT
GPG key ID: 8E2B45BC51A27EA7

View file

@ -1,19 +1,17 @@
// NOTE: This is at least unreliable and likely wrong #![cfg_attr(not(test), no_main)]
#![cfg_attr(not(test), no_std)]
#![no_main] #[cfg(not(test))]
#![no_std] extern crate panic_halt;
use defmt::{debug, info};
use hal::adc::{Adc, Ready, VRef, VTemp}; use hal::adc::{Adc, Ready, VRef, VTemp};
use hal::calibration::{VtempCal30, VtempCal130}; use hal::calibration::{VtempCal30, VtempCal130};
use panic_probe as _;
use defmt_rtt as _; // global logger
use cortex_m_rt::entry;
use hal::{pac, prelude::*, rcc::Config}; use hal::{pac, prelude::*, rcc::Config};
#[entry] use defmt::{debug, info};
use defmt_rtt as _; // global logger
#[cfg_attr(not(test), cortex_m_rt::entry)] // this is the entrypoint unless testing
fn main() -> ! { fn main() -> ! {
let dp = pac::Peripherals::take().unwrap(); let dp = pac::Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().unwrap(); let cp = cortex_m::Peripherals::take().unwrap();
@ -44,20 +42,27 @@ fn main() -> ! {
info!("tsense_cal1: {:?}", (30, tsense_cal1)); info!("tsense_cal1: {:?}", (30, tsense_cal1));
info!("tsense_cal2: {:?}", (130, tsense_cal2)); info!("tsense_cal2: {:?}", (130, tsense_cal2));
delay.delay_ms(50_u16); // read a few values into void, maybe this will help get that thing started
for _ in 0..20 {
let _ = read_temp_mv(&mut adc, 1.0);
delay.delay_ms(10_u16);
}
let vref_actual: u16 = adc.read(&mut VRef).unwrap(); let vref_actual: u16 = adc.read(&mut VRef).unwrap();
let vref_factor = vref_cal as f32 / vref_actual as f32; let vref_factor = vref_cal as f32 / vref_actual as f32;
debug!( info!(
"vref actual={} calibration={} => factor={}", "vref actual={} calibration={} => factor={}",
vref_actual, vref_cal, vref_factor vref_actual, vref_cal, vref_factor
); );
delay.delay_ms(10_u16);
let mut temp_c; let mut temp_c;
let mut temp_mv; let mut temp_mv;
loop { loop {
temp_mv = read_temp_mv(&mut adc, vref_factor); temp_mv = read_temp_mv(&mut adc, vref_factor);
temp_c = temp_mv_to_c(temp_mv, tsense_cal1, tsense_cal2); temp_c = temp_mv_to_c(temp_mv, tsense_cal1, tsense_cal2);
info!("Temperature: {:03}mv, {:04}c", temp_mv, temp_c as i32); info!("Temperature: {:03}mv, {:04}°C", temp_mv, temp_c as i32);
delay.delay_ms(500_u16); delay.delay_ms(500_u16);
} }
} }
@ -74,3 +79,26 @@ fn temp_mv_to_c(temp: f32, ts_cal_1: (i32, u16), ts_cal_2: (i32, u16)) -> f32 {
* (temp - ts_cal_1.1 as f32) * (temp - ts_cal_1.1 as f32)
+ ts_cal_1.0 as f32 + ts_cal_1.0 as f32
} }
#[cfg(test)]
mod tests {
// run these tests: cargo test --example=temperature --target=x86_64-unknown-linux-gnu
use super::temp_mv_to_c;
#[test]
fn test_mv_to_c() {
// values read out from my board as logged
// after flashing and running
//
// First is the temperature for that calibratoin, second is the measured voltage at that
// temperature
let calibration_data = [(30, 673), (130, 912)];
for caldat in calibration_data {
let degrees: f32 =
temp_mv_to_c(caldat.1 as f32, calibration_data[0], calibration_data[1]);
assert_eq!(caldat.0 as f32, degrees);
dbg!(caldat);
dbg!(degrees);
}
}
}