LilyGO / TTGO-T-Beam

336 stars 112 forks source link

How to turn the power off when there is a 18650 battery installed? #52

Open HouzuoGuo opened 3 years ago

HouzuoGuo commented 3 years ago

Hello, I have a simple generic sketch running on the TTGo T-Beam (board revision v1.1, 20191212). The sketch displays an image on OLED and that's about it.

When T-Beam is only powered by USB, I can turn the board off by pressing the power button for ~6.5 seconds. However, this does not turn the board off when the board is only powered by a 18650 cell.

So how does the power button work when there's a battery installed?

kaangoksal commented 2 years ago

I have the same version and it turns off when you hold it down.. what do you mean it doesn't turn off?

VeniceInventors commented 2 years ago

this does not turn the board off when the board is only powered by a 18650 cell. I read somewhere on the LilyGo docs that if the battery voltage is low, the APX chip may be unable to react to the power button. Not sure if that's your issue, or if it even applies to switching it off.

Here with board version 20210222 holding the power button down for 6 seconds turns it off when powered by the battery and/or USB, however, the blue CHG (charging) LED stays on even when there is no USB power source. [edit] It may be that the AXP192 charging chip (https://github.com/lewisxhe/AXP202X_Library) needs to be set to turn everything off in the code [/edit]

kaangoksal commented 2 years ago

You need to do this, so it is off when there is power supplied by the battery. I think the axp chip is always on, so when there is power it wakes up and turns on the led, also waits for the turn on button.

axp.setChgLEDMode(AXP20X_LED_OFF);

HouzuoGuo commented 2 years ago

Thanks all.

I learned that the AXP chip is quite configurable - and somehow it also persists the configuration once they're set, I still haven't quite figured out the exact mechanism of the persistence.

What I think happened was that the program preinstalled before delivery did not configure the power button to perform power-off, or it may have misconfigured the button to do nothing at all.

To power off the entire board regardless of whether it's powered by a battery alone or USB, I just had to use a couple of lines of code to configure the AXP chip:

static AXP20X_Class pmu;
...
    if (pmu.begin(Wire, AXP192_SLAVE_ADDRESS) != AXP_PASS)
    {
        ESP_LOGW(LOG_TAG, "failed to initialise AXP power management chip");
    }

    pmu.setDCDC1Voltage(3300); // OLED
    pmu.setDCDC2Voltage(0);    // Unused
    pmu.setLDO2Voltage(3300);  // LoRa
    pmu.setLDO3Voltage(3300);  // GPS

    pmu.setVWarningLevel1(3600);
    pmu.setVWarningLevel2(3800);
    pmu.setPowerDownVoltage(3300);

    pmu.setTimeOutShutdown(false);
    pmu.setTSmode(AXP_TS_PIN_MODE_DISABLE);
    pmu.setShutdownTime(AXP_POWER_OFF_TIME_4S);
    pmu.setStartupTime(AXP192_STARTUP_TIME_1S);

    // Turn on ADCs.
    pmu.adc1Enable(AXP202_BATT_VOL_ADC1, true);
    pmu.adc1Enable(AXP202_BATT_CUR_ADC1, true);
    pmu.adc1Enable(AXP202_VBUS_VOL_ADC1, true);
    pmu.adc1Enable(AXP202_VBUS_CUR_ADC1, true);

    // Handle power management events.
    pinMode(GPIO_NUM_35, INPUT_PULLUP);
    pmu.enableIRQ(AXP202_VBUS_REMOVED_IRQ | AXP202_VBUS_CONNECT_IRQ | AXP202_VBUS_OVER_VOL_IRQ | AXP202_BATT_REMOVED_IRQ |
                      AXP202_BATT_CONNECT_IRQ | AXP202_CHARGING_FINISHED_IRQ | AXP202_PEK_SHORTPRESS_IRQ,
                  1);
    pmu.clearIRQ();

    // Start charging the battery if it is installed.
    pmu.setChargeControlCur(AXP1XX_CHARGE_CUR_450MA);
    pmu.setChargingTargetVoltage(AXP202_TARGET_VOL_4_2V);
    pmu.enableChargeing(true);
    pmu.setChgLEDMode(AXP20X_LED_OFF);

    // Keep the on-board clock (& GPS) battery topped up.
    pmu.setBackupChargeCurrent(AXP202_BACKUP_CURRENT_100UA);
    pmu.setBackupChargeVoltage(AXP202_BACKUP_VOLTAGE_3V0);
    pmu.setBackupChargeControl(true);

    pmu.setPowerOutPut(AXP192_DCDC1, AXP202_ON);  // OLED
    pmu.setPowerOutPut(AXP192_DCDC2, AXP202_OFF); // Unused
    pmu.setPowerOutPut(AXP192_LDO2, AXP202_ON);   // LoRa
    pmu.setPowerOutPut(AXP192_LDO3, AXP202_ON);   // GPS
    pmu.setPowerOutPut(AXP192_EXTEN, AXP202_OFF); // Unused

(See also https://github.com/HouzuoGuo/hzgl-lora-communicator/blob/master/src/power_management.cpp)

VeniceInventors commented 2 years ago

axp.setChgLEDMode(AXP20X_LED_OFF);

Thanks for the hint. It helped. My T-Beam came with SoftRF (which makes extensive use of the axp20X) library, but I promptly reflashed it with my own code while it was still charging the battery, so the LED got stuck in that state with no code to tell the chip to turn it off. Much like Howard, I'm not sure how the settings may be retained by the axp, so I'll read the datasheet. Hopefully it has sane defaults so that it isn't necessary to add the axp configuration to all the .inos I might want to try on this board.