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