From 8f7c501d0af5b01b18202fc2691ae13e5cbbeeae Mon Sep 17 00:00:00 2001 From: Nathael Pajani Date: Thu, 12 Apr 2012 15:24:17 +0200 Subject: [PATCH] Synchro with LPC1114F/302 is working --- isp_main.c | 77 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 30 deletions(-) diff --git a/isp_main.c b/isp_main.c index b18c24e..c3f30f1 100644 --- a/isp_main.c +++ b/isp_main.c @@ -41,7 +41,7 @@ void help(char *prog_name) " Default baudrate is B115200\n" \ " is the (host) serial line used to programm the device\n" \ " is one of:\n" \ - " \t synchronize \n" \ + " \t synchronize (sync done each time unless -n is used (for multiple successive calls))\n" \ " \t unlock \n" \ " \t set-baud-rate \n" \ " \t echo \n" \ @@ -57,7 +57,7 @@ void help(char *prog_name) " \t compare \n" \ " \t read-uid \n" \ " Available options:\n" \ - " \t -s | --synchronize : Perform synchronization before sending command\n" \ + " \t -n | --no-synchronize : Do not perform synchronization before sending command\n" \ " \t -b | --baudrate=N : Use this baudrate (does not issue the set-baud-rate command)\n" \ " \t -t | --trace : turn on trace output of serial communication\n" \ " \t -h | --help : display this help\n" \ @@ -175,29 +175,40 @@ int serial_read(char* buf, unsigned int buf_size, unsigned int min_read) return count; } -/* Connect or reconnect to the target */ -/* Return 1 when connection is OK, 0 otherwise */ +/* Connect or reconnect to the target. + * Return positive value when connection is OK, or negative value otherwise. + */ int connect() { char buf[SERIAL_BUFSIZE]; /* Send synchronize request */ - serial_write(SYNCHRO_START, 1); + if (serial_write(SYNCHRO_START, strlen(SYNCHRO_START)) != strlen(SYNCHRO_START)) { + printf("Unable to send synchronize request.\n"); + return -5; + } /* Wait for answer */ - serial_read(buf, SERIAL_BUFSIZE, strlen(SYNCHRO)); + if (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) { serial_write(SYNCHRO, strlen(SYNCHRO)); } else { printf("Unable to synchronize.\n"); - return 0; + return -1; } - /* Empty read buffer (echo on ?) */ - serial_read(buf, SERIAL_BUFSIZE, 1); + + /* Empty read buffer (maybe echo is on ?) */ + usleep( 5000 ); + serial_read(buf, SERIAL_BUFSIZE, 0); + + /* FIXME : Do we always turn off echo ? */ /* and turn off echo */ - serial_write("A 0\r\n", 1); + serial_write("A 0\r\n", 5); return 1; } @@ -206,7 +217,7 @@ int connect() int main(int argc, char** argv) { int baudrate = SERIAL_BAUD; - int synchronize = 0; + int dont_synchronize = 0; char* serial_device = NULL; /* For "command" handling */ @@ -221,7 +232,7 @@ int main(int argc, char** argv) int c = 0; struct option long_options[] = { - {"synchronize", no_argument, 0, 's'}, + {"no-synchronize", no_argument, 0, 'n'}, {"baudrate", required_argument, 0, 'b'}, {"trace", no_argument, 0, 't'}, {"help", no_argument, 0, 'h'}, @@ -229,7 +240,7 @@ int main(int argc, char** argv) {0, 0, 0, 0} }; - c = getopt_long(argc, argv, "sb:thv", long_options, &option_index); + c = getopt_long(argc, argv, "nb:thv", long_options, &option_index); /* no more options to parse */ if (c == -1) break; @@ -247,8 +258,8 @@ int main(int argc, char** argv) break; /* s, synchronize */ - case 's': - synchronize = 1; + case 'n': + dont_synchronize = 1; break; /* v, version */ @@ -267,14 +278,24 @@ int main(int argc, char** argv) /* Parse remaining command line arguments (not options). */ - /* First one should be serial device */ + /* First one should (must) be serial device */ if (optind < argc) { serial_device = argv[optind++]; if (trace_on) { printf("Serial device : %s\n", serial_device); } } - /* Next one should be "command" */ + if (serial_device == NULL) { + printf("No serial device given, exiting\n"); + help(argv[0]); + return 0; + } + if (serial_open(baudrate, serial_device) != 0) { + printf("Serial open failed, unable to initiate serial communication with target.\n"); + return -1; + } + + /* Next one should be "command" (if present) */ if (optind < argc) { command = argv[optind++]; if (trace_on) { @@ -285,23 +306,19 @@ int main(int argc, char** argv) if (optind < argc) { nb_cmd_args = argc - optind; cmd_args = malloc(nb_cmd_args * sizeof(char *)); + if (trace_on) { + printf("Command arguments :\n"); + } while (optind < argc) { - printf ("%s\n", argv[optind++]); + static unsigned int idx = 0; + cmd_args[idx++] = argv[optind++]; + if (trace_on) { + printf("%s\n", cmd_args[idx - 1]); + } } } - if (serial_device == NULL) { - printf("No serial device given, exiting\n"); - help(argv[0]); - return 0; - } - if (serial_open(baudrate, serial_device) != 0) { - printf("Serial open failed, unable to initiate serial communication with target.\n"); - return -1; - } - - /* FIXME : do not sync if command is sync */ - if (synchronize) { + if (! dont_synchronize) { connect(); } -- 2.43.0