Utilise magic memory tree interface for jtagmkII.c

This commit is contained in:
Stefan Rueger
2024-04-21 16:05:46 +01:00
parent 97e659924a
commit c41fa3db2b
2 changed files with 80 additions and 106 deletions

View File

@@ -44,6 +44,7 @@ extern const char *pgmid; // Programmer -c string
#define mmt_strdup(s) cfg_strdup(__func__, s)
#define mmt_malloc(n) cfg_malloc(__func__, n)
#define mmt_realloc(p, n) cfg_realloc(__func__, p, n)
#define mmt_free(p) free(p)
int avrdude_message2(FILE *fp, int lno, const char *file, const char *func, int msgmode, int msglvl, const char *format, ...);

View File

@@ -176,12 +176,12 @@ static int jtagmkII_paged_load32(const PROGRAMMER *pgm, const AVRPART *p, const
unsigned int addr, unsigned int n_bytes);
void jtagmkII_setup(PROGRAMMER *pgm) {
pgm->cookie = cfg_malloc("jtagmkII_setup()", sizeof(struct pdata));
pgm->cookie = mmt_malloc(sizeof(struct pdata));
PDATA(pgm)->rts_mode = RTS_MODE_DEFAULT;
}
void jtagmkII_teardown(PROGRAMMER *pgm) {
free(pgm->cookie);
mmt_free(pgm->cookie);
}
static unsigned long b4_to_u32(unsigned char *b) {
@@ -404,12 +404,7 @@ int jtagmkII_send(const PROGRAMMER *pgm, unsigned char *data, size_t len) {
msg_debug("\n");
pmsg_debug("jtagmkII_send(): sending %lu bytes\n", (unsigned long) len);
if ((buf = malloc(len + 10)) == NULL)
{
pmsg_error("out of memory");
return -1;
}
buf = mmt_malloc(len + 10);
buf[0] = MESSAGE_START;
u16_to_b2(buf + 1, PDATA(pgm)->command_sequence);
u32_to_b4(buf + 3, len);
@@ -420,11 +415,11 @@ int jtagmkII_send(const PROGRAMMER *pgm, unsigned char *data, size_t len) {
if (serial_send(&pgm->fd, buf, len + 10) != 0) {
pmsg_error("unable to send command to serial port\n");
free(buf);
mmt_free(buf);
return -1;
}
free(buf);
mmt_free(buf);
return 0;
}
@@ -441,7 +436,7 @@ static int jtagmkII_drain(const PROGRAMMER *pgm, int display) {
* whether it matches the expected sequence number, including event
* notification frames (seqno == 0xffff).
*
* Caller must eventually free the buffer.
* Caller must eventually mmt_free() the buffer.
*/
static int jtagmkII_recv_frame(const PROGRAMMER *pgm, unsigned char **msg,
unsigned short * seqno) {
@@ -478,13 +473,14 @@ static int jtagmkII_recv_frame(const PROGRAMMER *pgm, unsigned char **msg,
for(l = 0; l < msglen; l++)
rv += serial_recv(&pgm->fd, &c, 1);
} else {
rv += serial_recv(&pgm->fd, buf + 8, msglen);
if(buf)
rv += serial_recv(&pgm->fd, buf + 8, msglen);
}
if (rv != 0) {
timedout:
/* timeout in receive */
pmsg_notice2("jtagmkII_recv(): timeout receiving packet\n");
free(buf);
mmt_free(buf);
return -1;
}
} else {
@@ -525,10 +521,8 @@ static int jtagmkII_recv_frame(const PROGRAMMER *pgm, unsigned char **msg,
msglen, MAX_MESSAGE);
state = sSTART;
headeridx = 0;
} else if ((buf = malloc(msglen + 10)) == NULL) {
pmsg_error("out of memory\n");
ignorpkt++;
} else {
buf = mmt_malloc(msglen + 10);
memcpy(buf, header, 8);
}
} else {
@@ -543,15 +537,16 @@ static int jtagmkII_recv_frame(const PROGRAMMER *pgm, unsigned char **msg,
break;
case sCSUM1:
case sCSUM2:
buf[l++] = c;
if (state == sCSUM2) {
if(buf)
buf[l++] = c;
if (state == sCSUM2 && buf) {
if (crcverify(buf, msglen + 10)) {
if (verbose >= 9)
pmsg_trace2("jtagmkII_recv(): CRC OK");
state = sDONE;
} else {
pmsg_error("wrong checksum\n");
free(buf);
mmt_free(buf);
return -4;
}
} else
@@ -559,14 +554,14 @@ static int jtagmkII_recv_frame(const PROGRAMMER *pgm, unsigned char **msg,
break;
default:
pmsg_error("unknown state\n");
free(buf);
mmt_free(buf);
return -5;
}
tnow = avr_timestamp();
if (tnow - tstart > timeoutval) {
pmsg_error("timeout\n");
free(buf);
mmt_free(buf);
return -1;
}
@@ -594,7 +589,7 @@ int jtagmkII_recv(const PROGRAMMER *pgm, unsigned char **msg) {
/*
* We move the payload to the beginning of the buffer, to make
* the job easier for the caller. We have to return the
* original pointer though, as the caller must free() it.
* original pointer though, as the caller must mmt_free() it.
*/
memmove(*msg, *msg + 8, rv);
@@ -609,7 +604,7 @@ int jtagmkII_recv(const PROGRAMMER *pgm, unsigned char **msg) {
pmsg_notice2("jtagmkII_recv(): got wrong sequence number, %u != %u\n",
r_seqno, PDATA(pgm)->command_sequence);
}
free(*msg);
mmt_free(*msg);
}
}
@@ -681,10 +676,10 @@ int jtagmkII_getsync(const PROGRAMMER *pgm, int mode) {
imsg_notice2("Device ID: %s\n",
resp + 16);
}
free(resp);
mmt_free(resp);
break;
}
free(resp);
mmt_free(resp);
}
}
if (tries >= MAXTRIES) {
@@ -789,7 +784,7 @@ retry:
} else
msg_notice2("0x%02x (%d bytes msg)\n", resp[0], status);
c = resp[0];
free(resp);
mmt_free(resp);
if (c != RSP_OK) {
pmsg_error("bad response to set parameter command: %s\n", jtagmkII_get_rc(pgm, c));
return -1;
@@ -830,7 +825,7 @@ static int jtagmkII_chip_erase(const PROGRAMMER *pgm, const AVRPART *p) {
} else
msg_notice2("0x%02x (%d bytes msg)\n", resp[0], status);
c = resp[0];
free(resp);
mmt_free(resp);
if (c != RSP_OK) {
pmsg_error("bad response to chip erase command: %s\n", jtagmkII_get_rc(pgm, c));
return -1;
@@ -915,7 +910,7 @@ static void jtagmkII_set_devdescr(const PROGRAMMER *pgm, const AVRPART *p) {
} else
msg_notice2("0x%02x (%d bytes msg)\n", resp[0], status);
c = resp[0];
free(resp);
mmt_free(resp);
if (c != RSP_OK) {
pmsg_error("bad response to set device descriptor command: %s\n", jtagmkII_get_rc(pgm, c));
}
@@ -985,7 +980,7 @@ static void jtagmkII_set_xmega_params(const PROGRAMMER *pgm, const AVRPART *p) {
} else
msg_notice2("0x%02x (%d bytes msg)\n", resp[0], status);
c = resp[0];
free(resp);
mmt_free(resp);
if (c != RSP_OK) {
pmsg_error("bad response to set device descriptor command: %s\n", jtagmkII_get_rc(pgm, c));
}
@@ -1026,7 +1021,7 @@ static int jtagmkII_reset(const PROGRAMMER *pgm, unsigned char flags) {
} else
msg_notice2("0x%02x (%d bytes msg)\n", resp[0], status);
c = resp[0];
free(resp);
mmt_free(resp);
if (c != RSP_OK) {
pmsg_error("bad response to reset command: %s\n", jtagmkII_get_rc(pgm, c));
return -1;
@@ -1065,7 +1060,7 @@ static int jtagmkII_program_enable(const PROGRAMMER *pgm) {
} else
msg_notice2("0x%02x (%d bytes msg)\n", resp[0], status);
c = resp[0];
free(resp);
mmt_free(resp);
if (c != RSP_OK) {
pmsg_warning("bad response to enter progmode command: %s\n", jtagmkII_get_rc(pgm, c));
if (c == RSP_ILLEGAL_JTAG_ID) {
@@ -1111,7 +1106,7 @@ static int jtagmkII_program_disable(const PROGRAMMER *pgm) {
} else
msg_notice2("0x%02x (%d bytes msg)\n", resp[0], status);
c = resp[0];
free(resp);
mmt_free(resp);
if (c != RSP_OK) {
pmsg_error("bad response to leave progmode command: %s\n", jtagmkII_get_rc(pgm, c));
return -1;
@@ -1273,18 +1268,11 @@ static int jtagmkII_initialize(const PROGRAMMER *pgm, const AVRPART *p) {
}
}
free(PDATA(pgm)->flash_pagecache);
free(PDATA(pgm)->eeprom_pagecache);
if ((PDATA(pgm)->flash_pagecache = malloc(PDATA(pgm)->flash_pagesize)) == NULL) {
pmsg_error("out of memory\n");
return -1;
}
if ((PDATA(pgm)->eeprom_pagecache = malloc(PDATA(pgm)->eeprom_pagesize)) == NULL) {
pmsg_error("out of memory\n");
free(PDATA(pgm)->flash_pagecache);
return -1;
}
PDATA(pgm)->flash_pageaddr = PDATA(pgm)->eeprom_pageaddr = (unsigned long)-1L;
mmt_free(PDATA(pgm)->flash_pagecache);
mmt_free(PDATA(pgm)->eeprom_pagecache);
PDATA(pgm)->flash_pagecache = mmt_malloc(PDATA(pgm)->flash_pagesize);
PDATA(pgm)->eeprom_pagecache = mmt_malloc(PDATA(pgm)->eeprom_pagesize);
PDATA(pgm)->flash_pageaddr = PDATA(pgm)->eeprom_pageaddr = (unsigned long) -1L;
if (PDATA(pgm)->fwver >= 0x700 && (p->prog_modes & (PM_PDI | PM_UPDI))) {
/*
@@ -1319,9 +1307,9 @@ static int jtagmkII_initialize(const PROGRAMMER *pgm, const AVRPART *p) {
}
static void jtagmkII_disable(const PROGRAMMER *pgm) {
free(PDATA(pgm)->flash_pagecache);
mmt_free(PDATA(pgm)->flash_pagecache);
PDATA(pgm)->flash_pagecache = NULL;
free(PDATA(pgm)->eeprom_pagecache);
mmt_free(PDATA(pgm)->eeprom_pagecache);
PDATA(pgm)->eeprom_pagecache = NULL;
/*
@@ -1751,7 +1739,7 @@ void jtagmkII_close(PROGRAMMER * pgm)
} else
msg_notice2("0x%02x (%d bytes msg)\n", resp[0], status);
c = resp[0];
free(resp);
mmt_free(resp);
if (c != RSP_OK) {
pmsg_error("bad response to GO command: %s\n", jtagmkII_get_rc(pgm, c));
}
@@ -1774,7 +1762,7 @@ void jtagmkII_close(PROGRAMMER * pgm)
} else
msg_notice2("0x%02x (%d bytes msg)\n", resp[0], status);
c = resp[0];
free(resp);
mmt_free(resp);
if (c != RSP_OK) {
pmsg_error("bad response to sign-off command: %s\n", jtagmkII_get_rc(pgm, c));
}
@@ -1870,11 +1858,11 @@ static int jtagmkII_page_erase(const PROGRAMMER *pgm, const AVRPART *p, const AV
msg_notice2("0x%02x (%d bytes msg)\n", resp[0], status);
if (resp[0] != RSP_OK) {
pmsg_error("bad response to xmega erase command: %s\n", jtagmkII_get_rc(pgm, resp[0]));
free(resp);
mmt_free(resp);
serial_recv_timeout = otimeout;
return -1;
}
free(resp);
mmt_free(resp);
serial_recv_timeout = otimeout;
@@ -1900,11 +1888,7 @@ static int jtagmkII_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const A
if (page_size == 0) page_size = 256;
else if (page_size > 256) page_size = 256;
if ((cmd = malloc(page_size + 10)) == NULL) {
pmsg_error("out of memory\n");
return -1;
}
cmd = mmt_malloc(page_size + 10);
cmd[0] = CMND_WRITE_MEMORY;
if (mem_is_flash(m)) {
PDATA(pgm)->flash_pageaddr = (unsigned long)-1L;
@@ -1920,11 +1904,11 @@ static int jtagmkII_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const A
for (; addr < maxaddr; addr++) {
status = jtagmkII_write_byte(pgm, p, m, addr, m->buf[addr]);
if (status < 0) {
free(cmd);
mmt_free(cmd);
return -1;
}
}
free(cmd);
mmt_free(cmd);
return n_bytes;
}
cmd[1] = p->prog_modes & (PM_PDI | PM_UPDI)? MTYPE_EEPROM_XMEGA: MTYPE_EEPROM_PAGE;
@@ -1979,7 +1963,7 @@ static int jtagmkII_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const A
goto retry;
}
pmsg_error("timeout/error communicating with programmer (status %d)\n", status);
free(cmd);
mmt_free(cmd);
serial_recv_timeout = otimeout;
return -1;
}
@@ -1990,15 +1974,15 @@ static int jtagmkII_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const A
msg_notice2("0x%02x (%d bytes msg)\n", resp[0], status);
if (resp[0] != RSP_OK) {
pmsg_error("bad response to write memory command: %s\n", jtagmkII_get_rc(pgm, resp[0]));
free(resp);
free(cmd);
mmt_free(resp);
mmt_free(cmd);
serial_recv_timeout = otimeout;
return -1;
}
free(resp);
mmt_free(resp);
}
free(cmd);
mmt_free(cmd);
serial_recv_timeout = otimeout;
PDATA(pgm)->recently_written = 1;
@@ -2083,12 +2067,12 @@ static int jtagmkII_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AV
msg_notice2("0x%02x (%d bytes msg)\n", resp[0], status);
if (resp[0] != RSP_MEMORY) {
pmsg_error("bad response to read memory command: %s\n", jtagmkII_get_rc(pgm, resp[0]));
free(resp);
mmt_free(resp);
serial_recv_timeout = otimeout;
return -1;
}
memcpy(m->buf + addr, resp + 1, status-1);
free(resp);
mmt_free(resp);
}
serial_recv_timeout = otimeout;
@@ -2280,12 +2264,12 @@ retry:
} else
*value = resp[1];
free(resp);
mmt_free(resp);
PDATA(pgm)->recently_written = 0;
return 0;
fail:
free(resp);
mmt_free(resp);
return -1;
}
@@ -2391,12 +2375,12 @@ retry:
goto fail;
}
free(resp);
mmt_free(resp);
PDATA(pgm)->recently_written = 1;
return 0;
fail:
free(resp);
mmt_free(resp);
return -1;
}
@@ -2478,12 +2462,12 @@ int jtagmkII_getparm(const PROGRAMMER *pgm, unsigned char parm,
c = resp[0];
if (c != RSP_PARAMETER) {
pmsg_error("bad response to get parameter command: %s\n", jtagmkII_get_rc(pgm, c));
free(resp);
mmt_free(resp);
return -1;
}
memcpy(value, resp + 1, 4);
free(resp);
mmt_free(resp);
return 0;
}
@@ -2541,7 +2525,7 @@ static int jtagmkII_setparm(const PROGRAMMER *pgm, unsigned char parm,
} else
msg_notice2("0x%02x (%d bytes msg)\n", resp[0], status);
c = resp[0];
free(resp);
mmt_free(resp);
if (c != RSP_OK) {
pmsg_error("bad response to set parameter %s: %s\n", parstr, jtagmkII_get_rc(pgm, c));
return -1;
@@ -2982,18 +2966,11 @@ static int jtagmkII_initialize32(const PROGRAMMER *pgm, const AVRPART *p) {
return -1;
}
free(PDATA(pgm)->flash_pagecache);
free(PDATA(pgm)->eeprom_pagecache);
if ((PDATA(pgm)->flash_pagecache = malloc(PDATA(pgm)->flash_pagesize)) == NULL) {
pmsg_error("out of memory\n");
return -1;
}
if ((PDATA(pgm)->eeprom_pagecache = malloc(PDATA(pgm)->eeprom_pagesize)) == NULL) {
pmsg_error("out of memory\n");
free(PDATA(pgm)->flash_pagecache);
return -1;
}
PDATA(pgm)->flash_pageaddr = PDATA(pgm)->eeprom_pageaddr = (unsigned long)-1L;
mmt_free(PDATA(pgm)->flash_pagecache);
mmt_free(PDATA(pgm)->eeprom_pagecache);
PDATA(pgm)->flash_pagecache = mmt_malloc(PDATA(pgm)->flash_pagesize);
PDATA(pgm)->eeprom_pagecache = mmt_malloc(PDATA(pgm)->eeprom_pagesize);
PDATA(pgm)->flash_pageaddr = PDATA(pgm)->eeprom_pageaddr = (unsigned long) -1L;
for(j=0; j<2; ++j) {
buf[0] = CMND_GET_IR;
@@ -3006,7 +2983,7 @@ static int jtagmkII_initialize32(const PROGRAMMER *pgm, const AVRPART *p) {
pmsg_error("timeout/error communicating with programmer (status %d)\n", status);
return -1;
}
free(resp);
mmt_free(resp);
memset(buf, 0, sizeof(buf));
buf[0] = CMND_GET_xxx;
@@ -3034,7 +3011,7 @@ static int jtagmkII_initialize32(const PROGRAMMER *pgm, const AVRPART *p) {
return -1;
}
}
free(resp);
mmt_free(resp);
}
return 0;
@@ -3069,7 +3046,7 @@ static int jtagmkII_chip_erase32(const PROGRAMMER *pgm, const AVRPART *p_unused)
{lineno = __LINE__; goto eRR;}
}
x = resp[1];
free(resp);
mmt_free(resp);
if(x == *retP) ++retP;
if(*retP == 0x00) break;
}
@@ -3088,7 +3065,7 @@ static int jtagmkII_chip_erase32(const PROGRAMMER *pgm, const AVRPART *p_unused)
jtagmkII_send(pgm, buf, 3);
status = jtagmkII_recv(pgm, &resp);
if(status < 0 || resp[0] != RSP_OK) {lineno = __LINE__; goto eRR;}
free(resp);
mmt_free(resp);
return 0;
@@ -3142,7 +3119,7 @@ static unsigned long jtagmkII_read_SABaddr(const PROGRAMMER *pgm, unsigned long
}
val = b4_to_u32r(&resp[1]);
free(resp);
mmt_free(resp);
msg_notice2("\n");
pmsg_notice("jtagmkII_read_SABaddr(): OCD Register %lx -> %4.4lx\n", addr, val);
@@ -3236,7 +3213,7 @@ static int jtagmkII_open32(PROGRAMMER *pgm, const char *port) {
status = jtagmkII_recv(pgm, &resp);
if(status < 0 || resp[0] != RSP_OK)
return -1;
free(resp);
mmt_free(resp);
buf[1] = 0x03;
buf[2] = 0x02;
@@ -3244,7 +3221,7 @@ static int jtagmkII_open32(PROGRAMMER *pgm, const char *port) {
status = jtagmkII_recv(pgm, &resp);
if(status < 0 || resp[0] != RSP_OK)
return -1;
free(resp);
mmt_free(resp);
buf[1] = 0x03;
buf[2] = 0x04;
@@ -3252,7 +3229,7 @@ static int jtagmkII_open32(PROGRAMMER *pgm, const char *port) {
status = jtagmkII_recv(pgm, &resp);
if(status < 0 || resp[0] != RSP_OK)
return -1;
free(resp);
mmt_free(resp);
return 0;
}
@@ -3271,7 +3248,7 @@ static void jtagmkII_close32(PROGRAMMER * pgm) {
jtagmkII_send(pgm, buf, 3);
status = jtagmkII_recv(pgm, &resp);
if(status < 0 || resp[0] != RSP_OK) {lineno = __LINE__; goto eRR;}
free(resp);
mmt_free(resp);
buf[0] = CMND_SIGN_OFF;
pmsg_notice2("jtagmkII_close(): sending sign-off command: ");
@@ -3289,7 +3266,7 @@ static void jtagmkII_close32(PROGRAMMER * pgm) {
} else
msg_notice2("0x%02x (%d bytes msg)\n", resp[0], status);
c = resp[0];
free(resp);
mmt_free(resp);
if (c != RSP_OK) {
pmsg_error("bad response to sign-off command: %s\n", jtagmkII_get_rc(pgm, c));
}
@@ -3358,11 +3335,11 @@ static int jtagmkII_paged_load32(const PROGRAMMER *pgm, const AVRPART *p_unused,
msg_notice2("0x%02x (%d bytes msg)\n", resp[0], status);
if (resp[0] != 0x87) {
pmsg_error("bad response to write memory command: %s\n", jtagmkII_get_rc(pgm, resp[0]));
free(resp);
mmt_free(resp);
return -1;
}
memcpy(m->buf + addr, resp + 1, block_size);
free(resp);
mmt_free(resp);
}
@@ -3404,11 +3381,7 @@ static int jtagmkII_paged_write32(const PROGRAMMER *pgm, const AVRPART *p_unused
// page_size, n_bytes, pages, m->offset, pgm->page_size);
// Before any errors can happen
if ((cmd = malloc(pgm->page_size + 10)) == NULL) {
pmsg_error("out of memory\n");
return -1;
}
cmd = mmt_malloc(pgm->page_size + 10);
// Init SMC and set clocks
if(!(PDATA(pgm)->flags32 & FLAGS32_INIT_SMC)) {
status = jtagmkII_smc_init32(pgm);
@@ -3459,11 +3432,11 @@ static int jtagmkII_paged_write32(const PROGRAMMER *pgm, const AVRPART *p_unused
msg_notice2("0x%02x (%d bytes msg)\n", resp[0], status);
if (resp[0] != RSP_OK) {
pmsg_error("bad response to write memory command: %s\n", jtagmkII_get_rc(pgm, resp[0]));
free(resp);
free(cmd);
mmt_free(resp);
mmt_free(cmd);
return -1;
}
free(resp);
mmt_free(resp);
addr += block_size;
@@ -3477,12 +3450,12 @@ static int jtagmkII_paged_write32(const PROGRAMMER *pgm, const AVRPART *p_unused
status = jtagmkII_reset32(pgm, AVR32_SET4RUNNING); // AVR32_SET4RUNNING | AVR32_RELEASE_JTAG
if(status < 0) {lineno = __LINE__; goto eRR;}
free(cmd);
mmt_free(cmd);
return addr;
eRR:
serial_recv_timeout = otimeout;
free(cmd);
mmt_free(cmd);
pmsg_error("paged write failed at line %d (status=%x val=%lx)\n", lineno, status, val);
return -1;
}
@@ -3618,7 +3591,7 @@ static int jtagmkII_updi_term_keep_alive(const PROGRAMMER *pgm, const AVRPART *p
}
c = resp[0];
free(resp);
mmt_free(resp);
if (c != RSP_OK) {
pmsg_error("bad response to get_sync command: %s\n", jtagmkII_get_rc(pgm, c));
return -1;