diff --git a/src/tool78/tool78_cmds.c b/src/tool78/tool78_cmds.c index 22ad4ad..3727046 100644 --- a/src/tool78/tool78_cmds.c +++ b/src/tool78/tool78_cmds.c @@ -1107,22 +1107,17 @@ int tool78_ocd_version(struct tool78_hw* hw, uint16_t* ver) { bytes[0] = tool78ocd_cmd_version; int rr = hw->send(1, &bytes[0], -1); - // bytes[0] is tool78ocd_cmd_version echoed back - rr = hw->recv(3, bytes, 1000); - if (rr != 3) { - /*printf("OCDver rr: %d\n", rr); - for (int iii = 0; iii < rr; ++iii) { + rr = hw->recv(2, bytes, 1000); + if (rr != 2) { + printf("OCDver rr: %d\n", rr); + /*for (int iii = 0; iii < rr; ++iii) { printf("0x%02x%c", bytes[iii], (iii==rr-1)?'\n':' '); }*/ return -1; } - if (rr == 3) { - *ver = bytes[1] | ((uint16_t)bytes[2] << 8); - } /*else { - *ver = bytes[0] | ((uint16_t)bytes[1] << 8); - }*/ - //printf("ver=%04x\n", *ver); + *ver = bytes[0] | ((uint16_t)bytes[1] << 8); + return 0; } int tool78_ocd_connect(struct tool78_hw* hw, const uint8_t passwd[10]) { @@ -1133,16 +1128,15 @@ int tool78_ocd_connect(struct tool78_hw* hw, const uint8_t passwd[10]) { stuff[0] = is_g10 ? tool78ocd_cmdg10_connect : tool78ocd_cmd_connect; int rr = hw->send(1, &stuff[0], -1); - // stuff[1] is tool78ocd_cmd_connect echoed back - rr = hw->recv(2, &stuff[1], 1000); - if (rr != 2) { - /*printf("OCDconn rr: %d\n", rr); - for (int iii = 0; iii < rr; ++iii) { + rr = hw->recv(1, &stuff[1], 1000); + if (rr != 1) { + printf("OCDconn rr: %d\n", rr); + /*for (int iii = 0; iii < rr; ++iii) { printf("0x%02x%c", stuff[1+iii], (iii==rr-1)?'\n':' '); }*/ return -1; } - connst=stuff[2]; + connst=stuff[1]; //printf("connst 1=%02x\n", connst); if (connst == 0xff) { rr = hw->recv(1, &connst, 1000); @@ -1161,32 +1155,31 @@ int tool78_ocd_connect(struct tool78_hw* hw, const uint8_t passwd[10]) { rr = hw->send(is_g10 ? 10 : 11, stuff, -1); // stuff[9] is the checksum byte echoed back - rr = hw->recv(2, &stuff[9], 100000); + rr = hw->recv(1, &stuff[9], 100000); //printf("ocdpw 2 = %02x %02x\n", stuff[9], stuff[10]); - if (rr != 2) { - /*printf("OCDconn2 rr: %d\n", rr); - for (int iii = 0; iii < rr; ++iii) { + if (rr != 1) { + printf("OCDconn2 rr: %d\n", rr); + /*for (int iii = 0; iii < rr; ++iii) { printf("0x%02x%c", stuff[9+iii], (iii==rr-1)?'\n':' '); }*/ return -2; } if (stuff[9] == 0xff && is_g10) { // framing error bullshit - stuff[9] = stuff[10]; - rr = hw->recv(1, &stuff[10], 100000); + rr = hw->recv(1, &stuff[9], 100000); if (rr != 1) return -2; } //printf("ocdpw 3 = %02x %02x\n", stuff[9], stuff[10]); - while (stuff[10] == 0) { - rr = hw->recv(1, &stuff[10], 100000); + while (stuff[9] == 0) { + rr = hw->recv(1, &stuff[9], 100000); if (rr != 1) return -2; } //printf("ocdpw 4 = %02x %02x\n", stuff[9], stuff[10]); /*printf("cksum=0x%02x conn=%02x pw=%02x\n", - cksum, connst, stuff[10]);*/ + cksum, connst, stuff[9]);*/ - //printf("connst=%u stuff=%u\n", connst, stuff[10]); - return stuff[10];//(uint32_t)connst | ((uint32_t)stuff[10] << 8); + //printf("connst=%u stuff=%u\n", connst, stuff[9]); + return stuff[9]; } int tool78_ocd_read(struct tool78_hw* hw, uint16_t off, uint8_t len, uint8_t* data) { @@ -1206,7 +1199,7 @@ int tool78_ocd_read(struct tool78_hw* hw, uint16_t off, uint8_t len, rr = hw->send(sizeof hdr, hdr, -1); if (rr != sizeof hdr) return -1; - // command echoed back + /*// command echoed back rr = hw->recv(4, hdr, 1000); if (rr != 4) return -2; if (hdr[0] == 0xff) { // frame error bullshit @@ -1215,7 +1208,7 @@ int tool78_ocd_read(struct tool78_hw* hw, uint16_t off, uint8_t len, hdr[2] = hdr[3]; rr = hw->recv(1, &hdr[3], 1000); if (rr != 1) return -2; - } + }*/ // status rr = hw->recv(1, hdr, 1000); @@ -1227,8 +1220,8 @@ int tool78_ocd_read(struct tool78_hw* hw, uint16_t off, uint8_t len, rr = hw->send(1, hdr, -1); if (rr != 1) return -1; - rr = hw->recv(1, hdr, 1000); // command echoed back - if (rr != 1) return -1; + /*rr = hw->recv(1, hdr, 1000); // command echoed back + if (rr != 1) return -1;*/ rr = hw->recv(2, data, 1000*len); if (rr != 2) return -2; @@ -1242,7 +1235,7 @@ int tool78_ocd_read(struct tool78_hw* hw, uint16_t off, uint8_t len, if (rr != sizeof hdr) return -1; //printf("sent hdr\n"); - rr = hw->recv(4, hdr, 1000); + /*rr = hw->recv(4, hdr, 1000); //printf("stat rr=%d hdr=%02x %02x %02x %02x\n", rr, hdr[0], hdr[1], hdr[2], hdr[3]); if (rr != 4) return -2; if (hdr[0] == 0xff) { // frame error bullshit @@ -1251,7 +1244,7 @@ int tool78_ocd_read(struct tool78_hw* hw, uint16_t off, uint8_t len, hdr[2] = hdr[3]; rr = hw->recv(1, &hdr[3], 1000); if (rr != 1) return -2; - } + }*/ rr = hw->recv(1, hdr, 1000); //printf("stat rr=%d hdr=%02x\n", rr, hdr[0]); if (rr != 1) return -1; @@ -1263,9 +1256,9 @@ int tool78_ocd_read(struct tool78_hw* hw, uint16_t off, uint8_t len, if (rr != 1) return -1; //printf("sent rdcmd\n"); - rr = hw->recv(1, hdr, 1000); // command echoed back + /*rr = hw->recv(1, hdr, 1000); // command echoed back //printf("recv dd=%d hdr=%02x\n", rr, hdr[0]); - if (rr != 1) return -1; + if (rr != 1) return -1;*/ rr = hw->recv(len, data, 1000*len); //printf("got data rr=%d data=%02x\n", rr, data[0]); @@ -1281,12 +1274,12 @@ int tool78_ocd_read(struct tool78_hw* hw, uint16_t off, uint8_t len, int rr = hw->send(sizeof hdr, hdr, -1); if (rr != sizeof hdr) return -1; - rr = hw->recv(1, &hdr[2], 1000); + /*rr = hw->recv(1, &hdr[2], 1000); // last byte of header sent echoed back if (rr != 1 || hdr[2] != hdr[3]) { //printf("rd recv=%02x\n", hdr[2]); return -2; - } + }*/ rr = hw->recv(len, data, 1000*len); //printf("ocd read %d\n", rr); @@ -1313,7 +1306,7 @@ int tool78_ocd_write(struct tool78_hw* hw, uint16_t addr, uint8_t len, rr = hw->send(sizeof hdr, hdr, -1); if (rr != sizeof hdr) return -1; - // command echoed back + /*// command echoed back rr = hw->recv(4, hdr, 1000); if (rr != 4) return -2; if (hdr[0] == 0xff) { // frame error bullshit @@ -1322,7 +1315,7 @@ int tool78_ocd_write(struct tool78_hw* hw, uint16_t addr, uint8_t len, hdr[2] = hdr[3]; rr = hw->recv(1, &hdr[3], 1000); if (rr != 1) return -2; - } + }*/ // status rr = hw->recv(1, hdr, 1000); @@ -1336,10 +1329,10 @@ int tool78_ocd_write(struct tool78_hw* hw, uint16_t addr, uint8_t len, rr = hw->send(2, hdr, -1); if (rr != 2) return -1; - rr = hw->recv(3, hdr, 1000); // command echoed back + ack - if (rr != 3) return -1; + rr = hw->recv(1, hdr, 1000); + if (rr != 1) return -1; - return (hdr[2] == 0) ? 0 : -2; + return (hdr[0] == 0) ? 0 : -2; } else { hdr[0] = tool78ocd_cmdg10_setptr; hdr[1] = addr & 0xff; // l @@ -1350,7 +1343,7 @@ int tool78_ocd_write(struct tool78_hw* hw, uint16_t addr, uint8_t len, if (rr != sizeof hdr) return -1; // command echoed back - rr = hw->recv(4, hdr, 1000); + /*rr = hw->recv(4, hdr, 1000); //printf("wr p1: rr=%d %02x %02x %02x %02x\n", rr, hdr[0], // hdr[1], hdr[2], hdr[3]); if (rr != 4) return -2; @@ -1360,7 +1353,7 @@ int tool78_ocd_write(struct tool78_hw* hw, uint16_t addr, uint8_t len, hdr[2] = hdr[3]; rr = hw->recv(1, &hdr[3], 1000); if (rr != 1) return -2; - } + }*/ //printf("wr p1: rr=%d %02x %02x %02x %02x\n", rr, hdr[0], // hdr[1], hdr[2], hdr[3]); @@ -1382,10 +1375,10 @@ int tool78_ocd_write(struct tool78_hw* hw, uint16_t addr, uint8_t len, busy_wait_us_32(100); rr = hw->send(1, hdr, -1); if (rr != 1) return -1; - rr = hw->recv(1, hdr, 1000); // sigh + /*rr = hw->recv(1, hdr, 1000); // sigh //printf("wr p3: rr=%d %02x %02x %02x %02x\n", rr, hdr[0], // hdr[1], hdr[2], hdr[3]); - if (rr != 1) return -1; + if (rr != 1) return -1;*/ for (int i = 0; i < len; i += 8) { int todo = len - i; @@ -1394,10 +1387,10 @@ int tool78_ocd_write(struct tool78_hw* hw, uint16_t addr, uint8_t len, busy_wait_us_32(100); rr = hw->send(todo, &data[i], -1); if (rr != todo) return -1; - rr = hw->recv(todo, NULL, 1000*todo); // sigh + /*rr = hw->recv(todo, NULL, 1000*todo); // sigh //printf("wr p4: rr=%d %02x %02x %02x %02x\n", rr, hdr[0], // hdr[1], hdr[2], hdr[3]); - if (rr != todo) return -1; + if (rr != todo) return -1;*/ } rr = hw->recv(1, hdr, 1000); @@ -1420,10 +1413,10 @@ int tool78_ocd_write(struct tool78_hw* hw, uint16_t addr, uint8_t len, if (rr != len) return -1; // last data byte echoed back - rr = hw->recv(2, hdr, 1000*len); - if (rr != 2) return -1; - ///printf("ocd write 0x%02x\n", hdr[1]); - return (hdr[1] == tool78ocd_cmd_write) ? 0 : -2; + rr = hw->recv(1, hdr, 1000*len); + if (rr != 1) return -1; + ///printf("ocd write 0x%02x\n", hdr[0]); + return (hdr[0] == tool78ocd_cmd_write) ? 0 : -2; } } int tool78_ocd_exec(struct tool78_hw* hw) { @@ -1436,11 +1429,13 @@ int tool78_ocd_exec(struct tool78_hw* hw) { int rr = hw->send(1, hdr, -1); - // G13 sends status before exec, G10 sends it after - rr = hw->recv(is_g10 ? 1 : 2, hdr, 1000); - if (rr != 1 && rr != 2) return -1; - //printf("exec r=%d: h[0]=0x%02x, h[1]=0x%02x\n", rr, hdr[0], hdr[1]); + // G13 etc send status before exec, G10 sends it after + if (!is_g10) { + rr = hw->recv(1, hdr, 1000); + if (rr != 1) return -1; + //printf("exec r=%d: h[0]=0x%02x, h[1]=0x%02x\n", rr, hdr[0], hdr[1]); + } return 0;//(hdr[0] == cmd) ? 0 : -2; } int tool78_ocd_leave(struct tool78_hw* hw, bool exit_to_ram) { @@ -1459,16 +1454,11 @@ int tool78_ocd_leave(struct tool78_hw* hw, bool exit_to_ram) { int rr = hw->send(1, hdr, -1); if (is_g10) { - rr = hw->recv(2, &hdr[0], 1000); - if (rr != 2) return -1; - - return 0; - } else { - rr = hw->recv(1, &hdr[1], 1000); + rr = hw->recv(1, &hdr[0], 1000); if (rr != 1) return -1; - - return (hdr[0] == hdr[1]) ? 0 : -2; } + + return 0; } // ---- @@ -1505,7 +1495,7 @@ static enum tool78_stat tool78_init_common(struct tool78_hw* hw) { //printf("baudrate result=0x%02x\n", st); if (st != tool78_stat_ack) return st; } else { - // 0x3a/0xc5 is echoed back as well + // 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];