Skip to content

Commit d30018e

Browse files
authored
Merge pull request #318 from ferrous-systems/master
Minimal DAC driver
2 parents f966590 + 7968b1e commit d30018e

File tree

5 files changed

+110
-1
lines changed

5 files changed

+110
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1414

1515
## Unreleased
1616

17+
### Added
18+
19+
- Add a minimal DAC driver ([#318])
20+
1721
### Fixed
1822

1923
- Missing `MosiPin` impl for `PB5` ([#322])
@@ -557,6 +561,7 @@ let clocks = rcc
557561
[filter]: https://defmt.ferrous-systems.com/filtering.html
558562

559563
[#322]: https://github.com/stm32-rs/stm32f3xx-hal/pull/322
564+
[#318]: https://github.com/stm32-rs/stm32f3xx-hal/pull/318
560565
[#314]: https://github.com/stm32-rs/stm32f3xx-hal/pull/314
561566
[#309]: https://github.com/stm32-rs/stm32f3xx-hal/pull/309
562567
[#308]: https://github.com/stm32-rs/stm32f3xx-hal/pull/308

examples/dac_sine.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#![no_std]
2+
#![no_main]
3+
4+
//! Example usage for DAC on STM32F303
5+
// Based on example in stm32hal by David-OConnor
6+
7+
use panic_semihosting as _;
8+
9+
use cortex_m_rt::entry;
10+
11+
use stm32f3xx_hal::{dac::Dac, pac, prelude::*};
12+
13+
#[entry]
14+
/// Main Thread
15+
fn main() -> ! {
16+
// Get peripherals, clocks and freeze them
17+
let dp = pac::Peripherals::take().unwrap();
18+
let mut rcc = dp.RCC.constrain();
19+
// let clocks = rcc.cfgr.freeze(&mut dp.FLASH.constrain().acr);
20+
21+
// Set up pin PA4 as analog pin.
22+
let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);
23+
let _dac1_out1 = gpioa.pa4.into_analog(&mut gpioa.moder, &mut gpioa.pupdr);
24+
25+
// set up led for blinking loop
26+
let mut ok_led = gpioa
27+
.pa15
28+
.into_push_pull_output(&mut gpioa.moder, &mut gpioa.otyper);
29+
30+
// set up dac1, data is twelve bits, aligned right
31+
#[cfg(not(feature = "svd-f302"))]
32+
let mut dac1 = Dac::new(dp.DAC1, &mut rcc.apb1);
33+
34+
#[cfg(feature = "svd-f302")]
35+
let mut dac1 = Dac::new(dp.DAC, &mut rcc.apb1);
36+
37+
let mut led = true;
38+
39+
loop {
40+
for value in (0..256).chain((0..255).rev()) {
41+
dac1.write_data((4095 / 255) * value);
42+
cortex_m::asm::delay(8_000);
43+
}
44+
if led {
45+
ok_led.set_low().unwrap();
46+
led = false;
47+
} else {
48+
ok_led.set_high().unwrap();
49+
led = true;
50+
}
51+
}
52+
}

src/dac.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//! Digital-to-Analog Converter
2+
3+
// Based on stm32hal by David-OConnor
4+
5+
use crate::rcc::{Enable, Reset, APB1};
6+
7+
#[cfg(feature = "svd-f302")]
8+
use crate::pac::DAC;
9+
10+
#[cfg(any(
11+
feature = "svd-f301",
12+
feature = "svd-f303",
13+
feature = "svd-f373",
14+
feature = "svd-f3x4",
15+
))]
16+
use crate::pac::DAC1 as DAC;
17+
18+
/// Represents a Digital to Analog Converter (DAC) peripheral.
19+
pub struct Dac {
20+
regs: DAC,
21+
}
22+
23+
impl Dac {
24+
/// Initializes the DAC peripheral.
25+
pub fn new(regs: DAC, apb1: &mut APB1) -> Self {
26+
DAC::enable(apb1);
27+
DAC::reset(apb1);
28+
29+
// Enable channel 1.
30+
regs.cr.modify(|_, w| w.en1().set_bit());
31+
32+
Self { regs }
33+
}
34+
35+
/// Writes a sample to the Channel 1 output register.
36+
///
37+
/// Only the low 12 bits of `data` will be used, the rest is ignored.
38+
pub fn write_data(&mut self, data: u16) {
39+
self.regs.dhr12r1.write(|w| {
40+
#[allow(unused_unsafe)]
41+
unsafe {
42+
w.dacc1dhr().bits(data)
43+
}
44+
})
45+
}
46+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ pub mod adc;
187187
#[cfg(all(feature = "can", not(feature = "svd-f301")))]
188188
#[cfg_attr(docsrs, doc(cfg(feature = "can")))]
189189
pub mod can;
190+
pub mod dac;
190191
pub mod delay;
191192
pub mod dma;
192193
pub mod flash;

src/rcc/enable.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,12 @@ bus! {
154154
feature = "svd-f3x4"
155155
))]
156156
bus! {
157-
DAC1 => (APB1, dac1en,), // 29
157+
DAC1 => (APB1, dac1en, dac1rst), // 29
158+
}
159+
160+
#[cfg(any(feature = "svd-f302",))]
161+
bus! {
162+
DAC => (APB1, dac1en, dac1rst), // 29
158163
}
159164

160165
#[cfg(any(feature = "svd-f303", feature = "svd-f373", feature = "svd-f3x4"))]

0 commit comments

Comments
 (0)