diff --git a/.vscode/settings.json b/.vscode/settings.json index 9f66321..d3dcb64 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -43,6 +43,8 @@ "streambuf": "cpp", "cinttypes": "cpp", "utility": "cpp", - "typeinfo": "cpp" + "typeinfo": "cpp", + "string": "cpp", + "string_view": "cpp" } -} \ No newline at end of file +} diff --git a/platformio.ini b/platformio.ini index 95f358e..7caacfb 100644 --- a/platformio.ini +++ b/platformio.ini @@ -52,7 +52,7 @@ monitor_speed = 115200 lib_deps = bblanchon/ArduinoJson @ 6.21.3 - lewisxhe/AXP202X_Library @ 1.1.3 + lewisxhe/XPowersLib @ 0.1.8 peterus/APRS-Decoder-Lib @ 0.0.6 peterus/esp-logger @ 1.0.0 peterus/ESP-FTP-Server-Lib @ 0.14.0 diff --git a/src/LoRa_APRS_iGate.cpp b/src/LoRa_APRS_iGate.cpp index 06a4555..0f44123 100644 --- a/src/LoRa_APRS_iGate.cpp +++ b/src/LoRa_APRS_iGate.cpp @@ -35,6 +35,15 @@ TaskQueue> toMQTT; System LoRaSystem; Configuration userConfig; +#ifdef HAS_AXP192 +AXP192 axp; +PowerManagement *powerManagement = &axp; +#endif +#ifdef HAS_AXP2101 +AXP2101 axp; +PowerManagement *powerManagement = &axp; +#endif + DisplayTask displayTask; // ModemTask modemTask(fromModem, toModem); RadiolibTask modemTask(fromModem, toModem); @@ -68,21 +77,21 @@ void setup() { } }*/ -#ifdef TBEAM_V10 +#if defined(HAS_AXP192) || defined(HAS_AXP2101) Wire.begin(SDA, SCL); - PowerManagement powerManagement; - if (!powerManagement.begin(Wire)) { - LoRaSystem.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_INFO, MODULE_NAME, "AXP192 init done!"); + if (powerManagement->begin(Wire)) { + LoRaSystem.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "PMU", "init done!"); } else { - LoRaSystem.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, MODULE_NAME, "AXP192 init failed!"); + LoRaSystem.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "PMU", "init failed!"); } - powerManagement.activateLoRa(); - powerManagement.activateOLED(); + powerManagement->activateLoRa(); + powerManagement->activateOLED(); if (userConfig.beacon.use_gps) { - powerManagement.activateGPS(); + powerManagement->activateGPS(); } else { - powerManagement.deactivateGPS(); + powerManagement->deactivateGPS(); } + // powerManagement->activateMeasurement(); #endif LoRaSystem.setUserConfig(&userConfig); @@ -98,7 +107,7 @@ void setup() { tcpip = true; } -#ifdef TINTERNET_POE +#ifdef T_INTERNET_POE LoRaSystem.getTaskManager().addAlwaysRunTask(ðTask); tcpip = true; #endif diff --git a/src/PowerManagement/power_management.cpp b/src/PowerManagement/power_management.cpp index 99037be..06af01f 100644 --- a/src/PowerManagement/power_management.cpp +++ b/src/PowerManagement/power_management.cpp @@ -1,37 +1,218 @@ +#include +#include #include "power_management.h" -PowerManagement::PowerManagement() : axp() { +AXP192::AXP192() { } -bool PowerManagement::begin(TwoWire &port) { - bool result = axp.begin(port, AXP192_SLAVE_ADDRESS); - if (!result) { - axp.setDCDC1Voltage(3300); +// cppcheck-suppress unusedFunction +bool AXP192::begin(TwoWire &port) { + _pmu = new XPowersAXP192(port); + if (!_pmu->init()) { + delete _pmu; + _pmu = 0; + return false; } - return result; + + // lora radio power channel + _pmu->setPowerChannelVoltage(XPOWERS_LDO2, 3300); + + // oled module power channel, + // disable it will cause abnormal communication between boot and AXP power supply, + // do not turn it off + _pmu->setPowerChannelVoltage(XPOWERS_DCDC1, 3300); + + // gnss module power channel - now turned on in setGpsPower + _pmu->setPowerChannelVoltage(XPOWERS_LDO3, 3300); + + // protected oled power source + //_pmu->setProtectedChannel(XPOWERS_DCDC1); + // protected esp32 power source + _pmu->setProtectedChannel(XPOWERS_DCDC3); + + // disable not use channel + _pmu->disablePowerOutput(XPOWERS_DCDC2); + + // disable all axp chip interrupt + _pmu->disableIRQ(XPOWERS_AXP192_ALL_IRQ); + + // Set constant current charging current + _pmu->setChargerConstantCurr(XPOWERS_AXP192_CHG_CUR_780MA); + + // Set up the charging voltage + _pmu->setChargeTargetVoltage(XPOWERS_AXP192_CHG_VOL_4V2); + + _pmu->setChargingLedMode(XPOWERS_CHG_LED_CTRL_CHG); + + return true; } -void PowerManagement::activateLoRa() { - axp.setPowerOutPut(AXP192_LDO2, AXP202_ON); +// cppcheck-suppress unusedFunction +void AXP192::activateLoRa() { + _pmu->enablePowerOutput(XPOWERS_LDO2); } -void PowerManagement::deactivateLoRa() { - axp.setPowerOutPut(AXP192_LDO2, AXP202_OFF); +// cppcheck-suppress unusedFunction +void AXP192::deactivateLoRa() { + _pmu->disablePowerOutput(XPOWERS_LDO2); } -void PowerManagement::activateGPS() { - axp.setPowerOutPut(AXP192_LDO3, AXP202_ON); +// cppcheck-suppress unusedFunction +void AXP192::activateGPS() { + _pmu->enablePowerOutput(XPOWERS_LDO3); } -void PowerManagement::deactivateGPS() { - axp.setPowerOutPut(AXP192_LDO3, AXP202_OFF); +// cppcheck-suppress unusedFunction +void AXP192::deactivateGPS() { + _pmu->disablePowerOutput(XPOWERS_LDO3); } -void PowerManagement::activateOLED() { - axp.setPowerOutPut(AXP192_DCDC1, AXP202_ON); +// cppcheck-suppress unusedFunction +void AXP192::activateOLED() { + _pmu->enablePowerOutput(XPOWERS_DCDC1); } -void PowerManagement::deactivateOLED() { - axp.setPowerOutPut(AXP192_DCDC1, AXP202_OFF); +// cppcheck-suppress unusedFunction +void AXP192::deactivateOLED() { + _pmu->disablePowerOutput(XPOWERS_DCDC1); +} + +// cppcheck-suppress unusedFunction +void AXP192::activateMeasurement() { + _pmu->enableBattVoltageMeasure(); +} + +// cppcheck-suppress unusedFunction +void AXP192::deactivateMeasurement() { + _pmu->disableBattVoltageMeasure(); +} + +// cppcheck-suppress unusedFunction +double AXP192::getBatteryVoltage() { + return _pmu->getBattVoltage() / 1000.0; +} + +// cppcheck-suppress unusedFunction +double AXP192::getBatteryChargeDischargeCurrent() { + if (isCharging()) { + return ((XPowersAXP192 *)_pmu)->getBatteryChargeCurrent(); + } + return -1.0 * ((XPowersAXP192 *)_pmu)->getBattDischargeCurrent(); +} + +bool AXP192::isBatteryConnect() { + return _pmu->isBatteryConnect(); +} + +bool AXP192::isCharging() { + return _pmu->isCharging(); +} + +AXP2101::AXP2101() { +} + +// cppcheck-suppress unusedFunction +bool AXP2101::begin(TwoWire &port) { + _pmu = new XPowersAXP2101(port); + if (!_pmu->init()) { + delete _pmu; + _pmu = 0; + return false; + } + + // Unuse power channel + _pmu->disablePowerOutput(XPOWERS_DCDC2); + _pmu->disablePowerOutput(XPOWERS_DCDC3); + _pmu->disablePowerOutput(XPOWERS_DCDC4); + _pmu->disablePowerOutput(XPOWERS_DCDC5); + _pmu->disablePowerOutput(XPOWERS_ALDO1); + _pmu->disablePowerOutput(XPOWERS_ALDO4); + _pmu->disablePowerOutput(XPOWERS_BLDO1); + _pmu->disablePowerOutput(XPOWERS_BLDO2); + _pmu->disablePowerOutput(XPOWERS_DLDO1); + _pmu->disablePowerOutput(XPOWERS_DLDO2); + + // GNSS RTC PowerVDD 3300mV + _pmu->setPowerChannelVoltage(XPOWERS_VBACKUP, 3300); + _pmu->enablePowerOutput(XPOWERS_VBACKUP); + + // LoRa VDD 3300mV + _pmu->setPowerChannelVoltage(XPOWERS_ALDO2, 3300); + _pmu->enablePowerOutput(XPOWERS_ALDO2); + + // GNSS VDD 3300mV + _pmu->setPowerChannelVoltage(XPOWERS_ALDO3, 3300); + _pmu->enablePowerOutput(XPOWERS_ALDO3); + + // disable all axp chip interrupt + _pmu->disableIRQ(XPOWERS_AXP2101_ALL_IRQ); + + // Set constant current charging current + _pmu->setChargerConstantCurr(XPOWERS_AXP2101_CHG_CUR_800MA); + + // Set up the charging voltage + _pmu->setChargeTargetVoltage(XPOWERS_AXP2101_CHG_VOL_4V2); + + _pmu->setChargingLedMode(XPOWERS_CHG_LED_CTRL_CHG); + + return true; +} + +// cppcheck-suppress unusedFunction +void AXP2101::activateLoRa() { + _pmu->enablePowerOutput(XPOWERS_ALDO2); +} + +// cppcheck-suppress unusedFunction +void AXP2101::deactivateLoRa() { + _pmu->disablePowerOutput(XPOWERS_ALDO2); +} + +// cppcheck-suppress unusedFunction +void AXP2101::activateGPS() { + _pmu->enablePowerOutput(XPOWERS_ALDO3); +} + +// cppcheck-suppress unusedFunction +void AXP2101::deactivateGPS() { + _pmu->disablePowerOutput(XPOWERS_ALDO3); +} + +// cppcheck-suppress unusedFunction +void AXP2101::activateOLED() { + _pmu->enablePowerOutput(XPOWERS_DCDC1); +} + +// cppcheck-suppress unusedFunction +void AXP2101::deactivateOLED() { + _pmu->disablePowerOutput(XPOWERS_DCDC1); +} + +// cppcheck-suppress unusedFunction +void AXP2101::activateMeasurement() { + _pmu->enableBattVoltageMeasure(); +} + +// cppcheck-suppress unusedFunction +void AXP2101::deactivateMeasurement() { + _pmu->disableBattVoltageMeasure(); +} + +// cppcheck-suppress unusedFunction +double AXP2101::getBatteryVoltage() { + return _pmu->getBattVoltage() / 1000.0; +} + +// cppcheck-suppress unusedFunction +double AXP2101::getBatteryChargeDischargeCurrent() { + return 0.0; +} + +bool AXP2101::isBatteryConnect() { + return _pmu->isBatteryConnect(); +} + +bool AXP2101::isCharging() { + return _pmu->isCharging(); } diff --git a/src/PowerManagement/power_management.h b/src/PowerManagement/power_management.h index 156c144..45ff6ea 100644 --- a/src/PowerManagement/power_management.h +++ b/src/PowerManagement/power_management.h @@ -1,12 +1,43 @@ #ifndef POWER_MANAGEMENT_H_ #define POWER_MANAGEMENT_H_ -#include -#include +#include +#include class PowerManagement { public: - PowerManagement(); + ~PowerManagement() { + } + + virtual bool begin(TwoWire &port) = 0; + + virtual void activateLoRa() = 0; + virtual void deactivateLoRa() = 0; + + virtual void activateGPS() = 0; + virtual void deactivateGPS() = 0; + + virtual void activateOLED() = 0; + virtual void deactivateOLED() = 0; + + virtual void activateMeasurement() = 0; + virtual void deactivateMeasurement() = 0; + + virtual double getBatteryVoltage() = 0; + + virtual double getBatteryChargeDischargeCurrent() = 0; + + virtual bool isBatteryConnect() = 0; + virtual bool isCharging() = 0; + +protected: + XPowersLibInterface *_pmu = 0; +}; + +class AXP192 : public PowerManagement { +public: + AXP192(); + bool begin(TwoWire &port); void activateLoRa(); @@ -18,8 +49,45 @@ public: void activateOLED(); void deactivateOLED(); -private: - AXP20X_Class axp; + void enableChgLed(); + void disableChgLed(); + + void activateMeasurement(); + void deactivateMeasurement(); + + double getBatteryVoltage(); + double getBatteryChargeDischargeCurrent(); + + bool isBatteryConnect(); + bool isCharging(); +}; + +class AXP2101 : public PowerManagement { +public: + AXP2101(); + + bool begin(TwoWire &port); + + void activateLoRa(); + void deactivateLoRa(); + + void activateGPS(); + void deactivateGPS(); + + void activateOLED(); + void deactivateOLED(); + + void enableChgLed(); + void disableChgLed(); + + void activateMeasurement(); + void deactivateMeasurement(); + + double getBatteryVoltage(); + double getBatteryChargeDischargeCurrent(); + + bool isBatteryConnect(); + bool isCharging(); }; #endif diff --git a/variants/tbeam/platformio.ini b/variants/tbeam/platformio.ini index 0fffcb1..a21ee20 100644 --- a/variants/tbeam/platformio.ini +++ b/variants/tbeam/platformio.ini @@ -1,6 +1,5 @@ -; The 1.0 release of the TBEAM board [env:tbeam] extends = esp32_base board = ttgo-t-beam build_flags = - ${esp32_base.build_flags} -DTBEAM_V10 -DUSE_SX1278 -DGPS_RX_PIN=34 -DGPS_TX_PIN=12 + ${esp32_base.build_flags} -DTBEAM_V10 -DUSE_SX1278 -DGPS_RX_PIN=34 -DGPS_TX_PIN=12 -DHAS_AXP192 diff --git a/variants/tbeam_axp2101/platformio.ini b/variants/tbeam_axp2101/platformio.ini new file mode 100644 index 0000000..704e2dc --- /dev/null +++ b/variants/tbeam_axp2101/platformio.ini @@ -0,0 +1,5 @@ +[env:tbeam_axp2101] +extends = esp32_base +board = ttgo-t-beam +build_flags = + ${esp32_base.build_flags} -DTBEAM_V12_AXP2101 -DUSE_SX1278 -DGPS_RX_PIN=34 -DGPS_TX_PIN=12 -DHAS_AXP2101