@@ -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,29 @@ 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 = if matches ! ( chan, TEMP_CHANNEL | VREF_CHANNEL ) {
504
+ self . enable_temp_vref ( )
505
+ } else {
506
+ false
507
+ } ;
508
+
509
+ let val = self . convert ( chan) ;
510
+
511
+ if tsv_enabled {
512
+ self . disable_temp_vref ( ) ;
513
+ }
514
+
515
+ val
516
+ }
517
+
518
+ /// Enables the temperature / VREF sensor.
519
+ ///
520
+ /// Enabling this before calling `read_temp` or `read_vref` will speed up the reading
521
+ /// since you won't have to wait for the sensor to start up.
522
+ ///
523
+ /// Returns true if the sensor was previously off.
524
+ pub fn enable_temp_vref ( & mut self ) -> bool {
525
+ if !self . is_temp_vref_enabled ( ) {
501
526
self . rb . cr2 ( ) . modify ( |_, w| w. tsvrefe ( ) . set_bit ( ) ) ;
502
527
503
528
// The reference manual says that a stabilization time is needed after the powering the
@@ -508,15 +533,19 @@ impl Adc<pac::ADC1> {
508
533
true
509
534
} else {
510
535
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
536
}
537
+ }
518
538
519
- val
539
+ /// Disables the temperature / VREF sensor.
540
+ ///
541
+ /// `read_temp` and `read_vref` will still work with this disabled, but will take a
542
+ /// bit longer since you have to wait for the sensor to start up.
543
+ pub fn disable_temp_vref ( & mut self ) {
544
+ self . rb . cr2 ( ) . modify ( |_, w| w. tsvrefe ( ) . clear_bit ( ) ) ;
545
+ }
546
+
547
+ pub fn is_temp_vref_enabled ( & self ) -> bool {
548
+ self . rb . cr2 ( ) . read ( ) . tsvrefe ( ) . bit_is_set ( )
520
549
}
521
550
522
551
/// Temperature sensor is connected to channel 16 on ADC1. This sensor can be used
@@ -531,6 +560,9 @@ impl Adc<pac::ADC1> {
531
560
/// temperature readings are needed, an external temperature sensor part should be used."
532
561
///
533
562
/// Formula to calculate temperature value is also taken from the section 11.10.
563
+ ///
564
+ /// If the temp/VREF sensor is disabled, this will still work but must wait
565
+ /// for the sensor to start up. Call `enable_temp_vref` to speed this up.
534
566
pub fn read_temp ( & mut self ) -> i32 {
535
567
/// According to section 5.3.18 "Temperature sensor characteristics"
536
568
/// from STM32F1xx datasheets, TS constants values are as follows:
@@ -556,8 +588,8 @@ impl Adc<pac::ADC1> {
556
588
} ;
557
589
558
590
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 ( ) ;
591
+ let val_temp: i32 = self . read_aux ( TEMP_CHANNEL ) . into ( ) ;
592
+ let val_vref: i32 = self . read_aux ( VREF_CHANNEL ) . into ( ) ;
561
593
let v_sense = val_temp * 1200 / val_vref;
562
594
563
595
self . restore_cfg ( prev_cfg) ;
@@ -573,8 +605,11 @@ impl Adc<pac::ADC1> {
573
605
/// For instance, reading from any ADC channel can be converted into voltage (mV)
574
606
/// using the following formula:
575
607
/// v_chan = adc.read(chan) * 1200 / adc.read_vref()
608
+ ///
609
+ /// If the temp/VREF sensor is disabled, this will still work but must wait
610
+ /// for the sensor to start up. Call `enable_temp_vref` to speed this up.
576
611
pub fn read_vref ( & mut self ) -> u16 {
577
- self . read_aux ( 17u8 )
612
+ self . read_aux ( VREF_CHANNEL )
578
613
}
579
614
}
580
615
0 commit comments