From 50c6ba2837af2d296a16adfc73245a060b1bac7e Mon Sep 17 00:00:00 2001 From: Verkehrsrot Date: Mon, 9 Sep 2019 21:45:19 +0200 Subject: [PATCH] T-Beam V10 (AXP192 PMU) enhanced power functions --- include/irqhandler.h | 6 ++++ include/power.h | 5 +++- src/irqhandler.cpp | 18 ++++++++++++ src/power.cpp | 65 ++++++++++++++++++++++++++++++++------------ 4 files changed, 75 insertions(+), 19 deletions(-) diff --git a/include/irqhandler.h b/include/irqhandler.h index 08d6bd8c..193e32d5 100644 --- a/include/irqhandler.h +++ b/include/irqhandler.h @@ -10,12 +10,14 @@ #define UNMASK_IRQ 0x040 #define BME_IRQ 0x080 #define MATRIX_DISPLAY_IRQ 0x100 +#define PMU_IRQ 0x200 #include "globals.h" #include "cyclic.h" #include "senddata.h" #include "timekeeper.h" #include "bmesensor.h" +#include "power.h" void irqHandler(void *pvParameters); void mask_user_IRQ(); @@ -33,5 +35,9 @@ void IRAM_ATTR MatrixDisplayIRQ(); void IRAM_ATTR ButtonIRQ(); #endif +#ifdef HAS_PMU +void IRAM_ATTR PMUIRQ(); +#endif + #endif \ No newline at end of file diff --git a/include/power.h b/include/power.h index 849ea3b1..ab738253 100644 --- a/include/power.h +++ b/include/power.h @@ -11,11 +11,14 @@ #ifdef HAS_PMU #include +extern AXP20X_Class pmu; // Make axp instance globally availabe +void pover_event_IRQ(void); #endif void AXP192_init(void); uint16_t read_voltage(void); void calibrate_voltage(void); -uint8_t getBattLevel (void); +uint8_t getBattLevel(void); + #endif \ No newline at end of file diff --git a/src/irqhandler.cpp b/src/irqhandler.cpp index db4201a1..74bcd29f 100644 --- a/src/irqhandler.cpp +++ b/src/irqhandler.cpp @@ -63,6 +63,12 @@ void irqHandler(void *pvParameters) { } #endif +// do we have a power event? +#if (HAS_PMU) + if (InterruptStatus & PMU_IRQ) + pover_event_IRQ(); +#endif + // is time to send the payload? if (InterruptStatus & SENDCYCLE_IRQ) sendData(); @@ -106,6 +112,18 @@ void IRAM_ATTR ButtonIRQ() { } #endif +#ifdef HAS_PMU +void IRAM_ATTR PMUIRQ() { + BaseType_t xHigherPriorityTaskWoken = pdFALSE; + + xTaskNotifyFromISR(irqHandlerTask, PMU_IRQ, eSetBits, + &xHigherPriorityTaskWoken); + + if (xHigherPriorityTaskWoken) + portYIELD_FROM_ISR(); +} +#endif + void mask_user_IRQ() { xTaskNotify(irqHandlerTask, MASK_IRQ, eSetBits); } void unmask_user_IRQ() { xTaskNotify(irqHandlerTask, UNMASK_IRQ, eSetBits); } \ No newline at end of file diff --git a/src/power.cpp b/src/power.cpp index 89aac6ae..ae1ce802 100644 --- a/src/power.cpp +++ b/src/power.cpp @@ -9,6 +9,43 @@ static const char TAG[] = __FILE__; AXP20X_Class pmu; +void pover_event_IRQ(void) { + // block i2c bus access + if (I2C_MUTEX_LOCK()) { + pmu.readIRQ(); + // put your power event handler code here + + if (pmu.isVbusOverVoltageIRQ()) + ESP_LOGI(TAG, "USB voltage too high."); + if (pmu.isVbusPlugInIRQ()) + ESP_LOGI(TAG, "USB plugged."); + if (pmu.isVbusRemoveIRQ()) + ESP_LOGI(TAG, "USB unplugged."); + + if (pmu.isBattPlugInIRQ()) + ESP_LOGI(TAG, "Battery is connected."); + if (pmu.isBattRemoveIRQ()) + ESP_LOGI(TAG, "Battery was removed."); + if (pmu.isChargingIRQ()) + ESP_LOGI(TAG, "Battery is charging."); + if (pmu.isChargingDoneIRQ()) + ESP_LOGI(TAG, "Battery charging done."); + if (pmu.isBattTempLowIRQ()) + ESP_LOGI(TAG, "Battery high temperature."); + if (pmu.isBattTempHighIRQ()) + ESP_LOGI(TAG, "Battery low temperature."); + + if (pmu.isPEKShortPressIRQ()) + ESP_LOGI(TAG, "Power Button short pressed."); + if (pmu.isPEKLongtPressIRQ()) + ESP_LOGI(TAG, "Power Button long pressed."); + + pmu.clearIRQ(); + I2C_MUTEX_UNLOCK(); // release i2c bus access + } else + ESP_LOGI(TAG, "Unknown PMU event."); +} + void AXP192_init(void) { // block i2c bus access @@ -27,25 +64,17 @@ void AXP192_init(void) { pmu.setChgLEDMode(AXP20X_LED_LOW_LEVEL); pmu.adc1Enable(AXP202_BATT_CUR_ADC1, 1); - /* - // I2C access of AXP202X library currently is not mutexable - // so we need to disable AXP interrupts - + // I2C access of AXP202X library currently is not mutexable + // so we need to disable AXP interrupts - #ifdef PMU_INT - pinMode(PMU_INT, INPUT_PULLUP); - attachInterrupt(digitalPinToInterrupt(PMU_INT), - [] { - ESP_LOGI(TAG, "Power source changed"); - // put your code here - }, - FALLING); - pmu.enableIRQ(AXP202_VBUS_REMOVED_IRQ | AXP202_VBUS_CONNECT_IRQ | - AXP202_BATT_REMOVED_IRQ | AXP202_BATT_CONNECT_IRQ, - 1); - pmu.clearIRQ(); - #endif // PMU_INT - */ +#ifdef PMU_INT + pinMode(PMU_INT, INPUT_PULLUP); + attachInterrupt(digitalPinToInterrupt(PMU_INT), PMUIRQ, FALLING); + pmu.enableIRQ(AXP202_VBUS_REMOVED_IRQ | AXP202_VBUS_CONNECT_IRQ | + AXP202_BATT_REMOVED_IRQ | AXP202_BATT_CONNECT_IRQ, + 1); + pmu.clearIRQ(); +#endif // PMU_INT ESP_LOGI(TAG, "AXP192 PMU initialized.");