| 1 | /* |
| 2 | * avarice - The "avarice" program. |
| 3 | * Copyright (C) 2012 Joerg Wunsch |
| 4 | * |
| 5 | * Heavily based on jtag2bp.cc, which is |
| 6 | * Copyright (C) 2007, Colin O'Flynn |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License Version 2 |
| 10 | * as published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
| 20 | * |
| 21 | * This file contains the breakpoint handling for the JTAGICE3 protocol. |
| 22 | * |
| 23 | * $Id: jtag3bp.cc 343 2013-06-29 19:37:16Z joerg_wunsch $ |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | #include <stdarg.h> |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <unistd.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include <termios.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <string.h> |
| 35 | |
| 36 | #include "avarice.h" |
| 37 | #include "jtag.h" |
| 38 | #include "jtag3.h" |
| 39 | |
| 40 | bool jtag3::codeBreakpointAt(unsigned int address) |
| 41 | { |
| 42 | int i; |
| 43 | |
| 44 | i = 0; |
| 45 | while (!bp[i].last) |
| 46 | { |
| 47 | if ((bp[i].address == address) && (bp[i].type == CODE) && bp[i].enabled) |
| 48 | return true; |
| 49 | |
| 50 | i++; |
| 51 | } |
| 52 | |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | void jtag3::deleteAllBreakpoints(void) |
| 57 | { |
| 58 | int i = 0; |
| 59 | |
| 60 | while (!bp[i].last) |
| 61 | { |
| 62 | if (bp[i].enabled && bp[i].icestatus) |
| 63 | bp[i].toremove = true; |
| 64 | |
| 65 | bp[i].enabled = false; |
| 66 | i++; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | |
| 71 | void jtag3::updateBreakpoints(void) |
| 72 | { |
| 73 | int bp_i; |
| 74 | |
| 75 | layoutBreakpoints(); |
| 76 | |
| 77 | // Delete all the breakpoints that were flagged first |
| 78 | bp_i = 0; |
| 79 | while (!bp[bp_i].last) |
| 80 | { |
| 81 | if (bp[bp_i].toremove) |
| 82 | { |
| 83 | debugOut("Breakpoint deleted in ICE. slot: %d type: %d addr: 0x%x\n", |
| 84 | bp[bp_i].bpnum, bp[bp_i].type, bp[bp_i].address); |
| 85 | |
| 86 | if (is_xmega && |
| 87 | bp[bp_i].type == CODE && bp[bp_i].bpnum != 0x00) |
| 88 | { |
| 89 | // no action needed on this one, has been auto-removed |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | uchar cmd[7]; |
| 94 | int cmdlen; |
| 95 | |
| 96 | cmd[0] = SCOPE_AVR; |
| 97 | cmd[2] = 0; |
| 98 | |
| 99 | if (bp[bp_i].bpnum == 0x00) |
| 100 | { |
| 101 | // Software BP, address must be given |
| 102 | cmd[1] = CMD3_CLEAR_SOFT_BP; |
| 103 | u32_to_b4(cmd + 3, bp[bp_i].address ); |
| 104 | cmdlen = 7; |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | // Hardware BP, only BP number needed |
| 109 | cmd[1] = CMD3_CLEAR_BP; |
| 110 | cmd[3] = bp[bp_i].bpnum; |
| 111 | cmdlen = 4; |
| 112 | } |
| 113 | |
| 114 | uchar *resp; |
| 115 | int respsize; |
| 116 | try |
| 117 | { |
| 118 | doJtagCommand(cmd, cmdlen, "clear BP", resp, respsize); |
| 119 | } |
| 120 | catch (jtag_exception& e) |
| 121 | { |
| 122 | fprintf(stderr, "Failed to clear breakpoint: %s\n", |
| 123 | e.what()); |
| 124 | throw; |
| 125 | } |
| 126 | |
| 127 | delete [] resp; |
| 128 | } |
| 129 | |
| 130 | // rip breakpoint |
| 131 | bp[bp_i].icestatus = false; |
| 132 | bp[bp_i].toremove = false; |
| 133 | } |
| 134 | |
| 135 | bp_i++; |
| 136 | } |
| 137 | |
| 138 | // Add all the new breakpoints |
| 139 | bp_i = 0; |
| 140 | while (!bp[bp_i].last) |
| 141 | { |
| 142 | if (bp[bp_i].toadd && bp[bp_i].enabled) |
| 143 | { |
| 144 | debugOut("Breakpoint added in ICE. slot: %d type: %d addr: 0x%x\n", |
| 145 | bp[bp_i].bpnum, bp[bp_i].type, bp[bp_i].address); |
| 146 | |
| 147 | if (is_xmega && |
| 148 | bp[bp_i].type == CODE && bp[bp_i].bpnum != 0x00) |
| 149 | { |
| 150 | if (xmega_n_bps >= 2) |
| 151 | throw jtag_exception("Too many hard BPs for Xmega"); |
| 152 | // Xmega code breakpoint |
| 153 | xmega_bps[xmega_n_bps++] = bp[bp_i].address; |
| 154 | // these breakpoints are auto-removed by the ICE |
| 155 | bp[bp_i].toremove = true; |
| 156 | bp[bp_i].icestatus = false; |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | uchar cmd[10]; |
| 161 | int cmdlen; |
| 162 | |
| 163 | cmd[0] = SCOPE_AVR; |
| 164 | cmd[2] = 0; |
| 165 | |
| 166 | if (bp[bp_i].bpnum != 0) |
| 167 | { |
| 168 | // Hardware BP |
| 169 | cmd[1] = CMD3_SET_BP; |
| 170 | cmd[4] = bp[bp_i].bpnum; |
| 171 | cmdlen = 10; |
| 172 | |
| 173 | // JTAGICE3 handles all BP addresses (including CODE) as |
| 174 | // byte addresses |
| 175 | if (bp[bp_i].type == DATA_MASK || |
| 176 | bp[bp_i].type == CODE) |
| 177 | u32_to_b4(cmd + 5, bp[bp_i].address); |
| 178 | else |
| 179 | u32_to_b4(cmd + 5, bp[bp_i].address & ~ADDR_SPACE_MASK); |
| 180 | |
| 181 | // cmd[9] is the BP mode (memory read/write/read or write/code) |
| 182 | // cmd[3] is the BP type (program memory, data, data mask) |
| 183 | switch (bp[bp_i].type) |
| 184 | { |
| 185 | case READ_DATA: |
| 186 | cmd[9] = 0x00; |
| 187 | cmd[3] = 0x02; |
| 188 | break; |
| 189 | case WRITE_DATA: |
| 190 | cmd[9] = 0x01; |
| 191 | cmd[3] = 0x02; |
| 192 | break; |
| 193 | case ACCESS_DATA: |
| 194 | cmd[9] = 0x02; |
| 195 | cmd[3] = 0x02; |
| 196 | break; |
| 197 | case DATA_MASK: |
| 198 | cmd[9] = 0x00; |
| 199 | cmd[3] = 0x03; |
| 200 | break; |
| 201 | case CODE: |
| 202 | cmd[9] = 0x03; |
| 203 | cmd[3] = 0x01; |
| 204 | break; |
| 205 | default: |
| 206 | throw jtag_exception("Invalid bp mode (for data bp)"); |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | else |
| 211 | { |
| 212 | // Software BP |
| 213 | cmd[1] = CMD3_SET_SOFT_BP; |
| 214 | cmdlen = 7; |
| 215 | |
| 216 | u32_to_b4(cmd + 3, bp[bp_i].address & ~ADDR_SPACE_MASK); |
| 217 | } |
| 218 | |
| 219 | uchar *resp; |
| 220 | int respsize; |
| 221 | try |
| 222 | { |
| 223 | doJtagCommand(cmd, cmdlen, "set BP", resp, respsize); |
| 224 | } |
| 225 | catch (jtag_exception& e) |
| 226 | { |
| 227 | fprintf(stderr, "Failed to set breakpoint: %s\n", |
| 228 | e.what()); |
| 229 | throw; |
| 230 | } |
| 231 | delete [] resp; |
| 232 | |
| 233 | bp[bp_i].icestatus = true; |
| 234 | } |
| 235 | |
| 236 | // It's a beautiful baby breakpoint |
| 237 | bp[bp_i].toadd = false; |
| 238 | } |
| 239 | |
| 240 | bp_i++; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | void jtag3::xmegaSendBPs(void) |
| 245 | { |
| 246 | if (!is_xmega) |
| 247 | return; |
| 248 | |
| 249 | uchar *resp; |
| 250 | int respsize; |
| 251 | uchar cmd[16] = { SCOPE_AVR, CMD3_SET_BP_XMEGA }; |
| 252 | if (xmega_n_bps > 0) |
| 253 | u32_to_b4(cmd + 3, (xmega_bps[0] / 2)); |
| 254 | if (xmega_n_bps > 1) |
| 255 | u32_to_b4(cmd + 7, (xmega_bps[1] / 2)); |
| 256 | cmd[14] = xmega_n_bps; |
| 257 | |
| 258 | /* |
| 259 | * Apparently (tracing IAR), Xmega data breakpoints are supposed to |
| 260 | * work like this: |
| 261 | * fill cmd[3:6] and cmd[7:10] with start and end address to |
| 262 | * watch for, respectively |
| 263 | * set cmd[14] to 2 (2 addresses present; only 1 for single-byte BP) |
| 264 | * set cmd[13] to 0xf2 for a range BP, 0xc2 if only single-byte BP |
| 265 | */ |
| 266 | try |
| 267 | { |
| 268 | doJtagCommand(cmd, 16, "set BP Xmega", resp, respsize); |
| 269 | } |
| 270 | catch (jtag_exception& e) |
| 271 | { |
| 272 | fprintf(stderr, "Failed to set Xmega breakpoints: %s\n", |
| 273 | e.what()); |
| 274 | throw; |
| 275 | } |
| 276 | delete [] resp; |
| 277 | |
| 278 | xmega_n_bps = 0; // must be set again upon next run |
| 279 | } |