diff options
author | Randall Spangler <rspangler@chromium.org> | 2017-08-04 14:59:32 -0700 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2017-08-08 17:34:09 -0700 |
commit | 6c55126080f7fa1f165d38924834c894c4a24c27 (patch) | |
tree | 2155c832dc5f1de8e86199038b27d3fb91bfea91 /common/device_state.c | |
parent | 9939855231350875737f9b05e208454451e3bb4a (diff) | |
download | chrome-ec-6c55126080f7fa1f165d38924834c894c4a24c27.tar.gz |
cr50: Clean up device state code
This mostly adds a bunch of comments, but does make a few changes to
the code:
1) The devices console command now prints both the current device
state and the last known state.
2) servo_state_unknown() also checks if we're bit-banging the EC UART,
since that could also cause EC_DETECT to go high.
BUG=none
BRANCH=cr50
TEST=make buildall; use 'devices' command
Change-Id: I73e7524545ef49494eb36155b99f4042c1fd466d
Signed-off-by: Randall Spangler <rspangler@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/602695
Reviewed-by: Mary Ruthven <mruthven@chromium.org>
Diffstat (limited to 'common/device_state.c')
-rw-r--r-- | common/device_state.c | 57 |
1 files changed, 43 insertions, 14 deletions
diff --git a/common/device_state.c b/common/device_state.c index fd9ebcf14b..0ba94d6115 100644 --- a/common/device_state.c +++ b/common/device_state.c @@ -7,23 +7,55 @@ #include "device_state.h" #include "hooks.h" -int device_get_state(enum device_type device) +#define CPRINTS(format, args...) cprints(CC_SYSTEM, format, ## args) + +/** + * Return text description for a state + * + * @param state State + * @return String describing that state + */ +static const char *state_desc(enum device_state state) +{ + return state == DEVICE_STATE_ON ? "on" : + state == DEVICE_STATE_OFF ? "off" : "unknown"; +} + +enum device_state device_get_state(enum device_type device) { return device_states[device].state; } int device_set_state(enum device_type device, enum device_state state) { - device_states[device].state = state; + struct device_config *dc = device_states + device; + + /* + * It'd be handy for debugging if we could print to the console when + * device_set_state() is called. But unfortunately, it'll be called a + * LOT when debouncing UART activity on DETECT_EC or DETECT_AP. So + * only print when the last known state changes below. + */ + + dc->state = state; - if (state != DEVICE_STATE_UNKNOWN && - device_states[device].last_known_state != state) { - device_states[device].last_known_state = state; + if (state != DEVICE_STATE_UNKNOWN && dc->last_known_state != state) { + dc->last_known_state = state; + CPRINTS("DEV %s -> %s", dc->name, state_desc(state)); return 1; } + return 0; } +/** + * Periodic check of device states. + * + * The board does all the work. + * + * Note that device states can change outside of this context as well, for + * example, from a GPIO interrupt handler. + */ static void check_device_state(void) { int i; @@ -33,19 +65,16 @@ static void check_device_state(void) } DECLARE_HOOK(HOOK_SECOND, check_device_state, HOOK_PRIO_DEFAULT); -static void print_state(const char *name, enum device_state state) -{ - ccprintf("%-9s %s\n", name, state == DEVICE_STATE_ON ? "on" : - state == DEVICE_STATE_OFF ? "off" : "unknown"); -} - static int command_devices(int argc, char **argv) { + const struct device_config *dc = device_states; int i; - for (i = 0; i < DEVICE_COUNT; i++) - print_state(device_states[i].name, - device_states[i].state); + ccprintf("Device State LastKnown\n"); + + for (i = 0; i < DEVICE_COUNT; i++, dc++) + ccprintf("%-9s %-7s %s\n", dc->name, state_desc(dc->state), + state_desc(dc->last_known_state)); return EC_SUCCESS; } |