feat: Battery ADC driver (Issue #533) #542

Closed
sl-jetson wants to merge 0 commits from sl-controls/issue-533-battery-adc into main
Collaborator

Summary

  • DMA-based ADC3 continuous-scan driver (DMA2_Stream0/Ch2) sampling Vbat (PC1/IN11) and Ibat (PC3/IN13) at ~14 kSPS each with 4× hardware oversampling
  • IIR low-pass filter (α = 1/8, ~4 Hz cutoff at 100 Hz tick rate) for stable voltage readings
  • Voltage divider calibration with ±500 mV offset clamp and optional scale factor override
  • JLINK_TLM_BATTERY (0x82) telemetry: publishes 10-byte packed struct to Jetson at 1 Hz via existing jlink framing (CRC16-XModem)
  • Power management integration (Issue #467): power_mgmt_notify_battery() triggers STOP-mode sleep after 5 s sustained critical voltage (< 9.6 V 3S / < 12.8 V 4S)
  • 3S/4S cell count auto-detection at first valid reading; separate threshold sets for each

Test plan

  • Run test/test_battery_adc.c (27/27 passing on host)
  • Verify ADC3 DMA continuous conversion on bench: battery_adc_get_voltage_mv() tracks known voltage
  • Verify IIR filter smooths step input within ~40 ticks
  • Calibrate: apply known offset, verify battery_adc_get_raw_voltage_mv() vs get_voltage_mv() delta matches offset
  • Verify JLINK_TLM_BATTERY (0x82) frames received on Jetson at 1 Hz
  • Verify power_mgmt_notify_battery() triggers sleep after 5 s below 9.6 V
  • Confirm no conflict with existing battery.c single-shot reads (both use ADC3 — resolve by disabling battery.c ADC3 init when battery_adc is active)

🤖 Generated with Claude Code

## Summary - DMA-based ADC3 continuous-scan driver (DMA2_Stream0/Ch2) sampling Vbat (PC1/IN11) and Ibat (PC3/IN13) at ~14 kSPS each with 4× hardware oversampling - IIR low-pass filter (α = 1/8, ~4 Hz cutoff at 100 Hz tick rate) for stable voltage readings - Voltage divider calibration with ±500 mV offset clamp and optional scale factor override - JLINK_TLM_BATTERY (0x82) telemetry: publishes 10-byte packed struct to Jetson at 1 Hz via existing jlink framing (CRC16-XModem) - Power management integration (Issue #467): `power_mgmt_notify_battery()` triggers STOP-mode sleep after 5 s sustained critical voltage (< 9.6 V 3S / < 12.8 V 4S) - 3S/4S cell count auto-detection at first valid reading; separate threshold sets for each ## Test plan - [ ] Run `test/test_battery_adc.c` (27/27 passing on host) - [ ] Verify ADC3 DMA continuous conversion on bench: `battery_adc_get_voltage_mv()` tracks known voltage - [ ] Verify IIR filter smooths step input within ~40 ticks - [ ] Calibrate: apply known offset, verify `battery_adc_get_raw_voltage_mv()` vs `get_voltage_mv()` delta matches offset - [ ] Verify JLINK_TLM_BATTERY (0x82) frames received on Jetson at 1 Hz - [ ] Verify `power_mgmt_notify_battery()` triggers sleep after 5 s below 9.6 V - [ ] Confirm no conflict with existing battery.c single-shot reads (both use ADC3 — resolve by disabling battery.c ADC3 init when battery_adc is active) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
sl-jetson added 1 commit 2026-03-07 10:01:27 -05:00
STM32F7 ADC driver for battery voltage/current monitoring using
DMA-based continuous sampling, IIR low-pass filter, voltage divider
calibration, and USART telemetry to Jetson. Integrates with power
management for low-battery sleep (Issue #467).

Implementation:
- include/battery_adc.h: New driver header with calibration struct and
  public API (init, tick, get_voltage_mv, get_current_ma, calibrate,
  publish, check_pm, is_low, is_critical)
- src/battery_adc.c: ADC3 continuous-scan DMA (DMA2_Stream0/Ch2), 4x
  hardware oversampling of both Vbat (PC1/IN11) and Ibat (PC3/IN13),
  IIR LPF (alpha=1/8, cutoff ~4 Hz at 100 Hz tick rate), calibration
  with ±500 mV offset clamp, 3S/4S auto-detection, 1 Hz USART publish
- include/jlink.h + src/jlink.c: Add JLINK_TLM_BATTERY (0x82) telemetry
  type and jlink_tlm_battery_t (10-byte packed struct), implement
  jlink_send_battery_telemetry() using CRC16-XModem framing
- include/power_mgmt.h + src/power_mgmt.c: Add
  power_mgmt_notify_battery() — triggers STOP-mode sleep when Vbat
  sustains critical level (Issue #467)
- test/test_battery_adc.c: 27 unit tests (27/27 passing): voltage
  conversion, calibration offset/scale, IIR LPF convergence, SoC
  estimation (3S/4S), low/critical flags, PM notification timing,
  calibration reset, publish rate-limiting

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
sl-jetson closed this pull request 2026-03-07 13:47:04 -05:00

Pull request closed

Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: seb/saltylab-firmware#542
No description provided.