From e29f365dd38c5d04e8c2bb5af37d37dc12350891 Mon Sep 17 00:00:00 2001 From: Namyoon Woo Date: Mon, 12 Aug 2019 13:37:40 -0700 Subject: util: enable chargen for USB console This patch allows chargen to print output to USB instead of UART, which is chosen by command parameter. If USB console is not supported, then the parameter will be ignored, and output shall go to UART port. The patch increases flash usage by 48 bytes if CONFIG_CMD_CHARGEN is defined. BUG=chromium:992607 BRANCH=None TEST=manually ran on fleex. for BOARD in {cr50, fleex} 1. Define CONFIG_CMD_CHARGEN in board/cr50/board.h, and baseboard/octopus/baseboard.h. 2. Build binaries, and program them. 3. Connect CCD to Octopus Fleex. 4. Open terminal to Cr50 and EC consoles, and run chargen (cr50) chargen 1 4 > // no output, because they went to UART. (cr50) chargen 1 4 usb 0000 > (ec) chargen 1 4 0000 > (ec) chargen 1 4 usb // usb parameter gets ignored. 0000 > Change-Id: I5810421fef56548e0bd667488e853e724f699a31 Signed-off-by: Namyoon Woo Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1769386 Reviewed-by: Ruben Rodriguez Buchillon --- common/chargen.c | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) (limited to 'common/chargen.c') diff --git a/common/chargen.c b/common/chargen.c index 64cd138924..eb7e93657f 100644 --- a/common/chargen.c +++ b/common/chargen.c @@ -3,10 +3,12 @@ * found in the LICENSE file. */ +#include "common.h" #include "config.h" #include "console.h" #include "timer.h" #include "uart.h" +#include "usb_console.h" #include "util.h" #include "watchdog.h" @@ -14,9 +16,11 @@ * Microseconds time to drain entire UART_TX console buffer at 115200 b/s, 10 * bits per character. */ -#define BUFFER_DRAIN_TIME_US (1000000UL * 10 * CONFIG_UART_TX_BUF_SIZE / 115200) +#define BUFFER_DRAIN_TIME_US (1000000UL * 10 * CONFIG_UART_TX_BUF_SIZE \ + / CONFIG_UART_BAUD_RATE) + /* - * Generate a stream of characters on the UART console. + * Generate a stream of characters on the UART (and USB) console. * * The stream is an ever incrementing pattern of characters from the following * set: 0..9A..Za..z. @@ -41,7 +45,10 @@ static int command_chargen(int argc, char **argv) uint32_t seq_number = 0; timestamp_t prev_watchdog_time; - while (uart_getc() != -1) + int (*putc_)(int c) = uart_putc; + int (*tx_is_blocked_)(void) = uart_buffer_full; + + while (uart_getc() != -1 || usb_getc() != -1) ; /* Drain received characters, if any. */ if (argc > 1) @@ -50,12 +57,22 @@ static int command_chargen(int argc, char **argv) if (argc > 2) seq_number = atoi(argv[2]); +#if defined(CONFIG_USB_CONSOLE) || defined(CONFIG_USB_CONSOLE_STREAM) + if (argc > 3) { + if (memcmp(argv[3], "usb", 3)) + return EC_ERROR_PARAM3; + + putc_ = usb_putc; + tx_is_blocked_ = usb_console_tx_blocked; + } +#endif + c = '0'; prev_watchdog_time = get_time(); - while (uart_getc() != 'x') { + while (uart_getc() != 'x' && usb_getc() != 'x') { timestamp_t current_time; - while (uart_buffer_full()) { + while (tx_is_blocked_()) { /* * Let's sleep enough time to drain half of TX * buffer. @@ -72,7 +89,7 @@ static int command_chargen(int argc, char **argv) prev_watchdog_time.val = current_time.val; } - uart_putc(c++); + putc_(c++); if (seq_number && (++seq_counter == seq_number)) break; @@ -91,11 +108,15 @@ static int command_chargen(int argc, char **argv) c = 'A'; } - uart_putc('\n'); - return 0; + putc_('\n'); + return EC_SUCCESS; } DECLARE_SAFE_CONSOLE_COMMAND(chargen, command_chargen, +#if defined(CONFIG_USB_CONSOLE) || defined(CONFIG_USB_CONSOLE_STREAM) + "[seq_length [num_chars [usb]]]", +#else "[seq_length [num_chars]]", +#endif "Generate a constant stream of characters on the " "UART console,\nrepeating every 'seq_length' " "characters, up to 'num_chars' total." -- cgit v1.2.1