Skip to content

Commit

Permalink
PeripheralRef: DAC
Browse files Browse the repository at this point in the history
  • Loading branch information
JurajSadel committed Dec 15, 2022
1 parent 602da2a commit 05b1ef2
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 6 deletions.
16 changes: 10 additions & 6 deletions esp-hal-common/src/analog/dac.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::peripherals::{RTCIO, SENS};

use crate::{
peripheral::{Peripheral, PeripheralRef},
peripherals::{RTCIO, SENS},
};
pub trait DAC {
fn write(&mut self, value: u8);
}
Expand Down Expand Up @@ -92,19 +94,21 @@ macro_rules! impl_dac {
pub use $crate::analog::dac::[<DAC $number Impl>];

/// DAC channel
pub struct [<DAC $number>] {
pub struct [<DAC $number>]<'d, DAC> {
_dac: PeripheralRef<'d, DAC>,
_private: PhantomData<()>,
}

impl [<DAC $number Impl>] for [<DAC $number>] {}
impl<'d, DAC> [<DAC $number Impl>] for [<DAC $number>]<'d, DAC> {}

impl [<DAC $number>] {
impl<'d, DAC> [<DAC $number>]<'d, DAC> {
/// Constructs a new DAC instance
pub fn dac(
_dac: $crate::analog::[<DAC $number>],
dac: impl crate::peripheral::Peripheral<P = DAC> +'d,
_pin: gpio::$gpio<$crate::Analog>,
) -> Result<Self, ()> {
let dac = Self {
_dac: dac.into_ref(),
_private: PhantomData,
}
.set_power();
Expand Down
59 changes: 59 additions & 0 deletions esp-hal-common/src/analog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,65 @@ impl crate::peripheral::Peripheral for &mut ADC2 {
}
}

impl core::ops::Deref for DAC1 {
type Target = DAC1;

fn deref(&self) -> &Self::Target {
self
}
}

impl core::ops::DerefMut for DAC1 {
fn deref_mut(&mut self) -> &mut Self::Target {
self
}
}

impl crate::peripheral::Peripheral for DAC1 {
type P = DAC1;
#[inline]
unsafe fn clone_unchecked(&mut self) -> Self::P {
DAC1 { _private: () }
}
}

impl crate::peripheral::Peripheral for &mut DAC1 {
type P = DAC1;
#[inline]
unsafe fn clone_unchecked(&mut self) -> Self::P {
DAC1 { _private: () }
}
}

impl core::ops::Deref for DAC2 {
type Target = DAC2;

fn deref(&self) -> &Self::Target {
self
}
}

impl core::ops::DerefMut for DAC2 {
fn deref_mut(&mut self) -> &mut Self::Target {
self
}
}

impl crate::peripheral::Peripheral for DAC2 {
type P = DAC2;
#[inline]
unsafe fn clone_unchecked(&mut self) -> Self::P {
DAC2 { _private: () }
}
}

impl crate::peripheral::Peripheral for &mut DAC2 {
type P = DAC2;
#[inline]
unsafe fn clone_unchecked(&mut self) -> Self::P {
DAC2 { _private: () }
}
}

cfg_if::cfg_if! {
if #[cfg(any(esp32, esp32s2, esp32s3))] {
Expand Down

0 comments on commit 05b1ef2

Please sign in to comment.