*main.c,avr.c,avr.h,par.c,stk500.c: Add function avr_chip_erase() to unify handling of cycle-count. Makes cycle-count work for avr910-programmers

git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@394 81a1dc3b-b13d-400b-aceb-764788c761c2
This commit is contained in:
hinni 2004-01-03 18:04:54 +00:00
parent ffc34dd86e
commit 2a725d152f
5 changed files with 97 additions and 148 deletions

106
avr.c
View File

@ -682,54 +682,37 @@ int avr_verify(AVRPART * p, AVRPART * v, char * memtype, int size)
int avr_get_cycle_count(PROGRAMMER * pgm, AVRPART * p, int * cycles) int avr_get_cycle_count(PROGRAMMER * pgm, AVRPART * p, int * cycles)
{ {
AVRMEM * a; AVRMEM * a;
int cycle_count; unsigned int cycle_count = 0;
unsigned char v1, v2, v3, v4; unsigned char v1;
int rc; int rc;
int i;
a = avr_locate_mem(p, "eeprom"); a = avr_locate_mem(p, "eeprom");
if (a == NULL) { if (a == NULL) {
return -1; return -1;
} }
rc = avr_read_byte(pgm, p, a, a->size-4, &v1); for (i=4; i>0; i--) {
rc = avr_read_byte(pgm, p, a, a->size-i, &v1);
if (rc < 0) { if (rc < 0) {
fprintf(stderr, "%s: WARNING: can't read memory for cycle count, rc=%d\n", fprintf(stderr, "%s: WARNING: can't read memory for cycle count, rc=%d\n",
progname, rc); progname, rc);
return -1; return -1;
} }
cycle_count = (cycle_count << 8) | v1;
rc = avr_read_byte(pgm, p, a, a->size-3, &v2);
if (rc < 0) {
fprintf(stderr, "%s: WARNING: can't read memory for cycle count, rc=%d\n",
progname, rc);
return -1;
} }
rc = avr_read_byte(pgm, p, a, a->size-2, &v3); /*
if (rc < 0) { * If the EEPROM is erased, the cycle count reads 0xffffffff.
fprintf(stderr, "%s: WARNING: can't read memory for cycle count, rc=%d\n", * In this case we return a cycle_count of zero.
progname, rc); * So, the calling function don't have to care about whether or not
return -1; * the cycle count was initialized.
*/
if (cycle_count == 0xffffffff) {
cycle_count = 0;
} }
rc = avr_read_byte(pgm, p, a, a->size-1, &v4); *cycles = (int) cycle_count;
if (rc < 0) {
fprintf(stderr, "%s: WARNING: can't read memory for cycle count, rc=%d\n",
progname, rc);
return -1;
}
if ((v1 == 0xff) && (v2 == 0xff) && (v3 != 0xff) && (v4 != 0xff)) {
v1 = 0;
v2 = 0;
}
cycle_count = (((unsigned int)v1) << 24) |
(((unsigned int)v2) << 16) |
(((unsigned int)v3) << 8) |
v4;
*cycles = cycle_count;
return 0; return 0;
} }
@ -738,43 +721,56 @@ int avr_get_cycle_count(PROGRAMMER * pgm, AVRPART * p, int * cycles)
int avr_put_cycle_count(PROGRAMMER * pgm, AVRPART * p, int cycles) int avr_put_cycle_count(PROGRAMMER * pgm, AVRPART * p, int cycles)
{ {
AVRMEM * a; AVRMEM * a;
unsigned char v1, v2, v3, v4; unsigned char v1;
int rc; int rc;
int i;
a = avr_locate_mem(p, "eeprom"); a = avr_locate_mem(p, "eeprom");
if (a == NULL) { if (a == NULL) {
return -1; return -1;
} }
v4 = cycles & 0x0ff; for (i=1; i<=4; i++) {
v3 = (cycles & 0x0ff00) >> 8; v1 = cycles & 0xff;
v2 = (cycles & 0x0ff0000) >> 16; cycles = cycles >> 8;
v1 = (cycles & 0x0ff000000) >> 24;
rc = avr_write_byte(pgm, p, a, a->size-4, v1); rc = avr_write_byte(pgm, p, a, a->size-i, v1);
if (rc < 0) { if (rc < 0) {
fprintf(stderr, "%s: WARNING: can't write memory for cycle count, rc=%d\n", fprintf(stderr, "%s: WARNING: can't write memory for cycle count, rc=%d\n",
progname, rc); progname, rc);
return -1; return -1;
} }
rc = avr_write_byte(pgm, p, a, a->size-3, v2);
if (rc < 0) {
fprintf(stderr, "%s: WARNING: can't write memory for cycle count, rc=%d\n",
progname, rc);
return -1;
}
rc = avr_write_byte(pgm, p, a, a->size-2, v3);
if (rc < 0) {
fprintf(stderr, "%s: WARNING: can't write memory for cycle count, rc=%d\n",
progname, rc);
return -1;
}
rc = avr_write_byte(pgm, p, a, a->size-1, v4);
if (rc < 0) {
fprintf(stderr, "%s: WARNING: can't write memory for cycle count, rc=%d\n",
progname, rc);
return -1;
} }
return 0; return 0;
}
int avr_chip_erase(PROGRAMMER * pgm, AVRPART * p)
{
int cycles;
int rc;
if (do_cycles) {
rc = avr_get_cycle_count(pgm, p, &cycles);
/*
* Don't update the cycle counter, if read failed
*/
if(rc != 0) {
do_cycles = 0;
}
}
rc = pgm->chip_erase(pgm, p);
/*
* Don't update the cycle counter, if erase failed
*/
if (do_cycles && (rc == 0)) {
cycles++;
fprintf(stderr, "%s: erase-rewrite cycle count is now %d\n",
progname, cycles);
avr_put_cycle_count(pgm, p, cycles);
}
return rc;
} }

