From e579b1b1be123f1d83cacab9c2e62f954bfe144e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Thu, 7 Aug 2025 17:16:12 +0200 Subject: [PATCH] Implement ADC trait based on metadata (#3911) --- esp-hal/CHANGELOG.md | 2 +- esp-hal/src/analog/adc/esp32.rs | 30 ---------------- esp-hal/src/analog/adc/mod.rs | 18 ++++------ esp-hal/src/analog/adc/riscv.rs | 60 -------------------------------- esp-hal/src/analog/adc/xtensa.rs | 32 ----------------- 5 files changed, 7 insertions(+), 135 deletions(-) diff --git a/esp-hal/CHANGELOG.md b/esp-hal/CHANGELOG.md index efd46e4e9..648108b43 100644 --- a/esp-hal/CHANGELOG.md +++ b/esp-hal/CHANGELOG.md @@ -32,7 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - PSRAM on ESP32-S2 (#3811) - WDT now allows configuring longer timeouts (#3816) - `ADC2` now cannot be used simultaneously with `radio` on ESP32 (#3876) -- Switched GPIO32 and GPIO33 ADC channel numbers (#3908) +- Switched GPIO32 and GPIO33 ADC channel numbers (#3908, #3911) ### Removed diff --git a/esp-hal/src/analog/adc/esp32.rs b/esp-hal/src/analog/adc/esp32.rs index e9520c2e7..5b61a23e6 100644 --- a/esp-hal/src/analog/adc/esp32.rs +++ b/esp-hal/src/analog/adc/esp32.rs @@ -397,36 +397,6 @@ impl Adc<'_, ADC1, crate::Blocking> { } } -mod adc_implementation { - crate::analog::adc::impl_adc_interface! { - ADC1 [ - (GPIO36<'_>, 0), // Alt. name: SENSOR_VP - (GPIO37<'_>, 1), // Alt. name: SENSOR_CAPP - (GPIO38<'_>, 2), // Alt. name: SENSOR_CAPN - (GPIO39<'_>, 3), // Alt. name: SENSOR_VN - (GPIO32<'_>, 4), // Alt. name: 32K_XP - (GPIO33<'_>, 5), // Alt. name: 32K_XN - (GPIO34<'_>, 6), // Alt. name: VDET_1 - (GPIO35<'_>, 7), // Alt. name: VDET_2 - ] - } - - crate::analog::adc::impl_adc_interface! { - ADC2 [ - (GPIO4<'_>, 0), - (GPIO0<'_>, 1), - (GPIO2<'_>, 2), - (GPIO15<'_>, 3), // Alt. name: MTDO - (GPIO13<'_>, 4), // Alt. name: MTCK - (GPIO12<'_>, 5), // Alt. name: MTDI - (GPIO14<'_>, 6), // Alt. name: MTMS - (GPIO27<'_>, 7), - (GPIO25<'_>, 8), - (GPIO26<'_>, 9), - ] - } -} - impl Drop for ADC2<'_> { fn drop(&mut self) { release_adc2(private::Internal); diff --git a/esp-hal/src/analog/adc/mod.rs b/esp-hal/src/analog/adc/mod.rs index 2934c428d..7ae71cbed 100644 --- a/esp-hal/src/analog/adc/mod.rs +++ b/esp-hal/src/analog/adc/mod.rs @@ -242,16 +242,10 @@ trait AdcCalEfuse { fn cal_code(atten: Attenuation) -> Option; } -macro_rules! impl_adc_interface { - ($adc:ident [ - $( ($pin:ident<'_>, $channel:expr) ,)+ - ]) => { - $( - impl $crate::analog::adc::AdcChannel for $crate::peripherals::$pin<'_> { - const CHANNEL: u8 = $channel; - } - )+ - } +for_each_analog_function! { + (($ch_name:ident, ADCn_CHm, $adc:literal, $ch:literal), $gpio:ident) => { + impl $crate::analog::adc::AdcChannel for $crate::peripherals::$gpio<'_> { + const CHANNEL: u8 = $ch; + } + }; } - -pub(crate) use impl_adc_interface; diff --git a/esp-hal/src/analog/adc/riscv.rs b/esp-hal/src/analog/adc/riscv.rs index 77c8fa90b..25c24d29a 100644 --- a/esp-hal/src/analog/adc/riscv.rs +++ b/esp-hal/src/analog/adc/riscv.rs @@ -449,66 +449,6 @@ impl super::AdcCalEfuse for crate::peripherals::ADC2<'_> { } } -#[cfg(esp32c2)] -mod adc_implementation { - crate::analog::adc::impl_adc_interface! { - ADC1 [ - (GPIO0<'_>, 0), - (GPIO1<'_>, 1), - (GPIO2<'_>, 2), - (GPIO3<'_>, 3), - (GPIO4<'_>, 4), - ] - } -} - -#[cfg(esp32c3)] -mod adc_implementation { - crate::analog::adc::impl_adc_interface! { - ADC1 [ - (GPIO0<'_>, 0), - (GPIO1<'_>, 1), - (GPIO2<'_>, 2), - (GPIO3<'_>, 3), - (GPIO4<'_>, 4), - ] - } - - crate::analog::adc::impl_adc_interface! { - ADC2 [ - (GPIO5<'_>, 0), - ] - } -} - -#[cfg(esp32c6)] -mod adc_implementation { - crate::analog::adc::impl_adc_interface! { - ADC1 [ - (GPIO0<'_>, 0), - (GPIO1<'_>, 1), - (GPIO2<'_>, 2), - (GPIO3<'_>, 3), - (GPIO4<'_>, 4), - (GPIO5<'_>, 5), - (GPIO6<'_>, 6), - ] - } -} - -#[cfg(esp32h2)] -mod adc_implementation { - crate::analog::adc::impl_adc_interface! { - ADC1 [ - (GPIO1<'_>, 0), - (GPIO2<'_>, 1), - (GPIO3<'_>, 2), - (GPIO4<'_>, 3), - (GPIO5<'_>, 4), - ] - } -} - impl<'d, ADCI> Adc<'d, ADCI, Async> where ADCI: RegisterAccess + 'd, diff --git a/esp-hal/src/analog/adc/xtensa.rs b/esp-hal/src/analog/adc/xtensa.rs index 2b49a7316..ce6210e4d 100644 --- a/esp-hal/src/analog/adc/xtensa.rs +++ b/esp-hal/src/analog/adc/xtensa.rs @@ -518,35 +518,3 @@ impl super::AdcCalEfuse for crate::peripherals::ADC2<'_> { Efuse::rtc_calib_cal_code(2, atten) } } - -mod adc_implementation { - crate::analog::adc::impl_adc_interface! { - ADC1 [ - (GPIO1<'_>, 0), - (GPIO2<'_>, 1), - (GPIO3<'_>, 2), - (GPIO4<'_>, 3), - (GPIO5<'_>, 4), - (GPIO6<'_>, 5), - (GPIO7<'_>, 6), - (GPIO8<'_>, 7), - (GPIO9<'_>, 8), - (GPIO10<'_>, 9), - ] - } - - crate::analog::adc::impl_adc_interface! { - ADC2 [ - (GPIO11<'_>, 0), - (GPIO12<'_>, 1), - (GPIO13<'_>, 2), - (GPIO14<'_>, 3), - (GPIO15<'_>, 4), - (GPIO16<'_>, 5), - (GPIO17<'_>, 6), - (GPIO18<'_>, 7), - (GPIO19<'_>, 8), - (GPIO20<'_>, 9), - ] - } -}