diff --git a/src/Power.cpp b/src/Power.cpp index 1c1f34cd406..212ecf56d2f 100644 --- a/src/Power.cpp +++ b/src/Power.cpp @@ -40,11 +40,6 @@ #include "input/LinuxJoystick.h" #endif -// Working USB detection for powered/charging states on the RAK platform -#ifdef NRF_APM -#include "nrfx_power.h" -#endif - #if defined(ARCH_NRF52) #include "Nrf52SaadcLock.h" #include "concurrency/LockGuard.h" @@ -563,12 +558,11 @@ class AnalogBatteryLevel : public HasBatteryLevel 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 -// VBUS was not properly connected and detected by the CPU -#elif defined(MUZI_BASE) || defined(PROMICRO_DIY_TCXO) - return powerHAL_isVBUSConnected(); -#endif + // powerHAL implements platform specific USB detections so if it says we good, we good + // if not implemented for a platform, it will return false so don't trust the false result + if (powerHAL_isVBUSConnected()) + return true; + return getBattVoltage() > chargingVolt; } @@ -808,6 +802,7 @@ bool Power::setup() } else if (analogInit()) { found = true; } else { +// NRF_APM is a last resort power management which can only detect if USB VBUS is connected #ifdef NRF_APM found = true; #endif @@ -941,10 +936,10 @@ void Power::readPowerStatus() if (batteryLevel) { hasBattery = batteryLevel->isBatteryConnect() ? OptTrue : OptFalse; -#ifndef NRF_APM + usbPowered = batteryLevel->isVbusIn() ? OptTrue : OptFalse; isChargingNow = batteryLevel->isCharging() ? OptTrue : OptFalse; -#endif + if (hasBattery) { batteryVoltageMv = batteryLevel->getBattVoltage(); // If the AXP192 returns a valid battery percentage, use it @@ -960,26 +955,18 @@ void Power::readPowerStatus() 0, 100); } } - } - -// FIXME: IMO we shouldn't be littering our code with all these ifdefs. Way -// better instead to make a Nrf52IsUsbPowered subclass (which shares a -// superclass with the BatteryLevel stuff) that just provides a few methods. But -// in the interest of fixing this bug I'm going to follow current practice. -#ifdef NRF_APM // Section of code detects USB power on the RAK4631 and updates - // the power states. Takes 20 seconds or so to detect changes. - - nrfx_power_usb_state_t nrf_usb_state = nrfx_power_usbstatus_get(); - // LOG_DEBUG("NRF Power %d", nrf_usb_state); - - // If changed to DISCONNECTED - if (nrf_usb_state == NRFX_POWER_USB_STATE_DISCONNECTED) - isChargingNow = usbPowered = OptFalse; - // If changed to CONNECTED / READY - else - isChargingNow = usbPowered = OptTrue; - + } else { // no batteryLevel implementation active - as a last resort try to detect VBUS using PowerHAL + if (powerHAL_isVBUSConnected()) { + usbPowered = isChargingNow = OptTrue; + } else { +// PowerHAL is supported so far only on NRF52 so if it returns +// false we know for sure VBUS is not connected. For other platforms +// it will return false as a placeholder +#ifdef NRF_APM + usbPowered = isChargingNow = OptFalse; #endif + } + } // Notify any status instances that are observing us const PowerStatus powerStatus2 = PowerStatus(hasBattery, usbPowered, isChargingNow, batteryVoltageMv, batteryChargePercent); diff --git a/src/platform/nrf52/architecture.h b/src/platform/nrf52/architecture.h index 600fb81925a..91f64a413ad 100644 --- a/src/platform/nrf52/architecture.h +++ b/src/platform/nrf52/architecture.h @@ -55,6 +55,10 @@ #define HAS_CUSTOM_CRYPTO_ENGINE 1 #endif +#ifndef NRF_APM +#define NRF_APM +#endif + // // set HW_VENDOR // diff --git a/variants/nrf52840/ELECROW-ThinkNode-M3/variant.h b/variants/nrf52840/ELECROW-ThinkNode-M3/variant.h index bd5bae9c858..5bafbb791ef 100644 --- a/variants/nrf52840/ELECROW-ThinkNode-M3/variant.h +++ b/variants/nrf52840/ELECROW-ThinkNode-M3/variant.h @@ -36,7 +36,6 @@ extern "C" { #define NUM_ANALOG_OUTPUTS (0) // Power Pin -#define NRF_APM #define PIN_GPS_EN 14 #define GPS_EN_ACTIVE HIGH #define PIN_POWER_USB 31 diff --git a/variants/nrf52840/ME25LS01-4Y10TD/variant.h b/variants/nrf52840/ME25LS01-4Y10TD/variant.h index 1d1af37adf9..b6e3beeb719 100644 --- a/variants/nrf52840/ME25LS01-4Y10TD/variant.h +++ b/variants/nrf52840/ME25LS01-4Y10TD/variant.h @@ -42,9 +42,6 @@ extern "C" { #define NUM_ANALOG_INPUTS (6) #define NUM_ANALOG_OUTPUTS (0) -// Use the native nrf52 usb power detection -#define NRF_APM - #define PIN_3V3_EN (32 + 5) //-1 #define PIN_3V3_ACC_EN -1 diff --git a/variants/nrf52840/ME25LS01-4Y10TD_e-ink/variant.h b/variants/nrf52840/ME25LS01-4Y10TD_e-ink/variant.h index a5bb53a33df..59b236b99a8 100644 --- a/variants/nrf52840/ME25LS01-4Y10TD_e-ink/variant.h +++ b/variants/nrf52840/ME25LS01-4Y10TD_e-ink/variant.h @@ -42,9 +42,6 @@ extern "C" { #define NUM_ANALOG_INPUTS (6) #define NUM_ANALOG_OUTPUTS (0) -// Use the native nrf52 usb power detection -#define NRF_APM - #define PIN_3V3_EN (32 + 5) //-1 #define PIN_3V3_ACC_EN -1 diff --git a/variants/nrf52840/MS24SF1/variant.h b/variants/nrf52840/MS24SF1/variant.h index a41b3a3509a..adc4efb0551 100644 --- a/variants/nrf52840/MS24SF1/variant.h +++ b/variants/nrf52840/MS24SF1/variant.h @@ -42,9 +42,6 @@ extern "C" { #define NUM_ANALOG_INPUTS (6) #define NUM_ANALOG_OUTPUTS (0) -// Use the native nrf52 usb power detection -#define NRF_APM - #define PIN_3V3_EN (32 + 5) //-1 #define PIN_3V3_ACC_EN -1 diff --git a/variants/nrf52840/gat562_mesh_trial_tracker/variant.h b/variants/nrf52840/gat562_mesh_trial_tracker/variant.h index b5632a7fbe3..bd391da35e8 100644 --- a/variants/nrf52840/gat562_mesh_trial_tracker/variant.h +++ b/variants/nrf52840/gat562_mesh_trial_tracker/variant.h @@ -217,9 +217,6 @@ SO GPIO 39/TXEN MAY NOT BE DEFINED FOR SUCCESSFUL OPERATION OF THE SX1262 - TG #define SX126X_DIO2_AS_RF_SWITCH #define SX126X_DIO3_TCXO_VOLTAGE 1.8 -// Testing USB detection -#define NRF_APM - // enables 3.3V periphery like GPS or IO Module // Do not toggle this for GPS power savings #define PIN_3V3_EN (34) diff --git a/variants/nrf52840/heltec_mesh_node_t1/variant.h b/variants/nrf52840/heltec_mesh_node_t1/variant.h index 88a2c8b4c03..97338b4f002 100644 --- a/variants/nrf52840/heltec_mesh_node_t1/variant.h +++ b/variants/nrf52840/heltec_mesh_node_t1/variant.h @@ -155,7 +155,6 @@ extern "C" { // Power / USB -#define NRF_APM // USB VBUS detection via nrfx_power_usbstatus_get() - no dedicated charging IC on this board #define HAS_RTC 0 // No external RTC fitted #ifdef __cplusplus diff --git a/variants/nrf52840/meshlink/variant.h b/variants/nrf52840/meshlink/variant.h index 00107ac347d..2f5d3482c61 100644 --- a/variants/nrf52840/meshlink/variant.h +++ b/variants/nrf52840/meshlink/variant.h @@ -34,9 +34,6 @@ extern "C" { #define LED_STATE_ON 0 // State when LED is lit -// Testing USB detection -// #define NRF_APM - /* * Analog pins */ diff --git a/variants/nrf52840/meshtiny/variant.h b/variants/nrf52840/meshtiny/variant.h index 4289fef4c44..d96f928e17a 100644 --- a/variants/nrf52840/meshtiny/variant.h +++ b/variants/nrf52840/meshtiny/variant.h @@ -166,9 +166,6 @@ static const uint8_t SCK = PIN_SPI_SCK; #define SX126X_DIO2_AS_RF_SWITCH #define SX126X_DIO3_TCXO_VOLTAGE 1.8 -// Testing USB detection -#define NRF_APM - #define PIN_3V3_EN (34) // Battery diff --git a/variants/nrf52840/r1-neo/variant.h b/variants/nrf52840/r1-neo/variant.h index 42d44d67361..3e879079b1a 100644 --- a/variants/nrf52840/r1-neo/variant.h +++ b/variants/nrf52840/r1-neo/variant.h @@ -112,9 +112,6 @@ static const uint8_t SCK = PIN_SPI_SCK; #define SX126X_DIO2_AS_RF_SWITCH #define SX126X_DIO3_TCXO_VOLTAGE 1.8 -// Testing USB detection -#define NRF_APM - #define PIN_GPS_EN (32 + 1) // P1.01 #define PIN_GPS_PPS (2) // P0.02 Pulse per second input from the GPS diff --git a/variants/nrf52840/rak2560/variant.h b/variants/nrf52840/rak2560/variant.h index acd1ae60e43..fd2b448618e 100644 --- a/variants/nrf52840/rak2560/variant.h +++ b/variants/nrf52840/rak2560/variant.h @@ -200,9 +200,6 @@ SO GPIO 39/TXEN MAY NOT BE DEFINED FOR SUCCESSFUL OPERATION OF THE SX1262 - TG #define SX126X_DIO2_AS_RF_SWITCH #define SX126X_DIO3_TCXO_VOLTAGE 1.8 -// Testing USB detection -#define NRF_APM - // enables 3.3V periphery like GPS or IO Module // Do not toggle this for GPS power savings #define PIN_3V3_EN (34) diff --git a/variants/nrf52840/rak3401_1watt/variant.h b/variants/nrf52840/rak3401_1watt/variant.h index 329ec21e4d3..60924d87465 100644 --- a/variants/nrf52840/rak3401_1watt/variant.h +++ b/variants/nrf52840/rak3401_1watt/variant.h @@ -163,10 +163,8 @@ static const uint8_t SCK = PIN_SPI_SCK; #define SX126X_DIO2_AS_RF_SWITCH #define SX126X_DIO3_TCXO_VOLTAGE 1.8 -// Testing USB detection -#define NRF_APM // If using a power chip like the INA3221 you can override the default battery voltage channel below -// and comment out NRF_APM to use the INA3221 instead of the USB detection for charging. +// and to use the INA3221 instead of the USB detection for charging. // INA3221Sensor.h provides INA3221_CH1/INA3221_CH2/INA3221_CH3 compatibility aliases, so // board variants can continue to use the named channel constants here. // #define INA3221_BAT_CH INA3221_CH2 diff --git a/variants/nrf52840/rak4631/variant.h b/variants/nrf52840/rak4631/variant.h index bdd797836d4..e5279d680fb 100644 --- a/variants/nrf52840/rak4631/variant.h +++ b/variants/nrf52840/rak4631/variant.h @@ -217,10 +217,8 @@ SO GPIO 39/TXEN MAY NOT BE DEFINED FOR SUCCESSFUL OPERATION OF THE SX1262 - TG #define SX126X_DIO2_AS_RF_SWITCH #define SX126X_DIO3_TCXO_VOLTAGE 1.8 -// Testing USB detection -#define NRF_APM // If using a power chip like the INA3221 you can override the default battery voltage channel below -// and comment out NRF_APM to use the INA3221 instead of the USB detection for charging. +// to use the INA3221 instead of the USB detection for charging. // INA3221Sensor.h provides compatibility aliases such as INA3221_CH1/INA3221_CH2/INA3221_CH3, // so board variants can continue to use the channel names below. // #define INA3221_BAT_CH INA3221_CH2 diff --git a/variants/nrf52840/rak4631_epaper/variant.h b/variants/nrf52840/rak4631_epaper/variant.h index 82c26af7b90..9617f825e80 100644 --- a/variants/nrf52840/rak4631_epaper/variant.h +++ b/variants/nrf52840/rak4631_epaper/variant.h @@ -203,9 +203,6 @@ static const uint8_t SCK = PIN_SPI_SCK; // RAK12002 RTC Module #define RV3028_RTC (uint8_t)0b1010010 -// Testing USB detection -#define NRF_APM - // Battery // The battery sense is hooked to pin A0 (5) #define BATTERY_PIN PIN_A0 diff --git a/variants/nrf52840/rak4631_epaper_onrxtx/variant.h b/variants/nrf52840/rak4631_epaper_onrxtx/variant.h index 2d34ab84caf..5528ba8bd7b 100644 --- a/variants/nrf52840/rak4631_epaper_onrxtx/variant.h +++ b/variants/nrf52840/rak4631_epaper_onrxtx/variant.h @@ -83,9 +83,6 @@ static const uint8_t AREF = PIN_AREF; #define PIN_SERIAL2_RX (-1) #define PIN_SERIAL2_TX (-1) -// Testing USB detection -#define NRF_APM - /* * SPI Interfaces */ diff --git a/variants/nrf52840/rak4631_eth_gw/variant.h b/variants/nrf52840/rak4631_eth_gw/variant.h index eb1d558ead4..4a942dd3ac9 100644 --- a/variants/nrf52840/rak4631_eth_gw/variant.h +++ b/variants/nrf52840/rak4631_eth_gw/variant.h @@ -208,9 +208,6 @@ SO GPIO 39/TXEN MAY NOT BE DEFINED FOR SUCCESSFUL OPERATION OF THE SX1262 - TG #define SX126X_DIO2_AS_RF_SWITCH #define SX126X_DIO3_TCXO_VOLTAGE 1.8 -// Testing USB detection -#define NRF_APM - // enables 3.3V periphery like GPS or IO Module // Do not toggle this for GPS power savings #define PIN_3V3_EN (34) diff --git a/variants/nrf52840/rak4631_nomadstar_meteor_pro/variant.h b/variants/nrf52840/rak4631_nomadstar_meteor_pro/variant.h index aea4973056d..d44f3408aff 100644 --- a/variants/nrf52840/rak4631_nomadstar_meteor_pro/variant.h +++ b/variants/nrf52840/rak4631_nomadstar_meteor_pro/variant.h @@ -207,9 +207,6 @@ SO GPIO 39/TXEN MAY NOT BE DEFINED FOR SUCCESSFUL OPERATION OF THE SX1262 - TG #define SX126X_DIO2_AS_RF_SWITCH #define SX126X_DIO3_TCXO_VOLTAGE 1.8 -// Testing USB detection -#define NRF_APM - // enables 3.3V periphery like GPS or IO Module // Do not toggle this for GPS power savings #define PIN_3V3_EN (34) diff --git a/variants/nrf52840/rak_wismeshtag/variant.h b/variants/nrf52840/rak_wismeshtag/variant.h index 9ea215e42a9..4bd51aa1e7b 100644 --- a/variants/nrf52840/rak_wismeshtag/variant.h +++ b/variants/nrf52840/rak_wismeshtag/variant.h @@ -198,9 +198,6 @@ SO GPIO 39/TXEN MAY NOT BE DEFINED FOR SUCCESSFUL OPERATION OF THE SX1262 - TG #define SX126X_DIO2_AS_RF_SWITCH #define SX126X_DIO3_TCXO_VOLTAGE 1.8 -// Testing USB detection -#define NRF_APM - // enables 3.3V periphery like GPS or IO Module // Do not toggle this for GPS power savings #define PIN_3V3_EN (34) diff --git a/variants/nrf52840/rak_wismeshtap/variant.h b/variants/nrf52840/rak_wismeshtap/variant.h index fe2cc69e944..ffc4433be44 100644 --- a/variants/nrf52840/rak_wismeshtap/variant.h +++ b/variants/nrf52840/rak_wismeshtap/variant.h @@ -227,9 +227,6 @@ SO GPIO 39/TXEN MAY NOT BE DEFINED FOR SUCCESSFUL OPERATION OF THE SX1262 - TG #define SX126X_DIO2_AS_RF_SWITCH #define SX126X_DIO3_TCXO_VOLTAGE 1.8 -// Testing USB detection -#define NRF_APM - // enables 3.3V periphery like GPS or IO Module #define PIN_3V3_EN (34) diff --git a/variants/nrf52840/seeed_mesh_tracker_X1/variant.h b/variants/nrf52840/seeed_mesh_tracker_X1/variant.h index b9882d71988..ae2ce9dc9a4 100644 --- a/variants/nrf52840/seeed_mesh_tracker_X1/variant.h +++ b/variants/nrf52840/seeed_mesh_tracker_X1/variant.h @@ -40,9 +40,6 @@ extern "C" { #define NUM_ANALOG_INPUTS (6) #define NUM_ANALOG_OUTPUTS (0) -// Use the native nrf52 usb power detection -#define NRF_APM - // PIN_3V3_EN use in deep sleep , as power pin. #define PIN_3V3_EN (32 + 7) // P1.7, Power to Sensors #define PIN_BAT_ADC_EN (32 + 6) // P1.6, Power to battery ADC diff --git a/variants/nrf52840/seeed_solar_node/variant.h b/variants/nrf52840/seeed_solar_node/variant.h index 69736a9e02e..a75ccfa3c1e 100644 --- a/variants/nrf52840/seeed_solar_node/variant.h +++ b/variants/nrf52840/seeed_solar_node/variant.h @@ -147,6 +147,7 @@ extern "C" { #define PIN_SERIAL2_RX (-1) #define PIN_SERIAL2_TX (-1) + #ifdef __cplusplus } #endif diff --git a/variants/nrf52840/seeed_wio_tracker_L1/variant.h b/variants/nrf52840/seeed_wio_tracker_L1/variant.h index 9e1df0fa347..aa9278ba481 100644 --- a/variants/nrf52840/seeed_wio_tracker_L1/variant.h +++ b/variants/nrf52840/seeed_wio_tracker_L1/variant.h @@ -112,8 +112,7 @@ static const uint8_t SCL = PIN_WIRE_SCL; #define ADC_MULTIPLIER 2.0 #define BATTERY_PIN PIN_VBAT // PIN_A7 #define AREF_VOLTAGE 3.6 -// We rely on the nrf52840 USB controller to tell us if we are hooked to a power supply -#define NRF_APM + // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ // GPS L76KB // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ diff --git a/variants/nrf52840/tracker-t1000-e/variant.h b/variants/nrf52840/tracker-t1000-e/variant.h index b064dbc9f24..4845aa2fdc6 100644 --- a/variants/nrf52840/tracker-t1000-e/variant.h +++ b/variants/nrf52840/tracker-t1000-e/variant.h @@ -40,9 +40,6 @@ extern "C" { #define NUM_ANALOG_INPUTS (6) #define NUM_ANALOG_OUTPUTS (0) -// Use the native nrf52 usb power detection -#define NRF_APM - #define PIN_3V3_EN (32 + 6) // P1.6, Power to Sensors #define PIN_3V3_ACC_EN (32 + 7) // P1.7, Power to Acc diff --git a/variants/nrf52840/wio-tracker-wm1110/variant.h b/variants/nrf52840/wio-tracker-wm1110/variant.h index 647bd47d89e..cebead703c9 100644 --- a/variants/nrf52840/wio-tracker-wm1110/variant.h +++ b/variants/nrf52840/wio-tracker-wm1110/variant.h @@ -43,9 +43,6 @@ extern "C" { #define WIRE_INTERFACES_COUNT 1 -// We rely on the nrf52840 USB controller to tell us if we are hooked to a power supply -#define NRF_APM - #define PIN_3V3_EN (32 + 1) // P1.01, Power to Sensors #define PIN_WIRE_SDA (0 + 5) // P0.05