Changed a few function headers and moved some code to ease
authorNathael Pajani <nathael.pajani@ed3l.fr>
Wed, 16 May 2012 22:27:46 +0000 (00:27 +0200)
committerNathael Pajani <nathael.pajani@ed3l.fr>
Wed, 16 May 2012 22:27:46 +0000 (00:27 +0200)
creation of the flash tool.

isp_commands.c
isp_commands.h
isp_main.c

index 5113cf1..d795233 100644 (file)
 
 extern int trace_on;
 
-struct isp_command {
-       char* name;
-       int nb_args;
-       int (*handler)(int arg_count, char** args);
-};
-
 
 /* Max should be 1270 for read memory, a little bit more for writes */
 #define SERIAL_BUFSIZE  1300
@@ -202,7 +196,7 @@ int isp_send_cmd_no_args(char* cmd_name, char* cmd)
        return ret;
 }
 
-int isp_cmd_unlock(int arg_count, char** args)
+int isp_cmd_unlock(int quiet)
 {
        int ret = 0;
 
@@ -211,12 +205,14 @@ int isp_cmd_unlock(int arg_count, char** args)
                printf("Unlock error.\n");
                return -1;
        }
-       printf("Device memory protection unlocked.\n");
+       if (quiet == 0) {
+               printf("Device memory protection unlocked.\n");
+       }
 
        return 0;
 }
 
-int isp_cmd_read_uid(int arg_count, char** args)
+int isp_cmd_read_uid(void)
 {
        char buf[REP_BUFSIZE];
        char* tmp = NULL;
@@ -244,7 +240,7 @@ int isp_cmd_read_uid(int arg_count, char** args)
        return 0;
 }
 
-int isp_cmd_part_id(int arg_count, char** args)
+int isp_cmd_part_id(int quiet)
 {
        char buf[REP_BUFSIZE];
        int ret = 0, len = 0;
@@ -262,12 +258,14 @@ int isp_cmd_part_id(int arg_count, char** args)
        }
        /* FIXME : some part IDs are on two 32bits values */
        part_id = strtoul(buf, NULL, 10);
-       printf("Part ID is 0x%08lx\n", part_id);
+       if (quiet == 0) {
+               printf("Part ID is 0x%08lx\n", part_id);
+       }
 
-       return 0;
+       return part_id;
 }
 
