Sitelet https://github.com/betaflight/betaflight/pull/15418
Skip to content

optimize at32f43x vbat and current more accurate when punch throttle - #15418

Merged
blckmn merged 3 commits into
betaflight:masterfrom
shanggl:at32f43x_adc_optimize
Jul 22, 2026
Merged

blckmn merged 3 commits into
betaflight:masterfrom
shanggl:at32f43x_adc_optimize

Conversation

@shanggl

@shanggl shanggl commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

Issue :

The ADC on the AT32F435 gives pretty poor readings – VBAT and current jump around a lot when you punch the throttle. Two issues:

Sample time was too short (ADC_SAMPLETIME_92_5). The cap doesn't get enough time to charge, especially on higher‑impedance inputs, so readings come out low and noisy.

Oversampling was configured (64×) but never enabled – adc_ordinary_oversample_enable() was missing, so all that setup did nothing.

What changed

Sample time change up to ADC_SAMPLETIME_640_5 (640.5 cycles). longer and more accure
Enable ADC oversample , it is just configed but not enable before

What we expect

VBAT should be within ±0.1 V of a multimeter.

Current should be steady at hover, no periodic glitches.

Temperature sensor shouldn't get worse.

Test results

Tested on NeutronRC F435 SE and Mini boards. Sample rate went from ~1.7 kHz down to ~1.1 kHz – still plenty for slow signals like battery voltage, current, and temp. OSD voltage looks solid in flight, no impact on handling.
One extra check: tweaking the scale factor from 110 to 112 gave a voltage difference of less than 0.01 V, so the calibration is now really tight.

Summary by CodeRabbit

  • Bug Fixes
    • Improved ADC measurement reliability on supported Artery hardware by adjusting sampling time and oversampling configuration.
    • Updated the sampling/oversampling balance to better match device sampling limitations and accuracy requirements.

AT32F43x ADC: enable oversampling and increase sample time for better accuracy

  - Increase sample time from 92.5 to 640.5 ADC cycles
  - Enable adc_ordinary_oversample (was missing)
  - Adjust oversampling from 64x/6-bit shift to 4x/2-bit shift
  - Longer per-sample time compensates for reduced oversampling ratio
@github-actions

Copy link
Copy Markdown

Do you want to test this code? You can flash it directly from the Betaflight App:

  • Simply put #15418 (this pull request number) in the Select commit field in the Firmware Flasher tab (you need to Enable expert mode, Show release candidates and Development).

WARNING: It may be unstable. Use only for testing!

@coderabbitai

coderabbitai Bot commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

AT32 ADC initialization now uses a 640.5-cycle sample time and ordinary 4× oversampling with a 2-bit shift, replacing the previous 92.5-cycle sample time and 64× oversampling configuration.

Changes

AT32 ADC Sampling

Layer / File(s) Summary
ADC initialization configuration
src/platform/AT32/adc_at32f43x.c
Updates adcInit documentation, increases the ADC sample time, and changes the oversampling ratio and shift settings.

Estimated code review effort: 2 (Simple) | ~5 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title matches the main change: improving AT32F43x VBAT/current ADC accuracy during throttle changes.
Description check ✅ Passed The description clearly explains the issue, the configuration changes, expected behavior, and test results.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@nerdCopter

Copy link
Copy Markdown
Member

AI Generated comment

Root cause is more specific than "a call was missing": adc_ordinary_oversample_enable() and adc_oversample_ratio_shift_set() both write to the same physical register, ADC_OVSP at offset 0x80 (at32f435_437_adc.h). Bit 0 (oosen) is the master enable for oversampling; bits [4:2]/[8:5] (osrsel/osssel) are the ratio/shift fields the pre-PR code did set, to 64x/shift-6. Since hardware only applies osrsel/osssel when oosen is asserted, and nothing in the old code ever wrote oosen, the existing adc_oversample_ratio_shift_set(..., ADC_OVERSAMPLE_RATIO_64, ADC_OVERSAMPLE_SHIFT_6) call was writing to register fields the peripheral was never gated to use — dead configuration, not just an absent one. (I couldn't confirm from the reference manual whether oosen happens to reset to 1 out of POR, which would be the one scenario where the old 64x/shift-6 setting had any effect; adc_reset() does run a full peripheral reset immediately before configuration either way.) This matches the reported symptom — noisy VBAT/current under load — exactly what you'd expect from single-sample conversion with no averaging applied despite the ratio/shift being configured.

On sample time: 640.5 cycles is the maximum value in the AT32 sample-time enum (0x07 of 8 steps), not an outlier choice. Betaflight's STM32 targets already max out sample time for this same channel group (vbat/current/rssi/external) — F4/F7 use their ceiling of 480 cycles, H7 uses 810.5 cycles for internal channels, and G4 uses exactly 640.5 cycles for external channels, identical to this PR's value.

Given that G4 pairs 640.5-cycle sampling with no additional oversampling stacked on top, dropping the ratio from the previously-configured (but never-active) 64x down to 4x is consistent with how the rest of the codebase balances sample time against oversampling for this channel class, rather than an unexplained reduction from the original 64x intent. Worth stating that reasoning explicitly in the PR description for future maintainers.

Minor: the Emsr: tag in the added code comment is a personal signature inline in source — Betaflight comments don't generally attribute to individual contributors; consider dropping the name and keeping just the technical rationale.

@haslinghuis haslinghuis moved this to Bugfix in 2026.6.0 Jul 15, 2026
@haslinghuis haslinghuis added this to the 2026.6 milestone Jul 15, 2026
Comment thread src/platform/AT32/adc_at32f43x.c Outdated

@coderabbitai coderabbitai Bot 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.

🧹 Nitpick comments (1)
src/platform/AT32/adc_at32f43x.c (1)

226-228: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Document the 4× oversampling trade-off.

The comment explains the longer sample time but not why it is paired with ADC_OVERSAMPLE_RATIO_4 and ADC_OVERSAMPLE_SHIFT_2 instead of the previous 64×/6-bit configuration. Add a brief rationale covering the accuracy-versus-sample-rate trade-off.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/platform/AT32/adc_at32f43x.c` around lines 226 - 228, Update the ADC
configuration comment near ADC_OVERSAMPLE_RATIO_4 and ADC_OVERSAMPLE_SHIFT_2 to
briefly explain choosing 4× oversampling over the previous 64×/6-bit setting,
emphasizing the balance between improved accuracy and preserving sample rate for
slow-changing inputs.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/platform/AT32/adc_at32f43x.c`:
- Around line 226-228: Update the ADC configuration comment near
ADC_OVERSAMPLE_RATIO_4 and ADC_OVERSAMPLE_SHIFT_2 to briefly explain choosing 4×
oversampling over the previous 64×/6-bit setting, emphasizing the balance
between improved accuracy and preserving sample rate for slow-changing inputs.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: b5066c0f-aee8-43a1-b02c-3271f60afc74

📥 Commits

Reviewing files that changed from the base of the PR and between f162a82 and 544915c.

📒 Files selected for processing (1)
  • src/platform/AT32/adc_at32f43x.c

@blckmn
blckmn merged commit a82fdb1 into betaflight:master Jul 22, 2026
53 checks passed
@github-project-automation github-project-automation Bot moved this from Bugfix to Done in 2026.6.0 Jul 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants