diff --git a/platform/drivers/keyboard/keyboard_ttwrplus.c b/platform/drivers/keyboard/keyboard_ttwrplus.c index 462d1329..f1c4018c 100644 --- a/platform/drivers/keyboard/keyboard_ttwrplus.c +++ b/platform/drivers/keyboard/keyboard_ttwrplus.c @@ -113,7 +113,7 @@ keyboard_t kbd_getKeys() * The power key is only connected to the PMU and its state is detected through * the PMU interrupts. */ - if (pmu_pwrOnBtnStatus()) + if (pmu_pwrBtnStatus() == 1) { // Update the volume only once when key is pressed if (!(keys & KEY_VOLUP) && volume_level < 246) diff --git a/platform/targets/ttwrplus/platform.c b/platform/targets/ttwrplus/platform.c index 9dec316c..4679969e 100644 --- a/platform/targets/ttwrplus/platform.c +++ b/platform/targets/ttwrplus/platform.c @@ -107,6 +107,7 @@ void platform_init() void platform_terminate() { + pmu_terminate(); } uint16_t platform_getVbat() @@ -153,6 +154,11 @@ bool platform_getPttStatus() bool platform_pwrButtonStatus() { + // Long press of the power on button triggers a shutdown + uint8_t btnStatus = pmu_pwrBtnStatus(); + if(btnStatus == 2) + return false; + return true; } diff --git a/platform/targets/ttwrplus/pmu.cpp b/platform/targets/ttwrplus/pmu.cpp index ec3e7c62..31918937 100644 --- a/platform/targets/ttwrplus/pmu.cpp +++ b/platform/targets/ttwrplus/pmu.cpp @@ -40,7 +40,7 @@ static const struct device *const i2c_dev = DEVICE_DT_GET(I2C_DEV_NODE); static const struct gpio_dt_spec pmu_irq = GPIO_DT_SPEC_GET(PMU_IRQ_NODE, gpios); static XPowersPMU PMU; -static bool pwrOnPressed = false; +static uint8_t pwrOnPressed = 0; static int pmu_registerReadByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, @@ -313,6 +313,13 @@ void pmu_init() printk("Setting Charge Target Voltage : %d\n", tableVoltage[val]); } +void pmu_terminate() +{ + PMU.disableDC3(); // Turn off baseband power + PMU.disableALDO4(); // Turn off GPS power + PMU.shutdown(); // General shutdown +} + uint16_t pmu_getVbat() { return PMU.isBatteryConnect() ? PMU.getBattVoltage() : 0; @@ -345,18 +352,15 @@ void pmu_handleIRQ() // Power on key rising edge if((irqStatus & XPOWERS_AXP2101_PKEY_POSITIVE_IRQ) != 0) - pwrOnPressed = false; + pwrOnPressed = 0; // Power on key falling edge if((irqStatus & XPOWERS_AXP2101_PKEY_NEGATIVE_IRQ) != 0) - pwrOnPressed = true; + pwrOnPressed = 1; // Power key long press if ((irqStatus & XPOWERS_AXP2101_PKEY_LONG_IRQ) != 0) - { - // TODO Shutdown radio, set platform_pwrButtonStatus to false - PMU.shutdown(); - } + pwrOnPressed = 2; // Charger start IRQ if((irqStatus & XPOWERS_AXP2101_BAT_CHG_START_IRQ) != 0) @@ -370,7 +374,7 @@ void pmu_handleIRQ() PMU.setChargingLedMode(XPOWERS_CHG_LED_BLINK_1HZ); } -bool pmu_pwrOnBtnStatus() +uint8_t pmu_pwrBtnStatus() { return pwrOnPressed; } diff --git a/platform/targets/ttwrplus/pmu.h b/platform/targets/ttwrplus/pmu.h index ef4bfe32..f6992b39 100644 --- a/platform/targets/ttwrplus/pmu.h +++ b/platform/targets/ttwrplus/pmu.h @@ -20,16 +20,59 @@ #ifndef PMU_H #define PMU_H +#include + #ifdef __cplusplus extern "C" { #endif +/** + * Initalise the AXP2101 power management unit and power on the main power + * supply rails. + */ void pmu_init(); + +/** + * Shutdown the AXP2101 and remove power from all the power supply rails. + * Calling this function shuts down the device. + */ +void pmu_terminate(); + +/** + * Get current battery voltage. + * + * @return battery voltage in mV. + */ uint16_t pmu_getVbat(); + +/** + * Control the baseband power supply rail. + * + * @param enable: set to true to enable the baseband power supply. + */ void pmu_setBasebandPower(bool enable); + +/** + * Control the GPS power supply rail. + * + * @param enable: set to true to enable the GPS power supply. + */ void pmu_setGPSPower(bool enable); + +/** + * Handle the AXP2101 IRQ flags. + * This function has to be called periodically. + */ void pmu_handleIRQ(); -bool pmu_pwrOnBtnStatus(); + +/** + * Get the current status of the power button input. + * The function returns zero if the button is released, one if is pressed and + * two in case of long press. + * + * @return current status of the power button. + */ +uint8_t pmu_pwrBtnStatus(); #ifdef __cplusplus }