|
| 1 | +use crate::gpio::{ |
| 2 | + gpioa::{PA4, PA5}, |
| 3 | + Analog, |
| 4 | +}; |
| 5 | +use crate::pac::{DAC, RCC}; |
| 6 | + |
| 7 | +/// DAC Errors |
| 8 | +#[derive(Debug)] |
| 9 | +pub enum Error { |
| 10 | + /// general |
| 11 | + error, |
| 12 | +} |
| 13 | + |
| 14 | +use core::mem; |
| 15 | + |
| 16 | +pub struct C1; |
| 17 | +pub struct C2; |
| 18 | + |
| 19 | +pub trait DacOut<V> { |
| 20 | + fn set_value(&mut self, val: V); |
| 21 | + fn get_value(&mut self) -> V; |
| 22 | +} |
| 23 | + |
| 24 | +pub trait DacPin { |
| 25 | + fn enable(&mut self); |
| 26 | +} |
| 27 | + |
| 28 | +pub trait Pins<DAC> { |
| 29 | + type Output; |
| 30 | +} |
| 31 | + |
| 32 | +impl Pins<DAC> for PA4<Analog> { |
| 33 | + type Output = C1; |
| 34 | +} |
| 35 | + |
| 36 | +impl Pins<DAC> for PA5<Analog> { |
| 37 | + type Output = C2; |
| 38 | +} |
| 39 | + |
| 40 | +impl Pins<DAC> for (PA4<Analog>, PA5<Analog>) { |
| 41 | + type Output = (C1, C2); |
| 42 | +} |
| 43 | + |
| 44 | +pub fn dac<PINS>(_dac: DAC, _pins: PINS) -> PINS::Output |
| 45 | +where |
| 46 | + PINS: Pins<DAC>, |
| 47 | +{ |
| 48 | + unsafe { |
| 49 | + // NOTE(unsafe) this reference will only be used for atomic writes with no side effects. |
| 50 | + let rcc = &(*RCC::ptr()); |
| 51 | + rcc.apb1enr.modify(|_, w| w.dacen().set_bit()); |
| 52 | + |
| 53 | + // Stall the pipeline to work around erratum 2.1.13 (DM00037591) |
| 54 | + cortex_m::asm::dsb(); |
| 55 | + |
| 56 | + rcc.apb1rstr.write(|w| w.dacrst().set_bit()); |
| 57 | + rcc.apb1rstr.write(|w| w.dacrst().clear_bit()); |
| 58 | + |
| 59 | + // NOTE(unsafe) ZST, doesn't need initialization. |
| 60 | + mem::MaybeUninit::uninit().assume_init() |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +macro_rules! dac { |
| 65 | + ($CX:ident, $en:ident, $cen:ident, $cal_flag:ident, $trim:ident, $mode:ident, $dhrx:ident, $dac_dor:ident, $daccxdhr:ident) => { |
| 66 | + impl DacPin for $CX { |
| 67 | + fn enable(&mut self) { |
| 68 | + let dac = unsafe { &(*DAC::ptr()) }; |
| 69 | + dac.cr.modify(|_, w| w.$en().set_bit()); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + impl DacOut<u16> for $CX { |
| 74 | + fn set_value(&mut self, val: u16) { |
| 75 | + let dac = unsafe { &(*DAC::ptr()) }; |
| 76 | + dac.$dhrx.write(|w| unsafe { w.bits(val as u32) }); |
| 77 | + } |
| 78 | + |
| 79 | + fn get_value(&mut self) -> u16 { |
| 80 | + let dac = unsafe { &(*DAC::ptr()) }; |
| 81 | + dac.$dac_dor.read().bits() as u16 |
| 82 | + } |
| 83 | + } |
| 84 | + }; |
| 85 | +} |
| 86 | + |
| 87 | +pub trait DacExt { |
| 88 | + fn constrain<PINS>(self, pins: PINS) -> PINS::Output |
| 89 | + where |
| 90 | + PINS: Pins<DAC>; |
| 91 | +} |
| 92 | + |
| 93 | +impl DacExt for DAC { |
| 94 | + fn constrain<PINS>(self, pins: PINS) -> PINS::Output |
| 95 | + where |
| 96 | + PINS: Pins<DAC>, |
| 97 | + { |
| 98 | + dac(self, pins) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +dac!(C1, en1, cen1, cal_flag1, otrim1, mode1, dhr12r1, dor1, dacc1dhr); |
| 103 | +dac!(C2, en2, cen2, cal_flag2, otrim2, mode2, dhr12r2, dor2, dacc2dhr); |
0 commit comments