From aa0ffaad1cd80118b2b02699c819a14347f82938 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Mon, 2 Dec 2024 00:02:31 +0100 Subject: [PATCH] XPowerlins / PMU fixes --- docs/display-led.md | 4 ++-- docs/hardware.md | 2 +- include/i2c.h | 2 +- include/power.h | 8 ++++---- shared/hal/ttgobeam10.h | 4 ++-- src/cyclic.cpp | 2 +- src/irqhandler.cpp | 2 +- src/lorawan.cpp | 10 +++++++--- src/main.cpp | 2 +- src/power.cpp | 38 +++++++++++++++++++------------------- src/reset.cpp | 2 +- 11 files changed, 40 insertions(+), 36 deletions(-) diff --git a/docs/display-led.md b/docs/display-led.md index 7c38147ec..a925dda83 100644 --- a/docs/display-led.md +++ b/docs/display-led.md @@ -46,8 +46,8 @@ This way the display is still positioned correctly to fit in the many available Some boards like the TTGO T-beam v1.0 and newer do have a power management chip. -- T-beam v1.0 and v1.1 use the AXP192 -- T-beam v1.2 uses AXP2101 +- T-Beam v1.0 and v1.1 use AXP192 +- T-Beam v1.2 and T-Supreme use AXP2101 At least the ones using the AXP192 power management chip do have a annoying oversight in the board design. diff --git a/docs/hardware.md b/docs/hardware.md index 13f66e2ce..5d7c8aa3e 100644 --- a/docs/hardware.md +++ b/docs/hardware.md @@ -35,7 +35,7 @@ Depending on board hardware following features are supported: - [OLED Display](display-led.md) (shows detailed status) - RGB LED (shows colorized status) - Button (short press: flip display page / long press: send alarm message) -- Battery voltage monitoring (analog read / AXP192 / IP5306) +- Battery voltage monitoring (analog read / AXP192 / AXP202 / AXP2101 / IP5306) - GPS (Generic serial NMEA, or Quectel L76 I2C) - Environmental sensors (Bosch BMP180/BME280/BME680/BMP280 I2C; SDS011 serial) - Real Time Clock (Maxim DS3231 I2C) diff --git a/include/i2c.h b/include/i2c.h index 2fc6e64ce..3fc172f6b 100644 --- a/include/i2c.h +++ b/include/i2c.h @@ -18,7 +18,7 @@ #define SSD1306_SECONDARY_ADDRESS (0x3C) #define BME_PRIMARY_ADDRESS (0x77) #define BME_SECONDARY_ADDRESS (0x76) -#define AXP192_PRIMARY_ADDRESS (0x34) +#define PMU_PRIMARY_ADDRESS (0x34) #define IP5306_PRIMARY_ADDRESS (0x75) #define MCP_24AA02E64_PRIMARY_ADDRESS (0x50) #define QUECTEL_GPS_PRIMARY_ADDRESS (0x10) diff --git a/include/power.h b/include/power.h index 1bbcbae36..e28069ce7 100644 --- a/include/power.h +++ b/include/power.h @@ -60,10 +60,10 @@ extern int8_t batt_level; extern XPowersPMU pmu; enum pmu_power_t { pmu_power_on, pmu_power_off, pmu_power_sleep }; void IRAM_ATTR PMUIRQ(); -void AXP192_powerevent_IRQ(void); -void AXP192_power(pmu_power_t powerlevel); -void AXP192_init(void); -void AXP192_showstatus(void); +void PMU_powerevent_IRQ(void); +void PMU_power(pmu_power_t powerlevel); +void PMU_init(void); +void PMU_showstatus(void); #endif // HAS_PMU #ifdef HAS_IP5306 diff --git a/shared/hal/ttgobeam10.h b/shared/hal/ttgobeam10.h index dbceeba64..f0342bbe8 100644 --- a/shared/hal/ttgobeam10.h +++ b/shared/hal/ttgobeam10.h @@ -33,8 +33,8 @@ Reset -> reset device #define LED_ACTIVE_LOW 1 // power management settings -#define HAS_PMU 1 // has AXP192 chip -#define XPOWERS_CHIP_AXP192 1 +#define HAS_PMU 1 // has PMU chip +#define XPOWERS_CHIP_AXP192 1 // PMU chip type AXP192 #define PMU_INT GPIO_NUM_35 // battery interrupt #define PMU_CHG_CURRENT XPOWERS_AXP192_CHG_CUR_1000MA // battery charge current // possible values (mA): diff --git a/src/cyclic.cpp b/src/cyclic.cpp index 839a8929e..8b09677b8 100644 --- a/src/cyclic.cpp +++ b/src/cyclic.cpp @@ -80,7 +80,7 @@ void doHousekeeping() { #if (defined BAT_MEASURE_ADC || defined HAS_PMU || defined HAS_IP5306) batt_level = read_battlevel(); #ifdef HAS_PMU - AXP192_showstatus(); + PMU_showstatus(); #endif #ifdef HAS_IP5306 printIP5306Stats(); diff --git a/src/irqhandler.cpp b/src/irqhandler.cpp index 9c84b13c2..2df501879 100644 --- a/src/irqhandler.cpp +++ b/src/irqhandler.cpp @@ -65,7 +65,7 @@ void irqHandler(void *pvParameters) { // do we have a power event? #ifdef HAS_PMU if (irqSource & PMU_IRQ) - AXP192_powerevent_IRQ(); + PMU_powerevent_IRQ(); #endif // is time to send the payload? diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 82cbd5b7f..7c7b0be10 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -26,10 +26,14 @@ class MyHalConfig_t : public Arduino_LMIC::HalConfiguration_t { void begin(void) override { SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_CS); } - // void end(void) override - // ostime_t setModuleActive(bool state) override + #ifdef LORA_BUSY // assuming SX126x chip + u1_t queryBusyPin(void) override { return LORA_BUSY; }; + bool queryUsingDcdc(void) override { return true; }; + bool queryUsingDIO2AsRfSwitch(void) override { return true; }; + bool queryUsingDIO3AsTCXOSwitch(void) override { return true; }; + #endif }; static MyHalConfig_t myHalConfig{}; @@ -39,7 +43,7 @@ static const lmic_pinmap myPinmap = { .nss = LORA_CS, .rxtx = LMIC_UNUSED_PIN, .rst = LORA_RST == NOT_A_PIN ? LMIC_UNUSED_PIN : LORA_RST, - .dio = {LORA_IRQ == NOT_A_PIN ? LMIC_UNUSED_PIN : LORA_IRQ, + .dio = {LORA_IRQ, LORA_IO1 == NOT_A_PIN ? LMIC_UNUSED_PIN : LORA_IO1, LORA_IO2 == NOT_A_PIN ? LMIC_UNUSED_PIN : LORA_IO2}, .rxtx_rx_active = LMIC_UNUSED_PIN, diff --git a/src/main.cpp b/src/main.cpp index 0c306a31f..6a80fefed 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -186,7 +186,7 @@ void setup() { #if defined HAS_PMU || defined HAS_IP5306 #ifdef HAS_PMU - AXP192_init(); + PMU_init(); #elif defined HAS_IP5306 IP5306_init(); #endif diff --git a/src/power.cpp b/src/power.cpp index 723283493..8533c23a6 100644 --- a/src/power.cpp +++ b/src/power.cpp @@ -25,7 +25,7 @@ XPowersPMU pmu; void IRAM_ATTR PMUIRQ() { doIRQ(PMU_IRQ); } -void AXP192_powerevent_IRQ(void) { +void PMU_powerevent_IRQ(void) { pmu.getIrqStatus(); if (pmu.isVbusOverVoltageIrq()) @@ -51,7 +51,7 @@ void AXP192_powerevent_IRQ(void) { // PEK button handling: // long press -> shutdown power, must be exited by another longpress if (pmu.isPekeyLongPressIrq()) - AXP192_power(pmu_power_off); // switch off Lora, GPS, display + PMU_power(pmu_power_off); // switch off Lora, GPS, display #ifdef HAS_BUTTON // short press -> esp32 deep sleep mode, must be exited by user button if (pmu.isPekeyShortPressIrq()) @@ -64,7 +64,7 @@ void AXP192_powerevent_IRQ(void) { read_battlevel(); } -void AXP192_power(pmu_power_t powerlevel) { +void PMU_power(pmu_power_t powerlevel) { switch (powerlevel) { case pmu_power_off: pmu.setChargingLedMode(XPOWERS_CHG_LED_OFF); @@ -88,7 +88,7 @@ void AXP192_power(pmu_power_t powerlevel) { } } -void AXP192_showstatus(void) { +void PMU_showstatus(void) { if (pmu.isBatteryConnect()) if (pmu.isCharging()) ESP_LOGI(TAG, "Battery charging, %.2fV @ %.0fmAh", @@ -105,11 +105,11 @@ void AXP192_showstatus(void) { ESP_LOGI(TAG, "USB not present"); } -void AXP192_init(void) { - if (!pmu.begin(Wire, AXP192_PRIMARY_ADDRESS, SCL, SDA)) - ESP_LOGI(TAG, "AXP192 PMU initialization failed"); +void PMU_init(void) { + if (!pmu.begin(Wire, PMU_PRIMARY_ADDRESS, SCL, SDA)) + ESP_LOGI(TAG, "PMU initialization failed"); else { - ESP_LOGD(TAG, "AXP192 ChipID:0x%x", pmu.getChipID()); + ESP_LOGD(TAG, "PMU ChipID:0x%x", pmu.getChipID()); // set pmu operating voltages pmu.setSysPowerDownVoltage(2700); @@ -138,18 +138,18 @@ void AXP192_init(void) { pinMode(PMU_INT, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(PMU_INT), PMUIRQ, FALLING); // disable all interrupts - pmu.disableIRQ(XPOWERS_AXP192_ALL_IRQ); + pmu.disableIRQ(XPOWERS_ALL_INT); // clear all interrupt flags pmu.clearIrqStatus(); // enable the required interrupt function - pmu.enableIRQ(XPOWERS_AXP192_BAT_INSERT_IRQ | - XPOWERS_AXP192_BAT_REMOVE_IRQ | // BATTERY - XPOWERS_AXP192_VBUS_INSERT_IRQ | - XPOWERS_AXP192_VBUS_REMOVE_IRQ | // VBUS - XPOWERS_AXP192_PKEY_SHORT_IRQ | - XPOWERS_AXP192_PKEY_LONG_IRQ | // POWER KEY - XPOWERS_AXP192_BAT_CHG_DONE_IRQ | - XPOWERS_AXP192_BAT_CHG_START_IRQ // CHARGE + pmu.enableIRQ(XPOWERS_BATTERY_INSERT_INT | + XPOWERS_BATTERY_REMOVE_INT | // battery state + XPOWERS_USB_INSERT_INT | + XPOWERS_USB_REMOVE_INT | // USB state + XPOWERS_PWR_BTN_CLICK_INT | + XPOWERS_PWR_BTN_LONGPRESSED_INT | // Power button + XPOWERS_CHARGE_DONE_INT | + XPOWERS_CHARGE_START_INT // Battery charging state ); #endif // PMU_INT @@ -161,8 +161,8 @@ void AXP192_init(void) { #endif // switch power rails on - AXP192_power(pmu_power_on); - ESP_LOGI(TAG, "AXP192 PMU initialized"); + PMU_power(pmu_power_on); + ESP_LOGI(TAG, "PMU initialized"); } } diff --git a/src/reset.cpp b/src/reset.cpp index 51d463ca1..68d0478be 100644 --- a/src/reset.cpp +++ b/src/reset.cpp @@ -160,7 +160,7 @@ void enter_deepsleep(uint32_t wakeup_sec, gpio_num_t wakeup_gpio) { // reduce power if has PMU or VEXT #ifdef HAS_PMU - AXP192_power(pmu_power_sleep); + PMU_power(pmu_power_sleep); #elif EXT_POWER_SW digitalWrite(EXT_POWER_SW, EXT_POWER_OFF); #endif