diff options
author | Randall Spangler <rspangler@chromium.org> | 2017-08-28 15:54:14 -0700 |
---|---|---|
committer | ChromeOS Commit Bot <chromeos-commit-bot@chromium.org> | 2017-09-07 22:34:24 +0000 |
commit | cdbe57cc7a04bb32c670dcf5c61bd2439883c686 (patch) | |
tree | 5e4e193d8beacf6bc2c3cf8ff9802921da5ce596 /board | |
parent | 4305103806451ba2dfa79ad08fd285106974174b (diff) | |
download | chrome-ec-cdbe57cc7a04bb32c670dcf5c61bd2439883c686.tar.gz |
cr50: Let state machines print their own states
Add a function to translate device_state enum into a string, then use
it for printing the ec and RDD state.
Refactor ec_state so that all state transitions go through a
set_state() function, which makes it easier to turn on debugging all
state transitions. That's normally not compiled in because it would
be spammy during debouncing.
BUG=none
BRANCH=cr50
TEST=ccd command prints EC and RDD states
Change-Id: Ie7bc56c7b66beee23d1d1989711c640e5e39ce43
Signed-off-by: Randall Spangler <rspangler@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/642121
Reviewed-by: Mary Ruthven <mruthven@chromium.org>
(cherry picked from commit b0891c9450516f94ae44cc17f0cd5cf0436b77da)
Reviewed-on: https://chromium-review.googlesource.com/656393
Commit-Queue: Vadim Bendebury <vbendeb@chromium.org>
Tested-by: Vadim Bendebury <vbendeb@chromium.org>
Reviewed-by: Vadim Bendebury <vbendeb@chromium.org>
Diffstat (limited to 'board')
-rw-r--r-- | board/cr50/board.c | 22 | ||||
-rw-r--r-- | board/cr50/board.h | 13 | ||||
-rw-r--r-- | board/cr50/ec_state.c | 37 | ||||
-rw-r--r-- | board/cr50/rdd.c | 7 |
4 files changed, 70 insertions, 9 deletions
diff --git a/board/cr50/board.c b/board/cr50/board.c index 96a6f41810..2453265fb2 100644 --- a/board/cr50/board.c +++ b/board/cr50/board.c @@ -115,6 +115,28 @@ void ec_tx_cr50_rx(enum gpio_signal signal) hook_call_deferred(&ec_uart_deferred__data, 0); } +const char *device_state_names[] = { + "init", + "init_debouncing", + "init_rx_only", + "disconnected", + "off", + "undetectable", + "connected", + "on", + "debouncing", + "unknown" +}; +BUILD_ASSERT(ARRAY_SIZE(device_state_names) == DEVICE_STATE_COUNT); + +const char *device_state_name(enum device_state state) +{ + if (state >= 0 && state < DEVICE_STATE_COUNT) + return device_state_names[state]; + else + return "?"; +} + int board_use_plt_rst(void) { return !!(board_properties & BOARD_USE_PLT_RESET); diff --git a/board/cr50/board.h b/board/cr50/board.h index 5c61eadb4a..0f59e48d56 100644 --- a/board/cr50/board.h +++ b/board/cr50/board.h @@ -220,8 +220,19 @@ enum device_state { /* Device state is unknown. Used only by legacy device_state code. */ DEVICE_STATE_UNKNOWN, + + /* Number of device states */ + DEVICE_STATE_COUNT }; +/** + * Return the name of the device state as as string. + * + * @param state State to look up + * @return Name of the state, or "?" if no match. + */ +const char *device_state_name(enum device_state state); + /* NVMem variables. */ enum nvmem_vars { NVMEM_VAR_CONSOLE_LOCKED = 0, @@ -270,6 +281,8 @@ int board_is_first_factory_boot(void); void enable_ccd_uart(int uart); void disable_ccd_uart(int uart); +void print_ec_state(void); + int ap_is_on(void); int ec_is_on(void); int rdd_is_connected(void); diff --git a/board/cr50/ec_state.c b/board/cr50/ec_state.c index 5958ea3b79..fb8087ab54 100644 --- a/board/cr50/ec_state.c +++ b/board/cr50/ec_state.c @@ -15,6 +15,11 @@ static enum device_state state = DEVICE_STATE_INIT; +void print_ec_state(void) +{ + ccprintf("EC: %s\n", device_state_name(state)); +} + int ec_is_on(void) { /* Debouncing and on are both still on */ @@ -22,6 +27,26 @@ int ec_is_on(void) } /** + * Set the EC state. + * + * Done as a function to make it easier to debug state transitions. Note that + * this ONLY sets the state (and possibly prints debug info), and doesn't do + * all the additional transition work that set_ec_on(), etc. do. + * + * @param new_state State to set. + */ +static void set_state(enum device_state new_state) +{ +#ifdef CR50_DEBUG_EC_STATE + /* Print all state transitions. May spam the console. */ + if (state != new_state) + CPRINTS("EC %s -> %s", + device_state_name(state), device_state_name(new_state)); +#endif + state = new_state; +} + +/** * Move the EC to the ON state. * * This can be deferred from the interrupt handler, or called from the state @@ -41,13 +66,13 @@ static void set_ec_on(void) CPRINTS("EC RX only"); if (!uart_bitbang_is_enabled(UART_EC)) uartn_enable(UART_EC); - state = DEVICE_STATE_INIT_RX_ONLY; + set_state(DEVICE_STATE_INIT_RX_ONLY); return; } /* If we were debouncing ON->OFF, cancel it because we're still on */ if (state == DEVICE_STATE_DEBOUNCING) - state = DEVICE_STATE_ON; + set_state(DEVICE_STATE_ON); /* If we're already on, done */ if (state == DEVICE_STATE_ON) @@ -55,7 +80,7 @@ static void set_ec_on(void) /* We were previously off */ CPRINTS("EC on"); - state = DEVICE_STATE_ON; + set_state(DEVICE_STATE_ON); /* Enable UART RX if we're not bit-banging */ if (!uart_bitbang_is_enabled(UART_EC)) @@ -94,7 +119,7 @@ static void ec_detect(void) if (state == DEVICE_STATE_DEBOUNCING || state == DEVICE_STATE_INIT_DEBOUNCING) { CPRINTS("EC off"); - state = DEVICE_STATE_OFF; + set_state(DEVICE_STATE_OFF); disable_ccd_uart(UART_EC); return; } @@ -104,9 +129,9 @@ static void ec_detect(void) * is actually off or just sending a 0-bit. So start debouncing. */ if (state == DEVICE_STATE_INIT) - state = DEVICE_STATE_INIT_DEBOUNCING; + set_state(DEVICE_STATE_INIT_DEBOUNCING); else - state = DEVICE_STATE_DEBOUNCING; + set_state(DEVICE_STATE_DEBOUNCING); gpio_enable_interrupt(GPIO_DETECT_EC); } DECLARE_HOOK(HOOK_SECOND, ec_detect, HOOK_PRIO_DEFAULT); diff --git a/board/cr50/rdd.c b/board/cr50/rdd.c index bfc58a136c..ca2da0fd75 100644 --- a/board/cr50/rdd.c +++ b/board/cr50/rdd.c @@ -189,9 +189,10 @@ static int command_ccd(int argc, char **argv) return EC_ERROR_PARAM1; } - ccprintf("CCD: %s\n", - rdd_detect_is_forced() ? "forced enable" : - rdd_is_connected() ? "enabled" : "disabled"); + print_ec_state(); + print_rdd_state(); + + ccprintf("CCD: %s\n", rdd_is_connected() ? "enabled" : "disabled"); ccprintf("AP UART: %s\n", uartn_is_enabled(UART_AP) ? uart_tx_is_connected(UART_AP) ? "RX+TX" : "RX" : "disabled"); |