@@ -18,6 +18,9 @@ use embedded_dma::WriteBuffer;
18
18
use crate :: pac:: { self , RCC } ;
19
19
use crate :: pacext:: adc:: { AdcRB , Cr1W , Cr2R , Cr2W , Dr , ExtSelW } ;
20
20
21
+ const TEMP_CHANNEL : u8 = 16 ;
22
+ const VREF_CHANNEL : u8 = 17 ;
23
+
21
24
/// Continuous mode
22
25
pub struct Continuous ;
23
26
/// Scan mode
@@ -497,7 +500,25 @@ where
497
500
498
501
impl Adc < pac:: ADC1 > {
499
502
fn read_aux ( & mut self , chan : u8 ) -> u16 {
500
- let tsv_off = if self . rb . cr2 ( ) . read ( ) . tsvrefe ( ) . bit_is_clear ( ) {
503
+ let tsv_enabled = matches ! ( chan, TEMP_CHANNEL | VREF_CHANNEL ) && self . enable_temp_vref ( ) ;
504
+
505
+ let val = self . convert ( chan) ;
506
+
507
+ if tsv_enabled {
508
+ self . disable_temp_vref ( ) ;
509
+ }
510
+
511
+ val
512
+ }
513
+
514
+ /// Enables the temperature / VREF sensor.
515
+ ///
516
+ /// Enabling this before calling `read_temp` or `read_vref` will speed up the reading
517
+ /// since you won't have to wait for the sensor to start up.
518
+ ///
519
+ /// Returns true if the sensor was previously off.
520
+ pub fn enable_temp_vref ( & mut self ) -> bool {
521
+ if !self . is_temp_vref_enabled ( ) {
501
522
self . rb . cr2 ( ) . modify ( |_, w| w. tsvrefe ( ) . set_bit ( ) ) ;
502
523
503
524
// The reference manual says that a stabilization time is needed after the powering the
@@ -508,15 +529,19 @@ impl Adc<pac::ADC1> {
508
529
true
509
530
} else {
510
531
false
511
- } ;
512
-
513
- let val = self . convert ( chan) ;
514
-
515
- if tsv_off {
516
- self . rb . cr2 ( ) . modify ( |_, w| w. tsvrefe ( ) . clear_bit ( ) ) ;
517
532
}
533
+ }
518
534
519
- val
535
+ /// Disables the temperature / VREF sensor.
536
+ ///
537
+ /// `read_temp` and `read_vref` will still work with this disabled, but will take a
538
+ /// bit longer since you have to wait for the sensor to start up.
539
+ pub fn disable_temp_vref ( & mut self ) {
540
+ self . rb . cr2 ( ) . modify ( |_, w| w. tsvrefe ( ) . clear_bit ( ) ) ;
541
+ }
542
+
543
+ pub fn is_temp_vref_enabled ( & self ) -> bool {
544
+ self . rb . cr2 ( ) . read ( ) . tsvrefe ( ) . bit_is_set ( )
520
545
}
521
546
522
547
/// Temperature sensor is connected to channel 16 on ADC1. This sensor can be used
@@ -531,6 +556,9 @@ impl Adc<pac::ADC1> {
531
556
/// temperature readings are needed, an external temperature sensor part should be used."
532
557
///
533
558
/// Formula to calculate temperature value is also taken from the section 11.10.
559
+ ///
560
+ /// If the temp/VREF sensor is disabled, this will still work but must wait
561
+ /// for the sensor to start up. Call `enable_temp_vref` to speed this up.
534
562
pub fn read_temp ( & mut self ) -> i32 {
535
563
/// According to section 5.3.18 "Temperature sensor characteristics"
536
564
/// from STM32F1xx datasheets, TS constants values are as follows:
@@ -556,8 +584,8 @@ impl Adc<pac::ADC1> {
556
584
} ;
557
585
558
586
self . set_sample_time ( sample_time) ;
559
- let val_temp: i32 = self . read_aux ( 16u8 ) . into ( ) ;
560
- let val_vref: i32 = self . read_aux ( 17u8 ) . into ( ) ;
587
+ let val_temp: i32 = self . read_aux ( TEMP_CHANNEL ) . into ( ) ;
588
+ let val_vref: i32 = self . read_aux ( VREF_CHANNEL ) . into ( ) ;
561
589
let v_sense = val_temp * 1200 / val_vref;
562
590
563
591
self . restore_cfg ( prev_cfg) ;
@@ -573,8 +601,11 @@ impl Adc<pac::ADC1> {
573
601
/// For instance, reading from any ADC channel can be converted into voltage (mV)
574
602
/// using the following formula:
575
603
/// v_chan = adc.read(chan) * 1200 / adc.read_vref()
604
+ ///
605
+ /// If the temp/VREF sensor is disabled, this will still work but must wait
606
+ /// for the sensor to start up. Call `enable_temp_vref` to speed this up.
576
607
pub fn read_vref ( & mut self ) -> u16 {
577
- self . read_aux ( 17u8 )
608
+ self . read_aux ( VREF_CHANNEL )
578
609
}
579
610
}
580
611
0 commit comments