Skip to content

Commit

Permalink
PeripheralRef: ADC
Browse files Browse the repository at this point in the history
  • Loading branch information
JurajSadel committed Dec 15, 2022
1 parent 4a780bd commit 602da2a
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 57 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
123 changes: 85 additions & 38 deletions esp-hal-common/src/analog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,86 @@ pub mod adc;
#[cfg(dac)]
pub mod dac;

cfg_if::cfg_if! {
if #[cfg(any(esp32, esp32s2, esp32s3))] {
use core::marker::PhantomData;
pub struct ADC1 {
_private: (),
}
pub struct ADC2 {
_private: (),
}

use crate::peripherals::SENS;
pub struct DAC1 {
_private: (),
}

pub struct ADC1 {
_private: PhantomData<()>,
}
pub struct ADC2 {
_private: PhantomData<()>,
}
pub struct DAC2 {
_private: (),
}

pub struct DAC1 {
_private: PhantomData<()>,
}
impl core::ops::Deref for ADC1 {
type Target = ADC1;

pub struct DAC2 {
_private: PhantomData<()>,
}
fn deref(&self) -> &Self::Target {
self
}
}

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

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

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

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

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

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

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

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


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

use crate::peripherals::SENS;

pub struct AvailableAnalog {
pub adc1: ADC1,
Expand All @@ -44,16 +104,16 @@ cfg_if::cfg_if! {
fn split(self) -> AvailableAnalog {
AvailableAnalog {
adc1: ADC1 {
_private: PhantomData,
_private: (),
},
adc2: ADC2 {
_private: PhantomData,
_private: (),
},
dac1: DAC1 {
_private: PhantomData,
_private: (),
},
dac2: DAC2 {
_private: PhantomData,
_private: (),
},
}
}
Expand All @@ -63,17 +123,9 @@ cfg_if::cfg_if! {

cfg_if::cfg_if! {
if #[cfg(esp32c3)] {
use core::marker::PhantomData;

use crate::peripherals::APB_SARADC;

pub struct ADC1 {
_private: PhantomData<()>,
}
pub struct ADC2 {
_private: PhantomData<()>,
}

pub struct AvailableAnalog {
pub adc1: ADC1,
pub adc2: ADC2,
Expand All @@ -84,14 +136,14 @@ cfg_if::cfg_if! {
fn split(self) -> AvailableAnalog;
}

impl SarAdcExt for APB_SARADC {
impl<'d, T: crate::peripheral::Peripheral<P = APB_SARADC> + 'd> SarAdcExt for T {
fn split(self) -> AvailableAnalog {
AvailableAnalog {
adc1: ADC1 {
_private: PhantomData,
_private: (),
},
adc2: ADC2 {
_private: PhantomData,
_private: (),
},
}
}
Expand All @@ -101,14 +153,9 @@ cfg_if::cfg_if! {

cfg_if::cfg_if! {
if #[cfg(esp32c2)] {
use core::marker::PhantomData;

use crate::peripherals::APB_SARADC;

pub struct ADC1 {
_private: PhantomData<()>,
}

pub struct AvailableAnalog {
pub adc1: ADC1,
}
Expand All @@ -118,11 +165,11 @@ cfg_if::cfg_if! {
fn split(self) -> AvailableAnalog;
}

impl SarAdcExt for APB_SARADC {
impl<'d, T: crate::peripheral::Peripheral<P = APB_SARADC> + 'd> SarAdcExt for T {
fn split(self) -> AvailableAnalog {
AvailableAnalog {
adc1: ADC1 {
_private: PhantomData,
_private: (),
},
}
}
Expand Down
1 change: 1 addition & 0 deletions esp-hal-common/src/peripherals/esp32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,6 @@ mod peripherals {
RTC_CNTL,
TIMG0,
TIMG1,
SENS,
}
}
1 change: 1 addition & 0 deletions esp-hal-common/src/peripherals/esp32c2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ mod peripherals {
DMA,
RTC_CNTL,
TIMG0,
APB_SARADC,
}
}
1 change: 1 addition & 0 deletions esp-hal-common/src/peripherals/esp32c3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ mod peripherals {
RTC_CNTL,
TIMG0,
TIMG1,
APB_SARADC,
}
}
2 changes: 2 additions & 0 deletions esp-hal-common/src/peripherals/esp32s2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,7 @@ mod peripherals {
RTC_CNTL,
TIMG0,
TIMG1,
APB_SARADC,
SENS,
}
}
2 changes: 2 additions & 0 deletions esp-hal-common/src/peripherals/esp32s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,7 @@ mod peripherals {
RTC_CNTL,
TIMG0,
TIMG1,
APB_SARADC,
SENS,
}
}

0 comments on commit 602da2a

Please sign in to comment.