From: Nathael Pajani Date: Wed, 11 Apr 2012 21:35:12 +0000 (+0200) Subject: adding hexdump-like dump utility function for debugging purpose X-Git-Tag: v1.0~54 X-Git-Url: http://git.techno-innov.fr/?a=commitdiff_plain;h=4ebdd7ddf505d738370fb95ece4310c2c8a3ae70;p=soft%2Ftools%2Flpctools adding hexdump-like dump utility function for debugging purpose --- diff --git a/Makefile b/Makefile index 502ee78..b04ff17 100644 --- a/Makefile +++ b/Makefile @@ -7,5 +7,5 @@ CFLAGS += -Wall -Wextra -O2 all: isp isp: isp_main.c isp_utils.c isp_commands.c - $(CC) $(CFLAGS) $< -o $@ + $(CC) $(CFLAGS) $^ -o $@ diff --git a/isp_main.c b/isp_main.c index b4c3d7c..5a02f2f 100644 --- a/isp_main.c +++ b/isp_main.c @@ -15,12 +15,13 @@ #include #include +#include +#include -#include +#include /* for serial config */ #include -#include -#include /* for getopt */ +#include "isp_utils.h" #define PROG_NAME "LPC11xx ISP" #define VERSION "0.01" @@ -66,7 +67,7 @@ void help(char *prog_name) #define SERIAL_BUFSIZE 128 -#define SYNCHRO_START '?' +#define SYNCHRO_START "?" #define SYNCHRO "Synchronized" #define SERIAL_BAUD B115200 @@ -104,13 +105,13 @@ int serial_open(int baudrate) return 0; } -int serial_write(const char* buf, int buf_size) +int serial_write(const char* buf, unsigned int buf_size) { int nb = 0; if (trace_on) { printf("Sending %d :\n", buf_size); - /* FIXME : display data as in hexdump -C */ + isp_dump((unsigned char*)buf, buf_size); } nb = write(serial_fd, buf, buf_size); if (nb <= 0) { @@ -129,7 +130,7 @@ int isp_ret_code(char* buf) return 0; } -int serial_read(char* buf, int buf_size) +int serial_read(char* buf, unsigned int buf_size) { int nb = 0; @@ -143,7 +144,7 @@ int serial_read(char* buf, int buf_size) } if (trace_on) { printf("Received : %d octets\n", nb); - /* FIXME : display data as in hexdump -C */ + isp_dump((unsigned char*)buf, nb); } return nb; } @@ -155,7 +156,7 @@ int connect() char buf[SERIAL_BUFSIZE]; /* Send synchronize request */ - serial_write("?", 1); + serial_write(SYNCHRO_START, 1); /* Wait for answer */ serial_read(buf, SERIAL_BUFSIZE); diff --git a/isp_utils.c b/isp_utils.c index 33890b1..ed4ed91 100644 --- a/isp_utils.c +++ b/isp_utils.c @@ -21,6 +21,52 @@ #include +/* display data as in hexdump -C : + 00000000 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 |.ELF............| + */ +void isp_dump(const unsigned char* buf, unsigned int buf_size) +{ + unsigned int count = 0; + char pass = 0; + while ((count < buf_size) || (pass == 0)) { + if (count == buf_size) { + while (count & 0x0F) { + printf(" "); + count++; + } + pass = 1; + count -= 0x10; + } + if ((count % 0x10) == 0) { + if (pass == 0) { + printf(" %08x ", count); + } else { + printf(" |"); + } + } + if (pass == 0) { + printf("%02x ", buf[count]); + } else { + if (isgraph((int)buf[count])) { + printf("%c", buf[count]); + } else { + printf("."); + } + } + count++; + if ((count % 0x10) == 0) { + if (pass == 0) { + count -= 0x10; + } else { + printf("|\n"); + } + pass = !pass; + } + } + printf("|\n"); +} + + /* FIXME : This is a place-holder forr uuencode and uudecode functions !!! */ int uu_encode(unsigned char* dest, unsigned char* src, int orig_size) { diff --git a/isp_utils.h b/isp_utils.h new file mode 100644 index 0000000..bf3ea17 --- /dev/null +++ b/isp_utils.h @@ -0,0 +1,6 @@ +#ifndef ISP_UTILS_H +#define ISP_UTILS_H + +void isp_dump(const unsigned char* buf, unsigned int buf_size); + +#endif /* ISP_UTILS_H */