diff --git a/src/Power.cpp b/src/Power.cpp index 26b9615254e..c43edc9004d 100644 --- a/src/Power.cpp +++ b/src/Power.cpp @@ -94,6 +94,20 @@ static const adc_atten_t atten = ADC_ATTENUATION; #endif #endif // BATTERY_PIN && ARCH_ESP32 +#ifdef EXT_PWR_DETECT +#ifndef EXT_PWR_DETECT_MODE +#define EXT_PWR_DETECT_MODE INPUT +// If using internal pull resistors, we can infer EXT_PWR_DETECT_VALUE +#elif EXT_PWR_DETECT_MODE == INPUT_PULLUP +#define EXT_PWR_DETECT_VALUE LOW +#elif EXT_PWR_DETECT_MODE == INPUT_PULLDOWN +#define EXT_PWR_DETECT_VALUE HIGH +#endif +#ifndef EXT_PWR_DETECT_VALUE +#define EXT_PWR_DETECT_VALUE HIGH +#endif +#endif + #ifdef EXT_CHRG_DETECT #ifndef EXT_CHRG_DETECT_MODE static const uint8_t ext_chrg_detect_mode = INPUT; @@ -469,28 +483,14 @@ class AnalogBatteryLevel : public HasBatteryLevel virtual bool isBatteryConnect() override { return getBatteryPercent() != -1; } #endif - /// If we see a battery voltage higher than physics allows - assume charger is - /// pumping in power On some boards we don't have the power management chip - /// (like AXPxxxx) so we use EXT_PWR_DETECT GPIO pin to detect external power - /// source + // Detect if an external power source is connected if we don’t have a PMIC; + // Firstly prefer EXT_PWR_DETECT GPIO if available, + // secondly try an nRF52-specific routine on some variants, + // lastly provide a fallback to indicate external power when fully charged. virtual bool isVbusIn() override { #ifdef EXT_PWR_DETECT -#if defined(HELTEC_CAPSULE_SENSOR_V3) || defined(HELTEC_SENSOR_HUB) - // if external powered that pin will be pulled down - if (digitalRead(EXT_PWR_DETECT) == LOW) { - return true; - } - // if it's not LOW - check the battery -#else - // if external powered that pin will be pulled up - if (digitalRead(EXT_PWR_DETECT) == HIGH) { - return true; - } - // if it's not HIGH - check the battery -#endif - // If we have an EXT_PWR_DETECT pin and it indicates no external power, believe it. - return false; + return digitalRead(EXT_PWR_DETECT) == EXT_PWR_DETECT_VALUE; // technically speaking this should work for all(?) NRF52 boards // but needs testing across multiple devices. NRF52 USB would not even work if @@ -646,11 +646,7 @@ Power::Power() : OSThread("Power") bool Power::analogInit() { #ifdef EXT_PWR_DETECT -#if defined(HELTEC_CAPSULE_SENSOR_V3) || defined(HELTEC_SENSOR_HUB) - pinMode(EXT_PWR_DETECT, INPUT_PULLUP); -#else - pinMode(EXT_PWR_DETECT, INPUT); -#endif + pinMode(EXT_PWR_DETECT, EXT_PWR_DETECT_MODE); #endif #ifdef EXT_CHRG_DETECT pinMode(EXT_CHRG_DETECT, ext_chrg_detect_mode); @@ -1837,7 +1833,7 @@ SerialBatteryLevel serialBatteryLevel; bool Power::serialBatteryInit() { #ifdef EXT_PWR_DETECT - pinMode(EXT_PWR_DETECT, INPUT); + pinMode(EXT_PWR_DETECT, EXT_PWR_DETECT_MODE); #endif #ifdef EXT_CHRG_DETECT pinMode(EXT_CHRG_DETECT, ext_chrg_detect_mode); diff --git a/variants/esp32s3/heltec_capsule_sensor_v3/variant.h b/variants/esp32s3/heltec_capsule_sensor_v3/variant.h index 3ee5545a8cf..f689b20a8a1 100644 --- a/variants/esp32s3/heltec_capsule_sensor_v3/variant.h +++ b/variants/esp32s3/heltec_capsule_sensor_v3/variant.h @@ -1,6 +1,7 @@ #define LED_POWER 33 #define LED_POWER2 34 #define EXT_PWR_DETECT 35 +#define EXT_PWR_DETECT_MODE INPUT_PULLUP #define BUTTON_PIN 18 #define BUTTON_ACTIVE_LOW false diff --git a/variants/esp32s3/heltec_sensor_hub/variant.h b/variants/esp32s3/heltec_sensor_hub/variant.h index 8c5d31c9aa7..64255c038da 100644 --- a/variants/esp32s3/heltec_sensor_hub/variant.h +++ b/variants/esp32s3/heltec_sensor_hub/variant.h @@ -1,4 +1,5 @@ #define EXT_PWR_DETECT 20 +#define EXT_PWR_DETECT_MODE INPUT_PULLUP #define BUTTON_PIN 17 #define BUTTON_ACTIVE_LOW false