From 2b3bef79683061e89d3e3c2823c87f592e2f2e23 Mon Sep 17 00:00:00 2001 From: Randall Spangler Date: Mon, 17 Oct 2011 14:27:33 -0700 Subject: Revised header files and moved in current implementation. BUG=None TEST=cd cros_ec && make Change-Id: Ieac6a131f0aef0f8f3343d4f3cd9fba5a211bc09 --- cros_ec/Makefile | 18 +++++ cros_ec/chip_stub/ec_uart.c | 41 ++++++++++++ cros_ec/host_command.c | 0 cros_ec/include/ec_common.h | 34 ++++++++++ cros_ec/include/ec_console.h | 37 +++++++++++ cros_ec/include/ec_keyboard.h | 20 ++++++ cros_ec/include/ec_uart.h | 102 ++++++++++++++++++++++++++++ cros_ec/keyboard.h | 17 ----- cros_ec/lib/ec_console.c | 150 ++++++++++++++++++++++++++++++++++++++++++ cros_ec/lib/ec_host_command.c | 0 cros_ec/test/fakemain.c | 16 +++++ ec_common.h | 29 -------- ec_console.h | 34 ---------- ec_uart.h | 102 ---------------------------- 14 files changed, 418 insertions(+), 182 deletions(-) create mode 100644 cros_ec/Makefile create mode 100644 cros_ec/chip_stub/ec_uart.c delete mode 100644 cros_ec/host_command.c create mode 100644 cros_ec/include/ec_common.h create mode 100644 cros_ec/include/ec_console.h create mode 100644 cros_ec/include/ec_keyboard.h create mode 100644 cros_ec/include/ec_uart.h delete mode 100644 cros_ec/keyboard.h create mode 100644 cros_ec/lib/ec_console.c create mode 100644 cros_ec/lib/ec_host_command.c create mode 100644 cros_ec/test/fakemain.c delete mode 100644 ec_common.h delete mode 100644 ec_console.h delete mode 100644 ec_uart.h diff --git a/cros_ec/Makefile b/cros_ec/Makefile new file mode 100644 index 0000000000..ff59ee3b50 --- /dev/null +++ b/cros_ec/Makefile @@ -0,0 +1,18 @@ +# Copyright (c) 2011 The Chromium OS Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +LIB_SRCS=\ + lib/ec_console.c + +STUB_SRCS=\ + chip_stub/ec_uart.c + +all: fakemain + +clean: + rm -f fakemain + +fakemain: test/fakemain.c $(LIB_SRCS) $(STUB_SRCS) + gcc -Wall -I include -o fakemain test/fakemain.c \ + $(LIB_SRCS) $(STUB_SRCS) diff --git a/cros_ec/chip_stub/ec_uart.c b/cros_ec/chip_stub/ec_uart.c new file mode 100644 index 0000000000..f76691169a --- /dev/null +++ b/cros_ec/chip_stub/ec_uart.c @@ -0,0 +1,41 @@ +/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/* UART module for Chrome EC, empty implementation */ + +#include "ec_uart.h" + + +EcError EcUartPrintf(const char* format, ...) { + return EC_ERROR_UNIMPLEMENTED; +} + + +EcError EcUartPuts(const char* outstr) { + return EC_ERROR_UNIMPLEMENTED; +} + + +void EcUartFlush(void) { +} + + +void EcUartFlushInput(void) { +} + + +int EcUartPeek(int c) { + return -1; +} + + +int EcUartGets(char* dest, int size) { + *dest = 0; + return 0; +} + + +void EcUartRegisterHasInputCallback(UartHasInputCallback callback, int c) { +} diff --git a/cros_ec/host_command.c b/cros_ec/host_command.c deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/cros_ec/include/ec_common.h b/cros_ec/include/ec_common.h new file mode 100644 index 0000000000..615a88adcf --- /dev/null +++ b/cros_ec/include/ec_common.h @@ -0,0 +1,34 @@ +/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/* ec_common.h - Common includes for Chrome EC */ + +#ifndef __CROS_EC_COMMON_H +#define __CROS_EC_COMMON_H + +#include + +/* Functions which return error return one of these. This is an + * integer instead of an enum to support module-internal error + * codes. */ +typedef int EcError; + +/* List of common EcError codes that can be returned */ +enum EcErrorList { + /* Success - no error */ + EC_SUCCESS = 0, + /* Unknown error */ + EC_ERROR_UNKNOWN = 1, + /* Function not implemented yet */ + EC_ERROR_UNIMPLEMENTED = 2, + /* Overflow error; too much input provided. */ + EC_ERROR_OVERFLOW = 3, + + /* Module-internal error codes may use this range. */ + EC_ERROR_INTERNAL_FIRST = 0x10000, + EC_ERROR_INTERNAL_LAST = 0x1FFFF +}; + +#endif /* __CROS_EC_COMMON_H */ diff --git a/cros_ec/include/ec_console.h b/cros_ec/include/ec_console.h new file mode 100644 index 0000000000..827be9fc86 --- /dev/null +++ b/cros_ec/include/ec_console.h @@ -0,0 +1,37 @@ +/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/* ec_console.h - Debug console for Chrome EC */ + +#ifndef __CROS_EC_CONSOLE_H +#define __CROS_EC_CONSOLE_H + +#include "ec_common.h" + +/* Console command */ +typedef struct EcConsoleCommand { + /* Command name. Case-insensitive. */ + const char* name; + /* Handler for the command. argv[0] will be the command name. */ + EcError (*handler)(int argc, char** argv); +} EcConsoleCommand; + + +/* Console command group */ +typedef struct EcConsoleCommandGroup { + const char* group_name; /* Name of the command group */ + const EcConsoleCommand* commands; /* List of commands */ + int command_count; /* Number of commands in list */ +} EcConsoleCommandGroup; + + +/* Initializes the console module. */ +EcError EcConsoleInit(void); + + +/* Registers a group of console commands. */ +EcError EcConsoleRegisterCommands(const EcConsoleCommandGroup* group); + +#endif /* __CROS_EC_CONSOLE_H */ diff --git a/cros_ec/include/ec_keyboard.h b/cros_ec/include/ec_keyboard.h new file mode 100644 index 0000000000..c1c5d5e250 --- /dev/null +++ b/cros_ec/include/ec_keyboard.h @@ -0,0 +1,20 @@ +/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/* Keyboard emulation */ + +#ifndef __CROS_EC_KEYBOARD_H +#define __CROS_EC_KEYBOARD_H + +/* Register the board-specific keyboard matrix translation function. + * The callback function accepts col/row and returns the scan code. + */ +EcError CrKeyboardMatrixRegister( + int8_t col_num, int8_t row_num, + EcError (*callback)( + int8_t column, int8_t row, int8_t pressed, + uint8_t *scan_code, int32_t* len)); + +#endif /* __CROS_EC_KEYBOARD_H */ diff --git a/cros_ec/include/ec_uart.h b/cros_ec/include/ec_uart.h new file mode 100644 index 0000000000..8f49d43cce --- /dev/null +++ b/cros_ec/include/ec_uart.h @@ -0,0 +1,102 @@ +/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/* ec_uart.h - UART module for Chrome EC */ + +#ifndef __CROS_EC_UART_H +#define __CROS_EC_UART_H + +#include "ec_common.h" + + +/* Initializes the UART module. */ +EcError EcUartInit(void); + +/*****************************************************************************/ +/* Output functions + * + * Output is buffered. If the buffer overflows, subsequent output is + * discarded. */ + +/* Print formatted output to the UART, like printf(). + * + * Returns error if output was truncated. + * + * Must support format strings for: + * char (%c) + * string (%s) + * native int (signed/unsigned) (%d / %u / %x) + * int32_t / uint32_t (%d / %x) + * int64_t / uint64_t (%ld / %lu / %lx) + * pointer (%p) + * including padding (%-5s, %8d, %08x) + * + * Note: Floating point output (%f / %g) is not required. + */ +EcError EcUartPrintf(const char* format, ...); + +/* Put a null-terminated string to the UART, like puts(). + * + * Returns error if output was truncated. */ +EcError EcUartPuts(const char* outstr); + +/* Flushes output. Blocks until UART has transmitted all output. */ +void EcUartFlushOutput(void); + +/*****************************************************************************/ +/* Input functions + * + * Input is buffered. If the buffer overflows, the oldest input in + * the buffer is discarded to make room for the new input. + * + * Input lines may be terminated by CR ('\r'), LF ('\n'), or CRLF; all + * are translated to newline. */ + +/* Flushes input buffer, discarding all input. */ +void EcUartFlushInput(void); + +/* Non-destructively checks for a character in the input buffer. + * + * Returns the offset into the input buffer of character , or -1 if + * it is not in the input buffer. */ +int EcUartPeek(int c); + +/* Reads characters from the UART, similar to fgets(). + * + * Reads input until one of the following conditions is met: + * (1) characters have been read. + * (2) A newline ('\n') has been read. + * (3) The input buffer is empty. + * + * Condition (3) means this call never blocks. This is important + * because it prevents a race condition where the caller calls + * UartPeek() to see if input is waiting, or is notified by the + * callack that input is waiting, but then the input buffer overflows + * or someone else grabs the input before UartGets() is called. + * + * Characters are stored in and are null-terminated. + * Characters include the newline if present, so that the caller can + * distinguish between a complete line and a truncated one. If the + * input buffer is empty, a null-terminated empty string ("") is + * returned. + * + * Returns the number of characters read (not counting the terminating + * null). */ +int EcUartGets(char* dest, int size); + +/* Callback handler prototype, called when the UART has input. */ +typedef void (*UartHasInputCallback)(void); + +/* Registers an input callback handler, replacing any existing + * callback handler. If callback==NULL, disables callbacks. + * + * Callback will be called whenever the UART receives character . + * If c<0, callback will be called when the UART receives any + * character. */ +void EcUartRegisterHasInputCallback(UartHasInputCallback callback, int c); + +/* TODO: getc(), putc() equivalents? */ + +#endif /* __CROS_EC_UART_H */ diff --git a/cros_ec/keyboard.h b/cros_ec/keyboard.h deleted file mode 100644 index ad9ef6c220..0000000000 --- a/cros_ec/keyboard.h +++ /dev/null @@ -1,17 +0,0 @@ -/* keyboard.h - - * - * (Chromium license) */ - -#ifndef __CROS_EC_KEYBOARD_H -#define __CROS_EC_KEYBOARD_H - -/* Register the board-specific keyboard matrix translation function. - * The callback function accepts col/row and returns the scan code. - */ -EcError CrKeyboardMatrixRegister( - int8_t col_num, int8_t row_num, - EcError (*callback)( - int8_t column, int8_t row, int8_t pressed, - uint8_t *scan_code, int32_t* len)); - -#endif /* __CROS_EC_KEYBOARD_H */ diff --git a/cros_ec/lib/ec_console.c b/cros_ec/lib/ec_console.c new file mode 100644 index 0000000000..0196f84a92 --- /dev/null +++ b/cros_ec/lib/ec_console.c @@ -0,0 +1,150 @@ +/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/* Console module for Chrome EC */ + +#include +#include + +#include "ec_console.h" +#include "ec_uart.h" + +#define MAX_COMMAND_GROUPS 20 +#define MAX_ARGS_PER_COMMAND 10 + +/* Forward declarations for console commands */ +static EcError CommandHelp(int argc, char** argv); + +static const EcConsoleCommandGroup *group_list[MAX_COMMAND_GROUPS]; +static int group_count = 0; + +static const EcConsoleCommand console_commands[] = { + {"help", CommandHelp}, + {"?", CommandHelp}, +}; +static const EcConsoleCommandGroup console_group = { + "Console", console_commands, + sizeof(console_commands) / sizeof(EcConsoleCommand) +}; + + +static void HasInputHandler(void) { + /* TODO: if we had threads, this would wake the processor thread */ +} + + +EcError EcConsoleInit(void) { + /* Register the input handler to handle newlines */ + EcUartRegisterHasInputCallback(HasInputHandler, '\n'); + + /* Register our internal commands */ + return EcConsoleRegisterCommands(&console_group); +} + + +/* Command handler - prints help. */ +static EcError CommandHelp(int argc, char** argv) { + const EcConsoleCommand *cmd; + int c, g; + + EcUartPuts("Known commands:\n"); + + for (g = 0; g < group_count; g++) { + cmd = group_list[g]->commands; + EcUartPrintf("Group %s:\n", group_list[g]->group_name); + for (c = group_list[g]->command_count; c > 0; c--, cmd++) + EcUartPrintf(" %s\n", cmd->name); + } + + return EC_SUCCESS; +} + + +EcError EcConsoleRegisterCommands(const EcConsoleCommandGroup* group) { + if (group_count >= MAX_COMMAND_GROUPS) + return EC_ERROR_OVERFLOW; /* No space for a new command group */ + + group_list[group_count++] = group; + return EC_SUCCESS; +} + + +/* Splits a line of input into words. Stores the count of words in + * . Stores pointers to the words in , which must be at + * least long. If more than words are found, + * discards the excess and returns EC_ERROR_OVERFLOW. */ +EcError SplitWords(char* input, int max_argc, int* argc, char** argv) { + char* c; + int in_word = 0; + + /* Parse input into words */ + for (c = input; c; c++) { + if (isspace(*c)) { + if (in_word) { + /* Ending a word */ + *c = '\0'; + ++*argc; + in_word = 0; + } + } else { + if (!in_word) { + /* Starting a new word */ + if (*argc >= max_argc) + return EC_ERROR_OVERFLOW; /* More words than we can handle */ + + argv[*argc] = c; + in_word = 1; + } + } + } + + return EC_SUCCESS; +} + + +/* Finds a command by name. Returns the command structure, or NULL if + * no match found. */ +const EcConsoleCommand* FindCommand(char* name) { + const EcConsoleCommand *cmd; + int c, g; + + /* Find the command in the command groups */ + for (g = 0; g < group_count; g++) { + cmd = group_list[g]->commands; + for (c = group_list[g]->command_count; c > 0; c--, cmd++) { + if (!strcasecmp(name, cmd->name)) + return cmd; + } + } + + return NULL; +} + + +/* Handles a line of input containing a single command. + * + * Modifies the input string during parsing. */ +EcError ConsoleHandleCommand(char* input) { + char* argv[MAX_ARGS_PER_COMMAND]; + const EcConsoleCommand *cmd; + int argc; + + /* Split input into words. Ignore words past our limit. */ + SplitWords(input, MAX_ARGS_PER_COMMAND, &argc, argv); + + /* If no command, nothing to do */ + if (!argc) + return EC_SUCCESS; + + cmd = FindCommand(argv[0]); + if (cmd) + return cmd->handler(argc, argv); + + EcUartPrintf("Command '%s' not found.\n", argv[0]); + return EC_ERROR_UNKNOWN; +} + + +/* TODO: task function */ diff --git a/cros_ec/lib/ec_host_command.c b/cros_ec/lib/ec_host_command.c new file mode 100644 index 0000000000..e69de29bb2 diff --git a/cros_ec/test/fakemain.c b/cros_ec/test/fakemain.c new file mode 100644 index 0000000000..40316d08eb --- /dev/null +++ b/cros_ec/test/fakemain.c @@ -0,0 +1,16 @@ +/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/* Fake main routine, to make sure lib functions link */ + +#include "ec_console.h" +#include "ec_uart.h" + +int main(void) { + + + + return 0; +} diff --git a/ec_common.h b/ec_common.h deleted file mode 100644 index a7ce19b21c..0000000000 --- a/ec_common.h +++ /dev/null @@ -1,29 +0,0 @@ -/* ec_common.h - Common includes for Chrome EC - * - * (Chromium license) */ - -#ifndef __EC_COMMON_H -#define __EC_COMMON_H - -#include - -typedef int EcError; /* Functions which return error return one of these */ -/* YJLOU: why not "typedef enum EcErrorListe EcError"? */ - -/* List of common EcError codes that can be returned */ -enum EcErrorList { - /* Success - no error */ - EC_SUCCESS = 0, - /* Unknown error */ - EC_ERROR_UNKNOWN = 1, - /* Function not implemented yet */ - EC_ERROR_UNIMPLEMENTED = 2, - /* Overflow error; too much input provided. */ - EC_ERROR_OVERFLOW = 3, - - /* Module-internal error codes may use this range. */ - EC_ERROR_INTERNAL_FIRST = 0x10000, - EC_ERROR_INTERNAL_LAST = 0x1FFFF -}; - -#endif /* __EC_COMMON_H */ diff --git a/ec_console.h b/ec_console.h deleted file mode 100644 index 64e38eda09..0000000000 --- a/ec_console.h +++ /dev/null @@ -1,34 +0,0 @@ -/* ec_console.h - Debug console for Chrome EC - * - * (Chromium license) */ - -#ifndef __CROS_EC_CONSOLE_H -#define __CROS_EC_CONSOLE_H - -#include "ec_common.h" - -/* Console command */ -typedef struct EcConsoleCommand { - /* Command name. Case-insensitive. */ - const char* name; - /* Handler for the command. argv[0] will be the command name. */ - EcError (*handler)(int argc, char** argv); -} EcConsoleCommand; - - -/* Console command group */ -typedef struct EcConsoleCommandGroup { - const char* group_name; /* Name of the command group */ - const EcConsoleCommand* commands; /* List of commands */ - int command_count; /* Number of commands in list */ -} EcConsoleCommandGroup; - - -/* Initializes the console module. */ -EcError ConsoleInit(void); - - -/* Registers a group of console commands. */ -EcError ConsoleRegisterCommands(const EcConsoleCommandGroup* group); - -#endif /* __CROS_EC_CONSOLE_H */ diff --git a/ec_uart.h b/ec_uart.h deleted file mode 100644 index 78d1b18764..0000000000 --- a/ec_uart.h +++ /dev/null @@ -1,102 +0,0 @@ -/* ec_uart.h - UART module for Chrome EC - * - * (Chromium license) */ - -#ifndef __CROS_EC_UART_H -#define __CROS_EC_UART_H - -#include "ec_common.h" - -/*****************************************************************************/ -/* Output functions */ - -/* Print formatted output to the UART, like printf(). - * - * Returns error if output was truncated. - * - * Must support format strings for: - * char (%c) - * string (%s) - * native int (signed/unsigned) (%d / %u / %x) - * int32_t / uint32_t (%d / %x) - * int64_t / uint64_t (%ld / %lu / %lx) - * pointer (%p) - * including padding (%-5s, %8d, %08x) - * - * Note: Floating point output (%f / %g) is not required. - */ -EcError UartPrintf(const char* format, ...); - -/* Put a null-terminated string to the UART, like puts(). - * - * Returns error if output was truncated. */ -EcError UartPuts(const char* outstr); - -/* Flushes output. Blocks until UART has transmitted all output. */ -void UartFlush(void); - -/*****************************************************************************/ -/* Input functions */ - -/* Flushes input buffer, discarding all input. */ -void UartFlushInput(void); - -/* Non-destructively checks for a character in the input buffer. - * - * Returns non-zero if the character is in the input buffer. If - * c<0, returns non-zero if any character is in the input buffer. */ -/* TODO: add boolean type? */ -/* TODO: return offset of the character? That is, how many bytes of - * input up to and including the character. */ -int UartPeek(int c); - -/* Reads characters from the UART, similar to fgets(). - * - * Reads input until one of the following conditions is met: - * (1) characters have been read. - * (2) A newline ('\n') has been read. - * (3) The input buffer is empty. - * - * Condition (3) means this call never blocks. This is important - * because it prevents a race condition where the caller calls - * UartPeek() to see if input is waiting, or is notified by the - * callack that input is waiting, but then the input buffer overflows - * or someone else grabs the input before UartGets() is called. - * - * Characters are stored in and are null-terminated, and - * include the newline if present. - * - * Returns the number of characters read (not counting the terminating - * null). */ -int UartGets(char* dest, int size); - -/* Callback handler prototype, called when the UART has input. */ -typedef void (*UartHasInputCallback)(void); - -/* Registers an input callback handler, replacing any existing - * callback handler. If callback==NULL, disables callbacks. - * - * Callback will be called whenever the UART receives character . - * If c<0, callback will be called when the UART receives any - * character. */ -void UartRegisterHasInputCallback(UartHasInputCallback callback, int c); - -/* TODO: what to do about input overflow? That is, what if the input - * buffer fills with 'A', and we're still waiting for a newline? Do - * we drop the oldest output, flush the whole thing, or call the input - * callback with a message code? Alternately, should the callback be able - * to specify a buffer-full-threshold where it gets called? - * - * Simplest just to flush the input buffer on overflow. */ -/* YJLOU: are you talking about the UartRegisterHasInputCallback()? - * If yes, this is not a problem because for every input key, we can drop it - * if there is no callback registered for it. - * Else if you are talking about the UartGets() function, I think you can add - * the third return critiria: internal key buffer reaches full. - */ - -/* TODO: getc(), putc() equivalents? */ - -/* TODO: input handler assumes debug input new lines are '\n'-delimited? */ - -#endif /* __CROS_EC_UART_H */ -- cgit v1.2.1