jtagmkii.c
来自「这是一个非常有价值的参考代码」· C语言 代码 · 共 1,822 行 · 第 1/4 页
C
1,822 行
if (verbose >= 3) fprintf(stderr, "%s: jtagmkII_recv(): CRC OK", progname); state = sDONE; } else { fprintf(stderr, "%s: jtagmkII_recv(): checksum error\n", progname); free(buf); return -4; } } else state++; break; default: fprintf(stderr, "%s: jtagmkII_recv(): unknown state\n", progname); free(buf); return -5; } gettimeofday(&tv, NULL); tnow = tv.tv_sec; if (tnow - tstart > timeoutval) { fprintf(stderr, "%s: jtagmkII_recv_frame(): timeout\n", progname); return -1; } } if (verbose >= 3)fprintf(stderr, "\n"); *seqno = r_seqno; *msg = buf; return msglen;}static int jtagmkII_recv(PROGRAMMER * pgm, unsigned char **msg) { unsigned short r_seqno; int rv; for (;;) { if ((rv = jtagmkII_recv_frame(pgm, msg, &r_seqno)) <= 0) return rv; if (verbose >= 3) fprintf(stderr, "%s: jtagmkII_recv(): " "Got message seqno %d (command_sequence == %d)\n", progname, r_seqno, command_sequence); if (r_seqno == command_sequence) { if (++command_sequence == 0xffff) command_sequence = 0; /* * 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. */ memmove(*msg, *msg + 8, rv); return rv; } if (r_seqno == 0xffff) { if (verbose >= 3) fprintf(stderr, "%s: jtagmkII_recv(): got asynchronous event\n", progname); } else { if (verbose >= 2) fprintf(stderr, "%s: jtagmkII_recv(): " "got wrong sequence number, %u != %u\n", progname, r_seqno, command_sequence); } free(*msg); } return 0;}static int jtagmkII_getsync(PROGRAMMER * pgm) { int tries;#define MAXTRIES 33 unsigned char buf[3], *resp, c = 0xff; int status; unsigned int fwver; if (verbose >= 3) fprintf(stderr, "%s: jtagmkII_getsync()\n", progname); for (tries = 0; tries < MAXTRIES; tries++) { /* Get the sign-on information. */ buf[0] = CMND_GET_SIGN_ON; if (verbose >= 2) fprintf(stderr, "%s: jtagmkII_getsync(): Sending sign-on command: ", progname); jtagmkII_send(pgm, buf, 1); status = jtagmkII_recv(pgm, &resp); if (status <= 0) { fprintf(stderr, "%s: jtagmkII_getsync(): sign-on command: " "status %d\n", progname, status); } else if (verbose >= 3) { putc('\n', stderr); jtagmkII_prmsg(pgm, resp, status); } else if (verbose == 2) fprintf(stderr, "0x%02x (%d bytes msg)\n", resp[0], status); if (status > 0) { if ((c = resp[0]) == RSP_SIGN_ON) { fwver = ((unsigned)resp[8] << 8) | (unsigned)resp[7]; memcpy(serno, resp + 10, 6); if (verbose >= 1 && status > 17) { fprintf(stderr, "JTAG ICE mkII sign-on message:\n"); fprintf(stderr, "Communications protocol version: %u\n", (unsigned)resp[1]); fprintf(stderr, "M_MCU:\n"); fprintf(stderr, " boot-loader FW version: %u\n", (unsigned)resp[2]); fprintf(stderr, " firmware version: %u.%02u\n", (unsigned)resp[4], (unsigned)resp[3]); fprintf(stderr, " hardware version: %u\n", (unsigned)resp[5]); fprintf(stderr, "S_MCU:\n"); fprintf(stderr, " boot-loader FW version: %u\n", (unsigned)resp[6]); fprintf(stderr, " firmware version: %u.%02u\n", (unsigned)resp[8], (unsigned)resp[7]); fprintf(stderr, " hardware version: %u\n", (unsigned)resp[9]); fprintf(stderr, "Serial number: " "%02x:%02x:%02x:%02x:%02x:%02x\n", serno[0], serno[1], serno[2], serno[3], serno[4], serno[5]); resp[status - 1] = '\0'; fprintf(stderr, "Device ID: %s\n", resp + 16); } break; } free(resp); } } if (tries >= MAXTRIES) { if (status <= 0) fprintf(stderr, "%s: jtagmkII_getsync(): " "timeout/error communicating with programmer (status %d)\n", progname, status); else fprintf(stderr, "%s: jtagmkII_getsync(): " "bad response to sign-on command: 0x%02x\n", progname, c); return -1; } device_descriptor_length = sizeof(struct device_descriptor); /* * There's no official documentation from Atmel about what firmware * revision matches what device descriptor length. The algorithm * below has been found empirically. */#define FWVER(maj, min) ((maj << 8) | (min)) if (fwver < FWVER(3, 16)) { device_descriptor_length -= 2; fprintf(stderr, "%s: jtagmkII_getsync(): " "S_MCU firmware version might be too old to work correctly\n", progname); } else if (fwver < FWVER(4, 0)) { device_descriptor_length -= 2; }#undef FWVER if (verbose >= 2) fprintf(stderr, "%s: jtagmkII_getsync(): Using a %zu-byte device descriptor\n", progname, device_descriptor_length); /* Turn the ICE into JTAG mode */ buf[0] = EMULATOR_MODE_JTAG; if (jtagmkII_setparm(pgm, PAR_EMULATOR_MODE, buf) < 0) return -1; /* GET SYNC forces the target into STOPPED mode */ buf[0] = CMND_GET_SYNC; if (verbose >= 2) fprintf(stderr, "%s: jtagmkII_getsync(): Sending get sync command: ", progname); jtagmkII_send(pgm, buf, 1); status = jtagmkII_recv(pgm, &resp); if (status <= 0) { if (verbose >= 2) putc('\n', stderr); fprintf(stderr, "%s: jtagmkII_getsync(): " "timeout/error communicating with programmer (status %d)\n", progname, status); return -1; } if (verbose >= 3) { putc('\n', stderr); jtagmkII_prmsg(pgm, resp, status); } else if (verbose == 2) fprintf(stderr, "0x%02x (%d bytes msg)\n", resp[0], status); c = resp[0]; free(resp); if (c != RSP_OK) { fprintf(stderr, "%s: jtagmkII_getsync(): " "bad response to set parameter command: 0x%02x\n", progname, c); return -1; } return 0;}static int jtagmkII_cmd(PROGRAMMER * pgm, unsigned char cmd[4], unsigned char res[4]){ fprintf(stderr, "%s: jtagmkII_command(): no direct SPI supported for JTAG\n", progname); return -1;}/* * issue the 'chip erase' command to the AVR device */static int jtagmkII_chip_erase(PROGRAMMER * pgm, AVRPART * p){ int status; unsigned char buf[1], *resp, c; buf[0] = CMND_CHIP_ERASE; if (verbose >= 2) fprintf(stderr, "%s: jtagmkII_chip_erase(): Sending chip erase command: ", progname); jtagmkII_send(pgm, buf, 1); status = jtagmkII_recv(pgm, &resp); if (status <= 0) { if (verbose >= 2) putc('\n', stderr); fprintf(stderr, "%s: jtagmkII_chip_erase(): " "timeout/error communicating with programmer (status %d)\n", progname, status); return -1; } if (verbose >= 3) { putc('\n', stderr); jtagmkII_prmsg(pgm, resp, status); } else if (verbose == 2) fprintf(stderr, "0x%02x (%d bytes msg)\n", resp[0], status); c = resp[0]; free(resp); if (c != RSP_OK) { fprintf(stderr, "%s: jtagmkII_chip_erase(): " "bad response to chip erase command: 0x%02x\n", progname, c); return -1; } pgm->initialize(pgm, p); return 0;}static void jtagmkII_set_devdescr(PROGRAMMER * pgm, AVRPART * p){ int status; unsigned char *resp, c; LNODEID ln; AVRMEM * m; struct { unsigned char cmd; struct device_descriptor dd; } sendbuf; memset(&sendbuf, 0, sizeof sendbuf); sendbuf.cmd = CMND_SET_DEVICE_DESCRIPTOR; sendbuf.dd.ucSPMCRAddress = p->spmcr; sendbuf.dd.ucRAMPZAddress = p->rampz; sendbuf.dd.ucIDRAddress = p->idr; u16_to_b2(sendbuf.dd.EECRAddress, p->eecr); sendbuf.dd.ucAllowFullPageBitstream = (p->flags & AVRPART_ALLOWFULLPAGEBITSTREAM) != 0; sendbuf.dd.EnablePageProgramming = (p->flags & AVRPART_ENABLEPAGEPROGRAMMING) != 0; for (ln = lfirst(p->mem); ln; ln = lnext(ln)) { m = ldata(ln); if (strcmp(m->desc, "flash") == 0) { flash_pagesize = m->page_size; u32_to_b4(sendbuf.dd.ulFlashSize, m->size); u16_to_b2(sendbuf.dd.uiFlashPageSize, flash_pagesize); u16_to_b2(sendbuf.dd.uiFlashpages, m->size / flash_pagesize); } else if (strcmp(m->desc, "eeprom") == 0) { sendbuf.dd.ucEepromPageSize = eeprom_pagesize = m->page_size; } } if (verbose >= 2) fprintf(stderr, "%s: jtagmkII_set_devdescr(): " "Sending set device descriptor command: ", progname); jtagmkII_send(pgm, (unsigned char *)&sendbuf, device_descriptor_length + sizeof(unsigned char)); status = jtagmkII_recv(pgm, &resp); if (status <= 0) { if (verbose >= 2) putc('\n', stderr); fprintf(stderr, "%s: jtagmkII_set_devdescr(): " "timeout/error communicating with programmer (status %d)\n", progname, status); return; } if (verbose >= 3) { putc('\n', stderr); jtagmkII_prmsg(pgm, resp, status); } else if (verbose == 2) fprintf(stderr, "0x%02x (%d bytes msg)\n", resp[0], status); c = resp[0]; free(resp); if (c != RSP_OK) { fprintf(stderr, "%s: jtagmkII_set_devdescr(): " "bad response to set device descriptor command: 0x%02x\n", progname, c); }}/* * Reset the target. */static int jtagmkII_reset(PROGRAMMER * pgm){ int status; unsigned char buf[1], *resp, c; buf[0] = CMND_RESET; if (verbose >= 2) fprintf(stderr, "%s: jtagmkII_reset(): Sending reset command: ", progname); jtagmkII_send(pgm, buf, 1); status = jtagmkII_recv(pgm, &resp); if (status <= 0) { if (verbose >= 2) putc('\n', stderr); fprintf(stderr, "%s: jtagmkII_reset(): " "timeout/error communicating with programmer (status %d)\n", progname, status); return -1; } if (verbose >= 3) { putc('\n', stderr); jtagmkII_prmsg(pgm, resp, status); } else if (verbose == 2) fprintf(stderr, "0x%02x (%d bytes msg)\n", resp[0], status); c = resp[0]; free(resp); if (c != RSP_OK) { fprintf(stderr, "%s: jtagmkII_reset(): " "bad response to reset command: 0x%02x\n", progname, c); return -1; } return 0;}static int jtagmkII_program_enable_dummy(PROGRAMMER * pgm, AVRPART * p){ return 0;}static int jtagmkII_program_enable(PROGRAMMER * pgm){ int status; unsigned char buf[1], *resp, c; if (prog_enabled) return 0; buf[0] = CMND_ENTER_PROGMODE; if (verbose >= 2) fprintf(stderr, "%s: jtagmkII_program_enable(): " "Sending enter progmode command: ", progname); jtagmkII_send(pgm, buf, 1); status = jtagmkII_recv(pgm, &resp); if (status <= 0) { if (verbose >= 2) putc('\n', stderr); fprintf(stderr, "%s: jtagmkII_program_enable(): " "timeout/error communicating with programmer (status %d)\n", progname, status); return -1; } if (verbose >= 3) { putc('\n', stderr); jtagmkII_prmsg(pgm, resp, status); } else if (verbose == 2) fprintf(stderr, "0x%02x (%d bytes msg)\n", resp[0], status); c = resp[0]; free(resp); if (c != RSP_OK) { fprintf(stderr, "%s: jtagmkII_program_enable(): " "bad response to enter progmode command: 0x%02x\n", progname, c); if (c == RSP_ILLEGAL_JTAG_ID) fprintf(stderr, "%s: JTAGEN fuse disabled?\n", progname); return -1; } prog_enabled = 1; return 0;}static int jtagmkII_program_disable(PROGRAMMER * pgm){ int status; unsigned char buf[1], *resp, c; if (!prog_enabled) return 0; buf[0] = CMND_LEAVE_PROGMODE; if (verbose >= 2) fprintf(stderr, "%s: jtagmkII_program_disable(): " "Sending leave progmode command: ", progname); jtagmkII_send(pgm, buf, 1); status = jtagmkII_recv(pgm, &resp); if (status <= 0) { if (verbose >= 2) putc('\n', stderr); fprintf(stderr, "%s: jtagmkII_program_disable(): " "timeout/error communicating with programmer (status %d)\n", progname, status); return -1; } if (verbose >= 3) { putc('\n', stderr);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?