diff --git a/esp-hal-common/src/analog/adc/esp32.rs b/esp-hal-common/src/analog/adc/esp32.rs index a94f96541f3..85bf7cc6550 100644 --- a/esp-hal-common/src/analog/adc/esp32.rs +++ b/esp-hal-common/src/analog/adc/esp32.rs @@ -4,6 +4,7 @@ use embedded_hal::adc::{Channel, OneShot}; use crate::{ analog::{ADC1, ADC2}, + peripheral::PeripheralRef, peripherals::{RTCIO, SENS}, }; @@ -48,6 +49,7 @@ where ADCI: RegisterAccess, { pub fn new() -> AdcConfig { + crate::into_ref!(); Self::default() } @@ -256,17 +258,20 @@ impl RegisterAccess for ADC2 { } } -pub struct ADC { - adc: PhantomData, +pub struct ADC<'d, ADC> { + _adc: PeripheralRef<'d, ADC>, attenuations: [Option; 10], active_channel: Option, } -impl ADC +impl<'d, ADCI> ADC<'d, ADCI> where ADCI: RegisterAccess, { - pub fn adc(_adc_instance: ADCI, config: AdcConfig) -> Result { + pub fn adc( + adc_instance: impl crate::peripheral::Peripheral

+ 'd, + config: AdcConfig, + ) -> Result { let sensors = unsafe { &*SENS::ptr() }; // Set reading and sampling resolution @@ -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, }; @@ -333,7 +338,7 @@ where } } -impl ADC { +impl<'d, ADC1> ADC<'d, ADC1> { pub fn enable_hall_sensor() { // Connect hall sensor let rtcio = unsafe { &*RTCIO::ptr() }; @@ -347,7 +352,7 @@ impl ADC { } } -impl OneShot> for ADC +impl<'d, ADCI, WORD, PIN> OneShot> for ADC<'d, ADCI> where WORD: From, PIN: Channel, diff --git a/esp-hal-common/src/analog/adc/riscv.rs b/esp-hal-common/src/analog/adc/riscv.rs index 6fa243e240f..20333238dca 100644 --- a/esp-hal-common/src/analog/adc/riscv.rs +++ b/esp-hal-common/src/analog/adc/riscv.rs @@ -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}, }; @@ -170,19 +171,19 @@ impl RegisterAccess for ADC2 { } } -pub struct ADC { - adc: PhantomData, +pub struct ADC<'d, ADCI> { + _adc: PeripheralRef<'d, ADCI>, attenuations: [Option; 5], active_channel: Option, } -impl ADC +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

+ 'd, config: AdcConfig, ) -> Result { peripheral_clock_controller.enable(Peripheral::ApbSarAdc); @@ -199,7 +200,7 @@ where .bits(0b11) }); let adc = ADC { - adc: PhantomData, + _adc: adc_instance.into_ref(), attenuations: config.attenuations, active_channel: None, }; @@ -208,7 +209,7 @@ where } } -impl OneShot> for ADC +impl<'d, ADCI, WORD, PIN> OneShot> for ADC<'d, ADCI> where WORD: From, PIN: Channel, diff --git a/esp-hal-common/src/analog/adc/xtensa.rs b/esp-hal-common/src/analog/adc/xtensa.rs index 8318cd53009..30d42812ef6 100644 --- a/esp-hal-common/src/analog/adc/xtensa.rs +++ b/esp-hal-common/src/analog/adc/xtensa.rs @@ -4,6 +4,7 @@ use embedded_hal::adc::{Channel, OneShot}; use crate::{ analog::{ADC1, ADC2}, + peripheral::PeripheralRef, peripherals::{APB_SARADC, SENS}, }; @@ -247,17 +248,20 @@ impl RegisterAccess for ADC2 { } } -pub struct ADC { - adc: PhantomData, +pub struct ADC<'d, ADC> { + _adc: PeripheralRef<'d, ADC>, attenuations: [Option; 10], active_channel: Option, } -impl ADC +impl<'d, ADCI> ADC<'d, ADCI> where ADCI: RegisterAccess, { - pub fn adc(_adc_instance: ADCI, config: AdcConfig) -> Result { + pub fn adc( + adc_instance: impl crate::peripheral::Peripheral

+ 'd, + config: AdcConfig, + ) -> Result { let sensors = unsafe { &*SENS::ptr() }; // Set reading and sampling resolution @@ -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, }; @@ -338,7 +342,7 @@ where } } -impl OneShot> for ADC +impl<'d, ADCI, WORD, PIN> OneShot> for ADC<'d, ADCI> where WORD: From, PIN: Channel, diff --git a/esp-hal-common/src/analog/mod.rs b/esp-hal-common/src/analog/mod.rs index 2eae5dea76a..9fe27559d30 100644 --- a/esp-hal-common/src/analog/mod.rs +++ b/esp-hal-common/src/analog/mod.rs @@ -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, @@ -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: (), }, } } @@ -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, @@ -84,14 +136,14 @@ cfg_if::cfg_if! { fn split(self) -> AvailableAnalog; } - impl SarAdcExt for APB_SARADC { + impl<'d, T: crate::peripheral::Peripheral

+ 'd> SarAdcExt for T { fn split(self) -> AvailableAnalog { AvailableAnalog { adc1: ADC1 { - _private: PhantomData, + _private: (), }, adc2: ADC2 { - _private: PhantomData, + _private: (), }, } } @@ -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, } @@ -118,11 +165,11 @@ cfg_if::cfg_if! { fn split(self) -> AvailableAnalog; } - impl SarAdcExt for APB_SARADC { + impl<'d, T: crate::peripheral::Peripheral

+ 'd> SarAdcExt for T { fn split(self) -> AvailableAnalog { AvailableAnalog { adc1: ADC1 { - _private: PhantomData, + _private: (), }, } } diff --git a/esp-hal-common/src/peripherals/esp32.rs b/esp-hal-common/src/peripherals/esp32.rs index d91ceeaba5a..9b213653bbd 100644 --- a/esp-hal-common/src/peripherals/esp32.rs +++ b/esp-hal-common/src/peripherals/esp32.rs @@ -76,5 +76,6 @@ mod peripherals { RTC_CNTL, TIMG0, TIMG1, + SENS, } } diff --git a/esp-hal-common/src/peripherals/esp32c2.rs b/esp-hal-common/src/peripherals/esp32c2.rs index 98662894589..1613e05427e 100644 --- a/esp-hal-common/src/peripherals/esp32c2.rs +++ b/esp-hal-common/src/peripherals/esp32c2.rs @@ -51,5 +51,6 @@ mod peripherals { DMA, RTC_CNTL, TIMG0, + APB_SARADC, } } diff --git a/esp-hal-common/src/peripherals/esp32c3.rs b/esp-hal-common/src/peripherals/esp32c3.rs index b44732156ce..2c6e955bd40 100644 --- a/esp-hal-common/src/peripherals/esp32c3.rs +++ b/esp-hal-common/src/peripherals/esp32c3.rs @@ -66,5 +66,6 @@ mod peripherals { RTC_CNTL, TIMG0, TIMG1, + APB_SARADC, } } diff --git a/esp-hal-common/src/peripherals/esp32s2.rs b/esp-hal-common/src/peripherals/esp32s2.rs index e82dc564003..7c4d42c03df 100644 --- a/esp-hal-common/src/peripherals/esp32s2.rs +++ b/esp-hal-common/src/peripherals/esp32s2.rs @@ -73,5 +73,7 @@ mod peripherals { RTC_CNTL, TIMG0, TIMG1, + APB_SARADC, + SENS, } } diff --git a/esp-hal-common/src/peripherals/esp32s3.rs b/esp-hal-common/src/peripherals/esp32s3.rs index 8ac126e0f1d..92248e320e3 100644 --- a/esp-hal-common/src/peripherals/esp32s3.rs +++ b/esp-hal-common/src/peripherals/esp32s3.rs @@ -89,5 +89,7 @@ mod peripherals { RTC_CNTL, TIMG0, TIMG1, + APB_SARADC, + SENS, } }