forked from stm32-rs/stm32h7xx-hal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dac.rs
66 lines (51 loc) · 1.61 KB
/
dac.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#![deny(warnings)]
#![no_main]
#![no_std]
use cortex_m::asm;
use cortex_m_rt::entry;
use stm32h7xx_hal::hal::Direction;
#[macro_use]
mod utilities;
use stm32h7xx_hal::{pac, prelude::*};
use stm32h7xx_hal::traits::DacOut;
use log::info;
#[entry]
fn main() -> ! {
utilities::logger::init();
let cp = cortex_m::Peripherals::take().unwrap();
let dp = pac::Peripherals::take().expect("Cannot take peripherals");
// Constrain and Freeze power
info!("Setup PWR... ");
let pwr = dp.PWR.constrain();
let pwrcfg = example_power!(pwr).freeze();
// Constrain and Freeze clock
info!("Setup RCC... ");
let rcc = dp.RCC.constrain();
let ccdr = rcc.sys_ck(8.mhz()).freeze(pwrcfg, &dp.SYSCFG);
let mut delay = cp.SYST.delay(ccdr.clocks);
let gpioa = dp.GPIOA.split(ccdr.peripheral.GPIOA);
#[cfg(not(feature = "rm0455"))]
let dac = dp.DAC.dac(gpioa.pa4, ccdr.peripheral.DAC12);
#[cfg(feature = "rm0455")]
let dac = dp.DAC1.dac(gpioa.pa4, ccdr.peripheral.DAC1);
// Calibrate output buffer then enable DAC channel
let mut dac = dac.calibrate_buffer(&mut delay).enable();
let mut dir = Direction::Upcounting;
let mut val = 0;
dac.set_value(2058);
asm::bkpt();
dac.set_value(4095);
asm::bkpt();
loop {
dac.set_value(val);
match val {
0 => dir = Direction::Upcounting,
4095 => dir = Direction::Downcounting,
_ => (),
};
match dir {
Direction::Upcounting => val += 1,
Direction::Downcounting => val -= 1,
}
}
}