@@ -49,11 +49,22 @@ use embedded_hal::{
49
49
blocking:: delay:: DelayUs ,
50
50
} ;
51
51
52
- use crate :: { delay:: Delay , gpio:: * , rcc:: Rcc , stm32} ;
52
+ use crate :: {
53
+ delay:: Delay ,
54
+ gpio:: * ,
55
+ rcc:: Rcc ,
56
+ stm32:: {
57
+ adc:: {
58
+ cfgr1:: { ALIGNW , RESW } ,
59
+ smpr:: SMPW ,
60
+ } ,
61
+ ADC ,
62
+ } ,
63
+ } ;
53
64
54
65
/// Analog to Digital converter interface
55
66
pub struct Adc {
56
- rb : stm32 :: ADC ,
67
+ rb : ADC ,
57
68
sample_time : AdcSampleTime ,
58
69
align : AdcAlign ,
59
70
precision : AdcPrecision ,
@@ -83,27 +94,27 @@ pub enum AdcSampleTime {
83
94
}
84
95
85
96
impl AdcSampleTime {
86
- fn write_bits ( self , adc : & mut stm32:: ADC ) {
87
- adc. smpr . write ( |w| {
88
- w. smp ( ) . bits ( match self {
89
- AdcSampleTime :: T_1 => 0b000_u8 ,
90
- AdcSampleTime :: T_7 => 0b001_u8 ,
91
- AdcSampleTime :: T_13 => 0b010_u8 ,
92
- AdcSampleTime :: T_28 => 0b011_u8 ,
93
- AdcSampleTime :: T_41 => 0b100_u8 ,
94
- AdcSampleTime :: T_55 => 0b101_u8 ,
95
- AdcSampleTime :: T_71 => 0b110_u8 ,
96
- AdcSampleTime :: T_239 => 0b111_u8 ,
97
- } )
98
- } ) ;
99
- }
100
-
101
97
/// Get the default sample time (currently 239.5 cycles)
102
98
pub fn default ( ) -> Self {
103
99
AdcSampleTime :: T_239
104
100
}
105
101
}
106
102
103
+ impl From < AdcSampleTime > for SMPW {
104
+ fn from ( val : AdcSampleTime ) -> Self {
105
+ match val {
106
+ AdcSampleTime :: T_1 => SMPW :: CYCLES1_5 ,
107
+ AdcSampleTime :: T_7 => SMPW :: CYCLES7_5 ,
108
+ AdcSampleTime :: T_13 => SMPW :: CYCLES13_5 ,
109
+ AdcSampleTime :: T_28 => SMPW :: CYCLES28_5 ,
110
+ AdcSampleTime :: T_41 => SMPW :: CYCLES41_5 ,
111
+ AdcSampleTime :: T_55 => SMPW :: CYCLES55_5 ,
112
+ AdcSampleTime :: T_71 => SMPW :: CYCLES71_5 ,
113
+ AdcSampleTime :: T_239 => SMPW :: CYCLES239_5 ,
114
+ }
115
+ }
116
+ }
117
+
107
118
#[ derive( Clone , Copy , Debug , PartialEq ) ]
108
119
/// ADC Result Alignment
109
120
pub enum AdcAlign {
@@ -128,22 +139,22 @@ pub enum AdcAlign {
128
139
}
129
140
130
141
impl AdcAlign {
131
- fn write_bits ( self , adc : & mut stm32:: ADC ) {
132
- adc. cfgr1 . write ( |w| {
133
- w. align ( ) . bit ( match self {
134
- AdcAlign :: Left => true ,
135
- AdcAlign :: Right => false ,
136
- AdcAlign :: LeftAsRM => true ,
137
- } )
138
- } ) ;
139
- }
140
-
141
142
/// Get the default alignment (currently right aligned)
142
143
pub fn default ( ) -> Self {
143
144
AdcAlign :: Right
144
145
}
145
146
}
146
147
148
+ impl From < AdcAlign > for ALIGNW {
149
+ fn from ( val : AdcAlign ) -> Self {
150
+ match val {
151
+ AdcAlign :: Left => ALIGNW :: LEFT ,
152
+ AdcAlign :: Right => ALIGNW :: RIGHT ,
153
+ AdcAlign :: LeftAsRM => ALIGNW :: LEFT ,
154
+ }
155
+ }
156
+ }
157
+
147
158
#[ derive( Clone , Copy , Debug , PartialEq ) ]
148
159
/// ADC Sampling Precision
149
160
pub enum AdcPrecision {
@@ -158,23 +169,23 @@ pub enum AdcPrecision {
158
169
}
159
170
160
171
impl AdcPrecision {
161
- fn write_bits ( self , adc : & mut stm32:: ADC ) {
162
- adc. cfgr1 . write ( |w| {
163
- w. res ( ) . bits ( match self {
164
- AdcPrecision :: B_12 => 0b00_u8 ,
165
- AdcPrecision :: B_10 => 0b01_u8 ,
166
- AdcPrecision :: B_8 => 0b10_u8 ,
167
- AdcPrecision :: B_6 => 0b11_u8 ,
168
- } )
169
- } ) ;
170
- }
171
-
172
172
/// Get the default precision (currently 12 bit precision)
173
173
pub fn default ( ) -> Self {
174
174
AdcPrecision :: B_12
175
175
}
176
176
}
177
177
178
+ impl From < AdcPrecision > for RESW {
179
+ fn from ( val : AdcPrecision ) -> Self {
180
+ match val {
181
+ AdcPrecision :: B_12 => RESW :: TWELVEBIT ,
182
+ AdcPrecision :: B_10 => RESW :: TENBIT ,
183
+ AdcPrecision :: B_8 => RESW :: EIGHTBIT ,
184
+ AdcPrecision :: B_6 => RESW :: SIXBIT ,
185
+ }
186
+ }
187
+ }
188
+
178
189
macro_rules! adc_pins {
179
190
( $( $pin: ty => $chan: expr) ,+ $( , ) * ) => {
180
191
$(
@@ -449,7 +460,7 @@ impl Adc {
449
460
/// Sets all configurable parameters to defaults, enables the HSI14 clock
450
461
/// for the ADC if it is not already enabled and performs a boot time
451
462
/// calibration. As such this method may take an appreciable time to run.
452
- pub fn new ( adc : stm32 :: ADC , rcc : & mut Rcc ) -> Self {
463
+ pub fn new ( adc : ADC , rcc : & mut Rcc ) -> Self {
453
464
let mut s = Self {
454
465
rb : adc,
455
466
sample_time : AdcSampleTime :: default ( ) ,
@@ -532,52 +543,58 @@ impl Adc {
532
543
533
544
fn calibrate ( & mut self ) {
534
545
/* Ensure that ADEN = 0 */
535
- if self . rb . cr . read ( ) . aden ( ) . bit_is_set ( ) {
546
+ if self . rb . cr . read ( ) . aden ( ) . is_enabled ( ) {
536
547
/* Clear ADEN by setting ADDIS */
537
- self . rb . cr . modify ( |_, w| w. addis ( ) . set_bit ( ) ) ;
548
+ self . rb . cr . modify ( |_, w| w. addis ( ) . disable ( ) ) ;
538
549
}
539
- while self . rb . cr . read ( ) . aden ( ) . bit_is_set ( ) { }
550
+ while self . rb . cr . read ( ) . aden ( ) . is_enabled ( ) { }
540
551
541
552
/* Clear DMAEN */
542
- self . rb . cfgr1 . modify ( |_, w| w. dmaen ( ) . clear_bit ( ) ) ;
553
+ self . rb . cfgr1 . modify ( |_, w| w. dmaen ( ) . disabled ( ) ) ;
543
554
544
555
/* Start calibration by setting ADCAL */
545
- self . rb . cr . modify ( |_, w| w. adcal ( ) . set_bit ( ) ) ;
556
+ self . rb . cr . modify ( |_, w| w. adcal ( ) . start_calibration ( ) ) ;
546
557
547
558
/* Wait until calibration is finished and ADCAL = 0 */
548
- while self . rb . cr . read ( ) . adcal ( ) . bit_is_set ( ) { }
559
+ while self . rb . cr . read ( ) . adcal ( ) . is_calibrating ( ) { }
549
560
}
550
561
551
562
fn select_clock ( & mut self , rcc : & mut Rcc ) {
552
- rcc. regs . apb2enr . modify ( |_, w| w. adcen ( ) . set_bit ( ) ) ;
553
- rcc. regs . cr2 . write ( |w| w. hsi14on ( ) . set_bit ( ) ) ;
554
- while rcc. regs . cr2 . read ( ) . hsi14rdy ( ) . bit_is_clear ( ) { }
563
+ rcc. regs . apb2enr . modify ( |_, w| w. adcen ( ) . enabled ( ) ) ;
564
+ rcc. regs . cr2 . write ( |w| w. hsi14on ( ) . on ( ) ) ;
565
+ while rcc. regs . cr2 . read ( ) . hsi14rdy ( ) . is_not_ready ( ) { }
555
566
}
556
567
557
568
fn power_up ( & mut self ) {
558
- if self . rb . isr . read ( ) . adrdy ( ) . bit_is_set ( ) {
559
- self . rb . isr . modify ( |_, w| w. adrdy ( ) . clear_bit ( ) ) ;
569
+ if self . rb . isr . read ( ) . adrdy ( ) . is_ready ( ) {
570
+ self . rb . isr . modify ( |_, w| w. adrdy ( ) . clear ( ) ) ;
560
571
}
561
- self . rb . cr . modify ( |_, w| w. aden ( ) . set_bit ( ) ) ;
562
- while self . rb . isr . read ( ) . adrdy ( ) . bit_is_clear ( ) { }
572
+ self . rb . cr . modify ( |_, w| w. aden ( ) . enabled ( ) ) ;
573
+ while self . rb . isr . read ( ) . adrdy ( ) . is_not_ready ( ) { }
563
574
}
564
575
565
576
fn power_down ( & mut self ) {
566
- self . rb . cr . modify ( |_, w| w. adstp ( ) . set_bit ( ) ) ;
567
- while self . rb . cr . read ( ) . adstp ( ) . bit_is_set ( ) { }
568
- self . rb . cr . modify ( |_, w| w. addis ( ) . set_bit ( ) ) ;
569
- while self . rb . cr . read ( ) . aden ( ) . bit_is_set ( ) { }
577
+ self . rb . cr . modify ( |_, w| w. adstp ( ) . stop_conversion ( ) ) ;
578
+ while self . rb . cr . read ( ) . adstp ( ) . is_stopping ( ) { }
579
+ self . rb . cr . modify ( |_, w| w. addis ( ) . disable ( ) ) ;
580
+ while self . rb . cr . read ( ) . aden ( ) . is_enabled ( ) { }
570
581
}
571
582
572
583
fn convert ( & mut self , chan : u8 ) -> u16 {
573
584
self . rb . chselr . write ( |w| unsafe { w. bits ( 1_u32 << chan) } ) ;
574
585
575
- self . sample_time . write_bits ( & mut self . rb ) ;
576
- self . align . write_bits ( & mut self . rb ) ;
577
- self . precision . write_bits ( & mut self . rb ) ;
586
+ self . rb
587
+ . smpr
588
+ . write ( |w| w. smp ( ) . variant ( self . sample_time . into ( ) ) ) ;
589
+ self . rb . cfgr1 . modify ( |_, w| {
590
+ w. res ( )
591
+ . variant ( self . precision . into ( ) )
592
+ . align ( )
593
+ . variant ( self . align . into ( ) )
594
+ } ) ;
578
595
579
- self . rb . cr . modify ( |_, w| w. adstart ( ) . set_bit ( ) ) ;
580
- while self . rb . isr . read ( ) . eoc ( ) . bit_is_clear ( ) { }
596
+ self . rb . cr . modify ( |_, w| w. adstart ( ) . start_conversion ( ) ) ;
597
+ while self . rb . isr . read ( ) . eoc ( ) . is_not_complete ( ) { }
581
598
582
599
let res = self . rb . dr . read ( ) . bits ( ) as u16 ;
583
600
if self . align == AdcAlign :: Left && self . precision == AdcPrecision :: B_6 {
0 commit comments