From 62d28c54b653b228a38f77f82a188373f72f89aa Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 9 May 2020 22:48:29 +0200 Subject: [PATCH] charge current control added --- include/power.h | 20 ++++++++++++++++++++ src/main.cpp | 5 +++++ src/power.cpp | 28 ++++++++++++++++++++++++---- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/include/power.h b/include/power.h index 795fbb87..d11235c0 100644 --- a/include/power.h +++ b/include/power.h @@ -18,6 +18,22 @@ #define BAT_MIN_VOLTAGE 3100 // millivolts #endif +#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 + typedef uint8_t (*mapFn_t)(uint16_t, uint16_t, uint16_t); uint16_t read_voltage(void); @@ -37,10 +53,14 @@ void AXP192_showstatus(void); #endif // HAS_PMU #ifdef HAS_IP5306 +void IP5306_init(void); void printIP5306Stats(void); uint8_t IP5306_GetPowerSource(void); uint8_t IP5306_GetBatteryLevel(void); uint8_t IP5306_GetBatteryFull(void); +void IP5306_SetChargerEnabled(uint8_t v); +void IP5306_SetChargeCutoffVoltage(uint8_t v); +void IP5306_SetEndChargeCurrentDetection(uint8_t v); #endif // The following map functions were taken from diff --git a/src/main.cpp b/src/main.cpp index b4b1ee5c..66d9c3cb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -185,8 +185,13 @@ void setup() { digitalWrite(EXT_POWER_SW, EXT_POWER_ON); strcat_P(features, " VEXT"); #endif + +#if defined HAS_PMU || defined HAS_IP5306 #ifdef HAS_PMU AXP192_init(); +#elif defined HAS_IP5306 + IP5306_init(); +#endif strcat_P(features, " PMU"); #endif diff --git a/src/power.cpp b/src/power.cpp index 74324c5e..5d3d770e 100644 --- a/src/power.cpp +++ b/src/power.cpp @@ -143,10 +143,10 @@ void AXP192_init(void) { pmu.clearIRQ(); #endif // PMU_INT -// set charge current according to user setting if we have -#ifdef PMU_CHGC - pmu.setChargeControlCur(PMU_CHGC); - pmu.setChargingTargetVoltage(AXP202_TARGET_VOL_4_2V); +// set charging parameterss according to user settings if we have (see power.h) +#ifdef PMU_CHARGE_CURRENT + pmu.setChargeControlCur(PMU_CHARGE_CURRENT); + pmu.setChargingTargetVoltage(PMU_CHARGE_CUTOFF); pmu.enableChargeing(true); #endif @@ -299,6 +299,20 @@ uint8_t IP5306_GetBatteryLevel(void) { return IP5306_LEDS2PCT(state); } +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 +} + void printIP5306Stats(void) { bool usb = IP5306_GetPowerSource(); bool full = IP5306_GetBatteryFull(); @@ -309,4 +323,10 @@ void printIP5306Stats(void) { full ? "CHARGED" : (usb ? "CHARGING" : "DISCHARGING"), level); } +void IP5306_init(void) { + IP5306_SetChargerEnabled(1); + IP5306_SetChargeCutoffVoltage(PMU_CHG_CUTOFF); + IP5306_SetEndChargeCurrentDetection(PMU_CHG_CURRENT); +} + #endif // HAS_IP5306 \ No newline at end of file