Skip to content

Commit f359dd3

Browse files
committed
Add support for ADC multi-channel conversions
1 parent 2de82e3 commit f359dd3

File tree

1 file changed

+36
-13
lines changed

1 file changed

+36
-13
lines changed

src/adc.rs

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ impl Adc<Ready> {
156156
///
157157
/// Panics, if `buffer` is larger than 65535.
158158
#[cfg(feature = "stm32l0x2")]
159-
pub fn start<Chan, DmaChan, Buf>(mut self,
160-
channel: Chan,
159+
pub fn start<DmaChan, Buf>(mut self,
160+
channels: impl Into<Channels>,
161161
trigger: Option<Trigger>,
162162
dma: &mut dma::Handle,
163163
dma_chan: DmaChan,
@@ -166,7 +166,6 @@ impl Adc<Ready> {
166166
-> Adc<Active<DmaChan, Buf>>
167167
where
168168
DmaToken: dma::Target<DmaChan>,
169-
Chan: Channel<Self, ID=u8>,
170169
Buf: DerefMut + 'static,
171170
Buf::Target: AsMutSlice<Element=u16>,
172171
DmaChan: dma::Channel,
@@ -214,7 +213,7 @@ impl Adc<Ready> {
214213
let continous = trigger.is_none();
215214

216215
self.power_up();
217-
self.configure(&channel, continous, trigger);
216+
self.configure(channels, continous, trigger);
218217

219218
Adc {
220219
rb: self.rb,
@@ -268,14 +267,11 @@ impl<State> Adc<State> {
268267
while self.rb.cr.read().aden().bit_is_set() {}
269268
}
270269

271-
fn configure<Chan>(&mut self,
272-
_channel: &Chan,
270+
fn configure(&mut self,
271+
channels: impl Into<Channels>,
273272
cont: bool,
274273
trigger: Option<Trigger>,
275-
)
276-
where
277-
Chan: Channel<Adc<Ready>, ID=u8>,
278-
{
274+
) {
279275
self.rb.cfgr1.write(|w| {
280276
w
281277
.res().bits(self.precision as u8)
@@ -303,7 +299,7 @@ impl<State> Adc<State> {
303299
self.rb.chselr.write(|w|
304300
// Safe, as long as there are no `Channel` implementations that
305301
// define invalid values.
306-
unsafe { w.bits(0b1 << Chan::channel()) }
302+
unsafe { w.bits(channels.into().flags) }
307303
);
308304

309305
self.rb.isr.modify(|_, w| w.eos().set_bit());
@@ -318,9 +314,9 @@ where
318314
{
319315
type Error = ();
320316

321-
fn read(&mut self, pin: &mut PIN) -> nb::Result<WORD, Self::Error> {
317+
fn read(&mut self, _: &mut PIN) -> nb::Result<WORD, Self::Error> {
322318
self.power_up();
323-
self.configure(pin, false, None);
319+
self.configure(Channels { flags: 0x1 << PIN::channel() }, false, None);
324320

325321
while self.rb.isr.read().eos().bit_is_clear() {}
326322

@@ -348,6 +344,33 @@ pub struct Active<DmaChan, Buf> {
348344
}
349345

350346

347+
/// A collection of channels
348+
///
349+
/// Used to set up multi-channel conversions.
350+
pub struct Channels {
351+
flags: u32,
352+
}
353+
354+
impl Channels {
355+
/// Adds a channel to the collection
356+
pub fn add<C>(&mut self, _: C)
357+
where C: Channel<Adc<Ready>, ID=u8>
358+
{
359+
self.flags |= 0x1 << C::channel()
360+
}
361+
}
362+
363+
impl<C> From<C> for Channels
364+
where C: Channel<Adc<Ready>, ID=u8>
365+
{
366+
fn from(channel: C) -> Self {
367+
let mut c = Channels { flags: 0 };
368+
c.add(channel);
369+
c
370+
}
371+
}
372+
373+
351374
/// Hardware triggers that can start an ADC conversion
352375
#[repr(u8)]
353376
pub enum Trigger {

0 commit comments

Comments
 (0)