2
avr.h
View File

@ -69,6 +69,8 @@ int avr_put_cycle_count(PROGRAMMER * pgm, AVRPART * p, int cycles);;
int avr_mem_hiaddr(AVRMEM * mem); int avr_mem_hiaddr(AVRMEM * mem);
int avr_chip_erase(PROGRAMMER * pgm, AVRPART * p);
extern void report_progress (int completed, int total, char *hdr); extern void report_progress (int completed, int total, char *hdr);
#endif #endif

77
main.c
View File

@ -1282,6 +1282,44 @@ int main(int argc, char * argv [])
} }
} }
if ((erase == 0) && (auto_erase == 1)) {
AVRMEM * m;
for (ln=lfirst(updates); ln; ln=lnext(ln)) {
upd = ldata(ln);
m = avr_locate_mem(p, upd->memtype);
if (m == NULL)
continue;
if ((strcasecmp(m->desc, "flash") == 0) && (upd->op == DEVICE_WRITE)) {
erase = 1;
fprintf(stderr,
"%s: NOTE: FLASH memory has been specified, an erase cycle will be performed\n"
"%sTo disable this feature, specify the -D option.\n",
progname, progbuf);
break;
}
}
}
/*
* Display cycle count, if and only if it is not set later on.
*
* The cycle count will be displayed anytime it will be changed later.
*/
if ((set_cycles == -1) && ((erase == 0) || (do_cycles == 0))) {
/*
* see if the cycle count in the last four bytes of eeprom seems
* reasonable
*/
rc = avr_get_cycle_count(pgm, p, &cycles);
if ((rc >= 0) && (cycles != 0)) {
fprintf(stderr,
"%s: current erase-rewrite cycle count is %d%s\n",
progname, cycles,
do_cycles ? "" : " (if being tracked)");
}
}
if (set_cycles != -1) { if (set_cycles != -1) {
rc = avr_get_cycle_count(pgm, p, &cycles); rc = avr_get_cycle_count(pgm, p, &cycles);
if (rc == 0) { if (rc == 0) {
@ -1302,26 +1340,6 @@ int main(int argc, char * argv [])
} }
} }
if ((erase == 0) && (auto_erase == 1)) {
AVRMEM * m;
for (ln=lfirst(updates); ln; ln=lnext(ln)) {
upd = ldata(ln);
m = avr_locate_mem(p, upd->memtype);
if (m == NULL)
continue;
if ((strcasecmp(m->desc, "flash") == 0) && (upd->op == DEVICE_WRITE)) {
erase = 1;
fprintf(stderr,
"%s: NOTE: FLASH memory has been specified, an erase cycle will be performed\n"
"%sTo disable this feature, specify the -D option.\n",
progname, progbuf);
break;
}
}
}
if (erase) { if (erase) {
/* /*
@ -1329,24 +1347,7 @@ int main(int argc, char * argv [])
* before the chip can accept new programming * before the chip can accept new programming
*/ */
fprintf(stderr, "%s: erasing chip\n", progname); fprintf(stderr, "%s: erasing chip\n", progname);
pgm->chip_erase(pgm, p); avr_chip_erase(pgm, p);
}
else if (set_cycles == -1) {
/*
* The erase routine displays this same information, so don't
* repeat it if an erase was done. Also, don't display this if we
* set the cycle count (due to -Y).
*
* see if the cycle count in the last four bytes of eeprom seems
* reasonable
*/
rc = avr_get_cycle_count(pgm, p, &cycles);
if ((rc >= 0) && (cycles != 0xffffffff)) {
fprintf(stderr,
"%s: current erase-rewrite cycle count is %d%s\n",
progname, cycles,
do_cycles ? "" : " (if being tracked)");
}
} }

