7
7
//!
8
8
//! - [Reading a voltage using ADC1](https://github.com/stm32-rs/stm32h5xx-hal/blob/master/examples/adc.rs)
9
9
//! - [Reading a temperature using ADC2](https://github.com/stm32-rs/stm32h5xx-hal/blob/master/examples/temperature.rs)
10
-
10
+ //!
11
+ //! Originally from https://github.com/stm32-rs/stm32h7xx-hal
11
12
mod h5;
12
13
13
14
use core:: convert:: Infallible ;
@@ -37,6 +38,9 @@ impl crate::Sealed for ADC2 {}
37
38
impl Instance for ADC1 { }
38
39
impl Instance for ADC2 { }
39
40
41
+ #[ cfg( feature = "defmt" ) ]
42
+ use defmt:: { assert, panic} ;
43
+
40
44
#[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
41
45
#[ derive( Copy , Clone , PartialEq , Debug ) ]
42
46
pub enum Resolution {
@@ -74,7 +78,6 @@ pub struct Adc<ADC, ED> {
74
78
rb : ADC ,
75
79
sample_time : AdcSampleTime ,
76
80
resolution : Resolution ,
77
- lshift : AdcLshift ,
78
81
clock : Hertz ,
79
82
current_channel : Option < u8 > ,
80
83
_enabled : PhantomData < ED > ,
@@ -150,27 +153,6 @@ impl From<AdcSampleTime> for u8 {
150
153
}
151
154
}
152
155
153
- /// ADC LSHIFT\[3:0\] of the converted value
154
- ///
155
- /// Only values in range of 0..=15 are allowed.
156
- #[ derive( Clone , Copy , Debug , PartialEq , Eq , Default ) ]
157
- #[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
158
- pub struct AdcLshift ( u8 ) ;
159
-
160
- impl AdcLshift {
161
- pub fn new ( lshift : u8 ) -> Self {
162
- if lshift > 15 {
163
- panic ! ( "LSHIFT[3:0] must be in range of 0..=15" ) ;
164
- }
165
-
166
- AdcLshift ( lshift)
167
- }
168
-
169
- pub fn value ( self ) -> u8 {
170
- self . 0
171
- }
172
- }
173
-
174
156
#[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
175
157
#[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
176
158
pub struct AdcCalOffset ( u8 ) ;
@@ -219,18 +201,13 @@ pub trait AdcExt<ADC>: Sized {
219
201
220
202
/// Stored ADC config can be restored using the `Adc::restore_cfg` method
221
203
#[ derive( Copy , Clone , Debug , PartialEq ) ]
222
- pub struct StoredConfig ( AdcSampleTime , Resolution , AdcLshift ) ;
204
+ pub struct StoredConfig ( AdcSampleTime , Resolution ) ;
223
205
224
206
#[ cfg( feature = "defmt" ) ]
225
207
impl defmt:: Format for StoredConfig {
226
208
fn format ( & self , fmt : defmt:: Formatter ) {
227
- defmt:: write!(
228
- fmt,
229
- "StoredConfig({:?}, {:?}, {:?})" ,
230
- self . 0 ,
231
- defmt:: Debug2Format ( & self . 1 ) ,
232
- self . 2
233
- )
209
+ let StoredConfig ( sample_time, res) = & self ;
210
+ defmt:: write!( fmt, "StoredConfig({:?}, {:?})" , sample_time, res)
234
211
}
235
212
}
236
213
@@ -341,7 +318,7 @@ impl<ADC: Instance> AdcExt<ADC> for ADC {
341
318
clocks : & CoreClocks ,
342
319
pwrcfg : & pwr:: PowerConfiguration ,
343
320
) -> Adc < ADC , Disabled > {
344
- Adc :: < ADC , Disabled > :: adc ( self , f_adc, delay, prec, clocks, pwrcfg)
321
+ Adc :: < ADC , Disabled > :: new ( self , f_adc, delay, prec, clocks, pwrcfg)
345
322
}
346
323
}
347
324
@@ -350,7 +327,7 @@ impl<ADC: Instance> Adc<ADC, Disabled> {
350
327
///
351
328
/// Sets all configurable parameters to one-shot defaults,
352
329
/// performs a boot-time calibration.
353
- pub fn adc (
330
+ pub fn new (
354
331
adc : ADC ,
355
332
f_adc : impl Into < Hertz > ,
356
333
delay : & mut impl DelayNs ,
@@ -385,7 +362,6 @@ impl<ADC: Instance> Adc<ADC, Disabled> {
385
362
rb,
386
363
sample_time : AdcSampleTime :: default ( ) ,
387
364
resolution : Resolution :: TwelveBit ,
388
- lshift : AdcLshift :: default ( ) ,
389
365
clock : Hertz :: from_raw ( 0 ) ,
390
366
current_channel : None ,
391
367
_enabled : PhantomData ,
@@ -558,7 +534,6 @@ impl<ADC: Instance> Adc<ADC, Disabled> {
558
534
rb : self . rb ,
559
535
sample_time : self . sample_time ,
560
536
resolution : self . resolution ,
561
- lshift : self . lshift ,
562
537
clock : self . clock ,
563
538
current_channel : None ,
564
539
_enabled : PhantomData ,
@@ -614,14 +589,16 @@ impl<ADC: Instance> Adc<ADC, Enabled> {
614
589
let chan = PIN :: channel ( ) ;
615
590
assert ! ( chan <= 19 ) ;
616
591
592
+ // TODO: Move to ADC init?
617
593
// Set resolution
618
594
self . rb
619
595
. cfgr ( )
620
596
. modify ( |_, w| unsafe { w. res ( ) . bits ( self . get_resolution ( ) as _ ) } ) ;
621
- // Set discontinuous mode
597
+
598
+ // TODO: Move to ADC init?
622
599
self . rb
623
600
. cfgr ( )
624
- . modify ( |_, w| w. cont ( ) . clear_bit ( ) . discen ( ) . set_bit ( ) ) ;
601
+ . modify ( |_, w| w. cont ( ) . single ( ) . discen ( ) . disabled ( ) ) ;
625
602
626
603
self . start_conversion_common ( chan) ;
627
604
}
@@ -643,10 +620,20 @@ impl<ADC: Instance> Adc<ADC, Enabled> {
643
620
self . current_channel = None ;
644
621
645
622
// Retrieve result
646
- let result = self . rb . dr ( ) . read ( ) . rdata ( ) . bits ( ) ;
623
+ let result = self . current_sample ( ) ;
647
624
Ok ( result)
648
625
}
649
626
627
+ /// Read the current value in the data register
628
+ ///
629
+ /// This simply returns whatever value is in the data register without blocking.
630
+ /// Use [Self::read_sample] if you want to wait for any ongoing conversion to finish.
631
+ ///
632
+ /// NOTE: Depending on OVRMOD the data register acts like a FIFO queue with three stages
633
+ pub fn current_sample ( & mut self ) -> u16 {
634
+ self . rb . dr ( ) . read ( ) . rdata ( ) . bits ( )
635
+ }
636
+
650
637
fn check_conversion_conditions ( & self ) {
651
638
let cr = self . rb . cr ( ) . read ( ) ;
652
639
// Ensure that no conversions are ongoing
@@ -683,7 +670,6 @@ impl<ADC: Instance> Adc<ADC, Enabled> {
683
670
rb : self . rb ,
684
671
sample_time : self . sample_time ,
685
672
resolution : self . resolution ,
686
- lshift : self . lshift ,
687
673
clock : self . clock ,
688
674
current_channel : None ,
689
675
_enabled : PhantomData ,
@@ -694,26 +680,20 @@ impl<ADC: Instance> Adc<ADC, Enabled> {
694
680
impl < ADC : Instance , ED > Adc < ADC , ED > {
695
681
/// Save current ADC config
696
682
pub fn save_cfg ( & mut self ) -> StoredConfig {
697
- StoredConfig (
698
- self . get_sample_time ( ) ,
699
- self . get_resolution ( ) ,
700
- self . get_lshift ( ) ,
701
- )
683
+ StoredConfig ( self . get_sample_time ( ) , self . get_resolution ( ) )
702
684
}
703
685
704
686
/// Restore saved ADC config
705
687
pub fn restore_cfg ( & mut self , cfg : StoredConfig ) {
706
688
self . set_sample_time ( cfg. 0 ) ;
707
689
self . set_resolution ( cfg. 1 ) ;
708
- self . set_lshift ( cfg. 2 ) ;
709
690
}
710
691
711
692
/// Reset the ADC config to default, return existing config
712
693
pub fn default_cfg ( & mut self ) -> StoredConfig {
713
694
let cfg = self . save_cfg ( ) ;
714
695
self . set_sample_time ( AdcSampleTime :: default ( ) ) ;
715
696
self . set_resolution ( Resolution :: TwelveBit ) ;
716
- self . set_lshift ( AdcLshift :: default ( ) ) ;
717
697
cfg
718
698
}
719
699
@@ -744,7 +724,7 @@ impl<ADC: Instance, ED> Adc<ADC, ED> {
744
724
Resolution :: TwelveBit => 12 ,
745
725
} ;
746
726
747
- let cycles = ( sample_cycles_x2 + 1 ) / 2 + sar_cycles;
727
+ let cycles = sample_cycles_x2. div_ceil ( 2 ) + sar_cycles;
748
728
Hertz :: Hz ( self . clock . to_Hz ( ) / cycles)
749
729
}
750
730
@@ -758,11 +738,6 @@ impl<ADC: Instance, ED> Adc<ADC, ED> {
758
738
self . resolution
759
739
}
760
740
761
- /// Get ADC lshift value
762
- pub fn get_lshift ( & self ) -> AdcLshift {
763
- self . lshift
764
- }
765
-
766
741
/// Set ADC sampling time
767
742
///
768
743
/// Options can be found in [AdcSampleTime].
@@ -775,22 +750,14 @@ impl<ADC: Instance, ED> Adc<ADC, ED> {
775
750
self . resolution = res;
776
751
}
777
752
778
- /// Set ADC lshift
779
- ///
780
- /// LSHIFT\[3:0\] must be in range of 0..=15
781
- pub fn set_lshift ( & mut self , lshift : AdcLshift ) {
782
- self . lshift = lshift;
783
- }
784
-
785
753
/// Returns the largest possible sample value for the current ADC configuration
786
754
///
787
755
/// Using this value as the denominator when calculating
788
756
/// transfer functions results in a gain error, and thus should
789
757
/// be avoided. Use the [slope](#method.slope) method instead.
790
758
#[ deprecated( since = "0.12.0" , note = "See the slope() method instead" ) ]
791
759
pub fn max_sample ( & self ) -> u32 {
792
- ( ( 1 << self . get_resolution ( ) . number_of_bits ( ) as u32 ) - 1 )
793
- << self . get_lshift ( ) . value ( ) as u32
760
+ ( 1 << self . get_resolution ( ) . number_of_bits ( ) ) - 1
794
761
}
795
762
796
763
/// Returns the slope for the current ADC configuration. 1 LSB = Vref / slope
@@ -804,8 +771,7 @@ impl<ADC: Instance, ED> Adc<ADC, ED> {
804
771
/// let v = adc.read(&ch).unwrap() as f32 * vref / adc.slope() as f32;
805
772
/// ```
806
773
pub fn slope ( & self ) -> u32 {
807
- 1 << ( self . get_resolution ( ) . number_of_bits ( ) as u32
808
- + self . get_lshift ( ) . value ( ) as u32 )
774
+ 1 << self . get_resolution ( ) . number_of_bits ( )
809
775
}
810
776
811
777
/// Returns the offset calibration value for single ended channel
@@ -831,8 +797,10 @@ where
831
797
{
832
798
type Error = Infallible ;
833
799
800
+ // TODO: We are not really non-blocking
834
801
fn read ( & mut self , pin : & mut PIN ) -> nb:: Result < u16 , Infallible > {
835
802
self . start_conversion ( pin) ;
836
- self . read_sample ( )
803
+ let res = nb:: block!( self . read_sample( ) ) . unwrap ( ) ;
804
+ Ok ( res. into ( ) )
837
805
}
838
806
}
0 commit comments