From 304ab6d368e046801f9c6bab326bbb27f78cb875 Mon Sep 17 00:00:00 2001 From: marco Date: Mon, 12 Jun 2006 21:37:02 +0000 Subject: [PATCH] implemented some commands (ver and sleep) git-svn-id: https://src.develer.com/svnoss/bertos/trunk@662 38d2e660-2303-0410-9eaa-f027e97ec537 --- app/triface/appconfig.h | 5 +- app/triface/doc/PROTOCOL | 2 +- app/triface/protocol.c | 122 ++++++++++++++++++++++++++++++++++----- app/triface/triface.c | 4 ++ app/triface/triface.mk | 6 +- config.mk | 13 +++-- drv/timer_avr.c | 5 +- mware/parser.h | 12 ++-- mware/pgm.h | 9 ++- 9 files changed, 150 insertions(+), 28 deletions(-) diff --git a/app/triface/appconfig.h b/app/triface/appconfig.h index 31ee1e85..c7ceca08 100755 --- a/app/triface/appconfig.h +++ b/app/triface/appconfig.h @@ -46,6 +46,9 @@ /*#* *#* $Log$ + *#* Revision 1.3 2006/06/12 21:37:01 marco + *#* implemented some commands (ver and sleep) + *#* *#* Revision 1.2 2006/06/01 12:29:21 marco *#* Add first simple protocol command (version request). *#* @@ -133,7 +136,7 @@ /*\}*/ /// Hardware timer selection for drv/timer.c -#define CONFIG_TIMER TIMER_ON_OUTPUT_COMPARE2 +#define CONFIG_TIMER TIMER_ON_OUTPUT_COMPARE0 /// Debug timer interrupt using a strobe pin. #define CONFIG_TIMER_STROBE 0 diff --git a/app/triface/doc/PROTOCOL b/app/triface/doc/PROTOCOL index d4dbee74..defc38d5 100755 --- a/app/triface/doc/PROTOCOL +++ b/app/triface/doc/PROTOCOL @@ -4,7 +4,7 @@ Command format: ... Where: - command - alphanurmeric command name + command - alphanumeric command name argN - numeric argument (unsigned base 10, 0-65535) Positive response format: diff --git a/app/triface/protocol.c b/app/triface/protocol.c index 1b66c990..c833837d 100755 --- a/app/triface/protocol.c +++ b/app/triface/protocol.c @@ -16,6 +16,9 @@ /*#* *#* $Log$ + *#* Revision 1.2 2006/06/12 21:37:02 marco + *#* implemented some commands (ver and sleep) + *#* *#* Revision 1.1 2006/06/01 12:29:21 marco *#* Add first simple protocol command (version request). *#* @@ -24,22 +27,30 @@ #include "protocol.h" #include +#include #include #include #include #include #include +#include /* malloc() + TODO: substitute with a more appropriate + memory allocator. */ + #include +#include + // DEBUG: set to 1 to force interactive mode #define FORCE_INTERACTIVE 1 /** * True if we are in interactive mode, false if we are in protocol mode. - * In interactive mode, commands are read through readline() (prompt, completion, - * history) without IDs, and replies/errors are sent to the serial output. + * In interactive mode, commands are read through readline() (prompt, + * completion, history) without IDs, and replies/errors are sent to the serial + * output. * In protocol mode, we implement the default protocol */ static bool interactive; @@ -62,32 +73,79 @@ INLINE void NAK(Serial *ser, const char *err) #endif } +/* + * Print args on s, with format specified in t->result_fmt. + * Return number of valid arguments or -1 in case of error. + */ +static int protocol_reply(Serial *s, const struct CmdTemplate *t, + const parms *args) +{ + int nres = strlen(t->result_fmt); + + if (nres > 0) + { + for (int i = strlen(t->arg_fmt); i < nres; ++i) + { + if (t->result_fmt[i] == 'd') + { + ser_printf(s, "%ld", args[i].l); + } + else if (t->result_fmt[i] == 's') + { + ser_printf(s, "%s ", args[i].s); + } + else + { + return -1; + } + } + } + ser_print(s, "\r\n"); + return nres; +} + static void protocol_parse(Serial *ser, const char *buf) { const struct CmdTemplate *templ; - parms args[8]; // FIXME FIXME!! + /* Command check. */ templ = parser_get_cmd_template(buf); - if (!templ) { - NAK(ser, "invalid command"); + NAK(ser, "Invalid command."); return; } - // Extract the arguments for the command + parms args[PARSER_MAX_ARGS]; + + /* Args Check. */ if (!parser_get_cmd_arguments(buf, templ, args)) { - NAK(ser, "invalid arguments"); + NAK(ser, "Invalid arguments."); return; } - // EXEC! + /* Execute. */ + if (!parser_execute_cmd(templ, args)) + { + NAK(ser, "Command failed."); + } + else + { + if (protocol_reply(ser, templ, args) < 0) + { + NAK(ser, "Invalid return format."); + } + } + return; } void protocol_run(Serial *ser) { - // \todo to be removed, we could probably access the serial FIFO directly + /** + * \todo to be removed, we could probably access the serial FIFO + * directly + */ static char linebuf[80]; if (!interactive) @@ -103,10 +161,13 @@ void protocol_run(Serial *ser) if (linebuf[0] == 0x1B && linebuf[1] == 0x1B) // ESC { interactive = true; - ser_printf(ser, "Entering interactive mode\r\n"); + ser_printf(ser, + "Entering interactive mode\r\n"); } else + { protocol_parse(ser, linebuf); + } } } else @@ -129,7 +190,8 @@ void protocol_run(Serial *ser) if (!strcmp(buf, "exit") || !strcmp(buf, "quit")) { rl_clear_history(&rl_ctx); - ser_printf(ser, "Leaving interactive mode...\r\n"); + ser_printf(ser, + "Leaving interactive mode...\r\n"); interactive = FORCE_INTERACTIVE; } else @@ -146,21 +208,50 @@ void protocol_run(Serial *ser) } } +/* + * Commands. + * TODO: Command declarations and definitions should be in another file(s). + * Maybe we should use CMD_HUNK_TEMPLATE. + * + */ + +/* Version. */ static ResultCode cmd_ver(const char **str) { *str = VERS_TAG; -kprintf("hello, version world!\nver=" VERS_TAG "\n"); - return RC_REPLY; + return 0; } -static ResultCode cmd_add_hunk(parms args_results[]) +static ResultCode cmd_ver_hunk(parms args_results[]) { return cmd_ver(&args_results[0].s); } + const struct CmdTemplate cmd_ver_template = { - "ver", "", "d", cmd_add_hunk + "ver", "", "s", cmd_ver_hunk, 0 +}; + +//DECLARE_CMD_HUNK(ver, (NIL), (string)(NIL)); + +/* Sleep. */ + +static ResultCode cmd_sleep(const long ms) +{ + timer_delay((mtime_t)ms); + return 0; +} + +static ResultCode cmd_sleep_hunk(parms args[]) +{ + return cmd_sleep(args[1].l); +} + +const struct CmdTemplate cmd_sleep_template = +{ + "sleep", "d", "", cmd_sleep_hunk, 0 }; +/* Register commands. */ static void protocol_registerCmds(void) { // parser_register_cmd(&CMD_HUNK_TEMPLATE(quit)); @@ -175,6 +266,7 @@ static void protocol_registerCmds(void) // parser_register_cmd(&CMD_HUNK_TEMPLATE(power_off)); parser_register_cmd(&cmd_ver_template); + parser_register_cmd(&cmd_sleep_template); } void protocol_init(Serial *ser) { diff --git a/app/triface/triface.c b/app/triface/triface.c index 7cd01e00..759ba3bf 100755 --- a/app/triface/triface.c +++ b/app/triface/triface.c @@ -15,6 +15,9 @@ /*#* *#* $Log$ + *#* Revision 1.3 2006/06/12 21:37:02 marco + *#* implemented some commands (ver and sleep) + *#* *#* Revision 1.2 2006/06/01 12:29:21 marco *#* Add first simple protocol command (version request). *#* @@ -43,6 +46,7 @@ int main(void) ser_setbaudrate(host_port, 38400); protocol_init(host_port); + timer_delay(2000); // Main loop for(;;) diff --git a/app/triface/triface.mk b/app/triface/triface.mk index 79b52235..c1d095dc 100755 --- a/app/triface/triface.mk +++ b/app/triface/triface.mk @@ -8,6 +8,9 @@ # Author: Bernardo Innocenti # # $Log$ +# Revision 1.3 2006/06/12 21:37:02 marco +# implemented some commands (ver and sleep) +# # Revision 1.2 2006/06/01 12:29:21 marco # Add first simple protocol command (version request). # @@ -39,7 +42,8 @@ triface_CSRC = \ triface_PCSRC += mware/formatwr.c -triface_CFLAGS = -Os -D'ARCH=0' -Iapp/triface/hw -Iapp/triface +#triface_CFLAGS = -O3 -D'ARCH=0' -Iapp/triface/hw -Iapp/triface +triface_CFLAGS = -O0 -D'ARCH=0' -Iapp/triface/hw -Iapp/triface triface_MCU = atmega128 # Debug stuff diff --git a/config.mk b/config.mk index 9e6b18db..27c0e822 100755 --- a/config.mk +++ b/config.mk @@ -10,6 +10,9 @@ # Author: Bernardo Innocenti # # $Log$ +# Revision 1.5 2006/06/12 21:37:01 marco +# implemented some commands (ver and sleep) +# # Revision 1.4 2006/05/27 22:41:46 bernie # Tweak optimization flags for loops. # @@ -28,7 +31,8 @@ # Programmer type # # AVR ISP dongle that blows up easily -DPROG = -V -c stk500 -P /dev/ttyS0 +DPROG = -V -c jtag2slow +#-P /dev/ttyUSB0 # STK200 parallel cable #DPROG = -c stk200 -E noreset @@ -36,7 +40,8 @@ DPROG = -V -c stk500 -P /dev/ttyS0 # PonyProg serial programmer #DPROG = -c dasa2 -OPTCFLAGS = -ffunction-sections -fdata-sections -funsafe-loop-optimizations +#OPTCFLAGS = -ffunction-sections -fdata-sections -funsafe-loop-optimizations +OPTCFLAGS = -funsafe-loop-optimizations # For AVRStudio #DEBUGCFLAGS = -gdwarf-2 @@ -47,7 +52,7 @@ DEBUGCFLAGS = -ggdb # # define some variables based on the AVR base path in $(AVR) # -CROSS = +CROSS = avr- CC = $(CROSS)gcc AS = $(CC) -x assembler-with-cpp LD = $(CC) @@ -116,7 +121,7 @@ CPPAFLAGS = $(DEBUGCFLAGS) -MMD ASFLAGS = $(DEBUGCFLAGS) # Default linker flags -#LDFLAGS = $(MAP_FLAGS) +LDFLAGS = $(MAP_FLAGS) #bernie: does not complain for missing symbols! #LDFLAGS = $(MAP_FLAGS) -Wl,--gc-sections diff --git a/drv/timer_avr.c b/drv/timer_avr.c index 0b24b043..877f9193 100755 --- a/drv/timer_avr.c +++ b/drv/timer_avr.c @@ -15,6 +15,9 @@ /*#* *#* $Log$ + *#* Revision 1.3 2006/06/12 21:37:02 marco + *#* implemented some commands (ver and sleep) + *#* *#* Revision 1.2 2006/05/18 00:37:58 bernie *#* Don't include unneeded header hw.h. *#* @@ -28,7 +31,7 @@ #include #include // BV() -#include +#include #include /*! HW dependent timer initialization */ diff --git a/mware/parser.h b/mware/parser.h index 34f6a14d..9ad6567d 100755 --- a/mware/parser.h +++ b/mware/parser.h @@ -14,6 +14,9 @@ /*#* *#* $Log$ + *#* Revision 1.2 2006/06/12 21:37:02 marco + *#* implemented some commands (ver and sleep) + *#* *#* Revision 1.1 2006/06/01 12:27:39 marco *#* Added utilities for protocols *#* @@ -57,10 +60,11 @@ typedef ResultCode (*CmdFuncPtr)(parms args_results[]); */ struct CmdTemplate { - const char *name; //!< name of command - const char *arg_fmt; //!< format string for the input - const char *result_fmt; //!< format string for the output - CmdFuncPtr func; //!< pointer to the handler function + const char *name; ///< Name of command + const char *arg_fmt; ///< Format string for the input + const char *result_fmt; ///< Format string for the output + CmdFuncPtr func; ///< Pointer to the handler function + uint16_t flags; ///< Currently unused. }; /** diff --git a/mware/pgm.h b/mware/pgm.h index 4bd1018c..8f5b878d 100755 --- a/mware/pgm.h +++ b/mware/pgm.h @@ -150,6 +150,9 @@ #ifndef PSTR #define PSTR(s) ({ static const char __c[] PROGMEM = (s); &__c[0]; }) #endif + #ifndef PFUNC + #define PFUNC(x) x ## _P + #endif #elif CPU_HARVARD #error Missing CPU support @@ -159,6 +162,10 @@ #define PSTR /* nothing */ #endif +#ifndef PFUNC +#define PFUNC(x) x +#endif + #ifndef PROGMEM #define PROGMEM /* nothing */ #endif @@ -209,7 +216,7 @@ typedef PROGMEM uint32_t pgm_uint32_t; */ #ifdef _PROGMEM #define PGM_READ_CHAR(s) pgm_read_char(s) - #define PGM_FUNC(x) x ## _P + #define PGM_FUNC(x) PFUNC(x) #define PGM_STR(x) PSTR(x) #define PGM_ATTR PROGMEM #else -- 2.25.1