Skip to content

Commit

Permalink
XPowerlins / PMU fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberman54 committed Dec 1, 2024
1 parent 25434c0 commit aa0ffaa
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 36 deletions.
4 changes: 2 additions & 2 deletions docs/display-led.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion docs/hardware.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion include/i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions include/power.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions shared/hal/ttgobeam10.h
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion src/cyclic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/irqhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
10 changes: 7 additions & 3 deletions src/lorawan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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{};
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 19 additions & 19 deletions src/power.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
Expand All @@ -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);
Expand All @@ -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",
Expand All @@ -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);
Expand Down Expand Up @@ -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

Expand All @@ -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");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/reset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit aa0ffaa

Please sign in to comment.