@@ -199,9 +199,19 @@ impl ADC {
199
199
}
200
200
201
201
/// Enable and get the `Temperature`
202
- pub fn enable_temperature ( & mut self ) -> Temperature {
202
+ pub fn enable_temperature ( & mut self , delay : & mut impl DelayUs < u32 > ) -> Temperature {
203
203
self . common . ccr . modify ( |_, w| w. ch17sel ( ) . set_bit ( ) ) ;
204
204
205
+ // rm0351 section 18.4.32 pg580 (L47/L48/L49/L4A models)
206
+ // Note:
207
+ // The sensor has a startup time after waking from power-down mode before it can output VTS
208
+ // at the correct level. The ADC also has a startup time after power-on, so to minimize the
209
+ // delay, the ADEN and CH17SEL bits should be set at the same time.
210
+ //
211
+ // https://github.com/STMicroelectronics/STM32CubeL4/blob/master/Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_ll_adc.h#L1363
212
+ // 120us is used in the ST HAL code
213
+ delay. delay_us ( 150 ) ;
214
+
205
215
Temperature { }
206
216
}
207
217
@@ -267,9 +277,14 @@ impl ADC {
267
277
}
268
278
269
279
/// Convert a raw sample from the `Temperature` to deg C
270
- pub fn to_degrees_centigrade ( sample : u16 ) -> f32 {
271
- ( 130.0 - 30.0 ) / ( VtempCal130 :: get ( ) . read ( ) as f32 - VtempCal30 :: get ( ) . read ( ) as f32 )
272
- * ( 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
+ // as signed because RM0351 doesn't specify against this being an
284
+ // inverse relation (which would result in a negative differential)
285
+ / ( VtempCal130 :: get ( ) . read ( ) as i32 - VtempCal30 :: get ( ) . read ( ) as i32 ) as f32
286
+ // this can definitely be negative so must be done as a signed value
287
+ * ( sample as i32 - VtempCal30 :: get ( ) . read ( ) as i32 ) as f32
273
288
+ 30.0
274
289
}
275
290
0 commit comments