From bd9af2d57b336c658d7e6f87e0abef4e695058b6 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 24 Sep 2013 21:04:56 +0700 Subject: [PATCH] added cli support --- demos/bsp/boards/board.h | 1 + .../boards/embedded_artists/board_ea4357.c | 5 + .../boards/embedded_artists/board_ea4357.h | 4 +- demos/bsp/boards/printf_retarget.c | 11 +- demos/host/host_os_none/host_os_none.uvopt | 116 +++++++------ demos/host/host_os_none/host_os_none.uvproj | 10 ++ demos/host/src/cli.c | 160 ++++++++++++++++++ demos/host/src/cli.h | 62 +++++++ demos/host/src/main.c | 14 +- 9 files changed, 324 insertions(+), 59 deletions(-) create mode 100644 demos/host/src/cli.c create mode 100644 demos/host/src/cli.h diff --git a/demos/bsp/boards/board.h b/demos/bsp/boards/board.h index bf8b6a8e..aaa8074e 100644 --- a/demos/bsp/boards/board.h +++ b/demos/bsp/boards/board.h @@ -125,6 +125,7 @@ void board_init(void); void board_leds(uint32_t on_mask, uint32_t off_mask); uint32_t board_uart_send(uint8_t *buffer, uint32_t length); uint32_t board_uart_recv(uint8_t *buffer, uint32_t length); +uint8_t board_uart_getchar(void); extern volatile uint32_t system_ticks; diff --git a/demos/bsp/boards/embedded_artists/board_ea4357.c b/demos/bsp/boards/embedded_artists/board_ea4357.c index 9e88c5d6..f8a22dab 100644 --- a/demos/bsp/boards/embedded_artists/board_ea4357.c +++ b/demos/bsp/boards/embedded_artists/board_ea4357.c @@ -135,6 +135,11 @@ uint32_t board_uart_recv(uint8_t *buffer, uint32_t length) { return UART_Receive(BOARD_UART_PORT, buffer, length, BLOCKING); } + +uint8_t board_uart_getchar(void) +{ + return UART_ReceiveByte(BOARD_UART_PORT); +} #endif diff --git a/demos/bsp/boards/embedded_artists/board_ea4357.h b/demos/bsp/boards/embedded_artists/board_ea4357.h index 3fbf1ecf..2a6ef849 100644 --- a/demos/bsp/boards/embedded_artists/board_ea4357.h +++ b/demos/bsp/boards/embedded_artists/board_ea4357.h @@ -66,8 +66,8 @@ #include "oem_base_board/pca9532.h" // LEDs -#define CFG_PRINTF_TARGET PRINTF_TARGET_SWO -//#define CFG_PRINTF_TARGET PRINTF_TARGET_UART // FIXME keil's cmsis rtx does not work with UART (work with SWO) +//#define CFG_PRINTF_TARGET PRINTF_TARGET_SWO +#define CFG_PRINTF_TARGET PRINTF_TARGET_UART // FIXME keil's cmsis rtx does not work with UART (work with SWO) /*========================================================================= HARDWARE MAC ADDRESS diff --git a/demos/bsp/boards/printf_retarget.c b/demos/bsp/boards/printf_retarget.c index 7932ab14..12303c89 100644 --- a/demos/bsp/boards/printf_retarget.c +++ b/demos/bsp/boards/printf_retarget.c @@ -103,9 +103,11 @@ int __sys_readc (void) #elif defined __CC_ARM // keil #if CFG_PRINTF_TARGET == PRINTF_TARGET_UART - #define retarget_putc(c) board_uart_send( (uint8_t*) &c, 1); + #define retarget_putc(c) board_uart_send( (uint8_t*) &c, 1); + #define retarget_getchar() board_uart_getchar() #elif CFG_PRINTF_TARGET == PRINTF_TARGET_SWO - #define retarget_putc(c) ITM_SendChar(c) + #define retarget_putc(c) ITM_SendChar(c) + #define retarget_getchar() ITM_ReceiveChar() #else #error Thach, did you forget something #endif @@ -116,6 +118,11 @@ struct __FILE { uint32_t handle; }; +int fgetc(FILE *f) +{ + return retarget_getchar(); +} + int fputc(int ch, FILE *f) { if (//CFG_PRINTF_NEWLINE[0] == '\r' && diff --git a/demos/host/host_os_none/host_os_none.uvopt b/demos/host/host_os_none/host_os_none.uvopt index cc4fd552..1dd23814 100644 --- a/demos/host/host_os_none/host_os_none.uvopt +++ b/demos/host/host_os_none/host_os_none.uvopt @@ -446,8 +446,8 @@ 0 0 0 - 101 - 107 + 1 + 1 0 ..\src\main.c main.c @@ -479,7 +479,7 @@ 0 0 169 - 186 + 180 0 ..\src\keyboard_app.c keyboard_app.c @@ -492,10 +492,10 @@ 1 0 0 - 0 + 44 0 - 123 - 126 + 120 + 127 0 ..\src\mouse_app.c mouse_app.c @@ -524,16 +524,32 @@ 1 0 0 - 0 + 20 0 1 - 1 + 6 0 ..\src\msc_app.c msc_app.c 0 0 + + 1 + 7 + 1 + 0 + 0 + 2 + 0 + 62 + 72 + 0 + ..\src\cli.c + cli.c + 0 + 0 + @@ -544,7 +560,7 @@ 0 2 - 7 + 8 1 0 0 @@ -560,7 +576,7 @@ 2 - 8 + 9 1 0 0 @@ -576,7 +592,7 @@ 2 - 9 + 10 1 0 0 @@ -592,7 +608,7 @@ 2 - 10 + 11 1 0 0 @@ -608,7 +624,7 @@ 2 - 11 + 12 1 0 0 @@ -624,7 +640,7 @@ 2 - 12 + 13 1 0 0 @@ -640,7 +656,7 @@ 2 - 13 + 14 1 0 0 @@ -656,7 +672,7 @@ 2 - 14 + 15 1 0 0 @@ -672,7 +688,7 @@ 2 - 15 + 16 1 0 0 @@ -688,7 +704,7 @@ 2 - 16 + 17 1 0 0 @@ -706,13 +722,13 @@ tinyusb - 1 + 0 0 0 0 3 - 17 + 18 1 0 0 @@ -728,7 +744,7 @@ 3 - 18 + 19 1 0 0 @@ -744,7 +760,7 @@ 3 - 19 + 20 1 0 0 @@ -760,7 +776,7 @@ 3 - 20 + 21 1 0 0 @@ -776,13 +792,13 @@ 3 - 21 + 22 1 0 0 0 0 - 493 + 494 501 0 ..\..\..\tinyusb\host\usbh.c @@ -792,13 +808,13 @@ 3 - 22 + 23 1 0 0 0 0 - 593 + 598 605 0 ..\..\..\tinyusb\host\ehci\ehci.c @@ -808,7 +824,7 @@ 3 - 23 + 24 1 0 0 @@ -824,7 +840,7 @@ 3 - 24 + 25 1 0 0 @@ -840,7 +856,7 @@ 3 - 25 + 26 1 0 0 @@ -856,7 +872,7 @@ 3 - 26 + 27 1 0 0 @@ -872,7 +888,7 @@ 3 - 27 + 28 1 0 0 @@ -888,7 +904,7 @@ 3 - 28 + 29 1 0 0 @@ -904,7 +920,7 @@ 3 - 29 + 30 1 0 0 @@ -920,7 +936,7 @@ 3 - 30 + 31 1 0 0 @@ -936,7 +952,7 @@ 3 - 31 + 32 1 0 0 @@ -952,14 +968,14 @@ 3 - 32 + 33 1 0 0 - 44 + 47 0 - 1 - 8 + 2 + 13 0 ..\..\..\tinyusb\class\msc_host.c msc_host.c @@ -970,13 +986,13 @@ cmsis - 1 + 0 0 0 0 4 - 33 + 34 1 0 0 @@ -992,7 +1008,7 @@ 4 - 34 + 35 1 0 0 @@ -1008,7 +1024,7 @@ 4 - 35 + 36 1 0 0 @@ -1024,7 +1040,7 @@ 4 - 36 + 37 1 0 0 @@ -1040,7 +1056,7 @@ 4 - 37 + 38 1 0 0 @@ -1056,7 +1072,7 @@ 4 - 38 + 39 1 0 0 @@ -1080,14 +1096,14 @@ 0 5 - 39 + 40 2 0 0 - 18 + 0 0 144 - 150 + 158 0 ..\..\bsp\lpc43xx\startup_keil\startup_LPC43xx.s startup_LPC43xx.s diff --git a/demos/host/host_os_none/host_os_none.uvproj b/demos/host/host_os_none/host_os_none.uvproj index 644f41bf..ab31f794 100644 --- a/demos/host/host_os_none/host_os_none.uvproj +++ b/demos/host/host_os_none/host_os_none.uvproj @@ -421,6 +421,11 @@ 1 ..\src\msc_app.c + + cli.c + 1 + ..\src\cli.c + @@ -1025,6 +1030,11 @@ 1 ..\src\msc_app.c + + cli.c + 1 + ..\src\cli.c + diff --git a/demos/host/src/cli.c b/demos/host/src/cli.c new file mode 100644 index 00000000..c8e84c96 --- /dev/null +++ b/demos/host/src/cli.c @@ -0,0 +1,160 @@ +/**************************************************************************/ +/*! + @file cli.c + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2013, hathach (tinyusb.org) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/**************************************************************************/ + +#include "cli.h" +#include "ctype.h" + +#if TUSB_CFG_HOST_MSC + +// command, function, description +#define CLI_COMMAND_TABLE(ENTRY) \ + ENTRY(unknow , cli_cmd_unknow, NULL) \ + ENTRY(help , cli_cmd_help, NULL) \ + +//--------------------------------------------------------------------+ +// Expands the function to have the standard function signature +//--------------------------------------------------------------------+ +#define CLI_PROTOTYPE_EXPAND(command, function, description) \ + tusb_error_t function(char const *);\ + +CLI_COMMAND_TABLE(CLI_PROTOTYPE_EXPAND); + +//--------------------------------------------------------------------+ +// Expand to enum value +//--------------------------------------------------------------------+ +#define CLI_ENUM_EXPAND(command, function, description) CLI_CMDTYPE_##command, +typedef enum { + CLI_COMMAND_TABLE(CLI_ENUM_EXPAND) + CLI_CMDTYPE_COUNT +}cli_cmdtype_t; + +//--------------------------------------------------------------------+ +// Expand to string table +//--------------------------------------------------------------------+ +#define CLI_STRING_EXPAND(command, function, description) #command, +char const* const cli_string_tbl[] = +{ + CLI_COMMAND_TABLE(CLI_STRING_EXPAND) + 0 +}; + +//--------------------------------------------------------------------+ +// Expand to Description table +//--------------------------------------------------------------------+ +#define CLI_DESCRIPTION_EXPAND(command, function, description) description, +char const* const cli_description_tbl[] = +{ + CLI_COMMAND_TABLE(CLI_DESCRIPTION_EXPAND) + 0 +}; + + +//--------------------------------------------------------------------+ +// Expand to Command Lookup Table +//--------------------------------------------------------------------+ +#define CMD_LOOKUP_EXPAND(command, function, description)\ + [CLI_CMDTYPE_##command] = function,\ + +typedef tusb_error_t (* const cli_cmdfunc_t)(char const *); +static cli_cmdfunc_t cli_command_tbl[] = +{ + CLI_COMMAND_TABLE(CMD_LOOKUP_EXPAND) +}; + +//--------------------------------------------------------------------+ +// IMPLEMENTATION +//--------------------------------------------------------------------+ +tusb_error_t cli_cmd_unknow(char const * para) +{ + puts("unknown command, please type \"help\""); + return TUSB_ERROR_NONE; +} + +tusb_error_t cli_cmd_help(char const * para) +{ + puts("current supported commands are:"); + puts("cd\tchange directory"); + puts("ls\tlist directory"); + + return TUSB_ERROR_NONE; +} + + +#define CLI_MAX_BUFFER 50 +static char cli_buffer[CLI_MAX_BUFFER]; + +void cli_init(void) +{ + memclr_(cli_buffer, CLI_MAX_BUFFER); +} + +void cli_poll(char ch) +{ + if ( isprint(ch) ) + { + if (strlen(cli_buffer) < CLI_MAX_BUFFER) + { + cli_buffer[ strlen(cli_buffer) ] = ch; + putchar(ch); + }else + { + puts("cli buffer overflows"); + memclr_(cli_buffer, CLI_MAX_BUFFER); + } + } + else if ( ch == '\r') + { + putchar('\n'); + for(cli_cmdtype_t cmd_id = CLI_CMDTYPE_help; cmd_id < CLI_CMDTYPE_COUNT; cmd_id++) + { + if( 0 == strncmp(cli_buffer, cli_string_tbl[cmd_id], CLI_MAX_BUFFER) ) + { + cli_command_tbl[cmd_id](NULL); + memclr_(cli_buffer, CLI_MAX_BUFFER); + return; + } + } + + cli_cmd_unknow(NULL); + memclr_(cli_buffer, CLI_MAX_BUFFER); + } + else if (ch=='\t') // \t may be used for auto-complete later + { + + } +} + +#endif diff --git a/demos/host/src/cli.h b/demos/host/src/cli.h new file mode 100644 index 00000000..75b06674 --- /dev/null +++ b/demos/host/src/cli.h @@ -0,0 +1,62 @@ +/**************************************************************************/ +/*! + @file cli.h + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2013, hathach (tinyusb.org) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/**************************************************************************/ +#ifndef _TUSB_CLI_H_ +#define _TUSB_CLI_H_ + +#include "boards/board.h" +#include "tusb.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +//--------------------------------------------------------------------+ +// PUBLIC API +//--------------------------------------------------------------------+ +void cli_init(void); +void cli_poll(char ch); + +//--------------------------------------------------------------------+ +// Callback API +//--------------------------------------------------------------------+ + + +#ifdef __cplusplus +} +#endif + +#endif // _TUSB_CLI_H_ diff --git a/demos/host/src/main.c b/demos/host/src/main.c index 14cf0b82..d38dba3f 100644 --- a/demos/host/src/main.c +++ b/demos/host/src/main.c @@ -50,6 +50,8 @@ #include "app_os_prio.h" #endif +#include "cli.h" + #include "mouse_app.h" #include "keyboard_app.h" #include "msc_app.h" @@ -113,11 +115,11 @@ void os_none_start_scheduler(void) cdc_serial_app_task(NULL); rndis_app_task(NULL); -// int ch = ITM_ReceiveChar(); -// if ( ch > 0 ) -// { -// printf("%c", ch); -// } + int ch = getchar(); + if ( ch > 0 ) + { + cli_poll( (char) ch); + } } } #endif @@ -130,6 +132,8 @@ int main(void) tusb_init(); + cli_init(); + //------------- application task init -------------// (void) osal_task_create( OSAL_TASK_REF(led_blinking_task) );