@@ -16,6 +16,7 @@ use crate::{
16
16
pub struct ADC {
17
17
inner : pac:: ADC ,
18
18
resolution : Resolution ,
19
+ sample_time : SampleTime ,
19
20
}
20
21
21
22
impl ADC {
@@ -69,6 +70,7 @@ impl ADC {
69
70
Self {
70
71
inner,
71
72
resolution : Resolution :: default ( ) ,
73
+ sample_time : SampleTime :: default ( ) ,
72
74
}
73
75
}
74
76
@@ -77,6 +79,11 @@ impl ADC {
77
79
self . resolution = resolution;
78
80
}
79
81
82
+ /// Set the sample time
83
+ pub fn set_sample_time ( & mut self , sample_time : SampleTime ) {
84
+ self . sample_time = sample_time;
85
+ }
86
+
80
87
/// Release the ADC peripheral
81
88
///
82
89
/// Drops `ADC` and returns the `pac::ADC` that is was wrapping, giving the
92
99
{
93
100
type Error = Infallible ;
94
101
95
- fn read ( & mut self , _ : & mut C ) -> nb:: Result < u16 , Self :: Error > {
102
+ fn read ( & mut self , channel : & mut C ) -> nb:: Result < u16 , Self :: Error > {
96
103
// Enable ADC
97
104
self . inner . isr . write ( |w| w. adrdy ( ) . set_bit ( ) ) ;
98
105
self . inner . cr . modify ( |_, w| w. aden ( ) . set_bit ( ) ) ;
@@ -105,6 +112,9 @@ where
105
112
unsafe { w. res ( ) . bits ( self . resolution as u8 ) }
106
113
} ) ;
107
114
115
+ // Configure channel
116
+ channel. set_sample_time ( & self . inner , self . sample_time ) ;
117
+
108
118
// Select channel
109
119
self . inner . sqr1 . write ( |w| {
110
120
// This is sound, as all `Channel` implementations set valid values.
@@ -154,14 +164,54 @@ impl Default for Resolution {
154
164
}
155
165
}
156
166
167
+ /// ADC sample time
168
+ ///
169
+ /// The default setting is 2.5 ADC clock cycles.
170
+ #[ derive( Clone , Copy , Debug , Eq , PartialEq , Ord , PartialOrd ) ]
171
+ pub enum SampleTime {
172
+ /// 2.5 ADC clock cycles
173
+ Cycles2_5 = 0b000 ,
174
+
175
+ /// 6.5 ADC clock cycles
176
+ Cycles6_5 = 0b001 ,
177
+
178
+ /// 12.5 ADC clock cycles
179
+ Cycles12_5 = 0b010 ,
180
+
181
+ /// 24.5 ADC clock cycles
182
+ Cycles24_5 = 0b011 ,
183
+
184
+ /// 47.5 ADC clock cycles
185
+ Cycles47_5 = 0b100 ,
186
+
187
+ /// 92.5 ADC clock cycles
188
+ Cycles92_5 = 0b101 ,
189
+
190
+ /// 247.5 ADC clock cycles
191
+ Cycles247_5 = 0b110 ,
192
+
193
+ /// 640.5 ADC clock cycles
194
+ Cycles640_5 = 0b111 ,
195
+ }
196
+
197
+ impl Default for SampleTime {
198
+ fn default ( ) -> Self {
199
+ Self :: Cycles2_5
200
+ }
201
+ }
202
+
157
203
/// Implemented for all types that represent ADC channels
158
- pub trait Channel : EmbeddedHalChannel < ADC , ID = u8 > { }
204
+ pub trait Channel : EmbeddedHalChannel < ADC , ID = u8 > {
205
+ fn set_sample_time ( & mut self , adc : & pac:: ADC , sample_time : SampleTime ) ;
206
+ }
159
207
160
208
macro_rules! external_channels {
161
209
(
162
210
$(
163
211
$id: expr,
164
- $pin: ident;
212
+ $pin: ident,
213
+ $smpr: ident,
214
+ $smp: ident;
165
215
) *
166
216
) => {
167
217
$(
@@ -173,26 +223,39 @@ macro_rules! external_channels {
173
223
}
174
224
}
175
225
176
- impl Channel for crate :: gpio:: $pin<Analog > { }
226
+ impl Channel for crate :: gpio:: $pin<Analog > {
227
+ fn set_sample_time( & mut self ,
228
+ adc: & pac:: ADC ,
229
+ sample_time: SampleTime ,
230
+ ) {
231
+ adc. $smpr. modify( |_, w| {
232
+ // This is sound, as all `SampleTime` values are valid
233
+ // for this field.
234
+ unsafe {
235
+ w. $smp( ) . bits( sample_time as u8 )
236
+ }
237
+ } )
238
+ }
239
+ }
177
240
) *
178
241
} ;
179
242
}
180
243
181
244
external_channels ! (
182
- 1 , PC0 ;
183
- 2 , PC1 ;
184
- 3 , PC2 ;
185
- 4 , PC3 ;
186
- 5 , PA0 ;
187
- 6 , PA1 ;
188
- 7 , PA2 ;
189
- 8 , PA3 ;
190
- 9 , PA4 ;
191
- 10 , PA5 ;
192
- 11 , PA6 ;
193
- 12 , PA7 ;
194
- 13 , PC4 ;
195
- 14 , PC5 ;
196
- 15 , PB0 ;
197
- 16 , PB1 ;
245
+ 1 , PC0 , smpr1 , smp1 ;
246
+ 2 , PC1 , smpr1 , smp2 ;
247
+ 3 , PC2 , smpr1 , smp3 ;
248
+ 4 , PC3 , smpr1 , smp4 ;
249
+ 5 , PA0 , smpr1 , smp5 ;
250
+ 6 , PA1 , smpr1 , smp6 ;
251
+ 7 , PA2 , smpr1 , smp7 ;
252
+ 8 , PA3 , smpr1 , smp8 ;
253
+ 9 , PA4 , smpr1 , smp9 ;
254
+ 10 , PA5 , smpr2 , smp10 ;
255
+ 11 , PA6 , smpr2 , smp11 ;
256
+ 12 , PA7 , smpr2 , smp12 ;
257
+ 13 , PC4 , smpr2 , smp13 ;
258
+ 14 , PC5 , smpr2 , smp14 ;
259
+ 15 , PB0 , smpr2 , smp15 ;
260
+ 16 , PB1 , smpr2 , smp16 ;
198
261
) ;
0 commit comments