@@ -46,6 +46,19 @@ pub enum SampleTime {
46
46
T_160 = 0b111 ,
47
47
}
48
48
49
+ // ADC Oversampling ratio
50
+ #[ derive( Copy , Clone , PartialEq ) ]
51
+ pub enum OversamplingRatio {
52
+ X_2 = 0b000 ,
53
+ X_4 = 0b001 ,
54
+ X_8 = 0b010 ,
55
+ X_16 = 0b011 ,
56
+ X_32 = 0b100 ,
57
+ X_64 = 0b101 ,
58
+ X_128 = 0b110 ,
59
+ X_256 = 0b111 ,
60
+ }
61
+
49
62
#[ derive( Clone , Copy , PartialEq , Eq , Debug ) ]
50
63
pub enum ClockSource {
51
64
Pclk ( PclkDiv ) ,
@@ -111,6 +124,7 @@ impl Adc {
111
124
}
112
125
}
113
126
127
+ /// Sets ADC source
114
128
pub fn set_clock_source ( & mut self , clock_source : ClockSource ) {
115
129
match clock_source {
116
130
ClockSource :: Pclk ( div) => self
@@ -176,22 +190,6 @@ impl Adc {
176
190
self . precision = precision;
177
191
}
178
192
179
- fn power_up ( & mut self ) {
180
- self . rb . isr . modify ( |_, w| w. adrdy ( ) . set_bit ( ) ) ;
181
- self . rb . cr . modify ( |_, w| w. aden ( ) . set_bit ( ) ) ;
182
- while self . rb . isr . read ( ) . adrdy ( ) . bit_is_clear ( ) { }
183
- }
184
-
185
- fn power_down ( & mut self ) {
186
- self . rb . cr . modify ( |_, w| w. addis ( ) . set_bit ( ) ) ;
187
- self . rb . isr . modify ( |_, w| w. adrdy ( ) . set_bit ( ) ) ;
188
- while self . rb . cr . read ( ) . aden ( ) . bit_is_set ( ) { }
189
- }
190
-
191
- pub fn release ( self ) -> ADC {
192
- self . rb
193
- }
194
-
195
193
/// The nuber of bits, the oversampling result is shifted in bits at the end of oversampling
196
194
pub fn set_oversamling_shift ( & mut self , nrbits : u8 ) {
197
195
self . rb
@@ -200,23 +198,14 @@ impl Adc {
200
198
}
201
199
202
200
/// Oversampling of adc according to datasheet of stm32g0, when oversampling is enabled
203
- /// 000: 2x
204
- /// 001: 4x
205
- /// 010: 8x
206
- /// 011: 16x
207
- /// 100: 32x
208
- /// 101: 64x
209
- /// 110: 128x
210
- /// 111: 256x
211
-
212
- pub fn set_oversamling_ratio ( & mut self , multyply : u8 ) {
201
+ pub fn set_oversamling_ratio ( & mut self , ratio : OversamplingRatio ) {
213
202
self . rb
214
203
. cfgr2
215
- . modify ( |_, w| unsafe { w. ovsr ( ) . bits ( multyply ) } ) ;
204
+ . modify ( |_, w| unsafe { w. ovsr ( ) . bits ( ratio as u8 ) } ) ;
216
205
}
217
206
218
- pub fn oversamling_enable ( & mut self ) {
219
- self . rb . cfgr2 . modify ( |_, w| w. ovse ( ) . set_bit ( ) ) ;
207
+ pub fn oversamling_enable ( & mut self , enable : bool ) {
208
+ self . rb . cfgr2 . modify ( |_, w| w. ovse ( ) . bit ( enable ) ) ;
220
209
}
221
210
222
211
pub fn start_injected ( & mut self ) {
@@ -233,6 +222,22 @@ impl Adc {
233
222
// maybe self.rb.cr.adstp().set_bit() must be performed before interrupt is disabled + wait abortion
234
223
self . rb . ier . modify ( |_, w| w. eocie ( ) . clear_bit ( ) ) ; // end of sequence interupt disable
235
224
}
225
+
226
+ pub fn release ( self ) -> ADC {
227
+ self . rb
228
+ }
229
+
230
+ fn power_up ( & mut self ) {
231
+ self . rb . isr . modify ( |_, w| w. adrdy ( ) . set_bit ( ) ) ;
232
+ self . rb . cr . modify ( |_, w| w. aden ( ) . set_bit ( ) ) ;
233
+ while self . rb . isr . read ( ) . adrdy ( ) . bit_is_clear ( ) { }
234
+ }
235
+
236
+ fn power_down ( & mut self ) {
237
+ self . rb . cr . modify ( |_, w| w. addis ( ) . set_bit ( ) ) ;
238
+ self . rb . isr . modify ( |_, w| w. adrdy ( ) . set_bit ( ) ) ;
239
+ while self . rb . cr . read ( ) . aden ( ) . bit_is_set ( ) { }
240
+ }
236
241
}
237
242
238
243
pub trait AdcExt {
0 commit comments