From ad1beffcf38d047e555b7c7d335f3ddd10b41228 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niccol=C3=B2=20Izzo?= Date: Thu, 31 Aug 2023 00:10:28 +0200 Subject: [PATCH] ttwrplus: implemented PMU interrupts handling TG-553 --- openrtx/src/core/threads.c | 7 ++ platform/drivers/keyboard/keyboard_ttwrplus.c | 1 + platform/targets/ttwrplus/pmu.cpp | 67 +++++++++++++++++-- platform/targets/ttwrplus/pmu.h | 2 + platform/targets/ttwrplus/ttwrplus.dts | 6 ++ 5 files changed, 79 insertions(+), 4 deletions(-) diff --git a/openrtx/src/core/threads.c b/openrtx/src/core/threads.c index 81c813f2..f9e0b68e 100644 --- a/openrtx/src/core/threads.c +++ b/openrtx/src/core/threads.c @@ -40,6 +40,9 @@ #endif #include +#if defined(PLATFORM_TTWRPLUS) +#include +#endif /* Mutex for concurrent access to RTX state variable */ pthread_mutex_t rtx_mutex; @@ -139,6 +142,10 @@ void *main_thread(void *arg) { time = getTick(); + #if defined(PLATFORM_TTWRPLUS) + pmu_handleIRQ(); + #endif + // Check if power off is requested pthread_mutex_lock(&state_mutex); if(platform_pwrButtonStatus() == false) diff --git a/platform/drivers/keyboard/keyboard_ttwrplus.c b/platform/drivers/keyboard/keyboard_ttwrplus.c index 760688e3..df5bccb5 100644 --- a/platform/drivers/keyboard/keyboard_ttwrplus.c +++ b/platform/drivers/keyboard/keyboard_ttwrplus.c @@ -22,6 +22,7 @@ #include #include #include +#include static const struct device *const buttons_dev = DEVICE_DT_GET(DT_NODELABEL(buttons)); diff --git a/platform/targets/ttwrplus/pmu.cpp b/platform/targets/ttwrplus/pmu.cpp index f2f2b8b0..812595fa 100644 --- a/platform/targets/ttwrplus/pmu.cpp +++ b/platform/targets/ttwrplus/pmu.cpp @@ -17,8 +17,10 @@ * along with this program; if not, see * ***************************************************************************/ +#include #include #include +#include #include #include "pmu.h" @@ -33,9 +35,12 @@ #error "Please set the correct I2C device" #endif +#define PMU_IRQ_NODE DT_ALIAS(pmu_irq) 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 int pmu_registerReadByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, @@ -73,6 +78,18 @@ void pmu_init() printk("I2C config failed\n"); } + // Configure IRQ gpio + if(gpio_is_ready_dt(&pmu_irq) == false) + { + printk("PMU IRQ gpio is not ready\n"); + } + + int ret = gpio_pin_configure_dt(&pmu_irq, GPIO_INPUT); + if (ret != 0) + { + printk("Failed to configure PMU IRQ gpio\n"); + } + bool result = PMU.begin(AXP2101_SLAVE_ADDRESS, pmu_registerReadByte, pmu_registerWriteByte); if (result == false) @@ -249,10 +266,11 @@ void pmu_init() PMU.clearIrqStatus(); // Enable the required interrupt function PMU.enableIRQ( - XPOWERS_AXP2101_BAT_INSERT_IRQ | XPOWERS_AXP2101_BAT_REMOVE_IRQ | //BATTERY - XPOWERS_AXP2101_VBUS_INSERT_IRQ | XPOWERS_AXP2101_VBUS_REMOVE_IRQ | //VBUS - XPOWERS_AXP2101_PKEY_SHORT_IRQ | XPOWERS_AXP2101_PKEY_LONG_IRQ | //POWER KEY - XPOWERS_AXP2101_BAT_CHG_DONE_IRQ | XPOWERS_AXP2101_BAT_CHG_START_IRQ //CHARGE + XPOWERS_AXP2101_BAT_INSERT_IRQ | XPOWERS_AXP2101_BAT_REMOVE_IRQ | // BATTERY + XPOWERS_AXP2101_VBUS_INSERT_IRQ | XPOWERS_AXP2101_VBUS_REMOVE_IRQ | // VBUS + XPOWERS_AXP2101_PKEY_POSITIVE_IRQ | XPOWERS_AXP2101_PKEY_NEGATIVE_IRQ | // POWER KEY ON/OFF + XPOWERS_AXP2101_PKEY_LONG_IRQ | // POWER KEY LONG PRESS + XPOWERS_AXP2101_BAT_CHG_DONE_IRQ | XPOWERS_AXP2101_BAT_CHG_START_IRQ // CHARGE ); // Set the precharge charging current @@ -315,3 +333,44 @@ void pmu_setGPSPower(bool enable) else PMU.disableALDO4(); } + +void pmu_handleIRQ() +{ + // Check if we got some interrupts + if(gpio_pin_get_dt(&pmu_irq) == 0) + return; + + uint32_t irqStatus = PMU.getIrqStatus(); + PMU.clearIrqStatus(); + + // Power on key rising edge + if((irqStatus & XPOWERS_AXP2101_PKEY_POSITIVE_IRQ) != 0) + pwrOnPressed = true; + + // Power on key falling edge + if((irqStatus & XPOWERS_AXP2101_PKEY_NEGATIVE_IRQ) != 0) + pwrOnPressed = false; + + // Power key long press + if ((irqStatus & XPOWERS_AXP2101_PKEY_LONG_IRQ) != 0) + { + // TODO Shutdown radio, set platform_pwrButtonStatus to false + PMU.shutdown(); + } + + // Charger start IRQ + if((irqStatus & XPOWERS_AXP2101_BAT_CHG_START_IRQ) != 0) + { + if(PMU.isBatteryConnect()) + PMU.setChargingLedMode(XPOWERS_CHG_LED_CTRL_CHG); + } + + // Battery remove IRQ + if((irqStatus & XPOWERS_AXP2101_BAT_REMOVE_IRQ) != 0) + PMU.setChargingLedMode(XPOWERS_CHG_LED_BLINK_1HZ); +} + +bool pmu_pwrOnBtnStatus() +{ + return pwrOnPressed; +} diff --git a/platform/targets/ttwrplus/pmu.h b/platform/targets/ttwrplus/pmu.h index 2e1fda5a..ef4bfe32 100644 --- a/platform/targets/ttwrplus/pmu.h +++ b/platform/targets/ttwrplus/pmu.h @@ -28,6 +28,8 @@ void pmu_init(); uint16_t pmu_getVbat(); void pmu_setBasebandPower(bool enable); void pmu_setGPSPower(bool enable); +void pmu_handleIRQ(); +bool pmu_pwrOnBtnStatus(); #ifdef __cplusplus } diff --git a/platform/targets/ttwrplus/ttwrplus.dts b/platform/targets/ttwrplus/ttwrplus.dts index 4975ebe2..fe5dc0d3 100644 --- a/platform/targets/ttwrplus/ttwrplus.dts +++ b/platform/targets/ttwrplus/ttwrplus.dts @@ -22,6 +22,7 @@ radio-pwr = &radio_pwr; radio-pdn = &radio_pdn; radio-ptt = &radio_ptt; + pmu-irq = &pmu_irq; qdec0 = &pcnt; led0 = &ws2812c; }; @@ -51,6 +52,11 @@ gpios = <&gpio1 9 GPIO_ACTIVE_HIGH>; label = "SA868S PTT on IO41"; }; + + pmu_irq: pmu_irq { + gpios = <&gpio0 4 GPIO_ACTIVE_LOW>; + label = "PMU IRQ Pin"; + }; }; buttons: buttons {