25
par.c
View File

@ -319,8 +319,6 @@ static int par_chip_erase(PROGRAMMER * pgm, AVRPART * p)
{ {
unsigned char cmd[4]; unsigned char cmd[4];
unsigned char res[4]; unsigned char res[4];
int cycles;
int rc;
if (p->op[AVR_OP_CHIP_ERASE] == NULL) { if (p->op[AVR_OP_CHIP_ERASE] == NULL) {
fprintf(stderr, "chip erase instruction not defined for part \"%s\"\n", fprintf(stderr, "chip erase instruction not defined for part \"%s\"\n",
@ -328,19 +326,6 @@ static int par_chip_erase(PROGRAMMER * pgm, AVRPART * p)
return -1; return -1;
} }
rc = avr_get_cycle_count(pgm, p, &cycles);
/*
* only print out the current cycle count if we aren't going to
* display it below
*/
if (!do_cycles && ((rc >= 0) && (cycles != 0xffffffff))) {
fprintf(stderr,
"%s: current erase-rewrite cycle count is %d%s\n",
progname, cycles,
do_cycles ? "" : " (if being tracked)");
}
pgm->pgm_led(pgm, ON); pgm->pgm_led(pgm, ON);
memset(cmd, 0, sizeof(cmd)); memset(cmd, 0, sizeof(cmd));
@ -352,16 +337,6 @@ static int par_chip_erase(PROGRAMMER * pgm, AVRPART * p)
pgm->pgm_led(pgm, OFF); pgm->pgm_led(pgm, OFF);
if (do_cycles && (cycles != -1)) {
if (cycles == 0x00ffff) {
cycles = 0;
}
cycles++;
fprintf(stderr, "%s: erase-rewrite cycle count is now %d\n",
progname, cycles);
avr_put_cycle_count(pgm, p, cycles);
}
return 0; return 0;
} }

View File

@ -171,8 +171,6 @@ static int stk500_chip_erase(PROGRAMMER * pgm, AVRPART * p)
{ {
unsigned char cmd[4]; unsigned char cmd[4];
unsigned char res[4]; unsigned char res[4];
int cycles;
int rc;
if (p->op[AVR_OP_CHIP_ERASE] == NULL) { if (p->op[AVR_OP_CHIP_ERASE] == NULL) {
fprintf(stderr, "chip erase instruction not defined for part \"%s\"\n", fprintf(stderr, "chip erase instruction not defined for part \"%s\"\n",
@ -180,19 +178,6 @@ static int stk500_chip_erase(PROGRAMMER * pgm, AVRPART * p)
return -1; return -1;
} }
rc = avr_get_cycle_count(pgm, p, &cycles);
/*
* only print out the current cycle count if we aren't going to
* display it below
*/
if (!do_cycles && ((rc >= 0) && (cycles != 0xffffffff))) {
fprintf(stderr,
"%s: current erase-rewrite cycle count is %d%s\n",
progname, cycles,
do_cycles ? "" : " (if being tracked)");
}
pgm->pgm_led(pgm, ON); pgm->pgm_led(pgm, ON);
memset(cmd, 0, sizeof(cmd)); memset(cmd, 0, sizeof(cmd));
@ -204,16 +189,6 @@ static int stk500_chip_erase(PROGRAMMER * pgm, AVRPART * p)
pgm->pgm_led(pgm, OFF); pgm->pgm_led(pgm, OFF);
if (do_cycles && (cycles != -1)) {
if (cycles == 0x00ffff) {
cycles = 0;
}
cycles++;
fprintf(stderr, "%s: erase-rewrite cycle count is now %d\n",
progname, cycles);
avr_put_cycle_count(pgm, p, cycles);
}
return 0; return 0;
} }