2019-09-07 19:52:25 +02:00
|
|
|
// Basic config
|
|
|
|
#include "globals.h"
|
|
|
|
#include "power.h"
|
|
|
|
|
2019-09-07 23:10:53 +02:00
|
|
|
// Local logging tag
|
|
|
|
static const char TAG[] = __FILE__;
|
2019-09-07 19:52:25 +02:00
|
|
|
|
2019-09-07 23:10:53 +02:00
|
|
|
#ifdef HAS_PMU
|
2019-09-07 19:52:25 +02:00
|
|
|
|
2019-09-09 12:06:23 +02:00
|
|
|
AXP20X_Class pmu;
|
2019-09-07 19:52:25 +02:00
|
|
|
|
2019-09-09 21:45:19 +02:00
|
|
|
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())
|
2019-09-18 22:38:39 +02:00
|
|
|
ESP_LOGI(TAG, "USB voltage %.1fV too high.", pmu.getVbusVoltage());
|
2019-09-09 21:45:19 +02:00
|
|
|
if (pmu.isVbusPlugInIRQ())
|
2019-09-20 18:11:05 +02:00
|
|
|
ESP_LOGI(TAG, "USB plugged.");
|
2019-09-09 21:45:19 +02:00
|
|
|
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())
|
2019-09-18 22:38:39 +02:00
|
|
|
ESP_LOGI(TAG, "Battery charging.");
|
2019-09-09 21:45:19 +02:00
|
|
|
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.");
|
|
|
|
|
2019-09-09 23:00:06 +02:00
|
|
|
// wake up
|
|
|
|
if (pmu.isPEKShortPressIRQ()) {
|
2019-09-09 21:45:19 +02:00
|
|
|
ESP_LOGI(TAG, "Power Button short pressed.");
|
2019-09-09 23:00:06 +02:00
|
|
|
AXP192_power(true);
|
|
|
|
}
|
|
|
|
// enter sleep mode
|
|
|
|
if (pmu.isPEKLongtPressIRQ()) {
|
2019-09-09 21:45:19 +02:00
|
|
|
ESP_LOGI(TAG, "Power Button long pressed.");
|
2019-09-09 23:00:06 +02:00
|
|
|
AXP192_power(false);
|
|
|
|
delay(20);
|
|
|
|
esp_sleep_enable_ext1_wakeup(GPIO_SEL_38, ESP_EXT1_WAKEUP_ALL_LOW);
|
|
|
|
esp_deep_sleep_start();
|
|
|
|
}
|
2019-09-09 21:45:19 +02:00
|
|
|
|
|
|
|
pmu.clearIRQ();
|
|
|
|
I2C_MUTEX_UNLOCK(); // release i2c bus access
|
|
|
|
} else
|
|
|
|
ESP_LOGI(TAG, "Unknown PMU event.");
|
|
|
|
}
|
|
|
|
|
2019-09-09 23:00:06 +02:00
|
|
|
void AXP192_power(bool on) {
|
|
|
|
|
|
|
|
if (on) {
|
2019-09-20 18:11:05 +02:00
|
|
|
pmu.setPowerOutPut(AXP192_LDO2, AXP202_ON); // Lora on T-Beam V1.0
|
|
|
|
pmu.setPowerOutPut(AXP192_LDO3, AXP202_ON); // Gps on T-Beam V1.0
|
2019-09-09 23:00:06 +02:00
|
|
|
pmu.setPowerOutPut(AXP192_DCDC1, AXP202_ON);
|
2019-09-20 18:11:05 +02:00
|
|
|
pmu.setPowerOutPut(AXP192_DCDC3, AXP202_ON);
|
|
|
|
pmu.setPowerOutPut(AXP192_EXTEN, AXP202_ON);
|
|
|
|
pmu.setChgLEDMode(AXP20X_LED_BLINK_1HZ);
|
2019-09-09 23:00:06 +02:00
|
|
|
} else {
|
|
|
|
pmu.setPowerOutPut(AXP192_EXTEN, AXP202_OFF);
|
2019-09-20 18:11:05 +02:00
|
|
|
pmu.setPowerOutPut(AXP192_DCDC3, AXP202_OFF);
|
|
|
|
pmu.setPowerOutPut(AXP192_DCDC1, AXP202_OFF);
|
2019-09-09 23:00:06 +02:00
|
|
|
pmu.setPowerOutPut(AXP192_LDO3, AXP202_OFF);
|
|
|
|
pmu.setPowerOutPut(AXP192_LDO2, AXP202_OFF);
|
|
|
|
pmu.setChgLEDMode(AXP20X_LED_OFF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 22:38:39 +02:00
|
|
|
void AXP192_displaypower(void) {
|
|
|
|
if (pmu.isBatteryConnect())
|
|
|
|
if (pmu.isChargeing())
|
2019-09-20 18:11:05 +02:00
|
|
|
ESP_LOGI(TAG, "Battery charging @ %.0fmAh", pmu.getBattChargeCurrent());
|
2019-09-18 22:38:39 +02:00
|
|
|
else
|
2019-09-20 18:11:05 +02:00
|
|
|
ESP_LOGI(TAG, "Battery not charging");
|
2019-09-18 22:38:39 +02:00
|
|
|
else
|
|
|
|
ESP_LOGI(TAG, "No Battery");
|
2019-09-19 17:06:09 +02:00
|
|
|
|
|
|
|
if (pmu.isVBUSPlug())
|
2019-09-20 18:11:05 +02:00
|
|
|
ESP_LOGI(TAG, "USB present");
|
2019-09-19 17:06:09 +02:00
|
|
|
else
|
|
|
|
ESP_LOGI(TAG, "USB not present");
|
2019-09-18 22:38:39 +02:00
|
|
|
}
|
|
|
|
|
2019-09-07 23:10:53 +02:00
|
|
|
void AXP192_init(void) {
|
2019-09-08 12:55:27 +02:00
|
|
|
|
2019-09-09 12:06:23 +02:00
|
|
|
// block i2c bus access
|
|
|
|
if (I2C_MUTEX_LOCK()) {
|
|
|
|
|
|
|
|
if (pmu.begin(Wire, AXP192_PRIMARY_ADDRESS))
|
|
|
|
ESP_LOGI(TAG, "AXP192 PMU initialization failed");
|
|
|
|
else {
|
|
|
|
|
2019-09-09 23:00:06 +02:00
|
|
|
// switch power on
|
2019-09-20 18:11:05 +02:00
|
|
|
pmu.setDCDC1Voltage(3300); // for external OLED display
|
2019-09-09 12:06:23 +02:00
|
|
|
pmu.adc1Enable(AXP202_BATT_CUR_ADC1, 1);
|
2019-09-09 23:00:06 +02:00
|
|
|
AXP192_power(true);
|
2019-09-09 12:06:23 +02:00
|
|
|
|
2019-09-20 18:11:05 +02:00
|
|
|
// set TS pin mode off to save power
|
|
|
|
pmu.setTSmode(AXP_TS_PIN_MODE_DISABLE);
|
|
|
|
|
2019-09-09 21:45:19 +02:00
|
|
|
// I2C access of AXP202X library currently is not mutexable
|
2019-09-18 22:38:39 +02:00
|
|
|
// so we better should disable AXP interrupts... ?
|
2019-09-09 21:45:19 +02:00
|
|
|
#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
|
2019-09-09 12:06:23 +02:00
|
|
|
|
2019-09-20 18:11:05 +02:00
|
|
|
ESP_LOGI(TAG, "AXP192 PMU initialized, chip temp %.1f°C", pmu.getTemp());
|
2019-09-18 22:38:39 +02:00
|
|
|
AXP192_displaypower();
|
2019-09-09 12:06:23 +02:00
|
|
|
}
|
|
|
|
I2C_MUTEX_UNLOCK(); // release i2c bus access
|
|
|
|
} else
|
|
|
|
ESP_LOGE(TAG, "I2c bus busy - PMU initialization error");
|
2019-09-07 19:52:25 +02:00
|
|
|
}
|
2019-09-07 23:10:53 +02:00
|
|
|
#endif // HAS_PMU
|
|
|
|
|
|
|
|
#ifdef BAT_MEASURE_ADC
|
|
|
|
esp_adc_cal_characteristics_t *adc_characs =
|
|
|
|
(esp_adc_cal_characteristics_t *)calloc(
|
|
|
|
1, sizeof(esp_adc_cal_characteristics_t));
|
|
|
|
|
2019-09-08 12:55:27 +02:00
|
|
|
#ifndef BAT_MEASURE_ADC_UNIT // ADC1
|
2019-09-07 23:10:53 +02:00
|
|
|
static const adc1_channel_t adc_channel = BAT_MEASURE_ADC;
|
2019-09-08 12:55:27 +02:00
|
|
|
#else // ADC2
|
|
|
|
static const adc2_channel_t adc_channel = BAT_MEASURE_ADC;
|
|
|
|
#endif
|
2019-09-07 23:10:53 +02:00
|
|
|
static const adc_atten_t atten = ADC_ATTEN_DB_11;
|
|
|
|
static const adc_unit_t unit = ADC_UNIT_1;
|
2019-09-08 12:55:27 +02:00
|
|
|
|
|
|
|
#endif // BAT_MEASURE_ADC
|
2019-09-07 23:10:53 +02:00
|
|
|
|
|
|
|
void calibrate_voltage(void) {
|
|
|
|
#ifdef BAT_MEASURE_ADC
|
2019-09-08 12:55:27 +02:00
|
|
|
// configure ADC
|
|
|
|
#ifndef BAT_MEASURE_ADC_UNIT // ADC1
|
2019-09-07 23:10:53 +02:00
|
|
|
ESP_ERROR_CHECK(adc1_config_width(ADC_WIDTH_BIT_12));
|
|
|
|
ESP_ERROR_CHECK(adc1_config_channel_atten(adc_channel, atten));
|
2019-09-08 12:55:27 +02:00
|
|
|
#else // ADC2
|
|
|
|
// ESP_ERROR_CHECK(adc2_config_width(ADC_WIDTH_BIT_12));
|
|
|
|
ESP_ERROR_CHECK(adc2_config_channel_atten(adc_channel, atten));
|
|
|
|
#endif
|
2019-09-07 23:10:53 +02:00
|
|
|
// calibrate ADC
|
|
|
|
esp_adc_cal_value_t val_type = esp_adc_cal_characterize(
|
|
|
|
unit, atten, ADC_WIDTH_BIT_12, DEFAULT_VREF, adc_characs);
|
|
|
|
// show ADC characterization base
|
|
|
|
if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) {
|
|
|
|
ESP_LOGI(TAG,
|
|
|
|
"ADC characterization based on Two Point values stored in eFuse");
|
|
|
|
} else if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) {
|
|
|
|
ESP_LOGI(TAG,
|
|
|
|
"ADC characterization based on reference voltage stored in eFuse");
|
|
|
|
} else {
|
|
|
|
ESP_LOGI(TAG, "ADC characterization based on default reference voltage");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-09-20 09:04:24 +02:00
|
|
|
bool batt_sufficient() {
|
2019-09-07 23:10:53 +02:00
|
|
|
#if (defined HAS_PMU || defined BAT_MEASURE_ADC)
|
2019-09-20 09:04:24 +02:00
|
|
|
uint16_t volts = read_voltage();
|
|
|
|
return ((volts < 1000) ||
|
|
|
|
(volts > OTA_MIN_BATT)); // no battery or battery sufficient
|
|
|
|
#else
|
|
|
|
return true;
|
2019-09-07 23:10:53 +02:00
|
|
|
#endif
|
2019-09-20 09:04:24 +02:00
|
|
|
}
|
2019-09-07 23:10:53 +02:00
|
|
|
|
|
|
|
uint16_t read_voltage() {
|
|
|
|
|
|
|
|
uint16_t voltage = 0;
|
|
|
|
|
|
|
|
#ifdef HAS_PMU
|
2019-09-09 12:06:23 +02:00
|
|
|
voltage = pmu.isVBUSPlug() ? 0xffff : pmu.getBattVoltage();
|
2019-09-07 23:10:53 +02:00
|
|
|
#else
|
|
|
|
|
|
|
|
#ifdef BAT_MEASURE_ADC
|
|
|
|
// multisample ADC
|
|
|
|
uint32_t adc_reading = 0;
|
2019-09-08 12:55:27 +02:00
|
|
|
int adc_buf = 0;
|
2019-09-07 23:10:53 +02:00
|
|
|
for (int i = 0; i < NO_OF_SAMPLES; i++) {
|
2019-09-08 12:55:27 +02:00
|
|
|
#ifndef BAT_MEASURE_ADC_UNIT // ADC1
|
2019-09-07 23:10:53 +02:00
|
|
|
adc_reading += adc1_get_raw(adc_channel);
|
2019-09-08 12:55:27 +02:00
|
|
|
#else // ADC2
|
|
|
|
ESP_ERROR_CHECK(adc2_get_raw(adc_channel, ADC_WIDTH_BIT_12, &adc_buf));
|
|
|
|
adc_reading += adc_buf;
|
|
|
|
#endif
|
2019-09-07 23:10:53 +02:00
|
|
|
}
|
|
|
|
adc_reading /= NO_OF_SAMPLES;
|
|
|
|
// Convert ADC reading to voltage in mV
|
|
|
|
voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_characs);
|
2019-09-08 12:55:27 +02:00
|
|
|
#endif // BAT_MEASURE_ADC
|
2019-09-07 23:10:53 +02:00
|
|
|
|
|
|
|
#ifdef BAT_VOLTAGE_DIVIDER
|
|
|
|
voltage *= BAT_VOLTAGE_DIVIDER;
|
2019-09-08 12:55:27 +02:00
|
|
|
#endif // BAT_VOLTAGE_DIVIDER
|
2019-09-07 23:10:53 +02:00
|
|
|
|
|
|
|
#endif // HAS_PMU
|
|
|
|
|
|
|
|
return voltage;
|
|
|
|
}
|