fix(power): correct charge/power detection preprocessor bugs (#10906)

* fix(power): check EXT_CHRG_DETECT with defined(), not truthiness

#elif EXT_CHRG_DETECT tests the macro's numeric value rather than
whether it's defined. Every existing board happens to assign it to a
nonzero pin, so this never misfired, but a board using pin 0 (e.g.
PA0 on STM32) would silently skip this branch even though the pin is
defined and configured.

Assisted-by: Claude Sonnet 5 <noreply@anthropic.com>
Signed-off-by: Andrew Yong <me@ndoo.sg>

* fix(power): consider EXT_CHRG_DETECT in isVbusIn()

isVbusIn() used EXT_PWR_DETECT alone. On boards where that pin is a
charge-complete/standby signal rather than a continuous power-present
signal, isVbusIn() reports false for the entire duration of an active
charge. If EXT_CHRG_DETECT is also defined, OR it in: charging can
only happen when power is present, so this only adds correct true
results and is a no-op for boards where EXT_PWR_DETECT already means
"power present" continuously.

Assisted-by: Claude Sonnet 5 <noreply@anthropic.com>
Signed-off-by: Andrew Yong <me@ndoo.sg>

---------

Signed-off-by: Andrew Yong <me@ndoo.sg>
Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
This commit is contained in:
Andrew Yong
2026-07-06 16:56:10 -05:00
committed by GitHub
co-authored by GitHub Ben Meadors
parent 24c1dccf00
commit 8058cafe5b
+11 -2
View File
@@ -551,7 +551,16 @@ class AnalogBatteryLevel : public HasBatteryLevel
return sgm41562->isInputPowerGood();
#endif
#ifdef EXT_PWR_DETECT
return digitalRead(EXT_PWR_DETECT) == EXT_PWR_DETECT_VALUE;
if (digitalRead(EXT_PWR_DETECT) == EXT_PWR_DETECT_VALUE)
return true;
#ifdef EXT_CHRG_DETECT
// EXT_PWR_DETECT alone may not catch active charging (e.g. a charge-complete
// pin that only asserts once the battery is full) - CHRG being active implies
// power is present regardless.
return digitalRead(EXT_CHRG_DETECT) == EXT_CHRG_DETECT_VALUE;
#else
return false;
#endif
// technically speaking this should work for all(?) NRF52 boards
// but needs testing across multiple devices. NRF52 USB would not even work if
@@ -577,7 +586,7 @@ class AnalogBatteryLevel : public HasBatteryLevel
#endif
#if defined(ELECROW_ThinkNode_M6)
return digitalRead(EXT_CHRG_DETECT) == EXT_CHRG_DETECT_VALUE || isVbusIn();
#elif EXT_CHRG_DETECT
#elif defined(EXT_CHRG_DETECT)
return digitalRead(EXT_CHRG_DETECT) == EXT_CHRG_DETECT_VALUE;
#elif defined(BATTERY_CHARGING_INV)
return !digitalRead(BATTERY_CHARGING_INV);