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,12 +15,37 @@ 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 */ return (state == DEVICE_STATE_DEBOUNCING || state == DEVICE_STATE_ON); } +/** + * 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. * @@ -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"); diff --git a/chip/g/rdd.c b/chip/g/rdd.c index 24388c02b6..5e5ed42f6d 100644 --- a/chip/g/rdd.c +++ b/chip/g/rdd.c @@ -49,6 +49,12 @@ static int rdd_is_detected(void) return (cc1 == cc2 && (cc1 == 3 || cc1 == 1)); } +void print_rdd_state(void) +{ + ccprintf("RDD: %s\n", + force_detected ? "forced enable" : device_state_name(state)); +} + /** * Handle debug accessory disconnecting */ @@ -234,8 +240,3 @@ void force_rdd_detect(int enable) if (force_detected) hook_call_deferred(&rdd_connect_data, 0); } - -int rdd_detect_is_forced(void) -{ - return force_detected; -} diff --git a/chip/g/rdd.h b/chip/g/rdd.h index 0da5cbef0e..4e25fe4293 100644 --- a/chip/g/rdd.h +++ b/chip/g/rdd.h @@ -22,10 +22,8 @@ void init_rdd_state(void); void force_rdd_detect(int enable); /** - * Check if debug accessory detection is forced. - * - * @return 1 if keepalive is enabled, 0 if disabled. + * Print debug accessory detect state */ -int rdd_detect_is_forced(void); +void print_rdd_state(void); #endif /* __CROS_RDD_H */