Skip to content

Commit

Permalink
Peripheral ref/adc (#321)
Browse files Browse the repository at this point in the history
* PeripheralRef: ADC

* PeripheralRef: DAC
  • Loading branch information
JurajSadel authored Dec 15, 2022
1 parent 4a780bd commit 4598df6
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 63 deletions.
19 changes: 12 additions & 7 deletions esp-hal-common/src/analog/adc/esp32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use embedded_hal::adc::{Channel, OneShot};

use crate::{
analog::{ADC1, ADC2},
peripheral::PeripheralRef,
peripherals::{RTCIO, SENS},
};

Expand Down Expand Up @@ -48,6 +49,7 @@ where
ADCI: RegisterAccess,
{
pub fn new() -> AdcConfig<ADCI> {
crate::into_ref!();
Self::default()
}

Expand Down Expand Up @@ -256,17 +258,20 @@ impl RegisterAccess for ADC2 {
}
}

pub struct ADC<ADC> {
adc: PhantomData<ADC>,
pub struct ADC<'d, ADC> {
_adc: PeripheralRef<'d, ADC>,
attenuations: [Option<Attenuation>; 10],
active_channel: Option<u8>,
}

impl<ADCI> ADC<ADCI>
impl<'d, ADCI> ADC<'d, ADCI>
where
ADCI: RegisterAccess,
{
pub fn adc(_adc_instance: ADCI, config: AdcConfig<ADCI>) -> Result<Self, ()> {
pub fn adc(
adc_instance: impl crate::peripheral::Peripheral<P = ADCI> + 'd,
config: AdcConfig<ADCI>,
) -> Result<Self, ()> {
let sensors = unsafe { &*SENS::ptr() };

// Set reading and sampling resolution
Expand Down Expand Up @@ -324,7 +329,7 @@ where
.modify(|_, w| unsafe { w.sar_amp_wait3().bits(1) });

let adc = ADC {
adc: PhantomData,
_adc: adc_instance.into_ref(),
attenuations: config.attenuations,
active_channel: None,
};
Expand All @@ -333,7 +338,7 @@ where
}
}

impl<ADC1> ADC<ADC1> {
impl<'d, ADC1> ADC<'d, ADC1> {
pub fn enable_hall_sensor() {
// Connect hall sensor
let rtcio = unsafe { &*RTCIO::ptr() };
Expand All @@ -347,7 +352,7 @@ impl<ADC1> ADC<ADC1> {
}
}

impl<ADCI, WORD, PIN> OneShot<ADCI, WORD, AdcPin<PIN, ADCI>> for ADC<ADCI>
impl<'d, ADCI, WORD, PIN> OneShot<ADCI, WORD, AdcPin<PIN, ADCI>> for ADC<'d, ADCI>
where
WORD: From<u16>,
PIN: Channel<ADCI, ID = u8>,
Expand Down
13 changes: 7 additions & 6 deletions esp-hal-common/src/analog/adc/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use embedded_hal::adc::{Channel, OneShot};
use crate::analog::ADC2;
use crate::{
analog::ADC1,
peripheral::PeripheralRef,
peripherals::APB_SARADC,
system::{Peripheral, PeripheralClockControl},
};
Expand Down Expand Up @@ -170,19 +171,19 @@ impl RegisterAccess for ADC2 {
}
}

pub struct ADC<ADC> {
adc: PhantomData<ADC>,
pub struct ADC<'d, ADCI> {
_adc: PeripheralRef<'d, ADCI>,
attenuations: [Option<Attenuation>; 5],
active_channel: Option<u8>,
}

impl<ADCI> ADC<ADCI>
impl<'d, ADCI> ADC<'d, ADCI>
where
ADCI: RegisterAccess,
{
pub fn adc(
peripheral_clock_controller: &mut PeripheralClockControl,
_adc_instance: ADCI,
adc_instance: impl crate::peripheral::Peripheral<P = ADCI> + 'd,
config: AdcConfig<ADCI>,
) -> Result<Self, ()> {
peripheral_clock_controller.enable(Peripheral::ApbSarAdc);
Expand All @@ -199,7 +200,7 @@ where
.bits(0b11)
});
let adc = ADC {
adc: PhantomData,
_adc: adc_instance.into_ref(),
attenuations: config.attenuations,
active_channel: None,
};
Expand All @@ -208,7 +209,7 @@ where
}
}

impl<ADCI, WORD, PIN> OneShot<ADCI, WORD, AdcPin<PIN, ADCI>> for ADC<ADCI>
impl<'d, ADCI, WORD, PIN> OneShot<ADCI, WORD, AdcPin<PIN, ADCI>> for ADC<'d, ADCI>
where
WORD: From<u16>,
PIN: Channel<ADCI, ID = u8>,
Expand Down
16 changes: 10 additions & 6 deletions esp-hal-common/src/analog/adc/xtensa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use embedded_hal::adc::{Channel, OneShot};

use crate::{
analog::{ADC1, ADC2},
peripheral::PeripheralRef,
peripherals::{APB_SARADC, SENS},
};

Expand Down Expand Up @@ -247,17 +248,20 @@ impl RegisterAccess for ADC2 {
}
}

pub struct ADC<ADC> {
adc: PhantomData<ADC>,
pub struct ADC<'d, ADC> {
_adc: PeripheralRef<'d, ADC>,
attenuations: [Option<Attenuation>; 10],
active_channel: Option<u8>,
}

impl<ADCI> ADC<ADCI>
impl<'d, ADCI> ADC<'d, ADCI>
where
ADCI: RegisterAccess,
{
pub fn adc(_adc_instance: ADCI, config: AdcConfig<ADCI>) -> Result<Self, ()> {
pub fn adc(
adc_instance: impl crate::peripheral::Peripheral<P = ADCI> + 'd,
config: AdcConfig<ADCI>,
) -> Result<Self, ()> {
let sensors = unsafe { &*SENS::ptr() };

// Set reading and sampling resolution
Expand Down Expand Up @@ -329,7 +333,7 @@ where
.modify(|_, w| unsafe { w.sar_amp_wait3().bits(1) });

let adc = ADC {
adc: PhantomData,
_adc: adc_instance.into_ref(),
attenuations: config.attenuations,
active_channel: None,
};
Expand All @@ -338,7 +342,7 @@ where
}
}

impl<ADCI, WORD, PIN> OneShot<ADCI, WORD, AdcPin<PIN, ADCI>> for ADC<ADCI>
impl<'d, ADCI, WORD, PIN> OneShot<ADCI, WORD, AdcPin<PIN, ADCI>> for ADC<'d, ADCI>
where
WORD: From<u16>,
PIN: Channel<ADCI, ID = u8>,
Expand Down
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
Loading

0 comments on commit 4598df6

Please sign in to comment.