@@ -15,6 +15,7 @@ use crate::{
15
15
/// Analog to Digital converter interface
16
16
pub struct ADC {
17
17
inner : pac:: ADC ,
18
+ resolution : Resolution ,
18
19
}
19
20
20
21
impl ADC {
@@ -65,7 +66,15 @@ impl ADC {
65
66
} ) ;
66
67
while inner. cr . read ( ) . adcal ( ) . bit_is_set ( ) { }
67
68
68
- Self { inner }
69
+ Self {
70
+ inner,
71
+ resolution : Resolution :: default ( ) ,
72
+ }
73
+ }
74
+
75
+ /// Set the ADC resolution
76
+ pub fn set_resolution ( & mut self , resolution : Resolution ) {
77
+ self . resolution = resolution;
69
78
}
70
79
71
80
/// Release the ADC peripheral
89
98
self . inner . cr . modify ( |_, w| w. aden ( ) . set_bit ( ) ) ;
90
99
while self . inner . isr . read ( ) . adrdy ( ) . bit_is_clear ( ) { }
91
100
101
+ // Configure ADC
102
+ self . inner . cfgr . write ( |w| {
103
+ // This is sound, as all `Resolution` values are valid for this
104
+ // field.
105
+ unsafe { w. res ( ) . bits ( self . resolution as u8 ) }
106
+ } ) ;
107
+
92
108
// Select channel
93
109
self . inner . sqr1 . write ( |w| {
94
110
// This is sound, as all `Channel` implementations set valid values.
@@ -114,6 +130,30 @@ where
114
130
}
115
131
}
116
132
133
+ /// ADC resolution setting
134
+ ///
135
+ /// The default setting is 12 bits.
136
+ #[ derive( Clone , Copy , Debug , Eq , PartialEq , Ord , PartialOrd ) ]
137
+ pub enum Resolution {
138
+ /// 12-bit resolution
139
+ Bits12 = 0b00 ,
140
+
141
+ /// 10-bit resolution
142
+ Bits10 = 0b01 ,
143
+
144
+ /// 8-bit resolution
145
+ Bits8 = 0b10 ,
146
+
147
+ /// 6-bit resolution
148
+ Bits6 = 0b11 ,
149
+ }
150
+
151
+ impl Default for Resolution {
152
+ fn default ( ) -> Self {
153
+ Self :: Bits12
154
+ }
155
+ }
156
+
117
157
macro_rules! external_channels {
118
158
(
119
159
$(
0 commit comments