Skip to content

Commit 8631223

Browse files
author
zhangpeng
committed
Add dac
1 parent ab6de2e commit 8631223

File tree

5 files changed

+116
-3
lines changed

5 files changed

+116
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1212
- Add changelog check on PRs
1313
- Replace UB code by a legitimate pointer access
1414
- Reexport `Direction` from `qei`
15+
- Add dac
1516

1617
## [v0.10.0] - 2022-12-12
1718

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,15 @@ stm32f107 = ["stm32f1/stm32f107", "device-selected", "connectivity"]
6161
# Devices with 64 or 128 Kb ROM
6262
medium = []
6363
# Devices with 256 or 512 Kb ROM
64-
high = ["medium"]
64+
high = ["medium", "has-dac"]
6565
# Devices with 768 Kb ROM or more
6666
xl = ["high"]
6767
# Connectivity line devices (`stm32f105xx` and `stm32f107xx`)
68-
connectivity = ["medium", "has-can"]
68+
connectivity = ["medium", "has-can", "has-dac"]
6969
# Devices with CAN interface
7070
has-can = []
71+
# Devices with Dac
72+
has-dac = []
7173

7274
rtic = ["rtic-monotonic"]
7375

src/dac.rs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//! # API for the Digital to Analog converter
2+
//!
3+
//! Currently only supports writing to the DR of the DAC,
4+
//! just a basic one-shot conversion.
5+
#![deny(unused_imports)]
6+
7+
use crate::{
8+
gpio::{Analog, PA4, PA5},
9+
pac::{DAC, RCC},
10+
rcc::{Enable, Reset},
11+
};
12+
13+
pub struct C1;
14+
pub struct C2;
15+
16+
pub trait DacOut<V> {
17+
fn set_value(&mut self, val: V);
18+
fn get_value(&mut self) -> V;
19+
}
20+
21+
pub trait DacPin {
22+
fn enable(&mut self);
23+
}
24+
25+
pub trait Pins<DAC> {
26+
type Output;
27+
#[doc(hidden)]
28+
fn init() -> Self::Output;
29+
}
30+
31+
impl Pins<DAC> for PA4<Analog> {
32+
type Output = C1;
33+
fn init() -> Self::Output {
34+
C1
35+
}
36+
}
37+
38+
impl Pins<DAC> for PA5<Analog> {
39+
type Output = C2;
40+
fn init() -> Self::Output {
41+
C2
42+
}
43+
}
44+
45+
impl Pins<DAC> for (PA4<Analog>, PA5<Analog>) {
46+
type Output = (C1, C2);
47+
fn init() -> Self::Output {
48+
(C1, C2)
49+
}
50+
}
51+
52+
pub fn dac<PINS>(_dac: DAC, _pins: PINS) -> PINS::Output
53+
where
54+
PINS: Pins<DAC>,
55+
{
56+
unsafe {
57+
// Enable and reset clock.
58+
let rcc = unsafe { &(*RCC::ptr()) };
59+
DAC::enable(rcc);
60+
DAC::reset(rcc);
61+
62+
PINS::init()
63+
}
64+
}
65+
66+
macro_rules! dac {
67+
($CX:ident, $en:ident, $cen:ident, $cal_flag:ident, $trim:ident, $mode:ident, $dhrx:ident, $dac_dor:ident, $daccxdhr:ident) => {
68+
impl DacPin for $CX {
69+
fn enable(&mut self) {
70+
let dac = unsafe { &(*DAC::ptr()) };
71+
dac.cr.modify(|_, w| w.$en().set_bit());
72+
}
73+
}
74+
75+
impl DacOut<u16> for $CX {
76+
fn set_value(&mut self, val: u16) {
77+
let dac = unsafe { &(*DAC::ptr()) };
78+
dac.$dhrx.write(|w| unsafe { w.bits(val as u32) });
79+
}
80+
81+
fn get_value(&mut self) -> u16 {
82+
let dac = unsafe { &(*DAC::ptr()) };
83+
dac.$dac_dor.read().bits() as u16
84+
}
85+
}
86+
};
87+
}
88+
89+
pub trait DacExt {
90+
fn constrain<PINS>(self, pins: PINS) -> PINS::Output
91+
where
92+
PINS: Pins<DAC>;
93+
}
94+
95+
impl DacExt for DAC {
96+
fn constrain<PINS>(self, pins: PINS) -> PINS::Output
97+
where
98+
PINS: Pins<DAC>,
99+
{
100+
dac(self, pins)
101+
}
102+
}
103+
104+
dac!(C1, en1, cen1, cal_flag1, otrim1, mode1, dhr12r1, dor1, dacc1dhr);
105+
dac!(C2, en2, cen2, cal_flag2, otrim2, mode2, dhr12r2, dor2, dacc2dhr);

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ pub mod bb;
150150
pub mod can;
151151
#[cfg(feature = "device-selected")]
152152
pub mod crc;
153+
#[cfg(all(feature = "device-selected", feature = "has-dac"))]
154+
pub mod dac;
153155
#[cfg(feature = "device-selected")]
154156
pub mod dma;
155157
#[cfg(feature = "device-selected")]

src/rcc/enable.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,13 @@ bus! {
7373
CAN1 => (APB1, 25),
7474
CAN2 => (APB1, 26),
7575
}
76+
#[cfg(feature = "has-dac")]
77+
bus! {
78+
DAC => (APB1, 29),
79+
}
7680
#[cfg(all(feature = "stm32f103", feature = "high"))]
7781
bus! {
7882
ADC3 => (APB2, 15),
79-
DAC => (APB1, 29),
8083
UART4 => (APB1, 19),
8184
UART5 => (APB1, 20),
8285
}

0 commit comments

Comments
 (0)