Skip to content

Commit

Permalink
Add example of using pin.observe
Browse files Browse the repository at this point in the history
  • Loading branch information
usbalbin committed Dec 23, 2024
1 parent c9c5695 commit 34aa3c6
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions examples/observe.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//! ## Origin
//!
//! This code has been taken from the stm32g0xx-hal project and modified slightly to support
//! STM32G4xx MCUs.
//#![deny(warnings)]
#![deny(unsafe_code)]
#![no_main]
#![no_std]

mod utils;
extern crate cortex_m_rt as rt;

use fugit::ExtU32 as _;
use hal::{
adc::AdcClaim as _,
comparator::{ComparatorExt, ComparatorSplit, Config},
delay::SYSTDelayExt as _,
gpio::GpioExt,
observable::Observable as _,
rcc::RccExt,
stm32,
};
use rt::entry;
use stm32g4xx_hal::{self as hal, adc::config::SampleTime, delay::DelayExt as _};

#[entry]
fn main() -> ! {
let cp = cortex_m::Peripherals::take().unwrap();
let dp = stm32::Peripherals::take().unwrap();
let mut rcc = dp.RCC.constrain();

let gpioa = dp.GPIOA.split(&mut rcc);

let (comp1, ..) = dp.COMP.split(&mut rcc);

let (pa1, [pa1_token]) = gpioa // <- The pin to keep track of
.pa1
.into_analog()
.observe();
let pa0 = gpioa.pa0.into_analog(); // <- Reference voltage

// Only pa1_token and pa0 consumed here
let comp1 = comp1.comparator(pa1_token, pa0, Config::default(), &rcc.clocks);
let _comp1 = comp1.enable(); // <-- TODO: Do things with comparator

let mut delay = cp.SYST.delay(&rcc.clocks);
let mut adc = dp.ADC1.claim_and_configure(
stm32g4xx_hal::adc::ClockSource::SystemClock,
&rcc,
stm32g4xx_hal::adc::config::AdcConfig::default(),
&mut delay,
false,
);

// Can not reconfigure pa1 here
loop {
// Can still use pa1 here
let sample = adc.convert(pa1.as_ref(), SampleTime::Cycles_640_5);
defmt::info!("Reading: {}", sample);
delay.delay(1000.millis());
}
}

0 comments on commit 34aa3c6

Please sign in to comment.