Skip to content

Commit 972f53a

Browse files
committed
Move ADC configuration to dedicated method
I'm working on implementing other means of reading ADC values, so being able to reuse this method will be useful.
1 parent fbf4ffb commit 972f53a

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

src/adc.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -134,22 +134,10 @@ impl<State> Adc<State> {
134134
while self.rb.cr.read().aden().bit_is_set() {}
135135
}
136136

137-
fn write_smpr(&mut self) {
138-
self.rb
139-
.smpr
140-
.modify(|_, w| w.smp().bits(self.sample_time as u8));
141-
}
142-
}
143-
144-
impl<WORD, PIN> OneShot<Adc<Ready>, WORD, PIN> for Adc<Ready>
145-
where
146-
WORD: From<u16>,
147-
PIN: Channel<Adc<Ready>, ID = u8>,
148-
{
149-
type Error = ();
150-
151-
fn read(&mut self, _pin: &mut PIN) -> nb::Result<WORD, Self::Error> {
152-
self.power_up();
137+
fn configure<Chan>(&mut self, _channel: &Chan)
138+
where
139+
Chan: Channel<Adc<Ready>, ID=u8>,
140+
{
153141
self.rb.cfgr1.modify(|_, w| {
154142
// Safe, as `self.precision` is of type `Precision`, which defines
155143
// only valid values.
@@ -160,15 +148,32 @@ where
160148
w.align().bit(self.align == Align::Left)
161149
});
162150

163-
self.write_smpr();
151+
self.rb
152+
.smpr
153+
.modify(|_, w| w.smp().bits(self.sample_time as u8));
164154

165155
self.rb.chselr.write(|w|
166156
// Safe, as long as there are no `Channel` implementations that
167157
// define invalid values.
168-
unsafe { w.bits(0b1 << PIN::channel()) });
158+
unsafe { w.bits(0b1 << Chan::channel()) }
159+
);
169160

170161
self.rb.isr.modify(|_, w| w.eos().set_bit());
171162
self.rb.cr.modify(|_, w| w.adstart().set_bit());
163+
}
164+
}
165+
166+
impl<WORD, PIN> OneShot<Adc<Ready>, WORD, PIN> for Adc<Ready>
167+
where
168+
WORD: From<u16>,
169+
PIN: Channel<Adc<Ready>, ID = u8>,
170+
{
171+
type Error = ();
172+
173+
fn read(&mut self, pin: &mut PIN) -> nb::Result<WORD, Self::Error> {
174+
self.power_up();
175+
self.configure(pin);
176+
172177
while self.rb.isr.read().eos().bit_is_clear() {}
173178

174179
let res = self.rb.dr.read().bits() as u16;

0 commit comments

Comments
 (0)