Move adc channel from associated constant to function (#3942) (#3943)

* Move adc channel from associated constant to function

* Move adc channel from associated constant to function

* Syntax cleanup

* RISCV and Format fixes

* FIx xtensa

---------

Co-authored-by: Juraj Sadel <juraj.sadel@espressif.com>
This commit is contained in:
millibyte-products 2025-09-08 09:11:44 -04:00 committed by GitHub
parent 779228ef28
commit e15e837246
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 27 additions and 18 deletions

View File

@ -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) - `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) - `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) - 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) - `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) - 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) - ESP32-S3: `PsramConfig::core_clock` is now an `Option` (#3974)

View File

@ -340,26 +340,29 @@ where
/// This method takes an [AdcPin](super::AdcPin) reference, as it is /// This method takes an [AdcPin](super::AdcPin) reference, as it is
/// expected that the ADC will be able to sample whatever channel /// expected that the ADC will be able to sample whatever channel
/// underlies the pin. /// underlies the pin.
pub fn read_oneshot<PIN>(&mut self, _pin: &mut super::AdcPin<PIN, ADCI>) -> nb::Result<u16, ()> pub fn read_oneshot<PIN>(&mut self, pin: &mut super::AdcPin<PIN, ADCI>) -> nb::Result<u16, ()>
where where
PIN: super::AdcChannel, PIN: super::AdcChannel,
{ {
if self.attenuations[PIN::CHANNEL as usize].is_none() { if self.attenuations[pin.pin.adc_channel() as usize].is_none() {
panic!("Channel {} is not configured reading!", PIN::CHANNEL); panic!(
"Channel {} is not configured reading!",
pin.pin.adc_channel()
);
} }
if let Some(active_channel) = self.active_channel { if let Some(active_channel) = self.active_channel {
// There is conversion in progress: // There is conversion in progress:
// - if it's for a different channel try again later // - if it's for a different channel try again later
// - if it's for the given channel, go ahead and check progress // - 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); return Err(nb::Error::WouldBlock);
} }
} else { } else {
// If no conversions are in progress, start a new one for given channel // 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::clear_start_sar();
ADCI::set_start_sar(); ADCI::set_start_sar();

View File

@ -131,7 +131,7 @@ impl<ADCI> AdcConfig<ADCI> {
{ {
// TODO revert this on drop // TODO revert this on drop
pin.set_analog(crate::private::Internal); pin.set_analog(crate::private::Internal);
self.attenuations[PIN::CHANNEL as usize] = Some(attenuation); self.attenuations[pin.adc_channel() as usize] = Some(attenuation);
AdcPin { AdcPin {
pin, pin,
@ -156,7 +156,7 @@ impl<ADCI> AdcConfig<ADCI> {
{ {
// TODO revert this on drop // TODO revert this on drop
pin.set_analog(crate::private::Internal); pin.set_analog(crate::private::Internal);
self.attenuations[PIN::CHANNEL as usize] = Some(attenuation); self.attenuations[pin.adc_channel() as usize] = Some(attenuation);
AdcPin { AdcPin {
pin, pin,
@ -194,7 +194,7 @@ pub trait CalibrationAccess: RegisterAccess {
/// A helper trait to get the ADC channel of a compatible GPIO pin. /// A helper trait to get the ADC channel of a compatible GPIO pin.
pub trait AdcChannel { pub trait AdcChannel {
/// Channel number used by the ADC /// Channel number used by the ADC
const CHANNEL: u8; fn adc_channel(&self) -> u8;
} }
/// A trait abstracting over calibration methods. /// A trait abstracting over calibration methods.
@ -245,7 +245,9 @@ trait AdcCalEfuse {
for_each_analog_function! { for_each_analog_function! {
(($ch_name:ident, ADCn_CHm, $adc:literal, $ch:literal), $gpio:ident) => { (($ch_name:ident, ADCn_CHm, $adc:literal, $ch:literal), $gpio:ident) => {
impl $crate::analog::adc::AdcChannel for $crate::peripherals::$gpio<'_> { impl $crate::analog::adc::AdcChannel for $crate::peripherals::$gpio<'_> {
const CHANNEL: u8 = $ch; fn adc_channel(&self) -> u8 {
$ch
}
} }
}; };
} }

View File

@ -342,20 +342,23 @@ where
PIN: super::AdcChannel, PIN: super::AdcChannel,
CS: super::AdcCalScheme<ADCI>, CS: super::AdcCalScheme<ADCI>,
{ {
if self.attenuations[PIN::CHANNEL as usize].is_none() { if self.attenuations[pin.pin.adc_channel() as usize].is_none() {
panic!("Channel {} is not configured reading!", PIN::CHANNEL); panic!(
"Channel {} is not configured reading!",
pin.pin.adc_channel()
);
} }
if let Some(active_channel) = self.active_channel { if let Some(active_channel) = self.active_channel {
// There is conversion in progress: // There is conversion in progress:
// - if it's for a different channel try again later // - if it's for a different channel try again later
// - if it's for the given channel, go ahead and check progress // - 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); return Err(nb::Error::WouldBlock);
} }
} else { } else {
// If no conversions are in progress, start a new one for given channel // 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 // Set ADC unit calibration according used scheme for pin
ADCI::set_init_code(pin.cal_scheme.adc_cal()); ADCI::set_init_code(pin.cal_scheme.adc_cal());
@ -481,7 +484,7 @@ where
PIN: super::AdcChannel, PIN: super::AdcChannel,
CS: super::AdcCalScheme<ADCI>, CS: super::AdcCalScheme<ADCI>,
{ {
let channel = PIN::CHANNEL; let channel = pin.pin.adc_channel();
if self.attenuations[channel as usize].is_none() { if self.attenuations[channel as usize].is_none() {
panic!("Channel {} is not configured reading!", channel); panic!("Channel {} is not configured reading!", channel);
} }

View File

@ -441,12 +441,12 @@ where
// There is conversion in progress: // There is conversion in progress:
// - if it's for a different channel try again later // - if it's for a different channel try again later
// - if it's for the given channel, go ahead and check progress // - 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); return Err(nb::Error::WouldBlock);
} }
} else { } else {
// If no conversions are in progress, start a new one for given channel // 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); self.start_sample(pin);
} }
@ -482,7 +482,7 @@ where
self.last_init_code = init_code; 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::clear_start_sample();
ADCI::start_sample(); ADCI::start_sample();