Skip to content

Commit edaae84

Browse files
committed
Fix VTemp conversion
It was not working and unnecessarily complicated
1 parent c4858d3 commit edaae84

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1515
- Update `remap_pins()` and remove critical section
1616
- Updated `stm32f0` peripheral access crate from 0.13 to 0.14
1717
- Address a few clippy lints
18+
- Fix `VTemp::convert_temp()`
1819

1920
### Added
2021

src/adc.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -270,19 +270,15 @@ impl VTemp {
270270
}
271271

272272
fn convert_temp(vtemp: u16, vdda: u16) -> i16 {
273-
let vtemp30_cal = i32::from(unsafe { ptr::read(VTEMPCAL30) }) * 100;
274-
let vtemp110_cal = i32::from(unsafe { ptr::read(VTEMPCAL110) }) * 100;
275-
276-
let mut temperature = i32::from(vtemp) * 100;
277-
temperature = (temperature * (i32::from(vdda) / i32::from(VDD_CALIB))) - vtemp30_cal;
278-
temperature *= (110 - 30) * 100;
279-
temperature /= vtemp110_cal - vtemp30_cal;
280-
temperature += 3000;
281-
temperature as i16
273+
let vtemp30_cal = unsafe { ptr::read(VTEMPCAL30) } as i32;
274+
let vtemp110_cal = unsafe { ptr::read(VTEMPCAL110) } as i32;
275+
let raw_temp_comp = vtemp as u32 * vdda as u32 / VDD_CALIB as u32;
276+
((raw_temp_comp as i32 - vtemp30_cal) * 10 * (110 - 30) / (vtemp110_cal - vtemp30_cal)
277+
+ 300) as i16
282278
}
283279

284280
/// Read the value of the internal temperature sensor and return the
285-
/// result in 100ths of a degree centigrade.
281+
/// result in 10ths of a degree centigrade.
286282
///
287283
/// Given a delay reference it will attempt to restrict to the
288284
/// minimum delay needed to ensure a 10 us t<sub>START</sub> value.

0 commit comments

Comments
 (0)