1
0
mirror of git://projects.qi-hardware.com/ben-blinkenlights.git synced 2024-11-23 22:42:49 +02:00

lpc111x-isp/lpc111x.c: option -v to control verbosity level

This commit is contained in:
Werner Almesberger 2012-12-28 23:12:37 -03:00
parent e4f8b97e36
commit c4d64fafa4

View File

@ -32,6 +32,13 @@
#define AUTOBAUD_TRIES 10
#define SYNC "Synchronized"
#define TRACE_ECHO (verbose > 3)
#define TRACE_PROGRESS (verbose > 2)
#define TRACE_RETRY (verbose > 1)
#define TRACE_FATAL (verbose > 0)
static int verbose = 0;
/* ----- Debugging and tracing --------------------------------------------- */
@ -82,23 +89,27 @@ static int dialog(const char *cmd, void *buf, int buf_len, int idle)
memcpy(tx_buf, cmd, cmd_len);
memcpy(tx_buf+cmd_len, "\r\n", 2);
trace_out(tx_buf, cmd_len+2);
if (TRACE_PROGRESS)
trace_out(tx_buf, cmd_len+2);
got = swuart_trx(tx_buf, cmd_len+2, rx_buf, cmd_len+2+buf_len,
idle, idle);
if (got < cmd_len+2) {
trace_in(rx_buf, got);
if (TRACE_FATAL)
trace_in(rx_buf, got);
fprintf(stderr, "response too short for echo\n");
exit(1);
}
if (memcmp(rx_buf, cmd, cmd_len) ||
rx_buf[cmd_len] != '\r' || rx_buf[cmd_len+1] != '\n') {
trace_in(rx_buf, got);
if (TRACE_FATAL)
trace_in(rx_buf, got);
fprintf(stderr, "echo mismatch\n");
exit(1);
}
trace_in(rx_buf, cmd_len+2);
if (TRACE_ECHO)
trace_in(rx_buf, cmd_len+2);
got -= cmd_len+2;
memcpy(buf, rx_buf+cmd_len+2, got);
@ -120,16 +131,24 @@ static int autobaud(void)
usleep(5*1000); /* UM 26.3.1 pg 408 says max 3 ms */
got = swuart_trx("?", 1, reply, sizeof(reply), 1000, 100);
trace_in(reply, got);
if (got != strlen(SYNC)+2 || memcmp(reply, SYNC "\r\n", got))
if (got != strlen(SYNC)+2 || memcmp(reply, SYNC "\r\n", got)) {
if (TRACE_RETRY)
trace_in(reply, got);
continue;
}
if (TRACE_PROGRESS)
trace_in(reply, got);
got = dialog(SYNC, reply, sizeof(reply), 100);
trace_in(reply, got);
if (got == 4 && !memcmp(reply, "OK\r\n", 4))
if (got == 4 && !memcmp(reply, "OK\r\n", 4)) {
if (TRACE_PROGRESS)
trace_in(reply, got);
return 1;
}
if (TRACE_RETRY)
trace_in(reply, got);
}
return 0;
}
@ -173,21 +192,50 @@ static void start_isp(void)
}
got = dialog("12000", reply, sizeof(reply), 100);
trace_in(reply, got);
if (got != 4 || memcmp(reply, "OK\r\n", 4)) {
if (TRACE_FATAL)
trace_in(reply, got);
fprintf(stderr, "cannot set clock rate\n");
exit(1);
}
if (TRACE_PROGRESS)
trace_in(reply, got);
}
/* ----- Command-line processing ------------------------------------------- */
static void usage(const char *name)
{
fprintf(stderr,
"usage: %s [-v ...]\n\n"
" -v increase verbosity level (nothing; errors; retry; progress; echo)\n"
, name);
exit(1);
}
int main(int argc, char **argv)
{
uint8_t reply[1000];
int got;
int c, got;
while ((c = getopt(argc, argv, "v")) != EOF)
switch (c) {
case 'v':
verbose++;
break;
default:
usage(*argv);
}
switch (argc-optind) {
case 0:
break;
default:
usage(*argv);
}
start_isp();