| 1 | /* |
| 2 | * avarice - The "avarice" program. |
| 3 | * Copyright (C) 2012 Joerg Wunsch |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License Version 2 |
| 7 | * as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
| 17 | * |
| 18 | * This file implements the target memory programming interface for |
| 19 | * the JTAGICE3 protocol. |
| 20 | * |
| 21 | * $Id: jtag3prog.cc 390 2020-09-17 20:34:33Z joerg_wunsch $ |
| 22 | */ |
| 23 | |
| 24 | |
| 25 | #include <stdarg.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <unistd.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <termios.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <string.h> |
| 33 | #include <math.h> |
| 34 | |
| 35 | #include "avarice.h" |
| 36 | #include "jtag.h" |
| 37 | #include "jtag3.h" |
| 38 | |
| 39 | |
| 40 | void jtag3::enableProgramming(void) |
| 41 | { |
| 42 | if (proto != PROTO_DW) |
| 43 | { |
| 44 | programmingEnabled = true; |
| 45 | doSimpleJtagCommand(CMD3_ENTER_PROGMODE, "enter progmode"); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | |
| 50 | void jtag3::disableProgramming(void) |
| 51 | { |
| 52 | if (proto != PROTO_DW) |
| 53 | { |
| 54 | programmingEnabled = false; |
| 55 | doSimpleJtagCommand(CMD3_LEAVE_PROGMODE, "leave progmode"); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | |
| 60 | // This is really a chip-erase which erases flash, lock-bits and eeprom |
| 61 | // (unless the save-eeprom fuse is set). |
| 62 | void jtag3::eraseProgramMemory(void) |
| 63 | { |
| 64 | if (proto == PROTO_DW) |
| 65 | // debugWIRE auto-erases when programming |
| 66 | return; |
| 67 | |
| 68 | uchar *resp; |
| 69 | int respsize; |
| 70 | uchar buf[8]; |
| 71 | |
| 72 | buf[0] = SCOPE_AVR; |
| 73 | buf[1] = CMD3_ERASE_MEMORY; |
| 74 | buf[2] = 0; |
| 75 | buf[3] = XMEGA_ERASE_CHIP; |
| 76 | buf[4] = buf[5] = buf[6] = buf[7] = 0; /* page address */ |
| 77 | |
| 78 | doJtagCommand(buf, 8, "chip erase", resp, respsize); |
| 79 | |
| 80 | delete [] resp; |
| 81 | } |
| 82 | |
| 83 | void jtag3::eraseProgramPage(unsigned long address) |
| 84 | { |
| 85 | uchar *resp; |
| 86 | int respsize; |
| 87 | uchar buf[8]; |
| 88 | |
| 89 | buf[0] = SCOPE_AVR; |
| 90 | buf[1] = CMD3_ERASE_MEMORY; |
| 91 | buf[2] = 0; |
| 92 | if (is_xmega && address >= appsize) |
| 93 | { |
| 94 | buf[3] = XMEGA_ERASE_BOOT_PAGE; |
| 95 | address -= appsize; |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | buf[3] = XMEGA_ERASE_APP_PAGE; |
| 100 | } |
| 101 | u32_to_b4(buf + 4, address); |
| 102 | |
| 103 | doJtagCommand(buf, 8, "page erase", resp, respsize); |
| 104 | |
| 105 | delete [] resp; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | void jtag3::downloadToTarget(const char* filename __attribute__((unused)), |
| 110 | bool program __attribute__((unused)), |
| 111 | bool verify __attribute__((unused))) |
| 112 | { |
| 113 | statusOut("\nDownload not done.\n"); |
| 114 | throw jtag_exception("Target programming not implemented for JTAGICE3"); |
| 115 | } |