diff options
author | Rong Chang <rongchang@chromium.org> | 2012-09-11 08:58:55 +0800 |
---|---|---|
committer | Rong Chang <rongchang@chromium.org> | 2012-09-10 20:40:39 -0700 |
commit | dfd30fee73e4c5fa9ef6f9c2859b5e58f26e101d (patch) | |
tree | 0788e776fb223c7cdaf37ed7b6072c27923a4a19 | |
parent | 3d05292d34f9840488f868d48def142efe03cc82 (diff) | |
download | chrome-ec-dfd30fee73e4c5fa9ef6f9c2859b5e58f26e101d.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
Original-Change-Id: Ifc6c6208dea8edf262e7294972d7321501b709e2
(cherry picked from https://gerrit.chromium.org/gerrit/32865)
Change-Id: Ic7fcac9dfdd499fd0d5a3d1598defb44278509df
Reviewed-on: https://gerrit.chromium.org/gerrit/32897
Reviewed-by: Rong Chang <rongchang@chromium.org>
Tested-by: Rong Chang <rongchang@chromium.org>
-rw-r--r-- | common/pmu_tps65090_charger.c | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/common/pmu_tps65090_charger.c b/common/pmu_tps65090_charger.c index b1284759de..60531c4fc6 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; } @@ -352,6 +364,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], |