Skip to content

Commit e15e837

Browse files
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 <[email protected]>
1 parent 779228e commit e15e837

File tree

5 files changed

+27
-18
lines changed

5 files changed

+27
-18
lines changed

esp-hal/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4848
- `DmaTxBuffer::from_view` and `DmaRxBuffer::from_view` now return an object with type `DmaTx/RxBuffer::Final`. (#3923)
4949
- `i2c::master::Config::timeout` has been de-stabilized, and `i2c::master::Config::software_timeout`. (#3926)
5050
- The default values of `i2c::master::Config` timeouts have been changed to their maximum possible values. (#3926)
51+
- Adc CHANNEL constant moved to trait function. (#3942)
5152
- `ShaDigest::finish` has been reimplemented to be properly non-blocking (#3948)
5253
- Replace Timer's `pub fn enable_interrupt(&mut self, enable: bool)` with `pub fn listen(&mut self)` and `pub fn unlisten(&mut self)` (#3933)
5354
- ESP32-S3: `PsramConfig::core_clock` is now an `Option` (#3974)

esp-hal/src/analog/adc/esp32.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,26 +340,29 @@ where
340340
/// This method takes an [AdcPin](super::AdcPin) reference, as it is
341341
/// expected that the ADC will be able to sample whatever channel
342342
/// underlies the pin.
343-
pub fn read_oneshot<PIN>(&mut self, _pin: &mut super::AdcPin<PIN, ADCI>) -> nb::Result<u16, ()>
343+
pub fn read_oneshot<PIN>(&mut self, pin: &mut super::AdcPin<PIN, ADCI>) -> nb::Result<u16, ()>
344344
where
345345
PIN: super::AdcChannel,
346346
{
347-
if self.attenuations[PIN::CHANNEL as usize].is_none() {
348-
panic!("Channel {} is not configured reading!", PIN::CHANNEL);
347+
if self.attenuations[pin.pin.adc_channel() as usize].is_none() {
348+
panic!(
349+
"Channel {} is not configured reading!",
350+
pin.pin.adc_channel()
351+
);
349352
}
350353

351354
if let Some(active_channel) = self.active_channel {
352355
// There is conversion in progress:
353356
// - if it's for a different channel try again later
354357
// - if it's for the given channel, go ahead and check progress
355-
if active_channel != PIN::CHANNEL {
358+
if active_channel != pin.pin.adc_channel() {
356359
return Err(nb::Error::WouldBlock);
357360
}
358361
} else {
359362
// If no conversions are in progress, start a new one for given channel
360-
self.active_channel = Some(PIN::CHANNEL);
363+
self.active_channel = Some(pin.pin.adc_channel());
361364

362-
ADCI::set_en_pad(PIN::CHANNEL);
365+
ADCI::set_en_pad(pin.pin.adc_channel());
363366

364367
ADCI::clear_start_sar();
365368
ADCI::set_start_sar();

esp-hal/src/analog/adc/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl<ADCI> AdcConfig<ADCI> {
131131
{
132132
// TODO revert this on drop
133133
pin.set_analog(crate::private::Internal);
134-
self.attenuations[PIN::CHANNEL as usize] = Some(attenuation);
134+
self.attenuations[pin.adc_channel() as usize] = Some(attenuation);
135135

136136
AdcPin {
137137
pin,
@@ -156,7 +156,7 @@ impl<ADCI> AdcConfig<ADCI> {
156156
{
157157
// TODO revert this on drop
158158
pin.set_analog(crate::private::Internal);
159-
self.attenuations[PIN::CHANNEL as usize] = Some(attenuation);
159+
self.attenuations[pin.adc_channel() as usize] = Some(attenuation);
160160

161161
AdcPin {
162162
pin,
@@ -194,7 +194,7 @@ pub trait CalibrationAccess: RegisterAccess {
194194
/// A helper trait to get the ADC channel of a compatible GPIO pin.
195195
pub trait AdcChannel {
196196
/// Channel number used by the ADC
197-
const CHANNEL: u8;
197+
fn adc_channel(&self) -> u8;
198198
}
199199

200200
/// A trait abstracting over calibration methods.
@@ -245,7 +245,9 @@ trait AdcCalEfuse {
245245
for_each_analog_function! {
246246
(($ch_name:ident, ADCn_CHm, $adc:literal, $ch:literal), $gpio:ident) => {
247247
impl $crate::analog::adc::AdcChannel for $crate::peripherals::$gpio<'_> {
248-
const CHANNEL: u8 = $ch;
248+
fn adc_channel(&self) -> u8 {
249+
$ch
250+
}
249251
}
250252
};
251253
}

esp-hal/src/analog/adc/riscv.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -342,20 +342,23 @@ where
342342
PIN: super::AdcChannel,
343343
CS: super::AdcCalScheme<ADCI>,
344344
{
345-
if self.attenuations[PIN::CHANNEL as usize].is_none() {
346-
panic!("Channel {} is not configured reading!", PIN::CHANNEL);
345+
if self.attenuations[pin.pin.adc_channel() as usize].is_none() {
346+
panic!(
347+
"Channel {} is not configured reading!",
348+
pin.pin.adc_channel()
349+
);
347350
}
348351

349352
if let Some(active_channel) = self.active_channel {
350353
// There is conversion in progress:
351354
// - if it's for a different channel try again later
352355
// - if it's for the given channel, go ahead and check progress
353-
if active_channel != PIN::CHANNEL {
356+
if active_channel != pin.pin.adc_channel() {
354357
return Err(nb::Error::WouldBlock);
355358
}
356359
} else {
357360
// If no conversions are in progress, start a new one for given channel
358-
self.active_channel = Some(PIN::CHANNEL);
361+
self.active_channel = Some(pin.pin.adc_channel());
359362

360363
// Set ADC unit calibration according used scheme for pin
361364
ADCI::set_init_code(pin.cal_scheme.adc_cal());
@@ -481,7 +484,7 @@ where
481484
PIN: super::AdcChannel,
482485
CS: super::AdcCalScheme<ADCI>,
483486
{
484-
let channel = PIN::CHANNEL;
487+
let channel = pin.pin.adc_channel();
485488
if self.attenuations[channel as usize].is_none() {
486489
panic!("Channel {} is not configured reading!", channel);
487490
}

esp-hal/src/analog/adc/xtensa.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,12 +441,12 @@ where
441441
// There is conversion in progress:
442442
// - if it's for a different channel try again later
443443
// - if it's for the given channel, go ahead and check progress
444-
if active_channel != PIN::CHANNEL {
444+
if active_channel != pin.pin.adc_channel() {
445445
return Err(nb::Error::WouldBlock);
446446
}
447447
} else {
448448
// If no conversions are in progress, start a new one for given channel
449-
self.active_channel = Some(PIN::CHANNEL);
449+
self.active_channel = Some(pin.pin.adc_channel());
450450

451451
self.start_sample(pin);
452452
}
@@ -482,7 +482,7 @@ where
482482
self.last_init_code = init_code;
483483
}
484484

485-
ADCI::set_en_pad(PIN::CHANNEL);
485+
ADCI::set_en_pad(pin.pin.adc_channel());
486486

487487
ADCI::clear_start_sample();
488488
ADCI::start_sample();

0 commit comments

Comments
 (0)