From 97c339ed76e8306bb288d2d5d9be5a6260953a1f Mon Sep 17 00:00:00 2001 From: Tang Qijun Date: Tue, 27 Dec 2022 15:08:43 +0800 Subject: chgstv2: optimization battery protection for low voltage Deep_charge_battery needs to precharge first, and then enter the idle or charge state. Because before entering deep_charge_battery, the curr.state state may be the idle state, which may cause no precharge process. Therefore, add a flag to curr.batt.flags for record whether the precharge process has been performed. BRANCH=trogdor BUG=b:263921114 TEST=After the battery is discharged below voltage_min, the charging process enters deep_charge_battery and precharges first, and when the battery voltage is greater than voltage_min, it enters the charge state and charges normally. TEST=run-battery_get_params_smart Change-Id: I3a353bbcb4149b873176006bc009fbd3e8824aab Signed-off-by: Tang Qijun Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4125706 Tested-by: Daisuke Nojiri Reviewed-by: Daisuke Nojiri Commit-Queue: Daisuke Nojiri Code-Coverage: Zoss Commit-Queue: Bob Moragues (cherry picked from commit f0436fe4c66f1a6759c2ae28157ec495b53eab9c) Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4212310 Reviewed-by: Shou-Chieh Hsu Reviewed-by: Owen Yang Commit-Queue: Ivan Chen Tested-by: Owen Yang --- common/charge_state_v2.c | 29 ++++++++++++++++++++++------- include/battery.h | 3 +++ test/battery_get_params_smart.c | 4 ++-- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/common/charge_state_v2.c b/common/charge_state_v2.c index 9b567ba2ff..6b5336c03a 100644 --- a/common/charge_state_v2.c +++ b/common/charge_state_v2.c @@ -1539,7 +1539,8 @@ __test_only enum charge_state_v2 charge_get_state_v2(void) static void deep_charge_battery(int *need_static) { - if (curr.state == ST_IDLE) { + if ((curr.state == ST_IDLE) && + (curr.batt.flags & BATT_FLAG_DEEP_CHARGE)) { /* Deep charge time out , do nothing */ curr.requested_voltage = 0; curr.requested_current = 0; @@ -1562,6 +1563,7 @@ static void deep_charge_battery(int *need_static) set_charge_state(ST_PRECHARGE); curr.requested_voltage = batt_info->voltage_max; curr.requested_current = batt_info->precharge_current; + curr.batt.flags |= BATT_FLAG_DEEP_CHARGE; } } @@ -1814,13 +1816,26 @@ void charger_task(void *u) goto wait_for_it; } - if (IS_ENABLED(CONFIG_BATTERY_LOW_VOLTAGE_PROTECTION) && - !(curr.batt.flags & BATT_FLAG_BAD_VOLTAGE) && - (curr.batt.voltage <= batt_info->voltage_min)) { - deep_charge_battery(&need_static); - goto wait_for_it; - } + /* + * When the battery voltage is lower than voltage_min,precharge + * first to protect the battery + */ + if (IS_ENABLED(CONFIG_BATTERY_LOW_VOLTAGE_PROTECTION)) { + if (!(curr.batt.flags & BATT_FLAG_BAD_VOLTAGE) && + (curr.batt.voltage <= batt_info->voltage_min)) { + deep_charge_battery(&need_static); + goto wait_for_it; + } + /* + * Finished deep charge before timeout. Clear the flag + * so that we can do deep charge again (when it's deeply + * discharged again). + */ + if ((curr.batt.flags & BATT_FLAG_DEEP_CHARGE)) { + curr.batt.flags &= ~BATT_FLAG_DEEP_CHARGE; + } + } /* The battery is responding. Yay. Try to use it. */ /* diff --git a/include/battery.h b/include/battery.h index ce39d8ddd2..724afad905 100644 --- a/include/battery.h +++ b/include/battery.h @@ -162,6 +162,9 @@ int battery_get_avg_voltage(void); /* in mV */ (BATT_FLAG_BAD_ANY | BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE | \ BATT_FLAG_IMBALANCED_CELL) +/* The flag of prechare when the battery voltage is lower than voltage_min */ +#define BATT_FLAG_DEEP_CHARGE 0x00010000 + /* Battery constants */ struct battery_info { /* Operation voltage in mV */ diff --git a/test/battery_get_params_smart.c b/test/battery_get_params_smart.c index 63a0abe64c..f63003d8d9 100644 --- a/test/battery_get_params_smart.c +++ b/test/battery_get_params_smart.c @@ -167,9 +167,9 @@ static int test_flags(void) */ reset_and_fail_on(0, 0, -1); batt.flags |= BATT_FLAG_BAD_TEMPERATURE; - batt.flags |= BIT(31); + batt.flags |= BATT_FLAG_DEEP_CHARGE; battery_get_params(&batt); - TEST_ASSERT(batt.flags & BIT(31)); + TEST_ASSERT(batt.flags & BATT_FLAG_DEEP_CHARGE); TEST_ASSERT(!(batt.flags & BATT_FLAG_BAD_ANY)); /* -- cgit v1.2.1