From cbb8eb65baefb07425d6eb90ec50d6ab4afc8136 Mon Sep 17 00:00:00 2001 From: HookdomPonix <83303405+HookdomPonix@users.noreply.github.com> Date: Wed, 22 Nov 2023 09:30:55 -0700 Subject: [PATCH] Add USB detection to RAK4631 based boards. (#2956) * Add support for the rak10701 board, no touch * Moved tftblack fillin and changed teh src flags * Added rak10701 to platformio.ini * Add USB detection to RAK4631 units. * Eliminate spurious symbol in comment field. --------- Co-authored-by: Ben Meadors --- src/Power.cpp | 26 +++++++++++++++++++++--- variants/rak10701/variant.h | 3 +++ variants/rak4631/variant.h | 3 +++ variants/rak4631_epaper/variant.h | 5 ++++- variants/rak4631_epaper_onrxtx/variant.h | 5 ++++- 5 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/Power.cpp b/src/Power.cpp index c7392c90b..0a56a1ba2 100644 --- a/src/Power.cpp +++ b/src/Power.cpp @@ -19,6 +19,11 @@ #include "meshUtils.h" #include "sleep.h" +// Working USB detection for powered/charging states on the RAK platform +#ifdef NRF_APM +#include "nrfx_power.h" +#endif + #ifdef DEBUG_HEAP_MQTT #include "mqtt/MQTT.h" #include "target_specific.h" @@ -460,10 +465,25 @@ void Power::readPowerStatus() } } + OptionalBool NRF_USB = OptFalse; + +#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(); + + if (nrf_usb_state == NRFX_POWER_USB_STATE_DISCONNECTED) { + powerFSM.trigger(EVENT_POWER_DISCONNECTED); + NRF_USB = OptFalse; + } else { + powerFSM.trigger(EVENT_POWER_CONNECTED); + NRF_USB = OptTrue; + } +#endif // Notify any status instances that are observing us - const PowerStatus powerStatus2 = - PowerStatus(hasBattery ? OptTrue : OptFalse, batteryLevel->isVbusIn() ? OptTrue : OptFalse, - batteryLevel->isCharging() ? OptTrue : OptFalse, batteryVoltageMv, batteryChargePercent); + const PowerStatus powerStatus2 = PowerStatus( + hasBattery ? OptTrue : OptFalse, batteryLevel->isVbusIn() || NRF_USB == OptTrue ? OptTrue : OptFalse, + batteryLevel->isCharging() || NRF_USB == OptTrue ? OptTrue : OptFalse, batteryVoltageMv, batteryChargePercent); LOG_DEBUG("Battery: usbPower=%d, isCharging=%d, batMv=%d, batPct=%d\n", powerStatus2.getHasUSB(), powerStatus2.getIsCharging(), powerStatus2.getBatteryVoltageMv(), powerStatus2.getBatteryChargePercent()); newStatus.notifyObservers(&powerStatus2); diff --git a/variants/rak10701/variant.h b/variants/rak10701/variant.h index 3b771d62b..5ff12a7de 100644 --- a/variants/rak10701/variant.h +++ b/variants/rak10701/variant.h @@ -234,6 +234,9 @@ 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/rak4631/variant.h b/variants/rak4631/variant.h index 89bb62c73..956bcd772 100644 --- a/variants/rak4631/variant.h +++ b/variants/rak4631/variant.h @@ -215,6 +215,9 @@ 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/rak4631_epaper/variant.h b/variants/rak4631_epaper/variant.h index 0253ec14d..bc2eddfee 100644 --- a/variants/rak4631_epaper/variant.h +++ b/variants/rak4631_epaper/variant.h @@ -209,6 +209,9 @@ 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 @@ -241,4 +244,4 @@ static const uint8_t SCK = PIN_SPI_SCK; * Arduino objects - C++ only *----------------------------------------------------------------------------*/ -#endif +#endif \ No newline at end of file diff --git a/variants/rak4631_epaper_onrxtx/variant.h b/variants/rak4631_epaper_onrxtx/variant.h index 6fc6da373..411e3eb17 100644 --- a/variants/rak4631_epaper_onrxtx/variant.h +++ b/variants/rak4631_epaper_onrxtx/variant.h @@ -84,6 +84,9 @@ 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 */ @@ -212,4 +215,4 @@ static const uint8_t SCK = PIN_SPI_SCK; * Arduino objects - C++ only *----------------------------------------------------------------------------*/ -#endif +#endif \ No newline at end of file