Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dac copied from stm32f0xx-hal #175

Merged
merged 1 commit into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Reexport PAC as `pac` for consistency with other crates, consider `stm32` virtually deprecated
- Added external interrupt (EXTI) support for output pins
- Added `check_interrupt` method for GPIO pins
- Basic support for DAC

## [v0.8.3] - 2020-06-12

Expand Down
97 changes: 97 additions & 0 deletions src/dac.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//! # API for the Digital to Analog converter
//!
//! Currently only supports writing to the DR of the DAC,
//! just a basic one-shot conversion.
#![deny(unused_imports)]
use core::mem;

use crate::gpio::gpioa::{PA4, PA5};
use crate::gpio::Analog;
use crate::pac::DAC;
use crate::stm32::RCC;

pub struct C1;
pub struct C2;

pub trait DacOut<V> {
fn set_value(&mut self, val: V);
fn get_value(&mut self) -> V;
}

pub trait DacPin {
fn enable(&mut self);
}

pub trait Pins<DAC> {
type Output;
}

impl Pins<DAC> for PA4<Analog> {
type Output = C1;
}

impl Pins<DAC> for PA5<Analog> {
type Output = C2;
}

impl Pins<DAC> for (PA4<Analog>, PA5<Analog>) {
type Output = (C1, C2);
}

pub fn dac<PINS>(_dac: DAC, _pins: PINS) -> PINS::Output
where
PINS: Pins<DAC>,
{
// NOTE(unsafe) This executes only during initialisation
let rcc = unsafe { &(*RCC::ptr()) };

// Enable DAC clocks
rcc.apb1enr.modify(|_, w| w.dacen().set_bit());

// Reset DAC
rcc.apb1rstr.modify(|_, w| w.dacrst().set_bit());
rcc.apb1rstr.modify(|_, w| w.dacrst().clear_bit());

unsafe { mem::MaybeUninit::uninit().assume_init() }
}

macro_rules! dac {
($CX:ident, $en:ident, $cen:ident, $cal_flag:ident, $trim:ident, $mode:ident, $dhrx:ident, $dac_dor:ident, $daccxdhr:ident) => {
impl DacPin for $CX {
fn enable(&mut self) {
let dac = unsafe { &(*DAC::ptr()) };
dac.cr.modify(|_, w| w.$en().set_bit());
}
}

impl DacOut<u16> for $CX {
fn set_value(&mut self, val: u16) {
let dac = unsafe { &(*DAC::ptr()) };
dac.$dhrx.write(|w| unsafe { w.bits(val as u32) });
}

fn get_value(&mut self) -> u16 {
let dac = unsafe { &(*DAC::ptr()) };
dac.$dac_dor.read().bits() as u16
}
}
};
}

pub trait DacExt {
fn constrain<PINS>(self, pins: PINS) -> PINS::Output
where
PINS: Pins<DAC>;
}

impl DacExt for DAC {
fn constrain<PINS>(self, pins: PINS) -> PINS::Output
where
PINS: Pins<DAC>,
{
dac(self, pins)
}
}

dac!(C1, en1, cen1, cal_flag1, otrim1, mode1, dhr12r1, dor1, dacc1dhr);
dac!(C2, en2, cen2, cal_flag2, otrim2, mode2, dhr12r2, dor2, dacc2dhr);
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ pub use crate::stm32::interrupt;
pub mod adc;
#[cfg(feature = "device-selected")]
pub mod bb;
#[cfg(all(
feature = "device-selected",
not(any(feature = "stm32f411", feature = "stm32f412", feature = "stm32f401",))
))]
pub mod dac;
#[cfg(feature = "device-selected")]
pub mod delay;
#[cfg(feature = "device-selected")]
Expand Down
5 changes: 5 additions & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ pub use embedded_hal::digital::v2::StatefulOutputPin as _embedded_hal_digital_v2
pub use embedded_hal::digital::v2::ToggleableOutputPin as _embedded_hal_digital_v2_ToggleableOutputPin;
pub use embedded_hal::prelude::*;

#[cfg(all(
feature = "device-selected",
not(any(feature = "stm32f411", feature = "stm32f412", feature = "stm32f401",))
))]
pub use crate::dac::DacExt as _stm32f4xx_hal_dac_DacExt;
pub use crate::gpio::GpioExt as _stm32f4xx_hal_gpio_GpioExt;
pub use crate::i2c::Pins as _stm32f4xx_hal_i2c_Pins;
pub use crate::rcc::RccExt as _stm32f4xx_hal_rcc_RccExt;
Expand Down