-int isp_cmd_boot_version(int arg_count, char** args)
+int isp_cmd_boot_version(void)
 {
        char buf[REP_BUFSIZE];
        int ret = 0, len = 0;
@@ -679,7 +677,7 @@ int isp_send_cmd_address(int arg_count, char** args, char* name, char cmd)
        unsigned long int addr1 = 0, addr2 = 0;
        unsigned long int length = 0;
 
-       /* Check compare arguments */
+       /* Check arguments */
        if (arg_count != 3) {
                printf("%s command needs two addresses and byte count.\n", name);
                return -7;
@@ -717,13 +715,13 @@ int isp_send_cmd_address(int arg_count, char** args, char* name, char cmd)
                        return -6;
        }
 
-       /* Create compare request */
+       /* Create request */
        len = snprintf(buf, SERIAL_BUFSIZE, "%c %lu %lu %lu\r\n", cmd, addr1, addr2, length);
        if (len > SERIAL_BUFSIZE) {
                len = SERIAL_BUFSIZE;
        }
 
-       /* Send compare request */
+       /* Send request */
        if (isp_serial_write(buf, len) != len) {
                printf("Unable to send %s request.\n", name);
                return -5;
@@ -952,64 +950,4 @@ int isp_cmd_erase(int arg_count, char** args)
 }
 
 
-static struct isp_command isp_cmds_list[] = {
-       {"unlock", 0, isp_cmd_unlock},
-       {"write-to-ram", 2, isp_cmd_write_to_ram},
-       {"read-memory", 3, isp_cmd_read_memory},
-       {"prepare-for-write", 2, isp_cmd_prepare_for_write},
-       {"copy-ram-to-flash", 3, isp_cmd_copy_ram_to_flash},
-       {"go", 2, isp_cmd_go},
-       {"erase", 2, isp_cmd_erase},
-       {"blank-check", 2, isp_cmd_blank_check},
-       {"read-part-id", 0, isp_cmd_part_id},
-       {"read-boot-version", 0, isp_cmd_boot_version},
-       {"compare", 3, isp_cmd_compare},
-       {"read-uid", 0, isp_cmd_read_uid},
-       {NULL, 0, NULL}
-};
-
-void isp_warn_trailing_args(int cmd_num, int arg_count, char** args)
-{
-       int i = 0;
-       printf("command \"%s\" needs %d args, got %d.\n",
-                       isp_cmds_list[cmd_num].name,
-                       isp_cmds_list[cmd_num].nb_args, arg_count);
-       for (i=0; i<arg_count; i++) {
-               printf("\targ[%d] : \"%s\"\n", i, args[i]);
-       }
-}
-
-/* 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)
-{
-       int cmd_found = -1;
-       int ret = 0;
-       int index = 0;
-
-       if (cmd == NULL) {
-               printf("isp_handle_command called with no command !\n");
-               return -1;
-       }
-
-       while ((cmd_found == -1) && (isp_cmds_list[index].name != NULL)) {
-               if (strncmp(isp_cmds_list[index].name, cmd, strlen(isp_cmds_list[index].name)) == 0) {
-                       cmd_found = index;
-                       break;
-               }
-               index++;
-       }
-       if (cmd_found == -1) {
-               printf("Unknown command \"%s\", use -h or --help for a list.\n", cmd);
-               return -2;
-       }
-       if (arg_count != isp_cmds_list[cmd_found].nb_args) {
-               isp_warn_trailing_args(cmd_found, arg_count, args);
-       }
-       ret = isp_cmds_list[cmd_found].handler(arg_count, args);
-
-       return ret;
-}
-
 
index f214159..aafca84 100644 (file)
  *
  *********************************************************************/
 
-/* 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);
+#ifndef ISP_COMMANDS_H
+#define ISP_COMMANDS_H
+
+
 
 /* Connect or reconnect to the target.
  * Return positive or NULL value when connection is OK, or negative value otherwise.
  */
 int isp_connect(unsigned int crystal_freq);
 
-/* 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 */
+
+int isp_send_buf_to_ram(char* data, unsigned long int addr, unsigned int count);
+
+
+int isp_cmd_unlock(int quiet);
+
+int isp_cmd_read_uid(void);
+
+int isp_cmd_part_id(int quiet);
+
+int isp_cmd_boot_version(void);
+
+int isp_cmd_read_memory(int arg_count, char** args);
+
+int isp_cmd_write_to_ram(int arg_count, char** args);
+
+int isp_cmd_compare(int arg_count, char** args);
+
+int isp_cmd_copy_ram_to_flash(int arg_count, char** args);
+
+int isp_cmd_go(int arg_count, char** args);
+
+int isp_cmd_blank_check(int arg_count, char** args);
+
+int isp_cmd_prepare_for_write(int arg_count, char** args);
+
+int isp_cmd_erase(int arg_count, char** args);
+
+
+#endif /* ISP_COMMANDS_H */
 
index fb64ca7..e35d905 100644 (file)
 #include <termios.h> /* for serial config */
 #include <ctype.h>
 
+#include <string.h> /* strncmp, strlen */
+
 #include "isp_utils.h"
 #include "isp_commands.h"
 
 #define PROG_NAME "LPC11xx ISP"
-#define VERSION   "0.01"
+#define VERSION   "1.0"
 
 /* short explanation on exit values:
  *
@@ -48,6 +50,9 @@ void help(char *prog_name)
                "  Default baudrate is B115200\n" \
                "  <device> is the (host) serial line used to programm the device\n" \
                "  <command> is one of:\n" \
+               "  \t unlock, write-to-ram, read-memory, prepare-for-write, copy-ram-to-flash, go, erase,\n" \
+               "  \t blank-check, read-part-id, read-boot-version, compare, and read-uid.\n" \
+               "  command specific arguments are as follow:\n" \
                "  \t unlock \n" \
                "  \t write-to-ram address file : send 'file' to 'address' in ram\n" \
                "  \t read-memory address count file : read 'count' bytes from 'address', store then in 'file'\n" \
@@ -85,6 +90,7 @@ void help(char *prog_name)
 
 int trace_on = 0;
 
+int isp_handle_command(char* cmd, int arg_count, char** args);
 
 int main(int argc, char** argv)
 {
@@ -227,3 +233,87 @@ int main(int argc, char** argv)
        return 0;
 }
 
+struct isp_command {
+       int cmd_num;
+       char* name;
+       int nb_args;
+       int (*handler)(int arg_count, char** args);
+};
+
+
+static struct isp_command isp_cmds_list[] = {
+       {0, "unlock", 0, NULL},
+       {1, "write-to-ram", 2, isp_cmd_write_to_ram},
+       {2, "read-memory", 3, isp_cmd_read_memory},
+       {3, "prepare-for-write", 2, isp_cmd_prepare_for_write},
+       {4, "copy-ram-to-flash", 3, isp_cmd_copy_ram_to_flash},
+       {5, "go", 2, isp_cmd_go},
+       {6, "erase", 2, isp_cmd_erase},
+       {7, "blank-check", 2, isp_cmd_blank_check},
+       {8, "read-part-id", 0, NULL},
+       {9, "read-boot-version", 0, NULL},
+       {10, "compare", 3, isp_cmd_compare},
+       {11, "read-uid", 0, NULL},
+       {-1, NULL, 0, NULL}
+};
+
+void isp_warn_args(int cmd_num, int arg_count, char** args)
+{
+       int i = 0;
+       printf("command \"%s\" needs %d args, got %d.\n",
+                       isp_cmds_list[cmd_num].name,
+                       isp_cmds_list[cmd_num].nb_args, arg_count);
+       for (i=0; i<arg_count; i++) {
+               printf("\targ[%d] : \"%s\"\n", i, args[i]);
+       }
+}
+
+/* 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)
+{
+       int cmd_found = -1;
+       int ret = 0;
+       int index = 0;
+
+       if (cmd == NULL) {
+               printf("isp_handle_command called with no command !\n");
+               return -1;
+       }
+
+       while ((cmd_found == -1) && (isp_cmds_list[index].name != NULL)) {
+               if (strncmp(isp_cmds_list[index].name, cmd, strlen(isp_cmds_list[index].name)) == 0) {
+                       cmd_found = index;
+                       break;
+               }
+               index++;
+       }
+       if (cmd_found == -1) {
+               printf("Unknown command \"%s\", use -h or --help for a list.\n", cmd);
+               return -2;
+       }
+       if (arg_count != isp_cmds_list[cmd_found].nb_args) {
+               isp_warn_args(cmd_found, arg_count, args);
+       }
+
+       switch (isp_cmds_list[cmd_found].cmd_num) {
+               case 0: /* unlock */
+                       ret = isp_cmd_unlock(0);
+                       break;
+               case 8: /* read-part-id */
+                       ret = isp_cmd_part_id(0);
+                       break;
+               case 9: /* read-boot-version */
+                       ret = isp_cmd_boot_version();
+                       break;
+               case 11: /* read-uid */
+                       ret = isp_cmd_read_uid();
+                       break;
+               default:
+                       ret = isp_cmds_list[cmd_found].handler(arg_count, args);
+       }
+
+       return ret;
+}
+