2024-01-03 05:13:44 +01:00
|
|
|
#include "configuration.h"
|
2024-05-24 20:24:40 +02:00
|
|
|
#include "battery_utils.h"
|
2024-11-05 22:20:44 +01:00
|
|
|
#include "board_pinout.h"
|
2024-01-03 05:13:44 +01:00
|
|
|
#include "power_utils.h"
|
|
|
|
|
|
2024-01-28 02:08:18 +01:00
|
|
|
#if defined(HAS_AXP192) || defined(HAS_AXP2101)
|
2025-03-03 16:15:50 +01:00
|
|
|
#ifdef TTGO_T_Beam_S3_SUPREME_V3
|
|
|
|
|
#define I2C0_SDA 17
|
|
|
|
|
#define I2C0_SCL 18
|
|
|
|
|
#define I2C1_SDA 42
|
|
|
|
|
#define I2C1_SCL 41
|
|
|
|
|
#define IRQ_PIN 40
|
|
|
|
|
#else
|
|
|
|
|
#define I2C_SDA 21
|
|
|
|
|
#define I2C_SCL 22
|
|
|
|
|
#define IRQ_PIN 35
|
|
|
|
|
#endif
|
2024-01-03 05:13:44 +01:00
|
|
|
#endif
|
|
|
|
|
|
2024-01-28 02:08:18 +01:00
|
|
|
#ifdef HAS_AXP192
|
2024-05-11 18:59:07 +02:00
|
|
|
XPowersAXP192 PMU;
|
2024-01-03 05:13:44 +01:00
|
|
|
#endif
|
2024-01-28 02:08:18 +01:00
|
|
|
#ifdef HAS_AXP2101
|
2024-05-11 18:59:07 +02:00
|
|
|
XPowersAXP2101 PMU;
|
2024-01-03 05:13:44 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
extern Configuration Config;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace POWER_Utils {
|
|
|
|
|
|
2024-05-27 17:46:14 +02:00
|
|
|
double getBatteryVoltage() {
|
|
|
|
|
#if defined(HAS_AXP192) || defined(HAS_AXP2101)
|
|
|
|
|
return (PMU.getBattVoltage() / 1000.0);
|
|
|
|
|
#else
|
|
|
|
|
return 0.0;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isBatteryConnected() {
|
|
|
|
|
#if defined(HAS_AXP192) || defined(HAS_AXP2101)
|
|
|
|
|
return PMU.isBatteryConnect();
|
|
|
|
|
#else
|
|
|
|
|
return false;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-24 14:09:05 +01:00
|
|
|
void activateMeasurement() {
|
|
|
|
|
#if defined(HAS_AXP192) || defined(HAS_AXP2101)
|
2024-05-11 18:59:07 +02:00
|
|
|
PMU.disableTSPinMeasure();
|
|
|
|
|
PMU.enableBattDetection();
|
|
|
|
|
PMU.enableVbusVoltageMeasure();
|
|
|
|
|
PMU.enableBattVoltageMeasure();
|
|
|
|
|
PMU.enableSystemVoltageMeasure();
|
2024-02-24 14:09:05 +01:00
|
|
|
#endif
|
2024-01-03 05:13:44 +01:00
|
|
|
}
|
|
|
|
|
|
2024-10-14 22:04:28 +02:00
|
|
|
void activateGPS() {
|
|
|
|
|
#ifdef HAS_AXP192
|
|
|
|
|
PMU.setLDO3Voltage(3300);
|
|
|
|
|
PMU.enableLDO3();
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef HAS_AXP2101
|
2025-03-03 16:15:50 +01:00
|
|
|
#ifdef TTGO_T_Beam_S3_SUPREME_V3
|
|
|
|
|
PMU.setALDO4Voltage(3300);
|
|
|
|
|
PMU.enableALDO4();
|
|
|
|
|
#else
|
2024-10-14 22:04:28 +02:00
|
|
|
PMU.setALDO3Voltage(3300);
|
|
|
|
|
PMU.enableALDO3();
|
2025-03-03 16:15:50 +01:00
|
|
|
#endif
|
2024-10-14 22:04:28 +02:00
|
|
|
#endif
|
|
|
|
|
#ifdef HELTEC_WIRELESS_TRACKER
|
|
|
|
|
digitalWrite(VEXT_CTRL, HIGH);
|
|
|
|
|
#endif
|
|
|
|
|
//gpsIsActive = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void deactivateGPS() {
|
|
|
|
|
#ifdef HAS_AXP192
|
|
|
|
|
PMU.disableLDO3();
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef HAS_AXP2101
|
2025-03-03 16:15:50 +01:00
|
|
|
#ifdef TTGO_T_Beam_S3_SUPREME_V3
|
|
|
|
|
PMU.disableALDO4();
|
|
|
|
|
#else
|
|
|
|
|
PMU.disableALDO3();
|
|
|
|
|
#endif
|
2024-10-14 22:04:28 +02:00
|
|
|
#endif
|
|
|
|
|
#ifdef HELTEC_WIRELESS_TRACKER
|
|
|
|
|
digitalWrite(VEXT_CTRL, LOW);
|
|
|
|
|
#endif
|
|
|
|
|
//gpsIsActive = false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-24 14:09:05 +01:00
|
|
|
void activateLoRa() {
|
|
|
|
|
#ifdef HAS_AXP192
|
2024-05-11 18:59:07 +02:00
|
|
|
PMU.setLDO2Voltage(3300);
|
|
|
|
|
PMU.enableLDO2();
|
2024-02-24 14:09:05 +01:00
|
|
|
#endif
|
|
|
|
|
#ifdef HAS_AXP2101
|
2025-03-03 16:15:50 +01:00
|
|
|
#ifdef TTGO_T_Beam_S3_SUPREME_V3
|
|
|
|
|
PMU.setALDO3Voltage(3300);
|
|
|
|
|
PMU.enableALDO3();
|
|
|
|
|
#else
|
|
|
|
|
PMU.setALDO2Voltage(3300);
|
|
|
|
|
PMU.enableALDO2();
|
|
|
|
|
#endif
|
2024-02-24 14:09:05 +01:00
|
|
|
#endif
|
2024-01-03 05:13:44 +01:00
|
|
|
}
|
|
|
|
|
|
2024-02-24 14:09:05 +01:00
|
|
|
void deactivateLoRa() {
|
|
|
|
|
#ifdef HAS_AXP192
|
2024-05-11 18:59:07 +02:00
|
|
|
PMU.disableLDO2();
|
2024-02-24 14:09:05 +01:00
|
|
|
#endif
|
|
|
|
|
#ifdef HAS_AXP2101
|
2025-03-03 16:15:50 +01:00
|
|
|
#ifdef TTGO_T_Beam_S3_SUPREME_V3
|
|
|
|
|
PMU.disableALDO3();
|
|
|
|
|
#else
|
|
|
|
|
PMU.disableALDO2();
|
|
|
|
|
#endif
|
2024-02-24 14:09:05 +01:00
|
|
|
#endif
|
2024-01-03 05:13:44 +01:00
|
|
|
}
|
|
|
|
|
|
2024-02-24 14:09:05 +01:00
|
|
|
bool begin(TwoWire &port) {
|
|
|
|
|
#if defined(HAS_AXP192)
|
2024-05-11 18:59:07 +02:00
|
|
|
bool result = PMU.begin(Wire, AXP192_SLAVE_ADDRESS, I2C_SDA, I2C_SCL);
|
|
|
|
|
if (result) {
|
|
|
|
|
PMU.disableDC2();
|
|
|
|
|
PMU.disableLDO2();
|
|
|
|
|
PMU.disableLDO3();
|
|
|
|
|
PMU.setDC1Voltage(3300);
|
|
|
|
|
PMU.enableDC1();
|
|
|
|
|
PMU.setProtectedChannel(XPOWERS_DCDC3);
|
|
|
|
|
PMU.disableIRQ(XPOWERS_AXP192_ALL_IRQ);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2024-02-24 14:09:05 +01:00
|
|
|
#elif defined(HAS_AXP2101)
|
2025-03-03 16:15:50 +01:00
|
|
|
#ifdef TTGO_T_Beam_S3_SUPREME_V3
|
|
|
|
|
bool result = PMU.begin(Wire1, AXP2101_SLAVE_ADDRESS, I2C1_SDA, I2C1_SCL);
|
|
|
|
|
#else
|
|
|
|
|
bool result = PMU.begin(Wire, AXP2101_SLAVE_ADDRESS, I2C_SDA, I2C_SCL);
|
|
|
|
|
#endif
|
2024-05-11 18:59:07 +02:00
|
|
|
if (result) {
|
|
|
|
|
PMU.disableDC2();
|
|
|
|
|
PMU.disableDC3();
|
|
|
|
|
PMU.disableDC4();
|
|
|
|
|
PMU.disableDC5();
|
2025-03-03 16:15:50 +01:00
|
|
|
#ifndef TTGO_T_Beam_S3_SUPREME_V3
|
|
|
|
|
PMU.disableALDO1();
|
|
|
|
|
PMU.disableALDO4();
|
|
|
|
|
#endif
|
2024-05-11 18:59:07 +02:00
|
|
|
PMU.disableBLDO1();
|
|
|
|
|
PMU.disableBLDO2();
|
|
|
|
|
PMU.disableDLDO1();
|
|
|
|
|
PMU.disableDLDO2();
|
|
|
|
|
PMU.setDC1Voltage(3300);
|
|
|
|
|
PMU.enableDC1();
|
2025-03-03 16:15:50 +01:00
|
|
|
#ifdef TTGO_T_Beam_S3_SUPREME_V3
|
|
|
|
|
PMU.setALDO1Voltage(3300);
|
|
|
|
|
#endif
|
2024-05-11 18:59:07 +02:00
|
|
|
PMU.setButtonBatteryChargeVoltage(3300);
|
|
|
|
|
PMU.enableButtonBatteryCharge();
|
|
|
|
|
PMU.disableIRQ(XPOWERS_AXP2101_ALL_IRQ);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2024-02-24 14:09:05 +01:00
|
|
|
#else
|
2024-05-11 18:59:07 +02:00
|
|
|
return true;
|
2024-02-24 14:09:05 +01:00
|
|
|
#endif
|
2024-01-03 05:13:44 +01:00
|
|
|
}
|
|
|
|
|
|
2024-02-24 14:09:05 +01:00
|
|
|
void setup() {
|
|
|
|
|
#ifdef HAS_AXP192
|
2024-05-11 18:59:07 +02:00
|
|
|
Wire.begin(SDA, SCL);
|
|
|
|
|
if (begin(Wire)) {
|
|
|
|
|
Serial.println("AXP192 init done!");
|
|
|
|
|
} else {
|
|
|
|
|
Serial.println("AXP192 init failed!");
|
|
|
|
|
}
|
|
|
|
|
activateLoRa();
|
|
|
|
|
activateMeasurement();
|
|
|
|
|
PMU.setChargerTerminationCurr(XPOWERS_AXP192_CHG_ITERM_LESS_10_PERCENT);
|
|
|
|
|
PMU.setChargeTargetVoltage(XPOWERS_AXP192_CHG_VOL_4V2);
|
|
|
|
|
PMU.setChargerConstantCurr(XPOWERS_AXP192_CHG_CUR_780MA);
|
|
|
|
|
PMU.setSysPowerDownVoltage(2600);
|
2024-02-24 14:09:05 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef HAS_AXP2101
|
2025-03-03 16:15:50 +01:00
|
|
|
bool beginStatus = false;
|
|
|
|
|
#ifdef TTGO_T_Beam_S3_SUPREME_V3
|
|
|
|
|
Wire1.begin(I2C1_SDA, I2C1_SCL);
|
|
|
|
|
Wire.begin(I2C0_SDA, I2C0_SCL);
|
|
|
|
|
if (begin(Wire1)) beginStatus = true;
|
|
|
|
|
#else
|
|
|
|
|
Wire.begin(SDA, SCL);
|
|
|
|
|
if (begin(Wire)) beginStatus = true;
|
|
|
|
|
#endif
|
|
|
|
|
if (beginStatus) {
|
2024-05-11 18:59:07 +02:00
|
|
|
Serial.println("AXP2101 init done!");
|
|
|
|
|
} else {
|
|
|
|
|
Serial.println("AXP2101 init failed!");
|
|
|
|
|
}
|
|
|
|
|
activateLoRa();
|
|
|
|
|
activateMeasurement();
|
|
|
|
|
PMU.setPrechargeCurr(XPOWERS_AXP2101_PRECHARGE_200MA);
|
|
|
|
|
PMU.setChargerTerminationCurr(XPOWERS_AXP2101_CHG_ITERM_25MA);
|
|
|
|
|
PMU.setChargeTargetVoltage(XPOWERS_AXP2101_CHG_VOL_4V2);
|
|
|
|
|
PMU.setChargerConstantCurr(XPOWERS_AXP2101_CHG_CUR_800MA);
|
|
|
|
|
PMU.setSysPowerDownVoltage(2600);
|
2024-02-24 14:09:05 +01:00
|
|
|
#endif
|
2024-04-19 17:06:26 +02:00
|
|
|
|
2024-05-02 18:57:18 +02:00
|
|
|
#ifdef BATTERY_PIN
|
2024-05-11 18:59:07 +02:00
|
|
|
pinMode(BATTERY_PIN, INPUT);
|
2024-05-02 18:57:18 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef INTERNAL_LED_PIN
|
2024-05-11 18:59:07 +02:00
|
|
|
pinMode(INTERNAL_LED_PIN, OUTPUT);
|
2024-05-02 18:57:18 +02:00
|
|
|
#endif
|
|
|
|
|
|
2024-05-24 20:24:40 +02:00
|
|
|
if (Config.battery.sendExternalVoltage || Config.battery.monitorExternalVoltage) {
|
2024-05-24 02:13:05 +02:00
|
|
|
pinMode(Config.battery.externalVoltagePin, INPUT);
|
2024-05-02 18:57:18 +02:00
|
|
|
}
|
|
|
|
|
|
2024-05-02 21:31:31 +02:00
|
|
|
#ifdef VEXT_CTRL
|
2024-05-14 05:30:15 +02:00
|
|
|
pinMode(VEXT_CTRL,OUTPUT); // GPS + TFT on HELTEC Wireless_Tracker and only for Oled in HELTEC V3
|
2025-01-10 15:53:52 +01:00
|
|
|
#if defined(HELTEC_WIRELESS_TRACKER) || defined(HELTEC_V3)
|
2024-05-16 21:43:13 +02:00
|
|
|
digitalWrite(VEXT_CTRL, HIGH);
|
|
|
|
|
#endif
|
2025-01-10 15:53:52 +01:00
|
|
|
#if defined(HELTEC_WP) || defined(HELTEC_WS) || defined(HELTEC_V3_2)
|
2024-08-16 21:52:20 +02:00
|
|
|
digitalWrite(VEXT_CTRL, LOW);
|
|
|
|
|
#endif
|
2024-05-02 18:57:18 +02:00
|
|
|
#endif
|
|
|
|
|
|
2024-10-14 22:04:28 +02:00
|
|
|
#ifdef HAS_GPS
|
|
|
|
|
if (Config.beacon.gpsActive) activateGPS();
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-05-02 21:31:31 +02:00
|
|
|
#ifdef ADC_CTRL
|
2024-05-11 18:59:07 +02:00
|
|
|
pinMode(ADC_CTRL, OUTPUT);
|
2024-04-19 17:06:26 +02:00
|
|
|
#endif
|
2024-05-11 18:02:08 +02:00
|
|
|
|
2024-09-11 20:49:58 +02:00
|
|
|
#if defined(HELTEC_WIRELESS_TRACKER)
|
2024-05-11 18:59:07 +02:00
|
|
|
Wire.begin(BOARD_I2C_SDA, BOARD_I2C_SCL);
|
2024-05-11 18:02:08 +02:00
|
|
|
#endif
|
2024-05-14 14:59:47 +02:00
|
|
|
|
2025-01-10 15:53:52 +01:00
|
|
|
#if defined(HELTEC_V3) || defined(HELTEC_V3_2) || defined(HELTEC_WS) || defined(LIGHTGATEWAY_1_0) || defined(TTGO_LORA32_T3S3_V1_2) || defined(HELTEC_V2)
|
2024-09-10 19:26:10 +02:00
|
|
|
Wire.begin(OLED_SDA, OLED_SCL);
|
|
|
|
|
#endif
|
2025-01-10 15:53:52 +01:00
|
|
|
|
|
|
|
|
#if defined(HELTEC_V3) || defined(HELTEC_V3_2) || defined(HELTEC_WP) || defined(HELTEC_WSL_V3) || defined(HELTEC_WSL_V3_DISPLAY)
|
|
|
|
|
Wire1.begin(BOARD_I2C_SDA, BOARD_I2C_SCL);
|
2025-01-22 03:00:49 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if defined(TTGO_T_DECK_GPS) || defined(TTGO_T_DECK_PLUS)
|
|
|
|
|
pinMode(BOARD_POWERON, OUTPUT);
|
|
|
|
|
digitalWrite(BOARD_POWERON, HIGH);
|
|
|
|
|
|
|
|
|
|
pinMode(BOARD_SDCARD_CS, OUTPUT);
|
|
|
|
|
pinMode(RADIO_CS_PIN, OUTPUT);
|
|
|
|
|
pinMode(TFT_CS, OUTPUT);
|
|
|
|
|
|
|
|
|
|
digitalWrite(BOARD_SDCARD_CS, HIGH);
|
|
|
|
|
digitalWrite(RADIO_CS_PIN, HIGH);
|
|
|
|
|
digitalWrite(TFT_CS, HIGH);
|
|
|
|
|
|
|
|
|
|
delay(500);
|
|
|
|
|
Wire.begin(BOARD_I2C_SDA, BOARD_I2C_SCL);
|
|
|
|
|
#endif
|
2024-05-11 18:02:08 +02:00
|
|
|
|
2024-05-02 18:57:18 +02:00
|
|
|
delay(1000);
|
2024-08-28 18:21:54 +02:00
|
|
|
BATTERY_Utils::setup();
|
2024-05-24 20:24:40 +02:00
|
|
|
BATTERY_Utils::startupBatteryHealth();
|
2024-01-03 05:13:44 +01:00
|
|
|
}
|
2024-02-24 14:09:05 +01:00
|
|
|
|
2024-01-03 05:13:44 +01:00
|
|
|
}
|