From 0b12206f1d22d482a2c42122bdfc6eabe6b6b27f Mon Sep 17 00:00:00 2001 From: david Date: Thu, 25 Jun 2009 11:11:56 +0200 Subject: [PATCH] add dump functions --- avr/usbload/Makefile | 2 +- avr/usbload/config.h | 11 ++++---- avr/usbload/debug.c | 51 +++++++++++++------------------------ avr/usbload/debug.h | 19 +++++++++++++- avr/usbload/dump.c | 60 ++++++++++++++++++++++++++++++++++++++++++++ avr/usbload/dump.h | 12 +++++++++ avr/usbload/main.c | 2 ++ avr/usbload/sram.c | 8 +++--- avr/usbload/sram.h | 2 +- 9 files changed, 121 insertions(+), 46 deletions(-) create mode 100644 avr/usbload/dump.c create mode 100644 avr/usbload/dump.h diff --git a/avr/usbload/Makefile b/avr/usbload/Makefile index 9ca26ea..b97f64f 100644 --- a/avr/usbload/Makefile +++ b/avr/usbload/Makefile @@ -8,7 +8,7 @@ AVRDUDE = avrdude -c usbasp -p $(DEVICE) CFLAGS = -Iusbdrv -I. -DDEBUG_LEVEL=0 #-std=gnu99 -OBJECTS = usbdrv/usbdrv.o usbdrv/usbdrvasm.o usbdrv/oddebug.o main.o usb_bulk.o uart.o fifo.o sram.o crc.o debug.o +OBJECTS = usbdrv/usbdrv.o usbdrv/usbdrvasm.o usbdrv/oddebug.o main.o usb_bulk.o uart.o fifo.o sram.o crc.o debug.o dump.o COMPILE = avr-gcc -Wall -Os -DF_CPU=$(F_CPU) $(CFLAGS) -mmcu=$(DEVICE) diff --git a/avr/usbload/config.h b/avr/usbload/config.h index 0d9e277..31752ba 100644 --- a/avr/usbload/config.h +++ b/avr/usbload/config.h @@ -2,13 +2,12 @@ #ifndef __config_h__ #define __config_h__ -#define DEBUG_USB 1 -#define DEBUG_USB_RAW 1 -#define DEBUG_SRAM 0 -#define DEBUG_SRAM_RAW 0 -#define DEBUG_SREG 0 #define DEBUG 1 - +#define DEBUG_USB 2 +#define DEBUG_USB_RAW 4 +#define DEBUG_SRAM 8 +#define DEBUG_SRAM_RAW 16 +#define DEBUG_SREG 32 #define REQ_STATUS_IDLE 0x01 #define REQ_STATUS_UPLOAD 0x02 diff --git a/avr/usbload/debug.c b/avr/usbload/debug.c index c453efa..d95fdc0 100644 --- a/avr/usbload/debug.c +++ b/avr/usbload/debug.c @@ -7,38 +7,23 @@ extern FILE uart_stdout; -void dump_packet(uint32_t addr, uint32_t len, uint8_t * packet) -{ - uint16_t i,j; - uint16_t sum = 0; - uint8_t clear = 0; +extern int debug_level; /* the higher, the more messages... */ - for (i = 0; i < len; i += 16) { - - sum = 0; - for (j = 0; j < 16; j++) { - sum += packet[i + j]; - } - if (!sum) { - clear = 1; - continue; - } - if (clear) { - printf("*\n"); - clear = 0; - } - printf("%08lx:", addr + i); - for (j = 0; j < 16; j++) { - printf(" %02x", packet[i + j]); - } - printf(" |"); - for (j = 0; j < 16; j++) { - if (packet[i + j] >= 33 && packet[i + j] <= 126) - printf("%c", packet[i + j]); - else - printf("."); - } - printf("|\n"); - } +#if defined(NO_DEBUG) && defined(__GNUC__) +/* Nothing. debug has been "defined away" in debug.h already. */ +#else +void debug(int level, char* format, ...) { +#ifdef NDEBUG + /* Empty body, so a good compiler will optimise calls + to pmesg away */ +#else + va_list args; + if (!(debug_level & level)) + return; + va_start(args, format); + printf(format, args); + va_end(args); +#endif /* NDEBUG */ +#endif /* NDEBUG && __GNUC__ */ } - + diff --git a/avr/usbload/debug.h b/avr/usbload/debug.h index ba212e2..d7bc8c9 100644 --- a/avr/usbload/debug.h +++ b/avr/usbload/debug.h @@ -1,5 +1,22 @@ +#ifndef DEBUG_H +#define DEBUG_H + #include -#include +#include +#include + + +#if defined(NO_DEBUG) && defined(__GNUC__) +/* gcc's cpp has extensions; it allows for macros with a variable number of + arguments. We use this extension here to preprocess pmesg away. */ +#define debug(level, format, args...) ((void)0) +#else +void debug(int level, char *format, ...); +/* print a message, if it is considered significant enough. + Adapted from [K&R2], p. 174 */ +#endif void dump_packet(uint32_t addr,uint32_t len,uint8_t *packet); +#endif /* DEBUG_H */ + \ No newline at end of file diff --git a/avr/usbload/dump.c b/avr/usbload/dump.c new file mode 100644 index 0000000..c217f29 --- /dev/null +++ b/avr/usbload/dump.c @@ -0,0 +1,60 @@ +#include +#include + +#include "debug.h" +#include "uart.h" +#include "sram.h" + + +extern FILE uart_stdout; + +void dump_packet(uint32_t addr, uint32_t len, uint8_t * packet) +{ + uint16_t i,j; + uint16_t sum = 0; + uint8_t clear = 0; + + for (i = 0; i < len; i += 16) { + + sum = 0; + for (j = 0; j < 16; j++) { + sum += packet[i + j]; + } + if (!sum) { + clear = 1; + continue; + } + if (clear) { + printf("*\n"); + clear = 0; + } + printf("%08lx:", addr + i); + for (j = 0; j < 16; j++) { + printf(" %02x", packet[i + j]); + } + printf(" |"); + for (j = 0; j < 16; j++) { + if (packet[i + j] >= 33 && packet[i + j] <= 126) + printf("%c", packet[i + j]); + else + printf("."); + } + printf("|\n"); + } +} + +void dump_memoryt(uint32_t bottom_addr, uint32_t top_addr) +{ + uint32_t addr; + uint8_t byte; + sram_bulk_read_start(bottom_addr); + printf("%08lx:", bottom_addr); + for ( addr = bottom_addr; addr < top_addr; addr++) { + if (addr%0x16 == 0) + printf("\n%08lx:", bottom_addr); + byte = sram_bulk_read(); + sram_bulk_read_next(); + printf(" %02x", byte); + } + sram_bulk_read_end(); +} diff --git a/avr/usbload/dump.h b/avr/usbload/dump.h new file mode 100644 index 0000000..9217254 --- /dev/null +++ b/avr/usbload/dump.h @@ -0,0 +1,12 @@ +#ifndef DUMP_H +#define DUMP_H + +#include +#include +#include + +void dump_packet(uint32_t addr,uint32_t len,uint8_t *packet); +void dump_memoryt(uint32_t bottom_addr, uint32_t top_addr); + +#endif + \ No newline at end of file diff --git a/avr/usbload/main.c b/avr/usbload/main.c index 46e50f1..7e32a81 100644 --- a/avr/usbload/main.c +++ b/avr/usbload/main.c @@ -17,6 +17,8 @@ extern FILE uart_stdout; +uint8_t debug_level = ( DEBUG | DEBUG_USB ); + uint8_t read_buffer[TRANSFER_BUFFER_SIZE]; uint32_t req_addr = 0; uint32_t req_size; diff --git a/avr/usbload/sram.c b/avr/usbload/sram.c index 65f4a7e..42ae2b0 100644 --- a/avr/usbload/sram.c +++ b/avr/usbload/sram.c @@ -285,13 +285,13 @@ void sram_bulk_read_buffer(uint32_t addr, uint8_t * dst, uint32_t len) void sram_bulk_set(uint32_t addr, uint32_t len,uint8_t value){ uint32_t i; #if DEBUG_SRAM - printf("sram_bulk_clear: addr=0x%08lx len=%li\n\r", addr,len); + printf("sram_bulk_set: addr=0x%08lx len=%li\n\r", addr,len); #endif sram_bulk_write_start(addr); for (i = addr; i < (addr + len); i++) { if (0 == i % 0xfff) #if DEBUG_SRAM - printf("sram_bulk_clear: addr=0x%08lx\n\r", i); + printf("sram_bulk_set: addr=0x%08lx\n\r", i); #endif sram_bulk_write(value); sram_bulk_write_next(); @@ -299,7 +299,7 @@ void sram_bulk_set(uint32_t addr, uint32_t len,uint8_t value){ sram_bulk_write_end(); } -void sram_clear(uint32_t addr, uint32_t len) +void sram_setr(uint32_t addr, uint32_t len,uint8_t value) { uint32_t i; #if DEBUG_SRAM @@ -310,7 +310,7 @@ void sram_clear(uint32_t addr, uint32_t len) #if DEBUG_SRAM printf("sram_clear: addr=0x%08lx\n\r", i); #endif - sram_write(i, 0x00); + sram_write(i, value); } } diff --git a/avr/usbload/sram.h b/avr/usbload/sram.h index 599ec4d..24e2620 100644 --- a/avr/usbload/sram.h +++ b/avr/usbload/sram.h @@ -145,7 +145,7 @@ void sreg_set(uint32_t addr); uint8_t sram_read(uint32_t addr); void sram_write(uint32_t addr, uint8_t data); -void sram_clear(uint32_t addr, uint32_t len); +void sram_set(uint32_t addr, uint32_t len, uint8_t value); void sram_copy(uint32_t addr,uint8_t *src, uint32_t len); void sram_read_buffer(uint32_t addr,uint8_t *dst, uint32_t len);