use crate::peripherals::{RTC_IO, SENS};
pub trait DAC {
fn write(&mut self, value: u8);
}
trait DAC1Impl {
fn set_power(self) -> Self
where
Self: Sized,
{
#[cfg(esp32s2)]
{
let sensors = unsafe { &*SENS::ptr() };
sensors
.sar_dac_ctrl1()
.modify(|_, w| w.dac_clkgate_en().set_bit());
}
let rtcio = unsafe { &*RTC_IO::ptr() };
rtcio.pad_dac1().modify(|_, w| {
w.pdac1_dac_xpd_force().set_bit();
w.pdac1_xpd_dac().set_bit()
});
self
}
fn write(&mut self, value: u8) {
let rtcio = unsafe { &*RTC_IO::ptr() };
let sensors = unsafe { &*SENS::ptr() };
sensors
.sar_dac_ctrl2()
.modify(|_, w| w.dac_cw_en1().clear_bit());
rtcio
.pad_dac1()
.modify(|_, w| unsafe { w.pdac1_dac().bits(value) });
}
}
trait DAC2Impl {
fn set_power(self) -> Self
where
Self: Sized,
{
#[cfg(esp32s2)]
{
let sensors = unsafe { &*SENS::ptr() };
sensors
.sar_dac_ctrl1()
.modify(|_, w| w.dac_clkgate_en().set_bit());
}
let rtcio = unsafe { &*RTC_IO::ptr() };
rtcio.pad_dac2().modify(|_, w| {
w.pdac2_dac_xpd_force().set_bit();
w.pdac2_xpd_dac().set_bit()
});
self
}
fn write(&mut self, value: u8) {
let rtcio = unsafe { &*RTC_IO::ptr() };
let sensors = unsafe { &*SENS::ptr() };
sensors
.sar_dac_ctrl2()
.modify(|_, w| w.dac_cw_en2().clear_bit());
rtcio
.pad_dac2()
.modify(|_, w| unsafe { w.pdac2_dac().bits(value) });
}
}
macro_rules! impl_dac {
($($number:literal => $gpio:ident),+) => {
$(
paste::paste! {
use $crate::analog::dac::[<DAC $number Impl>];
#[doc = "DAC channel " $number]
pub struct [<DAC $number>]<'d, DAC> {
_dac: $crate::peripheral::PeripheralRef<'d, DAC>,
_private: ::core::marker::PhantomData<()>,
}
impl<'d, DAC> [<DAC $number Impl>] for [<DAC $number>]<'d, DAC> {}
impl<'d, DAC> [<DAC $number>]<'d, DAC> {
pub fn dac(
dac: impl $crate::peripheral::Peripheral<P = DAC> +'d,
_pin: $crate::gpio::$gpio<$crate::gpio::Analog>,
) -> Result<Self, ()> {
let dac = Self {
_dac: dac.into_ref(),
_private: ::core::marker::PhantomData,
}
.set_power();
Ok(dac)
}
pub fn write(&mut self, value: u8) {
[<DAC $number Impl>]::write(self, value)
}
}
}
)+
};
}
pub use implementation::*;
#[cfg(esp32)]
mod implementation {
impl_dac!(1 => Gpio25, 2 => Gpio26);
}
#[cfg(esp32s2)]
mod implementation {
impl_dac!(1 => Gpio17, 2 => Gpio18);
}