summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRong Chang <rongchang@chromium.org>2012-03-16 17:01:59 +0800
committerRong Chang <rongchang@chromium.org>2012-03-19 16:08:40 +0800
commite8afc9946a048eef34ccf4328c6a021b74afd98c (patch)
treed988b915855cbb23b0cffe9b170e8445112d9932
parent54f8d7e323ce6e36612732b4d107e6ee846c6fad (diff)
downloadchrome-ec-e8afc9946a048eef34ccf4328c6a021b74afd98c.tar.gz
Add battery SMI events and refactor charging state
This CL adds battery SMI events. And refactors the charging state machine by adding share state context for all handlers. Power events are moved to common handler. Minor clean up on console output messages. Signed-off-by: Rong Chang <rongchang@chromium.org> BUG=chrome-os-partner:7526,7937,8450 TEST=manual: Watch console message when connecting/disconnecting AC adapter and battery. Check the state transition. Change-Id: I42eec4f87a9d49bd193cb9dde9080e3dfccbb77c
-rw-r--r--common/battery_commands.c4
-rw-r--r--common/charge_state.c478
-rw-r--r--include/battery.h6
3 files changed, 273 insertions, 215 deletions
diff --git a/common/battery_commands.c b/common/battery_commands.c
index 44d90b01d5..c4f282c699 100644
--- a/common/battery_commands.c
+++ b/common/battery_commands.c
@@ -24,8 +24,8 @@ enum lpc_status battery_command_get_info(uint8_t *data)
if (battery_design_capacity(&val))
return EC_LPC_RESULT_ERROR;
r->design_capacity = val;
- r->design_capacity_warning = val * BATTERY_WARNING_PERCENTAGE / 100;
- r->design_capacity_low = val * BATTERY_LOW_PERCENTAGE / 100;
+ r->design_capacity_warning = val * BATTERY_LEVEL_WARNING / 100;
+ r->design_capacity_low = val * BATTERY_LEVEL_LOW / 100;
if (battery_full_charge_capacity(&val))
return EC_LPC_RESULT_ERROR;
diff --git a/common/charge_state.c b/common/charge_state.c
index 088f43669f..1adea77969 100644
--- a/common/charge_state.c
+++ b/common/charge_state.c
@@ -6,6 +6,7 @@
*/
+#include "battery.h"
#include "battery_pack.h"
#include "board.h"
#include "console.h"
@@ -20,8 +21,6 @@
/* Stop charge when state of charge reaches this percentage */
#define STOP_CHARGE_THRESHOLD 100
-/* Critical level of when discharging reaches this percentage */
-#define BATT_CRITICAL_LEVEL 10
/* power state task polling period in usec */
#define POLL_PERIOD_LONG 500000
@@ -29,6 +28,26 @@
#define POLL_PERIOD_SHORT 100000
#define MIN_SLEEP_USEC 50000
+/* Power state error flags */
+#define F_CHARGER_INIT (1 << 0) /* Charger initialization */
+#define F_CHARGER_VOLTAGE (1 << 1) /* Charger maximun output voltage */
+#define F_CHARGER_CURRENT (1 << 2) /* Charger maximum output current */
+#define F_BATTERY_VOLTAGE (1 << 3) /* Battery voltage */
+#define F_BATTERY_CURRENT (1 << 4) /* Battery charging current */
+#define F_DESIRED_VOLTAGE (1 << 5) /* Battery desired voltage */
+#define F_DESIRED_CURRENT (1 << 6) /* Battery desired current */
+#define F_BATTERY_TEMPERATURE (1 << 7) /* Battery temperature */
+#define F_BATTERY_MODE (1 << 8) /* Battery mode */
+#define F_BATTERY_CAPACITY (1 << 9) /* Battery capacity */
+#define F_BATTERY_STATE_OF_CHARGE (1 << 10) /* State of charge, percentage */
+
+#define F_BATTERY_MASK (F_BATTERY_VOLTAGE | F_BATTERY_CURRENT | \
+ F_DESIRED_VOLTAGE | F_DESIRED_CURRENT | \
+ F_BATTERY_TEMPERATURE | F_BATTERY_MODE | \
+ F_BATTERY_CAPACITY | F_BATTERY_STATE_OF_CHARGE)
+#define F_CHARGER_MASK (F_CHARGER_VOLTAGE | F_CHARGER_CURRENT | \
+ F_CHARGER_INIT)
+
/* Power states */
enum power_state {
PWR_STATE_UNCHANGE = 0,
@@ -38,12 +57,9 @@ enum power_state {
PWR_STATE_CHARGE,
PWR_STATE_ERROR
};
-/* Debugging constants, in the same order as power_state.
- * This state name table and debug print will be removed
- * before production.
- */
-const static char *_state_name[] = {
- "null",
+/* Debugging constants, in the same order as power_state. */
+static const char * const state_name[] = {
+ "unchange",
"init",
"idle",
"discharge",
@@ -51,6 +67,32 @@ const static char *_state_name[] = {
"error"
};
+/* Power state data
+ * Status collection of charging state machine.
+ */
+struct power_state_data {
+ int ac;
+ int charging_voltage;
+ int charging_current;
+ struct batt_params batt;
+ enum power_state state;
+ uint32_t error;
+ timestamp_t ts;
+};
+
+/* State context
+ * The shared context for state handler. The context contains current and
+ * previous state.
+ */
+struct power_state_context {
+ struct power_state_data curr;
+ struct power_state_data prev;
+ uint32_t *memmap_batt_volt;
+ /* TODO(rong): check endianness of EC and memmap*/
+ uint32_t *memmap_batt_rate;
+ uint32_t *memmap_batt_cap;
+ uint8_t *memmap_batt_flags;
+};
/* helper function(s) */
static inline int get_ac(void)
@@ -58,61 +100,139 @@ static inline int get_ac(void)
return gpio_get_level(GPIO_AC_PRESENT);
}
-/* Get battery charging parameters from battery and
- * battery pack vendor table
+/* Common handler for charging states.
+ * This handler gets battery charging parameters, charger state, ac state,
+ * and timestamp. It also fills memory map and issues power events on state
+ * change.
*/
-static int battery_params(struct batt_params *batt)
+static int state_common(struct power_state_context *ctx)
{
int rv, d;
- /* Direct mem mapped EC data */
- uint32_t *memmap_batt_volt = (uint32_t*)(lpc_get_memmap_range() +
- EC_LPC_MEMMAP_BATT_VOLT);
- uint32_t *memmap_batt_rate = (uint32_t*)(lpc_get_memmap_range() +
- EC_LPC_MEMMAP_BATT_RATE);
- uint32_t *memmap_batt_cap = (uint32_t*)(lpc_get_memmap_range() +
- EC_LPC_MEMMAP_BATT_CAP);
+
+ struct power_state_data *curr = &ctx->curr;
+ struct power_state_data *prev = &ctx->prev;
+ struct batt_params *batt = &ctx->curr.batt;
+ uint8_t *batt_flags = ctx->memmap_batt_flags;
+
+ /* Copy previous state and init new state */
+ ctx->prev = ctx->curr;
+ curr->ts = get_time();
+ curr->error = 0;
+
+ /* Detect AC change */
+ curr->ac = get_ac();
+ if (curr->ac != prev->ac) {
+ if (curr->ac) {
+ /* AC on
+ * Initialize charger to power on reset mode
+ * TODO(rong):
+ * crosbug.com/p/7937 Send PCH AC present
+ */
+ rv = charger_post_init();
+ if (rv)
+ curr->error |= F_CHARGER_INIT;
+ lpc_set_host_events(EC_LPC_HOST_EVENT_MASK(
+ EC_LPC_HOST_EVENT_AC_CONNECTED));
+ } else {
+ /* AC off */
+ lpc_set_host_events(EC_LPC_HOST_EVENT_MASK(
+ EC_LPC_HOST_EVENT_AC_DISCONNECTED));
+ }
+ }
+
+ if (curr->ac) {
+ *batt_flags |= EC_BATT_FLAG_AC_PRESENT;
+ rv = charger_get_voltage(&curr->charging_voltage);
+ if (rv) {
+ charger_set_voltage(0);
+ charger_set_current(0);
+ curr->error |= F_CHARGER_VOLTAGE;
+ }
+ rv = charger_get_current(&curr->charging_current);
+ if (rv) {
+ charger_set_voltage(0);
+ charger_set_current(0);
+ curr->error |= F_CHARGER_CURRENT;
+ }
+ } else
+ *batt_flags &= ~EC_BATT_FLAG_AC_PRESENT;
rv = battery_temperature(&batt->temperature);
if (rv)
- return rv;
+ curr->error |= F_BATTERY_TEMPERATURE;
rv = battery_voltage(&batt->voltage);
if (rv)
- return rv;
- *memmap_batt_volt = batt->voltage;
+ curr->error |= F_BATTERY_VOLTAGE;
+ *ctx->memmap_batt_volt = batt->voltage;
rv = battery_current(&batt->current);
if (rv)
- return rv;
- *memmap_batt_rate = batt->current < 0 ? -batt->current : 0;
+ curr->error |= F_BATTERY_CURRENT;
+ /* Memory mapped value: discharge rate */
+ *ctx->memmap_batt_rate = batt->current < 0 ? -batt->current : 0;
rv = battery_desired_voltage(&batt->desired_voltage);
if (rv)
- return rv;
+ curr->error |= F_DESIRED_VOLTAGE;
rv = battery_desired_current(&batt->desired_current);
if (rv)
- return rv;
+ curr->error |= F_DESIRED_CURRENT;
+
+ rv = battery_state_of_charge(&batt->state_of_charge);
+ if (rv)
+ curr->error |= F_BATTERY_STATE_OF_CHARGE;
+
+ /* Check battery presence */
+ if (curr->error & F_BATTERY_MASK) {
+ *ctx->memmap_batt_flags &= ~EC_BATT_FLAG_BATT_PRESENT;
+ return curr->error;
+ }
+ *ctx->memmap_batt_flags |= EC_BATT_FLAG_BATT_PRESENT;
+
+ /* Battery charge level low */
+ if (batt->state_of_charge <= BATTERY_LEVEL_LOW &&
+ prev->batt.state_of_charge > BATTERY_LEVEL_LOW)
+ lpc_set_host_events(EC_LPC_HOST_EVENT_MASK(
+ EC_LPC_HOST_EVENT_BATTERY_LOW));
+
+ /* Battery charge level critical */
+ if (batt->state_of_charge <= BATTERY_LEVEL_CRITICAL) {
+ *ctx->memmap_batt_flags |= EC_BATT_FLAG_LEVEL_CRITICAL;
+ /* Send battery critical host event */
+ if (prev->batt.state_of_charge > BATTERY_LEVEL_CRITICAL)
+ lpc_set_host_events(EC_LPC_HOST_EVENT_MASK(
+ EC_LPC_HOST_EVENT_BATTERY_CRITICAL));
+ } else
+ *ctx->memmap_batt_flags &= ~EC_BATT_FLAG_LEVEL_CRITICAL;
+
+
+ /* Apply battery pack vendor charging method */
battery_vendor_params(batt);
rv = battery_get_battery_mode(&d);
- if (rv)
- return rv;
-
- if (d & MODE_CAPACITY) {
- /* Battery capacity mode was set to mW, set it back to mAh */
- d &= ~MODE_CAPACITY;
- rv = battery_set_battery_mode(d);
- if (rv)
- return rv;
+ if (rv) {
+ curr->error |= F_BATTERY_MODE;
+ } else {
+ if (d & MODE_CAPACITY) {
+ /* Battery capacity mode was set to mW
+ * reset it back to mAh
+ */
+ d &= ~MODE_CAPACITY;
+ rv = battery_set_battery_mode(d);
+ if (rv)
+ ctx->curr.error |= F_BATTERY_MODE;
+ }
}
rv = battery_remaining_capacity(&d);
if (rv)
- return rv;
- *memmap_batt_cap = d;
+ ctx->curr.error |= F_BATTERY_CAPACITY;
+ else
+ *ctx->memmap_batt_cap = d;
- return EC_SUCCESS;
+ return ctx->curr.error;
}
/* Init state handler
@@ -120,29 +240,18 @@ static int battery_params(struct batt_params *batt)
* - initialize charger
* - new states: DISCHARGE, IDLE
*/
-static enum power_state state_init(void)
+static enum power_state state_init(struct power_state_context *ctx)
{
- int rv, val;
-
/* Stop charger, unconditionally */
charger_set_current(0);
charger_set_voltage(0);
- /* Detect AC, init charger */
- if (!get_ac())
+ /* If AC is not present, switch to discharging state */
+ if (!ctx->curr.ac)
return PWR_STATE_DISCHARGE;
- /* Initialize charger to power on reset mode */
- rv = charger_post_init();
- if (rv)
- return PWR_STATE_ERROR;
- /* Check if charger is online */
- rv = charger_get_status(&val);
- if (rv)
- return PWR_STATE_ERROR;
-
- /* Detect battery */
- if (battery_temperature(&val))
+ /* Check general error conditions */
+ if (ctx->curr.error)
return PWR_STATE_ERROR;
return PWR_STATE_IDLE;
@@ -153,37 +262,29 @@ static enum power_state state_init(void)
* - detect charger and battery status change
* - new states: CHARGE, INIT
*/
-static enum power_state state_idle(void)
+static enum power_state state_idle(struct power_state_context *ctx)
{
- int voltage, current, state_of_charge;
- struct batt_params batt;
-
- if (!get_ac())
+ if (!ctx->curr.ac)
return PWR_STATE_INIT;
- /* Prevent charging in idle mode */
- if (charger_get_voltage(&voltage))
+ if (ctx->curr.error)
return PWR_STATE_ERROR;
- if (charger_get_current(&current))
- return PWR_STATE_ERROR;
- if (voltage || current)
- return PWR_STATE_INIT;
- if (battery_state_of_charge(&state_of_charge))
- return PWR_STATE_ERROR;
+ /* Prevent charging in idle mode */
+ if (ctx->curr.charging_voltage ||
+ ctx->curr.charging_current)
+ return PWR_STATE_INIT;
- if (state_of_charge >= STOP_CHARGE_THRESHOLD)
+ if (ctx->curr.batt.state_of_charge >= STOP_CHARGE_THRESHOLD)
return PWR_STATE_UNCHANGE;
- /* Check if the batter is good to charge */
- if (battery_params(&batt))
- return PWR_STATE_ERROR;
-
/* Configure init charger state and switch to charge state */
- if (batt.desired_voltage && batt.desired_current) {
- if (charger_set_voltage(batt.desired_voltage))
+ if (ctx->curr.batt.desired_voltage &&
+ ctx->curr.batt.desired_current) {
+ /* Set charger output constraints */
+ if (charger_set_voltage(ctx->curr.batt.desired_voltage))
return PWR_STATE_ERROR;
- if (charger_set_current(batt.desired_current))
+ if (charger_set_current(ctx->curr.batt.desired_current))
return PWR_STATE_ERROR;
return PWR_STATE_CHARGE;
}
@@ -195,43 +296,32 @@ static enum power_state state_idle(void)
* - detect battery status change
* - new state: INIT
*/
-static enum power_state state_charge(void)
+static enum power_state state_charge(struct power_state_context *ctx)
{
- int chg_voltage, chg_current;
- struct batt_params batt;
-
- if (!get_ac())
+ if (!ctx->curr.ac)
return PWR_STATE_INIT;
- if (charger_get_voltage(&chg_voltage))
- return PWR_STATE_ERROR;
-
- if (charger_get_current(&chg_current))
+ if (ctx->curr.error)
return PWR_STATE_ERROR;
/* Check charger reset */
- if (chg_voltage == 0 || chg_current == 0)
+ if (ctx->curr.charging_voltage == 0 ||
+ ctx->curr.charging_current == 0)
return PWR_STATE_INIT;
- if (battery_params(&batt))
- return PWR_STATE_ERROR;
-
- if (batt.desired_voltage != chg_voltage)
- if (charger_set_voltage(batt.desired_voltage))
- return PWR_STATE_ERROR;
- if (batt.desired_current != chg_current)
- if (charger_set_current(batt.desired_current))
- return PWR_STATE_ERROR;
-
- if (battery_state_of_charge(&batt.state_of_charge))
- return PWR_STATE_ERROR;
-
- if (batt.state_of_charge >= STOP_CHARGE_THRESHOLD) {
+ if (ctx->curr.batt.state_of_charge >= STOP_CHARGE_THRESHOLD) {
if (charger_set_voltage(0) || charger_set_current(0))
return PWR_STATE_ERROR;
return PWR_STATE_IDLE;
}
+ if (ctx->curr.batt.desired_voltage != ctx->curr.charging_voltage)
+ if (charger_set_voltage(ctx->curr.batt.desired_voltage))
+ return PWR_STATE_ERROR;
+ if (ctx->curr.batt.desired_current != ctx->curr.charging_current)
+ if (charger_set_current(ctx->curr.batt.desired_current))
+ return PWR_STATE_ERROR;
+
return PWR_STATE_UNCHANGE;
}
@@ -239,21 +329,17 @@ static enum power_state state_charge(void)
* - detect ac status
* - new state: INIT
*/
-static enum power_state state_discharge(void)
+static enum power_state state_discharge(struct power_state_context *ctx)
{
- struct batt_params batt;
- uint8_t *memmap_batt_flags = (uint8_t*)(lpc_get_memmap_range() +
- EC_LPC_MEMMAP_BATT_FLAG);
- if (get_ac())
+ if (ctx->curr.ac)
return PWR_STATE_INIT;
- if (battery_params(&batt))
+ if (ctx->curr.error)
return PWR_STATE_ERROR;
- if (batt.state_of_charge <= BATT_CRITICAL_LEVEL)
- *memmap_batt_flags |= EC_BATT_FLAG_LEVEL_CRITICAL;
-
- /* TODO: handle overtemp in discharge mode */
+ /* TODO(rong): crosbug.com/p/8451
+ * handle overtemp in discharge mode
+ */
return PWR_STATE_UNCHANGE;
}
@@ -263,170 +349,140 @@ static enum power_state state_discharge(void)
* - log error
* - new state: INIT
*/
-static enum power_state state_error(void)
+static enum power_state state_error(struct power_state_context *ctx)
{
- enum { F_CHG_V, F_CHG_I, F_BAT_V, F_BAT_I,
- F_DES_V, F_DES_I, F_BAT_T, F_LAST };
- static int last_error_flags;
- int error_flags = 0;
- int ac = 0;
- int bat_v = -1, bat_i = -1, bat_temp = -1;
- int desired_v = -1, desired_i = -1;
- uint8_t batt_flags = 0;
- uint8_t *memmap_batt_flags = (uint8_t*)(lpc_get_memmap_range() +
- EC_LPC_MEMMAP_BATT_FLAG);
+ static int logged_error;
- ac = get_ac();
- if (ac) {
- batt_flags = EC_BATT_FLAG_AC_PRESENT;
- if (charger_set_voltage(0))
- error_flags |= (1 << F_CHG_V);
- if (charger_set_current(0))
- error_flags |= (1 << F_CHG_I);
- } else
- batt_flags = EC_BATT_FLAG_DISCHARGING;
-
- if (battery_voltage(&bat_v))
- error_flags |= (1 << F_BAT_V);
- if (battery_current(&bat_i))
- error_flags |= (1 << F_BAT_I);
- if (battery_temperature(&bat_temp))
- error_flags |= (1 << F_BAT_T);
- if (battery_desired_voltage(&desired_v))
- error_flags |= (1 << F_DES_V);
- if (battery_desired_current(&desired_i))
- error_flags |= (1 << F_DES_I);
-
- if (error_flags == 0) {
- last_error_flags = 0;
+ if (!ctx->curr.error) {
+ logged_error = 0;
return PWR_STATE_INIT;
}
- /* Check if all battery operation returned success */
- if (!(error_flags & (F_BAT_V | F_BAT_I | F_DES_V | F_DES_I | F_BAT_T)))
- batt_flags |= EC_BATT_FLAG_BATT_PRESENT;
-
/* Debug output */
- if (error_flags != last_error_flags) {
- uart_printf("[Charge error flags %02x -> %02x; AC=%d",
- last_error_flags, error_flags, ac);
-
- if (error_flags & (F_CHG_V | F_CHG_I))
- uart_puts(", charger error");
-
- uart_printf(", battery V=%d I=%d T=%d Vwant=%d Iwant=%d]\n",
- bat_v, bat_i, (bat_temp - 2731) / 10,
- desired_v, desired_i);
-
- last_error_flags = error_flags;
+ if (ctx->curr.error != logged_error) {
+ uart_printf("[Charge error: flag[%08b -> %08b], ac %d, "
+ " charger %s, battery %s\n",
+ logged_error, ctx->curr.error, ctx->curr.ac,
+ (ctx->curr.error & F_CHARGER_MASK) ?
+ "(err)" : "ok",
+ (ctx->curr.error & F_BATTERY_MASK) ?
+ "(err)" : "ok");
+
+ logged_error = ctx->curr.error;
}
- *memmap_batt_flags = batt_flags;
-
return PWR_STATE_UNCHANGE;
}
-static void charging_progress(void)
+static void charging_progress(struct power_state_context *ctx)
{
- static int state_of_charge;
- int d;
-
- if (battery_state_of_charge(&d))
- return;
-
- if (d != state_of_charge) {
- state_of_charge = d;
- if (get_ac())
- battery_time_to_full(&d);
+ int minutes;
+ if (ctx->curr.batt.state_of_charge !=
+ ctx->prev.batt.state_of_charge) {
+ if (ctx->curr.ac)
+ battery_time_to_full(&minutes);
else
- battery_time_to_empty(&d);
+ battery_time_to_empty(&minutes);
- uart_printf("[Battery %3d%% / %dh:%d]\n", state_of_charge,
- d / 60, d % 60);
+ uart_printf("[Battery %3d%% / %dh:%d]\n",
+ ctx->curr.batt.state_of_charge,
+ minutes / 60, minutes % 60);
}
-
}
/* Battery charging task */
void charge_state_machine_task(void)
{
- timestamp_t prev_ts, ts;
+ struct power_state_context ctx;
+ timestamp_t ts;
int sleep_usec, diff_usec;
- enum power_state current_state, new_state;
- uint8_t *memmap_batt_flags = (uint8_t*)(lpc_get_memmap_range() +
- EC_LPC_MEMMAP_BATT_FLAG);
+ enum power_state new_state;
uint8_t batt_flags;
- prev_ts.val = 0;
- current_state = PWR_STATE_INIT;
+ ctx.prev.state = PWR_STATE_INIT;
+ ctx.curr.state = PWR_STATE_INIT;
+
+ /* Setup LPC direct memmap */
+ ctx.memmap_batt_volt = (uint32_t *)(lpc_get_memmap_range() +
+ EC_LPC_MEMMAP_BATT_VOLT);
+ ctx.memmap_batt_rate = (uint32_t *)(lpc_get_memmap_range() +
+ EC_LPC_MEMMAP_BATT_RATE);
+ ctx.memmap_batt_cap = (uint32_t *)(lpc_get_memmap_range() +
+ EC_LPC_MEMMAP_BATT_CAP);
+ ctx.memmap_batt_flags = (uint8_t *)(lpc_get_memmap_range() +
+ EC_LPC_MEMMAP_BATT_FLAG);
while (1) {
- ts = get_time();
- switch (current_state) {
+ state_common(&ctx);
+
+ switch (ctx.prev.state) {
case PWR_STATE_INIT:
- new_state = state_init();
+ new_state = state_init(&ctx);
break;
case PWR_STATE_IDLE:
- new_state = state_idle();
+ new_state = state_idle(&ctx);
break;
case PWR_STATE_DISCHARGE:
- new_state = state_discharge();
+ new_state = state_discharge(&ctx);
break;
case PWR_STATE_CHARGE:
- new_state = state_charge();
+ new_state = state_charge(&ctx);
break;
case PWR_STATE_ERROR:
- new_state = state_error();
+ new_state = state_error(&ctx);
break;
default:
+ uart_printf("[Undefined charging state %d]\n",
+ ctx.curr.state);
+ ctx.curr.state = PWR_STATE_ERROR;
new_state = PWR_STATE_ERROR;
}
- if (new_state)
+ if (new_state) {
+ ctx.curr.state = new_state;
uart_printf("[Charge state %s -> %s]\n",
- _state_name[current_state],
- _state_name[new_state]);
+ state_name[ctx.prev.state],
+ state_name[new_state]);
+ }
switch (new_state) {
case PWR_STATE_IDLE:
- *memmap_batt_flags = EC_BATT_FLAG_AC_PRESENT |
- EC_BATT_FLAG_BATT_PRESENT;
+ batt_flags = *ctx.memmap_batt_flags;
+ batt_flags &= ~EC_BATT_FLAG_CHARGING;
+ batt_flags &= ~EC_BATT_FLAG_DISCHARGING;
+ *ctx.memmap_batt_flags = batt_flags;
sleep_usec = POLL_PERIOD_LONG;
break;
case PWR_STATE_DISCHARGE:
- batt_flags = *memmap_batt_flags;
- *memmap_batt_flags = EC_BATT_FLAG_AC_PRESENT |
- EC_BATT_FLAG_BATT_PRESENT |
- EC_BATT_FLAG_DISCHARGING |
- (batt_flags & EC_BATT_FLAG_LEVEL_CRITICAL);
+ batt_flags = *ctx.memmap_batt_flags;
+ batt_flags &= ~EC_BATT_FLAG_CHARGING;
+ batt_flags |= EC_BATT_FLAG_DISCHARGING;
+ *ctx.memmap_batt_flags = batt_flags;
sleep_usec = POLL_PERIOD_LONG;
break;
case PWR_STATE_CHARGE:
- *memmap_batt_flags = EC_BATT_FLAG_AC_PRESENT |
- EC_BATT_FLAG_BATT_PRESENT |
- EC_BATT_FLAG_CHARGING;
+ batt_flags = *ctx.memmap_batt_flags;
+ batt_flags |= EC_BATT_FLAG_CHARGING;
+ batt_flags &= ~EC_BATT_FLAG_DISCHARGING;
+ *ctx.memmap_batt_flags = batt_flags;
sleep_usec = POLL_PERIOD_CHARGE;
break;
- case PWR_STATE_ERROR:
- sleep_usec = POLL_PERIOD_SHORT;
- break;
default:
sleep_usec = POLL_PERIOD_SHORT;
}
- diff_usec = (int)(ts.val - prev_ts.val);
+ /* Show charging progress in console */
+ charging_progress(&ctx);
+
+ ts = get_time();
+ diff_usec = (int)(ts.val - ctx.curr.ts.val);
sleep_usec -= diff_usec;
+
if (sleep_usec < MIN_SLEEP_USEC)
sleep_usec = MIN_SLEEP_USEC;
- prev_ts = ts;
usleep(sleep_usec);
-
- if (new_state)
- current_state = new_state;
-
- charging_progress();
}
}
diff --git a/include/battery.h b/include/battery.h
index 2baaf09894..fee0e8b425 100644
--- a/include/battery.h
+++ b/include/battery.h
@@ -8,8 +8,10 @@
#ifndef __CROS_EC_BATTERY_H
#define __CROS_EC_BATTERY_H
-#define BATTERY_WARNING_PERCENTAGE 10
-#define BATTERY_LOW_PERCENTAGE 3
+/* Design capacities, percentage */
+#define BATTERY_LEVEL_WARNING 15
+#define BATTERY_LEVEL_LOW 10
+#define BATTERY_LEVEL_CRITICAL 3
#endif /* __CROS_EC_BATTERY_H */