diff --git a/esp-hal/CHANGELOG.md b/esp-hal/CHANGELOG.md index 415e37b32..8e8e51a6c 100644 --- a/esp-hal/CHANGELOG.md +++ b/esp-hal/CHANGELOG.md @@ -48,6 +48,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `DmaTxBuffer::from_view` and `DmaRxBuffer::from_view` now return an object with type `DmaTx/RxBuffer::Final`. (#3923) - `i2c::master::Config::timeout` has been de-stabilized, and `i2c::master::Config::software_timeout`. (#3926) - The default values of `i2c::master::Config` timeouts have been changed to their maximum possible values. (#3926) +- Adc CHANNEL constant moved to trait function. (#3942) - `ShaDigest::finish` has been reimplemented to be properly non-blocking (#3948) - Replace Timer's `pub fn enable_interrupt(&mut self, enable: bool)` with `pub fn listen(&mut self)` and `pub fn unlisten(&mut self)` (#3933) - ESP32-S3: `PsramConfig::core_clock` is now an `Option` (#3974) diff --git a/esp-hal/src/analog/adc/esp32.rs b/esp-hal/src/analog/adc/esp32.rs index 5b61a23e6..4ca1aeb89 100644 --- a/esp-hal/src/analog/adc/esp32.rs +++ b/esp-hal/src/analog/adc/esp32.rs @@ -340,26 +340,29 @@ where /// This method takes an [AdcPin](super::AdcPin) reference, as it is /// expected that the ADC will be able to sample whatever channel /// underlies the pin. - pub fn read_oneshot(&mut self, _pin: &mut super::AdcPin) -> nb::Result + pub fn read_oneshot(&mut self, pin: &mut super::AdcPin) -> nb::Result where PIN: super::AdcChannel, { - if self.attenuations[PIN::CHANNEL as usize].is_none() { - panic!("Channel {} is not configured reading!", PIN::CHANNEL); + if self.attenuations[pin.pin.adc_channel() as usize].is_none() { + panic!( + "Channel {} is not configured reading!", + pin.pin.adc_channel() + ); } if let Some(active_channel) = self.active_channel { // There is conversion in progress: // - if it's for a different channel try again later // - if it's for the given channel, go ahead and check progress - if active_channel != PIN::CHANNEL { + if active_channel != pin.pin.adc_channel() { return Err(nb::Error::WouldBlock); } } else { // If no conversions are in progress, start a new one for given channel - self.active_channel = Some(PIN::CHANNEL); + self.active_channel = Some(pin.pin.adc_channel()); - ADCI::set_en_pad(PIN::CHANNEL); + ADCI::set_en_pad(pin.pin.adc_channel()); ADCI::clear_start_sar(); ADCI::set_start_sar(); diff --git a/esp-hal/src/analog/adc/mod.rs b/esp-hal/src/analog/adc/mod.rs index 7ae71cbed..7fdd09a58 100644 --- a/esp-hal/src/analog/adc/mod.rs +++ b/esp-hal/src/analog/adc/mod.rs @@ -131,7 +131,7 @@ impl AdcConfig { { // TODO revert this on drop pin.set_analog(crate::private::Internal); - self.attenuations[PIN::CHANNEL as usize] = Some(attenuation); + self.attenuations[pin.adc_channel() as usize] = Some(attenuation); AdcPin { pin, @@ -156,7 +156,7 @@ impl AdcConfig { { // TODO revert this on drop pin.set_analog(crate::private::Internal); - self.attenuations[PIN::CHANNEL as usize] = Some(attenuation); + self.attenuations[pin.adc_channel() as usize] = Some(attenuation); AdcPin { pin, @@ -194,7 +194,7 @@ pub trait CalibrationAccess: RegisterAccess { /// A helper trait to get the ADC channel of a compatible GPIO pin. pub trait AdcChannel { /// Channel number used by the ADC - const CHANNEL: u8; + fn adc_channel(&self) -> u8; } /// A trait abstracting over calibration methods. @@ -245,7 +245,9 @@ trait AdcCalEfuse { 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; + fn adc_channel(&self) -> u8 { + $ch + } } }; } diff --git a/esp-hal/src/analog/adc/riscv.rs b/esp-hal/src/analog/adc/riscv.rs index ca44a9d8d..94331889d 100644 --- a/esp-hal/src/analog/adc/riscv.rs +++ b/esp-hal/src/analog/adc/riscv.rs @@ -342,20 +342,23 @@ where PIN: super::AdcChannel, CS: super::AdcCalScheme, { - if self.attenuations[PIN::CHANNEL as usize].is_none() { - panic!("Channel {} is not configured reading!", PIN::CHANNEL); + if self.attenuations[pin.pin.adc_channel() as usize].is_none() { + panic!( + "Channel {} is not configured reading!", + pin.pin.adc_channel() + ); } if let Some(active_channel) = self.active_channel { // There is conversion in progress: // - if it's for a different channel try again later // - if it's for the given channel, go ahead and check progress - if active_channel != PIN::CHANNEL { + if active_channel != pin.pin.adc_channel() { return Err(nb::Error::WouldBlock); } } else { // If no conversions are in progress, start a new one for given channel - self.active_channel = Some(PIN::CHANNEL); + self.active_channel = Some(pin.pin.adc_channel()); // Set ADC unit calibration according used scheme for pin ADCI::set_init_code(pin.cal_scheme.adc_cal()); @@ -481,7 +484,7 @@ where PIN: super::AdcChannel, CS: super::AdcCalScheme, { - let channel = PIN::CHANNEL; + let channel = pin.pin.adc_channel(); if self.attenuations[channel as usize].is_none() { panic!("Channel {} is not configured reading!", channel); } diff --git a/esp-hal/src/analog/adc/xtensa.rs b/esp-hal/src/analog/adc/xtensa.rs index ce6210e4d..8f72e1cc3 100644 --- a/esp-hal/src/analog/adc/xtensa.rs +++ b/esp-hal/src/analog/adc/xtensa.rs @@ -441,12 +441,12 @@ where // There is conversion in progress: // - if it's for a different channel try again later // - if it's for the given channel, go ahead and check progress - if active_channel != PIN::CHANNEL { + if active_channel != pin.pin.adc_channel() { return Err(nb::Error::WouldBlock); } } else { // If no conversions are in progress, start a new one for given channel - self.active_channel = Some(PIN::CHANNEL); + self.active_channel = Some(pin.pin.adc_channel()); self.start_sample(pin); } @@ -482,7 +482,7 @@ where self.last_init_code = init_code; } - ADCI::set_en_pad(PIN::CHANNEL); + ADCI::set_en_pad(pin.pin.adc_channel()); ADCI::clear_start_sample(); ADCI::start_sample();