ESP32-PaxCounter/src/power.cpp

263 lines
7.9 KiB
C++
Raw Normal View History

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
#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-10-16 21:14:34 +02:00
#ifndef BAT_MEASURE_ADC_UNIT // ADC1
static const adc1_channel_t adc_channel = BAT_MEASURE_ADC;
#else // ADC2
static const adc2_channel_t adc_channel = BAT_MEASURE_ADC;
#endif
static const adc_atten_t atten = ADC_ATTEN_DB_11;
static const adc_unit_t unit = ADC_UNIT_1;
#endif // BAT_MEASURE_ADC
2019-09-07 19:52:25 +02:00
#ifdef HAS_PMU
2019-09-09 12:06:23 +02:00
AXP20X_Class pmu;
2019-09-07 19:52:25 +02:00
2019-10-16 21:14:34 +02:00
void AXP192_powerevent_IRQ(void) {
2019-09-30 12:35:03 +02:00
pmu.readIRQ();
if (pmu.isVbusOverVoltageIRQ())
ESP_LOGI(TAG, "USB voltage %.2fV too high.", pmu.getVbusVoltage() / 1000);
if (pmu.isVbusPlugInIRQ())
ESP_LOGI(TAG, "USB plugged, %.2fV @ %.0mA", pmu.getVbusVoltage() / 1000,
pmu.getVbusCurrent());
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 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.");
// short press -> esp32 deep sleep mode, can be exited by pressing user button
2019-10-16 21:14:34 +02:00
#ifdef HAS_BUTTON
if (pmu.isPEKShortPressIRQ() && (RTC_runmode == RUNMODE_NORMAL)) {
enter_deepsleep(0, HAS_BUTTON);
}
#endif
// long press -> shutdown power, can be exited by another longpress
2019-09-30 12:35:03 +02:00
if (pmu.isPEKLongtPressIRQ()) {
AXP192_power(pmu_power_off); // switch off Lora, GPS, display
pmu.shutdown(); // switch off device
2019-09-30 12:35:03 +02:00
}
2019-09-30 12:35:03 +02:00
pmu.clearIRQ();
// refresh stored voltage value
2020-04-11 21:30:09 +02:00
read_battlevel();
}
void AXP192_power(pmu_power_t powerlevel) {
switch (powerlevel) {
case pmu_power_off:
pmu.setChgLEDMode(AXP20X_LED_OFF);
pmu.setPowerOutPut(AXP192_DCDC1, AXP202_OFF);
pmu.setPowerOutPut(AXP192_LDO3, AXP202_OFF);
pmu.setPowerOutPut(AXP192_LDO2, AXP202_OFF);
2019-10-20 20:47:03 +02:00
// pmu.setPowerOutPut(AXP192_DCDC3, AXP202_OFF);
break;
case pmu_power_sleep:
pmu.setChgLEDMode(AXP20X_LED_BLINK_1HZ);
// we don't cut off DCDC1, because then display blocks i2c bus
pmu.setPowerOutPut(AXP192_LDO3, AXP202_OFF); // gps off
pmu.setPowerOutPut(AXP192_LDO2, AXP202_OFF); // lora off
break;
default: // all rails power on
pmu.setPowerOutPut(AXP192_LDO2, AXP202_ON); // Lora on T-Beam V1.0
pmu.setPowerOutPut(AXP192_LDO3, AXP202_ON); // Gps on T-Beam V1.0
pmu.setPowerOutPut(AXP192_DCDC1, AXP202_ON); // OLED on T-Beam v1.0
pmu.setChgLEDMode(AXP20X_LED_LOW_LEVEL);
break;
}
}
void AXP192_showstatus(void) {
2019-09-30 12:35:03 +02:00
if (pmu.isBatteryConnect())
if (pmu.isChargeing())
ESP_LOGI(TAG, "Battery charging, %.2fV @ %.0fmAh",
pmu.getBattVoltage() / 1000, pmu.getBattChargeCurrent());
else
2019-09-30 12:35:03 +02:00
ESP_LOGI(TAG, "Battery not charging");
else
ESP_LOGI(TAG, "No Battery");
if (pmu.isVBUSPlug())
ESP_LOGI(TAG, "USB powered, %.0fmW",
pmu.getVbusVoltage() / 1000 * pmu.getVbusCurrent());
else
ESP_LOGI(TAG, "USB not present");
}
2019-09-07 23:10:53 +02:00
void AXP192_init(void) {
2019-09-08 12:55:27 +02:00
2019-10-12 16:00:22 +02:00
if (pmu.begin(i2c_readBytes, i2c_writeBytes, AXP192_PRIMARY_ADDRESS) ==
AXP_FAIL)
2019-09-30 12:35:03 +02:00
ESP_LOGI(TAG, "AXP192 PMU initialization failed");
else {
2019-09-09 12:06:23 +02:00
2019-09-30 12:35:03 +02:00
// configure AXP192
pmu.setDCDC1Voltage(3300); // for external OLED display
pmu.setTimeOutShutdown(false); // no automatic shutdown
pmu.setTSmode(AXP_TS_PIN_MODE_DISABLE); // TS pin mode off to save power
2019-09-09 12:06:23 +02:00
2019-09-30 12:35:03 +02:00
// switch ADCs on
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);
2019-09-21 08:23:39 +02:00
2019-09-30 12:35:03 +02:00
// switch power rails on
AXP192_power(pmu_power_on);
2019-09-20 18:11:05 +02:00
#ifdef PMU_INT
2019-09-30 12:35:03 +02:00
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 |
AXP202_CHARGING_FINISHED_IRQ,
1);
pmu.clearIRQ();
#endif // PMU_INT
2019-09-09 12:06:23 +02:00
2019-09-30 12:35:03 +02:00
ESP_LOGI(TAG, "AXP192 PMU initialized");
}
}
2019-09-07 23:10:53 +02:00
#endif // HAS_PMU
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));
2019-09-08 12:55:27 +02:00
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
}
2020-04-11 21:30:09 +02:00
uint16_t read_voltage(void) {
2019-09-07 23:10:53 +02:00
uint16_t voltage = 0;
#ifdef HAS_PMU
2020-04-11 21:30:09 +02:00
voltage = 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
#ifndef BAT_MEASURE_ADC_UNIT // ADC1
2019-09-25 15:00:52 +02:00
for (int i = 0; i < NO_OF_SAMPLES; i++) {
2019-09-07 23:10:53 +02:00
adc_reading += adc1_get_raw(adc_channel);
2019-09-25 15:00:52 +02:00
}
2019-09-08 12:55:27 +02:00
#else // ADC2
2019-09-25 15:00:52 +02:00
int adc_buf = 0;
for (int i = 0; i < NO_OF_SAMPLES; i++) {
2019-09-08 12:55:27 +02:00
ESP_ERROR_CHECK(adc2_get_raw(adc_channel, ADC_WIDTH_BIT_12, &adc_buf));
adc_reading += adc_buf;
2019-09-07 23:10:53 +02:00
}
2019-10-12 16:00:22 +02:00
#endif // BAT_MEASURE_ADC_UNIT
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-10-12 16:00:22 +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
2020-04-11 21:30:09 +02:00
return voltage;
}
2020-04-10 23:26:29 +02:00
2020-04-11 21:30:09 +02:00
uint8_t read_battlevel() {
2020-04-10 23:26:29 +02:00
2020-04-11 21:30:09 +02:00
// return the battery value as sent in MAC Command
// DevStatusAns. Available defines in lorabase.h:
// MCMD_DEVS_EXT_POWER = 0x00, // external power supply
// MCMD_DEVS_BATT_MIN = 0x01, // min battery value
// MCMD_DEVS_BATT_MAX = 0xFE, // max battery value
// MCMD_DEVS_BATT_NOINFO = 0xFF, // unknown battery level
// we calculate the applicable value from MCMD_DEVS_BATT_MIN to
// MCMD_DEVS_BATT_MAX from bat_percent value
const uint16_t batt_voltage_range = BAT_MAX_VOLTAGE - BAT_MIN_VOLTAGE;
const uint8_t batt_level_range = MCMD_DEVS_BATT_MAX - MCMD_DEVS_BATT_MIN + 1;
const uint16_t batt_voltage = read_voltage() - BAT_MIN_VOLTAGE;
const uint8_t batt_percent =
batt_voltage > 0 ? batt_voltage / batt_voltage_range * 100 : 0;
uint8_t lmic_batt_level;
#ifdef HAS_PMU
if (batt_percent > 0)
lmic_batt_level = pmu.isVBUSPlug() ? MCMD_DEVS_EXT_POWER
: batt_percent / 100 * batt_level_range;
else
lmic_batt_level = MCMD_DEVS_BATT_NOINFO;
#else
if (batt_percent > 0)
lmic_batt_level = batt_percent / 100 * batt_level_range;
else
lmic_batt_level = MCMD_DEVS_BATT_NOINFO;
#endif // HAS_PMU
// set battery level value for lmic stack
#if (HAS_LORA)
//LMIC_setBattLevel(lmic_batt_level);
#endif
return batt_percent;
}
bool batt_sufficient() {
#if (defined HAS_PMU || defined BAT_MEASURE_ADC)
uint8_t my_batt_level = read_battlevel();
if (my_batt_level == MCMD_DEVS_EXT_POWER)
return true;
else
return (my_batt_level > OTA_MIN_BATT);
#else
return true; // we don't know batt level
#endif
}