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>
This commit is contained in:
Ruben Rodriguez Buchillon
2017-11-28 11:01:47 +08:00
committed by chrome-bot
parent be1f97a255
commit 51e9e69f38
6 changed files with 85 additions and 0 deletions

View File

@@ -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 */

View File

@@ -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;

View File

@@ -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)
{

View File

@@ -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, &current);
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};

View File

@@ -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 */

View File

@@ -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