summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRong Chang <rongchang@chromium.org>2012-09-11 08:58:55 +0800
committerGerrit <chrome-bot@google.com>2012-09-10 23:15:43 -0700
commit774d047d5ee363c565cf58561c2d92c871eb87cf (patch)
tree6399f9d6263a1c75895316638c4053ec6813e562
parente21db5e488259d5bcab1a8429367b5c8b35cfc27 (diff)
downloadchrome-ec-774d047d5ee363c565cf58561c2d92c871eb87cf.tar.gz
snow: Clear state of charge calculation window on state change
The moving average window contains previous discharging state of charge values after state change. This change resets the index to make it calculate only new battery readings. Signed-off-by: Rong Chang <rongchang@chromium.org> BRANCH=snow BUG=chrome-os-partner:13846 TEST=none Change-Id: Ifc6c6208dea8edf262e7294972d7321501b709e2 Reviewed-on: https://gerrit.chromium.org/gerrit/32865 Commit-Ready: Rong Chang <rongchang@chromium.org> Reviewed-by: Rong Chang <rongchang@chromium.org> Tested-by: Rong Chang <rongchang@chromium.org>
-rw-r--r--common/pmu_tps65090_charger.c23
1 files changed, 19 insertions, 4 deletions
diff --git a/common/pmu_tps65090_charger.c b/common/pmu_tps65090_charger.c
index 03c2d51f03..be3abd0bb2 100644
--- a/common/pmu_tps65090_charger.c
+++ b/common/pmu_tps65090_charger.c
@@ -132,6 +132,13 @@ static int notify_battery_low(void)
/*
* Calculate relative state of charge moving average
*
+ * @param state_of_charge Current battery state of charge reading,
+ * from 0 to 100. When state_of_charge < 0,
+ * resets the moving average window
+ * @return Average state of charge, rounded to nearest
+ * integer.
+ * -1 when window reset.
+ *
* The returned value will be rounded to the nearest integer, by set moving
* average init value to (0.5 * window_size).
*
@@ -140,11 +147,16 @@ static int rsoc_moving_average(int state_of_charge)
{
static uint8_t rsoc[4];
static int8_t index = -1;
- int moving_average = sizeof(rsoc) / 2;
+ int moving_average = ARRAY_SIZE(rsoc) / 2;
int i;
+ if (state_of_charge < 0) {
+ index = -1;
+ return -1;
+ }
+
if (index < 0) {
- for (i = 0; i < sizeof(rsoc); i++)
+ for (i = 0; i < ARRAY_SIZE(rsoc); i++)
rsoc[i] = (uint8_t)state_of_charge;
index = 0;
return state_of_charge;
@@ -154,9 +166,9 @@ static int rsoc_moving_average(int state_of_charge)
index++;
index %= sizeof(rsoc);
- for (i = 0; i < sizeof(rsoc); i++)
+ for (i = 0; i < ARRAY_SIZE(rsoc); i++)
moving_average += (int)rsoc[i];
- moving_average /= sizeof(rsoc);
+ moving_average /= ARRAY_SIZE(rsoc);
return moving_average;
}
@@ -388,6 +400,9 @@ void pmu_charger_task(void)
calc_next_state(state);
if (next_state != state) {
+ /* Reset state of charge moving average window */
+ rsoc_moving_average(-1);
+
pre_charging_count = 0;
CPRINTF("[batt] state %s -> %s\n",
state_list[state],