Skip to content

Commit 8256fbe

Browse files
committed
Add support for hardware triggers to ADC API
1 parent e2ddb4b commit 8256fbe

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

examples/adc_cont.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ fn main() -> ! {
6161
// Start reading ADC values
6262
let mut adc = adc.start(
6363
a0,
64+
None,
6465
&mut dma.handle,
6566
dma.channels.channel1,
6667
buffer,

src/adc.rs

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,12 @@ impl Adc<Ready> {
139139
///
140140
/// The `channel` argument specifies which channel should be converted.
141141
///
142-
/// In addition to the preceeding argument that configures the ADC,
142+
/// The `trigger` argument specifies the trigger that will start each
143+
/// conversion sequence. This only configures the ADC peripheral to accept
144+
/// this trigger. The trigger itself must also be configured using its own
145+
/// peripheral API.
146+
///
147+
/// In addition to the preceeding arguments that configure the ADC,
143148
/// additional arguments are required to configure the DMA transfer that is
144149
/// used to read the results from the ADC:
145150
/// - `dma` is a handle to the DMA peripheral.
@@ -153,6 +158,7 @@ impl Adc<Ready> {
153158
#[cfg(feature = "stm32l0x2")]
154159
pub fn start<Chan, DmaChan, Buf>(mut self,
155160
channel: Chan,
161+
trigger: Option<Trigger>,
156162
dma: &mut dma::Handle,
157163
dma_chan: DmaChan,
158164
buffer: Pin<Buf>,
@@ -205,8 +211,10 @@ impl Adc<Ready> {
205211
}
206212
.start();
207213

214+
let continous = trigger.is_none();
215+
208216
self.power_up();
209-
self.configure(&channel, true);
217+
self.configure(&channel, continous, trigger);
210218

211219
Adc {
212220
rb: self.rb,
@@ -260,7 +268,11 @@ impl<State> Adc<State> {
260268
while self.rb.cr.read().aden().bit_is_set() {}
261269
}
262270

263-
fn configure<Chan>(&mut self, _channel: &Chan, cont: bool)
271+
fn configure<Chan>(&mut self,
272+
_channel: &Chan,
273+
cont: bool,
274+
trigger: Option<Trigger>,
275+
)
264276
where
265277
Chan: Channel<Adc<Ready>, ID=u8>,
266278
{
@@ -272,7 +284,16 @@ impl<State> Adc<State> {
272284
// DMA circular mode
273285
.dmacfg().set_bit()
274286
// Generate DMA requests
275-
.dmaen().set_bit()
287+
.dmaen().set_bit();
288+
289+
if let Some(trigger) = trigger {
290+
// Select hardware trigger
291+
w.extsel().bits(trigger as u8);
292+
// Enable hardware trigger on rising edge
293+
w.exten().rising_edge();
294+
}
295+
296+
w
276297
});
277298

278299
self.rb
@@ -299,7 +320,7 @@ where
299320

300321
fn read(&mut self, pin: &mut PIN) -> nb::Result<WORD, Self::Error> {
301322
self.power_up();
302-
self.configure(pin, false);
323+
self.configure(pin, false, None);
303324

304325
while self.rb.isr.read().eos().bit_is_clear() {}
305326

@@ -327,6 +348,38 @@ pub struct Active<DmaChan, Buf> {
327348
}
328349

329350

351+
/// Hardware triggers that can start an ADC conversion
352+
#[repr(u8)]
353+
pub enum Trigger {
354+
/// TRG0
355+
TIM6_TRGO = 0b000,
356+
357+
/// TRG1
358+
TIM21_CH2 = 0b001,
359+
360+
/// TRG2
361+
TIM2_TRGO = 0b010,
362+
363+
/// TRG3
364+
TIM2_CH4 = 0b011,
365+
366+
/// TRG4
367+
TIM22_TRGO = 0b100,
368+
369+
/// TRG5
370+
///
371+
/// Only available on Category 5 devices.
372+
#[cfg(any(feature = "stm32l072", feature = "stm32l082"))]
373+
TIM2_CH3 = 0b101,
374+
375+
/// TRG6
376+
TIM3_TRGO = 0b110,
377+
378+
/// TRG7
379+
EXTI11 = 0b111,
380+
}
381+
382+
330383
/// Provides access to the buffer that the DMA writes ADC values into
331384
///
332385
/// Since the DMA transfer takes ownership of the buffer, we need to access it

0 commit comments

Comments
 (0)