From b643f0a1a03a69c3043a416ceba5bbfc5ba33ba9 Mon Sep 17 00:00:00 2001 From: Jan Egil Ruud Date: Wed, 30 Nov 2022 23:49:20 +0100 Subject: [PATCH] #822 Fixed bug in TPI byte write for ATtiny20 and ATtiny40. --- src/jtag3.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/jtag3.c b/src/jtag3.c index 94861f23..f099b245 100644 --- a/src/jtag3.c +++ b/src/jtag3.c @@ -2689,6 +2689,7 @@ static int jtag3_erase_tpi(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM static int jtag3_write_byte_tpi(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *mem, unsigned long addr, unsigned char data) { size_t len = 11; + size_t data_size = 2; unsigned char cmd[17]; unsigned char* resp; int status; @@ -2702,11 +2703,22 @@ static int jtag3_write_byte_tpi(const PROGRAMMER *pgm, const AVRPART *p, const A paddr = mem->offset + addr; + if (mem->n_word_writes != 0) { + if (mem->n_word_writes == 2) { + len = 13; + data_size = 4; + } + else if (mem->n_word_writes == 4) { + len = 17; + data_size = 8; + } + } + cmd[0] = XPRG_CMD_WRITE_MEM; cmd[1] = tpi_get_memtype(mem); cmd[2] = 0; // Page Mode - Not used - u32_to_b4_big_endian((cmd+3), paddr); // Address - u16_to_b2_big_endian((cmd+7), 2); // Size + u32_to_b4_big_endian((cmd+3), paddr); // Address + u16_to_b2_big_endian((cmd+7), data_size); // Size cmd[9] = data; cmd[10] = 0xFF; // len = 11 if no n_word_writes cmd[11] = 0xFF; @@ -2716,13 +2728,6 @@ static int jtag3_write_byte_tpi(const PROGRAMMER *pgm, const AVRPART *p, const A cmd[15] = 0xFF; cmd[16] = 0xFF; // len = 17 if n_word_writes == 4 - if (mem->n_word_writes != 0) { - if (mem->n_word_writes == 2) - len = 13; - else if (mem->n_word_writes == 4) - len = 17; - } - if ((status = jtag3_command_tpi(pgm, cmd, len, &resp, "write byte")) < 0) return -1; free(resp);