|
| 1 | +//#![deny(warnings)] |
| 2 | +#![deny(unsafe_code)] |
| 3 | +#![no_main] |
| 4 | +#![no_std] |
| 5 | + |
| 6 | +use hal::adc::config::SampleTime; |
| 7 | +use hal::adc::{AdcClaim, ClockSource}; |
| 8 | +use hal::comparator::{ComparatorExt, ComparatorSplit, Config, Hysteresis, RefintInput}; |
| 9 | +use hal::delay::SYSTDelayExt; |
| 10 | +use hal::gpio::GpioExt; |
| 11 | +use hal::prelude::OutputPin; |
| 12 | +use hal::rcc::RccExt; |
| 13 | +use stm32g4xx_hal as hal; |
| 14 | +mod utils; |
| 15 | +extern crate cortex_m_rt as rt; |
| 16 | + |
| 17 | +use hal::stm32; |
| 18 | +use rt::entry; |
| 19 | + |
| 20 | +#[entry] |
| 21 | +fn main() -> ! { |
| 22 | + let dp = stm32::Peripherals::take().expect("cannot take peripherals"); |
| 23 | + let mut rcc = dp.RCC.constrain(); |
| 24 | + |
| 25 | + let gpioa = dp.GPIOA.split(&mut rcc); |
| 26 | + |
| 27 | + let (comp1, comp2, ..) = dp.COMP.split(&mut rcc); |
| 28 | + |
| 29 | + let pa1 = gpioa.pa1.into_analog(); |
| 30 | + let pa0 = gpioa.pa0.into_analog(); |
| 31 | + let comp1 = comp1.comparator(pa1, pa0, Config::default(), &rcc.clocks); |
| 32 | + let comp1 = comp1.enable(); |
| 33 | + let mut led1 = gpioa.pa5.into_push_pull_output(); |
| 34 | + |
| 35 | + let pa7 = gpioa.pa7.into_analog(); |
| 36 | + let comp2 = comp2.comparator( |
| 37 | + pa7, |
| 38 | + RefintInput::VRefintM12, |
| 39 | + Config::default() |
| 40 | + .hysteresis(Hysteresis::None) |
| 41 | + .output_inverted(), |
| 42 | + &rcc.clocks, |
| 43 | + ); |
| 44 | + let led2 = gpioa.pa12.into_push_pull_output(); |
| 45 | + // Configure PA12 to the comparator's alternate function so it gets |
| 46 | + // changed directly by the comparator. |
| 47 | + comp2.output_pin(led2); |
| 48 | + let _comp2 = comp2.enable().lock(); |
| 49 | + |
| 50 | + loop { |
| 51 | + match comp1.output() { |
| 52 | + true => led1.set_high().unwrap(), |
| 53 | + false => led1.set_low().unwrap(), |
| 54 | + } |
| 55 | + } |
| 56 | +} |
0 commit comments