Skip to content

Commit 715ded2

Browse files
committed
temperature needs to be calibrated against Vref+
RM0351 pg580 says to scale the read value by (Vref+ / 3.0)
1 parent f1ac138 commit 715ded2

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/adc.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,11 @@ impl ADC {
277277
}
278278

279279
/// Convert a raw sample from the `Temperature` to deg C
280-
pub fn to_degrees_centigrade(sample: u16) -> f32 {
281-
(130.0 - 30.0) / (VtempCal130::get().read() as f32 - VtempCal30::get().read() as f32)
282-
* (sample as f32 - VtempCal30::get().read() as f32)
280+
pub fn to_degrees_centigrade(&self, sample: u16) -> f32 {
281+
let sample = (u32::from(sample) * self.calibrated_vdda) / VDDA_CALIB_MV;
282+
(VtempCal130::TEMP_DEGREES - VtempCal30::TEMP_DEGREES) as f32
283+
/ (VtempCal130::get().read() - VtempCal30::get().read()) as f32
284+
* (sample - u32::from(VtempCal30::get().read())) as f32
283285
+ 30.0
284286
}
285287

src/signature.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,25 +93,32 @@ impl VrefCal {
9393
}
9494

9595
/// A temperature reading taken at 30°C stored at the factory
96+
/// aka TS_CAL1
9697
#[derive(Debug)]
9798
#[repr(C)]
9899
pub struct VtempCal30(u16);
99100
define_ptr_type!(VtempCal30, 0x1FFF_75A8);
100101

101102
impl VtempCal30 {
103+
/// TS_CAL1_TEMP
104+
pub const TEMP_DEGREES: u16 = 30;
102105
/// Read calibration value
103106
pub fn read(&self) -> u16 {
104107
self.0
105108
}
106109
}
107110

108111
/// A temperature reading taken at 130°C stored at the factory
112+
/// aka TS_CAL2
109113
#[derive(Debug)]
110114
#[repr(C)]
111115
pub struct VtempCal130(u16);
112116
define_ptr_type!(VtempCal130, 0x1FFF_75CA);
113117

114118
impl VtempCal130 {
119+
/// TS_CAL2_TEMP
120+
/// TODO: this is 110 for L47x, 130 for L43x
121+
pub const TEMP_DEGREES: u16 = 130;
115122
/// Read calibration value
116123
pub fn read(&self) -> u16 {
117124
self.0

0 commit comments

Comments
 (0)