From 2a725d152f9298997b399406bafd73f1011d2167 Mon Sep 17 00:00:00 2001 From: hinni Date: Sat, 3 Jan 2004 18:04:54 +0000 Subject: [PATCH] *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 --- avr.c | 116 +++++++++++++++++++++++++++---------------------------- avr.h | 2 + main.c | 77 ++++++++++++++++++------------------ par.c | 25 ------------ stk500.c | 25 ------------ 5 files changed, 97 insertions(+), 148 deletions(-) diff --git a/avr.c b/avr.c index 58e81c13..87238814 100644 --- a/avr.c +++ b/avr.c @@ -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) { AVRMEM * a; - int cycle_count; - unsigned char v1, v2, v3, v4; + unsigned int cycle_count = 0; + unsigned char v1; int rc; + int i; a = avr_locate_mem(p, "eeprom"); if (a == NULL) { 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) { 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-3, &v2); - if (rc < 0) { - fprintf(stderr, "%s: WARNING: can't read memory for cycle count, rc=%d\n", - progname, rc); - return -1; + cycle_count = (cycle_count << 8) | v1; } - rc = avr_read_byte(pgm, p, a, a->size-2, &v3); - if (rc < 0) { - fprintf(stderr, "%s: WARNING: can't read memory for cycle count, rc=%d\n", - progname, rc); - return -1; + /* + * If the EEPROM is erased, the cycle count reads 0xffffffff. + * In this case we return a cycle_count of zero. + * So, the calling function don't have to care about whether or not + * the cycle count was initialized. + */ + if (cycle_count == 0xffffffff) { + cycle_count = 0; } - rc = avr_read_byte(pgm, p, a, a->size-1, &v4); - 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; + *cycles = (int) cycle_count; 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) { AVRMEM * a; - unsigned char v1, v2, v3, v4; + unsigned char v1; int rc; + int i; a = avr_locate_mem(p, "eeprom"); if (a == NULL) { return -1; } - v4 = cycles & 0x0ff; - v3 = (cycles & 0x0ff00) >> 8; - v2 = (cycles & 0x0ff0000) >> 16; - v1 = (cycles & 0x0ff000000) >> 24; + for (i=1; i<=4; i++) { + v1 = cycles & 0xff; + cycles = cycles >> 8; - rc = avr_write_byte(pgm, p, a, a->size-4, v1); - 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-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; + rc = avr_write_byte(pgm, p, a, a->size-i, v1); + if (rc < 0) { + fprintf(stderr, "%s: WARNING: can't write memory for cycle count, rc=%d\n", + progname, rc); + return -1; + } } 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; } diff --git a/avr.h b/avr.h index a197335d..73d85be7 100644 --- a/avr.h +++ b/avr.h @@ -69,6 +69,8 @@ int avr_put_cycle_count(PROGRAMMER * pgm, AVRPART * p, int cycles);; int avr_mem_hiaddr(AVRMEM * mem); +int avr_chip_erase(PROGRAMMER * pgm, AVRPART * p); + extern void report_progress (int completed, int total, char *hdr); #endif diff --git a/main.c b/main.c index 5d37afe8..4d16882a 100644 --- a/main.c +++ b/main.c @@ -1282,26 +1282,6 @@ int main(int argc, char * argv []) } } - if (set_cycles != -1) { - rc = avr_get_cycle_count(pgm, p, &cycles); - if (rc == 0) { - /* - * only attempt to update the cycle counter if we can actually - * read the old value - */ - cycles = set_cycles; - fprintf(stderr, "%s: setting erase-rewrite cycle count to %d\n", - progname, cycles); - rc = avr_put_cycle_count(pgm, p, cycles); - if (rc < 0) { - fprintf(stderr, - "%s: WARNING: failed to update the erase-rewrite cycle " - "counter\n", - progname); - } - } - } - if ((erase == 0) && (auto_erase == 1)) { AVRMEM * m; @@ -1321,6 +1301,44 @@ int main(int argc, char * argv []) } } + /* + * 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) { + rc = avr_get_cycle_count(pgm, p, &cycles); + if (rc == 0) { + /* + * only attempt to update the cycle counter if we can actually + * read the old value + */ + cycles = set_cycles; + fprintf(stderr, "%s: setting erase-rewrite cycle count to %d\n", + progname, cycles); + rc = avr_put_cycle_count(pgm, p, cycles); + if (rc < 0) { + fprintf(stderr, + "%s: WARNING: failed to update the erase-rewrite cycle " + "counter\n", + progname); + } + } + } if (erase) { @@ -1329,24 +1347,7 @@ int main(int argc, char * argv []) * before the chip can accept new programming */ fprintf(stderr, "%s: erasing chip\n", progname); - pgm->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)"); - } + avr_chip_erase(pgm, p); } diff --git a/par.c b/par.c index 85807236..ce931fd0 100644 --- a/par.c +++ b/par.c @@ -319,8 +319,6 @@ static int par_chip_erase(PROGRAMMER * pgm, AVRPART * p) { unsigned char cmd[4]; unsigned char res[4]; - int cycles; - int rc; if (p->op[AVR_OP_CHIP_ERASE] == NULL) { 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; } - 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); memset(cmd, 0, sizeof(cmd)); @@ -352,16 +337,6 @@ static int par_chip_erase(PROGRAMMER * pgm, AVRPART * p) 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; } diff --git a/stk500.c b/stk500.c index 3ff99c43..3b099d27 100644 --- a/stk500.c +++ b/stk500.c @@ -171,8 +171,6 @@ static int stk500_chip_erase(PROGRAMMER * pgm, AVRPART * p) { unsigned char cmd[4]; unsigned char res[4]; - int cycles; - int rc; if (p->op[AVR_OP_CHIP_ERASE] == NULL) { 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; } - 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); memset(cmd, 0, sizeof(cmd)); @@ -204,16 +189,6 @@ static int stk500_chip_erase(PROGRAMMER * pgm, AVRPART * p) 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; }