11
11
//! - [Using ADC1 and ADC2 in parallel](https://github.com/stm32-rs/stm32h7xx-hal/blob/master/examples/adc12_parallel.rs)
12
12
//! - [Using ADC1 through DMA](https://github.com/stm32-rs/stm32h7xx-hal/blob/master/examples/adc_dma.rs)
13
13
14
- use crate :: hal:: adc:: { Channel , OneShot } ;
15
- use crate :: hal:: blocking:: delay:: DelayUs ;
14
+ use crate :: hal:: delay:: DelayNs ;
16
15
17
16
use core:: convert:: Infallible ;
18
17
use core:: marker:: PhantomData ;
@@ -186,6 +185,11 @@ impl AdcCalLinear {
186
185
}
187
186
}
188
187
188
+ /// A marker trait to identify MCU pins that can be used as inputs to an ADC channel
189
+ pub trait Channel < ADC > {
190
+ type ID ;
191
+ fn channel ( ) -> Self :: ID ;
192
+ }
189
193
macro_rules! adc_pins {
190
194
( $ADC: ident, $( $input: ty => $chan: expr) ,+ $( , ) * ) => {
191
195
$(
@@ -364,7 +368,7 @@ pub trait AdcExt<ADC>: Sized {
364
368
fn adc (
365
369
self ,
366
370
f_adc : impl Into < Hertz > ,
367
- delay : & mut impl DelayUs < u8 > ,
371
+ delay : & mut impl DelayNs ,
368
372
prec : Self :: Rec ,
369
373
clocks : & CoreClocks ,
370
374
) -> Adc < ADC , Disabled > ;
@@ -421,7 +425,7 @@ pub fn adc12(
421
425
adc1 : ADC1 ,
422
426
adc2 : ADC2 ,
423
427
f_adc : impl Into < Hertz > ,
424
- delay : & mut impl DelayUs < u8 > ,
428
+ delay : & mut impl DelayNs ,
425
429
prec : rec:: Adc12 ,
426
430
clocks : & CoreClocks ,
427
431
) -> ( Adc < ADC1 , Disabled > , Adc < ADC2 , Disabled > ) {
@@ -500,7 +504,7 @@ macro_rules! adc_hal {
500
504
501
505
fn adc( self ,
502
506
f_adc: impl Into <Hertz >,
503
- delay: & mut impl DelayUs < u8 > ,
507
+ delay: & mut impl DelayNs ,
504
508
prec: rec:: $Rec,
505
509
clocks: & CoreClocks ) -> Adc <$ADC, Disabled >
506
510
{
@@ -513,7 +517,7 @@ macro_rules! adc_hal {
513
517
///
514
518
/// Sets all configurable parameters to one-shot defaults,
515
519
/// performs a boot-time calibration.
516
- pub fn $adcX( adc: $ADC, f_adc: impl Into <Hertz >, delay: & mut impl DelayUs < u8 > ,
520
+ pub fn $adcX( adc: $ADC, f_adc: impl Into <Hertz >, delay: & mut impl DelayNs ,
517
521
prec: rec:: $Rec, clocks: & CoreClocks
518
522
) -> Self {
519
523
// Consume ADC register block, produce Self with default
@@ -631,13 +635,14 @@ macro_rules! adc_hal {
631
635
/// Disables Deeppowerdown-mode and enables voltage regulator
632
636
///
633
637
/// Note: After power-up, a [`calibration`](#method.calibrate) shall be run
634
- pub fn power_up( & mut self , delay: & mut impl DelayUs < u8 > ) {
638
+ pub fn power_up( & mut self , delay: & mut impl DelayNs ) {
635
639
// Refer to RM0433 Rev 7 - Chapter 25.4.6
636
640
self . rb. cr. modify( |_, w|
637
641
w. deeppwd( ) . clear_bit( )
638
642
. advregen( ) . set_bit( )
639
643
) ;
640
- delay. delay_us( 10_u8 ) ;
644
+
645
+ delay. delay_us( 10 ) ;
641
646
642
647
// check LDORDY bit if present
643
648
$(
@@ -884,6 +889,17 @@ macro_rules! adc_hal {
884
889
nb:: Result :: Ok ( result)
885
890
}
886
891
892
+ /// Perform an ADC conversion on the specified pin
893
+ pub fn read<PIN , T >( & mut self , pin: & mut PIN ) -> nb:: Result <T , Infallible >
894
+ where
895
+ PIN : Channel <$ADC, ID = u8 >,
896
+ T : From <u32 >
897
+ {
898
+ self . start_conversion( pin) ;
899
+ let res = block!( self . read_sample( ) ) . unwrap( ) ;
900
+ Ok ( res. into( ) )
901
+ }
902
+
887
903
fn check_conversion_conditions( & self ) {
888
904
let cr = self . rb. cr. read( ) ;
889
905
// Ensure that no conversions are ongoing
@@ -1111,20 +1127,6 @@ macro_rules! adc_hal {
1111
1127
& mut self . rb
1112
1128
}
1113
1129
}
1114
-
1115
- impl <WORD , PIN > OneShot <$ADC, WORD , PIN > for Adc <$ADC, Enabled >
1116
- where
1117
- WORD : From <u32 >,
1118
- PIN : Channel <$ADC, ID = u8 >,
1119
- {
1120
- type Error = ( ) ;
1121
-
1122
- fn read( & mut self , pin: & mut PIN ) -> nb:: Result <WORD , Self :: Error > {
1123
- self . start_conversion( pin) ;
1124
- let res = block!( self . read_sample( ) ) . unwrap( ) ;
1125
- Ok ( res. into( ) )
1126
- }
1127
- }
1128
1130
) +
1129
1131
}
1130
1132
}
0 commit comments