Skip to content

Commit 104515f

Browse files
author
Jonas Schievink
committed
Fix rebase fallout, improve docs
1 parent 422fd75 commit 104515f

File tree

3 files changed

+19
-44
lines changed

3 files changed

+19
-44
lines changed

examples/dac_sine.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ use panic_semihosting as _;
88

99
use cortex_m_rt::entry;
1010

11-
use stm32f3xx_hal::{
12-
dac::{Dac, DacChannel},
13-
pac,
14-
prelude::*,
15-
};
11+
use stm32f3xx_hal::{dac::Dac, pac, prelude::*};
1612

1713
const LUT_LEN: usize = 256;
1814

@@ -49,15 +45,12 @@ fn main() -> ! {
4945
let _dac1_out1 = gpioa.pa4.into_analog(&mut gpioa.moder, &mut gpioa.pupdr);
5046

5147
// set up led for blinking loop
52-
let mut gpioe = dp.GPIOE.split(&mut rcc.ahb);
53-
let mut ok_led = gpioe
54-
.pe15
55-
.into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
48+
let mut ok_led = gpioa
49+
.pa15
50+
.into_push_pull_output(&mut gpioa.moder, &mut gpioa.otyper);
5651

5752
// set up dac1, data is twelve bits, alighned right
5853
let mut dac1 = Dac::new(dp.DAC1, &mut rcc.apb1);
59-
// enable channel one for single channel mode
60-
dac1.enable_channel(DacChannel::One);
6154

6255
let mut led = true;
6356

src/dac.rs

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,40 @@
1-
//Based on stm32hal by David-OConnor
1+
//! Digital-to-Analog Converter
2+
3+
// Based on stm32hal by David-OConnor
24

35
use cortex_m::asm;
46

57
use crate::{
68
gpio::{self, Analog},
79
pac::{dac1, DAC1},
8-
rcc::{Rcc, Clocks, AHB, APB1},
10+
rcc::{Clocks, Enable, Rcc, Reset, AHB, APB1},
911
};
1012

11-
#[cfg(any(
12-
feature = "stm32f303xb",
13-
feature = "stm32f303xc",
14-
feature = "stm32f303xd",
15-
feature = "stm32f303xe",
16-
))]
17-
use crate::pac::DMA1::{self};
13+
use crate::pac::DMA1;
1814

1915
use crate::pac::{self, rcc, RCC};
2016

21-
pub enum DacChannel {
22-
One,
23-
Two,
24-
}
25-
2617
/// Represents a Digital to Analog Converter (DAC) peripheral.
2718
pub struct Dac {
2819
regs: DAC1,
2920
}
3021

31-
// todo: Calculate the VDDA vref, as you do with onboard ADCs!
3222
impl Dac {
33-
/// Initialize a DAC peripheral, including enabling and resetting
23+
/// Initializes the DAC peripheral.
24+
pub fn new(regs: DAC1, apb1: &mut APB1) -> Self {
25+
DAC1::enable(apb1);
26+
DAC1::reset(apb1);
3427

35-
pub fn new(regs: DAC1, abp1: &mut APB1) -> Self {
36-
37-
abp1.enr().modify(|_, w| w.dac1en().set_bit());
38-
abp1.rstr().modify(|_, w| w.dac1rst().set_bit());
39-
abp1.rstr().modify(|_, w| w.dac1rst().clear_bit());
28+
// Enable channel 1.
29+
regs.cr.modify(|_, w| w.en1().set_bit());
4030

4131
Self { regs }
4232
}
4333

44-
pub fn enable_channel(&mut self, channel: DacChannel) {
45-
let cr = &self.regs.cr;
46-
47-
cr.modify(|_, w| match channel {
48-
DacChannel::One => w.en1().set_bit(),
49-
DacChannel::Two => w.en2().set_bit(),
50-
});
51-
}
52-
53-
/// the data parameter MUST be a 12-bit value -- values outside that range will result in misbehavior
34+
/// Writes a sample to the Channel 1 output register.
35+
///
36+
/// Only the low 12 bits of `data` will be used, the rest is ignored.
5437
pub fn write_data(&mut self, data: u16) {
55-
5638
self.regs.dhr12r1.write(|w| w.dacc1dhr().bits(data))
5739
}
5840
}

src/rcc/enable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ bus! {
154154
feature = "svd-f3x4"
155155
))]
156156
bus! {
157-
DAC1 => (APB1, dac1en,), // 29
157+
DAC1 => (APB1, dac1en, dac1rst), // 29
158158
}
159159

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

0 commit comments

Comments
 (0)