From 209f979c7f39829d7e09e1f111ac2ff98153811c Mon Sep 17 00:00:00 2001 From: "Brian S. Dean" Date: Mon, 22 Jan 2001 01:59:47 +0000 Subject: [PATCH] Makefile : use gzip -f for man page installation so that we don't get prompted. avr.c avr.h fileio.c term.c : Change the avrpart data structure so that the typedef AVRMEM is used as an index into an array for the sizes of the memory types and also for pointers to buffers that represent the chip data for that memory type. This removes a lot of conditional code of the form: switch (memtype) { case AVR_FLASH : ... } Also, re-code avr_read_byte() and avr_write_byte() to properly handle the flash memory type without having to tell them whether they should program the high byte or the low byte - figure that out from the address itself. For flash memory type, these routines now take the actual byte address instead of the word address. This _greatly_ simplifies many otherwise simple operations, such a reading or writing a range of memory, by not having to worry about whether the address starts on an odd byte or an even byte. git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@45 81a1dc3b-b13d-400b-aceb-764788c761c2 --- Makefile | 2 +- avr.c | 266 ++++++++++++++++--------------------------------------- avr.h | 28 +++++- fileio.c | 22 +---- term.c | 86 +++--------------- 5 files changed, 120 insertions(+), 284 deletions(-) diff --git a/Makefile b/Makefile index f628923f..d1bd23dd 100644 --- a/Makefile +++ b/Makefile @@ -46,5 +46,5 @@ ${BINDIR}/${TARGET} : ${TARGET} ${MANDIR}/${MANUAL}.gz : ${MANUAL} ${INSTALL_MANUAL} ${MANUAL} ${MANDIR} - gzip ${MANDIR}/${MANUAL} + gzip -f ${MANDIR}/${MANUAL} diff --git a/avr.c b/avr.c index b52222ae..ebed6dc9 100644 --- a/avr.c +++ b/avr.c @@ -47,26 +47,26 @@ extern char progbuf[]; /* Need to add information for 2323, 2343, and 4414 */ struct avrpart parts[] = { - { "AT90S1200", "1200", 1024, 64, 0xff, { 0x00, 0xff }, - 9000, 20000, 20000, NULL, NULL }, + { "AT90S1200", "1200", { 64, 1024 }, 0xff, { 0x00, 0xff }, + 9000, 20000, 20000, { NULL, NULL } }, - { "AT90S2313", "2313", 2048, 128, 0x7f, { 0x80, 0x7f }, - 9000, 20000, 20000, NULL, NULL }, + { "AT90S2313", "2313", { 128, 2048 }, 0x7f, { 0x80, 0x7f }, + 9000, 20000, 20000, { NULL, NULL } }, - { "AT90S2333", "2333", 2048, 128, 0xff, { 0x00, 0xff }, - 9000, 20000, 20000, NULL, NULL }, + { "AT90S2333", "2333", { 128, 2048 }, 0xff, { 0x00, 0xff }, + 9000, 20000, 20000, { NULL, NULL } }, - { "AT90S4433", "4433", 4096, 256, 0xff, { 0x00, 0xff }, - 9000, 20000, 20000, NULL, NULL }, + { "AT90S4433", "4433", { 256, 4096 }, 0xff, { 0x00, 0xff }, + 9000, 20000, 20000, { NULL, NULL } }, - { "AT90S4434", "4434", 4096, 256, 0xff, { 0x00, 0xff }, - 9000, 20000, 20000, NULL, NULL }, + { "AT90S4434", "4434", { 256, 4096 }, 0xff, { 0x00, 0xff }, + 9000, 20000, 20000, { NULL, NULL } }, - { "AT90S8515", "8515", 8192, 512, 0x7f, { 0x80, 0x7f }, - 9000, 20000, 20000, NULL, NULL }, + { "AT90S8515", "8515", { 512, 8192 }, 0x7f, { 0x80, 0x7f }, + 9000, 20000, 20000, { NULL, NULL } }, - { "AT90S8535", "8535", 8192, 512, 0xff, { 0x00, 0xff }, - 9000, 20000, 20000, NULL, NULL }, + { "AT90S8535", "8535", { 512, 8192 }, 0xff, { 0x00, 0xff }, + 9000, 20000, 20000, { NULL, NULL } }, }; #define N_AVRPARTS (sizeof(parts)/sizeof(struct avrpart)) @@ -169,27 +169,20 @@ int avr_cmd ( int fd, unsigned char cmd[4], unsigned char res[4] ) unsigned char avr_read_byte ( int fd, struct avrpart * p, AVRMEM memtype, unsigned short addr ) { + unsigned short offset; unsigned char cmd[4]; unsigned char res[4]; + /* order here is very important, AVR_EEPROM, AVR_FLASH, AVR_FLASH+1 */ + static unsigned char cmdbyte[3] = { 0xa0, 0x20, 0x28 }; - switch (memtype) { - case AVR_FLASH_LO: - cmd[0] = 0x20; - break; - case AVR_FLASH_HI: - cmd[0] = 0x28; - break; - case AVR_EEPROM: - cmd[0] = 0xa0; - break; - default: - fprintf(stderr, - "%s: avr_read_byte(); internal error: invalid memtype=%d\n", - progname, memtype); - return -1; - break; + offset = 0; + + if (memtype == AVR_FLASH) { + offset = addr & 0x01; + addr = addr / 2; } + cmd[0] = cmdbyte[memtype + offset]; cmd[1] = addr >> 8; /* high order bits of address */ cmd[2] = addr & 0x0ff; /* low order bits of address */ cmd[3] = 0; /* don't care */ @@ -208,60 +201,23 @@ unsigned char avr_read_byte ( int fd, struct avrpart * p, */ int avr_read ( int fd, struct avrpart * p, AVRMEM memtype ) { - unsigned char rbyte, memt; - unsigned short n, start, end, i, bi; - unsigned char * buf; - int bufsize; + unsigned char rbyte; + unsigned short i; + unsigned char * buf; + int size; - start = 0; + buf = p->mem[memtype]; + size = p->memsize[memtype]; - switch (memtype) { - case AVR_FLASH : - memt = AVR_FLASH_LO; - buf = p->flash; - n = p->flash_size/2; - bufsize = p->flash_size; - break; - - case AVR_EEPROM : - memt = memtype; - buf = p->eeprom; - n = p->eeprom_size; - bufsize = p->eeprom_size; - break; - - default: - fprintf(stderr, "%s: avr_read(); internal error: invalid memtype=%d\n", - progname, memtype); - return -1; - break; - } - - end = start+n; - - bi = 0; - - for (i=start; i> 8; /* high order bits of address */ - cmd[2] = addr & 0x0ff; /* low order bits of address */ + cmd[0] = cmdbyte[memtype + offset]; + cmd[1] = caddr >> 8; /* high order bits of address */ + cmd[2] = caddr & 0x0ff; /* low order bits of address */ cmd[3] = data; /* data */ avr_cmd(fd, cmd, res); @@ -355,83 +307,39 @@ int avr_write_byte ( int fd, struct avrpart * p, AVRMEM memtype, */ int avr_write ( int fd, struct avrpart * p, AVRMEM memtype, int size ) { - unsigned char data, memt; - unsigned short start, end, i, bi; - int nl; - int rc; - unsigned char * buf; - int bufsize; - int wsize; + int rc; + int wsize; + unsigned char * buf; + unsigned short i; + unsigned char data; - start = 0; - - switch (memtype) { - case AVR_FLASH : - buf = p->flash; - bufsize = p->flash_size; - wsize = bufsize; - if (size < wsize) { - bufsize = size; - } - end = start+bufsize/2; - memt = AVR_FLASH_LO; - break; - case AVR_EEPROM : - buf = p->eeprom; - bufsize = p->eeprom_size; - wsize = bufsize; - if (size < wsize) { - bufsize = size; - } - end = start+bufsize; - memt = memtype; - break; - default: - fprintf(stderr, "%s: avr_write(); internal error: invalid memtype=%d\n", - progname, memtype); - return -1; - break; + buf = p->mem[memtype]; + wsize = p->memsize[memtype]; + if (size < wsize) { + wsize = size; } - - /* were we asked to write out more data than the device can hold? */ - if (wsize < size) { + else if (size > wsize) { fprintf(stderr, "%s: WARNING: %d bytes requested, but memory region is only %d bytes\n" "%sOnly %d bytes will actually be written\n", - progname, size, bufsize, - progbuf, bufsize); + progname, size, wsize, + progbuf, wsize); } - bi = 0; - - for (i=start; iflash = (unsigned char *) malloc(p->flash_size); - if (p->flash == NULL) { - fprintf(stderr, "%s: can't alloc buffer for flash size of %d bytes\n", - progname, p->flash_size); - return -1; - } + int i; - p->eeprom = (unsigned char *) malloc(p->eeprom_size); - if (p->eeprom == NULL) { - fprintf(stderr, "%s: can't alloc buffer for eeprom size of %d bytes\n", - progname, p->eeprom_size); - return -1; + for (i=0; imem[i] = (unsigned char *) malloc(p->memsize[i]); + if (p->mem[i] == NULL) { + fprintf(stderr, "%s: can't alloc buffer for %s size of %d bytes\n", + progname, avr_memtstr(i), p->memsize[i]); + return -1; + } } return 0; @@ -598,24 +503,9 @@ int avr_verify(struct avrpart * p, struct avrpart * v, AVRMEM memtype, int size) unsigned char * buf1, * buf2; int vsize; - switch (memtype) { - case AVR_FLASH: - buf1 = p->flash; - buf2 = v->flash; - vsize = p->flash_size; - break; - - case AVR_EEPROM: - buf1 = p->eeprom; - buf2 = v->eeprom; - vsize = p->eeprom_size; - break; - - default: - fprintf(stderr, "%s: invalid memory type = %d for data verification\n", - progname, memtype); - return -1; - } + buf1 = p->mem[memtype]; + buf2 = v->mem[memtype]; + vsize = p->memsize[memtype]; if (vsize < size) { fprintf(stderr, @@ -654,8 +544,8 @@ void avr_display ( FILE * f, struct avrpart * p, char * prefix ) "%sFlash Polled Readback = 0x%02x\n" "%sEEPROM Polled Readback = 0x%02x, 0x%02x\n", prefix, p->partdesc, - prefix, p->flash_size, - prefix, p->eeprom_size, + prefix, p->memsize[AVR_FLASH], + prefix, p->memsize[AVR_EEPROM], prefix, p->min_write_delay, p->max_write_delay, prefix, p->chip_erase_delay, prefix, p->f_readback, diff --git a/avr.h b/avr.h index 160b3301..9a22e5d8 100644 --- a/avr.h +++ b/avr.h @@ -45,27 +45,47 @@ /* - * AVR memory designations + * AVR memory designations; the order of these is important, these are + * used as indexes into statically initialized data, don't change them + * around. */ typedef enum { AVR_EEPROM, - AVR_FLASH, - AVR_FLASH_LO, - AVR_FLASH_HI + AVR_FLASH } AVRMEM; +#define AVR_MAXMEMTYPES 2 /* just flash and eeprom */ + +#if 0 +struct avrmem { + AVRMEM memtype; + int startaddr; + int size; + unsigned char buf; + struct avrmem * next; +}; +#endif + struct avrpart { char * partdesc; /* long part name */ char * optiontag; /* short part name */ + int memsize[AVR_MAXMEMTYPES]; /* sizes for eeprom, + flash, etc, indexed by + AVR_EEPROM or AVR_FLASH */ +#if 0 int flash_size; /* size in bytes of flash */ int eeprom_size; /* size in bytes of eeprom */ +#endif unsigned char f_readback; /* flash write polled readback value */ unsigned char e_readback[2]; /* eeprom write polled readback values */ int min_write_delay; /* microseconds */ int max_write_delay; /* microseconds */ int chip_erase_delay; /* microseconds */ + unsigned char * mem[AVR_MAXMEMTYPES]; +#if 0 unsigned char * flash; unsigned char * eeprom; +#endif }; extern struct avrpart parts[]; diff --git a/fileio.c b/fileio.c index 2386003c..44642a53 100644 --- a/fileio.c +++ b/fileio.c @@ -433,24 +433,10 @@ int fileio ( int op, char * filename, FILEFMT format, } } - switch (memtype) { - case AVR_EEPROM: - buf = p->eeprom; - if (fio.op == FIO_READ) - size = p->eeprom_size; - break; - - case AVR_FLASH: - buf = p->flash; - if (fio.op == FIO_READ) - size = p->flash_size; - break; - - default: - fprintf(stderr, "%s: invalid memory type for %s: %d\n", - progname, fio.iodesc, memtype); - return -1; - } + /* point at the requested memory buffer */ + buf = p->mem[memtype]; + if (fio.op == FIO_READ) + size = p->memsize[memtype]; if (fio.op == FIO_READ) { /* 0xff fill unspecified memory */ diff --git a/term.c b/term.c index 5f8a27bf..23b1cdf7 100644 --- a/term.c +++ b/term.c @@ -196,8 +196,7 @@ int hexdump_buf ( FILE * f, int startaddr, char * buf, int len ) int cmd_dump ( int fd, struct avrpart * p, int argc, char * argv[] ) { char * e; - int i, j, l; - unsigned short daddr; + int i, l; char * buf; int maxsize; static AVRMEM memtype=AVR_FLASH; @@ -241,12 +240,7 @@ int cmd_dump ( int fd, struct avrpart * p, int argc, char * argv[] ) } } - switch (memtype) { - case AVR_FLASH : maxsize = p->flash_size; break; - case AVR_EEPROM : maxsize = p->eeprom_size; break; - default : return -1; /* this can't happen, but is silences gcc - warnings */ - } + maxsize = p->memsize[memtype]; if (addr > maxsize) { fprintf(stderr, @@ -265,29 +259,8 @@ int cmd_dump ( int fd, struct avrpart * p, int argc, char * argv[] ) return -1; } - j = 0; - daddr = addr; - if (memtype == AVR_FLASH) { - daddr = addr / 2; - if (addr & 0x01) { - buf[j++] = avr_read_byte( fd, p, AVR_FLASH_HI, daddr); - daddr++; - } - } - - i = daddr; - while (j < len) { - if (memtype == AVR_FLASH) { - buf[j++] = avr_read_byte( fd, p, AVR_FLASH_LO, i); - if (j < len) { - buf[j++] = avr_read_byte( fd, p, AVR_FLASH_HI, i); - } - } - else { - buf[j++] = avr_read_byte( fd, p, AVR_EEPROM, i); - } - i++; - } + for (i=0; iflash_size; } else if (strncasecmp(argv[1],"eeprom",l)==0) { memtype = AVR_EEPROM; - maxsize = p->eeprom_size; } else { fprintf(stderr, "%s (write): invalid memory type \"%s\"\n", @@ -329,6 +300,8 @@ int cmd_write ( int fd, struct avrpart * p, int argc, char * argv[] ) return -1; } + maxsize = p->memsize[memtype]; + addr = strtoul(argv[2], &e, 0); if (*e || (e == argv[2])) { fprintf(stderr, "%s (write): can't parse address \"%s\"\n", @@ -369,47 +342,14 @@ int cmd_write ( int fd, struct avrpart * p, int argc, char * argv[] ) } } - j = 0; - daddr = addr; - if (memtype == AVR_FLASH) { - daddr = addr / 2; - if (addr & 0x01) { - /* handle odd numbered memory locations in the flash area */ - rc = avr_write_byte(fd, p, AVR_FLASH_HI, daddr, buf[j++]); - if (rc) { - fprintf(stderr, "%s (write): error writing 0x%02x at 0x%04x\n", - progname, buf[j-1], daddr*2+1); - } - daddr++; + for (i=0; i