From 8058cafe5b5578ecd1c07726781544880e5afdc5 Mon Sep 17 00:00:00 2001 From: Andrew Yong Date: Tue, 7 Jul 2026 05:56:10 +0800 Subject: [PATCH] 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 Signed-off-by: Andrew Yong * 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 Signed-off-by: Andrew Yong --------- Signed-off-by: Andrew Yong Co-authored-by: Ben Meadors --- src/Power.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Power.cpp b/src/Power.cpp index d7c8f38ea..602b9a10c 100644 --- a/src/Power.cpp +++ b/src/Power.cpp @@ -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);