diff options
author | Ruben Rodriguez Buchillon <coconutruben@chromium.org> | 2017-11-28 11:01:47 +0800 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2018-01-16 04:08:26 -0800 |
commit | 51e9e69f386366256807e6f4ccdd258821cdcfe0 (patch) | |
tree | c6fc8260505edadf8da3e6e0237a7bc6b3906da3 | |
parent | be1f97a2551549d81f77f0439a23683343234c40 (diff) | |
download | chrome-ec-51e9e69f386366256807e6f4ccdd258821cdcfe0.tar.gz |
power: introducing pwr_avg console command
pwr_avg provides an average voltage, current, and power over the last
1 minute. It's up to the battery drivers to implement this
functionality.
This change allows us to have better power tracking while minimizing
the power impact on the EC, because
- the pwr_avg command only needs to be called once every minute, and is
short, thus less expensive to parse on ECs without a UART buffer
- the work done to keep the avg is partially done by the batteries
already and it's just a question of retrieving it.
undefined on wheatley since no power debugging planned on that board.
usage:
> pwr_avg
mv = 7153
ma = -605
mw = -4327
BUG=chromium:752320
BRANCH=None
TEST=make buildall -j
Change-Id: Id1a3479d277aedf90dfa965afb4ee9136654b1cf
Signed-off-by: Ruben Rodriguez Buchillon <coconutruben@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/823884
Reviewed-by: Vincent Palatin <vpalatin@chromium.org>
-rw-r--r-- | board/wheatley/board.h | 1 | ||||
-rw-r--r-- | common/charge_state_v2.c | 28 | ||||
-rw-r--r-- | driver/battery/max17055.c | 14 | ||||
-rw-r--r-- | driver/battery/smart.c | 24 | ||||
-rw-r--r-- | include/battery.h | 10 | ||||
-rw-r--r-- | include/config.h | 8 |
6 files changed, 85 insertions, 0 deletions
diff --git a/board/wheatley/board.h b/board/wheatley/board.h index c3ccf33a84..84eb25091c 100644 --- a/board/wheatley/board.h +++ b/board/wheatley/board.h @@ -147,6 +147,7 @@ #undef CONFIG_CONSOLE_CMDHELP #undef CONFIG_CMD_I2C_SCAN #undef CONFIG_CONSOLE_HISTORY +#undef CONFIG_CMD_PWR_AVG /* Features of eSPI */ #undef CONFIG_ESPI /* Use eSPI protocol for host interface of x86 CPU */ diff --git a/common/charge_state_v2.c b/common/charge_state_v2.c index cc145d6971..75c25d072d 100644 --- a/common/charge_state_v2.c +++ b/common/charge_state_v2.c @@ -1556,6 +1556,34 @@ DECLARE_HOST_COMMAND(EC_CMD_CHARGE_STATE, charge_command_charge_state, /*****************************************************************************/ /* Console commands */ +#ifdef CONFIG_CMD_PWR_AVG + +static int command_pwr_avg(int argc, char **argv) +{ + int avg_mv; + int avg_ma; + int avg_mw; + + if (argc != 1) + return EC_ERROR_PARAM_COUNT; + + avg_mv = battery_get_avg_voltage(); + if (avg_mv < 0) + return EC_ERROR_UNKNOWN; + avg_ma = battery_get_avg_current(); + avg_mw = avg_mv * avg_ma / 1000; + + ccprintf("mv = %d\nma = %d\nmw = %d\n", + avg_mv, avg_ma, avg_mw); + return EC_SUCCESS; +} + +DECLARE_CONSOLE_COMMAND(pwr_avg, command_pwr_avg, + NULL, + "Get 1 min power average"); + +#endif /* CONFIG_CMD_PWR_AVG */ + static int command_chgstate(int argc, char **argv) { int rv; diff --git a/driver/battery/max17055.c b/driver/battery/max17055.c index cd0a379e8e..1a38c2748f 100644 --- a/driver/battery/max17055.c +++ b/driver/battery/max17055.c @@ -263,6 +263,20 @@ void battery_get_params(struct batt_params *batt) batt->flags |= BATT_FLAG_WANT_CHARGE; } +#ifdef CONFIG_CMD_PWR_AVG +int battery_get_avg_current(void) +{ + /* TODO(crbug.com/752320) implement this */ + return EC_ERROR_UNIMPLEMENTED; +} + +int battery_get_avg_voltage(void) +{ + /* TODO(crbug.com/752320) implement this */ + return -EC_ERROR_UNIMPLEMENTED; +} +#endif /* CONFIG_CMD_PWR_AVG */ + /* Wait until battery is totally stable. */ int battery_wait_for_stable(void) { diff --git a/driver/battery/smart.c b/driver/battery/smart.c index bbbec8f21e..743ebc1ee1 100644 --- a/driver/battery/smart.c +++ b/driver/battery/smart.c @@ -283,6 +283,30 @@ test_mockable int battery_device_chemistry(char *dest, int size) return sb_read_string(SB_DEVICE_CHEMISTRY, dest, size); } +#ifdef CONFIG_CMD_PWR_AVG +int battery_get_avg_current(void) +{ + int current; + + /* This is a signed 16-bit value. */ + sb_read(SB_AVERAGE_CURRENT, ¤t); + return (int16_t)current; +} + +/* + * Technically returns only the instantaneous reading, but tests showed that + * for the majority of charge states above 3% this varies by less than 40mV + * every minute, so we accept the inaccuracy here. + */ +int battery_get_avg_voltage(void) +{ + int voltage = -EC_ERROR_UNKNOWN; + + sb_read(SB_VOLTAGE, &voltage); + return voltage; +} +#endif /* CONFIG_CMD_PWR_AVG */ + void battery_get_params(struct batt_params *batt) { struct batt_params batt_new = {0}; diff --git a/include/battery.h b/include/battery.h index 0ccd29319d..43425fcd8c 100644 --- a/include/battery.h +++ b/include/battery.h @@ -74,6 +74,16 @@ struct batt_params { int flags; /* Flags */ }; +/* + * Provide a 1 minute average of the current and voltage on the battery. + * Does not check for flags or whether those values are bad readings. + * See driver/battery/[your_driver].h/c for details on implementation and + * how the average is calculated. + */ + +int battery_get_avg_current(void); /* in mA */ +int battery_get_avg_voltage(void); /* in mV */ + /* Flags for batt_params */ /* Battery wants to be charged */ diff --git a/include/config.h b/include/config.h index 46705f89e3..50f84314ed 100644 --- a/include/config.h +++ b/include/config.h @@ -783,6 +783,7 @@ #undef CONFIG_CMD_PMU #define CONFIG_CMD_POWERINDEBUG #undef CONFIG_CMD_POWERLED +#define CONFIG_CMD_PWR_AVG #define CONFIG_CMD_POWER_AP #undef CONFIG_CMD_PPC_DUMP #define CONFIG_CMD_REGULATOR @@ -3281,6 +3282,13 @@ #undef CONFIG_HOSTCMD_PD #endif +/* + * Power Average task only works when there's a battery to talk to. + */ +#ifndef CONFIG_BATTERY +#undef CONFIG_CMD_PWR_AVG +#endif + /*****************************************************************************/ /* * Apply test config overrides last, since tests need to override some of the |