From 4fd20235f9e659c703151cd03285b240cb223c79 Mon Sep 17 00:00:00 2001 From: Nathael Pajani Date: Thu, 12 Apr 2012 16:02:42 +0200 Subject: [PATCH] Starting command handling --- isp_commands.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ isp_commands.h | 41 ++++++++++++++++++++++++++++++ isp_main.c | 68 +++++++++++--------------------------------------- 3 files changed, 121 insertions(+), 54 deletions(-) create mode 100644 isp_commands.h diff --git a/isp_commands.c b/isp_commands.c index e0809dc..f02c9c7 100644 --- a/isp_commands.c +++ b/isp_commands.c @@ -32,4 +32,70 @@ */ +#include "isp_utils.h" + +extern int trace_on; + +#define SERIAL_BUFSIZE 128 + +#define SYNCHRO_START "?" +#define SYNCHRO "Synchronized" + + +int isp_ret_code(char* buf) +{ + if (trace_on) { + /* FIXME : Find how return code are sent (binary or ASCII) */ + printf("Received code: '0x%02x'\n", *buf); + } + return 0; +} + + +/* Connect or reconnect to the target. + * Return positive or NULL value when connection is OK, or negative value otherwise. + */ +int isp_connect() +{ + char buf[SERIAL_BUFSIZE]; + + /* Send synchronize request */ + if (isp_serial_write(SYNCHRO_START, strlen(SYNCHRO_START)) != strlen(SYNCHRO_START)) { + printf("Unable to send synchronize request.\n"); + return -5; + } + + /* Wait for answer */ + if (isp_serial_read(buf, SERIAL_BUFSIZE, strlen(SYNCHRO)) < 0) { + printf("Error reading synchronize answer.\n"); + return -4; + } + + /* Check answer, and acknoledge if OK */ + if (strncmp(SYNCHRO, buf, strlen(SYNCHRO)) == 0) { + isp_serial_write(SYNCHRO, strlen(SYNCHRO)); + } else { + printf("Unable to synchronize.\n"); + return -1; + } + + /* Empty read buffer (maybe echo is on ?) */ + usleep( 5000 ); + isp_serial_read(buf, SERIAL_BUFSIZE, 0); + + /* FIXME : Do we always turn off echo ? */ + /* and turn off echo */ + isp_serial_write("A 0\r\n", 5); + + return 1; +} + +/* Handle one command + * Return positive or NULL value when command handling is OK, or negative value otherwise. + */ +int isp_handle_command(char* cmd, int arg_count, char** args) +{ + return 1; +} + diff --git a/isp_commands.h b/isp_commands.h new file mode 100644 index 0000000..fa67631 --- /dev/null +++ b/isp_commands.h @@ -0,0 +1,41 @@ +/********************************************************************* + * + * LPC1114 ISP Commands + * + *********************************************************************/ + +/* List of commands to be supported : + synchronize + unlock + set-baud-rate + echo + write-to-ram + read-memory + prepare-for-write + copy-ram-to-flash + go + erase + blank-check + read-part-id + read-boot-version + compare + read-uid +*/ + +#ifndef ISP_COMMADS_H +#define ISP_COMMADS_H + +int isp_ret_code(char* buf); + +/* Connect or reconnect to the target. + * Return positive or NULL value when connection is OK, or negative value otherwise. + */ +int isp_connect(); + +/* Handle one command + * Return positive or NULL value when command handling is OK, or negative value otherwise. + */ +int isp_handle_command(char* cmd, int arg_count, char** args); + +#endif /* ISP_COMMADS_H */ + diff --git a/isp_main.c b/isp_main.c index 8480f34..76cc790 100644 --- a/isp_main.c +++ b/isp_main.c @@ -22,6 +22,7 @@ #include #include "isp_utils.h" +#include "isp_commands.h" #define PROG_NAME "LPC11xx ISP" #define VERSION "0.01" @@ -65,65 +66,11 @@ void help(char *prog_name) fprintf(stderr, "-----------------------------------------------------------------------\n"); } -#define SERIAL_BUFSIZE 128 - -#define SYNCHRO_START "?" -#define SYNCHRO "Synchronized" - #define SERIAL_BAUD B115200 int trace_on = 0; -int isp_ret_code(char* buf) -{ - if (trace_on) { - /* FIXME : Find how return code are sent (binary or ASCII) */ - printf("Received code: '0x%02x'\n", *buf); - } - return 0; -} - - -/* Connect or reconnect to the target. - * Return positive value when connection is OK, or negative value otherwise. - */ -int isp_connect() -{ - char buf[SERIAL_BUFSIZE]; - - /* Send synchronize request */ - if (isp_serial_write(SYNCHRO_START, strlen(SYNCHRO_START)) != strlen(SYNCHRO_START)) { - printf("Unable to send synchronize request.\n"); - return -5; - } - - /* Wait for answer */ - if (isp_serial_read(buf, SERIAL_BUFSIZE, strlen(SYNCHRO)) < 0) { - printf("Error reading synchronize answer.\n"); - return -4; - } - - /* Check answer, and acknoledge if OK */ - if (strncmp(SYNCHRO, buf, strlen(SYNCHRO)) == 0) { - isp_serial_write(SYNCHRO, strlen(SYNCHRO)); - } else { - printf("Unable to synchronize.\n"); - return -1; - } - - /* Empty read buffer (maybe echo is on ?) */ - usleep( 5000 ); - isp_serial_read(buf, SERIAL_BUFSIZE, 0); - - /* FIXME : Do we always turn off echo ? */ - /* and turn off echo */ - isp_serial_write("A 0\r\n", 5); - - return 1; -} - - int main(int argc, char** argv) { int baudrate = SERIAL_BAUD; @@ -233,6 +180,19 @@ int main(int argc, char** argv) } /* FIXME : call command handler */ + if (command != NULL) { + int err = 0; + err = isp_handle_command(command, nb_cmd_args, cmd_args); + if (err >= 0) { + if (trace_on) { + printf("Command \"%s\" handled OK.\n", command); + } + } else { + printf("Error handling command \"%s\" : %d\n", command, err); + } + } else { + printf("No command given. use -h or --help for help on available commands.\n"); + } if (cmd_args != NULL) { -- 2.43.0