Skip to content

Stm32l47x adc temperature #265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
},
pac,
rcc::{Enable, Reset, AHB2, CCIPR},
signature::{VrefCal, VtempCal130, VtempCal30, VDDA_CALIB_MV},
signature::{VrefCal, VtempCalHigh, VtempCalLow, VDDA_CALIB_MV},
};

use pac::{ADC1, ADC_COMMON};
Expand Down Expand Up @@ -201,6 +201,7 @@ impl ADC {
pub fn enable_temperature(&mut self, delay: &mut impl DelayUs<u32>) -> Temperature {
self.common.ccr.modify(|_, w| w.ch17sel().set_bit());

// FIXME: This note from the reference manual is currently not possible
// rm0351 section 18.4.32 pg580 (L47/L48/L49/L4A models)
// Note:
// The sensor has a startup time after waking from power-down mode before it can output VTS
Expand Down Expand Up @@ -278,12 +279,14 @@ impl ADC {
/// Convert a raw sample from the `Temperature` to deg C
pub fn to_degrees_centigrade(&self, sample: u16) -> f32 {
let sample = (u32::from(sample) * self.calibrated_vdda) / VDDA_CALIB_MV;
(VtempCal130::TEMP_DEGREES - VtempCal30::TEMP_DEGREES) as f32
(VtempCalHigh::TEMP_DEGREES - VtempCalLow::TEMP_DEGREES) as f32
// as signed because RM0351 doesn't specify against this being an
// inverse relation (which would result in a negative differential)
/ (VtempCal130::get().read() as i32 - VtempCal30::get().read() as i32) as f32
/ (VtempCalHigh::get().read() as i32 - VtempCalLow::get().read() as i32) as f32
// this can definitely be negative so must be done as a signed value
* (sample as i32 - VtempCal30::get().read() as i32) as f32
* (sample as i32 - VtempCalLow::get().read() as i32) as f32
// while it would make sense for this to be `VtempCalLow::TEMP_DEGREES` (which is 30*C),
// the RM specifically uses 30*C so this will too
+ 30.0
}

Expand Down
28 changes: 22 additions & 6 deletions src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ impl VrefCal {
/// aka TS_CAL1 in reference manual
#[derive(Debug)]
#[repr(C)]
pub struct VtempCal30(u16);
define_ptr_type!(VtempCal30, 0x1FFF_75A8);
pub struct VtempCalLow(u16);
define_ptr_type!(VtempCalLow, 0x1FFF_75A8);

impl VtempCal30 {
impl VtempCalLow {
/// aka TS_CAL1_TEMP in reference manual
pub const TEMP_DEGREES: u16 = 30;
/// Read calibration value
Expand All @@ -112,13 +112,29 @@ impl VtempCal30 {
/// aka TS_CAL2 in reference manual
#[derive(Debug)]
#[repr(C)]
pub struct VtempCal130(u16);
define_ptr_type!(VtempCal130, 0x1FFF_75CA);
pub struct VtempCalHigh(u16);
define_ptr_type!(VtempCalHigh, 0x1FFF_75CA);

impl VtempCal130 {
impl VtempCalHigh {
/// aka TS_CAL2_TEMP in reference manual
/// Feature gate Required: this is 110 for L47x/L48x, 130 for other L4s according to
/// https://github.com/STMicroelectronics/STM32CubeL4/blob/5e1553e07706491bd11f4edd304e093b6e4b83a4/Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_ll_adc.h#L352-L356

// L47/L48
#[cfg(any(
feature = "stm32l471",
feature = "stm32l475",
feature = "stm32l476",
feature = "stm32l486"
))]
pub const TEMP_DEGREES: u16 = 110;
// else
#[cfg(not(any(
feature = "stm32l471",
feature = "stm32l475",
feature = "stm32l476",
feature = "stm32l486"
)))]
pub const TEMP_DEGREES: u16 = 130;
/// Read calibration value
pub fn read(&self) -> u16 {
Expand Down