diff --git a/src/avr910.c b/src/avr910.c index d6fe5d94b..06c664f91 100644 --- a/src/avr910.c +++ b/src/avr910.c @@ -88,7 +88,7 @@ static void avr910_teardown(PROGRAMMER * pgm) { static int avr910_send(const PROGRAMMER *pgm, char *buf, size_t len) { - return serial_send(&pgm->fd, (unsigned char *)buf, len); + return serial_send(&pgm->fd, (unsigned char *)buf, len); } @@ -98,12 +98,12 @@ static int avr910_recv(const PROGRAMMER *pgm, char *buf, size_t len) { static int avr910_drain(const PROGRAMMER *pgm, int display) { - return serial_drain(&pgm->fd, display); + return serial_drain(&pgm->fd, display); } static int avr910_vfy_cmd_sent(const PROGRAMMER *pgm, char *errmsg) { - char c; + char c; EI(avr910_recv(pgm, &c, 1)); if (c != '\r') { @@ -123,12 +123,12 @@ static int avr910_chip_erase(const PROGRAMMER *pgm, const AVRPART *p) { if (avr910_vfy_cmd_sent(pgm, "chip erase") < 0) return -1; - /* - * avr910 firmware may not delay long enough - */ - usleep (p->chip_erase_delay); + /* + * avr910 firmware may not delay long enough + */ + usleep (p->chip_erase_delay); - return 0; + return 0; } @@ -153,22 +153,22 @@ static int avr910_program_enable(const PROGRAMMER *pgm, const AVRPART *p) { * initialize the AVR device and prepare it to accept commands */ static int avr910_initialize(const PROGRAMMER *pgm, const AVRPART *p) { - char id[8]; - char sw[2]; - char hw[2]; - char buf[10]; - char type; - char c; - AVRPART * part; + char id[8]; + char sw[2]; + char hw[2]; + char buf[10]; + char type; + char c; + AVRPART * part; - /* Get the programmer identifier. Programmer returns exactly 7 chars - _without_ the null.*/ + /* Get the programmer identifier. Programmer returns exactly 7 chars + _without_ the null.*/ EI(avr910_send(pgm, "S", 1)); memset (id, 0, sizeof(id)); EI(avr910_recv(pgm, id, sizeof(id)-1)); - /* Get the HW and SW versions to see if the programmer is present. */ + /* Get the HW and SW versions to see if the programmer is present. */ EI(avr910_send(pgm, "V", 1)); EI(avr910_recv(pgm, sw, sizeof(sw))); @@ -176,23 +176,23 @@ static int avr910_initialize(const PROGRAMMER *pgm, const AVRPART *p) { EI(avr910_send(pgm, "v", 1)); EI(avr910_recv(pgm, hw, sizeof(hw))); - /* Get the programmer type (serial or parallel). Expect serial. */ + /* Get the programmer type (serial or parallel). Expect serial. */ EI(avr910_send(pgm, "p", 1)); EI(avr910_recv(pgm, &type, 1)); - msg_notice("Programmer id = %s; type = %c\n", id, type); - msg_notice("Software version = %c.%c; ", sw[0], sw[1]); - msg_notice("Hardware version = %c.%c\n", hw[0], hw[1]); + msg_notice("Programmer id = %s; type = %c\n", id, type); + msg_notice("Software version = %c.%c; ", sw[0], sw[1]); + msg_notice("Hardware version = %c.%c\n", hw[0], hw[1]); - /* See if programmer supports autoincrement of address. */ + /* See if programmer supports autoincrement of address. */ EI(avr910_send(pgm, "a", 1)); EI(avr910_recv(pgm, &PDATA(pgm)->has_auto_incr_addr, 1)); if (PDATA(pgm)->has_auto_incr_addr == 'Y') msg_notice("programmer supports auto addr increment\n"); - /* Check support for buffered memory access, ignore if not available */ + /* Check support for buffered memory access, ignore if not available */ if (PDATA(pgm)->test_blockmode == 1) { EI(avr910_send(pgm, "b", 1)); @@ -207,14 +207,17 @@ static int avr910_initialize(const PROGRAMMER *pgm, const AVRPART *p) { PDATA(pgm)->buffersize); PDATA(pgm)->use_blockmode = 1; } else { - PDATA(pgm)->use_blockmode = 0; + PDATA(pgm)->use_blockmode = 0; } + } else { + PDATA(pgm)->use_blockmode = 0; + } - if (PDATA(pgm)->devcode == 0) { - char devtype_1st; - int dev_supported = 0; + if (PDATA(pgm)->devcode == 0) { + char devtype_1st; + int dev_supported = 0; - /* Get list of devices that the programmer supports. */ + /* Get list of devices that the programmer supports. */ EI(avr910_send(pgm, "t", 1)); msg_notice2("\nProgrammer supports the following devices:\n"); @@ -227,40 +230,40 @@ static int avr910_initialize(const PROGRAMMER *pgm, const AVRPART *p) { break; part = locate_part_by_avr910_devcode(part_list, c); - msg_notice2(" Device code: 0x%02x = %s\n", c & 0xff, part? part->desc: "(unknown)"); - - /* FIXME: Need to lookup devcode and report the device. */ - - if (p->avr910_devcode == c) - dev_supported = 1; - }; - msg_notice2("\n"); - - if (!dev_supported) { - if(ovsigck) - pmsg_warning("selected device %s is not supported by programmer %s\n", p->desc, pgmid); - else { - pmsg_error("selected device %s is not supported by programmer %s\n", p->desc, pgmid); - return -1; - } - } - /* If the user forced the selection, use the first device - type that is supported by the programmer. */ - buf[1] = ovsigck? devtype_1st: p->avr910_devcode; - } else { - /* devcode overridden by -x devcode= option */ - buf[1] = (char)(PDATA(pgm)->devcode); + msg_notice2(" Device code: 0x%02x = %s\n", c & 0xff, part? part->desc: "(unknown)"); + + /* FIXME: Need to lookup devcode and report the device. */ + + if (p->avr910_devcode == c) + dev_supported = 1; + }; + msg_notice2("\n"); + + if (!dev_supported) { + if(ovsigck) + pmsg_warning("selected device %s is not supported by programmer %s\n", p->desc, pgmid); + else { + pmsg_error("selected device %s is not supported by programmer %s\n", p->desc, pgmid); + return -1; + } } + /* If the user forced the selection, use the first device + type that is supported by the programmer. */ + buf[1] = ovsigck? devtype_1st: p->avr910_devcode; + } else { + /* devcode overridden by -x devcode= option */ + buf[1] = (char)(PDATA(pgm)->devcode); + } - /* Tell the programmer which part we selected. */ - buf[0] = 'T'; - /* buf[1] has been set up above */ + /* Tell the programmer which part we selected. */ + buf[0] = 'T'; + /* buf[1] has been set up above */ EI(avr910_send(pgm, buf, 2)); if(avr910_vfy_cmd_sent(pgm, "select device") < 0) return -1; - pmsg_notice("avr910_devcode selected: 0x%02x\n", (unsigned) buf[1]); + pmsg_notice("avr910_devcode selected: 0x%02x\n", (unsigned) buf[1]); return pgm->program_enable(pgm, p); } @@ -282,64 +285,41 @@ static void avr910_enable(PROGRAMMER *pgm, const AVRPART *p) { static int avr910_cmd(const PROGRAMMER *pgm, const unsigned char *cmd, unsigned char *res) { - char buf[5]; + char buf[5]; - /* FIXME: Insert version check here */ + /* FIXME: Insert version check here */ - buf[0] = '.'; /* New Universal Command */ - buf[1] = cmd[0]; - buf[2] = cmd[1]; - buf[3] = cmd[2]; - buf[4] = cmd[3]; + buf[0] = '.'; /* New Universal Command */ + buf[1] = cmd[0]; + buf[2] = cmd[1]; + buf[3] = cmd[2]; + buf[4] = cmd[3]; EI(avr910_send(pgm, buf, 5)); EI(avr910_recv(pgm, buf, 2)); - res[0] = 0x00; /* Dummy value */ - res[1] = cmd[0]; - res[2] = cmd[1]; - res[3] = buf[0]; + res[0] = 0x00; /* Dummy value */ + res[1] = cmd[0]; + res[2] = cmd[1]; + res[3] = buf[0]; - return 0; + return 0; } static int avr910_parseextparms(const PROGRAMMER *pgm, const LISTID extparms) { - LNODEID ln; - const char *extended_param; - int rv = 0; - - for (ln = lfirst(extparms); ln; ln = lnext(ln)) { - extended_param = ldata(ln); - - if (str_starts(extended_param, "devcode=")) { - int devcode; - if (sscanf(extended_param, "devcode=%i", &devcode) != 1 || - devcode <= 0 || devcode > 255) { - pmsg_error("invalid devcode '%s'\n", extended_param); - rv = -1; - continue; - } - pmsg_notice2("avr910_parseextparms(): devcode overwritten as 0x%02x\n", devcode); - PDATA(pgm)->devcode = devcode; - - continue; - } - if (str_eq(extended_param, "no_blockmode")) { - pmsg_notice2("avr910_parseextparms(-x): no testing for Blockmode\n"); - PDATA(pgm)->test_blockmode = 0; - - continue; - } - if (str_eq(extended_param, "help")) { - msg_error("%s -c %s extended options:\n", progname, pgmid); - msg_error(" -xdevcode= Override device code\n"); - msg_error(" -xno_blockmode Disable default checking for block transfer capability\n"); - msg_error(" -xhelp Show this help menu and exit\n"); - exit(0); - } - - pmsg_error("invalid extended parameter '%s'\n", extended_param); + LNODEID ln; + const char *extended_param; + int rv = 0; + + for (ln = lfirst(extparms); ln; ln = lnext(ln)) { + extended_param = ldata(ln); + + if (str_starts(extended_param, "devcode=")) { + int devcode; + if (sscanf(extended_param, "devcode=%i", &devcode) != 1 || + devcode <= 0 || devcode > 255) { + pmsg_error("invalid devcode '%s'\n", extended_param); rv = -1; continue; } @@ -362,7 +342,11 @@ static int avr910_parseextparms(const PROGRAMMER *pgm, const LISTID extparms) { return LIBAVRDUDE_EXIT; } - return rv; + pmsg_error("invalid extended parameter '%s'\n", extended_param); + rv = -1; + } + + return rv; } @@ -390,12 +374,12 @@ static void avr910_close(PROGRAMMER *pgm) { static void avr910_display(const PROGRAMMER *pgm, const char *p) { - return; + return; } static void avr910_set_addr(const PROGRAMMER *pgm, unsigned long addr) { - char cmd[3]; + char cmd[3]; cmd[0] = 'A'; cmd[1] = (addr >> 8) & 0xff; @@ -409,7 +393,7 @@ static void avr910_set_addr(const PROGRAMMER *pgm, unsigned long addr) { static int avr910_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m, unsigned long addr, unsigned char value) { - char cmd[2]; + char cmd[2]; if (mem_is_flash(m)) { if (addr & 0x01) { @@ -426,10 +410,9 @@ static int avr910_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRM return avr_write_byte_default(pgm, p, m, addr, value); } - avr910_set_addr(pgm, addr); + cmd[1] = value; - avr910_send(pgm, cmd, sizeof(cmd)); - avr910_vfy_cmd_sent(pgm, "write byte"); + avr910_set_addr(pgm, addr); EI(avr910_send(pgm, cmd, sizeof(cmd))); return avr910_vfy_cmd_sent(pgm, "write byte"); @@ -439,7 +422,7 @@ static int avr910_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRM static int avr910_read_byte_flash(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m, unsigned long addr, unsigned char * value) { - char buf[2]; + char buf[2]; if(PDATA(pgm)->ctype == 'F' && PDATA(pgm)->caddr == addr) { *value = PDATA(pgm)->cvalue; @@ -456,7 +439,7 @@ static int avr910_read_byte_flash(const PROGRAMMER *pgm, const AVRPART *p, const PDATA(pgm)->cvalue = buf[addr & 1]; PDATA(pgm)->caddr = addr ^ 1; - return 0; + return 0; } @@ -467,22 +450,22 @@ static int avr910_read_byte_eeprom(const PROGRAMMER *pgm, const AVRPART *p, cons EI(avr910_send(pgm, "d", 1)); EI(avr910_recv(pgm, (char *) value, 1)); - return 0; + return 0; } static int avr910_read_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m, unsigned long addr, unsigned char * value) { - if (mem_is_flash(m)) { - return avr910_read_byte_flash(pgm, p, m, addr, value); - } + if (mem_is_flash(m)) { + return avr910_read_byte_flash(pgm, p, m, addr, value); + } - if (mem_is_eeprom(m)) { - return avr910_read_byte_eeprom(pgm, p, m, addr, value); - } + if (mem_is_eeprom(m)) { + return avr910_read_byte_eeprom(pgm, p, m, addr, value); + } - return avr_read_byte_default(pgm, p, m, addr, value); + return avr_read_byte_default(pgm, p, m, addr, value); } @@ -490,12 +473,12 @@ static int avr910_paged_write_flash(const PROGRAMMER *pgm, const AVRPART *p, con unsigned int page_size, unsigned int addr, unsigned int n_bytes) { - unsigned char cmd[] = {'c', 'C'}; - char buf[2]; - unsigned int max_addr = addr + n_bytes; - unsigned int page_addr; - int page_bytes = page_size; - int page_wr_cmd_pending = 0; + unsigned char cmd[] = {'c', 'C'}; + char buf[2]; + unsigned int max_addr = addr + n_bytes; + unsigned int page_addr; + int page_bytes = page_size; + int page_wr_cmd_pending = 0; PDATA(pgm)->ctype = 0; // Invalidate read cache @@ -510,40 +493,30 @@ static int avr910_paged_write_flash(const PROGRAMMER *pgm, const AVRPART *p, con if(avr910_vfy_cmd_sent(pgm, "write byte") < 0) return -1; - addr++; - page_bytes--; + addr++; + page_bytes--; - if (m->paged && (page_bytes == 0)) { - /* Send the "Issue Page Write" if we have sent a whole page. */ + if (m->paged && (page_bytes == 0)) { + /* Send the "Issue Page Write" if we have sent a whole page. */ avr910_set_addr(pgm, page_addr>>1); EI(avr910_send(pgm, "m", 1)); if(avr910_vfy_cmd_sent(pgm, "flush page") < 0) return -1; - page_wr_cmd_pending = 0; - usleep(m->max_write_delay); - avr910_set_addr(pgm, addr>>1); + page_wr_cmd_pending = 0; + usleep(m->max_write_delay); + avr910_set_addr(pgm, addr>>1); - /* Set page address for next page. */ + /* Set page address for next page. */ - page_addr = addr; - page_bytes = page_size; - } - else if ((PDATA(pgm)->has_auto_incr_addr != 'Y') && ((addr & 0x01) == 0)) { - avr910_set_addr(pgm, addr>>1); - } + page_addr = addr; + page_bytes = page_size; } - - /* If we didn't send the page wr cmd after the last byte written in the - loop, send it now. */ - - if (page_wr_cmd_pending) { - avr910_set_addr(pgm, page_addr>>1); - avr910_send(pgm, "m", 1); - avr910_vfy_cmd_sent(pgm, "flush final page"); - usleep(m->max_write_delay); + else if ((PDATA(pgm)->has_auto_incr_addr != 'Y') && ((addr & 0x01) == 0)) { + avr910_set_addr(pgm, addr>>1); } + } /* If we didn't send the page wr cmd after the last byte written in the loop, send it now. */ @@ -565,12 +538,12 @@ static int avr910_paged_write_eeprom(const PROGRAMMER *pgm, const AVRPART *p, unsigned int page_size, unsigned int addr, unsigned int n_bytes) { - char cmd[2]; - unsigned int max_addr = addr + n_bytes; + char cmd[2]; + unsigned int max_addr = addr + n_bytes; - avr910_set_addr(pgm, addr); + avr910_set_addr(pgm, addr); - cmd[0] = 'D'; + cmd[0] = 'D'; while (addr < max_addr) { cmd[1] = m->buf[addr]; @@ -579,7 +552,7 @@ static int avr910_paged_write_eeprom(const PROGRAMMER *pgm, const AVRPART *p, return -1; usleep(m->max_write_delay); - addr++; + addr++; if (PDATA(pgm)->has_auto_incr_addr != 'Y') avr910_set_addr(pgm, addr); @@ -655,7 +628,7 @@ static int avr910_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AVRM char buf[2]; int isee = mem_is_eeprom(m); - max_addr = addr + n_bytes; + max_addr = addr + n_bytes; if(mem_is_flash(m)) cmd[0] = 'R'; @@ -709,12 +682,12 @@ static int avr910_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AVRM /* Signature byte reads are always 3 bytes. */ static int avr910_read_sig_bytes(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m) { - unsigned char tmp; + unsigned char tmp; - if (m->size < 3) { - pmsg_error("memsize too small for sig byte read"); - return -1; - } + if (m->size < 3) { + pmsg_error("memsize too small for sig byte read"); + return -1; + } EI(avr910_send(pgm, "s", 1)); EI(avr910_recv(pgm, (char *) m->buf, 3)); @@ -723,40 +696,40 @@ static int avr910_read_sig_bytes(const PROGRAMMER *pgm, const AVRPART *p, const m->buf[2] = m->buf[0]; m->buf[0] = tmp; - return 3; + return 3; } const char avr910_desc[] = "Serial programmers using protocol described in application note AVR910"; void avr910_initpgm(PROGRAMMER *pgm) { - strcpy(pgm->type, "avr910"); - - /* - * mandatory functions - */ - pgm->initialize = avr910_initialize; - pgm->display = avr910_display; - pgm->enable = avr910_enable; - pgm->disable = avr910_disable; - pgm->program_enable = avr910_program_enable; - pgm->chip_erase = avr910_chip_erase; - pgm->cmd = avr910_cmd; - pgm->open = avr910_open; - pgm->close = avr910_close; - - /* - * optional functions - */ - - pgm->write_byte = avr910_write_byte; - pgm->read_byte = avr910_read_byte; - - pgm->paged_write = avr910_paged_write; - pgm->paged_load = avr910_paged_load; - - pgm->read_sig_bytes = avr910_read_sig_bytes; - - pgm->parseextparams = avr910_parseextparms; - pgm->setup = avr910_setup; - pgm->teardown = avr910_teardown; + strcpy(pgm->type, "avr910"); + + /* + * mandatory functions + */ + pgm->initialize = avr910_initialize; + pgm->display = avr910_display; + pgm->enable = avr910_enable; + pgm->disable = avr910_disable; + pgm->program_enable = avr910_program_enable; + pgm->chip_erase = avr910_chip_erase; + pgm->cmd = avr910_cmd; + pgm->open = avr910_open; + pgm->close = avr910_close; + + /* + * optional functions + */ + + pgm->write_byte = avr910_write_byte; + pgm->read_byte = avr910_read_byte; + + pgm->paged_write = avr910_paged_write; + pgm->paged_load = avr910_paged_load; + + pgm->read_sig_bytes = avr910_read_sig_bytes; + + pgm->parseextparams = avr910_parseextparms; + pgm->setup = avr910_setup; + pgm->teardown = avr910_teardown; } \ No newline at end of file