Skip to content

Commit c0bb1f2

Browse files
authored
Merge pull request #113 from hardfau18/dac
added dac support
2 parents f1e36d9 + 0812e78 commit c0bb1f2

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ micromath = "1.0.0"
2525
synopsys-usb-otg = { version = "0.2.3", features = ["cortex-m"], optional = true }
2626
stm32-fmc = { version = "0.2.0", features = ["sdram"], optional = true }
2727
rand_core = "0.5.1"
28-
bxcan = "0.4"
28+
bxcan = "0.5"
2929

3030
[dependencies.bare-metal]
3131
version = "0.2.4"

src/dac.rs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
use crate::gpio::{
2+
gpioa::{PA4, PA5},
3+
Analog,
4+
};
5+
use crate::pac::{DAC, RCC};
6+
7+
/// DAC Errors
8+
#[derive(Debug)]
9+
pub enum Error {
10+
/// general
11+
error,
12+
}
13+
14+
use core::mem;
15+
16+
pub struct C1;
17+
pub struct C2;
18+
19+
pub trait DacOut<V> {
20+
fn set_value(&mut self, val: V);
21+
fn get_value(&mut self) -> V;
22+
}
23+
24+
pub trait DacPin {
25+
fn enable(&mut self);
26+
}
27+
28+
pub trait Pins<DAC> {
29+
type Output;
30+
}
31+
32+
impl Pins<DAC> for PA4<Analog> {
33+
type Output = C1;
34+
}
35+
36+
impl Pins<DAC> for PA5<Analog> {
37+
type Output = C2;
38+
}
39+
40+
impl Pins<DAC> for (PA4<Analog>, PA5<Analog>) {
41+
type Output = (C1, C2);
42+
}
43+
44+
pub fn dac<PINS>(_dac: DAC, _pins: PINS) -> PINS::Output
45+
where
46+
PINS: Pins<DAC>,
47+
{
48+
unsafe {
49+
// NOTE(unsafe) this reference will only be used for atomic writes with no side effects.
50+
let rcc = &(*RCC::ptr());
51+
rcc.apb1enr.modify(|_, w| w.dacen().set_bit());
52+
53+
// Stall the pipeline to work around erratum 2.1.13 (DM00037591)
54+
cortex_m::asm::dsb();
55+
56+
rcc.apb1rstr.write(|w| w.dacrst().set_bit());
57+
rcc.apb1rstr.write(|w| w.dacrst().clear_bit());
58+
59+
// NOTE(unsafe) ZST, doesn't need initialization.
60+
mem::MaybeUninit::uninit().assume_init()
61+
}
62+
}
63+
64+
macro_rules! dac {
65+
($CX:ident, $en:ident, $cen:ident, $cal_flag:ident, $trim:ident, $mode:ident, $dhrx:ident, $dac_dor:ident, $daccxdhr:ident) => {
66+
impl DacPin for $CX {
67+
fn enable(&mut self) {
68+
let dac = unsafe { &(*DAC::ptr()) };
69+
dac.cr.modify(|_, w| w.$en().set_bit());
70+
}
71+
}
72+
73+
impl DacOut<u16> for $CX {
74+
fn set_value(&mut self, val: u16) {
75+
let dac = unsafe { &(*DAC::ptr()) };
76+
dac.$dhrx.write(|w| unsafe { w.bits(val as u32) });
77+
}
78+
79+
fn get_value(&mut self) -> u16 {
80+
let dac = unsafe { &(*DAC::ptr()) };
81+
dac.$dac_dor.read().bits() as u16
82+
}
83+
}
84+
};
85+
}
86+
87+
pub trait DacExt {
88+
fn constrain<PINS>(self, pins: PINS) -> PINS::Output
89+
where
90+
PINS: Pins<DAC>;
91+
}
92+
93+
impl DacExt for DAC {
94+
fn constrain<PINS>(self, pins: PINS) -> PINS::Output
95+
where
96+
PINS: Pins<DAC>,
97+
{
98+
dac(self, pins)
99+
}
100+
}
101+
102+
dac!(C1, en1, cen1, cal_flag1, otrim1, mode1, dhr12r1, dor1, dacc1dhr);
103+
dac!(C2, en2, cen2, cal_flag2, otrim2, mode2, dhr12r2, dor2, dacc2dhr);

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ pub mod fmc;
8686
#[cfg(feature = "device-selected")]
8787
pub mod gpio;
8888

89+
#[cfg(feature = "device-selected")]
90+
pub mod dac;
91+
8992
#[cfg(all(
9093
feature = "usb_fs",
9194
any(

0 commit comments

Comments
 (0)