forked from stm32-rs/stm32f1xx-hal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zhangpeng
committed
Mar 21, 2024
1 parent
ab6de2e
commit 8631223
Showing
5 changed files
with
116 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
//! # 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 crate::{ | ||
gpio::{Analog, PA4, PA5}, | ||
pac::{DAC, RCC}, | ||
rcc::{Enable, Reset}, | ||
}; | ||
|
||
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; | ||
#[doc(hidden)] | ||
fn init() -> Self::Output; | ||
} | ||
|
||
impl Pins<DAC> for PA4<Analog> { | ||
type Output = C1; | ||
fn init() -> Self::Output { | ||
C1 | ||
} | ||
} | ||
|
||
impl Pins<DAC> for PA5<Analog> { | ||
type Output = C2; | ||
fn init() -> Self::Output { | ||
C2 | ||
} | ||
} | ||
|
||
impl Pins<DAC> for (PA4<Analog>, PA5<Analog>) { | ||
type Output = (C1, C2); | ||
fn init() -> Self::Output { | ||
(C1, C2) | ||
} | ||
} | ||
|
||
pub fn dac<PINS>(_dac: DAC, _pins: PINS) -> PINS::Output | ||
where | ||
PINS: Pins<DAC>, | ||
{ | ||
unsafe { | ||
// Enable and reset clock. | ||
let rcc = unsafe { &(*RCC::ptr()) }; | ||
DAC::enable(rcc); | ||
DAC::reset(rcc); | ||
|
||
PINS::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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters