Skip to content

feat(adc): Update to support newer chips; improve API consistency; improve continuous adc performance; remove warnings#668

Merged
finger563 merged 7 commits into
mainfrom
feat/adc-improvements
Jul 15, 2026
Merged

feat(adc): Update to support newer chips; improve API consistency; improve continuous adc performance; remove warnings#668
finger563 merged 7 commits into
mainfrom
feat/adc-improvements

Conversation

@finger563

Copy link
Copy Markdown
Contributor

Description

  • ContinuousAdc: select output format for all chips, fix destructor
  • ContinuousAdc: Validate channels (unit / channel counts, DMA-capable units)
    and the aggregate sample frequency against the chip's SOC capabilities with
    clear errors instead of an opaque abort
  • OneshotAdc: fix calibration and channel validation, add optional convert_mode
  • OneshotAdc: add API wrappers get_mv() and get_raw() to match ContinuousAdc
  • Example: reorder continuous/oneshot sections, add Kconfig for board presets, use ADC1 channels on Tab5 (ESP32-P4)
  • ContinuousAdc: Add rate-limited logging of invalid samples in continuous mode, and log the raw unit/channel/data fields of any sample that parses but does not match a configured channel
  • ContinuousAdc: Remove per-sample hashing and allocation in the continuous ADC hot loop
  • CI: build example for esp32s3 and esp32p4 in addition to esp32
  • Fix IDF 6 deprecation warning for adc_continuous_config_t::format
  • Add warning for ADC2-only continuous mode on ESP32-P4
  • Update README and class docs to reflect the above changes

Motivation and Context

  • Allows the continuous ADC example to work on ESP32-P4 and other newer chips
  • Improves the OneshotAdc API to be more equivalent to ContinuousAdc, and fixes several bugs in both classes
  • Improves validation and error reporting for both classes, and adds a warning for ADC2-only continuous mode on ESP32-P4
  • Improves performance of the continuous ADC hot loop by removing per-sample hashing and allocation

How has this been tested?

  • Build and run adc/example on ESP32, ESP32-S3, and ESP32-P4 (M5Stack Tab5) with the example preset for each board; verify that the continuous ADC section produces plausible readings and that the oneshot section produces correct calibrated voltages

Screenshots (if appropriate, e.g. schematic, board, console logs, lab pictures):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update
  • Hardware (schematic, board, system design) change
  • Software change

Checklist:

  • My change requires a change to the documentation.
  • I have added / updated the documentation related to this change via either README or WIKI

Software

  • I have added tests to cover my changes.
  • I have updated the .github/workflows/build.yml file to add my new test to the automated cloud build github action.
  • All new and existing tests passed.
  • My code follows the code style of this project.

Copilot AI review requested due to automatic review settings July 15, 2026 14:48
@github-actions

github-actions Bot commented Jul 15, 2026

Copy link
Copy Markdown

✅Static analysis result - no issues found! ✅

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot couldn't run its full agentic review because no GitHub Actions runner was available. Make sure your repository has a runner available to run Copilot's review, or add a copilot-setup-steps.yml file specifying one with the runs-on attribute. See the docs for more details.

This PR updates the ADC component to support newer ESP chips (notably ESP32-P4), align Oneshot/Continuous APIs, improve continuous-mode performance, and replace abort-style failures with clearer validation/logging.

Changes:

  • OneshotAdc: add get_raw() / get_mv() wrappers, fix per-attenuation calibration handling, and tighten channel validation.
  • ContinuousAdc: derive/validate conversion mode and aggregate sampling rate per chip, remove per-sample hashing/allocations, and add rate-limited logging for invalid/unexpected samples.
  • Example/CI: add Kconfig-driven board presets, reorder example sections for ESP32-P4, and build the example for esp32/esp32s3/esp32p4 in CI.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
