Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 19 additions & 32 deletions src/Power.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/platform/nrf52/architecture.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
#define HAS_CUSTOM_CRYPTO_ENGINE 1
#endif

#ifndef NRF_APM
#define NRF_APM
#endif

//
// set HW_VENDOR
//
Expand Down
1 change: 0 additions & 1 deletion variants/nrf52840/ELECROW-ThinkNode-M3/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions variants/nrf52840/ME25LS01-4Y10TD/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 0 additions & 3 deletions variants/nrf52840/ME25LS01-4Y10TD_e-ink/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 0 additions & 3 deletions variants/nrf52840/MS24SF1/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 0 additions & 3 deletions variants/nrf52840/gat562_mesh_trial_tracker/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion variants/nrf52840/heltec_mesh_node_t1/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions variants/nrf52840/meshlink/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ extern "C" {

#define LED_STATE_ON 0 // State when LED is lit

// Testing USB detection
// #define NRF_APM

/*
* Analog pins
*/
Expand Down
3 changes: 0 additions & 3 deletions variants/nrf52840/meshtiny/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions variants/nrf52840/r1-neo/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 0 additions & 3 deletions variants/nrf52840/rak2560/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions variants/nrf52840/rak3401_1watt/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions variants/nrf52840/rak4631/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions variants/nrf52840/rak4631_epaper/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions variants/nrf52840/rak4631_epaper_onrxtx/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
3 changes: 0 additions & 3 deletions variants/nrf52840/rak4631_eth_gw/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions variants/nrf52840/rak4631_nomadstar_meteor_pro/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions variants/nrf52840/rak_wismeshtag/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions variants/nrf52840/rak_wismeshtap/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 0 additions & 3 deletions variants/nrf52840/seeed_mesh_tracker_X1/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions variants/nrf52840/seeed_solar_node/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ extern "C" {

#define PIN_SERIAL2_RX (-1)
#define PIN_SERIAL2_TX (-1)

#ifdef __cplusplus
}
#endif
Expand Down
3 changes: 1 addition & 2 deletions variants/nrf52840/seeed_wio_tracker_L1/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Expand Down
3 changes: 0 additions & 3 deletions variants/nrf52840/tracker-t1000-e/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 0 additions & 3 deletions variants/nrf52840/wio-tracker-wm1110/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down