Skip to content

Commit 38dc9f4

Browse files
committed
Add support for setting ADC resolution
1 parent 8e4b9fb commit 38dc9f4

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/adc.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::{
1515
/// Analog to Digital converter interface
1616
pub struct ADC {
1717
inner: pac::ADC,
18+
resolution: Resolution,
1819
}
1920

2021
impl ADC {
@@ -65,7 +66,15 @@ impl ADC {
6566
});
6667
while inner.cr.read().adcal().bit_is_set() {}
6768

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;
6978
}
7079

7180
/// Release the ADC peripheral
@@ -89,6 +98,13 @@ where
8998
self.inner.cr.modify(|_, w| w.aden().set_bit());
9099
while self.inner.isr.read().adrdy().bit_is_clear() {}
91100

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+
92108
// Select channel
93109
self.inner.sqr1.write(|w| {
94110
// This is sound, as all `Channel` implementations set valid values.
@@ -114,6 +130,30 @@ where
114130
}
115131
}
116132

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+
117157
macro_rules! external_channels {
118158
(
119159
$(

0 commit comments

Comments
 (0)