optimize at32f43x vbat and current more accurate when punch throttle - #15418
Conversation
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
|
Do you want to test this code? You can flash it directly from the Betaflight App:
WARNING: It may be unstable. Use only for testing! |
WalkthroughAT32 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. ChangesAT32 ADC Sampling
Estimated code review effort: 2 (Simple) | ~5 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
|
AI Generated comment Root cause is more specific than "a call was missing": 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 |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/platform/AT32/adc_at32f43x.c (1)
226-228: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winDocument the 4× oversampling trade-off.
The comment explains the longer sample time but not why it is paired with
ADC_OVERSAMPLE_RATIO_4andADC_OVERSAMPLE_SHIFT_2instead 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
📒 Files selected for processing (1)
src/platform/AT32/adc_at32f43x.c
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