components/adc/include/oneshot_adc.hpp Per-attenuation calibration, new API wrappers, improved validation/logging.
components/adc/include/continuous_adc.hpp Chip-aware validation/format selection, dense indexing for hot loop performance, safer shutdown behavior.
components/adc/include/adc_types.hpp Mark comparison operators as optionally unused to reduce warnings.
components/adc/example/main/adc_example.cpp Kconfig-driven unit/channel selection and ESP32-P4 ordering workaround.
components/adc/example/main/Kconfig.projbuild Add board presets + custom configuration menu for unit/channels.
components/adc/example/README.md Document new configuration workflow and board presets.
.github/workflows/build.yml Add ADC example builds for esp32s3 and esp32p4 targets.

Comment thread components/adc/include/oneshot_adc.hpp
Comment thread components/adc/include/continuous_adc.hpp Outdated
Comment thread components/adc/include/continuous_adc.hpp
Comment thread components/adc/include/continuous_adc.hpp
Comment thread components/adc/include/continuous_adc.hpp
Comment thread components/adc/example/README.md
@finger563 finger563 self-assigned this Jul 15, 2026
@finger563 finger563 added enhancement New feature or request adc labels Jul 15, 2026
@finger563 finger563 force-pushed the feat/adc-improvements branch from b71bd4f to dfca031 Compare July 15, 2026 16:04
finger563 and others added 4 commits July 15, 2026 11:15
- OneshotAdc / ContinuousAdc destructors are now safe when init() fails
  or returns early: the driver handles are null-initialized and guarded,
  and ContinuousAdc no longer creates/starts its reader task (start()
  errors cleanly) when construction failed
- ContinuousAdc::init() now fails fast: validation errors (invalid unit
  / channel, non-DMA-capable unit, aggregate sample frequency out of
  range) accumulate and abort initialization before any driver call, so
  the first reported error is the root cause instead of a later opaque
  ESP_ERROR_CHECK abort
- Encode the 0-based adc_unit_t assumption (ADC_UNIT_1 == 0, matching
  the SOC_ADC_* capability macros and the DMA output unit field) with a
  static_assert, and size the id lookup table from SOC_ADC_PERIPH_NUM
  instead of a hardcoded unit count
- Fix compilation on single-ADC chips (C2/C6/H2/...), whose type2 DMA
  output data has no unit field (pre-existing break, surfaced by the new
  SOC-based sizing work); add esp32c6 to the example CI matrix to keep
  it covered

Verified: example clean-builds for esp32, esp32p4, and esp32c6.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…of both classes

Thread-safety review of both classes. OneshotAdc was already thread-safe
(all reads serialize on its recursive mutex; remaining state is
immutable after construction) - documented as such. ContinuousAdc had
three gaps, now fixed:

- start()/stop() had a check-then-act race: concurrent callers could
  both pass the running_ check and double-start (or double-stop) the
  driver, turning a driver error into an ESP_ERROR_CHECK abort. Both
  methods now serialize on a control mutex.
- The reader task could wake and read from a driver that another task
  had just stopped, producing the (benign but noisy) "driver is already
  stopped" error seen at every stop/destruction; the task now skips the
  read when not running.
- task_handle_ is written by the reader task and read by the
  conversion-done ISR and the destructor; it is now std::atomic (the ISR
  does a lock-free load).

Verified: example clean-builds for esp32, esp32p4, and esp32c6.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…not BOTH

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Document the two multi-unit continuous conversion modes in both the
ContinuousAdc class docs and the continuous_adc rst page: ALTER (the
derived default) performs one conversion per trigger alternating between
units, keeping every channel at ~sample_rate_hz for any channel mix -
right for general multi-channel monitoring; BOTH converts on both units
simultaneously in lockstep - right when relative timing matters (e.g.
simultaneous voltage + current for instantaneous power), with a note
that the effective per-channel rate is then higher than sample_rate_hz.
Includes example configurations for each.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@finger563 finger563 merged commit 7febaee into main Jul 15, 2026
129 of 130 checks passed
@finger563 finger563 deleted the feat/adc-improvements branch July 15, 2026 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

adc enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants