Skip to content

Commit 34aa3c6

Browse files
committed
Add example of using pin.observe
1 parent c9c5695 commit 34aa3c6

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

examples/observe.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//! ## Origin
2+
//!
3+
//! This code has been taken from the stm32g0xx-hal project and modified slightly to support
4+
//! STM32G4xx MCUs.
5+
6+
//#![deny(warnings)]
7+
#![deny(unsafe_code)]
8+
#![no_main]
9+
#![no_std]
10+
11+
mod utils;
12+
extern crate cortex_m_rt as rt;
13+
14+
use fugit::ExtU32 as _;
15+
use hal::{
16+
adc::AdcClaim as _,
17+
comparator::{ComparatorExt, ComparatorSplit, Config},
18+
delay::SYSTDelayExt as _,
19+
gpio::GpioExt,
20+
observable::Observable as _,
21+
rcc::RccExt,
22+
stm32,
23+
};
24+
use rt::entry;
25+
use stm32g4xx_hal::{self as hal, adc::config::SampleTime, delay::DelayExt as _};
26+
27+
#[entry]
28+
fn main() -> ! {
29+
let cp = cortex_m::Peripherals::take().unwrap();
30+
let dp = stm32::Peripherals::take().unwrap();
31+
let mut rcc = dp.RCC.constrain();
32+
33+
let gpioa = dp.GPIOA.split(&mut rcc);
34+
35+
let (comp1, ..) = dp.COMP.split(&mut rcc);
36+
37+
let (pa1, [pa1_token]) = gpioa // <- The pin to keep track of
38+
.pa1
39+
.into_analog()
40+
.observe();
41+
let pa0 = gpioa.pa0.into_analog(); // <- Reference voltage
42+
43+
// Only pa1_token and pa0 consumed here
44+
let comp1 = comp1.comparator(pa1_token, pa0, Config::default(), &rcc.clocks);
45+
let _comp1 = comp1.enable(); // <-- TODO: Do things with comparator
46+
47+
let mut delay = cp.SYST.delay(&rcc.clocks);
48+
let mut adc = dp.ADC1.claim_and_configure(
49+
stm32g4xx_hal::adc::ClockSource::SystemClock,
50+
&rcc,
51+
stm32g4xx_hal::adc::config::AdcConfig::default(),
52+
&mut delay,
53+
false,
54+
);
55+
56+
// Can not reconfigure pa1 here
57+
loop {
58+
// Can still use pa1 here
59+
let sample = adc.convert(pa1.as_ref(), SampleTime::Cycles_640_5);
60+
defmt::info!("Reading: {}", sample);
61+
delay.delay(1000.millis());
62+
}
63+
}

0 commit comments

Comments
 (0)