Skip to content

Commit 3c68d70

Browse files
committed
Both examples/comp.rs and examples/dac.rs seems to be working
1 parent 240ff5a commit 3c68d70

File tree

4 files changed

+552
-200
lines changed

4 files changed

+552
-200
lines changed

examples/comp.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
}

examples/dac.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// #![deny(warnings)]
2+
#![deny(unsafe_code)]
3+
#![no_main]
4+
#![no_std]
5+
6+
use embedded_hal::Direction;
7+
use hal::dac::{DacExt, DacOut, GeneratorConfig};
8+
use hal::delay::SYSTDelayExt;
9+
use hal::gpio::GpioExt;
10+
use hal::rcc::RccExt;
11+
use stm32g4xx_hal as hal;
12+
mod utils;
13+
extern crate cortex_m_rt as rt;
14+
15+
use hal::stm32;
16+
use rt::entry;
17+
18+
#[entry]
19+
fn main() -> ! {
20+
let dp = stm32::Peripherals::take().expect("cannot take peripherals");
21+
let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
22+
23+
let mut rcc = dp.RCC.constrain();
24+
let mut delay = cp.SYST.delay(&rcc.clocks);
25+
26+
let gpioa = dp.GPIOA.split(&mut rcc);
27+
let (dac1ch1, dac1ch2) = dp.DAC1.constrain((gpioa.pa4, gpioa.pa5), &mut rcc);
28+
29+
let mut dac = dac1ch1.calibrate_buffer(&mut delay).enable();
30+
let mut generator = dac1ch2.enable_generator(GeneratorConfig::noise(11));
31+
32+
let mut dir = Direction::Upcounting;
33+
let mut val = 0;
34+
35+
loop {
36+
generator.trigger();
37+
dac.set_value(val);
38+
match val {
39+
0 => dir = Direction::Upcounting,
40+
4095 => dir = Direction::Downcounting,
41+
_ => (),
42+
};
43+
44+
match dir {
45+
Direction::Upcounting => val += 1,
46+
Direction::Downcounting => val -= 1,
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)