From af02757b51a5f92496a0aa991688c984dbb643cb Mon Sep 17 00:00:00 2001 From: Michal Sojka Date: Fri, 20 Feb 2009 21:19:36 +0100 Subject: [PATCH] tohit: Added verbose messages --- app/tohit/tohit.c | 9 +++-- app/tohit/tohit_fn.c | 78 +++++++++++++++++++++++++++++++------------- app/tohit/tohit_fn.h | 1 + 3 files changed, 64 insertions(+), 24 deletions(-) diff --git a/app/tohit/tohit.c b/app/tohit/tohit.c index 8e28452..5f6ba88 100644 --- a/app/tohit/tohit.c +++ b/app/tohit/tohit.c @@ -48,6 +48,7 @@ usage(void) printf(" -k, --break send communication break character\n"); printf(" -u, --upload upload memory block [download]\n"); printf(" -f, --format format of data file [intelhex]\n"); + printf(" -v, --verbose increase verbosity level\n"); printf(" -V, --version show version\n"); printf(" -h, --help this usage screen\n"); } @@ -73,15 +74,16 @@ int main(int argc, char **argv) { "upload",0, 0, 'u' }, { "format",1, 0, 'f' }, { "version",0,0, 'V' }, + { "verbose",0,0, 'v' }, { "help", 0, 0, 'h' }, { 0, 0, 0, 0} }; int opt; #ifndef HAS_GETOPT_LONG - while ((opt = getopt(argc, argv, "d:B:c:b:w:E:es:l:g:rkuf:VhD:")) != EOF) + while ((opt = getopt(argc, argv, "d:B:c:b:w:E:es:l:g:rkuf:vVhD:")) != EOF) #else - while ((opt = getopt_long(argc, argv, "d:B:c:b:w:E:es:l:g:rkuf:VhD:", + while ((opt = getopt_long(argc, argv, "d:B:c:b:w:E:es:l:g:rkuf:vVhD:", &long_opts[0], NULL)) != EOF) #endif switch (opt) { @@ -132,6 +134,9 @@ int main(int argc, char **argv) break; case 'f': break; + case 'v': + tohit_verbosity++; + break; case 'V': fputs("tohit pre alpha\n", stdout); exit(0); diff --git a/app/tohit/tohit_fn.c b/app/tohit/tohit_fn.c index 6a2be8a..aefb959 100644 --- a/app/tohit/tohit_fn.c +++ b/app/tohit/tohit_fn.c @@ -12,10 +12,16 @@ #define DEBUG 0 #define DEBUG_COUNT 0 +int tohit_verbosity; + +#define verbose(level, ...) \ + do { if (tohit_verbosity >= (level)) { printf("tohit: " __VA_ARGS__); } } while (0) + //#define WITHOUT_CFSETSPEED #ifdef WITHOUT_CFSETSPEED + struct rs232_speed_struct { speed_t value; @@ -197,13 +203,12 @@ int rs232_setmode(int fd, int baud, int mode, int flowc) int rs232_sendch(int fd,unsigned char c) { + verbose(3, " > sending 0x%02X\n", c); if(write(fd, &c, 1) != 1){ fprintf(stderr,"Error in rs232_sendch\n"); return -1; } - #if DEBUG - printf("rs232_sendch 0x%02X \n ",c); - #endif + return c; } @@ -228,9 +233,9 @@ int rs232_recch(int fd) printf("Read 0x%02X a recieved:%d \n",c,recieved); return -1; } - #if DEBUG - printf("Read 0x%02X \n",c); - #endif + + verbose(3, " < read 0x%02X \n",c); + return c; } @@ -287,6 +292,7 @@ int tohit_synchronize(int fd) { int i; unsigned char c; + verbose(1, "Synchronizing with target\n"); i=10; do{ c=0; @@ -324,6 +330,17 @@ int tohit_synchronize(int fd) return 0; } +const char *cmd_name[] = { + [1+TOHIT_WRITEBB] = "WRITEBB", + [1+TOHIT_WRITE] = "WRITE", + [1+TOHIT_WRITEFL] = "WRITEFL", + [1+TOHIT_READ] = "READ", + [1+TOHIT_ERASEBL] = "ERASEBL", + [1+TOHIT_ERASEREG] = "ERASEREG", + [1+TOHIT_GOTO] = "GOTO", + [1+TOHIT_RESET] = "RESET" +}; + int tohit_open4cmd(char *sdev, int baud, int cmd) { @@ -350,6 +367,7 @@ int tohit_open4cmd(char *sdev, int baud, int cmd) if(cmd!=-1){ /* send cmd */ + verbose(1, "Sending command 0x%02X (%s)\n", cmd, cmd_name[1+cmd]); c=cmd | ((cmd ^ 7) << 3); rs232_sendch(fd,c); rs232_test(fd,500000); @@ -364,6 +382,7 @@ int tohit_open4cmd(char *sdev, int baud, int cmd) int tohit_cmdrepchk(int fd) { int res; + verbose(2, "Waiting for end reply\n"); rs232_test(fd,2000000); res=rs232_recch(fd); if(res<0){ @@ -392,6 +411,8 @@ int tohit_goto(unsigned long adr) close(fd); return -1; } + + verbose(2, "Sending go address 0x%08lx\n", adr); if (tohit_sendichk(fd,adr,4)<0) { printf("Error in goto adr send and reply\n"); close(fd); @@ -421,17 +442,20 @@ int tohit_writemem(int cmd, const unsigned char *buf, return -1; } if(cmd==TOHIT_WRITEBB){ + verbose(2, "Sending size 0x%04lx\n", size); if (tohit_sendichk(fd,size,2)<0) { printf("Error in start adr send and reply\n"); close(fd); return -4; } }else{ + verbose(2, "Sending address 0x%08lx\n", adr); if (tohit_sendichk(fd,adr,4)<0) { printf("Error in start adr send and reply\n"); close(fd); return -4; } + verbose(2, "Sending size 0x%08lx\n", size); if (tohit_sendichk(fd,size,4)<0) { printf("Error in size send and reply\n"); close(fd); @@ -443,6 +467,7 @@ int tohit_writemem(int cmd, const unsigned char *buf, printf("Data send\n"); #endif /* DEBUG */ if(!blockmode){ + verbose(2, "Writing in single byte mode\n"); for(i=0;i(size-i)) count=size-i; - #if DEBUG_COUNT - printf("count %d\n",count); - #endif + verbose(3, " writing %d bytes\n",count); + j=0; while(j