Skip to content

Commit 2de82e3

Browse files
committed
Add example for ADC with hardware trigger
1 parent 8256fbe commit 2de82e3

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ opt-level = "s"
266266
name = "adc_cont"
267267
required-features = ["stm32l0x2"]
268268

269+
[[example]]
270+
name = "adc_trig"
271+
required-features = ["stm32l0x2"]
272+
269273
[[example]]
270274
name = "aes_ecb"
271275
required-features = ["stm32l082"]

examples/adc_trig.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//! Example showing continuous ADC with hardware trigger
2+
3+
4+
#![no_main]
5+
#![no_std]
6+
7+
8+
extern crate panic_halt;
9+
10+
11+
use core::{
12+
fmt::Write as _,
13+
pin::Pin,
14+
};
15+
16+
17+
use cortex_m_rt::entry;
18+
use stm32l0xx_hal::{
19+
prelude::*,
20+
adc,
21+
dma::DMA,
22+
pac::{
23+
self,
24+
tim2::cr2::MMS_A,
25+
},
26+
rcc,
27+
serial,
28+
};
29+
30+
31+
#[entry]
32+
fn main() -> ! {
33+
let dp = pac::Peripherals::take().unwrap();
34+
35+
let mut rcc = dp.RCC.freeze(rcc::Config::hsi16());
36+
let adc = dp.ADC.constrain(&mut rcc);
37+
let mut dma = DMA::new(dp.DMA1, &mut rcc);
38+
let gpioa = dp.GPIOA.split(&mut rcc);
39+
40+
// The A0 connector on the B-L072Z-LRWAN1 Discovery kit
41+
let a0 = gpioa.pa0.into_analog();
42+
43+
// Connected to the host computer via the ST-LINK
44+
let tx = gpioa.pa2;
45+
let rx = gpioa.pa3;
46+
47+
// Initialize USART for test output
48+
let (mut tx, _) = dp.USART2
49+
.usart(
50+
(tx, rx),
51+
serial::Config::default()
52+
.baudrate(115_200.bps()),
53+
&mut rcc,
54+
)
55+
.unwrap()
56+
.split();
57+
58+
// Create the buffer we're going to use for DMA.
59+
//
60+
// This is safe, since this is the main function, and it's only executed
61+
// once. This means there is no other code accessing this `static`.
62+
static mut BUFFER: [u16; 256] = [0; 256];
63+
let buffer = Pin::new(unsafe { &mut BUFFER });
64+
65+
// Start reading ADC values
66+
let mut adc = adc.start(
67+
a0,
68+
Some(adc::Trigger::TIM2_TRGO),
69+
&mut dma.handle,
70+
dma.channels.channel1,
71+
buffer,
72+
);
73+
74+
// Enable trigger output for TIM2. This must happen after ADC has been
75+
// configured.
76+
dp.TIM2
77+
.timer(1u32.hz(), &mut rcc)
78+
.select_master_mode(MMS_A::UPDATE);
79+
80+
loop {
81+
for val in adc.read_available().unwrap() {
82+
write!(tx, "{}\r\n", val.unwrap()).unwrap();
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)