2019-09-07 19:52:25 +02:00
|
|
|
#ifndef _POWER_H
|
|
|
|
#define _POWER_H
|
|
|
|
|
|
|
|
#include <Arduino.h>
|
2019-09-07 23:10:53 +02:00
|
|
|
#include <esp_adc_cal.h>
|
2021-12-24 16:52:33 +01:00
|
|
|
#include <soc/adc_channel.h>
|
2020-03-29 18:08:52 +02:00
|
|
|
|
2019-10-16 21:14:34 +02:00
|
|
|
#include "i2c.h"
|
2019-10-20 20:47:03 +02:00
|
|
|
#include "reset.h"
|
2019-09-07 19:52:25 +02:00
|
|
|
|
2019-09-07 23:10:53 +02:00
|
|
|
#define DEFAULT_VREF 1100 // tbd: use adc2_vref_to_gpio() for better estimate
|
|
|
|
#define NO_OF_SAMPLES 64 // we do some multisampling to get better values
|
|
|
|
|
2020-04-10 23:26:29 +02:00
|
|
|
#ifndef BAT_MAX_VOLTAGE
|
2020-04-12 22:12:13 +02:00
|
|
|
#define BAT_MAX_VOLTAGE 4200 // millivolts
|
2020-04-11 21:30:09 +02:00
|
|
|
#endif
|
|
|
|
#ifndef BAT_MIN_VOLTAGE
|
2020-04-13 22:07:26 +02:00
|
|
|
#define BAT_MIN_VOLTAGE 3100 // millivolts
|
2020-04-10 23:26:29 +02:00
|
|
|
#endif
|
|
|
|
|
2020-05-09 22:48:29 +02:00
|
|
|
#ifndef PMU_CHG_CUTOFF
|
|
|
|
#ifdef HAS_PMU
|
|
|
|
#define PMU_CHG_CUTOFF AXP202_TARGET_VOL_4_2V
|
|
|
|
#elif defined HAS_IP5306
|
|
|
|
#define PMU_CHG_CUTOFF 0
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef PMU_CHG_CURRENT
|
|
|
|
#ifdef HAS_PMU
|
|
|
|
#define PMU_CHG_CURRENT AXP1XX_CHARGE_CUR_450MA
|
|
|
|
#elif defined HAS_IP5306
|
|
|
|
#define PMU_CHG_CURRENT 2
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2021-02-13 14:04:03 +01:00
|
|
|
#ifdef EXT_POWER_SW
|
|
|
|
#ifndef EXT_POWER_ON
|
|
|
|
#define EXT_POWER_ON 1
|
|
|
|
#endif
|
|
|
|
#ifndef EXT_POWER_OFF
|
|
|
|
#define EXT_POWER_OFF (!EXT_POWER_ON)
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2021-02-13 15:58:56 +01:00
|
|
|
#ifdef BAT_MEASURE_ADC_UNIT // ADC2 wifi bug workaround
|
|
|
|
extern RTC_NOINIT_ATTR uint64_t RTC_reg_b;
|
|
|
|
#include "soc/sens_reg.h" // needed for adc pin reset
|
|
|
|
#endif
|
|
|
|
|
2020-04-13 22:07:26 +02:00
|
|
|
typedef uint8_t (*mapFn_t)(uint16_t, uint16_t, uint16_t);
|
|
|
|
|
2019-09-23 15:45:47 +02:00
|
|
|
uint16_t read_voltage(void);
|
|
|
|
void calibrate_voltage(void);
|
|
|
|
bool batt_sufficient(void);
|
|
|
|
|
2019-09-07 19:52:25 +02:00
|
|
|
#ifdef HAS_PMU
|
|
|
|
#include <axp20x.h>
|
2020-04-14 00:15:51 +02:00
|
|
|
extern AXP20X_Class pmu;
|
2019-10-20 20:05:13 +02:00
|
|
|
enum pmu_power_t { pmu_power_on, pmu_power_off, pmu_power_sleep };
|
2019-10-16 21:14:34 +02:00
|
|
|
void AXP192_powerevent_IRQ(void);
|
2019-10-20 20:05:13 +02:00
|
|
|
void AXP192_power(pmu_power_t powerlevel);
|
2019-09-07 19:52:25 +02:00
|
|
|
void AXP192_init(void);
|
2019-09-23 15:45:47 +02:00
|
|
|
void AXP192_showstatus(void);
|
|
|
|
#endif // HAS_PMU
|
2019-09-09 21:45:19 +02:00
|
|
|
|
2020-04-19 19:51:23 +02:00
|
|
|
#ifdef HAS_IP5306
|
2020-05-09 22:48:29 +02:00
|
|
|
void IP5306_init(void);
|
2020-04-19 19:51:23 +02:00
|
|
|
void printIP5306Stats(void);
|
|
|
|
uint8_t IP5306_GetPowerSource(void);
|
|
|
|
uint8_t IP5306_GetBatteryLevel(void);
|
|
|
|
uint8_t IP5306_GetBatteryFull(void);
|
2020-05-09 22:48:29 +02:00
|
|
|
void IP5306_SetChargerEnabled(uint8_t v);
|
|
|
|
void IP5306_SetChargeCutoffVoltage(uint8_t v);
|
|
|
|
void IP5306_SetEndChargeCurrentDetection(uint8_t v);
|
2020-04-19 19:51:23 +02:00
|
|
|
#endif
|
|
|
|
|
2020-04-13 22:07:26 +02:00
|
|
|
// The following map functions were taken from
|
2020-04-14 00:15:51 +02:00
|
|
|
//
|
|
|
|
// Battery.h - Battery library
|
|
|
|
// Copyright (c) 2014 Roberto Lo Giacco
|
|
|
|
// https://github.com/rlogiacco/BatterySense
|
2020-04-19 19:51:23 +02:00
|
|
|
|
2020-04-13 22:07:26 +02:00
|
|
|
/**
|
|
|
|
* Symmetric sigmoidal approximation
|
|
|
|
* https://www.desmos.com/calculator/7m9lu26vpy
|
|
|
|
*
|
|
|
|
* c - c / (1 + k*x/v)^3
|
|
|
|
*/
|
2020-04-19 19:51:23 +02:00
|
|
|
static inline uint8_t sigmoidal(uint16_t voltage, uint16_t minVoltage,
|
|
|
|
uint16_t maxVoltage) {
|
|
|
|
// slow
|
|
|
|
// uint8_t result = 110 - (110 / (1 + pow(1.468 * (voltage -
|
|
|
|
// minVoltage)/(maxVoltage - minVoltage), 6)));
|
|
|
|
|
|
|
|
// steep
|
|
|
|
// uint8_t result = 102 - (102 / (1 + pow(1.621 * (voltage -
|
|
|
|
// minVoltage)/(maxVoltage - minVoltage), 8.1)));
|
|
|
|
|
|
|
|
// normal
|
|
|
|
uint8_t result = 105 - (105 / (1 + pow(1.724 * (voltage - minVoltage) /
|
|
|
|
(maxVoltage - minVoltage),
|
|
|
|
5.5)));
|
|
|
|
return result >= 100 ? 100 : result;
|
2020-04-13 22:07:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asymmetric sigmoidal approximation
|
|
|
|
* https://www.desmos.com/calculator/oyhpsu8jnw
|
|
|
|
*
|
|
|
|
* c - c / [1 + (k*x/v)^4.5]^3
|
|
|
|
*/
|
2020-04-19 19:51:23 +02:00
|
|
|
static inline uint8_t asigmoidal(uint16_t voltage, uint16_t minVoltage,
|
|
|
|
uint16_t maxVoltage) {
|
|
|
|
uint8_t result = 101 - (101 / pow(1 + pow(1.33 * (voltage - minVoltage) /
|
|
|
|
(maxVoltage - minVoltage),
|
|
|
|
4.5),
|
|
|
|
3));
|
|
|
|
return result >= 100 ? 100 : result;
|
2020-04-13 22:07:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Linear mapping
|
|
|
|
* https://www.desmos.com/calculator/sowyhttjta
|
|
|
|
*
|
|
|
|
* x * 100 / v
|
|
|
|
*/
|
2020-04-19 19:51:23 +02:00
|
|
|
static inline uint8_t linear(uint16_t voltage, uint16_t minVoltage,
|
|
|
|
uint16_t maxVoltage) {
|
|
|
|
return (unsigned long)(voltage - minVoltage) * 100 /
|
|
|
|
(maxVoltage - minVoltage);
|
2020-04-13 22:07:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t read_battlevel(mapFn_t mapFunction = &sigmoidal);
|
|
|
|
|
2019-09-07 19:52:25 +02:00
|
|
|
#endif
|