diff --git a/src/helpers/TBeamBoard.h b/src/helpers/TBeamBoard.h index fc52e712..8eba6933 100644 --- a/src/helpers/TBeamBoard.h +++ b/src/helpers/TBeamBoard.h @@ -7,6 +7,9 @@ // Defined using AXP2102 #define XPOWERS_CHIP_AXP2101 +#define PIN_BOARD_SDA1 42 //SDA for PMU and PFC8563 (RTC) +#define PIN_BOARD_SCL1 41 //SCL for PMU and PFC8563 (RTC) +#define PIN_PMU_IRQ 40 //IRQ pin for PMU // LoRa radio module pins for TBeam #define P_LORA_DIO_0 26 @@ -28,15 +31,13 @@ #include class TBeamBoard : public ESP32Board { - XPowersAXP2101 power; - + XPowersLibInterface *PMU = NULL; public: + bool power_init(); + void printPMU(); + void begin() { ESP32Board::begin(); - - power.setALDO2Voltage(3300); - power.enableALDO2(); - pinMode(38, INPUT_PULLUP); esp_reset_reason_t reason = esp_reset_reason(); @@ -49,6 +50,7 @@ public: rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS); rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1); } + power_init(); } void enterDeepSleep(uint32_t secs, int pin_wake_btn = -1) { @@ -75,7 +77,8 @@ public: } uint16_t getBattMilliVolts() override { - return power.getBattVoltage(); + if(PMU) return PMU->getBattVoltage(); + else return 0; } const char* getManufacturerName() const override { diff --git a/src/helpers/TBeamS3SupremeBoard.h b/src/helpers/TBeamS3SupremeBoard.h index ccb8e24c..47160787 100644 --- a/src/helpers/TBeamS3SupremeBoard.h +++ b/src/helpers/TBeamS3SupremeBoard.h @@ -75,6 +75,7 @@ public: rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS); rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1); } + power_init(); } void enterDeepSleep(uint32_t secs, int pin_wake_btn = -1) { diff --git a/variants/lilygo_tbeam/target.cpp b/variants/lilygo_tbeam/target.cpp index 116088d6..47c14297 100644 --- a/variants/lilygo_tbeam/target.cpp +++ b/variants/lilygo_tbeam/target.cpp @@ -3,6 +3,14 @@ TBeamBoard board; +// Using PMU AXP2102 +#define PMU_WIRE_PORT Wire + +bool pmuIntFlag = false; +static void setPMUIntFlag(){ + pmuIntFlag = true; +} + #if defined(P_LORA_SCLK) static SPIClass spi; RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_0, P_LORA_RESET, P_LORA_DIO_1, spi); @@ -24,6 +32,112 @@ SensorManager sensors; #define LORA_CR 5 #endif +bool TBeamBoard::power_init() +{ + if (!PMU) + { + PMU = new XPowersAXP2101(PMU_WIRE_PORT); + if (!PMU->init()) + { + // Serial.println("Warning: Failed to find AXP2101 power management"); + delete PMU; + PMU = NULL; + } + else + { + // Serial.println("AXP2101 PMU init succeeded, using AXP2101 PMU"); + } + } + if (!PMU) + { + PMU = new XPowersAXP192(PMU_WIRE_PORT); + if (!PMU->init()) + { + // Serial.println("Warning: Failed to find AXP192 power management"); + delete PMU; + PMU = NULL; + } + else + { + // Serial.println("AXP192 PMU init succeeded, using AXP192 PMU"); + } + } + + if (!PMU) + { + Serial.println("PMU init failed."); + return false; + } + + // Serial.printf("PMU ID:0x%x\n", PMU->getChipID()); + // printPMU(); + if (PMU->getChipModel() == XPOWERS_AXP192) + { + // lora radio power channel + PMU->setPowerChannelVoltage(XPOWERS_LDO2, 3300); + PMU->enablePowerOutput(XPOWERS_LDO2); + // 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); + // enable oled power + PMU->enablePowerOutput(XPOWERS_DCDC1); + // gnss module power channel + PMU->setPowerChannelVoltage(XPOWERS_LDO3, 3300); + // power->enablePowerOutput(XPOWERS_LDO3); + // 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); + PMU->setChargerConstantCurr(XPOWERS_AXP192_CHG_CUR_550MA); + } + else if (PMU->getChipModel() == XPOWERS_AXP2101) + { + // gnss module power channel + PMU->setPowerChannelVoltage(XPOWERS_ALDO4, 3300); + PMU->enablePowerOutput(XPOWERS_ALDO4); + // lora radio power channel + PMU->setPowerChannelVoltage(XPOWERS_ALDO3, 3300); + PMU->enablePowerOutput(XPOWERS_ALDO3); + // m.2 interface + PMU->setPowerChannelVoltage(XPOWERS_DCDC3, 3300); + PMU->enablePowerOutput(XPOWERS_DCDC3); + // power->setPowerChannelVoltage(XPOWERS_DCDC4, 3300); + // power->enablePowerOutput(XPOWERS_DCDC4); + // not use channel + PMU->disablePowerOutput(XPOWERS_DCDC2); // not elicited + PMU->disablePowerOutput(XPOWERS_DCDC5); // not elicited + PMU->disablePowerOutput(XPOWERS_DLDO1); // Invalid power channel, it does not exist + PMU->disablePowerOutput(XPOWERS_DLDO2); // Invalid power channel, it does not exist + PMU->disablePowerOutput(XPOWERS_VBACKUP); + // disable all axp chip interrupt + PMU->disableIRQ(XPOWERS_AXP2101_ALL_IRQ); + PMU->setChargerConstantCurr(XPOWERS_AXP2101_CHG_CUR_500MA); + + // Set up PMU interrupts + // Serial.println("Setting up PMU interrupts"); + pinMode(PIN_PMU_IRQ, INPUT_PULLUP); + attachInterrupt(PIN_PMU_IRQ, setPMUIntFlag, FALLING); + + // Reset and re-enable PMU interrupts + // Serial.println("Re-enable interrupts"); + PMU->disableIRQ(XPOWERS_AXP2101_ALL_IRQ); + PMU->clearIrqStatus(); + PMU->enableIRQ( + XPOWERS_AXP2101_BAT_INSERT_IRQ | XPOWERS_AXP2101_BAT_REMOVE_IRQ | // Battery interrupts + XPOWERS_AXP2101_VBUS_INSERT_IRQ | XPOWERS_AXP2101_VBUS_REMOVE_IRQ | // VBUS interrupts + XPOWERS_AXP2101_PKEY_SHORT_IRQ | XPOWERS_AXP2101_PKEY_LONG_IRQ | // Power Key interrupts + XPOWERS_AXP2101_BAT_CHG_DONE_IRQ | XPOWERS_AXP2101_BAT_CHG_START_IRQ // Charging interrupts + ); + } + + return true; +} + bool radio_init() { fallback_clock.begin(); rtc_clock.begin(Wire); @@ -66,3 +180,24 @@ mesh::LocalIdentity radio_new_identity() { RadioNoiseListener rng(radio); return mesh::LocalIdentity(&rng); // create new random identity } + +#ifdef MESH_DEBUG +void TBeamBoard::printPMU() +{ + Serial.print("isCharging:"); Serial.println(PMU->isCharging() ? "YES" : "NO"); + Serial.print("isDischarge:"); Serial.println(PMU->isDischarge() ? "YES" : "NO"); + Serial.print("isVbusIn:"); Serial.println(PMU->isVbusIn() ? "YES" : "NO"); + Serial.print("getBattVoltage:"); Serial.print(PMU->getBattVoltage()); Serial.println("mV"); + Serial.print("getVbusVoltage:"); Serial.print(PMU->getVbusVoltage()); Serial.println("mV"); + Serial.print("getSystemVoltage:"); Serial.print(PMU->getSystemVoltage()); Serial.println("mV"); + + // The battery percentage may be inaccurate at first use, the PMU will automatically + // learn the battery curve and will automatically calibrate the battery percentage + // after a charge and discharge cycle + if (PMU->isBatteryConnect()) { + Serial.print("getBatteryPercent:"); Serial.print(PMU->getBatteryPercent()); Serial.println("%"); + } + + Serial.println(); +} +#endif \ No newline at end of file