From 8fab6d984cfe86b3c611deabc2192130f4cf1b65 Mon Sep 17 00:00:00 2001 From: sys64738 Date: Sun, 3 Jul 2022 17:13:39 +0200 Subject: [PATCH] rl78/g10 now works too --- src/tool78/tool78_cmds.c | 51 ++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/src/tool78/tool78_cmds.c b/src/tool78/tool78_cmds.c index 3727046..6abe39e 100644 --- a/src/tool78/tool78_cmds.c +++ b/src/tool78/tool78_cmds.c @@ -946,12 +946,12 @@ enum tool78_stat tool78_do_g10_get_flash_size(struct tool78_hw* hw, if (rr != 1) return tool78_stat_internal_error; data[1] = data[2] = 0; - rr = hw->recv(3, data, (tDT+tDRT)*100); // cmd is echoed back + rr = hw->recv(2, data, (tDT+tDRT)*100); // cmd is echoed back //printf("gfsz rr=%d data=%02x %02x %02x\n", rr, data[0], data[1], data[2]); - if (rr != 3) return tool78_stat_timeout_error; - if (data[1] != tool78_stat_ack) return data[1]; + if (rr != 2) return tool78_stat_timeout_error; + if (data[0] != tool78_stat_ack) return data[0]; - *fsz = data[2]; + *fsz = data[1]; busy_wait_us_32(tDTR); data[0] = tool78_stat_nak; // should send ack here, but send nak to stop crc calc @@ -959,7 +959,7 @@ enum tool78_stat tool78_do_g10_get_flash_size(struct tool78_hw* hw, //printf("send nak: rr=%d data=%02x\n", rr, data[0]); if (rr != 1) return tool78_stat_internal_error; - rr = hw->recv(3, data, tCRC); + rr = hw->recv(2, data, tCRC); //printf("recv rr=%d data=%02x %02x %02x\n", rr, data[0], data[1], data[2]); // should answer with 0x04 but let's not care here @@ -974,17 +974,17 @@ enum tool78_stat tool78_do_g10_check_crc(struct tool78_hw* hw, int rr = hw->send(1, data, -1); if (rr != 1) return tool78_stat_internal_error; - rr = hw->recv(3, data, (tDT+tDRT)*100); - if (rr != 3) return tool78_stat_timeout_error; - if (data[1] != tool78_stat_ack) return data[1]; + rr = hw->recv(2, data, (tDT+tDRT)*100); + if (rr != 2) return tool78_stat_timeout_error; + if (data[0] != tool78_stat_ack) return data[0]; busy_wait_us_32(tDTR); - if (data[2] != fsz) { + if (data[1] != fsz) { data[0] = tool78_stat_nak; // should send ack here, but send nak to stop crc calc rr = hw->send(1, data, -1); if (rr != 1) return tool78_stat_internal_error; - rr = hw->recv(3, data, tCRC); + rr = hw->recv(2, data, tCRC); // should answer with 0x04 but let's not care here return tool78_stat_bad_flash_size; @@ -993,20 +993,19 @@ enum tool78_stat tool78_do_g10_check_crc(struct tool78_hw* hw, rr = hw->send(1, data, -1); if (rr != 1) return tool78_stat_internal_error; - rr = hw->recv(2, data, tCRC*2); - if (rr != 2) return tool78_stat_timeout_error; + rr = hw->recv(1, data, tCRC*2); + if (rr != 1) return tool78_stat_timeout_error; if (data[0] == 0xff) { // framing error bullshit - data[0] = data[1]; - rr = hw->recv(1, &data[1], tCRC); + rr = hw->recv(1, &data[0], tCRC); if (rr != 1) return tool78_stat_timeout_error; } //printf("recv1 rr=%d data=%02x %02x\n", rr, data[0], data[1]); - enum tool78_stat st = data[1]; + enum tool78_stat st = data[0]; if (st != tool78_stat_ack) return st; - rr = hw->recv(2, data, tDT*1000); + rr = hw->recv(3, data, tDT*1000); //printf("recv2 rr=%d data=%02x %02x %02x\n", rr, data[0], data[1], data[2]); - if (rr != 2) return tool78_stat_timeout_error; + if (rr != 3) return tool78_stat_timeout_error; if (data[0] == 0xff) { data[0] = data[1]; rr = hw->recv(1, &data[1], tDT*500); @@ -1014,7 +1013,7 @@ enum tool78_stat tool78_do_g10_check_crc(struct tool78_hw* hw, } //printf("recv2 rr=%d data=%02x %02x %02x\n", rr, data[0], data[1], data[2]); - *crc = data[0] | ((uint16_t)data[1] << 8); + *crc = data[1] | ((uint16_t)data[2] << 8); return tool78_stat_ack; } @@ -1248,7 +1247,13 @@ int tool78_ocd_read(struct tool78_hw* hw, uint16_t off, uint8_t len, rr = hw->recv(1, hdr, 1000); //printf("stat rr=%d hdr=%02x\n", rr, hdr[0]); if (rr != 1) return -1; - if (hdr[0] != 0x00) return -3; + if (hdr[0] != 0x00) { + if (hdr[0] == len) { + rr = hw->recv(1, hdr, 1000); + //printf("stat 2 rr=%d hdr=%02x\n", rr, hdr[0]); + if (rr != 1 || hdr[0] != 0x00) return -3; + } else return -3; + } busy_wait_us_32(100); hdr[0] = tool78ocd_cmdg10_read_raw; @@ -1497,12 +1502,12 @@ static enum tool78_stat tool78_init_common(struct tool78_hw* hw) { } else { // 0x3a/0xc5 is echoed back as well // TODO err why does this happen? uint8_t data[5]; - int rr = hw->recv(2, data, 1000); - st = data[1]; + int rr = hw->recv(1, data, 1000); //printf("init rr:%d st=%02x\n", rr, st); - if (rr != 2) return tool78_stat_timeout_error; + if (rr != 1) return tool78_stat_timeout_error; + st = data[0]; - if (st == 0x06) { + if (st == 0x06 && (hw->flags & tool78_hw_flag_do_ocd)) { // should be 5 zero bytes, but let's not check rr = hw->recv(5, data, 5*1000); if (rr != 5) return tool78_stat_no_ocd_status;