diff --git a/src/avr.c b/src/avr.c index 158dcb115..2ffcd64c1 100644 --- a/src/avr.c +++ b/src/avr.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -57,7 +58,7 @@ int avr_tpi_chip_erase(const PROGRAMMER *pgm, const AVRPART *p) { led_set(pgm, LED_PGM); /* Set Pointer Register */ - mem = avr_locate_mem(p, "flash"); + mem = avr_locate_flash(p); if (mem == NULL) { pmsg_error("no flash memory to erase for part %s\n", p->desc); led_set(pgm, LED_ERR); @@ -234,7 +235,7 @@ int avr_read_byte_default(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM if (readop == NULL) { #if DEBUG - pmsg_error("operation not supported on memory type %s\n", mem->desc); + pmsg_error("operation not supported on memory %s\n", mem->desc); #endif goto error; } @@ -302,7 +303,7 @@ int avr_mem_hiaddr(const AVRMEM * mem) return mem->size; /* if the memory is not a flash-type memory do not remove trailing 0xff */ - if(!avr_mem_is_flash_type(mem)) + if(!mem_is_in_flash(mem)) return mem->size; /* return the highest non-0xff address regardless of how much @@ -322,17 +323,17 @@ int avr_mem_hiaddr(const AVRMEM * mem) /* - * Read the entirety of the specified memory type into the corresponding + * Read the entirety of the specified memory into the corresponding * buffer of the avrpart pointed to by p. If v is non-NULL, verify against * v's memory area, only those cells that are tagged TAG_ALLOCATED are * verified. * * Return the number of bytes read, or < 0 if an error occurs. */ -int avr_read(const PROGRAMMER *pgm, const AVRPART *p, const char *memtype, const AVRPART *v) { - AVRMEM *mem = avr_locate_mem(p, memtype); +int avr_read(const PROGRAMMER *pgm, const AVRPART *p, const char *memstr, const AVRPART *v) { + AVRMEM *mem = avr_locate_mem(p, memstr); if (mem == NULL) { - pmsg_error("no %s memory for part %s\n", memtype, p->desc); + pmsg_error("no %s memory for part %s\n", memstr, p->desc); return LIBAVRDUDE_GENERAL_FAILURE; } @@ -467,7 +468,7 @@ int avr_read_mem(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *mem, con /* else: fall back to byte-at-a-time read, for historical reasons */ } - if (str_eq(mem->desc, "signature")) { + if (mem_is_signature(mem)) { if (pgm->read_sig_bytes) { int rc = pgm->read_sig_bytes(pgm, p, mem); if (rc < 0) @@ -663,7 +664,7 @@ int avr_write_byte_default(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM goto error; } - if (str_eq(mem->desc, "flash")) { + if (mem_is_flash(mem)) { pmsg_error("writing a byte to flash is not supported for %s\n", p->desc); goto error; } else if ((mem->offset + addr) & 1) { @@ -675,7 +676,7 @@ int avr_write_byte_default(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM continue; /* must erase fuse first */ - if (str_eq(mem->desc, "fuse")) { + if (mem_is_a_fuse(mem)) { // TPI parts only have one fuse /* setup for SECTION_ERASE (high byte) */ avr_tpi_setup_rw(pgm, mem, addr | 1, TPI_NVMCMD_SECTION_ERASE); @@ -725,7 +726,7 @@ int avr_write_byte_default(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM rc = -2; goto rcerror; } - // Read operation is not support on this memory type + // Read operation is not support on this memory } else { readok = 1; @@ -758,7 +759,7 @@ int avr_write_byte_default(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM if (writeop == NULL) { #if DEBUG - pmsg_error("write not supported for memory type %s\n", mem->desc); + pmsg_error("write not supported for memory %s\n", mem->desc); #endif goto error; } @@ -782,7 +783,7 @@ int avr_write_byte_default(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM if (readok == 0) { /* - * read operation not supported for this memory type, just wait + * read operation not supported for this memory, just wait * the max programming time and then return */ usleep(mem->max_write_delay); /* maximum write delay */ @@ -833,7 +834,7 @@ int avr_write_byte_default(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM else if (mem->pwroff_after_write) { /* * The device has been flagged as power-off after write to this - * memory type. The reason we don't just blindly follow the + * memory. The reason we don't just blindly follow the * flag is that the power-off advice may only apply to some * memory bits but not all. We only actually power-off the * device if the data read back does not match what we wrote. @@ -907,10 +908,10 @@ int avr_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *mem, * * Return the number of bytes written, or LIBAVRDUDE_GENERAL_FAILURE on error. */ -int avr_write(const PROGRAMMER *pgm, const AVRPART *p, const char *memtype, int size, int auto_erase) { - AVRMEM *m = avr_locate_mem(p, memtype); +int avr_write(const PROGRAMMER *pgm, const AVRPART *p, const char *memstr, int size, int auto_erase) { + AVRMEM *m = avr_locate_mem(p, memstr); if (m == NULL) { - pmsg_error("no %s memory for part %s\n", memtype, p->desc); + pmsg_error("no %s memory for part %s\n", memstr, p->desc); return LIBAVRDUDE_GENERAL_FAILURE; } @@ -1154,7 +1155,7 @@ int avr_write_mem(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m, int int page_tainted = 0; int flush_page = 0; - int paged = avr_mem_is_flash_type(m) && m->paged; + int paged = mem_is_in_flash(m) && m->paged; if(paged) wsize = (wsize+1)/2*2; // Round up write size for word boundary @@ -1230,13 +1231,19 @@ int avr_signature(const PROGRAMMER *pgm, const AVRPART *p) { // Obtain bitmask for byte in memory (classic, TPI, PDI and UPDI parts) int avr_mem_bitmask(const AVRPART *p, const AVRMEM *mem, int addr) { int bitmask = mem->bitmask; + // Collective memory fuses will have a different bitmask for each address (ie, fuse) - if(str_eq(mem->desc, "fuses") && addr >=0 && addr < 16) { // Get right fuse in fuses memory - char memtype[64]; - AVRMEM *dfuse; - sprintf(memtype, "fuse%x", addr); - if((dfuse = avr_locate_mem(p, memtype)) && dfuse->size == 1) + if(mem_is_fuses(mem) && addr >=0 && addr < 16) { // Get right fuse in fuses memory + AVRMEM *dfuse = avr_locate_fuse_by_offset(p, addr); + if(dfuse) { bitmask = dfuse->bitmask; + if(dfuse->size == 2 && addr == (int) mem_fuse_offset(dfuse)+1) // High byte of 2-byte fuse + bitmask >>= 8; + } + } else if(mem_is_a_fuse(mem) && mem->size == 2 && addr == 1) { + bitmask >>= 8; + } else if(mem_is_lock(mem) && mem->size > 1 && mem->size <= 4 && addr >= 0 && addr < mem->size) { + bitmask >>= (8*addr); } return bitmask & 0xff; @@ -1281,21 +1288,21 @@ int compare_memory_masked(AVRMEM * m, uint8_t b1, uint8_t b2) { * * Return the number of bytes verified, or -1 if they don't match. */ -int avr_verify(const PROGRAMMER *pgm, const AVRPART *p, const AVRPART *v, const char *memtype, int size) { +int avr_verify(const PROGRAMMER *pgm, const AVRPART *p, const AVRPART *v, const char *memstr, int size) { int i; unsigned char * buf1, * buf2; int vsize; AVRMEM * a, * b; - a = avr_locate_mem(p, memtype); + a = avr_locate_mem(p, memstr); if (a == NULL) { - pmsg_error("memory type %s not defined for part %s\n", memtype, p->desc); + pmsg_error("memory %s not defined for part %s\n", memstr, p->desc); return -1; } - b = avr_locate_mem(v, memtype); + b = avr_locate_mem(v, memstr); if (b == NULL) { - pmsg_error("memory type %s not defined for part %s\n", memtype, v->desc); + pmsg_error("memory %s not defined for part %s\n", memstr, v->desc); return -1; } @@ -1305,7 +1312,7 @@ int avr_verify(const PROGRAMMER *pgm, const AVRPART *p, const AVRPART *v, const if (vsize < size) { pmsg_warning("requested verification for %d bytes\n", size); - imsg_warning("%s memory region only contains %d bytes\n", memtype, vsize); + imsg_warning("%s memory region only contains %d bytes\n", memstr, vsize); imsg_warning("only %d bytes will be verified\n", vsize); size = vsize; } @@ -1319,7 +1326,7 @@ int avr_verify(const PROGRAMMER *pgm, const AVRPART *p, const AVRPART *v, const if(vroerror < 10) { if(!(verror + vroerror)) pmsg_warning("verification mismatch%s\n", - avr_mem_is_flash_type(a)? " in r/o areas, expected for vectors and/or bootloader": ""); + mem_is_in_flash(a)? " in r/o areas, expected for vectors and/or bootloader": ""); imsg_warning("device 0x%02x != input 0x%02x at addr 0x%04x (read only location)\n", buf1[i], buf2[i], i); } else if(vroerror == 10) @@ -1342,12 +1349,12 @@ int avr_verify(const PROGRAMMER *pgm, const AVRPART *p, const AVRPART *v, const // Mismatch is only in unused bits if ((buf1[i] | bitmask) != 0xff) { // Programmer returned unused bits as 0, must be the part/programmer - pmsg_warning("ignoring mismatch in unused bits of %s\n", memtype); + pmsg_warning("ignoring mismatch in unused bits of %s\n", memstr); imsg_warning("(device 0x%02x != input 0x%02x); to prevent this warning fix\n", buf1[i], buf2[i]); imsg_warning("the part or programmer definition in the config file\n"); } else { // Programmer returned unused bits as 1, must be the user - pmsg_warning("ignoring mismatch in unused bits of %s\n", memtype); + pmsg_warning("ignoring mismatch in unused bits of %s\n", memstr); imsg_warning("(device 0x%02x != input 0x%02x); to prevent this warning set\n", buf1[i], buf2[i]); imsg_warning("unused bits to 1 when writing (double check with datasheet)\n"); } @@ -1366,17 +1373,16 @@ int avr_get_cycle_count(const PROGRAMMER *pgm, const AVRPART *p, int *cycles) { int rc; int i; - a = avr_locate_mem(p, "eeprom"); - if (a == NULL) { + a = avr_locate_eeprom(p); + if (a == NULL) return -1; - } for (i=4; i>0; i--) { rc = pgm->read_byte(pgm, p, a, a->size-i, &v1); - if (rc < 0) { - pmsg_warning("cannot read memory for cycle count, rc=%d\n", rc); - return -1; - } + if (rc < 0) { + pmsg_warning("cannot read memory for cycle count, rc=%d\n", rc); + return -1; + } cycle_count = (cycle_count << 8) | v1; } @@ -1402,10 +1408,9 @@ int avr_put_cycle_count(const PROGRAMMER *pgm, const AVRPART *p, int cycles) { int rc; int i; - a = avr_locate_mem(p, "eeprom"); - if (a == NULL) { + a = avr_locate_eeprom(p); + if (a == NULL) return -1; - } for (i=1; i<=4; i++) { v1 = cycles & 0xff; @@ -1459,67 +1464,87 @@ char *avr_prog_modes(int pm) { // Typical order in which memories show in avrdude.conf, runtime adds unknown ones (if any) -const char *avr_mem_order[100] = { - "eeprom", "flash", "application", "apptable", - "boot", "lfuse", "hfuse", "efuse", - "fuse", "fuse0", "wdtcfg", "fuse1", - "bodcfg", "fuse2", "osccfg", "fuse3", - "fuse4", "tcd0cfg", "fuse5", "syscfg0", - "fuse6", "syscfg1", "fuse7", "append", - "codesize", "fuse8", "fuse9", "bootend", - "bootsize", "fusea", "pdicfg", "fuses", - "lock", "lockbits", "prodsig", "sigrow", - "signature", "calibration", "tempsense", "sernum", - "osccal16", "osccal20", "osc16err", "osc20err", - "bootrow", "usersig", "userrow", "data", - "io", "sib", +memtable_t avr_mem_order[100] = { + {"eeprom", MEM_EEPROM}, + {"flash", MEM_FLASH | MEM_IN_FLASH}, + {"application", MEM_APPLICATION | MEM_IN_FLASH}, + {"apptable", MEM_APPTABLE | MEM_IN_FLASH}, + {"boot", MEM_BOOT | MEM_IN_FLASH}, + {"fuses", MEM_FUSES}, + {"fuse", MEM_FUSE0 | MEM_IS_A_FUSE}, + {"lfuse", MEM_FUSE0 | MEM_IS_A_FUSE}, + {"hfuse", MEM_FUSE1 | MEM_IS_A_FUSE}, + {"efuse", MEM_FUSE2 | MEM_IS_A_FUSE}, + {"fuse0", MEM_FUSE0 | MEM_IS_A_FUSE}, + {"wdtcfg", MEM_FUSE0 | MEM_IS_A_FUSE}, + {"fuse1", MEM_FUSE1 | MEM_IS_A_FUSE}, + {"bodcfg", MEM_FUSE1 | MEM_IS_A_FUSE}, + {"fuse2", MEM_FUSE2 | MEM_IS_A_FUSE}, + {"osccfg", MEM_FUSE2 | MEM_IS_A_FUSE}, + {"fuse4", MEM_FUSE4 | MEM_IS_A_FUSE}, + {"tcd0cfg", MEM_FUSE4 | MEM_IS_A_FUSE}, + {"fuse5", MEM_FUSE5 | MEM_IS_A_FUSE}, + {"syscfg0", MEM_FUSE5 | MEM_IS_A_FUSE}, + {"fuse6", MEM_FUSE6 | MEM_IS_A_FUSE}, + {"syscfg1", MEM_FUSE6 | MEM_IS_A_FUSE}, + {"fuse7", MEM_FUSE7 | MEM_IS_A_FUSE}, + {"append", MEM_FUSE7 | MEM_IS_A_FUSE}, + {"codesize", MEM_FUSE7 | MEM_IS_A_FUSE}, + {"fuse8", MEM_FUSE8 | MEM_IS_A_FUSE}, + {"bootend", MEM_FUSE8 | MEM_IS_A_FUSE}, + {"bootsize", MEM_FUSE8 | MEM_IS_A_FUSE}, + {"fusea", MEM_FUSEA | MEM_IS_A_FUSE}, + {"pdicfg", MEM_FUSEA | MEM_IS_A_FUSE}, + {"lock", MEM_LOCK}, + {"lockbits", MEM_LOCK}, + {"prodsig", MEM_SIGROW | MEM_IN_SIGROW | MEM_READONLY}, + {"sigrow", MEM_SIGROW | MEM_IN_SIGROW | MEM_READONLY}, + {"signature", MEM_SIGNATURE | MEM_IN_SIGROW | MEM_READONLY}, + {"calibration", MEM_CALIBRATION | MEM_IN_SIGROW | MEM_READONLY}, + {"tempsense", MEM_TEMPSENSE | MEM_IN_SIGROW | MEM_READONLY}, + {"sernum", MEM_SERNUM | MEM_IN_SIGROW | MEM_READONLY}, + {"osccal16", MEM_OSCCAL16 | MEM_IN_SIGROW | MEM_READONLY}, + {"osccal20", MEM_OSCCAL20 | MEM_IN_SIGROW | MEM_READONLY}, + {"osc16err", MEM_OSC16ERR | MEM_IN_SIGROW | MEM_READONLY}, + {"osc20err", MEM_OSC20ERR | MEM_IN_SIGROW | MEM_READONLY}, + {"bootrow", MEM_BOOTROW | MEM_USER_TYPE}, + {"usersig", MEM_USERROW | MEM_USER_TYPE}, + {"userrow", MEM_USERROW | MEM_USER_TYPE}, + {"data", MEM_SRAM}, + {"io", MEM_IO}, + {"sib", MEM_SIB | MEM_READONLY}, }; -void avr_add_mem_order(const char *str) { +int avr_get_mem_type(const char *str) { for(size_t i=0; i < sizeof avr_mem_order/sizeof *avr_mem_order; i++) { - if(avr_mem_order[i] && str_eq(avr_mem_order[i], str)) - return; - if(!avr_mem_order[i]) { - avr_mem_order[i] = cfg_strdup("avr_mem_order()", str); - return; + if(avr_mem_order[i].str && str_eq(avr_mem_order[i].str, str)) + return avr_mem_order[i].type; + if(!avr_mem_order[i].str) { + pmsg_warning("avr_mem_order[] does not know %s; add to array and recompile\n", str); + avr_mem_order[i].str = cfg_strdup(__func__, str); + return avr_mem_order[i].type; } } pmsg_error("avr_mem_order[] under-dimensioned in avr.c; increase and recompile\n"); exit(1); } -int avr_memtype_is_flash_type(const char *memtype) { - return memtype && ( - str_eq(memtype, "flash") || - str_eq(memtype, "application") || - str_eq(memtype, "apptable") || - str_eq(memtype, "boot")); -} - int avr_mem_is_flash_type(const AVRMEM *mem) { - return avr_memtype_is_flash_type(mem->desc); -} - -int avr_memtype_is_eeprom_type(const char *memtype) { - return memtype && str_eq(memtype, "eeprom"); + return mem_is_in_flash(mem); } int avr_mem_is_eeprom_type(const AVRMEM *mem) { - return avr_memtype_is_eeprom_type(mem->desc); -} - -int avr_memtype_is_usersig_type(const char *memtype) { // Bootrow is subsumed under usersig type - return memtype && (str_eq(memtype, "bootrow") || str_eq(memtype, "usersig") || str_eq(memtype, "userrow")); + return mem_is_eeprom(mem); } int avr_mem_is_usersig_type(const AVRMEM *mem) { - return avr_memtype_is_usersig_type(mem->desc); + return mem_is_user_type(mem); } int avr_mem_is_known(const char *str) { if(str && *str) for(size_t i=0; i < sizeof avr_mem_order/sizeof *avr_mem_order; i++) - if(avr_mem_order[i] && str_eq(avr_mem_order[i], str)) + if(avr_mem_order[i].str && str_eq(avr_mem_order[i].str, str)) return 1; return 0; } @@ -1527,7 +1552,7 @@ int avr_mem_is_known(const char *str) { int avr_mem_might_be_known(const char *str) { if(str && *str) for(size_t i=0; i < sizeof avr_mem_order/sizeof *avr_mem_order; i++) - if(avr_mem_order[i] && str_starts(avr_mem_order[i], str)) + if(avr_mem_order[i].str && str_starts(avr_mem_order[i].str, str)) return 1; return 0; } @@ -1593,3 +1618,14 @@ void report_progress(int completed, int total, const char *hdr) { update_progress(percent, t - start_time, hdr, total < 0? -1: !!total); } } + + +// Output comms buffer +void trace_buffer(const char *funstr, const unsigned char *buf, size_t buflen) { + pmsg_trace("%s: ", funstr); + while(buflen--) { + unsigned char c = *buf++; + msg_trace("%c [%02x]%s", isascii(c) && isprint(c)? c: '.', c, buflen? " ": ""); + } + msg_trace("\n"); +} diff --git a/src/avr910.c b/src/avr910.c index 288178e02..f7f5fb2b1 100644 --- a/src/avr910.c +++ b/src/avr910.c @@ -403,7 +403,7 @@ static int avr910_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRM { char cmd[2]; - if (str_eq(m->desc, "flash")) { + if (mem_is_flash(m)) { if (addr & 0x01) { cmd[0] = 'C'; /* Write Program Mem high byte */ } @@ -413,7 +413,7 @@ static int avr910_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRM addr >>= 1; } - else if (str_eq(m->desc, "eeprom")) { + else if (mem_is_eeprom(m)) { cmd[0] = 'D'; } else { @@ -468,11 +468,11 @@ static int avr910_read_byte_eeprom(const PROGRAMMER *pgm, const AVRPART *p, cons static int avr910_read_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m, unsigned long addr, unsigned char * value) { - if (str_eq(m->desc, "flash")) { + if (mem_is_flash(m)) { return avr910_read_byte_flash(pgm, p, m, addr, value); } - if (str_eq(m->desc, "eeprom")) { + if (mem_is_eeprom(m)) { return avr910_read_byte_eeprom(pgm, p, m, addr, value); } @@ -574,9 +574,9 @@ static int avr910_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AVR { int rval = 0; if (PDATA(pgm)->use_blockmode == 0) { - if (str_eq(m->desc, "flash")) { + if (mem_is_flash(m)) { rval = avr910_paged_write_flash(pgm, p, m, page_size, addr, n_bytes); - } else if (str_eq(m->desc, "eeprom")) { + } else if (mem_is_eeprom(m)) { rval = avr910_paged_write_eeprom(pgm, p, m, page_size, addr, n_bytes); } else { rval = -2; @@ -589,7 +589,7 @@ static int avr910_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AVR unsigned int blocksize = PDATA(pgm)->buffersize; int wr_size; - if (!str_eq(m->desc, "flash") && !str_eq(m->desc, "eeprom")) + if (!mem_is_flash(m) && !mem_is_eeprom(m)) return -2; if (m->desc[0] == 'e') { @@ -640,10 +640,10 @@ static int avr910_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AVRM max_addr = addr + n_bytes; - if (str_eq(m->desc, "flash")) { + if (mem_is_flash(m)) { cmd[0] = 'R'; rd_size = 2; /* read two bytes per addr */ - } else if (str_eq(m->desc, "eeprom")) { + } else if (mem_is_eeprom(m)) { cmd[0] = 'd'; rd_size = 1; } else { diff --git a/src/avrcache.c b/src/avrcache.c index 4a75efe3c..fca48b2dc 100644 --- a/src/avrcache.c +++ b/src/avrcache.c @@ -50,9 +50,9 @@ * int avr_reset_cache(const PROGRAMMER *pgm, const AVRPART *p); * * avr_read_byte_cached() and avr_write_byte_cached() use a cache if paged - * routines are available and if the device memory type is flash, EEPROM, - * bootrow or usersig. The AVRXMEGA memories application, apptable and boot - * are subsumed under flash. Userrow is subsumed under usersig provided + * routines are available and if the device memory is flash, EEPROM, bootrow + * or usersig. The AVRXMEGA memories application, apptable and boot are + * subsumed under flash. Userrow is subsumed under usersig provided * avrdude.conf has a memory alias from usersig to userrow. In all other * cases the cached read/write functions fall back to pgm->read_byte() and * pgm->write_byte(), respectively. Bytewise cached read always gets its data @@ -130,7 +130,7 @@ int avr_has_paged_access(const PROGRAMMER *pgm, const AVRMEM *mem) { return pgm->paged_load && pgm->paged_write && mem->page_size > 0 && (mem->page_size & (mem->page_size-1)) == 0 && mem->size > 0 && mem->size % mem->page_size == 0 && - (avr_mem_is_flash_type(mem) || avr_mem_is_eeprom_type(mem) || avr_mem_is_usersig_type(mem)); + mem_is_paged_type(mem); } @@ -263,8 +263,8 @@ static int loadCachePage(AVR_Cache *cp, const PROGRAMMER *pgm, const AVRPART *p, static int initCache(AVR_Cache *cp, const PROGRAMMER *pgm, const AVRPART *p) { - AVRMEM *basemem = avr_locate_mem(p, - cp == pgm->cp_flash? "flash": cp == pgm->cp_eeprom? "eeprom": cp == pgm->cp_bootrow? "bootrow": "usersig"); + AVRMEM *basemem = cp == pgm->cp_flash? avr_locate_flash(p): cp == pgm->cp_eeprom? avr_locate_eeprom(p): + cp == pgm->cp_bootrow? avr_locate_bootrow(p): avr_locate_usersig(p); if(!basemem || !avr_has_paged_access(pgm, basemem)) return LIBAVRDUDE_GENERAL_FAILURE; @@ -276,7 +276,7 @@ static int initCache(AVR_Cache *cp, const PROGRAMMER *pgm, const AVRPART *p) { cp->copy = cfg_malloc("initCache()", cp->size); cp->iscached = cfg_malloc("initCache()", cp->size/cp->page_size); - if((pgm->prog_modes & PM_SPM) && avr_mem_is_flash_type(basemem)) { // Could be vector bootloader + if((pgm->prog_modes & PM_SPM) && mem_is_in_flash(basemem)) { // Could be vector bootloader // Caching the vector page hands over to the progammer that then can patch the reset vector if(loadCachePage(cp, pgm, p, basemem, 0, 0, 0) < 0) return LIBAVRDUDE_GENERAL_FAILURE; @@ -377,10 +377,10 @@ typedef struct { // Write flash, EEPROM, bootrow and usersig caches to device and free them int avr_flush_cache(const PROGRAMMER *pgm, const AVRPART *p) { CacheDesc_t mems[] = { - { avr_locate_mem(p, "flash"), pgm->cp_flash, 1, 0, -1, 0 }, - { avr_locate_mem(p, "eeprom"), pgm->cp_eeprom, 0, 1, -1, 0 }, - { avr_locate_mem(p, "bootrow"), pgm->cp_bootrow, 0, 0, -1, 0 }, - { avr_locate_mem(p, "usersig"), pgm->cp_usersig, 0, 0, -1, 0 }, + { avr_locate_flash(p), pgm->cp_flash, 1, 0, -1, 0 }, + { avr_locate_eeprom(p), pgm->cp_eeprom, 0, 1, -1, 0 }, + { avr_locate_bootrow(p), pgm->cp_bootrow, 0, 0, -1, 0 }, + { avr_locate_usersig(p), pgm->cp_usersig, 0, 0, -1, 0 }, }; int chpages = 0; @@ -449,7 +449,7 @@ int avr_flush_cache(const PROGRAMMER *pgm, const AVRPART *p) { } } - if(!avr_mem_is_usersig_type(mems[i].mem)) // Only force CE if unable to write to flash/EEPROM + if(!mem_is_user_type(mems[i].mem)) // Only force CE if unable to write to flash/EEPROM chiperase = 1; } @@ -471,7 +471,7 @@ int avr_flush_cache(const PROGRAMMER *pgm, const AVRPART *p) { AVR_Cache *cp = mems[i].cp; if(!mem) continue; - if(avr_mem_is_usersig_type(mem)) // CE does not affect bootrow/usersig + if(mem_is_user_type(mem)) // CE does not affect bootrow/userrow continue; for(int pgno = 0, n = 0; n < cp->size; pgno++, n += cp->page_size) @@ -487,7 +487,7 @@ int avr_flush_cache(const PROGRAMMER *pgm, const AVRPART *p) { AVR_Cache *cp = mems[i].cp; if(!mem) continue; - if(avr_mem_is_usersig_type(mem)) // CE does not affect bootrow/usersig + if(mem_is_user_type(mem)) // CE does not affect bootrow/userrow continue; for(int ird = 0, pgno = 0, n = 0; n < cp->size; pgno++, n += cp->page_size) { @@ -516,7 +516,7 @@ int avr_flush_cache(const PROGRAMMER *pgm, const AVRPART *p) { AVR_Cache *cp = mems[i].cp; if(!mem) continue; - if(avr_mem_is_usersig_type(mem)) // CE does not affect bootrow/usersig + if(mem_is_user_type(mem)) // CE does not affect bootrow/userrow continue; if(mems[i].isflash) { @@ -628,9 +628,8 @@ int avr_read_byte_cached(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM * return LIBAVRDUDE_SUCCESS; } - AVR_Cache *cp = avr_mem_is_eeprom_type(mem)? pgm->cp_eeprom: - avr_mem_is_flash_type(mem)? pgm->cp_flash: - str_eq(mem->desc, "bootrow")? pgm->cp_bootrow: pgm->cp_usersig; + AVR_Cache *cp = mem_is_eeprom(mem)? pgm->cp_eeprom: mem_is_in_flash(mem)? pgm->cp_flash: + mem_is_bootrow(mem)? pgm->cp_bootrow: pgm->cp_usersig; if(!cp->cont) // Init cache if needed if(initCache(cp, pgm, p) < 0) @@ -669,9 +668,8 @@ int avr_write_byte_cached(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM if(addr >= (unsigned long) mem->size) return avr_flush_cache(pgm, p); - AVR_Cache *cp = avr_mem_is_eeprom_type(mem)? pgm->cp_eeprom: - avr_mem_is_flash_type(mem)? pgm->cp_flash: - str_eq(mem->desc, "bootrow")? pgm->cp_bootrow: pgm->cp_usersig; + AVR_Cache *cp = mem_is_eeprom(mem)? pgm->cp_eeprom: mem_is_in_flash(mem)? pgm->cp_flash: + mem_is_bootrow(mem)? pgm->cp_bootrow: pgm->cp_usersig; if(!cp->cont) // Init cache if needed if(initCache(cp, pgm, p) < 0) @@ -700,8 +698,8 @@ int avr_write_byte_cached(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM // Erase the chip and set the cache accordingly int avr_chip_erase_cached(const PROGRAMMER *pgm, const AVRPART *p) { CacheDesc_t mems[3] = { - { avr_locate_mem(p, "flash"), pgm->cp_flash, 1, 0, -1, 0 }, - { avr_locate_mem(p, "eeprom"), pgm->cp_eeprom, 0, 1, -1, 0 }, + { avr_locate_flash(p), pgm->cp_flash, 1, 0, -1, 0 }, + { avr_locate_eeprom(p), pgm->cp_eeprom, 0, 1, -1, 0 }, // bootrow/usersig is unaffected by CE }; int rc; @@ -773,9 +771,8 @@ int avr_page_erase_cached(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM return LIBAVRDUDE_GENERAL_FAILURE; } - AVR_Cache *cp = avr_mem_is_eeprom_type(mem)? pgm->cp_eeprom: - avr_mem_is_flash_type(mem)? pgm->cp_flash: - str_eq(mem->desc, "bootrow")? pgm->cp_bootrow: pgm->cp_usersig; + AVR_Cache *cp = mem_is_eeprom(mem)? pgm->cp_eeprom: mem_is_in_flash(mem)? pgm->cp_flash: + mem_is_bootrow(mem)? pgm->cp_bootrow: pgm->cp_usersig; if(!cp->cont) // Init cache if needed if(initCache(cp, pgm, p) < 0) diff --git a/src/avrdude.1 b/src/avrdude.1 index 03ce290d4..7e4aacfa1 100644 --- a/src/avrdude.1 +++ b/src/avrdude.1 @@ -767,7 +767,7 @@ device-dependent, the actual configuration can be viewed with the .Cm part command in terminal mode. Typically, a device's memory configuration at least contains -the memory types +the memories .Ar flash , .Ar eeprom , .Ar signature @@ -786,7 +786,7 @@ will also typically have fuse bytes, which are read/write memories for configuration of the device and calibration memories that typically contain read-only factory calibration values. .Pp -Classic devices may have the following memory types in addition to eeprom, flash, signature and lock: +Classic devices may have the following memories in addition to eeprom, flash, signature and lock: .Bl -tag -width " calibration" -compact .It calibration One or more bytes of RC oscillator calibration data @@ -824,7 +824,7 @@ methods only by bootloaders, which has limited use unless the bootloader jumps to the application directly, i.e., without a WDT reset .El .Pp -ATxmega devices have the following memory types in addition to eeprom, flash, signature and lock: +ATxmega devices have the following memories in addition to eeprom, flash, signature and lock: .Bl -tag -width "calibration" -compact .It application Application flash area @@ -859,7 +859,7 @@ Volatile register memory; can read this memory but not write to it using external programming .El .Pp -Modern 8-bit AVR devices have the following memory types in addition to eeprom, flash, signature and lock: +Modern 8-bit AVR devices have the following memories in addition to eeprom, flash, signature and lock: .Bl -tag -width "calibration" -compact .It fuse0 A.k.a. wdtcfg: watchdog configuration @@ -1252,7 +1252,7 @@ on a non-zero verbosity level the line numbers are printed, too. Display the device signature bytes. .It Ar part Display the current part settings and parameters. Includes chip -specific information including all memory types supported by the +specific information including all memories supported by the device, read/write timing, etc. .It Ar verbose Op Ar level Change (when diff --git a/src/avrdude.conf.in b/src/avrdude.conf.in index db781b0bb..5e2918c55 100644 --- a/src/avrdude.conf.in +++ b/src/avrdude.conf.in @@ -184,7 +184,7 @@ avrdude_conf_version = "@AVRDUDE_FULL_VERSION@"; # # parameters for bootloaders # autobaud_sync = ; # autobaud detection byte, default 0x30 # -# memory +# memory # paged = ; # yes/no (flash only, do not use for EEPROM) # offset = ; # memory offset # size = ; # bytes @@ -244,13 +244,13 @@ avrdude_conf_version = "@AVRDUDE_FULL_VERSION@"; # NOTES: # * 'devicecode' is the device code used by the STK500 (see codes # listed below) -# * Not all memory types will implement all instructions +# * Not all memories will implement all instructions # * AVR Fuse bits and Lock bits are implemented as a type of memory -# * Example memory types are: +# * Example memories are: # "flash", "eeprom", "fuse", "lfuse" (low fuse), "hfuse" (high # fuse), "signature", "calibration", "lock" -# * The memory type specified on the avrdude command line must match -# one of the memory types defined for the specified chip +# * The memory specified on the avrdude command line must match +# one of the memories defined for the specified chip # * The pwroff_after_write flag causes avrdude to attempt to # power the device off and back on after an unsuccessful write to # the affected memory area if VCC programmer pins are defined. If @@ -19202,6 +19202,12 @@ part # .avr8x ocd_base = 0x0f80; syscfg_base = 0x0f00; + memory "fuses" + size = 10; + offset = 0x1280; + readsize = 1; + ; + memory "fuse0" size = 1; initval = 0x00; @@ -19290,13 +19296,6 @@ part # .avr8x alias "fuse8"; ; - memory "fuses" - size = 10; - page_size = 10; - offset = 0x1280; - readsize = 10; - ; - memory "lock" size = 1; offset = 0x128a; @@ -21436,6 +21435,12 @@ part # .avrdx ocd_base = 0x0f80; syscfg_base = 0x0f00; + memory "fuses" + size = 16; + offset = 0x1050; + readsize = 1; + ; + memory "fuse0" size = 1; initval = 0x00; @@ -21524,13 +21529,6 @@ part # .avrdx alias "fuse8"; ; - memory "fuses" - size = 16; - page_size = 16; - offset = 0x1050; - readsize = 16; - ; - memory "lock" size = 4; offset = 0x1040; diff --git a/src/avrftdi.c b/src/avrftdi.c index 3ffa6d45a..e3af2e464 100644 --- a/src/avrftdi.c +++ b/src/avrftdi.c @@ -1246,9 +1246,9 @@ static int avrftdi_flash_read(const PROGRAMMER *pgm, const AVRPART *p, const AVR static int avrftdi_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m, unsigned int page_size, unsigned int addr, unsigned int n_bytes) { - if (str_eq(m->desc, "flash")) + if (mem_is_flash(m)) return avrftdi_flash_write(pgm, p, m, page_size, addr, n_bytes); - else if (str_eq(m->desc, "eeprom")) + else if (mem_is_eeprom(m)) return avrftdi_eeprom_write(pgm, p, m, page_size, addr, n_bytes); else return -2; @@ -1257,9 +1257,9 @@ static int avrftdi_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AV static int avrftdi_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m, unsigned int page_size, unsigned int addr, unsigned int n_bytes) { - if (str_eq(m->desc, "flash")) + if (mem_is_flash(m)) return avrftdi_flash_read(pgm, p, m, page_size, addr, n_bytes); - else if(str_eq(m->desc, "eeprom")) + else if(mem_is_eeprom(m)) return avrftdi_eeprom_read(pgm, p, m, page_size, addr, n_bytes); else return -2; @@ -1534,7 +1534,7 @@ static int avrftdi_jtag_chip_erase(const PROGRAMMER *pgm, const AVRPART *p) static int avrftdi_jtag_read_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m, unsigned long addr, unsigned char *value) { - if (str_eq(m->desc, "lfuse")) { + if (mem_is_lfuse(m)) { avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS); avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_FUSE_READ, 15); @@ -1542,7 +1542,7 @@ static int avrftdi_jtag_read_byte(const PROGRAMMER *pgm, const AVRPART *p, avrftdi_jtag_dr_out(pgm, 0x3200, 15); *value = avrftdi_jtag_dr_inout(pgm, 0x3300, 15) & 0xff; - } else if (str_eq(m->desc, "hfuse")) { + } else if (mem_is_hfuse(m)) { avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS); avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_FUSE_READ, 15); @@ -1550,7 +1550,7 @@ static int avrftdi_jtag_read_byte(const PROGRAMMER *pgm, const AVRPART *p, avrftdi_jtag_dr_out(pgm, 0x3e00, 15); *value = avrftdi_jtag_dr_inout(pgm, 0x3f00, 15) & 0xff; - } else if (str_eq(m->desc, "efuse")) { + } else if (mem_is_efuse(m)) { avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS); avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_FUSE_READ, 15); @@ -1558,7 +1558,7 @@ static int avrftdi_jtag_read_byte(const PROGRAMMER *pgm, const AVRPART *p, avrftdi_jtag_dr_out(pgm, 0x3a00, 15); *value = avrftdi_jtag_dr_inout(pgm, 0x3b00, 15) & 0xff; - } else if (str_eq(m->desc, "lock")) { + } else if (mem_is_lock(m)) { avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS); avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_FUSE_READ, 15); @@ -1566,7 +1566,7 @@ static int avrftdi_jtag_read_byte(const PROGRAMMER *pgm, const AVRPART *p, avrftdi_jtag_dr_out(pgm, 0x3600, 15); *value = avrftdi_jtag_dr_inout(pgm, 0x3700, 15) & 0xff; - } else if (str_eq(m->desc, "signature")) { + } else if (mem_is_signature(m)) { avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS); avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_SIGCAL_READ, 15); avrftdi_jtag_dr_out(pgm, 0x0300 | (addr & 0xff), 15); @@ -1575,7 +1575,7 @@ static int avrftdi_jtag_read_byte(const PROGRAMMER *pgm, const AVRPART *p, avrftdi_jtag_dr_out(pgm, 0x3200, 15); *value = avrftdi_jtag_dr_inout(pgm, 0x3300, 15) & 0xff; - } else if (str_eq(m->desc, "calibration")) { + } else if (mem_is_calibration(m)) { avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS); avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_SIGCAL_READ, 15); avrftdi_jtag_dr_out(pgm, 0x0300 | (addr & 0xff), 15); @@ -1584,7 +1584,7 @@ static int avrftdi_jtag_read_byte(const PROGRAMMER *pgm, const AVRPART *p, avrftdi_jtag_dr_out(pgm, 0x3600, 15); *value = avrftdi_jtag_dr_inout(pgm, 0x3700, 15) & 0xff; - } else if (str_eq(m->desc, "prodsig")) { + } else if (mem_is_sigrow(m)) { avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS); avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_SIGCAL_READ, 15); avrftdi_jtag_dr_out(pgm, 0x0300 | (addr/2 & 0xff), 15); @@ -1603,7 +1603,7 @@ static int avrftdi_jtag_read_byte(const PROGRAMMER *pgm, const AVRPART *p, static int avrftdi_jtag_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m, unsigned long addr, unsigned char value) { - if (str_eq(m->desc, "lfuse")) { + if (mem_is_lfuse(m)) { avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS); avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_FUSE_WRITE, 15); @@ -1620,7 +1620,7 @@ static int avrftdi_jtag_write_byte(const PROGRAMMER *pgm, const AVRPART *p, while (!(avrftdi_jtag_dr_inout(pgm, 0x3300, 15) & 0x0200)) ; - } else if (str_eq(m->desc, "hfuse")) { + } else if (mem_is_hfuse(m)) { avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS); avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_FUSE_WRITE, 15); @@ -1637,7 +1637,7 @@ static int avrftdi_jtag_write_byte(const PROGRAMMER *pgm, const AVRPART *p, while (!(avrftdi_jtag_dr_inout(pgm, 0x3700, 15) & 0x0200)) ; - } else if (str_eq(m->desc, "efuse")) { + } else if (mem_is_efuse(m)) { avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS); avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_FUSE_WRITE, 15); @@ -1654,7 +1654,7 @@ static int avrftdi_jtag_write_byte(const PROGRAMMER *pgm, const AVRPART *p, while (!(avrftdi_jtag_dr_inout(pgm, 0x3b00, 15) & 0x0200)) ; - } else if (str_eq(m->desc, "lock")) { + } else if (mem_is_lock(m)) { avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS); avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_LOCK_WRITE, 15); @@ -1685,7 +1685,7 @@ static int avrftdi_jtag_paged_write(const PROGRAMMER *pgm, const AVRPART *p, unsigned int maxaddr = addr + n_bytes; unsigned char byte; - if (str_eq(m->desc, "flash")) { + if (mem_is_flash(m)) { avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS); avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_FLASH_WRITE, 15); @@ -1712,7 +1712,7 @@ static int avrftdi_jtag_paged_write(const PROGRAMMER *pgm, const AVRPART *p, while (!(avrftdi_jtag_dr_inout(pgm, 0x3700, 15) & 0x0200)) ; - } else if (str_eq(m->desc, "eeprom")) { + } else if (mem_is_eeprom(m)) { avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS); avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_EEPROM_WRITE, 15); @@ -1760,7 +1760,7 @@ static int avrftdi_jtag_paged_read(const PROGRAMMER *pgm, const AVRPART *p, buf = alloca(n_bytes * 8 + 1); ptr = buf; - if (str_eq(m->desc, "flash")) { + if (mem_is_flash(m)) { avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS); avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_FLASH_READ, 15); @@ -1800,7 +1800,7 @@ static int avrftdi_jtag_paged_read(const PROGRAMMER *pgm, const AVRPART *p, m->buf[addr + i] = (buf[i * 2] >> 1) | (buf[(i * 2) + 1] << 2); } - } else if (str_eq(m->desc, "eeprom")) { + } else if (mem_is_eeprom(m)) { avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS); avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_EEPROM_READ, 15); diff --git a/src/avrintel.h b/src/avrintel.h index d340f7d2d..fd749c7c3 100644 --- a/src/avrintel.h +++ b/src/avrintel.h @@ -26,7 +26,7 @@ typedef struct { const char *name; // Name of this configuration item int nvalues; // Number of (symbolic) values const Valueitem_t *vlist; // Pointer to nvalues value items - const char *memtype; // Fuse/Lock memory for this configuration + const char *memstr; // Fuse/Lock memory for this configuration int memoffset; // Byte offset within fuses (always 0 for lock) int mask; // Bit mask of fuse/lock memory int lsh; // Values need shifting left by lsh to be in mask diff --git a/src/avrpart.c b/src/avrpart.c index 399992671..f1cb04122 100644 --- a/src/avrpart.c +++ b/src/avrpart.c @@ -137,7 +137,7 @@ int avr_set_addr_mem(const AVRMEM *mem, int opnum, unsigned char *cmd, unsigned if(!(op = mem->op[opnum])) return -1; - isflash = avr_mem_is_flash_type(mem); + isflash = mem_is_in_flash(mem); memsize = mem->size >> isflash; // word addresses for flash pagesize = mem->page_size >> isflash; @@ -327,8 +327,8 @@ static char * bittype(int type) *** Elementary functions dealing with AVRMEM structures ***/ -AVRMEM *avr_new_memtype(void) { - AVRMEM *m = (AVRMEM *) cfg_malloc("avr_new_memtype()", sizeof(*m)); +AVRMEM *avr_new_mem(void) { + AVRMEM *m = (AVRMEM *) cfg_malloc("avr_new_mem()", sizeof(*m)); m->desc = cache_string(""); m->page_size = 1; // Ensure not 0 m->initval = -1; // Unknown value represented as -1 @@ -363,7 +363,7 @@ int avr_initmem(const AVRPART *p) { AVRMEM *avr_dup_mem(const AVRMEM *m) { - AVRMEM *n = avr_new_memtype(); + AVRMEM *n = avr_new_mem(); if(m) { *n = *m; @@ -421,20 +421,20 @@ void avr_free_memalias(AVRMEM_ALIAS *m) { } AVRMEM_ALIAS *avr_locate_memalias(const AVRPART *p, const char *desc) { - AVRMEM_ALIAS * m, * match; + AVRMEM_ALIAS *m, *match; LNODEID ln; - int matches; + int matches, d1; size_t l; - if(!p || !desc || !p->mem_alias) + if(!p || !desc || !(d1 = *desc) || !p->mem_alias) return NULL; l = strlen(desc); matches = 0; match = NULL; - for (ln=lfirst(p->mem_alias); ln; ln=lnext(ln)) { + for(ln=lfirst(p->mem_alias); ln; ln=lnext(ln)) { m = ldata(ln); - if(l && str_starts(m->desc, desc)) { // Partial initial match + if(d1 == *m->desc && !strncmp(m->desc, desc, l)) { // Partial initial match match = m; matches++; if(m->desc[l] == 0) // Exact match; return straight away @@ -446,20 +446,20 @@ AVRMEM_ALIAS *avr_locate_memalias(const AVRPART *p, const char *desc) { } AVRMEM *avr_locate_mem_noalias(const AVRPART *p, const char *desc) { - AVRMEM * m, * match; + AVRMEM *m, *match; LNODEID ln; - int matches; + int matches, d1; size_t l; - if(!p || !desc || !p->mem) + if(!p || !desc || !(d1 = *desc) || !p->mem) return NULL; l = strlen(desc); matches = 0; match = NULL; - for (ln=lfirst(p->mem); ln; ln=lnext(ln)) { + for(ln=lfirst(p->mem); ln; ln=lnext(ln)) { m = ldata(ln); - if(l && str_starts(m->desc, desc)) { // Partial initial match + if(d1 == *m->desc && !strncmp(m->desc, desc, l)) { // Partial initial match match = m; matches++; if(m->desc[l] == 0) // Exact match; return straight away @@ -482,6 +482,42 @@ AVRMEM *avr_locate_mem(const AVRPART *p, const char *desc) { return a? a->aliased_mem: NULL; } +// Return the first fuse which has off as offset or which has high byte and off-1 as offset +AVRMEM *avr_locate_fuse_by_offset(const AVRPART *p, unsigned int off) { + AVRMEM *m; + + if(p && p->mem) + for(LNODEID ln=lfirst(p->mem); ln; ln=lnext(ln)) + if(mem_is_a_fuse(m = ldata(ln))) + if(off == mem_fuse_offset(m) || (m->size == 2 && off-1 == mem_fuse_offset(m))) + return m; + + return NULL; +} + +// Return the first memory that shares the type incl any fuse identified by offset in fuses +AVRMEM *avr_locate_mem_by_type(const AVRPART *p, memtype_t type) { + AVRMEM *m; + memtype_t off = type & MEM_FUSEOFF_MASK; + type &= ~(memtype_t) MEM_FUSEOFF_MASK; + + if(p && p->mem) + for(LNODEID ln=lfirst(p->mem); ln; ln=lnext(ln)) + if((m = ldata(ln))->type & type) + if(type != MEM_IS_A_FUSE || off == mem_fuse_offset(m)) + return m; + + return NULL; +} + +// Return offset of memory data +unsigned int avr_data_offset(const AVRPART *p) { + AVRMEM *data = avr_locate_data(p); + if(!data) + pmsg_warning("cannot locate data memory; is avrdude.conf up to date?\n"); + return data? data->offset: 0; +} + AVRMEM_ALIAS *avr_find_memalias(const AVRPART *p, const AVRMEM *m_orig) { if(p && p->mem_alias && m_orig) for(LNODEID ln=lfirst(p->mem_alias); ln; ln=lnext(ln)) { @@ -493,7 +529,6 @@ AVRMEM_ALIAS *avr_find_memalias(const AVRPART *p, const AVRMEM *m_orig) { return NULL; } - void avr_mem_display(const char *prefix, FILE *f, const AVRMEM *m, const AVRPART *p, int verbose) { static unsigned int prev_mem_offset; diff --git a/src/bitbang.c b/src/bitbang.c index 680b3ca2b..13de7016e 100644 --- a/src/bitbang.c +++ b/src/bitbang.c @@ -425,7 +425,7 @@ int bitbang_chip_erase(const PROGRAMMER *pgm, const AVRPART *p) { bitbang_tpi_tx(pgm, TPI_NVMCMD_CHIP_ERASE); /* CHIP_ERASE */ /* Set Pointer Register */ - mem = avr_locate_mem(p, "flash"); + mem = avr_locate_flash(p); if (mem == NULL) { pmsg_error("no flash memory to erase for part %s\n", p->desc); return -1; diff --git a/src/buspirate.c b/src/buspirate.c index 69181b757..17089cac9 100644 --- a/src/buspirate.c +++ b/src/buspirate.c @@ -902,7 +902,7 @@ static int buspirate_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const A } // determine what type of memory to read, only flash is supported - if (!str_eq(m->desc, "flash")) { + if (!mem_is_flash(m)) { return -1; } diff --git a/src/butterfly.c b/src/butterfly.c index a8b69df7b..49ee5a5d0 100644 --- a/src/butterfly.c +++ b/src/butterfly.c @@ -119,7 +119,7 @@ static int butterfly_default_led(const PROGRAMMER *pgm, int value) { */ static int butterfly_chip_erase(const PROGRAMMER *pgm, const AVRPART *p) { long bak_timeout = serial_recv_timeout; - AVRMEM *fl = avr_locate_mem(p, "flash"); + AVRMEM *fl = avr_locate_flash(p); int ret = 0; // Estimated time it takes to erase all pages in bootloader @@ -434,7 +434,7 @@ static int butterfly_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const A int size; int use_ext_addr = m->op[AVR_OP_LOAD_EXT_ADDR] != NULL; - if (str_eq(m->desc, "flash") || str_eq(m->desc, "eeprom")) + if (mem_is_flash(m) || mem_is_eeprom(m)) { cmd[0] = 'B'; cmd[1] = 0; @@ -454,7 +454,7 @@ static int butterfly_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const A butterfly_set_addr(pgm, addr); } } - else if (str_eq(m->desc, "lock")) + else if (mem_is_lock(m)) { cmd[0] = 'l'; cmd[1] = value; @@ -493,9 +493,9 @@ static int butterfly_read_byte_flash(const PROGRAMMER *pgm, const AVRPART *p, co } // Defaults to flash read ('F') char msg[4] = {'g', 0x00, 0x02, 'F'}; - if (str_eq(m->desc, "prodsig")) + if (mem_is_sigrow(m)) msg[3] = 'P'; - else if (str_eq(m->desc, "usersig")) + else if (mem_is_userrow(m)) msg[3] = 'U'; butterfly_send(pgm, msg, 4); /* Read back the program mem word (MSB first) */ @@ -530,26 +530,24 @@ static int butterfly_read_byte(const PROGRAMMER *pgm, const AVRPART *p, const AV { char cmd; - if (str_eq(m->desc, "flash") || - str_eq(m->desc, "prodsig") || - str_eq(m->desc, "usersig")) { + if (mem_is_flash(m) || mem_is_sigrow(m) || mem_is_userrow(m)) { return butterfly_read_byte_flash(pgm, p, m, addr, value); } - if (str_eq(m->desc, "eeprom")) { + if (mem_is_eeprom(m)) { return butterfly_read_byte_eeprom(pgm, p, m, addr, value); } - if (str_eq(m->desc, "lfuse")) { + if (mem_is_lfuse(m)) { cmd = 'F'; } - else if (str_eq(m->desc, "hfuse")) { + else if (mem_is_hfuse(m)) { cmd = 'N'; } - else if (str_eq(m->desc, "efuse")) { + else if (mem_is_efuse(m)) { cmd = 'Q'; } - else if (str_eq(m->desc, "lock")) { + else if (mem_is_lock(m)) { cmd = 'r'; } else @@ -573,9 +571,7 @@ static int butterfly_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const int use_ext_addr = m->op[AVR_OP_LOAD_EXT_ADDR] != NULL; unsigned int wr_size = 2; - if (!str_eq(m->desc, "flash") && - !str_eq(m->desc, "eeprom") && - !str_eq(m->desc, "usersig")) + if (!mem_is_flash(m) && !mem_is_eeprom(m) && !mem_is_userrow(m)) return -2; if (m->desc[0] == 'e') @@ -629,10 +625,8 @@ static int butterfly_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const A int blocksize = PDATA(pgm)->buffersize; int use_ext_addr = m->op[AVR_OP_LOAD_EXT_ADDR] != NULL; - /* check parameter syntax: only "flash", "eeprom" or "usersig" is allowed */ - if (!str_eq(m->desc, "flash") && - !str_eq(m->desc, "eeprom") && - !str_eq(m->desc, "usersig")) + /* check parameter syntax: only "flash", "eeprom" or "usersig"/"userrow" is allowed */ + if (!mem_is_flash(m) && !mem_is_eeprom(m) && !mem_is_userrow(m)) return -2; if (m->desc[0] == 'e') diff --git a/src/ch341a.c b/src/ch341a.c index 4c36473ca..6bfbc77f3 100644 --- a/src/ch341a.c +++ b/src/ch341a.c @@ -354,10 +354,10 @@ static int ch341a_spi_chip_erase(const struct programmer_t *pgm, const AVRPART * static int ch341a_spi_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m, unsigned int page_size, unsigned int addr, unsigned int n_bytes) { - int isflash = avr_mem_is_flash_type(m); + int isflash = mem_is_in_flash(m); if(n_bytes) { - if(!isflash && !avr_mem_is_eeprom_type(m)) + if(!isflash && !mem_is_eeprom(m)) return -2; // Always called with addr at page boundary and n_bytes == m->page_size @@ -376,10 +376,10 @@ static int ch341a_spi_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const static int ch341a_spi_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m, unsigned int page_size, unsigned int addr, unsigned int n_bytes) { - int isflash = avr_mem_is_flash_type(m); + int isflash = mem_is_in_flash(m); if(n_bytes) { - if(!isflash && !avr_mem_is_eeprom_type(m)) + if(!isflash && !mem_is_eeprom(m)) return -2; // Always called with addr at page boundary and n_bytes == m->page_size diff --git a/src/config.c b/src/config.c index 6b401d8e1..90fdc59cb 100644 --- a/src/config.c +++ b/src/config.c @@ -956,7 +956,7 @@ void cfg_update_mcuid(AVRPART *part) { for(size_t i=0; i < sizeof uP_table/sizeof *uP_table; i++) if(part->mcuid == (int) uP_table[i].mcuid) { // Complain unless it can be considered a variant, eg, ATmega32L and ATmega32 - AVRMEM *flash = avr_locate_mem(part, "flash"); + AVRMEM *flash = avr_locate_flash(part); if(flash) { size_t l1 = strlen(part->desc), l2 = strlen(uP_table[i].name); if(strncasecmp(part->desc, uP_table[i].name, l1 < l2? l1: l2) || diff --git a/src/config_gram.y b/src/config_gram.y index 310e2e85c..9b211de5b 100644 --- a/src/config_gram.y +++ b/src/config_gram.y @@ -994,11 +994,11 @@ part_parm : { /* select memory for extension or create if not there */ AVRMEM *mem = avr_locate_mem_noalias(current_part, $2->value.string); if(!mem) { - mem = avr_new_memtype(); + mem = avr_new_mem(); mem->desc = cache_string($2->value.string); ladd(current_part->mem, mem); + mem->type = avr_get_mem_type($2->value.string); } - avr_add_mem_order($2->value.string); current_mem = mem; free_token($2); } @@ -1362,11 +1362,11 @@ static int parse_cmdbits(OPCODE * op, int opnum) case 'a': sb = opnum == AVR_OP_LOAD_EXT_ADDR? bitno+8: bitno-8; // should be this number if(bitno < 8 || bitno > 23) { - if(!current_mem || !str_eq(current_mem->desc, "prodsig")) // Known exemption + if(!current_mem || !mem_is_sigrow(current_mem)) // Known exemption yywarning("address bits don't normally appear in Bytes 0 or 3 of SPI commands"); } else if((bn & 31) != sb) { if(!current_part || !str_casestarts(current_part->desc, "AT89S5")) // Exempt AT89S5x - if(!current_mem || !str_eq(current_mem->desc, "prodsig")) // and prodsig + if(!current_mem || !mem_is_sigrow(current_mem)) // and prodsig yywarning("a%d would normally be expected to be a%d", bn, sb); } else if(bn < 0 || bn > 31) yywarning("invalid address bit a%d, using a%d", bn, bn & 31); diff --git a/src/developer_opts.c b/src/developer_opts.c index 98c52028b..0db2866dd 100644 --- a/src/developer_opts.c +++ b/src/developer_opts.c @@ -492,8 +492,8 @@ static int avrpart_deep_copy(AVRPARTdeep *d, const AVRPART *p) { // Fill in all memories we got in defined order di = 0; - for(size_t mi=0; mi < sizeof avr_mem_order/sizeof *avr_mem_order && avr_mem_order[mi]; mi++) { - m = dev_locate_mem(p, avr_mem_order[mi]); + for(size_t mi=0; mi < sizeof avr_mem_order/sizeof *avr_mem_order && avr_mem_order[mi].str; mi++) { + m = dev_locate_mem(p, avr_mem_order[mi].str); if(m) { if(di >= sizeof d->mems/sizeof *d->mems) { pmsg_error("ran out of mems[] space, increase size in AVRMEMdeep of developer_opts.c and recompile\n"); @@ -730,11 +730,11 @@ static void dev_part_strct(const AVRPART *p, bool tsv, const AVRPART *base, bool if(!base || opcodecmp(p->op[i], base->op[i], i)) dev_part_strct_entry(tsv, ".ptop", p->desc, "part", opcodename(i), opcode2str(p->op[i], i, !tsv), p->comments); - for(size_t mi=0; mi < sizeof avr_mem_order/sizeof *avr_mem_order && avr_mem_order[mi]; mi++) { + for(size_t mi=0; mi < sizeof avr_mem_order/sizeof *avr_mem_order && avr_mem_order[mi].str; mi++) { AVRMEM *m, *bm; - m = dev_locate_mem(p, avr_mem_order[mi]); - bm = base? dev_locate_mem(base, avr_mem_order[mi]): NULL; + m = dev_locate_mem(p, avr_mem_order[mi].str); + bm = base? dev_locate_mem(base, avr_mem_order[mi].str): NULL; if(!m && bm && !tsv) dev_info("\n memory \"%s\" %*s= NULL;\n", bm->desc, 13 > strlen(bm->desc)? 13-strlen(bm->desc): 0, ""); @@ -743,7 +743,7 @@ static void dev_part_strct(const AVRPART *p, bool tsv, const AVRPART *base, bool continue; if(base && !bm) - bm = avr_new_memtype(); + bm = avr_new_mem(); if(!tsv) { if(!memorycmp(bm, m)) { // Same memory bit for bit, only instantiate on injected parameters @@ -941,12 +941,12 @@ void dev_output_part_defs(char *partdesc) { AVRPART *p = ldata(ln1); if(p->mem) for(LNODEID lnm=lfirst(p->mem); lnm; lnm=lnext(lnm)) - avr_add_mem_order(((AVRMEM *) ldata(lnm))->desc); + avr_get_mem_type(((AVRMEM *) ldata(lnm))->desc); // Same for aliased memories (though probably not needed) if(p->mem_alias) for(LNODEID lnm=lfirst(p->mem_alias); lnm; lnm=lnext(lnm)) - avr_add_mem_order(((AVRMEM_ALIAS *) ldata(lnm))->desc); + avr_get_mem_type(((AVRMEM_ALIAS *) ldata(lnm))->desc); } if((nprinted = dev_nprinted)) { @@ -982,12 +982,12 @@ void dev_output_part_defs(char *partdesc) { if(p->mem) { for(LNODEID lnm=lfirst(p->mem); lnm; lnm=lnext(lnm)) { AVRMEM *m = ldata(lnm); - if(!flashsize && str_eq(m->desc, "flash")) { + if(!flashsize && mem_is_flash(m)) { flashsize = m->size; flashpagesize = m->page_size; flashoffset = m->offset; } - if(!eepromsize && str_eq(m->desc, "eeprom")) { + if(!eepromsize && mem_is_eeprom(m)) { eepromsize = m->size; eepromoffset = m->offset; eeprompagesize = m->page_size; @@ -1010,7 +1010,7 @@ void dev_output_part_defs(char *partdesc) { if(!p->op[AVR_OP_CHIP_ERASE]) ok &= ~DEV_SPI_EN_CE_SIG; - if((m = avr_locate_mem(p, "flash"))) { + if((m = avr_locate_flash(p))) { if((oc = m->op[AVR_OP_LOAD_EXT_ADDR])) { // @@@ to do: check whether address is put at lsb of third byte } else @@ -1060,7 +1060,7 @@ void dev_output_part_defs(char *partdesc) { } else ok &= ~(DEV_SPI_PROGMEM_PAGED | DEV_SPI_PROGMEM); - if((m = avr_locate_mem(p, "eeprom"))) { + if((m = avr_locate_eeprom(p))) { if((oc = m->op[AVR_OP_READ])) { if(cmdok) checkaddr(m->size, 1, AVR_OP_READ, oc, p, m); @@ -1087,33 +1087,33 @@ void dev_output_part_defs(char *partdesc) { } else ok &= ~(DEV_SPI_EEPROM_PAGED | DEV_SPI_EEPROM); - if((m = avr_locate_mem(p, "signature")) && (oc = m->op[AVR_OP_READ])) { + if((m = avr_locate_signature(p)) && (oc = m->op[AVR_OP_READ])) { if(cmdok) checkaddr(m->size, 1, AVR_OP_READ, oc, p, m); } else ok &= ~DEV_SPI_EN_CE_SIG; - if((m = avr_locate_mem(p, "calibration")) && (oc = m->op[AVR_OP_READ])) { + if((m = avr_locate_calibration(p)) && (oc = m->op[AVR_OP_READ])) { if(cmdok) checkaddr(m->size, 1, AVR_OP_READ, oc, p, m); } else ok &= ~DEV_SPI_CALIBRATION; // Actually, some AT90S... parts cannot read, only write lock bits :-0 - if( ! ((m = avr_locate_mem(p, "lock")) && m->op[AVR_OP_WRITE])) + if( !((m = avr_locate_lock(p)) && m->op[AVR_OP_WRITE])) ok &= ~DEV_SPI_LOCK; - if(((m = avr_locate_mem(p, "fuse")) || (m = avr_locate_mem(p, "lfuse"))) && m->op[AVR_OP_READ] && m->op[AVR_OP_WRITE]) + if((m = avr_locate_fuse(p)) && m->op[AVR_OP_READ] && m->op[AVR_OP_WRITE]) nfuses++; else ok &= ~DEV_SPI_LFUSE; - if((m = avr_locate_mem(p, "hfuse")) && m->op[AVR_OP_READ] && m->op[AVR_OP_WRITE]) + if((m = avr_locate_hfuse(p)) && m->op[AVR_OP_READ] && m->op[AVR_OP_WRITE]) nfuses++; else ok &= ~DEV_SPI_HFUSE; - if((m = avr_locate_mem(p, "efuse")) && m->op[AVR_OP_READ] && m->op[AVR_OP_WRITE]) + if((m = avr_locate_efuse(p)) && m->op[AVR_OP_READ] && m->op[AVR_OP_WRITE]) nfuses++; else ok &= ~DEV_SPI_EFUSE; @@ -1154,7 +1154,7 @@ void dev_output_part_defs(char *partdesc) { for(LNODEID lnm=lfirst(p->mem); lnm; lnm=lnext(lnm)) { AVRMEM *m = ldata(lnm); // Write delays not needed for read-only calibration and signature memories - if(!str_eq(m->desc, "calibration") && !str_eq(m->desc, "signature")) { + if(!mem_is_calibration(m) && !mem_is_signature(m)) { if(p->prog_modes & PM_ISP) { if(m->min_write_delay == m->max_write_delay) dev_info(".wd_%s %.3f ms %s\n", m->desc, m->min_write_delay/1000.0, p->desc); diff --git a/src/doc/avrdude.texi b/src/doc/avrdude.texi index 53b3c4f12..d4615d946 100644 --- a/src/doc/avrdude.texi +++ b/src/doc/avrdude.texi @@ -131,13 +131,13 @@ supplies a direct instruction mode allowing one to issue any programming instruction to the AVR chip regardless of whether AVRDUDE implements that specific feature of a particular chip. -AVRDUDE can be used effectively via the command line to read or write -all chip memory types (eeprom, flash, fuse bits, lock bits, signature -bytes) or via an interactive (terminal) mode. Using AVRDUDE from the -command line works well for programming the entire memory of the chip -from the contents of a file, while interactive mode is useful for -exploring memory contents, modifying individual bytes of eeprom, -programming fuse/lock bits, etc. +AVRDUDE can be used via the command line to read or write chip memories +(eeprom, flash, fuses, lock bits) and read memories such as signature or +calibration bytes; the same can be achieved via an interactive terminal +mode. Using AVRDUDE from the command line works well for programming the +entire memory of the chip from the contents of a file, while interactive mode +is useful for exploring memory contents, modifying individual bytes of +eeprom, programming fuse/lock bits, etc. @cindex Programmers supported @@ -806,7 +806,7 @@ Perform a memory operation when it is its turn in relation to other @code{-U} memory operations. The @var{memory} field specifies the memory type to operate on. Use the @option{-T part} option on the command line or the @code{part} command in the interactive terminal to display all the -memory types supported by a particular device. +memories supported by a particular device. Typically, a device's memory configuration at least contains the memory types @code{flash}, @code{eeprom}, @code{signature} and @code{lock}, which @@ -818,7 +818,7 @@ parts of it, is allowed. Parts will also typically have fuse bytes, which are read/write memories for configuration of the device and calibration memories that typically contain read-only factory calibration values. -Classic devices may have the following memory types in addition to +Classic devices may have the following memories in addition to @code{eeprom}, @code{flash}, @code{signature} and @code{lock}: @table @code @item calibration @@ -854,7 +854,7 @@ methods only by bootloaders, which has limited use unless the bootloader jumps to the application directly, i.e., without a WDT reset @end table -ATxmega devices have the following memory types in addition to +ATxmega devices have the following memories in addition to @code{eeprom}, @code{flash}, @code{signature} and @code{lock}: @table @code @item application @@ -887,7 +887,7 @@ Volatile register memory; AVRDUDE can read this memory but not write to it using external programming @end table -Modern 8-bit AVR devices have the following memory types in addition to +Modern 8-bit AVR devices have the following memories in addition to @code{eeprom}, @code{flash}, @code{signature} and @code{lock}: @table @code @item fuse0 @@ -2408,7 +2408,7 @@ Display the device signature bytes. @item part Display the current part settings and parameters. Includes chip -specific information including all memory types supported by the +specific information including all memories supported by the device, read/write timing, etc. @item verbose [@var{level}] @@ -3119,7 +3119,7 @@ part # parameters for bootloaders autobaud_sync = ; # autobaud detection byte, default 0x30 - memory + memory paged = ; # yes/no (flash only, do not use for EEPROM) offset = ; # memory offset size = ; # bytes @@ -3289,19 +3289,19 @@ Atmel's AVR061 application note available from @url{http://www.atmel.com/dyn/resources/prod_documents/doc2525.pdf}. @item -Not all memory types will implement all instructions. +Not all memories will implement all instructions. @item AVR Fuse bits and Lock bits are implemented as a type of memory. @item -Example memory types are: @code{flash}, @code{eeprom}, @code{fuse}, +Example memories are: @code{flash}, @code{eeprom}, @code{fuse}, @code{lfuse} (low fuse), @code{hfuse} (high fuse), @code{efuse} (extended fuse), @code{signature}, @code{calibration}, @code{lock}. @item -The memory type specified on the AVRDUDE command line must match one of -the memory types defined for the specified chip. +The memory specified on the AVRDUDE command line must match one of +the memories defined for the specified chip. @item The @code{pwroff_after_write} flag causes AVRDUDE to attempt to power diff --git a/src/dryrun.c b/src/dryrun.c index f13cf458f..472879b4a 100644 --- a/src/dryrun.c +++ b/src/dryrun.c @@ -73,7 +73,7 @@ static int dryrun_chip_erase(const PROGRAMMER *pgm, const AVRPART *punused) { pmsg_debug("%s()\n", __func__); if(!dry.dp) Return("no dryrun device? Raise an issue at https://github.com/avrdudes/avrdude/issues"); - if(!(flm = avr_locate_mem(dry.dp, "flash"))) + if(!(flm = avr_locate_flash(dry.dp))) Return("cannot locate %s flash memory for chip erase", dry.dp->desc); if(flm->size < 1) Return("cannot erase %s flash memory owing to its size %d", dry.dp->desc, flm->size); @@ -122,43 +122,42 @@ static void dryrun_enable(PROGRAMMER *pgm, const AVRPART *p) { // Initialise the device with fuse factory setting and erase flash/EEPROM to 0xff for (LNODEID ln=lfirst(dry.dp->mem); ln; ln=lnext(ln)) { AVRMEM *m = ldata(ln); - if(avr_mem_is_flash_type(m) || avr_mem_is_eeprom_type(m)) { + if(mem_is_in_flash(m) || mem_is_eeprom(m)) { memset(m->buf, 0xff, m->size); - } else if(str_eq(m->desc, "fuses")) { + } else if(mem_is_fuses(m)) { fusesm = m; - } else if(str_contains(m->desc, "fuse") || str_contains(m->desc, "lock")) { + } else if(mem_is_a_fuse(m) || mem_is_lock(m)) { // Lock, eg, can have 4 bytes: still allow initialisation from initval if(m->initval != -1 && m->size >=1 && m->size <= (int) sizeof(m->initval)) { memcpy(m->buf, &m->initval, m->size); // FIXME: relying on little endian here - if(str_starts(m->desc, "fuse") && m->desc[4] && isxdigit(0xff & m->desc[4]) && !m->desc[5]) { - int fno = strtol(m->desc+4, NULL, 16); - if(fno >= 0) - for(int i = 0; i < m->size && fno+i < (int) sizeof inifuses; i++) - inifuses[fno+i] = m->initval >> 8*i; + if(mem_is_a_fuse(m)) { + int fno = mem_fuse_offset(m); + for(int i = 0; i < m->size && fno+i < (int) sizeof inifuses; i++) // pdicfg has 2 bytes + inifuses[fno+i] = m->initval >> 8*i; } } else { memset(m->buf, 0xff, m->size); } - } else if(str_eq(m->desc, "signature") && (int) sizeof(dry.dp->signature) == m->size) { + } else if(mem_is_signature(m) && (int) sizeof(dry.dp->signature) == m->size) { memcpy(m->buf, dry.dp->signature, m->size); - } else if(str_eq(m->desc, "calibration")) { + } else if(mem_is_calibration(m)) { memset(m->buf, 'U', m->size); // 'U' for uncalibrated or unknown :) - } else if(str_eq(m->desc, "osc16err")) { + } else if(mem_is_osc16err(m)) { memset(m->buf, 'e', m->size); - } else if(str_eq(m->desc, "osc20err")) { + } else if(mem_is_osc20err(m)) { memset(m->buf, 'E', m->size); - } else if(str_eq(m->desc, "osccal16")) { + } else if(mem_is_osccal16(m)) { memset(m->buf, 'o', m->size); - } else if(str_eq(m->desc, "osccal20")) { + } else if(mem_is_osccal20(m)) { memset(m->buf, 'O', m->size); - } else if(str_eq(m->desc, "sib")) { + } else if(mem_is_sib(m)) { memset(m->buf, 'S', m->size); - } else if( str_eq(m->desc, "tempsense")) { + } else if( mem_is_tempsense(m)) { memset(m->buf, 'T', m->size); // 'T' for temperature calibration values - } else if(str_eq(m->desc, "sernum")) { + } else if(mem_is_sernum(m)) { for(int i = 0; i < m->size; i++) // Set serial number UTSRQPONM... m->buf[i] = 'U'-i >= 'A'? 'U'-i: 0xff; - } else if(str_eq(m->desc, "prodsig") && m->size >= 6) { + } else if(mem_is_sigrow(m) && m->size >= 6) { prodsigm = m; memset(m->buf, 0xff, m->size); if(p->prog_modes & PM_PDI) { @@ -182,7 +181,7 @@ static void dryrun_enable(PROGRAMMER *pgm, const AVRPART *p) { memcpy(prodsigm->buf + off, m->buf, cpy); } } - if(!(p->prog_modes & (PM_PDI|PM_UPDI)) && (calm = avr_locate_mem(dry.dp, "calibration"))) { + if(!(p->prog_modes & (PM_PDI|PM_UPDI)) && (calm = avr_locate_calibration(dry.dp))) { // Calibration bytes of classic parts are interspersed with signature for(int i=0; isize; i++) if(2*i+1 < prodsigm->size) @@ -264,8 +263,8 @@ static int dryrun_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AVR unsigned int end; // Paged writes only valid for flash and eeprom - mchr = avr_mem_is_flash_type(m)? 'F': 'E'; - if(mchr == 'E' && !avr_mem_is_eeprom_type(m)) + mchr = mem_is_in_flash(m)? 'F': 'E'; + if(mchr == 'E' && !mem_is_eeprom(m)) return -2; if(!(dmem = avr_locate_mem(dry.dp, m->desc))) @@ -287,16 +286,16 @@ static int dryrun_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AVR // Copy chunk to overlapping XMEGA's apptable, application, boot and flash memories if(mchr == 'F') { - if(str_eq(dmem->desc, "flash")) { + if(mem_is_flash(dmem)) { for(LNODEID ln=lfirst(dry.dp->mem); ln; ln=lnext(ln)) { dm2 = ldata(ln); - if(avr_mem_is_flash_type(dm2) && !str_eq(dm2->desc, "flash")) { // Overlapping region? + if(mem_is_in_flash(dm2) && !mem_is_flash(dm2)) { // Overlapping region? unsigned int cpaddr = addr + dmem->offset - dm2->offset; if(cpaddr < (unsigned int) dm2->size && cpaddr + chunk <= (unsigned int) dm2->size) memcpy(dm2->buf+cpaddr, dmem->buf+addr, chunk); } } - } else if((dm2 = avr_locate_mem(dry.dp, "flash"))) { + } else if((dm2 = avr_locate_flash(dry.dp))) { unsigned int cpaddr = addr + dmem->offset - dm2->offset; if(cpaddr < (unsigned int) dm2->size && cpaddr + chunk <= (unsigned int) dm2->size) memcpy(dm2->buf+cpaddr, dmem->buf+addr, chunk); @@ -322,8 +321,8 @@ static int dryrun_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AVRM unsigned int end; // Paged load only valid for flash and eeprom - mchr = avr_mem_is_flash_type(m)? 'F': 'E'; - if(mchr == 'E' && !avr_mem_is_eeprom_type(m)) + mchr = mem_is_in_flash(m)? 'F': 'E'; + if(mchr == 'E' && !mem_is_eeprom(m)) return -2; if(!(dmem = avr_locate_mem(dry.dp, m->desc))) @@ -364,12 +363,13 @@ int dryrun_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m, if(dmem->size != m->size) Return("cannot write byte to %s %s as sizes differ: 0x%04x vs 0x%04x", dry.dp->desc, dmem->desc, dmem->size, m->size); - if(str_eq(dmem->desc, "calibration") || str_eq(dmem->desc, "osc16err") || - str_eq(dmem->desc, "osccal16") || str_eq(dmem->desc, "osc20err") || - str_eq(dmem->desc, "osccal20") || str_eq(dmem->desc, "prodsig") || - str_eq(dmem->desc, "sernum") || str_eq(dmem->desc, "sib") || - str_eq(dmem->desc, "signature") || str_eq(dmem->desc, "tempsense")) + if(mem_is_readonly(dmem)) { + unsigned char is; + if(pgm->read_byte(pgm, p, m, addr, &is) >= 0 && is == data) + return 0; + Return("cannot write to write-protected memory %s %s", dry.dp->desc, dmem->desc); + } if(addr >= (unsigned long) dmem->size) Return("cannot write byte to %s %s as address 0x%04lx outside range [0, 0x%04x]", @@ -383,22 +383,19 @@ int dryrun_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m, dmem->buf[addr] = data; - if(str_eq(dmem->desc, "fuses") && addr < 16) { // Copy the byte to corresponding fuse[0-9a-f] - char memtype[64]; - sprintf(memtype, "fuse%lx", addr); - if((dfuse = avr_locate_mem(dry.dp, memtype))) - dfuse->buf[0] = data; - else if(addr > 0) { // Could be high byte of two-byte fuse - sprintf(memtype, "fuse%lx", addr-1); - if((dfuse = avr_locate_mem(dry.dp, memtype))) - dfuse->buf[1] = data; + if(mem_is_fuses(dmem) && addr < 16) { // Copy the byte to corresponding individual fuse + for(LNODEID ln=lfirst(dry.dp->mem); ln; ln=lnext(ln)) { + if(mem_is_a_fuse(dfuse = ldata(ln))) { + if(addr == mem_fuse_offset(dfuse)) + dfuse->buf[0] = data; + else if(dfuse->size == 2 && addr-1 == mem_fuse_offset(dfuse)) // High byte of 2-byte fuse + dfuse->buf[1] = data; + } } - } else if(str_starts(m->desc, "fuse") && m->desc[4] && isxdigit(0xff & m->desc[4]) && !m->desc[5]) { - // Copy fuseX byte into fuses memory - int fno = strtol(m->desc+4, NULL, 16); - if(fno >= 0) - if((dfuse = avr_locate_mem(dry.dp, "fuses")) && (int) (fno+addr) < dfuse->size) - dfuse->buf[fno+addr] = data; + } else if(mem_is_a_fuse(m) && (dfuse = avr_locate_fuses(dry.dp))) { // Copy fuse to fuses + int fidx = addr + mem_fuse_offset(m); + if(fidx >=0 && fidx < dfuse->size) + dfuse->buf[fidx] = data; } return 0; diff --git a/src/fileio.c b/src/fileio.c index f29cd1c73..47f54a8f0 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -759,7 +759,7 @@ static int elf_mem_limits(const AVRMEM *mem, const AVRPART *p, int rv = 0; if (p->prog_modes & PM_aWire) { // AVR32 - if (str_eq(mem->desc, "flash")) { + if (mem_is_flash(mem)) { *lowbound = 0x80000000; *highbound = 0xffffffff; *fileoff = 0; @@ -767,48 +767,31 @@ static int elf_mem_limits(const AVRMEM *mem, const AVRPART *p, rv = -1; } } else { - if (str_eq(mem->desc, "flash") || - str_eq(mem->desc, "boot") || - str_eq(mem->desc, "application") || - str_eq(mem->desc, "apptable")) { + if (mem_is_in_flash(mem)) { *lowbound = 0; *highbound = 0x7Fffff; // Max 8 MiB *fileoff = 0; - } else if (str_eq(mem->desc, "data")) { // SRAM for XMEGAs + } else if (mem_is_data(mem)) { // SRAM for XMEGAs *lowbound = 0x802000; *highbound = 0x80ffff; *fileoff = 0; - } else if (str_eq(mem->desc, "eeprom")) { + } else if (mem_is_eeprom(mem)) { *lowbound = 0x810000; *highbound = 0x81ffff; // Max 64 KiB *fileoff = 0; - } else if (str_eq(mem->desc, "lfuse") || str_eq(mem->desc, "fuse") || str_eq(mem->desc, "fuses")) { + } else if (mem_is_a_fuse(mem) || mem_is_fuses(mem)) { *lowbound = 0x820000; *highbound = 0x82ffff; - *fileoff = 0; - } else if (str_eq(mem->desc, "hfuse")) { - *lowbound = 0x820000; - *highbound = 0x82ffff; - *fileoff = 1; - } else if (str_eq(mem->desc, "efuse")) { - *lowbound = 0x820000; - *highbound = 0x82ffff; - *fileoff = 2; - } else if (str_starts(mem->desc, "fuse") && - mem->desc[4] && isxdigit(0xff & mem->desc[4]) && !mem->desc[5]) { - /* Xmega or modern AVR fuseX */ - *lowbound = 0x820000; - *highbound = 0x82ffff; - *fileoff = strtol(mem->desc+4, NULL, 16); - } else if (str_starts(mem->desc, "lock")) { // Lock or lockbits + *fileoff = mem_is_a_fuse(mem)? mem_fuse_offset(mem): 0; + } else if (mem_is_lock(mem)) { // Lock or lockbits *lowbound = 0x830000; *highbound = 0x83ffff; *fileoff = 0; - } else if (str_eq(mem->desc, "signature")) { // Read only + } else if (mem_is_signature(mem)) { // Read only *lowbound = 0x840000; *highbound = 0x84ffff; *fileoff = 0; - } else if (str_starts(mem->desc, "user")) { // Usersig or userrow + } else if (mem_is_userrow(mem)) { // usersig or userrow *lowbound = 0x850000; *highbound = 0x85ffff; *fileoff = 0; @@ -841,11 +824,8 @@ static int elf2b(const char *infile, FILE *inf, const AVRMEM *mem, * sections out of an ELF file that contains section data for more * than one sub-segment. */ - if ((p->prog_modes & PM_PDI) != 0 && - (str_eq(mem->desc, "boot") || - str_eq(mem->desc, "application") || - str_eq(mem->desc, "apptable"))) { - AVRMEM *flashmem = avr_locate_mem(p, "flash"); + if ((p->prog_modes & PM_PDI) && mem_is_in_flash(mem) && !mem_is_flash(mem)) { + AVRMEM *flashmem = avr_locate_flash(p); if (flashmem == NULL) { pmsg_error("no flash memory region found, cannot compute bounds of %s sub-region\n", mem->desc); return -1; @@ -1432,11 +1412,11 @@ int fileio_fmt_autodetect(const char *fname) { int fileio(int op, const char *filename, FILEFMT format, - const AVRPART *p, const char *memtype, int size) { + const AVRPART *p, const char *memstr, int size) { - AVRMEM *mem = avr_locate_mem(p, memtype); + AVRMEM *mem = avr_locate_mem(p, memstr); if(mem == NULL) { - pmsg_error("memory type %s not configured for device %s\n", memtype, p->desc); + pmsg_error("memory %s not configured for device %s\n", memstr, p->desc); return -1; } diff --git a/src/flip1.c b/src/flip1.c index 69ecf3ad4..b3ca1e42c 100644 --- a/src/flip1.c +++ b/src/flip1.c @@ -92,7 +92,7 @@ struct flip1_cmd struct flip1_cmd_header /* for memory read/write */ { unsigned char cmd; - unsigned char memtype; + unsigned char memchr; unsigned char start_addr[2]; unsigned char end_addr[2]; unsigned char padding[26]; @@ -371,7 +371,7 @@ int flip1_read_byte(const PROGRAMMER *pgm, const AVRPART *part, const AVRMEM *me if (FLIP1(pgm)->dfu == NULL) return -1; - if (str_eq(mem->desc, "signature")) { + if (mem_is_signature(mem)) { if (flip1_read_sig_bytes(pgm, part, mem) < 0) return -1; if (addr >= (unsigned long) mem->size) { diff --git a/src/flip2.c b/src/flip2.c index 7baf6e86c..5ea2d488e 100644 --- a/src/flip2.c +++ b/src/flip2.c @@ -401,7 +401,7 @@ int flip2_read_byte(const PROGRAMMER *pgm, const AVRPART *part, const AVRMEM *me if (mem_unit == FLIP2_MEM_UNIT_UNKNOWN) { pmsg_error("%s memory not accessible using FLIP", mem->desc); - if (str_eq(mem->desc, "flash")) + if (mem_is_flash(mem)) msg_error(" (did you mean \"application\"?)"); msg_error("\n"); return -1; @@ -422,7 +422,7 @@ int flip2_write_byte(const PROGRAMMER *pgm, const AVRPART *part, const AVRMEM *m if (mem_unit == FLIP2_MEM_UNIT_UNKNOWN) { pmsg_error("%s memory not accessible using FLIP", mem->desc); - if (str_eq(mem->desc, "flash")) + if (mem_is_flash(mem)) msg_error(" (did you mean \"application\"?)"); msg_error("\n"); return -1; @@ -444,7 +444,7 @@ int flip2_paged_load(const PROGRAMMER *pgm, const AVRPART *part, const AVRMEM *m if (mem_unit == FLIP2_MEM_UNIT_UNKNOWN) { pmsg_error("%s memory not accessible using FLIP", mem->desc); - if (str_eq(mem->desc, "flash")) + if (mem_is_flash(mem)) msg_error(" (did you mean \"application\"?)"); msg_error("\n"); return -1; @@ -475,7 +475,7 @@ int flip2_paged_write(const PROGRAMMER *pgm, const AVRPART *part, const AVRMEM * if (mem_unit == FLIP2_MEM_UNIT_UNKNOWN) { pmsg_error("%s memory not accessible using FLIP", mem->desc); - if (str_eq(mem->desc, "flash")) + if (mem_is_flash(mem)) msg_error(" (did you mean \"application\"?)"); msg_error("\n"); return -1; diff --git a/src/ft245r.c b/src/ft245r.c index d07ae1fbe..3762feed2 100644 --- a/src/ft245r.c +++ b/src/ft245r.c @@ -1098,10 +1098,10 @@ static int ft245r_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AVR if(!n_bytes) return 0; - if(str_eq(m->desc, "flash")) + if(mem_is_flash(m)) return ft245r_paged_write_flash(pgm, p, m, page_size, addr, n_bytes); - if(str_eq(m->desc, "eeprom")) + if(mem_is_eeprom(m)) return ft245r_paged_write_gen(pgm, p, m, page_size, addr, n_bytes); return -2; @@ -1196,10 +1196,10 @@ static int ft245r_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AVRM if(!n_bytes) return 0; - if(str_eq(m->desc, "flash")) + if(mem_is_flash(m)) return ft245r_paged_load_flash(pgm, p, m, page_size, addr, n_bytes); - if(str_eq(m->desc, "eeprom")) + if(mem_is_eeprom(m)) return ft245r_paged_load_gen(pgm, p, m, page_size, addr, n_bytes); return -2; diff --git a/src/jtag3.c b/src/jtag3.c index db3ebfce5..ad1af1288 100644 --- a/src/jtag3.c +++ b/src/jtag3.c @@ -127,7 +127,7 @@ void jtag3_print_parms1(const PROGRAMMER *pgm, const char *p, FILE *fp); static int jtag3_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m, unsigned int page_size, unsigned int addr, unsigned int n_bytes); -static unsigned char jtag3_memtype(const PROGRAMMER *pgm, const AVRPART *p, unsigned long addr); +static unsigned char jtag3_mtype(const PROGRAMMER *pgm, const AVRPART *p, unsigned long addr); static unsigned int jtag3_memaddr(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m, unsigned long addr); @@ -1180,39 +1180,39 @@ static int jtag3_initialize(const PROGRAMMER *pgm, const AVRPART *p) { struct xmega_device_desc xd; LNODEID ln; AVRMEM * m; + int fuseinit = 0; u16_to_b2(xd.nvm_base_addr, p->nvm_base); u16_to_b2(xd.mcu_base_addr, p->mcu_base); for (ln = lfirst(p->mem); ln; ln = lnext(ln)) { m = ldata(ln); - if (str_eq(m->desc, "flash")) { + if (mem_is_flash(m)) { if (m->readsize != 0 && m->readsize < m->page_size) PDATA(pgm)->flash_pagesize = m->readsize; else PDATA(pgm)->flash_pagesize = m->page_size; u16_to_b2(xd.flash_page_size, m->page_size); - } else if (str_eq(m->desc, "eeprom")) { + } else if (mem_is_eeprom(m)) { PDATA(pgm)->eeprom_pagesize = m->page_size; xd.eeprom_page_size = m->page_size; u16_to_b2(xd.eeprom_size, m->size); u32_to_b4(xd.nvm_eeprom_offset, m->offset); - } else if (str_eq(m->desc, "application")) { + } else if (mem_is_application(m)) { u32_to_b4(xd.app_size, m->size); u32_to_b4(xd.nvm_app_offset, m->offset); - } else if (str_eq(m->desc, "boot")) { + } else if (mem_is_boot(m)) { u16_to_b2(xd.boot_size, m->size); u32_to_b4(xd.nvm_boot_offset, m->offset); - } else if (str_eq(m->desc, "fuse1")) { + } else if (mem_is_a_fuse(m) && !fuseinit++) { // Any fuse is OK u32_to_b4(xd.nvm_fuse_offset, m->offset & ~15); - } else if (str_starts(m->desc, "lock")) { + } else if (mem_is_lock(m)) { u32_to_b4(xd.nvm_lock_offset, m->offset); - } else if (str_eq(m->desc, "usersig") || - str_eq(m->desc, "userrow")) { + } else if (mem_is_userrow(m)) { u32_to_b4(xd.nvm_user_sig_offset, m->offset); - } else if (str_eq(m->desc, "prodsig")) { + } else if (mem_is_sigrow(m)) { u32_to_b4(xd.nvm_prod_sig_offset, m->offset); - } else if (str_eq(m->desc, "data")) { + } else if (mem_is_data(m)) { u32_to_b4(xd.nvm_data_offset, m->offset); } } @@ -1231,7 +1231,7 @@ static int jtag3_initialize(const PROGRAMMER *pgm, const AVRPART *p) { for (ln = lfirst(p->mem); ln; ln = lnext(ln)) { m = ldata(ln); - if (str_eq(m->desc, "flash")) { + if (mem_is_flash(m)) { u16_to_b2(xd.prog_base, m->offset&0xFFFF); xd.prog_base_msb = m->offset>>16; @@ -1249,28 +1249,27 @@ static int jtag3_initialize(const PROGRAMMER *pgm, const AVRPART *p) { else xd.address_mode = UPDI_ADDRESS_MODE_16BIT; } - else if (str_eq(m->desc, "eeprom")) { + else if (mem_is_eeprom(m)) { PDATA(pgm)->eeprom_pagesize = m->page_size; xd.eeprom_page_size = m->page_size; u16_to_b2(xd.eeprom_bytes, m->size); u16_to_b2(xd.eeprom_base, m->offset); } - else if (str_eq(m->desc, "usersig") || - str_eq(m->desc, "userrow")) { + else if (mem_is_userrow(m)) { u16_to_b2(xd.user_sig_bytes, m->size); u16_to_b2(xd.user_sig_base, m->offset); } - else if (str_eq(m->desc, "signature")) { + else if (mem_is_signature(m)) { u16_to_b2(xd.signature_base, m->offset); xd.device_id[0] = p->signature[1]; xd.device_id[1] = p->signature[2]; } - else if (str_eq(m->desc, "fuses")) { + else if (mem_is_fuses(m)) { xd.fuses_bytes = m->size; u16_to_b2(xd.fuses_base, m->offset); } - else if (str_eq(m->desc, "lock")) { + else if (mem_is_lock(m)) { u16_to_b2(xd.lockbits_base, m->offset); } } @@ -1338,7 +1337,7 @@ static int jtag3_initialize(const PROGRAMMER *pgm, const AVRPART *p) { for (ln = lfirst(p->mem); ln; ln = lnext(ln)) { m = ldata(ln); - if (str_eq(m->desc, "flash")) { + if (mem_is_flash(m)) { if (m->readsize != 0 && m->readsize < m->page_size) PDATA(pgm)->flash_pagesize = m->readsize; else @@ -1347,7 +1346,7 @@ static int jtag3_initialize(const PROGRAMMER *pgm, const AVRPART *p) { u32_to_b4(md.flash_size, (flashsize = m->size)); // do we need it? just a wild guess u32_to_b4(md.boot_address, (m->size - m->page_size * 4) / 2); - } else if (str_eq(m->desc, "eeprom")) { + } else if (mem_is_eeprom(m)) { PDATA(pgm)->eeprom_pagesize = m->page_size; md.eeprom_page_size = m->page_size; u16_to_b2(md.eeprom_size, m->size); @@ -1441,7 +1440,7 @@ static int jtag3_initialize(const PROGRAMMER *pgm, const AVRPART *p) { // Read chip silicon revision if(pgm->read_chip_rev && p->prog_modes & (PM_PDI | PM_UPDI)) { - char chip_rev[AVR_CHIP_REVLEN]; + unsigned char chip_rev[AVR_CHIP_REVLEN]; pgm->read_chip_rev(pgm, p, chip_rev); pmsg_notice("silicon revision: %x.%x\n", chip_rev[0] >> 4, chip_rev[0] & 0x0f); } @@ -1449,8 +1448,8 @@ static int jtag3_initialize(const PROGRAMMER *pgm, const AVRPART *p) { PDATA(pgm)->boot_start = ULONG_MAX; if (p->prog_modes & PM_PDI) { // Find the border between application and boot area - AVRMEM *bootmem = avr_locate_mem(p, "boot"); - AVRMEM *flashmem = avr_locate_mem(p, "flash"); + AVRMEM *bootmem = avr_locate_boot(p); + AVRMEM *flashmem = avr_locate_flash(p); if (bootmem == NULL || flashmem == NULL) { pmsg_error("cannot locate flash or boot memories in description\n"); } else { @@ -1492,7 +1491,7 @@ static void jtag3_disable(const PROGRAMMER *pgm) { static void jtag3_enable(PROGRAMMER *pgm, const AVRPART *p) { // Page erase only useful for classic parts with usersig mem or AVR8X/XMEGAs if(!(p->prog_modes & (PM_PDI | PM_UPDI))) - if(!avr_locate_mem(p, "usersig")) + if(!avr_locate_usersig(p)) pgm->page_erase = NULL; } @@ -1849,7 +1848,7 @@ static int jtag3_page_erase(const PROGRAMMER *pgm, const AVRPART *p, const AVRME pmsg_notice2("jtag3_page_erase(.., %s, 0x%x)\n", m->desc, addr); - if(!(p->prog_modes & (PM_PDI | PM_UPDI)) && !str_eq(m->desc, "usersig")) { + if(!(p->prog_modes & (PM_PDI | PM_UPDI)) && !mem_is_userrow(m)) { pmsg_error("page erase only available for AVR8X/XMEGAs or classic-part usersig mem\n"); return -1; } @@ -1861,15 +1860,14 @@ static int jtag3_page_erase(const PROGRAMMER *pgm, const AVRPART *p, const AVRME cmd[1] = CMD3_ERASE_MEMORY; cmd[2] = 0; - if (avr_mem_is_flash_type(m)) { - if (p->prog_modes & PM_UPDI || jtag3_memtype(pgm, p, addr) == MTYPE_FLASH) + if (mem_is_in_flash(m)) { + if (p->prog_modes & PM_UPDI || jtag3_mtype(pgm, p, addr) == MTYPE_FLASH) cmd[3] = XMEGA_ERASE_APP_PAGE; else cmd[3] = XMEGA_ERASE_BOOT_PAGE; - } else if (str_eq(m->desc, "eeprom")) { + } else if (mem_is_eeprom(m)) { cmd[3] = XMEGA_ERASE_EEPROM_PAGE; - } else if (str_eq(m->desc, "usersig") || - str_eq(m->desc, "userrow")) { + } else if (mem_is_userrow(m)) { cmd[3] = XMEGA_ERASE_USERSIG; } else { cmd[3] = XMEGA_ERASE_APP_PAGE; @@ -1896,7 +1894,7 @@ static int jtag3_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AVRM unsigned int maxaddr = addr + n_bytes; unsigned char *cmd; unsigned char *resp; - int status, dynamic_memtype = 0; + int status, dynamic_mtype = 0; long otimeout = serial_recv_timeout; pmsg_notice2("jtag3_paged_write(.., %s, %d, 0x%04x, %d)\n", m->desc, page_size, addr, n_bytes); @@ -1920,13 +1918,13 @@ static int jtag3_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AVRM cmd[0] = SCOPE_AVR; cmd[1] = CMD3_WRITE_MEMORY; cmd[2] = 0; - if (str_eq(m->desc, "flash")) { + if (mem_is_flash(m)) { PDATA(pgm)->flash_pageaddr = (unsigned long)-1L; - cmd[3] = jtag3_memtype(pgm, p, addr); + cmd[3] = jtag3_mtype(pgm, p, addr); if (p->prog_modes & PM_PDI) - /* dynamically decide between flash/boot memtype */ - dynamic_memtype = 1; - } else if (str_eq(m->desc, "eeprom")) { + /* dynamically decide between flash/boot mtype */ + dynamic_mtype = 1; + } else if (mem_is_eeprom(m)) { if (pgm->flag & PGM_FL_IS_DW) { /* * jtag3_paged_write() to EEPROM attempted while in @@ -1944,10 +1942,9 @@ static int jtag3_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AVRM } cmd[3] = p->prog_modes & (PM_PDI | PM_UPDI)? MTYPE_EEPROM_XMEGA: MTYPE_EEPROM_PAGE; PDATA(pgm)->eeprom_pageaddr = (unsigned long)-1L; - } else if (str_eq(m->desc, "usersig") || - str_eq(m->desc, "userrow")) { + } else if (mem_is_userrow(m)) { cmd[3] = MTYPE_USERSIG; - } else if (str_eq(m->desc, "boot")) { + } else if (mem_is_boot(m)) { cmd[3] = MTYPE_BOOT_FLASH; } else if (p->prog_modes & (PM_PDI | PM_UPDI)) { cmd[3] = MTYPE_FLASH; @@ -1963,8 +1960,8 @@ static int jtag3_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AVRM pmsg_debug("jtag3_paged_write(): " "block_size at addr %d is %d\n", addr, block_size); - if (dynamic_memtype) - cmd[3] = jtag3_memtype(pgm, p, addr); + if (dynamic_mtype) + cmd[3] = jtag3_mtype(pgm, p, addr); u32_to_b4(cmd + 8, page_size); u32_to_b4(cmd + 4, jtag3_memaddr(pgm, p, m, addr)); @@ -2003,7 +2000,7 @@ static int jtag3_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AVRME unsigned int maxaddr = addr + n_bytes; unsigned char cmd[12]; unsigned char *resp; - int status, dynamic_memtype = 0; + int status, dynamic_mtype = 0; long otimeout = serial_recv_timeout; pmsg_notice2("jtag3_paged_load(.., %s, %d, 0x%04x, %d)\n", @@ -2023,21 +2020,20 @@ static int jtag3_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AVRME cmd[1] = CMD3_READ_MEMORY; cmd[2] = 0; - if (str_eq(m->desc, "flash")) { - cmd[3] = jtag3_memtype(pgm, p, addr); + if (mem_is_flash(m)) { + cmd[3] = jtag3_mtype(pgm, p, addr); if (p->prog_modes & PM_PDI) - /* dynamically decide between flash/boot memtype */ - dynamic_memtype = 1; - } else if (str_eq(m->desc, "eeprom")) { + /* dynamically decide between flash/boot mtype */ + dynamic_mtype = 1; + } else if (mem_is_eeprom(m)) { cmd[3] = p->prog_modes & (PM_PDI | PM_UPDI)? MTYPE_EEPROM: MTYPE_EEPROM_PAGE; if (pgm->flag & PGM_FL_IS_DW) return -1; - } else if (str_eq(m->desc, "prodsig")) { + } else if (mem_is_sigrow(m)) { cmd[3] = MTYPE_PRODSIG; - } else if (str_eq(m->desc, "usersig") || - str_eq(m->desc, "userrow")) { + } else if (mem_is_userrow(m)) { cmd[3] = MTYPE_USERSIG; - } else if (str_eq(m->desc, "boot")) { + } else if (mem_is_boot(m)) { cmd[3] = MTYPE_BOOT_FLASH; } else if (p->prog_modes & PM_PDI) { cmd[3] = MTYPE_FLASH; @@ -2055,8 +2051,8 @@ static int jtag3_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AVRME pmsg_debug("jtag3_paged_load(): " "block_size at addr %d is %d\n", addr, block_size); - if (dynamic_memtype) - cmd[3] = jtag3_memtype(pgm, p, addr); + if (dynamic_mtype) + cmd[3] = jtag3_mtype(pgm, p, addr); u32_to_b4(cmd + 8, block_size); u32_to_b4(cmd + 4, jtag3_memaddr(pgm, p, m, addr)); @@ -2119,13 +2115,13 @@ static int jtag3_read_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM cmd[2] = 0; cmd[3] = p->prog_modes & (PM_PDI | PM_UPDI)? MTYPE_FLASH: MTYPE_FLASH_PAGE; - if (avr_mem_is_flash_type(mem)) { + if (mem_is_in_flash(mem)) { addr += mem->offset & (512 * 1024 - 1); /* max 512 KiB flash */ pagesize = PDATA(pgm)->flash_pagesize; paddr = addr & ~(pagesize - 1); paddr_ptr = &PDATA(pgm)->flash_pageaddr; cache_ptr = PDATA(pgm)->flash_pagecache; - } else if (avr_mem_is_eeprom_type(mem)) { + } else if (mem_is_eeprom(mem)) { if ( (pgm->flag & PGM_FL_IS_DW) || (p->prog_modes & PM_PDI) || (p->prog_modes & PM_UPDI) ) { cmd[3] = MTYPE_EEPROM; } else { @@ -2135,26 +2131,19 @@ static int jtag3_read_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM paddr = addr & ~(pagesize - 1); paddr_ptr = &PDATA(pgm)->eeprom_pageaddr; cache_ptr = PDATA(pgm)->eeprom_pagecache; - } else if (str_contains(mem->desc, "fuse") && strlen(mem->desc) <= 5) { + } else if (mem_is_a_fuse(mem) || mem_is_fuses(mem)) { cmd[3] = MTYPE_FUSE_BITS; - if (str_eq(mem->desc, "lfuse") || str_eq(mem->desc, "fuse")) - addr = 0; - else if (str_eq(mem->desc, "hfuse")) - addr = 1; - else if (str_eq(mem->desc, "efuse")) - addr = 2; - else if (str_starts(mem->desc, "fuse") && !(p->prog_modes & PM_UPDI)) - addr = mem->offset & 7; + if(!(p->prog_modes & PM_UPDI) && mem_is_a_fuse(mem)) + addr = mem_fuse_offset(mem); if (pgm->flag & PGM_FL_IS_DW) unsupp = 1; - } else if (str_starts(mem->desc, "lock")) { + } else if (mem_is_lock(mem)) { cmd[3] = MTYPE_LOCK_BITS; if (pgm->flag & PGM_FL_IS_DW) unsupp = 1; - } else if (str_eq(mem->desc, "usersig") || - str_eq(mem->desc, "userrow")) { + } else if (mem_is_userrow(mem)) { cmd[3] = MTYPE_USERSIG; - } else if (str_eq(mem->desc, "prodsig")) { + } else if (mem_is_sigrow(mem)) { if (p->prog_modes & (PM_PDI | PM_UPDI)) { cmd[3] = MTYPE_PRODSIG; } else { @@ -2163,25 +2152,25 @@ static int jtag3_read_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM if (pgm->flag & PGM_FL_IS_DW) unsupp = 1; } - } else if (str_eq(mem->desc, "sernum")) { + } else if (mem_is_sernum(mem)) { cmd[3] = MTYPE_SIGN_JTAG; - } else if (str_eq(mem->desc, "osccal16")) { + } else if (mem_is_osccal16(mem)) { cmd[3] = MTYPE_SIGN_JTAG; - } else if (str_eq(mem->desc, "osccal20")) { + } else if (mem_is_osccal20(mem)) { cmd[3] = MTYPE_SIGN_JTAG; - } else if (str_eq(mem->desc, "tempsense")) { + } else if (mem_is_tempsense(mem)) { cmd[3] = MTYPE_SIGN_JTAG; - } else if (str_eq(mem->desc, "osc16err")) { + } else if (mem_is_osc16err(mem)) { cmd[3] = MTYPE_SIGN_JTAG; - } else if (str_eq(mem->desc, "osc20err")) { + } else if (mem_is_osc20err(mem)) { cmd[3] = MTYPE_SIGN_JTAG; - } else if (str_eq(mem->desc, "calibration")) { + } else if (mem_is_calibration(mem)) { cmd[3] = MTYPE_OSCCAL_BYTE; if (pgm->flag & PGM_FL_IS_DW) unsupp = 1; - } else if (str_eq(mem->desc, "io")) { + } else if (mem_is_io(mem)) { cmd[3] = MTYPE_SRAM; - } else if (str_eq(mem->desc, "sib")) { + } else if (mem_is_sib(mem)) { if(addr >= AVR_SIBLEN) { pmsg_error("cannot read byte from %s sib as address 0x%04lx outside range [0, 0x%04x]\n", p->desc, addr, AVR_SIBLEN-1); @@ -2193,7 +2182,7 @@ static int jtag3_read_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM } *value = PDATA(pgm)->sib_string[addr]; return 0; - } else if (str_eq(mem->desc, "signature")) { + } else if (mem_is_signature(mem)) { static unsigned char signature_cache[2]; cmd[3] = MTYPE_SIGN_JTAG; @@ -2225,7 +2214,7 @@ static int jtag3_read_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM return -1; } } else { - pmsg_error("unknown memory %s in %s()\n", mem->desc, __func__); + pmsg_error("unknown memory %s\n", mem->desc); return -1; } @@ -2310,13 +2299,13 @@ static int jtag3_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRME cmd[1] = CMD3_WRITE_MEMORY; cmd[2] = 0; cmd[3] = p->prog_modes & (PM_PDI | PM_UPDI)? MTYPE_FLASH: MTYPE_SPM; - if (str_eq(mem->desc, "flash")) { + if (mem_is_flash(mem)) { cache_ptr = PDATA(pgm)->flash_pagecache; pagesize = PDATA(pgm)->flash_pagesize; PDATA(pgm)->flash_pageaddr = (unsigned long)-1L; if (pgm->flag & PGM_FL_IS_DW) unsupp = 1; - } else if (str_eq(mem->desc, "eeprom")) { + } else if (mem_is_eeprom(mem)) { if (pgm->flag & PGM_FL_IS_DW) { cmd[3] = MTYPE_EEPROM; } else { @@ -2324,34 +2313,23 @@ static int jtag3_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRME pagesize = PDATA(pgm)->eeprom_pagesize; } PDATA(pgm)->eeprom_pageaddr = (unsigned long)-1L; - } else if (str_contains(mem->desc, "fuse") && strlen(mem->desc) <= 5) { + } else if (mem_is_a_fuse(mem) || mem_is_fuses(mem)) { cmd[3] = MTYPE_FUSE_BITS; - if (str_eq(mem->desc, "lfuse") || str_eq(mem->desc, "fuse")) - addr = 0; - else if (str_eq(mem->desc, "hfuse")) - addr = 1; - else if (str_eq(mem->desc, "efuse")) - addr = 2; - else if (str_starts(mem->desc, "fuse") && !(p->prog_modes & PM_UPDI)) - addr = mem->offset & 7; + if(!(p->prog_modes & PM_UPDI) && mem_is_a_fuse(mem)) + addr = mem_fuse_offset(mem); if (pgm->flag & PGM_FL_IS_DW) unsupp = 1; - } else if (str_starts(mem->desc, "lock")) { + } else if (mem_is_lock(mem)) { cmd[3] = MTYPE_LOCK_BITS; if (pgm->flag & PGM_FL_IS_DW) unsupp = 1; - } else if (str_eq(mem->desc, "usersig") || - str_eq(mem->desc, "userrow")) { + } else if (mem_is_userrow(mem)) { cmd[3] = MTYPE_USERSIG; - } else if (str_eq(mem->desc, "io")) + } else if (mem_is_io(mem)) cmd[3] = MTYPE_SRAM; // Read-only memories or unsupported by debugWire - if(str_eq(mem->desc, "calibration") || str_eq(mem->desc, "osc16err") || - str_eq(mem->desc, "osccal16") || str_eq(mem->desc, "osc20err") || - str_eq(mem->desc, "osccal20") || str_eq(mem->desc, "prodsig") || - str_eq(mem->desc, "sernum") || str_eq(mem->desc, "sib") || - str_eq(mem->desc, "signature") || str_eq(mem->desc, "tempsense") || unsupp) { + if(mem_is_readonly(mem) || unsupp) { unsigned char is; if(jtag3_read_byte(pgm, p, mem, addr, &is) >= 0 && is == data) return 0; @@ -2529,13 +2507,16 @@ int jtag3_read_sib(const PROGRAMMER *pgm, const AVRPART *p, char *sib) { return 0; } -int jtag3_read_chip_rev(const PROGRAMMER *pgm, const AVRPART *p, char *chip_rev) { +int jtag3_read_chip_rev(const PROGRAMMER *pgm, const AVRPART *p, unsigned char *chip_rev) { // XMEGA using JTAG or PDI, tinyAVR0/1/2, megaAVR0, AVR-Dx, AVR-Ex using UPDI if(p->prog_modes & (PM_PDI | PM_UPDI)) { - AVRMEM *m = avr_locate_mem(p, "io"); + AVRMEM *m = avr_locate_io(p); + if(!m) { + pmsg_error("cannot locate io memory; is avrdude.conf up to date?\n"); + return -1; + } int status = pgm->read_byte(pgm, p, m, - p->prog_modes & PM_PDI? p->mcu_base+3 :p->syscfg_base+1, - (unsigned char *)chip_rev); + p->prog_modes & PM_PDI? p->mcu_base+3 :p->syscfg_base+1, chip_rev); if (status < 0) return status; } else { @@ -2745,7 +2726,7 @@ static void jtag3_print_parms(const PROGRAMMER *pgm, FILE *fp) { jtag3_print_parms1(pgm, "", fp); } -static unsigned char jtag3_memtype(const PROGRAMMER *pgm, const AVRPART *p, unsigned long addr) { +static unsigned char jtag3_mtype(const PROGRAMMER *pgm, const AVRPART *p, unsigned long addr) { if (p->prog_modes & PM_PDI) { if (addr >= PDATA(pgm)->boot_start) return MTYPE_BOOT_FLASH; @@ -2765,33 +2746,25 @@ static unsigned int jtag3_memaddr(const PROGRAMMER *pgm, const AVRPART *p, const if(addr >= PDATA(pgm)->boot_start) addr -= PDATA(pgm)->boot_start; } else if(p->prog_modes & PM_UPDI) { // Modern AVR8X part - if(!str_eq(m->desc, "flash")) + if(!mem_is_flash(m)) if(m->size >= 1) addr += m->offset; } else { // Classic part - if(str_eq(m->desc, "usersig")) + if(mem_is_userrow(m)) addr += m->offset; } return addr; } -unsigned char tpi_get_memtype(const AVRMEM *mem) { - unsigned char memtype; - if (str_eq(mem->desc, "fuse")) { - memtype = XPRG_MEM_TYPE_FUSE; - } else if (str_eq(mem->desc, "lock")) { - memtype = XPRG_MEM_TYPE_LOCKBITS; - } else if (str_eq(mem->desc, "calibration")) { - memtype = XPRG_MEM_TYPE_LOCKBITS; - } else if (str_eq(mem->desc, "signature")) { - memtype = XPRG_MEM_TYPE_LOCKBITS; - } else if (str_eq(mem->desc, "sigrow")) { - memtype = XPRG_MEM_TYPE_LOCKBITS; - } else { - memtype = XPRG_MEM_TYPE_APPL; - } - return memtype; +static unsigned char tpi_get_mtype(const AVRMEM *m) { + return + mem_is_a_fuse(m)? XPRG_MEM_TYPE_FUSE: + mem_is_lock(m)? XPRG_MEM_TYPE_LOCKBITS: + mem_is_calibration(m)? XPRG_MEM_TYPE_LOCKBITS: // Sic, uses offset to distingish memories + mem_is_signature(m)? XPRG_MEM_TYPE_LOCKBITS: + mem_is_sigrow(m)? XPRG_MEM_TYPE_LOCKBITS: + XPRG_MEM_TYPE_APPL; // Sic, TPI parts do not have eeprom } /* @@ -2959,7 +2932,7 @@ static int jtag3_read_byte_tpi(const PROGRAMMER *pgm, const AVRPART *p, const AV paddr = mem->offset + addr; cmd[0] = XPRG_CMD_READ_MEM; - cmd[1] = tpi_get_memtype(mem); + cmd[1] = tpi_get_mtype(mem); u32_to_b4_big_endian((cmd+2), paddr); // Address u16_to_b2_big_endian((cmd+6), 1); // Size @@ -2979,9 +2952,9 @@ static int jtag3_erase_tpi(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM unsigned long paddr = 0UL; cmd[0] = XPRG_CMD_ERASE; - if (str_eq(mem->desc, "fuse")) { + if (mem_is_a_fuse(mem)) { cmd[1] = XPRG_ERASE_CONFIG; - } else if (str_eq(mem->desc, "flash")) { + } else if (mem_is_flash(mem)) { cmd[1] = XPRG_ERASE_APP; } else { pmsg_error("jtag3_erase_tpi() unsupported memory: %s\n", mem->desc); @@ -3025,7 +2998,7 @@ static int jtag3_write_byte_tpi(const PROGRAMMER *pgm, const AVRPART *p, const A } cmd[0] = XPRG_CMD_WRITE_MEM; - cmd[1] = tpi_get_memtype(mem); + cmd[1] = tpi_get_mtype(mem); cmd[2] = 0; // Page Mode - Not used u32_to_b4_big_endian((cmd+3), paddr); // Address u16_to_b2_big_endian((cmd+7), data_size); // Size @@ -3051,7 +3024,7 @@ static int jtag3_chip_erase_tpi(const PROGRAMMER *pgm, const AVRPART *p) { int status; unsigned long paddr = 0UL; - AVRMEM *m = avr_locate_mem(p, "flash"); + AVRMEM *m = avr_locate_flash(p); if (m == NULL) { pmsg_error("no flash memory for part %s\n", p->desc); return LIBAVRDUDE_GENERAL_FAILURE; @@ -3100,7 +3073,7 @@ static int jtag3_paged_load_tpi(const PROGRAMMER *pgm, const AVRPART *p, imsg_notice2("mapped to address: 0x%04x\n", (addr+m->offset)); cmd[0] = XPRG_CMD_READ_MEM; - cmd[1] = tpi_get_memtype(m); + cmd[1] = tpi_get_mtype(m); if(m->blocksize > (int) page_size) page_size = m->blocksize; @@ -3166,7 +3139,7 @@ static int jtag3_paged_write_tpi(const PROGRAMMER *pgm, const AVRPART *p, } cmd[0] = XPRG_CMD_WRITE_MEM; - cmd[1] = tpi_get_memtype(m); + cmd[1] = tpi_get_mtype(m); cmd[2] = 0; // Page Mode; Not used - ignored serial_recv_timeout = 100; diff --git a/src/jtag3_private.h b/src/jtag3_private.h index 0c3806a6d..f5c276d77 100644 --- a/src/jtag3_private.h +++ b/src/jtag3_private.h @@ -156,7 +156,7 @@ #define EVT3_SLEEP 0x11 /* General scope, also wakeup */ #define EVT3_POWER 0x10 /* General scope */ -/* memory types */ +/* memories */ #define MTYPE_SRAM 0x20 /* target's SRAM or [ext.] IO registers */ #define MTYPE_EEPROM 0x22 /* EEPROM, what way? */ #define MTYPE_SPM 0xA0 /* flash through LPM/SPM */ @@ -260,7 +260,7 @@ #define PARM3_UPDI_HV_AUTO_POWER_TOGGLE 0x02 /* Toggle power automatically and then apply a high-voltage pulse */ #define PARM3_UPDI_HV_USER_POWER_TOGGLE 0x03 /* The user toggles power, and the tool applies a high-voltage pulse on power-up */ -/* Xmega erase memory types, for CMND_XMEGA_ERASE */ +/* Xmega erase memories for CMND_XMEGA_ERASE */ #define XMEGA_ERASE_CHIP 0x00 #define XMEGA_ERASE_APP 0x01 #define XMEGA_ERASE_BOOT 0x02 @@ -345,7 +345,7 @@ #define XPRG_ERR_ILLEGAL_PARAM 0x04 #define XPRG_ERR_UNKNOWN_COMMAND 0x10 -// TPI Memory types +// TPI Memories #define XPRG_MEM_TYPE_APPL 0x01 #define XPRG_MEM_TYPE_BOOT 0x02 #define XPRG_MEM_TYPE_EEPROM 0x03 diff --git a/src/jtagmkI.c b/src/jtagmkI.c index dfaaa3e85..6b6ff65bf 100644 --- a/src/jtagmkI.c +++ b/src/jtagmkI.c @@ -367,10 +367,10 @@ static void jtagmkI_set_devdescr(const PROGRAMMER *pgm, const AVRPART *p) { sendbuf.dd.ucIDRAddress = p->idr; for (ln = lfirst(p->mem); ln; ln = lnext(ln)) { m = ldata(ln); - if (str_eq(m->desc, "flash")) { + if (mem_is_flash(m)) { PDATA(pgm)->flash_pagesize = m->page_size; u16_to_b2(sendbuf.dd.uiFlashPageSize, PDATA(pgm)->flash_pagesize); - } else if (str_eq(m->desc, "eeprom")) { + } else if (mem_is_eeprom(m)) { sendbuf.dd.ucEepromPageSize = PDATA(pgm)->eeprom_pagesize = m->page_size; } } @@ -482,7 +482,6 @@ static unsigned char jtagmkI_get_baud(long baud) * initialize the AVR device and prepare it to accept commands */ static int jtagmkI_initialize(const PROGRAMMER *pgm, const AVRPART *p) { - AVRMEM hfuse; unsigned char cmd[1], resp[5]; unsigned char b; @@ -551,8 +550,8 @@ static int jtagmkI_initialize(const PROGRAMMER *pgm, const AVRPART *p) { if (jtagmkI_reset(pgm) < 0) return -1; - hfuse.desc = cache_string("hfuse"); - if (jtagmkI_read_byte(pgm, p, &hfuse, 1, &b) < 0) + AVRMEM *hf = avr_locate_hfuse(p); + if (!hf || jtagmkI_read_byte(pgm, p, hf, 1, &b) < 0) return -1; if ((b & OCDEN) != 0) pmsg_warning("OCDEN fuse not programmed, " @@ -678,12 +677,12 @@ static int jtagmkI_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AV } cmd[0] = CMD_WRITE_MEM; - if (str_eq(m->desc, "flash")) { + if (mem_is_flash(m)) { cmd[1] = MTYPE_FLASH_PAGE; PDATA(pgm)->flash_pageaddr = (unsigned long)-1L; page_size = PDATA(pgm)->flash_pagesize; is_flash = 1; - } else if (str_eq(m->desc, "eeprom")) { + } else if (mem_is_eeprom(m)) { cmd[1] = MTYPE_EEPROM_PAGE; PDATA(pgm)->eeprom_pageaddr = (unsigned long)-1L; page_size = PDATA(pgm)->eeprom_pagesize; @@ -786,10 +785,10 @@ static int jtagmkI_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AVR page_size = m->readsize; cmd[0] = CMD_READ_MEM; - if (str_eq(m->desc, "flash")) { + if (mem_is_flash(m)) { cmd[1] = MTYPE_FLASH_PAGE; is_flash = 1; - } else if (str_eq(m->desc, "eeprom")) { + } else if (mem_is_eeprom(m)) { cmd[1] = MTYPE_EEPROM_PAGE; } @@ -867,38 +866,34 @@ static int jtagmkI_read_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRM cmd[0] = CMD_READ_MEM; - if (str_eq(mem->desc, "flash")) { + if (mem_is_flash(mem)) { cmd[1] = MTYPE_FLASH_PAGE; pagesize = mem->page_size; paddr = addr & ~(pagesize - 1); paddr_ptr = &PDATA(pgm)->flash_pageaddr; cache_ptr = PDATA(pgm)->flash_pagecache; is_flash = 1; - } else if (str_eq(mem->desc, "eeprom")) { + } else if (mem_is_eeprom(mem)) { cmd[1] = MTYPE_EEPROM_PAGE; pagesize = mem->page_size; paddr = addr & ~(pagesize - 1); paddr_ptr = &PDATA(pgm)->eeprom_pageaddr; cache_ptr = PDATA(pgm)->eeprom_pagecache; - } else if (str_contains(mem->desc, "fuse") && strlen(mem->desc) <= 5) { + } else if (mem_is_a_fuse(mem) || mem_is_fuses(mem)) { cmd[1] = MTYPE_FUSE_BITS; - if (str_eq(mem->desc, "lfuse") || str_eq(mem->desc, "fuse")) - addr = 0; - else if (str_eq(mem->desc, "hfuse")) - addr = 1; - else if (str_eq(mem->desc, "efuse")) - addr = 2; - } else if (str_eq(mem->desc, "lock")) { + if(mem_is_a_fuse(mem)) + addr = mem_fuse_offset(mem); + } else if (mem_is_lock(mem)) { cmd[1] = MTYPE_LOCK_BITS; - } else if (str_eq(mem->desc, "calibration")) { + } else if (mem_is_calibration(mem)) { cmd[1] = MTYPE_OSCCAL_BYTE; - } else if (str_eq(mem->desc, "signature")) { + } else if (mem_is_signature(mem)) { cmd[1] = MTYPE_SIGN_JTAG; - } else if (str_eq(mem->desc, "prodsig")) { + } else if (mem_is_sigrow(mem)) { cmd[1] = addr&1? MTYPE_OSCCAL_BYTE: MTYPE_SIGN_JTAG; addr /= 2; } else { - pmsg_error("unknown memory %s in %s()\n", mem->desc, __func__); + pmsg_error("unknown memory %s\n", mem->desc); return -1; } @@ -977,34 +972,30 @@ static int jtagmkI_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVR writedata = data; cmd[0] = CMD_WRITE_MEM; - if (str_eq(mem->desc, "flash")) { + if (mem_is_flash(mem)) { cmd[1] = MTYPE_SPM; need_progmode = 0; PDATA(pgm)->flash_pageaddr = (unsigned long)-1L; - } else if (str_eq(mem->desc, "eeprom")) { + } else if (mem_is_eeprom(mem)) { cmd[1] = MTYPE_EEPROM; need_progmode = 0; need_dummy_read = 1; PDATA(pgm)->eeprom_pageaddr = (unsigned long)-1L; - } else if (str_contains(mem->desc, "fuse") && strlen(mem->desc) <= 5) { + } else if (mem_is_a_fuse(mem) || mem_is_fuses(mem)) { cmd[1] = MTYPE_FUSE_BITS; need_dummy_read = 1; - if (str_eq(mem->desc, "lfuse") || str_eq(mem->desc, "lfuse")) - addr = 0; - else if (str_eq(mem->desc, "hfuse")) - addr = 1; - else if (str_eq(mem->desc, "efuse")) - addr = 2; - } else if (str_eq(mem->desc, "lock")) { + if(mem_is_a_fuse(mem)) + addr = mem_fuse_offset(mem); + } else if (mem_is_lock(mem)) { cmd[1] = MTYPE_LOCK_BITS; need_dummy_read = 1; - } else if (str_eq(mem->desc, "calibration")) { + } else if (mem_is_calibration(mem)) { cmd[1] = MTYPE_OSCCAL_BYTE; need_dummy_read = 1; - } else if (str_eq(mem->desc, "signature")) { + } else if (mem_is_signature(mem)) { cmd[1] = MTYPE_SIGN_JTAG; } else { - pmsg_error("unknown memory %s in %s()\n", mem->desc, __func__); + pmsg_error("unknown memory %s\n", mem->desc); return -1; } diff --git a/src/jtagmkII.c b/src/jtagmkII.c index 701188a5d..d36cc96b5 100644 --- a/src/jtagmkII.c +++ b/src/jtagmkII.c @@ -151,7 +151,7 @@ static void jtagmkII_print_parms1(const PROGRAMMER *pgm, const char *p, FILE *fp static int jtagmkII_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m, unsigned int page_size, unsigned int addr, unsigned int n_bytes); -static unsigned char jtagmkII_memtype(const PROGRAMMER *pgm, const AVRPART *p, unsigned long addr); +static unsigned char jtagmkII_mtype(const PROGRAMMER *pgm, const AVRPART *p, unsigned long addr); static unsigned int jtagmkII_memaddr(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m, unsigned long addr); // AVR32 @@ -620,27 +620,9 @@ int jtagmkII_recv(const PROGRAMMER *pgm, unsigned char **msg) { */ memmove(*msg, *msg + 8, rv); - if (verbose == 4) - { - int i = rv; - unsigned char *p = *msg; - pmsg_trace("recv: "); - - while (i) { - unsigned char c = *p; - if (isprint(c)) { - msg_trace("%c ", c); - } - else { - msg_trace(". "); - } - msg_trace("[%02x] ", c); - - p++; - i--; - } - msg_trace("\n"); - } + if(verbose > 3) + trace_buffer(__func__, *msg, rv); + return rv; } if (r_seqno == 0xffff) { @@ -907,7 +889,7 @@ static void jtagmkII_set_devdescr(const PROGRAMMER *pgm, const AVRPART *p) { sendbuf.dd.ucSPMCRAddress = p->spmcr; sendbuf.dd.ucRAMPZAddress = p->rampz; sendbuf.dd.ucIDRAddress = p->idr; - if((flm = avr_locate_mem(p, "flash")) && p->boot_section_size > 0) { + if((flm = avr_locate_flash(p)) && p->boot_section_size > 0) { unsigned int sbls = (flm->size - p->boot_section_size)/2; // Words sendbuf.dd.uiStartSmallestBootLoaderSection[0] = sbls; sendbuf.dd.uiStartSmallestBootLoaderSection[1] = sbls>>8; @@ -919,7 +901,7 @@ static void jtagmkII_set_devdescr(const PROGRAMMER *pgm, const AVRPART *p) { (p->flags & AVRPART_ENABLEPAGEPROGRAMMING) != 0; for (ln = lfirst(p->mem); ln; ln = lnext(ln)) { AVRMEM *m = ldata(ln); - if (str_eq(m->desc, "flash")) { + if (mem_is_flash(m)) { if (m->page_size > 256) PDATA(pgm)->flash_pagesize = 256; else @@ -931,7 +913,7 @@ static void jtagmkII_set_devdescr(const PROGRAMMER *pgm, const AVRPART *p) { memcpy(sendbuf.dd.ucFlashInst, p->flash_instr, FLASH_INSTR_SIZE); memcpy(sendbuf.dd.ucEepromInst, p->eeprom_instr, EEPROM_INSTR_SIZE); } - } else if (str_eq(m->desc, "eeprom")) { + } else if (mem_is_eeprom(m)) { sendbuf.dd.ucEepromPageSize = PDATA(pgm)->eeprom_pagesize = m->page_size; } } @@ -962,7 +944,7 @@ static void jtagmkII_set_devdescr(const PROGRAMMER *pgm, const AVRPART *p) { } static void jtagmkII_set_xmega_params(const PROGRAMMER *pgm, const AVRPART *p) { - int status; + int status, fuseinit = 0; unsigned char *resp, c; LNODEID ln; AVRMEM * m; @@ -980,37 +962,37 @@ static void jtagmkII_set_xmega_params(const PROGRAMMER *pgm, const AVRPART *p) { for (ln = lfirst(p->mem); ln; ln = lnext(ln)) { m = ldata(ln); - if (str_eq(m->desc, "flash")) { + if (mem_is_flash(m)) { if (m->page_size > 256) PDATA(pgm)->flash_pagesize = 256; else PDATA(pgm)->flash_pagesize = m->page_size; u16_to_b2(sendbuf.dd.flash_page_size, m->page_size); - } else if (str_eq(m->desc, "eeprom")) { + } else if (mem_is_eeprom(m)) { sendbuf.dd.eeprom_page_size = m->page_size; u16_to_b2(sendbuf.dd.eeprom_size, m->size); u32_to_b4(sendbuf.dd.nvm_eeprom_offset, m->offset); - } else if (str_eq(m->desc, "application")) { + } else if (mem_is_application(m)) { u32_to_b4(sendbuf.dd.app_size, m->size); u32_to_b4(sendbuf.dd.nvm_app_offset, m->offset); - } else if (str_eq(m->desc, "boot")) { + } else if (mem_is_boot(m)) { u16_to_b2(sendbuf.dd.boot_size, m->size); u32_to_b4(sendbuf.dd.nvm_boot_offset, m->offset); - } else if (str_eq(m->desc, "fuse1")) { + } else if(mem_is_a_fuse(m) && !fuseinit++) { // Any fuse is OK u32_to_b4(sendbuf.dd.nvm_fuse_offset, m->offset & ~15); - } else if (str_starts(m->desc, "lock")) { + } else if (mem_is_lock(m)) { u32_to_b4(sendbuf.dd.nvm_lock_offset, m->offset); - } else if (str_eq(m->desc, "usersig") || str_eq(m->desc, "userrow")) { + } else if (mem_is_userrow(m)) { u32_to_b4(sendbuf.dd.nvm_user_sig_offset, m->offset); - } else if (str_eq(m->desc, "prodsig")) { + } else if (mem_is_sigrow(m)) { u32_to_b4(sendbuf.dd.nvm_prod_sig_offset, m->offset); - } else if (str_eq(m->desc, "data")) { + pmsg_notice2("prod_sig_offset addr 0x%05x\n", m->offset); + } else if (mem_is_data(m)) { u32_to_b4(sendbuf.dd.nvm_data_offset, m->offset); } } - pmsg_notice2("jtagmkII_set_xmega_params(): " - "Sending set Xmega params command: "); + pmsg_notice2("%s() sending set Xmega params command: ", __func__); jtagmkII_send(pgm, (unsigned char *)&sendbuf, sizeof sendbuf); status = jtagmkII_recv(pgm, &resp); @@ -1213,7 +1195,6 @@ static unsigned char jtagmkII_get_baud(long baud) * initialize the AVR device and prepare it to accept commands */ static int jtagmkII_initialize(const PROGRAMMER *pgm, const AVRPART *p) { - AVRMEM hfuse; unsigned char b; int ok; const char *ifname; @@ -1293,8 +1274,8 @@ static int jtagmkII_initialize(const PROGRAMMER *pgm, const AVRPART *p) { PDATA(pgm)->boot_start = ULONG_MAX; if ((p->prog_modes & (PM_PDI | PM_UPDI))) { // Find the border between application and boot area - AVRMEM *bootmem = avr_locate_mem(p, "boot"); - AVRMEM *flashmem = avr_locate_mem(p, "flash"); + AVRMEM *bootmem = avr_locate_boot(p); + AVRMEM *flashmem = avr_locate_flash(p); if (bootmem == NULL || flashmem == NULL) { if(str_starts(pgmid, "jtagmkII")) pmsg_error("cannot locate flash or boot memories in description\n"); @@ -1344,8 +1325,8 @@ static int jtagmkII_initialize(const PROGRAMMER *pgm, const AVRPART *p) { } if ((pgm->flag & PGM_FL_IS_JTAG) && !(p->prog_modes & (PM_PDI | PM_UPDI))) { - hfuse.desc = cache_string("hfuse"); - if (jtagmkII_read_byte(pgm, p, &hfuse, 1, &b) < 0) + AVRMEM *hf = avr_locate_hfuse(p); + if (!hf || jtagmkII_read_byte(pgm, p, hf, 1, &b) < 0) return -1; if ((b & OCDEN) != 0) pmsg_warning("OCDEN fuse not programmed, " @@ -1353,7 +1334,7 @@ static int jtagmkII_initialize(const PROGRAMMER *pgm, const AVRPART *p) { } if (pgm->read_chip_rev && p->prog_modes & (PM_PDI | PM_UPDI)) { - char chip_rev[AVR_CHIP_REVLEN]; + unsigned char chip_rev[AVR_CHIP_REVLEN]; pgm->read_chip_rev(pgm, p, chip_rev); pmsg_notice("silicon revision: %x.%x\n", chip_rev[0] >> 4, chip_rev[0] & 0x0f); } @@ -1379,7 +1360,7 @@ static void jtagmkII_disable(const PROGRAMMER *pgm) { static void jtagmkII_enable(PROGRAMMER *pgm, const AVRPART *p) { // Page erase only useful for classic parts with usersig mem or AVR8X/XMEGAs if(!(p->prog_modes & (PM_PDI | PM_UPDI))) - if(!avr_locate_mem(p, "usersig")) + if(!avr_locate_usersig(p)) pgm->page_erase = NULL; if(pgm->flag & PGM_FL_IS_DW) @@ -1842,7 +1823,7 @@ static int jtagmkII_page_erase(const PROGRAMMER *pgm, const AVRPART *p, const AV pmsg_notice2("jtagmkII_page_erase(.., %s, 0x%x)\n", m->desc, addr); - if (!(p->prog_modes & (PM_PDI | PM_UPDI)) && !str_eq(m->desc, "usersig")) { + if (!(p->prog_modes & (PM_PDI | PM_UPDI)) && !mem_is_userrow(m)) { pmsg_error("page erase only available for AVR8X/XMEGAs or classic-part usersig mem\n"); return -1; } @@ -1855,16 +1836,16 @@ static int jtagmkII_page_erase(const PROGRAMMER *pgm, const AVRPART *p, const AV return -1; cmd[0] = CMND_XMEGA_ERASE; - if (str_eq(m->desc, "flash")) { - if (jtagmkII_memtype(pgm, p, addr) == MTYPE_FLASH) + if (mem_is_flash(m)) { + if (jtagmkII_mtype(pgm, p, addr) == MTYPE_FLASH) cmd[1] = XMEGA_ERASE_APP_PAGE; else cmd[1] = XMEGA_ERASE_BOOT_PAGE; - } else if (str_eq(m->desc, "eeprom")) { + } else if (mem_is_eeprom(m)) { cmd[1] = XMEGA_ERASE_EEPROM_PAGE; - } else if (str_eq(m->desc, "usersig") || str_eq(m->desc, "userrow")) { + } else if (mem_is_userrow(m)) { cmd[1] = XMEGA_ERASE_USERSIG; - } else if (str_eq(m->desc, "boot")) { + } else if (mem_is_boot(m)) { cmd[1] = XMEGA_ERASE_BOOT_PAGE; } else { cmd[1] = XMEGA_ERASE_APP_PAGE; @@ -1924,7 +1905,7 @@ static int jtagmkII_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const A unsigned int maxaddr = addr + n_bytes; unsigned char *cmd; unsigned char *resp; - int status, tries, dynamic_memtype = 0; + int status, tries, dynamic_mtype = 0; long otimeout = serial_recv_timeout; pmsg_notice2("jtagmkII_paged_write(.., %s, %d, %d)\n", m->desc, page_size, n_bytes); @@ -1941,13 +1922,12 @@ static int jtagmkII_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const A } cmd[0] = CMND_WRITE_MEMORY; - if (str_eq(m->desc, "flash")) { + if (mem_is_flash(m)) { PDATA(pgm)->flash_pageaddr = (unsigned long)-1L; - cmd[1] = jtagmkII_memtype(pgm, p, addr); - if (p->prog_modes & (PM_PDI | PM_UPDI)) - /* dynamically decide between flash/boot memtype */ - dynamic_memtype = 1; - } else if (str_eq(m->desc, "eeprom")) { + cmd[1] = jtagmkII_mtype(pgm, p, addr); + if (p->prog_modes & (PM_PDI | PM_UPDI)) // Dynamically decide between flash/boot mtype + dynamic_mtype = 1; + } else if (mem_is_eeprom(m)) { if (pgm->flag & PGM_FL_IS_DW) { /* * jtagmkII_paged_write() to EEPROM attempted while in @@ -1965,9 +1945,9 @@ static int jtagmkII_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const A } cmd[1] = p->prog_modes & (PM_PDI | PM_UPDI)? MTYPE_EEPROM: MTYPE_EEPROM_PAGE; PDATA(pgm)->eeprom_pageaddr = (unsigned long)-1L; - } else if (str_eq(m->desc, "usersig") || str_eq(m->desc, "userrow")) { + } else if (mem_is_userrow(m)) { cmd[1] = MTYPE_USERSIG; - } else if (str_eq(m->desc, "boot")) { + } else if (mem_is_boot(m)) { cmd[1] = MTYPE_BOOT_FLASH; } else if (p->prog_modes & (PM_PDI | PM_UPDI)) { cmd[1] = MTYPE_FLASH; @@ -1983,8 +1963,8 @@ static int jtagmkII_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const A pmsg_debug("jtagmkII_paged_write(): " "block_size at addr %d is %d\n", addr, block_size); - if (dynamic_memtype) - cmd[1] = jtagmkII_memtype(pgm, p, addr); + if (dynamic_mtype) + cmd[1] = jtagmkII_mtype(pgm, p, addr); u32_to_b4(cmd + 2, page_size); u32_to_b4(cmd + 6, jtagmkII_memaddr(pgm, p, m, addr)); @@ -2048,7 +2028,7 @@ static int jtagmkII_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AV unsigned int maxaddr = addr + n_bytes; unsigned char cmd[10]; unsigned char *resp; - int status, tries, dynamic_memtype = 0; + int status, tries, dynamic_mtype = 0; long otimeout = serial_recv_timeout; pmsg_notice2("jtagmkII_paged_load(.., %s, %d, %d)\n", m->desc, page_size, n_bytes); @@ -2059,20 +2039,19 @@ static int jtagmkII_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AV page_size = m->readsize; cmd[0] = CMND_READ_MEMORY; - if (str_eq(m->desc, "flash")) { - cmd[1] = jtagmkII_memtype(pgm, p, addr); - if (p->prog_modes & (PM_PDI | PM_UPDI)) - /* dynamically decide between flash/boot memtype */ - dynamic_memtype = 1; - } else if (str_eq(m->desc, "eeprom")) { + if (mem_is_flash(m)) { + cmd[1] = jtagmkII_mtype(pgm, p, addr); + if (p->prog_modes & (PM_PDI | PM_UPDI)) // Dynamically decide between flash/boot mtype + dynamic_mtype = 1; + } else if (mem_is_eeprom(m)) { cmd[1] = p->prog_modes & (PM_PDI | PM_UPDI)? MTYPE_EEPROM: MTYPE_EEPROM_PAGE; if (pgm->flag & PGM_FL_IS_DW) return -1; - } else if (str_eq(m->desc, "prodsig")) { + } else if (mem_is_sigrow(m)) { cmd[1] = MTYPE_PRODSIG; - } else if (str_eq(m->desc, "usersig") || str_eq(m->desc, "userrow")) { + } else if (mem_is_userrow(m)) { cmd[1] = MTYPE_USERSIG; - } else if (str_eq(m->desc, "boot")) { + } else if (mem_is_boot(m)) { cmd[1] = MTYPE_BOOT_FLASH; } else if (p->prog_modes & (PM_PDI | PM_UPDI)) { cmd[1] = MTYPE_FLASH; @@ -2088,8 +2067,8 @@ static int jtagmkII_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AV pmsg_debug("jtagmkII_paged_load(): " "block_size at addr %d is %d\n", addr, block_size); - if (dynamic_memtype) - cmd[1] = jtagmkII_memtype(pgm, p, addr); + if (dynamic_mtype) + cmd[1] = jtagmkII_mtype(pgm, p, addr); u32_to_b4(cmd + 2, block_size); u32_to_b4(cmd + 6, jtagmkII_memaddr(pgm, p, m, addr)); @@ -2131,13 +2110,16 @@ static int jtagmkII_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AV return n_bytes; } -static int jtagmkII_read_chip_rev(const PROGRAMMER *pgm, const AVRPART *p, char *chip_rev) { +static int jtagmkII_read_chip_rev(const PROGRAMMER *pgm, const AVRPART *p, unsigned char *chip_rev) { // XMEGA using JTAG or PDI, tinyAVR0/1/2, megaAVR0, AVR-Dx, AVR-Ex using UPDI if(p->prog_modes & (PM_PDI | PM_UPDI)) { - AVRMEM *m = avr_locate_mem(p, "io"); + AVRMEM *m = avr_locate_io(p); + if(!m) { + pmsg_error("cannot locate io memory; is avrdude.conf up to date?\n"); + return -1; + } int status = pgm->read_byte(pgm, p, m, - p->prog_modes & PM_PDI? p->mcu_base+3 :p->syscfg_base+1, - (unsigned char *)chip_rev); + p->prog_modes & PM_PDI? p->mcu_base+3 :p->syscfg_base+1, chip_rev); if (status < 0) return status; } else { @@ -2168,12 +2150,12 @@ static int jtagmkII_read_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVR addr += mem->offset; cmd[1] = p->prog_modes & (PM_PDI | PM_UPDI)? MTYPE_FLASH: MTYPE_FLASH_PAGE; - if (avr_mem_is_flash_type(mem)) { + if (mem_is_in_flash(mem)) { pagesize = PDATA(pgm)->flash_pagesize; paddr = addr & ~(pagesize - 1); paddr_ptr = &PDATA(pgm)->flash_pageaddr; cache_ptr = PDATA(pgm)->flash_pagecache; - } else if (avr_mem_is_eeprom_type(mem)) { + } else if (mem_is_eeprom(mem)) { if ( (pgm->flag & PGM_FL_IS_DW) || (p->prog_modes & (PM_PDI | PM_UPDI)) ) { /* debugWire cannot use page access for EEPROM */ cmd[1] = MTYPE_EEPROM; @@ -2184,38 +2166,31 @@ static int jtagmkII_read_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVR paddr_ptr = &PDATA(pgm)->eeprom_pageaddr; cache_ptr = PDATA(pgm)->eeprom_pagecache; } - } else if (str_contains(mem->desc, "fuse") && strlen(mem->desc) <= 5) { + } else if(mem_is_a_fuse(mem) || mem_is_fuses(mem)) { cmd[1] = MTYPE_FUSE_BITS; - if (str_eq(mem->desc, "lfuse") || str_eq(mem->desc, "fuse")) - addr = 0; - else if (str_eq(mem->desc, "hfuse")) - addr = 1; - else if (str_eq(mem->desc, "efuse")) - addr = 2; + if(!(p->prog_modes & (PM_PDI | PM_UPDI)) && mem_is_a_fuse(mem)) + addr = mem_fuse_offset(mem); if (pgm->flag & PGM_FL_IS_DW) unsupp = 1; - } else if (str_starts(mem->desc, "lock")) { + } else if (mem_is_lock(mem)) { cmd[1] = MTYPE_LOCK_BITS; if (pgm->flag & PGM_FL_IS_DW) unsupp = 1; - } else if (str_eq(mem->desc, "usersig") || str_eq(mem->desc, "userrow")) { + } else if (mem_is_userrow(mem)) { cmd[1] = MTYPE_USERSIG; - } else if (str_eq(mem->desc, "prodsig")) { + } else if (mem_is_sigrow(mem)) { if (p->prog_modes & (PM_PDI | PM_UPDI)) { cmd[1] = MTYPE_PRODSIG; + pmsg_notice2("is_sigrow addr 0x%05lx\n", addr); } else { cmd[1] = addr&1? MTYPE_OSCCAL_BYTE: MTYPE_SIGN_JTAG; addr /= 2; } - } else if (str_eq(mem->desc, "calibration")) { + } else if (mem_is_calibration(mem)) { cmd[1] = MTYPE_OSCCAL_BYTE; if (pgm->flag & PGM_FL_IS_DW) unsupp = 1; - } else if (str_eq(mem->desc, "io")) { - cmd[1] = MTYPE_FLASH; - AVRMEM *data = avr_locate_mem(p, "data"); - addr += data->offset; - } else if (str_eq(mem->desc, "signature")) { + } else if (mem_is_signature(mem)) { cmd[1] = MTYPE_SIGN_JTAG; if (pgm->flag & PGM_FL_IS_DW) { @@ -2244,8 +2219,14 @@ static int jtagmkII_read_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVR } return 0; } + } else if ((p->prog_modes & (PM_PDI | PM_UPDI)) && mem_is_in_sigrow(mem)) { + cmd[1] = MTYPE_PRODSIG; + pmsg_notice2("in_sigrow addr 0x%05lx\n", addr); + } else if (mem_is_io(mem)) { + cmd[1] = MTYPE_FLASH; + addr += avr_data_offset(p); } else { - pmsg_error("unknown memory %s in %s()\n", mem->desc, __func__); + pmsg_error("unknown memory %s\n", mem->desc); return -1; } @@ -2335,7 +2316,7 @@ static int jtagmkII_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const AV writedata = data; cmd[0] = CMND_WRITE_MEMORY; cmd[1] = p->prog_modes & (PM_PDI | PM_UPDI)? MTYPE_FLASH: MTYPE_SPM; - if (str_eq(mem->desc, "flash")) { + if (mem_is_flash(mem)) { if ((addr & 1) == 1) { /* odd address = high byte */ writedata = 0xFF; /* don't modify the low byte */ @@ -2348,43 +2329,38 @@ static int jtagmkII_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const AV PDATA(pgm)->flash_pageaddr = (unsigned long)-1L; if (pgm->flag & PGM_FL_IS_DW) unsupp = 1; - } else if (str_eq(mem->desc, "eeprom")) { + } else if (mem_is_eeprom(mem)) { cmd[1] = p->prog_modes & (PM_PDI | PM_UPDI)? MTYPE_EEPROM_XMEGA: MTYPE_EEPROM; if(str_eq(p->family_id, "megaAVR") || str_eq(p->family_id, "tinyAVR")) // AVRs with UPDI except AVR-Dx/Ex need_progmode = 0; PDATA(pgm)->eeprom_pageaddr = (unsigned long)-1L; - } else if (str_contains(mem->desc, "fuse") && strlen(mem->desc) <= 5) { + } else if (mem_is_a_fuse(mem) || mem_is_fuses(mem)) { cmd[1] = MTYPE_FUSE_BITS; - if (str_eq(mem->desc, "lfuse") || str_eq(mem->desc, "fuse")) - addr = 0; - else if (str_eq(mem->desc, "hfuse")) - addr = 1; - else if (str_eq(mem->desc, "efuse")) - addr = 2; + if(!(p->prog_modes & (PM_PDI | PM_UPDI)) && mem_is_a_fuse(mem)) + addr = mem_fuse_offset(mem); if (pgm->flag & PGM_FL_IS_DW) unsupp = 1; - } else if (str_eq(mem->desc, "usersig") || str_eq(mem->desc, "userrow")) { + } else if (mem_is_userrow(mem)) { cmd[1] = MTYPE_USERSIG; - } else if (str_eq(mem->desc, "prodsig")) { + } else if (mem_is_sigrow(mem)) { cmd[1] = MTYPE_PRODSIG; - } else if (str_starts(mem->desc, "lock")) { + } else if (mem_is_lock(mem)) { cmd[1] = MTYPE_LOCK_BITS; if (pgm->flag & PGM_FL_IS_DW) unsupp = 1; - } else if (str_eq(mem->desc, "calibration")) { + } else if (mem_is_calibration(mem)) { cmd[1] = MTYPE_OSCCAL_BYTE; if (pgm->flag & PGM_FL_IS_DW) unsupp = 1; - } else if (str_eq(mem->desc, "io")) { + } else if (mem_is_io(mem)) { cmd[1] = MTYPE_FLASH; // Works with jtag2updi, does not work with any xmega - AVRMEM *data = avr_locate_mem(p, "data"); - addr += data->offset; - } else if (str_eq(mem->desc, "signature")) { + addr += avr_data_offset(p); + } else if (mem_is_signature(mem)) { cmd[1] = MTYPE_SIGN_JTAG; if (pgm->flag & PGM_FL_IS_DW) unsupp = 1; } else { - pmsg_error("unknown memory %s in %s()\n", mem->desc, __func__); + pmsg_error("unknown memory %s\n", mem->desc); return -1; } @@ -2520,21 +2496,22 @@ static int jtagmkII_setparm(const PROGRAMMER *pgm, unsigned char parm, */ unsigned char buf[2 + 4], *resp, c; size_t size; + const char *parstr = "???"; pmsg_notice2("jtagmkII_setparm()\n"); switch (parm) { - case PAR_HW_VERSION: size = 2; break; - case PAR_FW_VERSION: size = 4; break; - case PAR_EMULATOR_MODE: size = 1; break; - case PAR_BAUD_RATE: size = 1; break; - case PAR_OCD_VTARGET: size = 2; break; - case PAR_OCD_JTAG_CLK: size = 1; break; - case PAR_TIMERS_RUNNING: size = 1; break; - case PAR_EXTERNAL_RESET: size = 1; break; - case PAR_DAISY_CHAIN_INFO: size = 4; break; - case PAR_PDI_OFFSET_START: - case PAR_PDI_OFFSET_END: size = 4; break; + case PAR_HW_VERSION: size = 2; parstr ="hw_version"; break; + case PAR_FW_VERSION: size = 4; parstr ="fw_version"; break; + case PAR_EMULATOR_MODE: size = 1; parstr ="emulator_mode"; break; + case PAR_BAUD_RATE: size = 1; parstr ="baud_rate"; break; + case PAR_OCD_VTARGET: size = 2; parstr ="ocd_vtarget"; break; + case PAR_OCD_JTAG_CLK: size = 1; parstr ="ocd_jtag_clk"; break; + case PAR_TIMERS_RUNNING: size = 1; parstr ="timers_running"; break; + case PAR_EXTERNAL_RESET: size = 1; parstr ="external_reset"; break; + case PAR_DAISY_CHAIN_INFO: size = 4; parstr ="daisy_chain_info"; break; + case PAR_PDI_OFFSET_START: size = 4; parstr ="pdi_offset_start"; break; + case PAR_PDI_OFFSET_END: size = 4; parstr ="pdi_offset_end"; break; default: pmsg_error("unknown parameter 0x%02x\n", parm); return -1; @@ -2543,8 +2520,8 @@ static int jtagmkII_setparm(const PROGRAMMER *pgm, unsigned char parm, buf[0] = CMND_SET_PARAMETER; buf[1] = parm; memcpy(buf + 2, value, size); - pmsg_notice2("jtagmkII_setparm(): " - "Sending set parameter command (parm 0x%02x, %u bytes): ", parm, (unsigned)size); + pmsg_notice2("%s() sending set parameter command " + "(parm %s 0x%02x, %u bytes): ", __func__, parstr, parm, (unsigned) size); jtagmkII_send(pgm, buf, size + 2); status = jtagmkII_recv(pgm, &resp); @@ -2561,7 +2538,7 @@ static int jtagmkII_setparm(const PROGRAMMER *pgm, unsigned char parm, c = resp[0]; free(resp); if (c != RSP_OK) { - pmsg_error("bad response to set parameter command: %s\n", jtagmkII_get_rc(c)); + pmsg_error("bad response to set parameter %s: %s\n", parstr, jtagmkII_get_rc(c)); return -1; } @@ -2629,7 +2606,7 @@ static void jtagmkII_print_parms(const PROGRAMMER *pgm, FILE *fp) { jtagmkII_print_parms1(pgm, "", fp); } -static unsigned char jtagmkII_memtype(const PROGRAMMER *pgm, const AVRPART *p, unsigned long addr) { +static unsigned char jtagmkII_mtype(const PROGRAMMER *pgm, const AVRPART *p, unsigned long addr) { if (p->prog_modes & (PM_PDI | PM_UPDI)) { if (addr >= PDATA(pgm)->boot_start) return MTYPE_BOOT_FLASH; diff --git a/src/jtagmkII_private.h b/src/jtagmkII_private.h index 8d95da9c3..bc91bc7f9 100644 --- a/src/jtagmkII_private.h +++ b/src/jtagmkII_private.h @@ -166,7 +166,7 @@ #define EVT_ERROR_PHY_OPT_RECEIVED_BREAK 0xFA #define EVT_RESULT_PHY_NO_ACTIVITY 0xFB -/* memory types for CMND_{READ,WRITE}_MEMORY */ +/* memories for CMND_{READ,WRITE}_MEMORY */ #define MTYPE_IO_SHADOW 0x30 /* cached IO registers? */ #define MTYPE_SRAM 0x20 /* target's SRAM or [ext.] IO registers */ #define MTYPE_EEPROM 0x22 /* EEPROM, what way? */ @@ -278,7 +278,7 @@ # define PAGEPROG_NOT_ALLOWED 0x00 # define PAGEPROG_ALLOWED 0x01 -/* Xmega erase memory types, for CMND_XMEGA_ERASE */ +/* Xmega erase memories for CMND_XMEGA_ERASE */ #define XMEGA_ERASE_CHIP 0x00 #define XMEGA_ERASE_APP 0x01 #define XMEGA_ERASE_BOOT 0x02 diff --git a/src/jtagmkI_private.h b/src/jtagmkI_private.h index 7d5e247a9..e67e0b3f8 100644 --- a/src/jtagmkI_private.h +++ b/src/jtagmkI_private.h @@ -132,7 +132,7 @@ #define JTAG_BITRATE_250_kHz 0xfd #define JTAG_BITRATE_125_kHz 0xfb -/* memory types for CMND_{READ,WRITE}_MEMORY */ +/* memories for CMND_{READ,WRITE}_MEMORY */ #define MTYPE_IO_SHADOW 0x30 /* cached IO registers? */ #define MTYPE_SRAM 0x20 /* target's SRAM or [ext.] IO registers */ #define MTYPE_EEPROM 0x22 /* EEPROM, what way? */ diff --git a/src/libavrdude.h b/src/libavrdude.h index 0e957756e..4657736d8 100644 --- a/src/libavrdude.h +++ b/src/libavrdude.h @@ -232,7 +232,7 @@ typedef struct opcode { * - lexer.l * - Either Component_t avr_comp[] of config.c or in config_gram.y * - dev_part_strct() in developer_opts.c - * - avr_new_part() and/or avr_new_memtype() in avrpart.c for + * - avr_new_part() and/or avr_new_mem() in avrpart.c for * initialisation; note that all const char * must be initialised with "" */ typedef struct avrpart { @@ -322,10 +322,156 @@ typedef struct avrpart { int lineno; /* config file line number */ } AVRPART; + +typedef unsigned int memtype_t; +typedef struct { + const char *str; + memtype_t type; +} memtable_t; + +// The least significant 4 bits of type are the offset of a fuse in fuses mem +#define MEM_FUSEOFF_MASK 15 // Mask for offset +#define MEM_FUSE0 0 // fuse lfuse fuse0 wdtcfg +#define MEM_FUSE1 1 // hfuse fuse1 bodcfg +#define MEM_FUSE2 2 // efuse fuse2 osccfg +#define MEM_FUSE4 4 // fuse4 tcd0cfg +#define MEM_FUSE5 5 // fuse5 syscfg0 +#define MEM_FUSE6 6 // fuse6 syscfg1 +#define MEM_FUSE7 7 // fuse7 append codesize +#define MEM_FUSE8 8 // fuse8 bootend bootsize +#define MEM_FUSEA 10 // fusea pdicfg + +// Individual memories that may have different names in different parts +#define MEM_EEPROM (1<< 4) // eeprom +#define MEM_FLASH (1<< 5) // flash +#define MEM_APPLICATION (1<< 6) // application +#define MEM_APPTABLE (1<< 7) // apptable +#define MEM_BOOT (1<< 8) // boot +#define MEM_FUSES (1<< 9) // fuses +#define MEM_LOCK (1<<10) // lock lockbits +#define MEM_SIGROW (1<<11) // prodsig sigrow +#define MEM_SIGNATURE (1<<12) // signature +#define MEM_CALIBRATION (1<<13) // calibration +#define MEM_TEMPSENSE (1<<14) // tempsense +#define MEM_SERNUM (1<<15) // sernum +#define MEM_OSCCAL16 (1<<16) // osccal16 +#define MEM_OSCCAL20 (1<<17) // osccal20 +#define MEM_OSC16ERR (1<<18) // osc16err +#define MEM_OSC20ERR (1<<19) // osc20err +#define MEM_BOOTROW (1<<20) // bootrow +#define MEM_USERROW (1<<21) // userrow usersig +#define MEM_SRAM (1<<22) // data +#define MEM_IO (1<<23) // io +#define MEM_SIB (1<<24) // sib + +// Attributes +#define MEM_IN_FLASH (1<<27) // flash application apptable boot +#define MEM_IS_A_FUSE (1<<28) // fuse [elh]fuse fuseN wdtcfg bodcfg osccfg tcd0cfg syscfg0 syscfg1 append codesize bootend bootsize pdicfg +#define MEM_USER_TYPE (1<<29) // userrow usersig bootrow +#define MEM_IN_SIGROW (1<<30) // prodsig sigrow signature calibration sernum tempsense osccal16 osccal20 osc16err osc20err +#define MEM_READONLY (1U<<31) // sib prodsig sigrow signature sernum tempsense calibration osccal16 osccal20 osc16err osc20err + +// Marcos to locate a memory type or a fuse +#define avr_locate_eeprom(p) avr_locate_mem_by_type((p), MEM_EEPROM) +#define avr_locate_flash(p) avr_locate_mem_by_type((p), MEM_FLASH) +#define avr_locate_application(p) avr_locate_mem_by_type((p), MEM_APPLICATION) +#define avr_locate_apptable(p) avr_locate_mem_by_type((p), MEM_APPTABLE) +#define avr_locate_boot(p) avr_locate_mem_by_type((p), MEM_BOOT) +#define avr_locate_fuses(p) avr_locate_mem_by_type((p), MEM_FUSES) +#define avr_locate_lock(p) avr_locate_mem_by_type((p), MEM_LOCK) +#define avr_locate_lockbits(p) avr_locate_mem_by_type((p), MEM_LOCK) +#define avr_locate_prodsig(p) avr_locate_mem_by_type((p), MEM_SIGROW) +#define avr_locate_sigrow(p) avr_locate_mem_by_type((p), MEM_SIGROW) +#define avr_locate_signature(p) avr_locate_mem_by_type((p), MEM_SIGNATURE) +#define avr_locate_calibration(p) avr_locate_mem_by_type((p), MEM_CALIBRATION) +#define avr_locate_tempsense(p) avr_locate_mem_by_type((p), MEM_TEMPSENSE) +#define avr_locate_sernum(p) avr_locate_mem_by_type((p), MEM_SERNUM) +#define avr_locate_osccal16(p) avr_locate_mem_by_type((p), MEM_OSCCAL16) +#define avr_locate_osccal20(p) avr_locate_mem_by_type((p), MEM_OSCCAL20) +#define avr_locate_osc16err(p) avr_locate_mem_by_type((p), MEM_OSC16ERR) +#define avr_locate_osc20err(p) avr_locate_mem_by_type((p), MEM_OSC20ERR) +#define avr_locate_bootrow(p) avr_locate_mem_by_type((p), MEM_BOOTROW) +#define avr_locate_usersig(p) avr_locate_mem_by_type((p), MEM_USERROW) +#define avr_locate_userrow(p) avr_locate_mem_by_type((p), MEM_USERROW) +#define avr_locate_data(p) avr_locate_mem_by_type((p), MEM_SRAM) +#define avr_locate_io(p) avr_locate_mem_by_type((p), MEM_IO) +#define avr_locate_sib(p) avr_locate_mem_by_type((p), MEM_SIB) + +#define avr_locate_fuse(p) avr_locate_mem_by_type((p), MEM_IS_A_FUSE | MEM_FUSE0) +#define avr_locate_lfuse(p) avr_locate_mem_by_type((p), MEM_IS_A_FUSE | MEM_FUSE0) +#define avr_locate_hfuse(p) avr_locate_mem_by_type((p), MEM_IS_A_FUSE | MEM_FUSE1) +#define avr_locate_efuse(p) avr_locate_mem_by_type((p), MEM_IS_A_FUSE | MEM_FUSE2) +#define avr_locate_fuse0(p) avr_locate_mem_by_type((p), MEM_IS_A_FUSE | MEM_FUSE0) +#define avr_locate_wdtcfg(p) avr_locate_mem_by_type((p), MEM_IS_A_FUSE | MEM_FUSE0) +#define avr_locate_fuse1(p) avr_locate_mem_by_type((p), MEM_IS_A_FUSE | MEM_FUSE1) +#define avr_locate_bodcfg(p) avr_locate_mem_by_type((p), MEM_IS_A_FUSE | MEM_FUSE1) +#define avr_locate_fuse2(p) avr_locate_mem_by_type((p), MEM_IS_A_FUSE | MEM_FUSE2) +#define avr_locate_osccfg(p) avr_locate_mem_by_type((p), MEM_IS_A_FUSE | MEM_FUSE2) +#define avr_locate_fuse4(p) avr_locate_mem_by_type((p), MEM_IS_A_FUSE | MEM_FUSE4) +#define avr_locate_tcd0cfg(p) avr_locate_mem_by_type((p), MEM_IS_A_FUSE | MEM_FUSE4) +#define avr_locate_fuse5(p) avr_locate_mem_by_type((p), MEM_IS_A_FUSE | MEM_FUSE5) +#define avr_locate_syscfg0(p) avr_locate_mem_by_type((p), MEM_IS_A_FUSE | MEM_FUSE5) +#define avr_locate_fuse6(p) avr_locate_mem_by_type((p), MEM_IS_A_FUSE | MEM_FUSE6) +#define avr_locate_syscfg1(p) avr_locate_mem_by_type((p), MEM_IS_A_FUSE | MEM_FUSE6) +#define avr_locate_fuse7(p) avr_locate_mem_by_type((p), MEM_IS_A_FUSE | MEM_FUSE7) +#define avr_locate_append(p) avr_locate_mem_by_type((p), MEM_IS_A_FUSE | MEM_FUSE7) +#define avr_locate_codesize(p) avr_locate_mem_by_type((p), MEM_IS_A_FUSE | MEM_FUSE7) +#define avr_locate_fuse8(p) avr_locate_mem_by_type((p), MEM_IS_A_FUSE | MEM_FUSE8) +#define avr_locate_bootend(p) avr_locate_mem_by_type((p), MEM_IS_A_FUSE | MEM_FUSE8) +#define avr_locate_bootsize(p) avr_locate_mem_by_type((p), MEM_IS_A_FUSE | MEM_FUSE8) +#define avr_locate_fusea(p) avr_locate_mem_by_type((p), MEM_IS_A_FUSE | MEM_FUSEA) +#define avr_locate_pdicfg(p) avr_locate_mem_by_type((p), MEM_IS_A_FUSE | MEM_FUSEA) + +// Fuse offset and memory type/attribute macros +#define mem_is_eeprom(mem) (!!((mem)->type & MEM_EEPROM)) +#define mem_is_flash(mem) (!!((mem)->type & MEM_FLASH)) +#define mem_is_application(mem) (!!((mem)->type & MEM_APPLICATION)) +#define mem_is_apptable(mem) (!!((mem)->type & MEM_APPTABLE)) +#define mem_is_boot(mem) (!!((mem)->type & MEM_BOOT)) +#define mem_is_fuses(mem) (!!((mem)->type & MEM_FUSES)) +#define mem_is_lock(mem) (!!((mem)->type & MEM_LOCK)) +#define mem_is_sigrow(mem) (!!((mem)->type & MEM_SIGROW)) +#define mem_is_signature(mem) (!!((mem)->type & MEM_SIGNATURE)) +#define mem_is_calibration(mem) (!!((mem)->type & MEM_CALIBRATION)) +#define mem_is_tempsense(mem) (!!((mem)->type & MEM_TEMPSENSE)) +#define mem_is_sernum(mem) (!!((mem)->type & MEM_SERNUM)) +#define mem_is_osccal16(mem) (!!((mem)->type & MEM_OSCCAL16)) +#define mem_is_osccal20(mem) (!!((mem)->type & MEM_OSCCAL20)) +#define mem_is_osc16err(mem) (!!((mem)->type & MEM_OSC16ERR)) +#define mem_is_osc20err(mem) (!!((mem)->type & MEM_OSC20ERR)) +#define mem_is_bootrow(mem) (!!((mem)->type & MEM_BOOTROW)) +#define mem_is_userrow(mem) (!!((mem)->type & MEM_USERROW)) +#define mem_is_data(mem) (!!((mem)->type & MEM_SRAM)) +#define mem_is_io(mem) (!!((mem)->type & MEM_IO)) +#define mem_is_sib(mem) (!!((mem)->type & MEM_SIB)) + +#define mem_is_in_flash(mem) (!!((mem)->type & MEM_IN_FLASH)) +#define mem_is_a_fuse(mem) (!!((mem)->type & MEM_IS_A_FUSE)) +#define mem_is_in_fuses(mem) (!!((mem)->type & (MEM_FUSES | MEM_IS_A_FUSE))) // If fuses exists, that is +#define mem_is_user_type(mem) (!!((mem)->type & MEM_USER_TYPE)) +#define mem_is_in_sigrow(mem) (!!((mem)->type & MEM_IN_SIGROW)) // If sigrow exists, that is +#define mem_is_readonly(mem) (!!((mem)->type & MEM_READONLY)) +#define mem_is_paged_type(mem) (!!((mem)->type & (MEM_IN_FLASH | MEM_EEPROM | MEM_USER_TYPE))) +#define mem_is_lfuse(m) (((m)->type&(MEM_IS_A_FUSE|MEM_FUSEOFF_MASK)) == (MEM_IS_A_FUSE|MEM_FUSE0)) +#define mem_is_hfuse(m) (((m)->type&(MEM_IS_A_FUSE|MEM_FUSEOFF_MASK)) == (MEM_IS_A_FUSE|MEM_FUSE1)) +#define mem_is_efuse(m) (((m)->type&(MEM_IS_A_FUSE|MEM_FUSEOFF_MASK)) == (MEM_IS_A_FUSE|MEM_FUSE2)) +#define mem_is_fuse0(m) (((m)->type&(MEM_IS_A_FUSE|MEM_FUSEOFF_MASK)) == (MEM_IS_A_FUSE|MEM_FUSE0)) +#define mem_is_fuse1(m) (((m)->type&(MEM_IS_A_FUSE|MEM_FUSEOFF_MASK)) == (MEM_IS_A_FUSE|MEM_FUSE1)) +#define mem_is_fuse2(m) (((m)->type&(MEM_IS_A_FUSE|MEM_FUSEOFF_MASK)) == (MEM_IS_A_FUSE|MEM_FUSE2)) +#define mem_is_fuse4(m) (((m)->type&(MEM_IS_A_FUSE|MEM_FUSEOFF_MASK)) == (MEM_IS_A_FUSE|MEM_FUSE4)) +#define mem_is_fuse5(m) (((m)->type&(MEM_IS_A_FUSE|MEM_FUSEOFF_MASK)) == (MEM_IS_A_FUSE|MEM_FUSE5)) +#define mem_is_fuse6(m) (((m)->type&(MEM_IS_A_FUSE|MEM_FUSEOFF_MASK)) == (MEM_IS_A_FUSE|MEM_FUSE6)) +#define mem_is_fuse7(m) (((m)->type&(MEM_IS_A_FUSE|MEM_FUSEOFF_MASK)) == (MEM_IS_A_FUSE|MEM_FUSE7)) +#define mem_is_fuse8(m) (((m)->type&(MEM_IS_A_FUSE|MEM_FUSEOFF_MASK)) == (MEM_IS_A_FUSE|MEM_FUSE8)) +#define mem_is_fusea(m) (((m)->type&(MEM_IS_A_FUSE|MEM_FUSEOFF_MASK)) == (MEM_IS_A_FUSE|MEM_FUSEA)) + +#define mem_fuse_offset(mem) ((mem)->type & MEM_FUSEOFF_MASK) // Valid if mem_is_a_fuse(mem) + typedef struct avrmem { const char *desc; /* memory description ("flash", "eeprom", etc) */ + memtype_t type; /* internally used type, cannot be set in conf files */ LISTID comments; // Used by developer options -p*/[ASsr...] - int paged; /* page addressed (e.g. ATmega flash) */ + int paged; /* 16-bit page addressed, e.g., ATmega flash but not EEPROM */ int size; /* total memory size in bytes */ int page_size; /* size of memory page (if page addressed) */ int num_pages; /* number of pages (if page addressed) */ @@ -335,7 +481,7 @@ typedef struct avrmem { unsigned int offset; /* offset in IO memory (ATxmega) */ int min_write_delay; /* microseconds */ int max_write_delay; /* microseconds */ - int pwroff_after_write; /* after this memory type is written to, + int pwroff_after_write; /* after this memory is written to, the device must be powered off and back on, see errata https://www.microchip.com/content/dam/mchp/documents/OTH/ProductDocuments/DataSheets/doc1042.pdf */ @@ -379,7 +525,7 @@ const char *opcodename(int opnum); char *opcode2str(const OPCODE *op, int opnum, int detailed); /* Functions for AVRMEM structures */ -AVRMEM * avr_new_memtype(void); +AVRMEM * avr_new_mem(void); AVRMEM_ALIAS * avr_new_memalias(void); int avr_initmem(const AVRPART *p); AVRMEM * avr_dup_mem(const AVRMEM *m); @@ -387,6 +533,9 @@ void avr_free_mem(AVRMEM * m); void avr_free_memalias(AVRMEM_ALIAS * m); AVRMEM * avr_locate_mem(const AVRPART *p, const char *desc); AVRMEM * avr_locate_mem_noalias(const AVRPART *p, const char *desc); +AVRMEM * avr_locate_fuse_by_offset(const AVRPART *p, unsigned int off); +AVRMEM * avr_locate_mem_by_type(const AVRPART *p, memtype_t type); +unsigned int avr_data_offset(const AVRPART *p); AVRMEM_ALIAS * avr_locate_memalias(const AVRPART *p, const char *desc); AVRMEM_ALIAS * avr_find_memalias(const AVRPART *p, const AVRMEM *m_orig); void avr_mem_display(const char *prefix, FILE *f, const AVRMEM *m, @@ -398,8 +547,7 @@ AVRPART * avr_dup_part(const AVRPART *d); void avr_free_part(AVRPART * d); AVRPART * locate_part(const LISTID parts, const char *partdesc); AVRPART * locate_part_by_avr910_devcode(const LISTID parts, int devcode); -AVRPART * locate_part_by_signature(const LISTID parts, unsigned char *sig, - int sigsize); +AVRPART * locate_part_by_signature(const LISTID parts, unsigned char *sig, int sigsize); void avr_display(FILE *f, const AVRPART *p, const char *prefix, int verbose); typedef void (*walk_avrparts_cb)(const char *name, const char *desc, @@ -838,7 +986,7 @@ typedef struct programmer_t { unsigned long addr, unsigned char *value); int (*read_sig_bytes) (const struct programmer_t *pgm, const AVRPART *p, const AVRMEM *m); int (*read_sib) (const struct programmer_t *pgm, const AVRPART *p, char *sib); - int (*read_chip_rev) (const struct programmer_t *pgm, const AVRPART *p, char *chip_rev); + int (*read_chip_rev) (const struct programmer_t *pgm, const AVRPART *p, unsigned char *chip_rev); int (*term_keep_alive)(const struct programmer_t *pgm, const AVRPART *p); void (*print_parms) (const struct programmer_t *pgm, FILE *fp); int (*set_vtarget) (const struct programmer_t *pgm, double v); @@ -921,7 +1069,7 @@ void sort_programmers(LISTID programmers); typedef void (*FP_UpdateProgress)(int percent, double etime, const char *hdr, int finish); extern struct avrpart parts[]; -extern const char *avr_mem_order[100]; +extern memtable_t avr_mem_order[100]; extern FP_UpdateProgress update_progress; @@ -937,7 +1085,7 @@ int avr_read_byte_default(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM int avr_read_mem(const PROGRAMMER * pgm, const AVRPART *p, const AVRMEM *mem, const AVRPART *v); -int avr_read(const PROGRAMMER * pgm, const AVRPART *p, const char *memtype, const AVRPART *v); +int avr_read(const PROGRAMMER * pgm, const AVRPART *p, const char *memstr, const AVRPART *v); int avr_write_page(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *mem, unsigned long addr); @@ -962,7 +1110,7 @@ int avr_write_byte_default(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM int avr_write_mem(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *mem, int size, int auto_erase); -int avr_write(const PROGRAMMER *pgm, const AVRPART *p, const char *memtype, int size, int auto_erase); +int avr_write(const PROGRAMMER *pgm, const AVRPART *p, const char *memstr, int size, int auto_erase); int avr_signature(const PROGRAMMER *pgm, const AVRPART *p); @@ -976,18 +1124,12 @@ int avr_put_cycle_count(const PROGRAMMER *pgm, const AVRPART *p, int cycles); char *avr_prog_modes(int pm); -void avr_add_mem_order(const char *str); - -int avr_memtype_is_flash_type(const char *mem); +int avr_get_mem_type(const char *str); int avr_mem_is_flash_type(const AVRMEM *mem); -int avr_memtype_is_eeprom_type(const char *mem); - int avr_mem_is_eeprom_type(const AVRMEM *mem); -int avr_memtype_is_usersig_type(const char *mem); - int avr_mem_is_usersig_type(const AVRMEM *mem); int avr_mem_is_known(const char *str); @@ -1003,6 +1145,8 @@ int avr_unlock(const PROGRAMMER *pgm, const AVRPART *p); void report_progress(int completed, int total, const char *hdr); +void trace_buffer(const char *funstr, const unsigned char *buf, size_t buflen); + int avr_has_paged_access(const PROGRAMMER *pgm, const AVRMEM *m); int avr_read_page_default(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *mem, int addr, unsigned char *buf); @@ -1077,7 +1221,7 @@ int fileio_fmt_autodetect_fp(FILE *f); int fileio_fmt_autodetect(const char *fname); int fileio(int oprwv, const char *filename, FILEFMT format, - const AVRPART *p, const char *memtype, int size); + const AVRPART *p, const char *memstr, int size); int segment_normalise(const AVRMEM *mem, Segment_t *segp); @@ -1107,7 +1251,7 @@ enum updateflags { typedef struct update_t { const char *cmdline; // -T line is stored here and takes precedence if it exists - char *memtype; // Memory name for -U + char *memstr; // Memory name for -U int op; // Symbolic memory operation DEVICE_... for -U char *filename; // Filename for -U, can be - int format; // File format FMT_... @@ -1136,7 +1280,7 @@ extern void free_update(UPDATE *upd); char *update_str(const UPDATE *upd); int do_op(const PROGRAMMER *pgm, const AVRPART *p, const UPDATE *upd, enum updateflags flags); -int memstats(const AVRPART *p, const char *memtype, int size, Filestats *fsp); +int memstats(const AVRPART *p, const char *memstr, int size, Filestats *fsp); // Helper functions for dry run to determine file access int update_is_okfile(const char *fn); @@ -1302,7 +1446,7 @@ bool is_bigendian(); void change_endian(void *p, int size); int memall(const void *p, char c, size_t n); unsigned long long int str_ull(const char *str, char **endptr, int base); -Str2data *str_todata(const char *str, int type, const AVRPART *part, const char *memtype); +Str2data *str_todata(const char *str, int type, const AVRPART *part, const char *memstr); void str_freedata(Str2data *sd); unsigned long long int str_int(const char *str, int type, const char **errpp); int str_membuf(const char *str, int type, unsigned char *buf, int size, const char **errpp); diff --git a/src/main.c b/src/main.c index b872ed8ab..2d0b3fa62 100644 --- a/src/main.c +++ b/src/main.c @@ -244,7 +244,7 @@ static void usage(void) " -O Perform RC oscillator calibration (see AVR053)\n" " -t Run an interactive terminal when it is its turn\n" " -T Run terminal line when it is its turn\n" - " -U :r|w|v:[:format]\n" + " -U :r|w|v:[:format]\n" " Carry out memory operation when it is its turn\n" " Multiple -t, -T and -U options can be specified\n" " -n Do not write to the device whilst processing -U\n" @@ -1342,12 +1342,12 @@ int main(int argc, char * argv []) int doexit = 0; for (ln=lfirst(updates); ln; ln=lnext(ln)) { upd = ldata(ln); - if (upd->memtype == NULL && upd->cmdline == NULL) { + if (upd->memstr == NULL && upd->cmdline == NULL) { const char *mtype = p->prog_modes & PM_PDI? "application": "flash"; - pmsg_notice2("defaulting memtype in -U %c:%s option to \"%s\"\n", + pmsg_notice2("defaulting memstr in -U %c:%s option to \"%s\"\n", (upd->op == DEVICE_READ)? 'r': (upd->op == DEVICE_WRITE)? 'w': 'v', upd->filename, mtype); - upd->memtype = cfg_strdup("main()", mtype); + upd->memstr = cfg_strdup("main()", mtype); } rc = update_dryrun(p, upd); if (rc && rc != LIBAVRDUDE_SOFTFAIL) @@ -1471,7 +1471,7 @@ int main(int argc, char * argv []) } } - sig = avr_locate_mem(p, "signature"); + sig = avr_locate_signature(p); if (sig == NULL) pmsg_warning("signature memory not defined for device %s\n", p->desc); @@ -1546,9 +1546,9 @@ int main(int argc, char * argv []) uflags &= ~UF_AUTO_ERASE; for (ln=lfirst(updates); ln; ln=lnext(ln)) { upd = ldata(ln); - if(!upd->memtype) + if(!upd->memstr) continue; - m = avr_locate_mem(p, upd->memtype); + m = avr_locate_mem(p, upd->memstr); if (m == NULL) continue; if(str_eq(m->desc, memname) && upd->op == DEVICE_WRITE) { @@ -1588,6 +1588,7 @@ int main(int argc, char * argv []) int wrmem = 0, terminal = 0; for (ln=lfirst(updates); ln; ln=lnext(ln)) { + const AVRMEM *m; upd = ldata(ln); if(upd->cmdline && wrmem) { // Invalidate cache if device was written to wrmem = 0; @@ -1602,7 +1603,7 @@ int main(int argc, char * argv []) if (rc && rc != LIBAVRDUDE_SOFTFAIL) { exitrc = 1; break; - } else if(rc == 0 && upd->op == DEVICE_WRITE && avr_memtype_is_flash_type(upd->memtype)) + } else if(rc == 0 && upd->op == DEVICE_WRITE && (m = avr_locate_mem(p, upd->memstr)) && mem_is_in_flash(m)) ce_delayed = 0; // Redeemed chip erase promise } pgm->flush_cache(pgm, p); diff --git a/src/micronucleus.c b/src/micronucleus.c index 2bf918fe5..e15a44dbb 100644 --- a/src/micronucleus.c +++ b/src/micronucleus.c @@ -814,10 +814,7 @@ static int micronucleus_read_byte(const PROGRAMMER *pgm, const AVRPART *p, const { pmsg_debug("micronucleus_read_byte(desc=%s, addr=0x%04lX)\n", mem->desc, addr); - if (str_eq(mem->desc, "lfuse") || - str_eq(mem->desc, "hfuse") || - str_eq(mem->desc, "efuse") || - str_eq(mem->desc, "lock")) + if (mem_is_a_fuse(mem) || mem_is_lock(mem)) { *value = 0xFF; return 0; @@ -850,7 +847,7 @@ static int micronucleus_paged_write(const PROGRAMMER *pgm, const AVRPART *p, con { pmsg_debug("micronucleus_paged_write(page_size=0x%X, addr=0x%X, n_bytes=0x%X)\n", page_size, addr, n_bytes); - if (str_eq(mem->desc, "flash")) + if (mem_is_flash(mem)) { pdata_t* pdata = PDATA(pgm); @@ -897,7 +894,7 @@ static int micronucleus_paged_write(const PROGRAMMER *pgm, const AVRPART *p, con } else { - pmsg_error("unsupported memory type: %s\n", mem->desc); + pmsg_error("unsupported memory %s\n", mem->desc); return -1; } } diff --git a/src/pickit2.c b/src/pickit2.c index c075916d1..52ac3793a 100644 --- a/src/pickit2.c +++ b/src/pickit2.c @@ -454,7 +454,7 @@ static int pickit2_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AVR unsigned int page_size, unsigned int addr, unsigned int n_bytes) { // only supporting flash & eeprom page reads - if ((!mem->paged || page_size <= 1) || (!str_eq(mem->desc, "flash") && !str_eq(mem->desc, "eeprom"))) + if ((!mem->paged || page_size <= 1) || (!mem_is_flash(mem) && !mem_is_eeprom(mem))) { return -1; } @@ -591,7 +591,7 @@ static int pickit2_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const A unsigned int page_size, unsigned int addr, unsigned int n_bytes) { // only paged write for flash implemented - if (!str_eq(mem->desc, "flash") && !str_eq(mem->desc, "eeprom")) + if (!mem_is_flash(mem) && !mem_is_eeprom(mem)) { pmsg_error("part does not support %d paged write of %s\n", page_size, mem->desc); return -1; diff --git a/src/ser_posix.c b/src/ser_posix.c index aec8ba5c5..b6c9dbd60 100644 --- a/src/ser_posix.c +++ b/src/ser_posix.c @@ -436,42 +436,19 @@ static void ser_rawclose(union filedescriptor *fd) { close(fd->ifd); } -static int ser_send(const union filedescriptor *fd, const unsigned char * buf, size_t buflen) { +static int ser_send(const union filedescriptor *fd, const unsigned char *buf, size_t len) { int rc; - const unsigned char * p = buf; - size_t len = buflen; - - if (!len) - return 0; - - if (verbose > 3) - { - pmsg_trace("send: "); - - while (buflen) { - unsigned char c = *buf; - if (isprint(c)) { - msg_trace("%c ", c); - } - else { - msg_trace(". "); - } - msg_trace("[%02x] ", c); - - buf++; - buflen--; - } - msg_trace("\n"); - } + if(verbose > 3) + trace_buffer(__func__, buf, len); - while (len) { - rc = write(fd->ifd, p, (len > 1024) ? 1024 : len); + while(len) { + rc = write(fd->ifd, buf, len > 1024? 1024: len); if (rc < 0) { pmsg_ext_error("unable to write: %s\n", strerror(errno)); return -1; } - p += rc; + buf += rc; len -= rc; } @@ -479,12 +456,12 @@ static int ser_send(const union filedescriptor *fd, const unsigned char * buf, s } -static int ser_recv(const union filedescriptor *fd, unsigned char * buf, size_t buflen) { +static int ser_recv(const union filedescriptor *fd, unsigned char *buf, size_t buflen) { struct timeval timeout, to2; fd_set rfds; int nfds; int rc; - unsigned char * p = buf; + unsigned char *p = buf; size_t len = 0; timeout.tv_sec = serial_recv_timeout / 1000L; @@ -512,7 +489,7 @@ static int ser_recv(const union filedescriptor *fd, unsigned char * buf, size_t } } - rc = read(fd->ifd, p, (buflen - len > 1024) ? 1024 : buflen - len); + rc = read(fd->ifd, p, buflen - len > 1024? 1024: buflen - len); if (rc < 0) { pmsg_ext_error("unable to read: %s\n", strerror(errno)); return -1; @@ -521,27 +498,8 @@ static int ser_recv(const union filedescriptor *fd, unsigned char * buf, size_t len += rc; } - p = buf; - - if (verbose > 3) - { - pmsg_trace("recv: "); - - while (len) { - unsigned char c = *p; - if (isprint(c)) { - msg_trace("%c ", c); - } - else { - msg_trace(". "); - } - msg_trace("[%02x] ", c); - - p++; - len--; - } - msg_trace("\n"); - } + if(verbose > 3) + trace_buffer(__func__, buf, len); return 0; } diff --git a/src/ser_win32.c b/src/ser_win32.c index b7480ac2f..08aad21df 100644 --- a/src/ser_win32.c +++ b/src/ser_win32.c @@ -256,12 +256,7 @@ static int ser_open(const char *port, union pinfo pinfo, union filedescriptor *f if (str_casestarts(port, "com")) { // prepend "\\\\.\\" to name, required for port # >= 10 - newname = malloc(strlen("\\\\.\\") + strlen(port) + 1); - - if (newname == 0) { - pmsg_error("out of memory\n"); - exit(1); - } + newname = cfg_malloc(__func__, strlen("\\\\.\\") + strlen(port) + 1); strcpy(newname, "\\\\.\\"); strcat(newname, port); @@ -346,42 +341,23 @@ static int ser_set_dtr_rts(const union filedescriptor *fd, int is_on) { } } -static int net_send(const union filedescriptor *fd, const unsigned char * buf, size_t buflen) { +static int net_send(const union filedescriptor *fd, const unsigned char *buf, size_t len) { LPVOID lpMsgBuf; int rc; - const unsigned char *p = buf; - size_t len = buflen; if (fd->ifd < 0) { pmsg_notice("net_send(): connection not open\n"); - exit(1); + return -1; } - if (!len) { + if (!len) return 0; - } - - if (verbose > 3) { - pmsg_trace("send: "); - while (buflen) { - unsigned char c = *buf; - if (isprint(c)) { - msg_trace("%c ", c); - } else { - msg_trace(". "); - } - msg_trace("[%02x] ", c); - - buf++; - buflen--; - } - - msg_trace("\n"); - } + if (verbose > 3) + trace_buffer(__func__, buf, len); while (len) { - rc = send(fd->ifd, (const char *) p, (len > 1024)? 1024: len, 0); + rc = send(fd->ifd, (const char *) buf, len > 1024? 1024: len, 0); if (rc < 0) { FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | @@ -395,9 +371,9 @@ static int net_send(const union filedescriptor *fd, const unsigned char * buf, s NULL); pmsg_error("unable to send: %s\n", (char *) lpMsgBuf); LocalFree(lpMsgBuf); - exit(1); + return -1; } - p += rc; + buf += rc; len -= rc; } @@ -405,15 +381,11 @@ static int net_send(const union filedescriptor *fd, const unsigned char * buf, s } -static int ser_send(const union filedescriptor *fd, const unsigned char * buf, size_t buflen) { - if (serial_over_ethernet) { - return net_send(fd, buf, buflen); - } +static int ser_send(const union filedescriptor *fd, const unsigned char *buf, size_t len) { + if (serial_over_ethernet) + return net_send(fd, buf, len); - size_t len = buflen; - unsigned char c='\0'; DWORD written; - const unsigned char * b = buf; HANDLE hComPort=(HANDLE)fd->pfd; @@ -423,35 +395,19 @@ static int ser_send(const union filedescriptor *fd, const unsigned char * buf, s } if (!len) - return 0; + return 0; if (verbose > 3) - { - pmsg_trace("send: "); - - while (len) { - c = *b; - if (isprint(c)) { - msg_trace("%c ", c); - } - else { - msg_trace(". "); - } - msg_trace("[%02x] ", c); - b++; - len--; - } - msg_trace("\n"); - } + trace_buffer(__func__, buf, len); serial_w32SetTimeOut(hComPort,500); - if (!WriteFile (hComPort, buf, buflen, &written, NULL)) { + if (!WriteFile (hComPort, buf, len, &written, NULL)) { pmsg_error("unable to write: %s\n", "sorry no info avail"); // TODO return -1; } - if (written != buflen) { + if (written != len) { pmsg_error("size/send mismatch\n"); return -1; } @@ -460,7 +416,7 @@ static int ser_send(const union filedescriptor *fd, const unsigned char * buf, s } -static int net_recv(const union filedescriptor *fd, unsigned char * buf, size_t buflen) { +static int net_recv(const union filedescriptor *fd, unsigned char *buf, size_t buflen) { LPVOID lpMsgBuf; struct timeval timeout, to2; fd_set rfds; @@ -471,7 +427,7 @@ static int net_recv(const union filedescriptor *fd, unsigned char * buf, size_t if (fd->ifd < 0) { pmsg_error("connection not open\n"); - exit(1); + return -1; } timeout.tv_sec = serial_recv_timeout / 1000L; @@ -506,7 +462,7 @@ static int net_recv(const union filedescriptor *fd, unsigned char * buf, size_t NULL); pmsg_error("select(): %s\n", (char *) lpMsgBuf); LocalFree(lpMsgBuf); - exit(1); + return -1; } } @@ -524,42 +480,22 @@ static int net_recv(const union filedescriptor *fd, unsigned char * buf, size_t NULL); pmsg_error("unable to read: %s\n", (char *) lpMsgBuf); LocalFree(lpMsgBuf); - exit(1); + return -1; } p += rc; len += rc; } - p = buf; - - if (verbose > 3) { - pmsg_trace("Recv: "); - - while (len) { - unsigned char c = *p; - if (isprint(c)) { - msg_trace("%c ", c); - } else { - msg_trace(". "); - } - msg_trace("[%02x] ", c); - - p++; - len--; - } - msg_trace("\n"); - } + if (verbose > 3) + trace_buffer(__func__, buf, len); return 0; } -static int ser_recv(const union filedescriptor *fd, unsigned char * buf, size_t buflen) { - if (serial_over_ethernet) { +static int ser_recv(const union filedescriptor *fd, unsigned char *buf, size_t buflen) { + if (serial_over_ethernet) return net_recv(fd, buf, buflen); - } - unsigned char c; - unsigned char * p = buf; DWORD read; HANDLE hComPort=(HANDLE)fd->pfd; @@ -582,9 +518,9 @@ static int ser_recv(const union filedescriptor *fd, unsigned char * buf, size_t MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpMsgBuf, 0, - NULL ); + NULL); pmsg_error("unable to read: %s\n", (char*) lpMsgBuf); - LocalFree( lpMsgBuf ); + LocalFree(lpMsgBuf); return -1; } @@ -594,28 +530,10 @@ static int ser_recv(const union filedescriptor *fd, unsigned char * buf, size_t return -1; } - p = buf; - if (verbose > 3) - { - pmsg_trace("recv: "); - - while (read) { - c = *p; - if (isprint(c)) { - msg_trace("%c ", c); - } - else { - msg_trace(". "); - } - msg_trace("[%02x] ", c); + trace_buffer(__func__, buf, read); - p++; - read--; - } - msg_trace("\n"); - } - return 0; + return 0; } static int net_drain(const union filedescriptor *fd, int display) { @@ -628,7 +546,7 @@ static int net_drain(const union filedescriptor *fd, int display) { if (fd->ifd < 0) { pmsg_error("connection not open\n"); - exit(1); + return -1; } if (display) { @@ -667,7 +585,7 @@ static int net_drain(const union filedescriptor *fd, int display) { NULL); pmsg_error("select(): %s\n", (char *) lpMsgBuf); LocalFree(lpMsgBuf); - exit(1); + return -1; } } @@ -685,7 +603,7 @@ static int net_drain(const union filedescriptor *fd, int display) { NULL); pmsg_error("unable to read: %s\n", (char *) lpMsgBuf); LocalFree(lpMsgBuf); - exit(1); + return -1; } if (display) { diff --git a/src/serialupdi.c b/src/serialupdi.c index f93eb5de1..ab656945b 100644 --- a/src/serialupdi.c +++ b/src/serialupdi.c @@ -693,7 +693,7 @@ static int serialupdi_read_byte(const PROGRAMMER *pgm, const AVRPART *p, const A Return("cannot read byte from %s %s as address 0x%04lx outside range [0, 0x%04x]", p->desc, mem->desc, addr, mem->size-1); - if(str_eq(mem->desc, "sib")) { + if(mem_is_sib(mem)) { if(addr >= SIB_INFO_STRING_LENGTH) Return("cannot read byte from %s sib as address 0x%04lx outside range [0, 0x%04x]", p->desc, addr, SIB_INFO_STRING_LENGTH-1); @@ -717,33 +717,29 @@ static int serialupdi_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const Return("cannot write byte to %s %s as address 0x%04lx outside range [0, 0x%04x]", p->desc, mem->desc, addr, mem->size-1); - if (str_contains(mem->desc, "fuse")) { + if (mem_is_a_fuse(mem) || mem_is_fuses(mem)) { return updi_nvm_write_fuse(pgm, p, mem->offset + addr, value); } - if (str_eq(mem->desc, "lock")) { + if (mem_is_lock(mem)) { return updi_nvm_write_fuse(pgm, p, mem->offset + addr, value); } - if (str_eq(mem->desc, "eeprom")) { + if (mem_is_eeprom(mem)) { unsigned char buffer[1]; buffer[0]=value; return updi_nvm_write_eeprom(pgm, p, mem->offset + addr, buffer, 1); } - if (str_eq(mem->desc, "flash")) { + if (mem_is_flash(mem)) { unsigned char buffer[1]; buffer[0]=value; return updi_nvm_write_flash(pgm, p, mem->offset + addr, buffer, 1); } // Read-only memories - if(str_eq(mem->desc, "osc16err") || str_eq(mem->desc, "osccal16") || - str_eq(mem->desc, "osc20err") || str_eq(mem->desc, "osccal20") || - str_eq(mem->desc, "prodsig") || str_eq(mem->desc, "sernum") || - str_eq(mem->desc, "signature") || str_eq(mem->desc, "sib")) { - + if(mem_is_readonly(mem)) { unsigned char is; if(serialupdi_read_byte(pgm, p, mem, addr, &is) >= 0 && is == value) return 0; - Return("cannot write to read-only memory %s %s", p->desc, mem->desc); + Return("cannot write to read-only memory %s", mem->desc); } return updi_write_byte(pgm, mem->offset + addr, value); @@ -800,20 +796,20 @@ static int serialupdi_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const while (remaining_bytes > 0) { - if (str_eq(m->desc, "eeprom")) { + if (mem_is_eeprom(m)) { rc = updi_nvm_write_eeprom(pgm, p, m->offset + write_offset, m->buf + write_offset, remaining_bytes > m->page_size ? m->page_size : remaining_bytes); - } else if (str_eq(m->desc, "flash")) { + } else if (mem_is_flash(m)) { rc = updi_nvm_write_flash(pgm, p, m->offset + write_offset, m->buf + write_offset, remaining_bytes > m->page_size ? m->page_size : remaining_bytes); - } else if (str_eq(m->desc, "userrow")) { + } else if (mem_is_userrow(m)) { rc = serialupdi_write_userrow(pgm, p, m, page_size, write_offset, remaining_bytes > m->page_size ? m->page_size : remaining_bytes); - } else if (str_eq(m->desc, "fuses")) { + } else if (mem_is_fuses(m)) { pmsg_debug("page write operation requested for fuses, falling back to byte-level write\n"); return -1; } else { - pmsg_error("invalid memory type: <%s:%d>, 0x%06X, %d (0x%04X)\n", m->desc, page_size, addr, n_bytes, n_bytes); + pmsg_error("invalid memory <%s:%d>, 0x%06X, %d (0x%04X)\n", m->desc, page_size, addr, n_bytes, n_bytes); rc = -1; } @@ -828,17 +824,17 @@ static int serialupdi_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const } return write_bytes; } else { - if (str_eq(m->desc, "eeprom")) { + if (mem_is_eeprom(m)) { rc = updi_nvm_write_eeprom(pgm, p, m->offset+addr, m->buf+addr, n_bytes); - } else if (str_eq(m->desc, "flash")) { + } else if (mem_is_flash(m)) { rc = updi_nvm_write_flash(pgm, p, m->offset+addr, m->buf+addr, n_bytes); - } else if (str_eq(m->desc, "userrow")) { + } else if (mem_is_userrow(m)) { rc = serialupdi_write_userrow(pgm, p, m, page_size, addr, n_bytes); - } else if (str_eq(m->desc, "fuses")) { + } else if (mem_is_fuses(m)) { pmsg_debug("page write operation requested for fuses, falling back to byte-level write\n"); rc = -1; } else { - pmsg_error("invalid memory type: <%s:%d>, 0x%06X, %d (0x%04X)\n", m->desc, page_size, addr, n_bytes, n_bytes); + pmsg_error("invalid memory: <%s:%d>, 0x%06X, %d (0x%04X)\n", m->desc, page_size, addr, n_bytes, n_bytes); rc = -1; } return rc; diff --git a/src/stk500.c b/src/stk500.c index b4fc90bdd..1563ba9fa 100644 --- a/src/stk500.c +++ b/src/stk500.c @@ -445,31 +445,15 @@ static int stk500_initialize(const PROGRAMMER *pgm, const AVRPART *p) { buf[5] = 1; /* polling supported - XXX need this in config file */ buf[6] = 1; /* programming is self-timed - XXX need in config file */ - m = avr_locate_mem(p, "lock"); - if (m) - buf[7] = m->size; - else - buf[7] = 0; + buf[7] = (m = avr_locate_lock(p))? m->size: 0; - /* - * number of fuse bytes - */ + // Number of fuse bytes (for classic parts) buf[8] = 0; - m = avr_locate_mem(p, "fuse"); - if (m) - buf[8] += m->size; - m = avr_locate_mem(p, "lfuse"); - if (m) - buf[8] += m->size; - m = avr_locate_mem(p, "hfuse"); - if (m) - buf[8] += m->size; - m = avr_locate_mem(p, "efuse"); - if (m) - buf[8] += m->size; - - m = avr_locate_mem(p, "flash"); - if (m) { + for(int fu = 0; fu < 3; fu++) + if((m = avr_locate_fuse_by_offset(p, fu))) + buf[8] += m->size; + + if ((m = avr_locate_flash(p))) { buf[9] = m->readback[0]; buf[10] = m->readback[1]; if (m->paged) { @@ -480,10 +464,9 @@ static int stk500_initialize(const PROGRAMMER *pgm, const AVRPART *p) { buf[18] = (m->size >> 16) & 0xff; buf[19] = (m->size >> 8) & 0xff; buf[20] = m->size & 0xff; - } - else { + } else { buf[9] = 0xff; - buf[10] = 0xff; + buf[10] = 0xff; buf[13] = 0; buf[14] = 0; buf[17] = 0; @@ -492,14 +475,12 @@ static int stk500_initialize(const PROGRAMMER *pgm, const AVRPART *p) { buf[20] = 0; } - m = avr_locate_mem(p, "eeprom"); - if (m) { + if ((m = avr_locate_eeprom(p))) { buf[11] = m->readback[0]; buf[12] = m->readback[1]; buf[15] = (m->size >> 8) & 0x00ff; buf[16] = m->size & 0x00ff; - } - else { + } else { buf[11] = 0xff; buf[12] = 0xff; buf[15] = 0; @@ -819,7 +800,7 @@ static void stk500_enable(PROGRAMMER *pgm, const AVRPART *p) { AVRMEM *mem; if(pgm->prog_modes & PM_SPM) // For bootloaders (eg, arduino) if(!(p->prog_modes & (PM_UPDI | PM_PDI | PM_aWire))) // Classic parts, eg, optiboot with word addresses - if((mem = avr_locate_mem(p, "eeprom"))) + if((mem = avr_locate_eeprom(p))) if(mem->page_size == 1) // Increase pagesize if it is 1 mem->page_size = 16; return; @@ -949,9 +930,9 @@ static int stk500_loadaddr(const PROGRAMMER *pgm, const AVRMEM *mem, unsigned in } -static int set_memtype_a_div(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m, int *memtypep, int *a_divp) { - if(avr_mem_is_flash_type(m)) { - *memtypep = 'F'; +static int set_memchr_a_div(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m, int *memchrp, int *a_divp) { + if(mem_is_in_flash(m)) { + *memchrp = 'F'; if(!(pgm->prog_modes & PM_SPM)) // Programmer *not* for bootloaders: original stk500v1 protocol *a_divp = m->op[AVR_OP_LOADPAGE_LO] || m->op[AVR_OP_READ_LO]? 2: 1; else if(!(p->prog_modes & (PM_UPDI | PM_PDI | PM_aWire))) @@ -961,8 +942,8 @@ static int set_memtype_a_div(const PROGRAMMER *pgm, const AVRPART *p, const AVRM return 0; } - if(avr_mem_is_eeprom_type(m)) { - *memtypep = 'E'; + if(mem_is_eeprom(m)) { + *memchrp = 'E'; // Word addr for bootloaders or Arduino as ISP if part is a "classic" part, byte addr otherwise *a_divp = ((pgm->prog_modes & PM_SPM) || str_caseeq(pgmid, "arduino_as_isp")) \ && !(p->prog_modes & (PM_UPDI | PM_PDI))? 2: 1; @@ -978,14 +959,14 @@ static int stk500_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AVR unsigned int addr, unsigned int n_bytes) { unsigned char* buf = alloca(page_size + 16); - int memtype; + int memchr; int a_div; int block_size; int tries; unsigned int n; unsigned int i; - if(set_memtype_a_div(pgm, p, m, &memtype, &a_div) < 0) + if(set_memchr_a_div(pgm, p, m, &memchr, &a_div) < 0) return -2; n = addr + n_bytes; @@ -1019,7 +1000,7 @@ static int stk500_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AVR buf[i++] = Cmnd_STK_PROG_PAGE; buf[i++] = (block_size >> 8) & 0xff; buf[i++] = block_size & 0xff; - buf[i++] = memtype; + buf[i++] = memchr; memcpy(&buf[i], &m->buf[addr], block_size); i += block_size; buf[i++] = Sync_CRC_EOP; @@ -1060,13 +1041,13 @@ static int stk500_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AVRM unsigned int addr, unsigned int n_bytes) { unsigned char buf[16]; - int memtype; + int memchr; int a_div; int tries; unsigned int n; int block_size; - if(set_memtype_a_div(pgm, p, m, &memtype, &a_div) < 0) + if(set_memchr_a_div(pgm, p, m, &memchr, &a_div) < 0) return -2; n = addr + n_bytes; @@ -1088,7 +1069,7 @@ static int stk500_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AVRM buf[0] = Cmnd_STK_READ_PAGE; buf[1] = (block_size >> 8) & 0xff; buf[2] = block_size & 0xff; - buf[3] = memtype; + buf[3] = memchr; buf[4] = Sync_CRC_EOP; stk500_send(pgm, buf, 5); diff --git a/src/stk500v2.c b/src/stk500v2.c index e3e51b0e5..95425c6a4 100644 --- a/src/stk500v2.c +++ b/src/stk500v2.c @@ -1235,8 +1235,8 @@ static int stk500v2_initialize(const PROGRAMMER *pgm, const AVRPART *p) { */ if (p->prog_modes & PM_PDI) { // Find the border between application and boot area - AVRMEM *bootmem = avr_locate_mem(p, "boot"); - AVRMEM *flashmem = avr_locate_mem(p, "flash"); + AVRMEM *bootmem = avr_locate_boot(p); + AVRMEM *flashmem = avr_locate_flash(p); if (bootmem == NULL || flashmem == NULL) { pmsg_error("cannot locate flash or boot memories\n"); } else { @@ -1360,14 +1360,14 @@ static int stk500v2_initialize(const PROGRAMMER *pgm, const AVRPART *p) { PDATA(pgm)->eeprom_pagesize = 1; for (ln = lfirst(p->mem); ln; ln = lnext(ln)) { m = ldata(ln); - if (str_eq(m->desc, "flash")) { + if (mem_is_flash(m)) { if (m->page_size > 1) { if (m->page_size > 256) PDATA(pgm)->flash_pagesize = 256; else PDATA(pgm)->flash_pagesize = m->page_size; } - } else if (str_eq(m->desc, "eeprom")) { + } else if (mem_is_eeprom(m)) { if (m->page_size > 1) PDATA(pgm)->eeprom_pagesize = m->page_size; } @@ -1508,14 +1508,14 @@ static int stk500v2_jtag3_initialize(const PROGRAMMER *pgm, const AVRPART *p) { PDATA(pgm)->eeprom_pagesize = 1; for (ln = lfirst(p->mem); ln; ln = lnext(ln)) { m = ldata(ln); - if (str_eq(m->desc, "flash")) { + if (mem_is_flash(m)) { if (m->page_size > 1) { if (m->page_size > 256) PDATA(pgm)->flash_pagesize = 256; else PDATA(pgm)->flash_pagesize = m->page_size; } - } else if (str_eq(m->desc, "eeprom")) { + } else if (mem_is_eeprom(m)) { if (m->page_size > 1) PDATA(pgm)->eeprom_pagesize = m->page_size; } @@ -1674,14 +1674,14 @@ static int stk500hv_initialize(const PROGRAMMER *pgm, const AVRPART *p, enum hvm PDATA(pgm)->eeprom_pagesize = 1; for (ln = lfirst(p->mem); ln; ln = lnext(ln)) { m = ldata(ln); - if (str_eq(m->desc, "flash")) { + if (mem_is_flash(m)) { if (m->page_size > 1) { if (m->page_size > 256) PDATA(pgm)->flash_pagesize = 256; else PDATA(pgm)->flash_pagesize = m->page_size; } - } else if (str_eq(m->desc, "eeprom")) { + } else if (mem_is_eeprom(m)) { if (m->page_size > 1) PDATA(pgm)->eeprom_pagesize = m->page_size; } @@ -1809,7 +1809,7 @@ static void stk500v2_enable(PROGRAMMER *pgm, const AVRPART *p) { stk600_setup_isp(pgm); } } - AVRMEM *mem = avr_locate_mem(p, "flash"); + AVRMEM *mem = avr_locate_flash(p); if(mem && mem->op[AVR_OP_WRITE_LO]) // Old part that can only write flash bytewise if(mem->page_size < 2) // Override page size, as STK500v2/EDBG uses flash word addresses mem->page_size = 2; @@ -2278,7 +2278,7 @@ static int stk500hv_read_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVR pmsg_notice2("stk500hv_read_byte(.., %s, 0x%lx, ...)\n", mem->desc, addr); - if (str_eq(mem->desc, "flash")) { + if (mem_is_flash(mem)) { buf[0] = mode == PPMODE? CMD_READ_FLASH_PP: CMD_READ_FLASH_HVSP; cmdlen = 3; pagesize = PDATA(pgm)->flash_pagesize; @@ -2295,7 +2295,7 @@ static int stk500hv_read_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVR if (mem->op[AVR_OP_LOAD_EXT_ADDR] != NULL) { use_ext_addr = (1U << 31); } - } else if (str_eq(mem->desc, "eeprom")) { + } else if (mem_is_eeprom(mem)) { buf[0] = mode == PPMODE? CMD_READ_EEPROM_PP: CMD_READ_EEPROM_HVSP; cmdlen = 3; pagesize = mem->page_size; @@ -2304,25 +2304,24 @@ static int stk500hv_read_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVR paddr = addr & ~(pagesize - 1); paddr_ptr = &PDATA(pgm)->eeprom_pageaddr; cache_ptr = PDATA(pgm)->eeprom_pagecache; - } else if (str_contains(mem->desc, "fuse") && strlen(mem->desc) <= 5) { + } else if (mem_is_a_fuse(mem) || mem_is_fuses(mem)) { buf[0] = mode == PPMODE? CMD_READ_FUSE_PP: CMD_READ_FUSE_HVSP; - if (str_eq(mem->desc, "lfuse") || str_eq(mem->desc, "fuse")) - addr = 0; - else if (str_eq(mem->desc, "hfuse")) - addr = 1; - else if (str_eq(mem->desc, "efuse")) - addr = 2; - } else if (str_eq(mem->desc, "lock")) { + if(mem_is_a_fuse(mem)) + addr = mem_fuse_offset(mem); + } else if (mem_is_lock(mem)) { buf[0] = mode == PPMODE? CMD_READ_LOCK_PP: CMD_READ_LOCK_HVSP; - } else if (str_eq(mem->desc, "calibration")) { + } else if (mem_is_calibration(mem)) { buf[0] = mode == PPMODE? CMD_READ_OSCCAL_PP: CMD_READ_OSCCAL_HVSP; - } else if (str_eq(mem->desc, "signature")) { + } else if (mem_is_signature(mem)) { buf[0] = mode == PPMODE? CMD_READ_SIGNATURE_PP: CMD_READ_SIGNATURE_HVSP; - } else if (str_eq(mem->desc, "prodsig")) { + } else if (mem_is_sigrow(mem)) { buf[0] = addr&1? (mode == PPMODE? CMD_READ_OSCCAL_PP: CMD_READ_OSCCAL_HVSP): (mode == PPMODE? CMD_READ_SIGNATURE_PP: CMD_READ_SIGNATURE_HVSP); addr /= 2; + } else { + pmsg_error("unsupported memory %s\n", mem->desc); + return -1; } /* @@ -2407,9 +2406,9 @@ static int stk500isp_read_byte(const PROGRAMMER *pgm, const AVRPART *p, const AV pmsg_notice2("stk500isp_read_byte(.., %s, 0x%lx, ...)\n", mem->desc, addr); - if (str_eq(mem->desc, "flash") || str_eq(mem->desc, "eeprom")) { + if (mem_is_flash(mem) || mem_is_eeprom(mem)) { // use paged access, and cache result - if (str_eq(mem->desc, "flash")) { + if (mem_is_flash(mem)) { pagesize = PDATA(pgm)->flash_pagesize; paddr = addr & ~(pagesize - 1); paddr_ptr = &PDATA(pgm)->flash_pageaddr; @@ -2438,25 +2437,23 @@ static int stk500isp_read_byte(const PROGRAMMER *pgm, const AVRPART *p, const AV return 0; } - if (str_contains(mem->desc, "fuse") && strlen(mem->desc) <= 5) { + if (mem_is_a_fuse(mem) || mem_is_fuses(mem)) { buf[0] = CMD_READ_FUSE_ISP; - if (str_eq(mem->desc, "lfuse") || str_eq(mem->desc, "fuse")) - addr = 0; - else if (str_eq(mem->desc, "hfuse")) - addr = 1; - else if (str_eq(mem->desc, "efuse")) - addr = 2; - } else if (str_eq(mem->desc, "lock")) { + if(mem_is_a_fuse(mem)) + addr = mem_fuse_offset(mem); + } else if (mem_is_lock(mem)) { buf[0] = CMD_READ_LOCK_ISP; - } else if (str_eq(mem->desc, "calibration")) { + } else if (mem_is_calibration(mem)) { buf[0] = CMD_READ_OSCCAL_ISP; - } else if (str_eq(mem->desc, "signature")) { + } else if (mem_is_signature(mem)) { buf[0] = CMD_READ_SIGNATURE_ISP; - } else if (str_eq(mem->desc, "prodsig")) { + } else if (mem_is_sigrow(mem)) { buf[0] = addr&1? CMD_READ_OSCCAL_ISP: CMD_READ_SIGNATURE_ISP; + } else { + pmsg_error("unsupported memory %s\n", mem->desc); + return -1; } - if ((op = mem->op[AVR_OP_READ]) == NULL) { pmsg_error("invalid operation AVR_OP_READ on %s memory\n", mem->desc); return -1; @@ -2499,7 +2496,7 @@ static int stk500hv_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const AV pmsg_notice2("stk500hv_write_byte(.., %s, 0x%lx, ...)\n", mem->desc, addr); - if (str_eq(mem->desc, "flash")) { + if (mem_is_flash(mem)) { buf[0] = mode == PPMODE? CMD_PROGRAM_FLASH_PP: CMD_PROGRAM_FLASH_HVSP; pagesize = PDATA(pgm)->flash_pagesize; paddr = addr & ~(pagesize - 1); @@ -2515,7 +2512,7 @@ static int stk500hv_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const AV if (mem->op[AVR_OP_LOAD_EXT_ADDR] != NULL) { use_ext_addr = (1U << 31); } - } else if (str_eq(mem->desc, "eeprom")) { + } else if (mem_is_eeprom(mem)) { buf[0] = mode == PPMODE? CMD_PROGRAM_EEPROM_PP: CMD_PROGRAM_EEPROM_HVSP; pagesize = mem->page_size; if (pagesize == 0) @@ -2523,22 +2520,18 @@ static int stk500hv_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const AV paddr = addr & ~(pagesize - 1); paddr_ptr = &PDATA(pgm)->eeprom_pageaddr; cache_ptr = PDATA(pgm)->eeprom_pagecache; - } else if (str_contains(mem->desc, "fuse") && strlen(mem->desc) <= 5) { + } else if (mem_is_a_fuse(mem) || mem_is_fuses(mem)) { buf[0] = mode == PPMODE? CMD_PROGRAM_FUSE_PP: CMD_PROGRAM_FUSE_HVSP; pulsewidth = p->programfusepulsewidth; timeout = p->programfusepolltimeout; - if (str_eq(mem->desc, "lfuse") || str_eq(mem->desc, "fuse")) - addr = 0; - else if (str_eq(mem->desc, "hfuse")) - addr = 1; - else if (str_eq(mem->desc, "efuse")) - addr = 2; - } else if (str_eq(mem->desc, "lock")) { + if(mem_is_a_fuse(mem)) + addr = mem_fuse_offset(mem); + } else if (mem_is_lock(mem)) { buf[0] = mode == PPMODE? CMD_PROGRAM_LOCK_PP: CMD_PROGRAM_LOCK_HVSP; pulsewidth = p->programlockpulsewidth; timeout = p->programlockpolltimeout; } else { - pmsg_error("unsupported memory type %s\n", mem->desc); + pmsg_error("unsupported memory %s\n", mem->desc); return -1; } @@ -2647,8 +2640,8 @@ static int stk500isp_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const A pmsg_notice2("stk500isp_write_byte(.., %s, 0x%lx, ...)\n", mem->desc, addr); - if (str_eq(mem->desc, "flash") || str_eq(mem->desc, "eeprom")) { - if (str_eq(mem->desc, "flash")) { + if (mem_is_flash(mem) || mem_is_eeprom(mem)) { + if (mem_is_flash(mem)) { pagesize = PDATA(pgm)->flash_pagesize; paddr = addr & ~(pagesize - 1); paddr_ptr = &PDATA(pgm)->flash_pageaddr; @@ -2684,18 +2677,20 @@ static int stk500isp_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const A } memset(buf, 0, sizeof buf); - if (str_contains(mem->desc, "fuse") && strlen(mem->desc) <= 5) { + if (mem_is_a_fuse(mem) || mem_is_fuses(mem)) { buf[0] = CMD_PROGRAM_FUSE_ISP; - if (str_eq(mem->desc, "lfuse") || str_eq(mem->desc, "fuse")) - addr = 0; - else if (str_eq(mem->desc, "hfuse")) - addr = 1; - else if (str_eq(mem->desc, "efuse")) - addr = 2; - } else if (str_eq(mem->desc, "lock")) { + if(mem_is_a_fuse(mem)) + addr = mem_fuse_offset(mem); + } else if (mem_is_lock(mem)) { buf[0] = CMD_PROGRAM_LOCK_ISP; + } else if(mem_is_readonly(mem)) { + unsigned char is; + if(pgm->read_byte(pgm, p, mem, addr, &is) >= 0 && is == data) + return 0; + pmsg_error("cannot write to read-only memory %s\n", mem->desc); + return -1; } else { - pmsg_error("unsupported memory type: %s\n", mem->desc); + pmsg_error("unsupported memory %s\n", mem->desc); return -1; } @@ -2751,7 +2746,7 @@ static int stk500v2_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const A use_ext_addr = 0; // determine which command is to be used - if (str_eq(m->desc, "flash")) { + if (mem_is_flash(m)) { addrshift = 1; PDATA(pgm)->flash_pageaddr = ~0UL; // Invalidate cache commandbuf[0] = CMD_PROGRAM_FLASH_ISP; @@ -2764,7 +2759,7 @@ static int stk500v2_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const A if (m->op[AVR_OP_LOAD_EXT_ADDR] != NULL) { use_ext_addr = (1U << 31); } - } else if (str_eq(m->desc, "eeprom")) { + } else if (mem_is_eeprom(m)) { PDATA(pgm)->eeprom_pageaddr = ~0UL; // Invalidate cache commandbuf[0] = CMD_PROGRAM_EEPROM_ISP; } @@ -2884,7 +2879,7 @@ static int stk500hv_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const A use_ext_addr = 0; // determine which command is to be used - if (str_eq(m->desc, "flash")) { + if (mem_is_flash(m)) { addrshift = 1; PDATA(pgm)->flash_pageaddr = (unsigned long)-1L; commandbuf[0] = mode == PPMODE? CMD_PROGRAM_FLASH_PP: CMD_PROGRAM_FLASH_HVSP; @@ -2897,7 +2892,7 @@ static int stk500hv_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const A if (m->op[AVR_OP_LOAD_EXT_ADDR] != NULL) { use_ext_addr = (1U << 31); } - } else if (str_eq(m->desc, "eeprom")) { + } else if (mem_is_eeprom(m)) { PDATA(pgm)->eeprom_pageaddr = (unsigned long)-1L; commandbuf[0] = mode == PPMODE? CMD_PROGRAM_EEPROM_PP: CMD_PROGRAM_EEPROM_HVSP; } @@ -3002,7 +2997,7 @@ static int stk500v2_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AV use_ext_addr = 0; // determine which command is to be used - if (str_eq(m->desc, "flash")) { + if (mem_is_flash(m)) { commandbuf[0] = CMD_READ_FLASH_ISP; rop = m->op[AVR_OP_READ_LO]; addrshift = 1; @@ -3016,7 +3011,7 @@ static int stk500v2_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AV use_ext_addr = (1U << 31); } } - else if (str_eq(m->desc, "eeprom")) { + else if (mem_is_eeprom(m)) { commandbuf[0] = CMD_READ_EEPROM_ISP; } @@ -3092,7 +3087,7 @@ static int stk500hv_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AV use_ext_addr = 0; // determine which command is to be used - if (str_eq(m->desc, "flash")) { + if (mem_is_flash(m)) { commandbuf[0] = mode == PPMODE? CMD_READ_FLASH_PP: CMD_READ_FLASH_HVSP; addrshift = 1; /* @@ -3105,7 +3100,7 @@ static int stk500hv_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AV use_ext_addr = (1U << 31); } } - else if (str_eq(m->desc, "eeprom")) { + else if (mem_is_eeprom(m)) { commandbuf[0] = mode == PPMODE? CMD_READ_EEPROM_PP: CMD_READ_EEPROM_HVSP; } @@ -4143,7 +4138,7 @@ static int stk600_xprog_program_enable(const PROGRAMMER *pgm, const AVRPART *p) pmsg_error("no nvm_base parameter for PDI device\n"); return -1; } - if ((mem = avr_locate_mem(p, "eeprom")) != NULL) { + if ((mem = avr_locate_eeprom(p)) != NULL) { if (mem->page_size <= 1) { pmsg_error("no EEPROM page_size parameter for PDI device\n"); return -1; @@ -4222,16 +4217,18 @@ static int stk600_xprog_program_enable(const PROGRAMMER *pgm, const AVRPART *p) // Read XMEGA chip silicon revision if(p->prog_modes & PM_PDI) { - AVRMEM *m = avr_locate_mem(p, "io"); + AVRMEM *m = avr_locate_io(p); unsigned char chip_rev[AVR_CHIP_REVLEN]; - pgm->read_byte(pgm, p, m, p->mcu_base+3, chip_rev); - pmsg_notice("silicon revision: %x.%x\n", chip_rev[0] >> 4, chip_rev[0] & 0x0f); + if(m && pgm->read_byte(pgm, p, m, p->mcu_base+3, chip_rev) >= 0) + pmsg_notice("silicon revision: %x.%x\n", chip_rev[0] >> 4, chip_rev[0] & 0x0f); + else + pmsg_warning("cannot read silicon revision; is avrdude.conf up to date?\n"); } return 0; } -static unsigned char stk600_xprog_memtype(const PROGRAMMER *pgm, unsigned long addr) { +static unsigned char stk600_xprog_mtype(const PROGRAMMER *pgm, unsigned long addr) { if (addr >= PDATA(pgm)->boot_start) return XPRG_MEM_TYPE_BOOT; else @@ -4258,32 +4255,27 @@ static int stk600_xprog_write_byte(const PROGRAMMER *pgm, const AVRPART *p, cons memset(b, 0, sizeof(b)); - if (str_eq(mem->desc, "flash")) { - memcode = stk600_xprog_memtype(pgm, addr); - } else if (str_eq(mem->desc, "application") || str_eq(mem->desc, "apptable")) { + if (mem_is_flash(mem)) { + memcode = stk600_xprog_mtype(pgm, addr); + } else if (mem_is_application(mem) || mem_is_apptable(mem)) { memcode = XPRG_MEM_TYPE_APPL; - } else if (str_eq(mem->desc, "boot")) { + } else if (mem_is_boot(mem)) { memcode = XPRG_MEM_TYPE_BOOT; - } else if (str_eq(mem->desc, "eeprom")) { + } else if (mem_is_eeprom(mem)) { memcode = XPRG_MEM_TYPE_EEPROM; - } else if (str_eq(mem->desc, "io")) { + } else if (mem_is_io(mem)) { memcode = XPRG_MEM_TYPE_APPL; - AVRMEM *data = avr_locate_mem(p, "data"); - addr += data->offset; - } else if (str_starts(mem->desc, "lock")) { + addr += avr_data_offset(p); + } else if (mem_is_lock(mem)) { memcode = XPRG_MEM_TYPE_LOCKBITS; - } else if (str_starts(mem->desc, "fuse")) { + } else if (mem_is_a_fuse(mem) || mem_is_fuses(mem)) { memcode = XPRG_MEM_TYPE_FUSE; if (p->prog_modes & PM_TPI) - /* - * TPI devices need a mystic erase prior to writing their - * fuses. - */ need_erase = 1; - } else if (str_eq(mem->desc, "usersig") || str_eq(mem->desc, "userrow")) { + } else if (mem_is_userrow(mem)) { memcode = XPRG_MEM_TYPE_USERSIG; } else { - pmsg_error("unknown memory %s\n", mem->desc); + pmsg_error("unsupported memory %s\n", mem->desc); return -1; } addr += mem->offset; @@ -4333,30 +4325,29 @@ static int stk600_xprog_read_byte(const PROGRAMMER *pgm, const AVRPART *p, const { unsigned char b[8]; - if (str_eq(mem->desc, "flash")) { - b[1] = stk600_xprog_memtype(pgm, addr); - } else if (str_eq(mem->desc, "application") || str_eq(mem->desc, "apptable")) { + if (mem_is_flash(mem)) { + b[1] = stk600_xprog_mtype(pgm, addr); + } else if (mem_is_application(mem) || mem_is_apptable(mem)) { b[1] = XPRG_MEM_TYPE_APPL; - } else if (str_eq(mem->desc, "boot")) { + } else if (mem_is_boot(mem)) { b[1] = XPRG_MEM_TYPE_BOOT; - } else if (str_eq(mem->desc, "eeprom")) { + } else if (mem_is_eeprom(mem)) { b[1] = XPRG_MEM_TYPE_EEPROM; - } else if (str_eq(mem->desc, "io")) { + } else if (mem_is_io(mem)) { b[1] = XPRG_MEM_TYPE_APPL; - AVRMEM *data = avr_locate_mem(p, "data"); - addr += data->offset; - } else if (str_eq(mem->desc, "signature")) { + addr += avr_data_offset(p); + } else if (mem_is_signature(mem)) { b[1] = XPRG_MEM_TYPE_APPL; - } else if (str_starts(mem->desc, "fuse")) { + } else if (mem_is_a_fuse(mem) || mem_is_fuses(mem)) { b[1] = XPRG_MEM_TYPE_FUSE; - } else if (str_starts(mem->desc, "lock")) { + } else if (mem_is_lock(mem)) { b[1] = XPRG_MEM_TYPE_LOCKBITS; - } else if (str_eq(mem->desc, "calibration") || str_eq(mem->desc, "prodsig")) { + } else if (mem_is_calibration(mem) || mem_is_sigrow(mem)) { b[1] = XPRG_MEM_TYPE_FACTORY_CALIBRATION; - } else if (str_eq(mem->desc, "usersig") || str_eq(mem->desc, "userrow")) { + } else if (mem_is_userrow(mem)) { b[1] = XPRG_MEM_TYPE_USERSIG; } else { - pmsg_error("unknown memory %s\n", mem->desc); + pmsg_error("unsupported memory %s\n", mem->desc); return -1; } addr += mem->offset; @@ -4383,8 +4374,8 @@ static int stk600_xprog_paged_load(const PROGRAMMER *pgm, const AVRPART *p, cons { unsigned char *b; unsigned int offset; - unsigned char memtype; - int n_bytes_orig = n_bytes, dynamic_memtype = 0; + unsigned char mtype; + int n_bytes_orig = n_bytes, dynamic_mtype = 0; unsigned long use_ext_addr = 0; /* @@ -4399,39 +4390,38 @@ static int stk600_xprog_paged_load(const PROGRAMMER *pgm, const AVRPART *p, cons * This is probably what AVR079 means when writing about the * "TIF address space". */ - if (str_eq(mem->desc, "flash")) { - memtype = 0; - dynamic_memtype = 1; + if (mem_is_flash(mem)) { + mtype = 0; + dynamic_mtype = 1; if (mem->size > 64 * 1024) use_ext_addr = (1UL << 31); - } else if (str_eq(mem->desc, "application") || str_eq(mem->desc, "apptable")) { - memtype = XPRG_MEM_TYPE_APPL; + } else if (mem_is_application(mem) || mem_is_apptable(mem)) { + mtype = XPRG_MEM_TYPE_APPL; if (mem->size > 64 * 1024) use_ext_addr = (1UL << 31); - } else if (str_eq(mem->desc, "boot")) { - memtype = XPRG_MEM_TYPE_BOOT; + } else if (mem_is_boot(mem)) { + mtype = XPRG_MEM_TYPE_BOOT; // Do we have to consider the total amount of flash // instead to decide whether to use extended addressing? if (mem->size > 64 * 1024) use_ext_addr = (1UL << 31); - } else if (str_eq(mem->desc, "eeprom")) { - memtype = XPRG_MEM_TYPE_EEPROM; - } else if (str_eq(mem->desc, "io")) { - memtype = XPRG_MEM_TYPE_APPL; - AVRMEM *data = avr_locate_mem(p, "data"); - addr += data->offset; - } else if (str_eq(mem->desc, "signature")) { - memtype = XPRG_MEM_TYPE_APPL; - } else if (str_starts(mem->desc, "fuse")) { - memtype = XPRG_MEM_TYPE_FUSE; - } else if (str_starts(mem->desc, "lock")) { - memtype = XPRG_MEM_TYPE_LOCKBITS; - } else if (str_eq(mem->desc, "calibration") || str_eq(mem->desc, "prodsig")) { - memtype = XPRG_MEM_TYPE_FACTORY_CALIBRATION; - } else if (str_eq(mem->desc, "usersig") || str_eq(mem->desc, "userrow")) { - memtype = XPRG_MEM_TYPE_USERSIG; + } else if (mem_is_eeprom(mem)) { + mtype = XPRG_MEM_TYPE_EEPROM; + } else if (mem_is_io(mem)) { + mtype = XPRG_MEM_TYPE_APPL; + addr += avr_data_offset(p); + } else if (mem_is_signature(mem)) { + mtype = XPRG_MEM_TYPE_APPL; + } else if (mem_is_a_fuse(mem) || mem_is_fuses(mem)) { + mtype = XPRG_MEM_TYPE_FUSE; + } else if (mem_is_lock(mem)) { + mtype = XPRG_MEM_TYPE_LOCKBITS; + } else if (mem_is_calibration(mem) || mem_is_sigrow(mem)) { + mtype = XPRG_MEM_TYPE_FACTORY_CALIBRATION; + } else if (mem_is_userrow(mem)) { + mtype = XPRG_MEM_TYPE_USERSIG; } else { - pmsg_error("unknown paged memory %s\n", mem->desc); + pmsg_error("unsupported memory %s\n", mem->desc); return -1; } offset = addr; @@ -4448,11 +4438,11 @@ static int stk600_xprog_paged_load(const PROGRAMMER *pgm, const AVRPART *p, cons } while (n_bytes != 0) { - if (dynamic_memtype) - memtype = stk600_xprog_memtype(pgm, addr - mem->offset); + if (dynamic_mtype) + mtype = stk600_xprog_mtype(pgm, addr - mem->offset); b[0] = XPRG_CMD_READ_MEM; - b[1] = memtype; + b[1] = mtype; b[2] = addr >> 24; b[3] = addr >> 16; b[4] = addr >> 8; @@ -4483,8 +4473,8 @@ static int stk600_xprog_paged_write(const PROGRAMMER *pgm, const AVRPART *p, con { unsigned char *b; unsigned int offset; - unsigned char memtype; - int n_bytes_orig = n_bytes, dynamic_memtype = 0; + unsigned char mtype; + int n_bytes_orig = n_bytes, dynamic_mtype = 0; size_t writesize; unsigned long use_ext_addr = 0; unsigned char writemode; @@ -4503,44 +4493,44 @@ static int stk600_xprog_paged_write(const PROGRAMMER *pgm, const AVRPART *p, con * This is probably what AVR079 means when writing about the * "TIF address space". */ - if (str_eq(mem->desc, "flash")) { - memtype = 0; - dynamic_memtype = 1; + if (mem_is_flash(mem)) { + mtype = 0; + dynamic_mtype = 1; writemode = (1 << XPRG_MEM_WRITE_WRITE); if (mem->size > 64 * 1024) use_ext_addr = (1UL << 31); - } else if (str_eq(mem->desc, "application") || str_eq(mem->desc, "apptable")) { - memtype = XPRG_MEM_TYPE_APPL; + } else if (mem_is_application(mem) || mem_is_apptable(mem)) { + mtype = XPRG_MEM_TYPE_APPL; writemode = (1 << XPRG_MEM_WRITE_WRITE); if (mem->size > 64 * 1024) use_ext_addr = (1UL << 31); - } else if (str_eq(mem->desc, "boot")) { - memtype = XPRG_MEM_TYPE_BOOT; + } else if (mem_is_boot(mem)) { + mtype = XPRG_MEM_TYPE_BOOT; writemode = (1 << XPRG_MEM_WRITE_WRITE); // Do we have to consider the total amount of flash // instead to decide whether to use extended addressing? if (mem->size > 64 * 1024) use_ext_addr = (1UL << 31); - } else if (str_eq(mem->desc, "eeprom")) { - memtype = XPRG_MEM_TYPE_EEPROM; + } else if (mem_is_eeprom(mem)) { + mtype = XPRG_MEM_TYPE_EEPROM; writemode = (1 << XPRG_MEM_WRITE_WRITE) | (1 << XPRG_MEM_WRITE_ERASE); - } else if (str_eq(mem->desc, "signature")) { - memtype = XPRG_MEM_TYPE_APPL; + } else if (mem_is_signature(mem)) { + mtype = XPRG_MEM_TYPE_APPL; writemode = (1 << XPRG_MEM_WRITE_WRITE); - } else if (str_starts(mem->desc, "fuse")) { - memtype = XPRG_MEM_TYPE_FUSE; + } else if (mem_is_a_fuse(mem) || mem_is_fuses(mem)) { + mtype = XPRG_MEM_TYPE_FUSE; writemode = (1 << XPRG_MEM_WRITE_WRITE); - } else if (str_starts(mem->desc, "lock")) { - memtype = XPRG_MEM_TYPE_LOCKBITS; + } else if (mem_is_lock(mem)) { + mtype = XPRG_MEM_TYPE_LOCKBITS; writemode = (1 << XPRG_MEM_WRITE_WRITE); - } else if (str_eq(mem->desc, "calibration")) { - memtype = XPRG_MEM_TYPE_FACTORY_CALIBRATION; + } else if (mem_is_calibration(mem)) { + mtype = XPRG_MEM_TYPE_FACTORY_CALIBRATION; writemode = (1 << XPRG_MEM_WRITE_WRITE); - } else if (str_eq(mem->desc, "usersig") || str_eq(mem->desc, "userrow")) { - memtype = XPRG_MEM_TYPE_USERSIG; + } else if (mem_is_userrow(mem)) { + mtype = XPRG_MEM_TYPE_USERSIG; writemode = (1 << XPRG_MEM_WRITE_WRITE); } else { - pmsg_error("unknown paged memory %s\n", mem->desc); + pmsg_error("unsupported memory %s\n", mem->desc); return -1; } offset = addr; @@ -4558,8 +4548,8 @@ static int stk600_xprog_paged_write(const PROGRAMMER *pgm, const AVRPART *p, con while (n_bytes != 0) { - if (dynamic_memtype) - memtype = stk600_xprog_memtype(pgm, addr - mem->offset); + if (dynamic_mtype) + mtype = stk600_xprog_mtype(pgm, addr - mem->offset); if (page_size > 256) { /* @@ -4585,7 +4575,7 @@ static int stk600_xprog_paged_write(const PROGRAMMER *pgm, const AVRPART *p, con writesize = 256; } b[0] = XPRG_CMD_WRITE_MEM; - b[1] = memtype; + b[1] = mtype; b[2] = writemode; b[3] = addr >> 24; b[4] = addr >> 16; @@ -4618,7 +4608,7 @@ static int stk600_xprog_paged_write(const PROGRAMMER *pgm, const AVRPART *p, con writesize = page_size; } b[0] = XPRG_CMD_WRITE_MEM; - b[1] = memtype; + b[1] = mtype; b[2] = writemode; b[3] = addr >> 24; b[4] = addr >> 16; @@ -4651,7 +4641,7 @@ static int stk600_xprog_chip_erase(const PROGRAMMER *pgm, const AVRPART *p) { unsigned int addr = 0; if (p->prog_modes & PM_TPI) { - if ((mem = avr_locate_mem(p, "flash")) == NULL) { + if ((mem = avr_locate_flash(p)) == NULL) { pmsg_error("no FLASH definition found for TPI device\n"); return -1; } @@ -4676,16 +4666,16 @@ static int stk600_xprog_page_erase(const PROGRAMMER *pgm, const AVRPART *p, cons { unsigned char b[6]; - if (str_eq(m->desc, "flash")) { - b[1] = stk600_xprog_memtype(pgm, addr) == XPRG_MEM_TYPE_APPL? + if (mem_is_flash(m)) { + b[1] = stk600_xprog_mtype(pgm, addr) == XPRG_MEM_TYPE_APPL? XPRG_ERASE_APP_PAGE: XPRG_ERASE_BOOT_PAGE; - } else if (str_eq(m->desc, "application") || str_eq(m->desc, "apptable")) { + } else if (mem_is_application(m) || mem_is_apptable(m)) { b[1] = XPRG_ERASE_APP_PAGE; - } else if (str_eq(m->desc, "boot")) { + } else if (mem_is_boot(m)) { b[1] = XPRG_ERASE_BOOT_PAGE; - } else if (str_eq(m->desc, "eeprom")) { + } else if (mem_is_eeprom(m)) { b[1] = XPRG_ERASE_EEPROM_PAGE; - } else if (str_eq(m->desc, "usersig") || str_eq(m->desc, "userrow")) { + } else if (mem_is_userrow(m)) { b[1] = XPRG_ERASE_USERSIG; } else { pmsg_error("unknown paged memory %s\n", m->desc); diff --git a/src/strutil.c b/src/strutil.c index 308cc691e..811ead447 100644 --- a/src/strutil.c +++ b/src/strutil.c @@ -553,7 +553,7 @@ unsigned long long int str_ull(const char *str, char **endptr, int base) { * function, but is also used for generic string to integer conversions in str_int() below. Both * routines define the "character" of how avrdude understands strings in (most) of its dealings. * The granularity of type is an bitwise-or combination of bits making up STR_INTEGER; STR_FLOAT; - * STR_DOUBLE or STR_STRING. The arguments part and memtype are only needed for input from files. + * STR_DOUBLE or STR_STRING. The arguments part and memstr are only needed for input from files. */ #define Return(...) do { \ @@ -574,7 +574,7 @@ unsigned long long int str_ull(const char *str, char **endptr, int base) { (ll) < INT16_MIN || (ll) > INT16_MAX? 4: \ (ll) < INT8_MIN || (ll) > INT8_MAX? 2: 1) -Str2data *str_todata(const char *s, int type, const AVRPART *part, const char *memtype) { +Str2data *str_todata(const char *s, int type, const AVRPART *part, const char *memstr) { char *end_ptr; Str2data *sd = cfg_malloc(__func__, sizeof *sd); char *str = cfg_strdup(__func__, s); @@ -766,7 +766,7 @@ Str2data *str_todata(const char *s, int type, const AVRPART *part, const char *m } } - if(type & STR_FILE && part && memtype) { // File name containing data to be loaded + if(type & STR_FILE && part && memstr) { // File name containing data to be loaded int format = FMT_AUTO; FILE *f; char fmtstr[4] = { 0 }; @@ -790,12 +790,12 @@ Str2data *str_todata(const char *s, int type, const AVRPART *part, const char *m } // Obtain a copy of the part incl all memories AVRPART *dp = avr_dup_part(part); - AVRMEM *mem = avr_locate_mem(dp, memtype); + AVRMEM *mem = avr_locate_mem(dp, memstr); if(!mem) { avr_free_part(dp); - Return("memory type %s not configured for device %s", memtype, part->desc); + Return("memory %s not configured for device %s", memstr, part->desc); } - int rc = fileio(FIO_READ_FOR_VERIFY, str, format, dp, memtype, -1); + int rc = fileio(FIO_READ_FOR_VERIFY, str, format, dp, memstr, -1); if(rc < 0) { avr_free_part(dp); Return("unable to read the%s %s file", fmtstr, fileio_fmtstr(format)); diff --git a/src/teensy.c b/src/teensy.c index f657ea2c9..8fdaf197f 100644 --- a/src/teensy.c +++ b/src/teensy.c @@ -129,7 +129,7 @@ static int teensy_get_bootloader_info(pdata_t* pdata, const AVRPART* p) { // To use this workaround, the -F option is required. pmsg_error("cannot detect board type (HID usage is 0)\n"); - AVRMEM* mem = avr_locate_mem(p, "flash"); + AVRMEM* mem = avr_locate_flash(p); if (mem == NULL) { pmsg_error("no flash memory defined for part %s\n", p->desc); @@ -463,17 +463,14 @@ static int teensy_read_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRME { pmsg_debug("teensy_read_byte(desc=%s, addr=0x%04lX)\n", mem->desc, addr); - if (str_eq(mem->desc, "lfuse") || - str_eq(mem->desc, "hfuse") || - str_eq(mem->desc, "efuse") || - str_eq(mem->desc, "lock")) + if (mem_is_a_fuse(mem) || mem_is_lock(mem)) { *value = 0xFF; return 0; } else { - pmsg_error("unsupported memory type: %s\n", mem->desc); + pmsg_error("unsupported memory %s\n", mem->desc); return -1; } } @@ -499,7 +496,7 @@ static int teensy_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AVR { pmsg_debug("teensy_paged_write(page_size=0x%X, addr=0x%X, n_bytes=0x%X)\n", page_size, addr, n_bytes); - if (str_eq(mem->desc, "flash")) + if (mem_is_flash(mem)) { pdata_t* pdata = PDATA(pgm); @@ -544,7 +541,7 @@ static int teensy_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AVR } else { - pmsg_error("unsupported memory type: %s\n", mem->desc); + pmsg_error("unsupported memory %s\n", mem->desc); return -1; } } diff --git a/src/term.c b/src/term.c index ba00401b6..6242b1457 100644 --- a/src/term.c +++ b/src/term.c @@ -231,14 +231,14 @@ static int cmd_dump(const PROGRAMMER *pgm, const AVRPART *p, int argc, char *arg } enum { read_size = 256 }; - char *memtype; + char *memstr; if(argc > 1) - memtype = argv[1]; + memstr = argv[1]; else - memtype = (char*)read_mem[i].mem->desc; - const AVRMEM *mem = avr_locate_mem(p, memtype); + memstr = (char*)read_mem[i].mem->desc; + const AVRMEM *mem = avr_locate_mem(p, memstr); if (mem == NULL) { - pmsg_error("(%s) %s memory type not defined for part %s\n", cmd, memtype, p->desc); + pmsg_error("(%s) memory %s not defined for part %s\n", cmd, memstr, p->desc); return -1; } @@ -337,7 +337,7 @@ static int cmd_dump(const PROGRAMMER *pgm, const AVRPART *p, int argc, char *arg report_progress(1, -1, NULL); pmsg_error("(%s) error reading %s address 0x%05lx of part %s\n", cmd, mem->desc, (long) read_mem[i].addr + j, p->desc); if (rc == -1) - imsg_error("%*sread operation not supported on memory type %s\n", 7, "", mem->desc); + imsg_error("%*sread operation not supported on memory %s\n", 7, "", mem->desc); free(buf); return -1; } @@ -425,10 +425,10 @@ static int cmd_write(const PROGRAMMER *pgm, const AVRPART *p, int argc, char *ar int write_mode; // Operation mode, standard or fill int start_offset; // Which argc argument int len; // Number of bytes to write to memory - char *memtype = argv[1]; // Memory name string - const AVRMEM *mem = avr_locate_mem(p, memtype); + char *memstr = argv[1]; // Memory name string + const AVRMEM *mem = avr_locate_mem(p, memstr); if (mem == NULL) { - pmsg_error("(write) %s memory type not defined for part %s\n", memtype, p->desc); + pmsg_error("(write) memory %s not defined for part %s\n", memstr, p->desc); return -1; } int maxsize = mem->size; @@ -610,7 +610,7 @@ static int cmd_write(const PROGRAMMER *pgm, const AVRPART *p, int argc, char *ar } else if(rc) { pmsg_error("(write) error writing 0x%02x at 0x%05x, rc=%d\n", buf[i], addr+i, (int) rc); if (rc == -1) - imsg_error("%*swrite operation not supported on memory type %s\n", 8, "", mem->desc); + imsg_error("%*swrite operation not supported on memory %s\n", 8, "", mem->desc); } else if(pgm->read_byte_cached(pgm, p, mem, addr+i, &b) < 0) { imsg_error("%*sreadback from %s failed\n", 8, "", mem->desc); } else { // Read back byte b is now set @@ -641,7 +641,7 @@ static int cmd_save(const PROGRAMMER *pgm, const AVRPART *p, int argc, char *arg AVRMEM *mem, *omem; if(!(omem = avr_locate_mem(p, argv[1]))) { - pmsg_error("(save) %s memory type not defined for part %s\n", argv[1], p->desc); + pmsg_error("(save) memory %s not defined for part %s\n", argv[1], p->desc); return -1; } @@ -831,13 +831,13 @@ static int cmd_erase(const PROGRAMMER *pgm, const AVRPART *p, int argc, char *ar } if (argc > 1) { - char *memtype = argv[1]; - const AVRMEM *mem = avr_locate_mem(p, memtype); + char *memstr = argv[1]; + const AVRMEM *mem = avr_locate_mem(p, memstr); if (mem == NULL) { - pmsg_error("(erase) %s memory type not defined for part %s\n", argv[1], p->desc); + pmsg_error("(erase) memory %s not defined for part %s\n", argv[1], p->desc); return -1; } - char *args[] = {"write", memtype, "", "", "0xff", "...", NULL}; + char *args[] = {"write", memstr, "", "", "0xff", "...", NULL}; // erase if (argc == 2) { args[2] = "0"; @@ -858,7 +858,7 @@ static int cmd_erase(const PROGRAMMER *pgm, const AVRPART *p, int argc, char *ar if(rc == LIBAVRDUDE_SOFTFAIL) { pmsg_info("(erase) emulating chip erase by writing 0xff to flash "); - const AVRMEM *flm = avr_locate_mem(p, "flash"); + const AVRMEM *flm = avr_locate_flash(p); if(!flm) { msg_error("but flash not defined for part %s?\n", p->desc); return -1; @@ -907,14 +907,14 @@ static int cmd_pgerase(const PROGRAMMER *pgm, const AVRPART *p, int argc, char * return -1; } - char *memtype = argv[1]; - const AVRMEM *mem = avr_locate_mem(p, memtype); + char *memstr = argv[1]; + const AVRMEM *mem = avr_locate_mem(p, memstr); if(!mem) { - pmsg_error("(pgerase) %s memory type not defined for part %s\n", memtype, p->desc); + pmsg_error("(pgerase) memory %s not defined for part %s\n", memstr, p->desc); return -1; } if(!avr_has_paged_access(pgm, mem)) { - pmsg_error("(pgerase) %s memory cannot be paged addressed by %s\n", memtype, pgmid); + pmsg_error("(pgerase) %s memory cannot be paged addressed by %s\n", memstr, pgmid); return -1; } @@ -999,7 +999,7 @@ static int getfusel(const PROGRAMMER *pgm, const AVRPART *p, Fusel_t *fl, const const AVRMEM *mem = avr_locate_mem(p, cci->memstr); if(!mem) { - err = cache_string(tofree = str_sprintf("%s memory type not defined for part %s", cci->memstr, p->desc)); + err = cache_string(tofree = str_sprintf("memory %s not defined for part %s", cci->memstr, p->desc)); free(tofree); goto back; } @@ -1357,12 +1357,11 @@ static int cmd_config(const PROGRAMMER *pgm, const AVRPART *p, int argc, char *a cc = cfg_malloc(__func__, sizeof *cc*nc); fc = cfg_malloc(__func__, sizeof *fc*nc); - char *locktype = "lock"; - if(!avr_locate_mem(p, "lock") && avr_locate_mem(p, "lockbits")) - locktype = "lockbits"; + AVRMEM *m = avr_locate_lock(p); + const char *locktype = m? m->desc: "lock"; for(int i=0; idesc); + pmsg_error("(config) memory %s not defined for part %s\n", cc[ci].memstr, p->desc); ret = -1; goto finished; } @@ -1591,17 +1590,15 @@ static int cmd_sig(const PROGRAMMER *pgm, const AVRPART *p, int argc, char *argv } rc = avr_signature(pgm, p); - if (rc != 0) { + if(rc != 0) pmsg_error("(sig) error reading signature data, rc=%d\n", rc); - } - m = avr_locate_mem(p, "signature"); - if (m == NULL) { + m = avr_locate_signature(p); + if(m == NULL) { pmsg_error("(sig) signature data not defined for device %s\n", p->desc); - } - else { + } else { term_out("Device signature = 0x"); - for (i=0; isize; i++) + for(i=0; isize; i++) term_out("%02x", m->buf[i]); term_out("\n"); } @@ -1793,7 +1790,7 @@ static int cmd_help(const PROGRAMMER *pgm, const AVRPART *p, int argc, char *arg "Note that not all programmer derivatives support all commands. Flash and\n" "EEPROM type memories are normally read and written using a cache via paged\n" "read and write access; the cache is synchronised on quit or flush commands.\n" - "The part command displays valid memory types for use with dump and write.\n"); + "The part command displays valid memories for use with dump and write.\n"); return 0; } diff --git a/src/update.c b/src/update.c index 03339cec2..fe3cca69a 100644 --- a/src/update.c +++ b/src/update.c @@ -45,7 +45,7 @@ UPDATE *parse_op(const char *s) { // Assume -U [:] first UPDATE *upd = (UPDATE *) cfg_malloc(__func__, sizeof *upd); - upd->memtype = NULL; // Defaults to flash or application + upd->memstr = NULL; // Defaults to flash or application upd->op = DEVICE_WRITE; const char *fn = s; @@ -55,12 +55,12 @@ UPDATE *parse_op(const char *s) { if(!strchr("rwv", fc[1])) { pmsg_error("invalid I/O mode :%c: in -U %s\n", fc[1], s); imsg_error("I/O mode can be r, w or v for read, write or verify device\n"); - free(upd->memtype); + free(upd->memstr); free(upd); return NULL; } - upd->memtype = memcpy(cfg_malloc(__func__, fc-s+1), s, fc-s); + upd->memstr = memcpy(cfg_malloc(__func__, fc-s+1), s, fc-s); upd->op = fc[1]=='r'? DEVICE_READ: fc[1]=='w'? DEVICE_WRITE: DEVICE_VERIFY; @@ -82,7 +82,7 @@ UPDATE *parse_op(const char *s) { if(f != FMT_ERROR) imsg_error(" :%c %s\n", c, fileio_fmtstr(f)); } - free(upd->memtype); + free(upd->memstr); free(upd); return NULL; } @@ -96,28 +96,18 @@ UPDATE *parse_op(const char *s) { UPDATE *dup_update(const UPDATE *upd) { - UPDATE * u; - - u = (UPDATE *) cfg_malloc("dup_update()", sizeof(UPDATE)); - - memcpy(u, upd, sizeof(UPDATE)); - - if (upd->memtype != NULL) - u->memtype = cfg_strdup("dup_update()", upd->memtype); - else - u->memtype = NULL; - u->filename = cfg_strdup("dup_update()", upd->filename); + UPDATE *u = (UPDATE *) cfg_malloc(__func__, sizeof *u); + memcpy(u, upd, sizeof*u); + u->memstr = upd->memstr? cfg_strdup(__func__, upd->memstr): NULL; + u->filename = cfg_strdup(__func__, upd->filename); return u; } UPDATE *new_update(int op, const char *memstr, int filefmt, const char *fname) { - UPDATE * u; - - u = (UPDATE *) cfg_malloc("new_update()", sizeof(UPDATE)); - - u->memtype = cfg_strdup("new_update()", memstr); - u->filename = cfg_strdup("new_update()", fname); + UPDATE *u = (UPDATE *) cfg_malloc(__func__, sizeof *u); + u->memstr = cfg_strdup(__func__, memstr); + u->filename = cfg_strdup(__func__, fname); u->op = op; u->format = filefmt; @@ -127,22 +117,17 @@ UPDATE *new_update(int op, const char *memstr, int filefmt, const char *fname) { UPDATE *cmd_update(const char *cmd) { UPDATE *u = (UPDATE *) cfg_malloc(__func__, sizeof *u); u->cmdline = cmd; + return u; } -void free_update(UPDATE * u) -{ - if (u != NULL) { - if(u->memtype != NULL) { - free(u->memtype); - u->memtype = NULL; - } - if(u->filename != NULL) { - free(u->filename); - u->filename = NULL; - } - free(u); - } +void free_update(UPDATE *u) { + if(u) { + free(u->memstr); + free(u->filename); + memset(u, 0, sizeof *u); + free(u); + } } char *update_str(const UPDATE *upd) { @@ -150,24 +135,24 @@ char *update_str(const UPDATE *upd) { return str_sprintf("-%c %s", str_eq("interactive terminal", upd->cmdline)? 't': 'T', upd->cmdline); return str_sprintf("-U %s:%c:%s:%c", - upd->memtype, + upd->memstr, upd->op == DEVICE_READ? 'r': upd->op == DEVICE_WRITE? 'w': 'v', upd->filename, fileio_fmtchr(upd->format)); } // Memory statistics considering holes after a file read returned size bytes -int memstats(const AVRPART *p, const char *memtype, int size, Filestats *fsp) { +int memstats(const AVRPART *p, const char *memstr, int size, Filestats *fsp) { Filestats ret = { 0 }; - AVRMEM *mem = avr_locate_mem(p, memtype); + AVRMEM *mem = avr_locate_mem(p, memstr); if(!mem) { - pmsg_error("%s %s undefined\n", p->desc, memtype); + pmsg_error("%s %s undefined\n", p->desc, memstr); return LIBAVRDUDE_GENERAL_FAILURE; } if(!mem->buf || !mem->tags) { - pmsg_error("%s %s is not set\n", p->desc, memtype); + pmsg_error("%s %s is not set\n", p->desc, memstr); return LIBAVRDUDE_GENERAL_FAILURE; } @@ -176,7 +161,7 @@ int memstats(const AVRPART *p, const char *memtype, int size, Filestats *fsp) { pgsize = 1; if(size < 0 || size > mem->size) { - pmsg_error("size %d at odds with %s %s size %d\n", size, p->desc, memtype, mem->size); + pmsg_error("size %d at odds with %s %s size %d\n", size, p->desc, memstr, mem->size); return LIBAVRDUDE_GENERAL_FAILURE; } @@ -296,10 +281,10 @@ int update_dryrun(const AVRPART *p, UPDATE *upd) { * Reject an update if memory name is not known amongst any part (suspect a typo) * but accept when the specific part does not have it (allow unifying i/faces) */ - if(!avr_mem_might_be_known(upd->memtype)) { - pmsg_error("unknown memory type %s\n", upd->memtype); + if(!avr_mem_might_be_known(upd->memstr)) { + pmsg_error("unknown memory %s\n", upd->memstr); ret = LIBAVRDUDE_GENERAL_FAILURE; - } else if(p && !avr_locate_mem(p, upd->memtype)) + } else if(p && !avr_locate_mem(p, upd->memstr)) ret = LIBAVRDUDE_SOFTFAIL; known = 0; @@ -395,9 +380,9 @@ int do_op(const PROGRAMMER *pgm, const AVRPART *p, const UPDATE *upd, enum updat return terminal_mode(pgm, p); } - mem = avr_locate_mem(p, upd->memtype); + mem = avr_locate_mem(p, upd->memstr); if (mem == NULL) { - pmsg_warning("skipping -U %s:... as memory not defined for part %s\n", upd->memtype, p->desc); + pmsg_warning("skipping -U %s:... as memory not defined for part %s\n", upd->memstr, p->desc); return LIBAVRDUDE_SOFTFAIL; } @@ -420,7 +405,7 @@ int do_op(const PROGRAMMER *pgm, const AVRPART *p, const UPDATE *upd, enum updat if(mem->size > 32 || verbose > 1) report_progress(0, 1, "Reading"); - rc = avr_read(pgm, p, upd->memtype, 0); + rc = avr_read(pgm, p, upd->memstr, 0); report_progress(1, 1, NULL); if (rc < 0) { pmsg_error("unable to read all of %s%s memory, rc=%d\n", mem->desc, alias_mem_desc, rc); @@ -432,7 +417,7 @@ int do_op(const PROGRAMMER *pgm, const AVRPART *p, const UPDATE *upd, enum updat pmsg_notice("flash is empty, resulting file has no contents\n"); pmsg_info("writing output file %s\n", str_outname(upd->filename)); - rc = fileio(FIO_WRITE, upd->filename, upd->format, p, upd->memtype, size); + rc = fileio(FIO_WRITE, upd->filename, upd->format, p, upd->memstr, size); if (rc < 0) { pmsg_error("write to file %s failed\n", str_outname(upd->filename)); return LIBAVRDUDE_GENERAL_FAILURE; @@ -442,7 +427,7 @@ int do_op(const PROGRAMMER *pgm, const AVRPART *p, const UPDATE *upd, enum updat case DEVICE_WRITE: // Write the selected device memory using data from a file - rc = fileio(FIO_READ, upd->filename, upd->format, p, upd->memtype, -1); + rc = fileio(FIO_READ, upd->filename, upd->format, p, upd->memstr, -1); if (rc < 0) { pmsg_error("read from file %s failed\n", str_inname(upd->filename)); return LIBAVRDUDE_GENERAL_FAILURE; @@ -451,7 +436,7 @@ int do_op(const PROGRAMMER *pgm, const AVRPART *p, const UPDATE *upd, enum updat pmsg_info("reading input file %s for %s%s\n", str_inname(upd->filename), mem->desc, alias_mem_desc); - if(memstats(p, upd->memtype, rc, &fs) < 0) + if(memstats(p, upd->memstr, rc, &fs) < 0) return LIBAVRDUDE_GENERAL_FAILURE; imsg_info("with %d byte%s in %d section%s within %s\n", @@ -470,14 +455,14 @@ int do_op(const PROGRAMMER *pgm, const AVRPART *p, const UPDATE *upd, enum updat // Patch flash input, eg, for vector bootloaders if(pgm->flash_readhook) { - AVRMEM *mem = avr_locate_mem(p, upd->memtype); - if(mem && str_eq(mem->desc, "flash")) { + AVRMEM *mem = avr_locate_mem(p, upd->memstr); + if(mem && mem_is_flash(mem)) { rc = pgm->flash_readhook(pgm, p, mem, upd->filename, rc); if (rc < 0) { pmsg_notice("readhook for file %s failed\n", str_inname(upd->filename)); return LIBAVRDUDE_GENERAL_FAILURE; } - if(memstats(p, upd->memtype, rc, &fs_patched) < 0) + if(memstats(p, upd->memstr, rc, &fs_patched) < 0) return LIBAVRDUDE_GENERAL_FAILURE; if(memcmp(&fs_patched, &fs, sizeof fs)) { pmsg_info("preparing flash input for device%s\n", @@ -500,18 +485,18 @@ int do_op(const PROGRAMMER *pgm, const AVRPART *p, const UPDATE *upd, enum updat } size = rc; - // Write the buffer contents to the selected memory type + // Write the buffer contents to the selected memory pmsg_info("writing %d byte%s %s%s ...\n", fs.nbytes, str_plural(fs.nbytes), mem->desc, alias_mem_desc); if (!(flags & UF_NOWRITE)) { if(mem->size > 32 || verbose > 1) report_progress(0, 1, "Writing"); - rc = avr_write(pgm, p, upd->memtype, size, (flags & UF_AUTO_ERASE) != 0); + rc = avr_write(pgm, p, upd->memstr, size, (flags & UF_AUTO_ERASE) != 0); report_progress(1, 1, NULL); } else { // Test mode: write to stdout in intel hex rather than to the chip - rc = fileio(FIO_WRITE, "-", FMT_IHEX, p, upd->memtype, size); + rc = fileio(FIO_WRITE, "-", FMT_IHEX, p, upd->memstr, size); } if (rc < 0) { @@ -540,7 +525,7 @@ int do_op(const PROGRAMMER *pgm, const AVRPART *p, const UPDATE *upd, enum updat pmsg_notice("load %s%s data from input file %s\n", mem->desc, alias_mem_desc, str_inname(upd->filename)); - rc = fileio(FIO_READ_FOR_VERIFY, upd->filename, upd->format, p, upd->memtype, -1); + rc = fileio(FIO_READ_FOR_VERIFY, upd->filename, upd->format, p, upd->memstr, -1); if (rc < 0) { pmsg_error("read from file %s failed\n", str_inname(upd->filename)); @@ -550,7 +535,7 @@ int do_op(const PROGRAMMER *pgm, const AVRPART *p, const UPDATE *upd, enum updat } size = rc; - if(memstats(p, upd->memtype, size, &fs) < 0) { + if(memstats(p, upd->memstr, size, &fs) < 0) { led_set(pgm, LED_ERR); led_clr(pgm, LED_VFY); return LIBAVRDUDE_GENERAL_FAILURE; @@ -571,7 +556,7 @@ int do_op(const PROGRAMMER *pgm, const AVRPART *p, const UPDATE *upd, enum updat if(mem->size > 32 || verbose > 1) report_progress (0,1,"Reading"); - rc = avr_read(pgm, p, upd->memtype, v); + rc = avr_read(pgm, p, upd->memstr, v); report_progress (1,1,NULL); if (rc < 0) { pmsg_error("unable to read all of %s%s memory, rc=%d\n", mem->desc, alias_mem_desc, rc); @@ -584,7 +569,7 @@ int do_op(const PROGRAMMER *pgm, const AVRPART *p, const UPDATE *upd, enum updat if (quell_progress < 2) pmsg_notice2("verifying ...\n"); - rc = avr_verify(pgm, p, v, upd->memtype, size); + rc = avr_verify(pgm, p, v, upd->memstr, size); if (rc < 0) { pmsg_error("verification mismatch\n"); led_set(pgm, LED_ERR); diff --git a/src/urclock.c b/src/urclock.c index a4f20c03e..0919db746 100644 --- a/src/urclock.c +++ b/src/urclock.c @@ -585,7 +585,7 @@ static int urclock_flash_readhook(const PROGRAMMER *pgm, const AVRPART *p, const // Check size of uploded application and protect bootloader from being overwritten if((ur.boothigh && size > maxsize) || (!ur.boothigh && firstbeg <= ur.blend)) - Return("input [0x%04x, 0x%04x] overlaps bootloader [0x%04x, 0x%04x]", + Return("input [0x%04x, 0x%04x] overlaps bootloader [0x%04x, 0x%04x]; consider -xrestore", firstbeg, size-1, ur.blstart, ur.blend); if(size > maxsize) @@ -1039,7 +1039,7 @@ static void set_uP(const PROGRAMMER *pgm, const AVRPART *p, int mcuid, int mcuid p->prog_modes & PM_TPI? F_AVR8L: p->prog_modes & (PM_ISP | PM_HVPP | PM_HVSP)? F_AVR8: 0; memcpy(ur.uP.sigs, p->signature, sizeof ur.uP.sigs); - if((mem = avr_locate_mem(p, "flash"))) { + if((mem = avr_locate_flash(p))) { ur.uP.flashoffset = mem->offset; ur.uP.flashsize = mem->size; ur.uP.pagesize = mem->page_size; @@ -1050,7 +1050,7 @@ static void set_uP(const PROGRAMMER *pgm, const AVRPART *p, int mcuid, int mcuid } ur.uP.nboots = -1; ur.uP.bootsize = -1; - if((mem = avr_locate_mem(p, "eeprom"))) { + if((mem = avr_locate_eeprom(p))) { ur.uP.eepromoffset = mem->offset; ur.uP.eepromsize = mem->size; ur.uP.eeprompagesize = mem->page_size; @@ -1260,7 +1260,7 @@ static int ur_initstruct(const PROGRAMMER *pgm, const AVRPART *p) { AVRMEM *flm; int rc; - if(!(flm = avr_locate_mem(p, "flash"))) + if(!(flm = avr_locate_flash(p))) Return("cannot obtain flash memory for %s", p->desc); if(flm->page_size > (int) sizeof spc) @@ -1867,7 +1867,7 @@ static int readUrclockID(const PROGRAMMER *pgm, const AVRPART *p, uint64_t *urcl mchr = 'F'; } - const char *memtype = mchr == 'E'? "eeprom": "flash"; + const char *memstr = mchr == 'E'? "eeprom": "flash"; size = mchr == 'F'? ur.uP.flashsize: ur.uP.eepromsize; @@ -1877,11 +1877,11 @@ static int readUrclockID(const PROGRAMMER *pgm, const AVRPART *p, uint64_t *urcl if(addr < 0 || addr >= size) Return("effective address %d of -xids=%s string out of %s range [0, 0x%04x]\n", - addr, ur.iddesc, memtype, size-1); + addr, ur.iddesc, memstr, size-1); if(addr+len > size) Return("memory range [0x%04x, 0x%04x] of -xid=%s out of %s range [0, 0x%04x]\n", - addr, addr+len-1, ur.iddesc, memtype, size-1); + addr, addr+len-1, ur.iddesc, memstr, size-1); } memset(spc, 0, sizeof spc); @@ -2152,7 +2152,7 @@ static int urclock_chip_erase(const PROGRAMMER *pgm, const AVRPART *p) { if(!emulated) { // Write jump to boot section to reset vector if(ur.boothigh && ur.blstart && ur.vbllevel==1) { - AVRMEM *flm = avr_locate_mem(p, "flash"); + AVRMEM *flm = avr_locate_flash(p); int vecsz = ur.uP.flashsize <= 8192? 2: 4; if(flm && flm->page_size >= vecsz) { unsigned char *page = cfg_malloc(__func__, flm->page_size); @@ -2269,8 +2269,8 @@ static int urclock_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AV if(n_bytes) { // Paged writes only valid for flash and eeprom - mchr = avr_mem_is_flash_type(m)? 'F': 'E'; - if(mchr == 'E' && !avr_mem_is_eeprom_type(m)) + mchr = mem_is_in_flash(m)? 'F': 'E'; + if(mchr == 'E' && !mem_is_eeprom(m)) return -2; if(mchr == 'E' && !ur.bleepromrw && !ur.xeepromrw) @@ -2301,8 +2301,8 @@ static int urclock_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AVR if(n_bytes) { // Paged reads only valid for flash and eeprom - mchr = avr_mem_is_flash_type(m)? 'F': 'E'; - if(mchr == 'E' && !avr_mem_is_eeprom_type(m)) + mchr = mem_is_in_flash(m)? 'F': 'E'; + if(mchr == 'E' && !mem_is_eeprom(m)) return -2; if(mchr == 'F' && ur.urprotocol && !(ur.urfeatures & UB_READ_FLASH)) @@ -2356,9 +2356,9 @@ int urclock_read_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *mem unsigned long addr, unsigned char *value) { // Bytewise read only valid for flash and eeprom - int mchr = avr_mem_is_flash_type(mem)? 'F': 'E'; - if(mchr == 'E' && !avr_mem_is_eeprom_type(mem)) { - if(str_eq(mem->desc, "signature") && pgm->read_sig_bytes) { + int mchr = mem_is_in_flash(mem)? 'F': 'E'; + if(mchr == 'E' && !mem_is_eeprom(mem)) { + if(mem_is_signature(mem) && pgm->read_sig_bytes) { if((int) addr < 0 || (int) addr >= mem->size) { return -1; } @@ -2405,7 +2405,7 @@ static void urclock_display(const PROGRAMMER *pgm, const char *p_unused) { static int urclock_readonly(const struct programmer_t *pgm, const AVRPART *p_unused, const AVRMEM *mem, unsigned int addr) { - if(avr_mem_is_flash_type(mem)) { + if(mem_is_in_flash(mem)) { if(addr > (unsigned int) ur.pfend) return 1; if(addr < (unsigned int) ur.pfstart) @@ -2420,7 +2420,7 @@ static int urclock_readonly(const struct programmer_t *pgm, const AVRPART *p_unu return 1; } } - } else if(!avr_mem_is_eeprom_type(mem)) + } else if(!mem_is_eeprom(mem)) return 1; return 0; @@ -2455,7 +2455,7 @@ static int urclock_parseextparms(const PROGRAMMER *pgm, LISTID extparms) { {"vectornum", &ur.xvectornum, ARG, "Treat bootloader as vector b/loader using this vector"}, {"eepromrw", &ur.xeepromrw, NA, "Assert bootloader EEPROM read/write capability"}, {"emulate_ce", &ur.xemulate_ce, NA, "Emulate chip erase"}, - {"restore", &ur.restore, NA, "Restore a flash backup as is trimming the bootloader"}, + {"restore", &ur.restore, NA, "Restore a flash backup and trim the bootloader"}, {"initstore", &ur.initstore, NA, "Fill store with 0xff on writing to flash"}, //@@@ {"copystore", &ur.copystore, NA, "Copy over store on writing to flash"}, {"nofilename", &ur.nofilename, NA, "Do not store filename on writing to flash"}, diff --git a/src/usb_hidapi.c b/src/usb_hidapi.c index c7021608a..874555f94 100644 --- a/src/usb_hidapi.c +++ b/src/usb_hidapi.c @@ -224,16 +224,12 @@ static void usbhid_close(union filedescriptor *fd) { static int usbhid_send(const union filedescriptor *fd, const unsigned char *bp, size_t mlen) { hid_device *udev = (hid_device *)fd->usb.handle; int rv; - int i = mlen; - const unsigned char * p = bp; unsigned char usbbuf[USBDEV_MAX_XFER_3 + 1]; - - int tx_size; + const int tx_size = mlen < USBDEV_MAX_XFER_3? mlen: USBDEV_MAX_XFER_3; if (udev == NULL) return -1; - tx_size = (mlen < USBDEV_MAX_XFER_3)? mlen: USBDEV_MAX_XFER_3; usbbuf[0] = 0; /* No report ID used */ memcpy(usbbuf + 1, bp, tx_size); rv = hid_write(udev, usbbuf, tx_size + 1); @@ -244,22 +240,9 @@ static int usbhid_send(const union filedescriptor *fd, const unsigned char *bp, if (rv != tx_size + 1) pmsg_error("short write to USB: %d bytes out of %d written\n", rv, tx_size + 1); - if (verbose > 4) { - pmsg_trace2("sent: "); - - while (i) { - unsigned char c = *p; - if (isprint(c)) - msg_trace2("%c ", c); - else - msg_trace2(". "); - msg_trace2("[%02x] ", c); + if(verbose > 4) + trace_buffer(__func__, bp, tx_size); - p++; - i--; - } - msg_trace2("\n"); - } return 0; } @@ -277,22 +260,8 @@ static int usbhid_recv(const union filedescriptor *fd, unsigned char *buf, size_ else if ((size_t) i != nbytes) pmsg_error("short read, read only %d out of %lu bytes\n", i, (unsigned long) nbytes); - if (verbose > 4) { - pmsg_trace2("recv: "); - - while (i) { - unsigned char c = *p; - if (isprint(c)) - msg_trace2("%c ", c); - else - msg_trace2(". "); - msg_trace2("[%02x] ", c); - - p++; - i--; - } - msg_trace2("\n"); - } + if (verbose > 4 && i > 0) + trace_buffer(__func__, p, i); return rv; } diff --git a/src/usb_libusb.c b/src/usb_libusb.c index 20ef605ed..51aca49f6 100644 --- a/src/usb_libusb.c +++ b/src/usb_libusb.c @@ -349,25 +349,8 @@ static int usbdev_send(const union filedescriptor *fd, const unsigned char *bp, mlen -= tx_size; } while (mlen > 0); - if (verbose > 3) - { - pmsg_trace("sent: "); - - while (i) { - unsigned char c = *p; - if (isprint(c)) { - msg_trace("%c ", c); - } - else { - msg_trace(". "); - } - msg_trace("[%02x] ", c); - - p++; - i--; - } - msg_trace("\n"); - } + if(verbose > 3) + trace_buffer(__func__, p, i); return 0; } @@ -424,25 +407,8 @@ static int usbdev_recv(const union filedescriptor *fd, unsigned char *buf, size_ i += amnt; } - if (verbose > 4) - { - pmsg_trace2("recv: "); - - while (i) { - unsigned char c = *p; - if (isprint(c)) { - msg_trace2("%c ", c); - } - else { - msg_trace2(". "); - } - msg_trace2("[%02x] ", c); - - p++; - i--; - } - msg_trace2("\n"); - } + if(verbose > 4) + trace_buffer(__func__, p, i); return 0; } @@ -460,8 +426,7 @@ static int usbdev_recv_frame(const union filedescriptor *fd, unsigned char *buf, { usb_dev_handle *udev = (usb_dev_handle *)fd->usb.handle; int rv, n; - int i; - unsigned char * p = buf; + unsigned char *p = buf; if (udev == NULL) return -1; @@ -532,26 +497,9 @@ static int usbdev_recv_frame(const union filedescriptor *fd, unsigned char *buf, */ printout: - if (verbose > 3) - { - i = n & USB_RECV_LENGTH_MASK; - pmsg_trace("recv: "); + if(verbose > 3) + trace_buffer(__func__, p, n & USB_RECV_LENGTH_MASK); - while (i) { - unsigned char c = *p; - if (isprint(c)) { - msg_trace("%c ", c); - } - else { - msg_trace(". "); - } - msg_trace("[%02x] ", c); - - p++; - i--; - } - msg_trace("\n"); - } return n; } diff --git a/src/usbasp.c b/src/usbasp.c index 79ad5ac4d..5acf6d16b 100644 --- a/src/usbasp.c +++ b/src/usbasp.c @@ -771,9 +771,9 @@ static int usbasp_spi_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const pmsg_debug("usbasp_program_paged_load(\"%s\", 0x%x, %d)\n", m->desc, address, n_bytes); - if (str_eq(m->desc, "flash")) { + if (mem_is_flash(m)) { function = USBASP_FUNC_READFLASH; - } else if (str_eq(m->desc, "eeprom")) { + } else if (mem_is_eeprom(m)) { function = USBASP_FUNC_READEEPROM; } else { return -2; @@ -836,9 +836,9 @@ static int usbasp_spi_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const pmsg_debug("usbasp_program_paged_write(\"%s\", 0x%x, %d)\n", m->desc, address, n_bytes); - if (str_eq(m->desc, "flash")) { + if (mem_is_flash(m)) { function = USBASP_FUNC_WRITEFLASH; - } else if (str_eq(m->desc, "eeprom")) { + } else if (mem_is_eeprom(m)) { function = USBASP_FUNC_WRITEEEPROM; } else { return -2; @@ -1171,8 +1171,8 @@ static int usbasp_tpi_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const pr = addr + m->offset; writed = 0; - /* must erase fuse first */ - if(str_eq(m->desc, "fuse")) + /* must erase fuse first, TPI parts only have one fuse */ + if(mem_is_a_fuse(m)) { /* Set PR */ usbasp_tpi_send_byte(pgm, TPI_OP_SSTPR(0)); diff --git a/src/usbtiny.c b/src/usbtiny.c index 1adb25772..91d2d1207 100644 --- a/src/usbtiny.c +++ b/src/usbtiny.c @@ -604,7 +604,7 @@ static int usbtiny_chip_erase(const PROGRAMMER *pgm, const AVRPART *p) { return -1; if(pgm->prog_modes & PM_SPM) { // Talking to bootloader directly - AVRMEM *fl = avr_locate_mem(p, "flash"); + AVRMEM *fl = avr_locate_flash(p); // Estimated time it takes to erase all pages in bootloader usleep(p->chip_erase_delay * (fl? fl->num_pages: 999)); } else @@ -643,7 +643,7 @@ static int usbtiny_paged_load (const PROGRAMMER *pgm, const AVRPART *p, const AV unsigned char cmd[8]; // First determine what we're doing - function = str_eq(m->desc, "eeprom")? USBTINY_EEPROM_READ: USBTINY_FLASH_READ; + function = mem_is_eeprom(m)? USBTINY_EEPROM_READ: USBTINY_FLASH_READ; // paged_load() only called for pages, so OK to set ext addr once at start if((lext = m->op[AVR_OP_LOAD_EXT_ADDR])) { @@ -713,7 +713,7 @@ static int usbtiny_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AV int delay; // delay required between SPI commands // First determine what we're doing - if (str_eq(m->desc, "flash")) { + if (mem_is_flash(m)) { function = USBTINY_FLASH_WRITE; } else { function = USBTINY_EEPROM_WRITE;