2019-09-07 19:52:25 +02:00
|
|
|
// Basic config
|
|
|
|
#include "globals.h"
|
|
|
|
#include "power.h"
|
|
|
|
|
|
|
|
|
2022-09-18 17:21:22 +02:00
|
|
|
int8_t batt_level = -1; // percent batt level, global variable, -1 means no batt
|
2021-03-31 21:43:51 +02:00
|
|
|
|
2019-10-20 20:05:13 +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
|
|
|
|
2019-10-20 20:05:13 +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;
|
2021-02-13 15:58:56 +01:00
|
|
|
RTC_NOINIT_ATTR uint64_t RTC_reg_b;
|
2019-10-20 20:05:13 +02:00
|
|
|
#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
|
|
|
|
2019-10-20 20:05:13 +02:00
|
|
|
#ifdef HAS_PMU
|
2022-08-17 19:05:41 +02:00
|
|
|
XPowersPMU pmu;
|
2019-09-07 19:52:25 +02:00
|
|
|
|
2022-11-06 15:00:54 +01:00
|
|
|
void IRAM_ATTR PMUIRQ() { doIRQ(PMU_IRQ); }
|
|
|
|
|
2019-10-16 21:14:34 +02:00
|
|
|
void AXP192_powerevent_IRQ(void) {
|
2022-08-17 19:05:41 +02:00
|
|
|
pmu.getIrqStatus();
|
2019-09-30 12:35:03 +02:00
|
|
|
|
2022-08-17 19:05:41 +02:00
|
|
|
if (pmu.isVbusOverVoltageIrq())
|
2019-09-30 12:35:03 +02:00
|
|
|
ESP_LOGI(TAG, "USB voltage %.2fV too high.", pmu.getVbusVoltage() / 1000);
|
2022-08-17 19:05:41 +02:00
|
|
|
if (pmu.isVbusInsertIrq())
|
2019-09-30 12:35:03 +02:00
|
|
|
ESP_LOGI(TAG, "USB plugged, %.2fV @ %.0mA", pmu.getVbusVoltage() / 1000,
|
|
|
|
pmu.getVbusCurrent());
|
2022-08-17 19:05:41 +02:00
|
|
|
if (pmu.isVbusRemoveIrq())
|
2019-09-30 12:35:03 +02:00
|
|
|
ESP_LOGI(TAG, "USB unplugged.");
|
2022-08-17 19:05:41 +02:00
|
|
|
if (pmu.isBatInsertIrq())
|
2019-09-30 12:35:03 +02:00
|
|
|
ESP_LOGI(TAG, "Battery is connected.");
|
2022-08-17 19:05:41 +02:00
|
|
|
if (pmu.isBatRemoveIrq())
|
2019-09-30 12:35:03 +02:00
|
|
|
ESP_LOGI(TAG, "Battery was removed.");
|
2022-08-17 19:05:41 +02:00
|
|
|
if (pmu.isBatChagerStartIrq())
|
|
|
|
ESP_LOGI(TAG, "Battery charging started.");
|
|
|
|
if (pmu.isBatChagerDoneIrq())
|
2019-09-30 12:35:03 +02:00
|
|
|
ESP_LOGI(TAG, "Battery charging done.");
|
2022-08-17 19:05:41 +02:00
|
|
|
if (pmu.isBattTempLowIrq())
|
2019-09-30 12:35:03 +02:00
|
|
|
ESP_LOGI(TAG, "Battery high temperature.");
|
2022-08-17 19:05:41 +02:00
|
|
|
if (pmu.isBattTempHighIrq())
|
2019-09-30 12:35:03 +02:00
|
|
|
ESP_LOGI(TAG, "Battery low temperature.");
|
2022-08-18 12:11:47 +02:00
|
|
|
|
|
|
|
// PEK button handling:
|
|
|
|
// long press -> shutdown power, must be exited by another longpress
|
|
|
|
if (pmu.isPekeyLongPressIrq())
|
|
|
|
AXP192_power(pmu_power_off); // switch off Lora, GPS, display
|
2022-07-17 15:47:31 +02:00
|
|
|
#ifdef HAS_BUTTON
|
2022-08-18 12:11:47 +02:00
|
|
|
// short press -> esp32 deep sleep mode, must be exited by user button
|
|
|
|
if (pmu.isPekeyShortPressIrq())
|
2022-11-13 22:27:06 +01:00
|
|
|
enter_deepsleep(0UL, HAS_BUTTON);
|
2022-07-17 15:47:31 +02:00
|
|
|
#endif
|
2019-09-23 15:45:47 +02:00
|
|
|
|
2022-08-17 19:05:41 +02:00
|
|
|
pmu.clearIrqStatus();
|
2019-09-23 15:45:47 +02:00
|
|
|
|
|
|
|
// refresh stored voltage value
|
2020-04-11 21:30:09 +02:00
|
|
|
read_battlevel();
|
2019-09-09 21:45:19 +02:00
|
|
|
}
|
|
|
|
|
2019-10-20 20:05:13 +02:00
|
|
|
void AXP192_power(pmu_power_t powerlevel) {
|
|
|
|
switch (powerlevel) {
|
|
|
|
case pmu_power_off:
|
2022-09-17 18:34:30 +02:00
|
|
|
pmu.setChargingLedMode(XPOWERS_CHG_LED_OFF);
|
2020-12-16 18:32:59 +01:00
|
|
|
pmu.shutdown();
|
2019-10-20 20:05:13 +02:00
|
|
|
break;
|
|
|
|
case pmu_power_sleep:
|
2022-09-17 18:34:30 +02:00
|
|
|
pmu.setChargingLedMode(XPOWERS_CHG_LED_CTRL_CHG);
|
2020-12-24 18:20:53 +01:00
|
|
|
// we don't cut off DCDC1, because OLED display will then block i2c bus
|
2022-08-17 19:05:41 +02:00
|
|
|
// pmu.disableDC1(); // OLED off
|
|
|
|
pmu.disableLDO3(); // gps off
|
|
|
|
pmu.disableLDO2(); // lora off
|
|
|
|
pmu.enableSleep();
|
2019-10-20 20:05:13 +02:00
|
|
|
break;
|
2020-12-16 18:32:59 +01:00
|
|
|
case pmu_power_on:
|
2020-12-24 18:20:53 +01:00
|
|
|
default:
|
2022-08-17 19:05:41 +02:00
|
|
|
pmu.enableLDO2(); // Lora on T-Beam V1.0/1.1
|
|
|
|
pmu.enableLDO3(); // Gps on T-Beam V1.0/1.1
|
|
|
|
pmu.enableDC1(); // OLED on T-Beam v1.0/1.1
|
2022-09-17 18:34:30 +02:00
|
|
|
pmu.setChargingLedMode(XPOWERS_CHG_LED_ON);
|
2019-10-20 20:05:13 +02:00
|
|
|
break;
|
2019-09-09 23:00:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 15:45:47 +02:00
|
|
|
void AXP192_showstatus(void) {
|
2019-09-30 12:35:03 +02:00
|
|
|
if (pmu.isBatteryConnect())
|
2022-08-17 19:05:41 +02:00
|
|
|
if (pmu.isCharging())
|
2019-09-30 12:35:03 +02:00
|
|
|
ESP_LOGI(TAG, "Battery charging, %.2fV @ %.0fmAh",
|
2022-09-18 17:21:22 +02:00
|
|
|
pmu.getBattVoltage() / 1000.0, pmu.getBatteryChargeCurrent());
|
2019-09-23 15:45:47 +02:00
|
|
|
else
|
2019-09-30 12:35:03 +02:00
|
|
|
ESP_LOGI(TAG, "Battery not charging");
|
|
|
|
else
|
2022-09-18 17:21:22 +02:00
|
|
|
ESP_LOGI(TAG, "Battery not present");
|
2019-09-30 12:35:03 +02:00
|
|
|
|
2022-08-17 19:05:41 +02:00
|
|
|
if (pmu.isVbusIn())
|
2019-09-30 12:35:03 +02:00
|
|
|
ESP_LOGI(TAG, "USB powered, %.0fmW",
|
|
|
|
pmu.getVbusVoltage() / 1000 * pmu.getVbusCurrent());
|
|
|
|
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) {
|
2022-08-17 19:05:41 +02:00
|
|
|
if (!pmu.begin(Wire, AXP192_PRIMARY_ADDRESS, SCL, SDA))
|
2019-09-30 12:35:03 +02:00
|
|
|
ESP_LOGI(TAG, "AXP192 PMU initialization failed");
|
|
|
|
else {
|
2022-08-17 19:05:41 +02:00
|
|
|
ESP_LOGD(TAG, "AXP192 ChipID:0x%x", pmu.getChipID());
|
|
|
|
|
|
|
|
// set pmu operating voltages
|
2022-09-17 18:34:30 +02:00
|
|
|
pmu.setSysPowerDownVoltage(2700);
|
|
|
|
pmu.setVbusVoltageLimit(XPOWERS_AXP192_VBUS_VOL_LIM_4V5);
|
|
|
|
pmu.setVbusCurrentLimit(XPOWERS_AXP192_VBUS_CUR_LIM_OFF);
|
2020-12-24 16:52:28 +01:00
|
|
|
|
2022-08-17 19:05:41 +02:00
|
|
|
// set device operating voltages
|
|
|
|
pmu.setDC1Voltage(3300); // for external OLED display
|
|
|
|
pmu.setLDO2Voltage(3300); // LORA VDD 3v3
|
|
|
|
pmu.setLDO3Voltage(3300); // GPS VDD 3v3
|
2021-01-30 17:54:34 +01:00
|
|
|
|
2020-12-24 16:52:28 +01:00
|
|
|
// configure PEK button settings
|
2022-09-17 18:34:30 +02:00
|
|
|
pmu.setPowerKeyPressOffTime(XPOWERS_POWEROFF_4S);
|
|
|
|
pmu.setPowerKeyPressOnTime(XPOWERS_POWERON_128MS);
|
2020-12-24 16:52:28 +01:00
|
|
|
|
|
|
|
// set battery temperature sensing pin off to save power
|
2022-08-17 19:05:41 +02:00
|
|
|
pmu.disableTSPinMeasure();
|
2019-09-09 12:06:23 +02:00
|
|
|
|
2022-08-17 19:05:41 +02:00
|
|
|
// Enable internal ADC detection
|
|
|
|
pmu.enableBattDetection();
|
|
|
|
pmu.enableVbusVoltageMeasure();
|
|
|
|
pmu.enableBattVoltageMeasure();
|
|
|
|
pmu.enableSystemVoltageMeasure();
|
2019-09-21 08:23:39 +02:00
|
|
|
|
2019-09-09 21:45:19 +02:00
|
|
|
#ifdef PMU_INT
|
2019-09-30 12:35:03 +02:00
|
|
|
pinMode(PMU_INT, INPUT_PULLUP);
|
|
|
|
attachInterrupt(digitalPinToInterrupt(PMU_INT), PMUIRQ, FALLING);
|
2022-08-17 19:05:41 +02:00
|
|
|
// disable all interrupts
|
2022-09-17 18:34:30 +02:00
|
|
|
pmu.disableIRQ(XPOWERS_AXP192_ALL_IRQ);
|
2022-08-17 19:05:41 +02:00
|
|
|
// clear all interrupt flags
|
|
|
|
pmu.clearIrqStatus();
|
|
|
|
// enable the required interrupt function
|
2022-09-18 17:21:22 +02:00
|
|
|
pmu.enableIRQ(XPOWERS_AXP192_BAT_INSERT_IRQ |
|
|
|
|
XPOWERS_AXP192_BAT_REMOVE_IRQ | // BATTERY
|
|
|
|
XPOWERS_AXP192_VBUS_INSERT_IRQ |
|
|
|
|
XPOWERS_AXP192_VBUS_REMOVE_IRQ | // VBUS
|
|
|
|
XPOWERS_AXP192_PKEY_SHORT_IRQ |
|
|
|
|
XPOWERS_AXP192_PKEY_LONG_IRQ | // POWER KEY
|
|
|
|
XPOWERS_AXP192_BAT_CHG_DONE_IRQ |
|
|
|
|
XPOWERS_AXP192_BAT_CHG_START_IRQ // CHARGE
|
2022-08-17 19:05:41 +02:00
|
|
|
);
|
2019-09-09 21:45:19 +02:00
|
|
|
#endif // PMU_INT
|
2019-09-09 12:06:23 +02:00
|
|
|
|
2022-08-18 12:11:47 +02:00
|
|
|
// set charging parameters according to user settings if we have (see power.h)
|
2021-01-28 23:17:39 +01:00
|
|
|
#ifdef PMU_CHG_CURRENT
|
2022-09-17 18:34:30 +02:00
|
|
|
pmu.setChargerConstantCurr(PMU_CHG_CURRENT);
|
|
|
|
pmu.setChargeTargetVoltage(PMU_CHG_CUTOFF);
|
2022-08-17 19:05:41 +02:00
|
|
|
pmu.enableCharge();
|
2020-05-09 13:42:08 +02:00
|
|
|
#endif
|
|
|
|
|
2020-12-16 18:54:23 +01:00
|
|
|
// switch power rails on
|
|
|
|
AXP192_power(pmu_power_on);
|
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
|
2020-12-21 19:41:25 +01:00
|
|
|
adc1_config_width(ADC_WIDTH_BIT_12);
|
|
|
|
adc1_config_channel_atten(adc_channel, atten);
|
2019-09-08 12:55:27 +02:00
|
|
|
#else // ADC2
|
2020-12-21 19:41:25 +01:00
|
|
|
adc2_config_channel_atten(adc_channel, atten);
|
2021-02-13 15:58:56 +01:00
|
|
|
// ADC2 wifi bug workaround, see
|
|
|
|
// https://github.com/espressif/arduino-esp32/issues/102
|
|
|
|
RTC_reg_b = READ_PERI_REG(SENS_SAR_READ_CTRL2_REG);
|
2019-09-08 12:55:27 +02:00
|
|
|
#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++) {
|
2021-02-13 15:58:56 +01:00
|
|
|
// ADC2 wifi bug workaround, see
|
|
|
|
// https://github.com/espressif/arduino-esp32/issues/102
|
|
|
|
WRITE_PERI_REG(SENS_SAR_READ_CTRL2_REG, RTC_reg_b);
|
|
|
|
SET_PERI_REG_MASK(SENS_SAR_READ_CTRL2_REG, SENS_SAR2_DATA_INV);
|
2020-12-21 19:41:25 +01:00
|
|
|
adc2_get_raw(adc_channel, ADC_WIDTH_BIT_12, &adc_buf);
|
2019-09-08 12:55:27 +02:00
|
|
|
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
|
|
|
|
2022-09-18 17:21:22 +02:00
|
|
|
int8_t read_battlevel(mapFn_t mapFunction) {
|
2020-04-14 00:15:51 +02:00
|
|
|
// returns the estimated battery level in values 0 ... 100 [percent]
|
2021-10-11 09:59:41 +02:00
|
|
|
uint8_t batt_percent = 0;
|
2020-04-19 19:51:23 +02:00
|
|
|
#ifdef HAS_IP5306
|
2021-10-11 09:59:41 +02:00
|
|
|
batt_percent = IP5306_GetBatteryLevel();
|
2022-08-17 19:05:41 +02:00
|
|
|
#elif defined HAS_PMU
|
2022-09-18 17:21:22 +02:00
|
|
|
batt_percent = pmu.getBatteryPercent();
|
2020-04-19 19:51:23 +02:00
|
|
|
#else
|
2020-04-14 10:57:30 +02:00
|
|
|
const uint16_t batt_voltage = read_voltage();
|
2020-04-13 22:07:26 +02:00
|
|
|
if (batt_voltage <= BAT_MIN_VOLTAGE)
|
2021-10-11 09:59:41 +02:00
|
|
|
batt_percent = 0;
|
2020-04-13 22:07:26 +02:00
|
|
|
else if (batt_voltage >= BAT_MAX_VOLTAGE)
|
2021-10-11 09:59:41 +02:00
|
|
|
batt_percent = 100;
|
2020-04-13 22:07:26 +02:00
|
|
|
else
|
2021-10-11 09:59:41 +02:00
|
|
|
batt_percent =
|
|
|
|
(*mapFunction)(batt_voltage, BAT_MIN_VOLTAGE, BAT_MAX_VOLTAGE);
|
2020-05-02 17:24:52 +02:00
|
|
|
#endif
|
2021-10-11 09:59:41 +02:00
|
|
|
|
|
|
|
#if (HAS_LORA)
|
|
|
|
// set the battery status value to send by LMIC 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 batt_percent value
|
|
|
|
|
2022-09-18 17:21:22 +02:00
|
|
|
if (batt_percent == -1)
|
2021-10-11 09:59:41 +02:00
|
|
|
LMIC_setBatteryLevel(MCMD_DEVS_BATT_NOINFO);
|
|
|
|
else
|
|
|
|
LMIC_setBatteryLevel(batt_percent / 100.0 *
|
|
|
|
(MCMD_DEVS_BATT_MAX - MCMD_DEVS_BATT_MIN + 1));
|
|
|
|
|
|
|
|
// overwrite calculated value if we have external power
|
|
|
|
#ifdef HAS_PMU
|
2022-08-17 19:05:41 +02:00
|
|
|
if (pmu.isVbusIn())
|
2021-10-11 09:59:41 +02:00
|
|
|
LMIC_setBatteryLevel(MCMD_DEVS_EXT_POWER);
|
|
|
|
#elif defined HAS_IP5306
|
|
|
|
if (IP5306_GetPowerSource())
|
|
|
|
LMIC_setBatteryLevel(MCMD_DEVS_EXT_POWER);
|
|
|
|
#endif // HAS_PMU
|
|
|
|
|
|
|
|
#endif // HAS_LORA
|
|
|
|
|
|
|
|
return batt_percent;
|
2020-04-11 21:30:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool batt_sufficient() {
|
2020-04-19 19:51:23 +02:00
|
|
|
#if (defined HAS_PMU || defined BAT_MEASURE_ADC || defined HAS_IP5306)
|
2022-09-18 17:21:22 +02:00
|
|
|
if (batt_level > 0) // we have a battery percent value
|
2020-12-26 18:15:56 +01:00
|
|
|
return (batt_level > OTA_MIN_BATT);
|
|
|
|
else
|
2020-04-11 21:30:09 +02:00
|
|
|
#endif
|
2020-12-26 18:15:56 +01:00
|
|
|
return true; // we don't know batt level
|
2020-04-11 21:30:09 +02:00
|
|
|
}
|
2020-04-19 19:51:23 +02:00
|
|
|
|
|
|
|
#ifdef HAS_IP5306
|
|
|
|
|
|
|
|
// IP5306 code snippet was taken from
|
|
|
|
// https://gist.github.com/me-no-dev/7702f08dd578de5efa47caf322250b57
|
|
|
|
|
|
|
|
#define IP5306_REG_SYS_0 0x00
|
|
|
|
#define IP5306_REG_SYS_1 0x01
|
|
|
|
#define IP5306_REG_SYS_2 0x02
|
|
|
|
#define IP5306_REG_CHG_0 0x20
|
|
|
|
#define IP5306_REG_CHG_1 0x21
|
|
|
|
#define IP5306_REG_CHG_2 0x22
|
|
|
|
#define IP5306_REG_CHG_3 0x23
|
|
|
|
#define IP5306_REG_CHG_4 0x24
|
|
|
|
#define IP5306_REG_READ_0 0x70
|
|
|
|
#define IP5306_REG_READ_1 0x71
|
|
|
|
#define IP5306_REG_READ_2 0x72
|
|
|
|
#define IP5306_REG_READ_3 0x77
|
|
|
|
#define IP5306_REG_READ_4 0x78
|
|
|
|
|
|
|
|
#define IP5306_LEDS2PCT(byte) \
|
|
|
|
((byte & 0x01 ? 25 : 0) + (byte & 0x02 ? 25 : 0) + (byte & 0x04 ? 25 : 0) + \
|
|
|
|
(byte & 0x08 ? 25 : 0))
|
|
|
|
|
|
|
|
uint8_t ip5306_get_bits(uint8_t reg, uint8_t index, uint8_t bits) {
|
|
|
|
uint8_t value;
|
2020-05-01 12:24:52 +02:00
|
|
|
if (i2c_readBytes(IP5306_PRIMARY_ADDRESS, reg, &value, 1) == 0xff) {
|
2020-04-19 19:51:23 +02:00
|
|
|
ESP_LOGW(TAG, "IP5306 get bits fail: 0x%02x", reg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return (value >> index) & ((1 << bits) - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ip5306_set_bits(uint8_t reg, uint8_t index, uint8_t bits, uint8_t value) {
|
|
|
|
uint8_t mask = (1 << bits) - 1, v;
|
2020-05-01 12:24:52 +02:00
|
|
|
if (i2c_readBytes(IP5306_PRIMARY_ADDRESS, reg, &v, 1) == 0xff) {
|
2020-04-19 19:51:23 +02:00
|
|
|
ESP_LOGW(TAG, "IP5306 register read fail: 0x%02x", reg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
v &= ~(mask << index);
|
|
|
|
v |= ((value & mask) << index);
|
|
|
|
|
2020-05-01 12:24:52 +02:00
|
|
|
if (i2c_writeBytes(IP5306_PRIMARY_ADDRESS, reg, &v, 1) == 0xff)
|
2020-04-19 19:51:23 +02:00
|
|
|
ESP_LOGW(TAG, "IP5306 register write fail: 0x%02x", reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t IP5306_GetPowerSource(void) {
|
2020-05-01 12:24:52 +02:00
|
|
|
return ip5306_get_bits(IP5306_REG_READ_0, 3, 1); // 0:BAT, 1:VIN
|
2020-04-19 19:51:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t IP5306_GetBatteryFull(void) {
|
2020-05-01 12:24:52 +02:00
|
|
|
return ip5306_get_bits(IP5306_REG_READ_1, 3, 1); // 0:CHG/DIS, 1:FULL
|
2020-04-19 19:51:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t IP5306_GetBatteryLevel(void) {
|
|
|
|
uint8_t state = (~ip5306_get_bits(IP5306_REG_READ_4, 4, 4)) & 0x0F;
|
|
|
|
// LED[0-4] State (inverted)
|
|
|
|
return IP5306_LEDS2PCT(state);
|
|
|
|
}
|
|
|
|
|
2020-05-09 22:48:29 +02:00
|
|
|
void IP5306_SetChargerEnabled(uint8_t v) {
|
|
|
|
ip5306_set_bits(IP5306_REG_SYS_0, 4, 1, v); // 0:dis,*1:en
|
|
|
|
}
|
|
|
|
|
|
|
|
void IP5306_SetChargeCutoffVoltage(uint8_t v) {
|
|
|
|
ip5306_set_bits(IP5306_REG_CHG_2, 2, 2,
|
|
|
|
v); //*0:4.2V, 1:4.3V, 2:4.35V, 3:4.4V
|
|
|
|
}
|
|
|
|
|
|
|
|
void IP5306_SetEndChargeCurrentDetection(uint8_t v) {
|
|
|
|
ip5306_set_bits(IP5306_REG_CHG_1, 6, 2,
|
|
|
|
v); // 0:200mA, 1:400mA, *2:500mA, 3:600mA
|
|
|
|
}
|
|
|
|
|
2020-04-19 19:51:23 +02:00
|
|
|
void printIP5306Stats(void) {
|
|
|
|
bool usb = IP5306_GetPowerSource();
|
|
|
|
bool full = IP5306_GetBatteryFull();
|
2020-04-20 17:05:12 +02:00
|
|
|
uint8_t level = IP5306_GetBatteryLevel();
|
2020-04-19 19:51:23 +02:00
|
|
|
ESP_LOGI(TAG,
|
2020-04-20 17:05:12 +02:00
|
|
|
"IP5306: Power Source: %s, Battery State: %s, Battery Level: %u%%",
|
2020-04-19 19:51:23 +02:00
|
|
|
usb ? "USB" : "BATTERY",
|
2020-04-20 17:05:12 +02:00
|
|
|
full ? "CHARGED" : (usb ? "CHARGING" : "DISCHARGING"), level);
|
2020-04-19 19:51:23 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 22:48:29 +02:00
|
|
|
void IP5306_init(void) {
|
|
|
|
IP5306_SetChargerEnabled(1);
|
|
|
|
IP5306_SetChargeCutoffVoltage(PMU_CHG_CUTOFF);
|
|
|
|
IP5306_SetEndChargeCurrentDetection(PMU_CHG_CURRENT);
|
|
|
|
}
|
|
|
|
|
2020-04-19 19:51:23 +02:00
|
|
|
#endif // HAS_IP5306
|