Debian Codegraph @

src/jtag2run.cc

raw symbols SCIP
1 /*
2 * avarice - The "avarice" program.
3 * Copyright (C) 2001 Scott Finneran
4 * Copyright (C) 2002 Intel Corporation
5 * Copyright (C) 2005, 2007 Joerg Wunsch
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License Version 2
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
19 *
20 * This file implements target execution handling for the mkII protocol.
21 *
22 * $Id: jtag2run.cc 317 2012-12-19 14:04:28Z joerg_wunsch $
23 */
24
25
26 #include <ctype.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <sys/stat.h>
32 #include <sys/time.h>
33 #include <termios.h>
34 #include <fcntl.h>
35 #include <string.h>
36
37 #include "avarice.h"
38 #include "jtag.h"
39 #include "jtag2.h"
40 #include "remote.h"
41
42 unsigned long jtag2::getProgramCounter(void)
43 {
44 if (cached_pc_is_valid)
45 return cached_pc;
46
47 uchar *response;
48 int responseSize;
49 uchar command[] = { CMND_READ_PC };
50
51 try
52 {
53 doJtagCommand(command, sizeof(command), response, responseSize, true);
54 }
55 catch (jtag_exception& e)
56 {
57 fprintf(stderr, "cannot read program counter: %s\n",
58 e.what());
59 throw;
60 }
61
62 unsigned long result = b4_to_u32(response + 1);
63 delete [] response;
64
65 // The JTAG box sees program memory as 16-bit wide locations. GDB
66 // sees bytes. As such, double the PC value.
67 result *= 2;
68
69 cached_pc_is_valid = true;
70 return cached_pc = result;
71 }
72
73 void jtag2::setProgramCounter(unsigned long pc)
74 {
75 uchar *response;
76 int responseSize;
77 uchar command[5] = { CMND_WRITE_PC };
78
79 u32_to_b4(command + 1, pc / 2);
80
81 try
82 {
83 doJtagCommand(command, sizeof(command), response, responseSize);
84 }
85 catch (jtag_exception& e)
86 {
87 fprintf(stderr, "cannot write program counter: %s\n",
88 e.what());
89 throw;
90 }
91
92 delete [] response;
93
94 cached_pc_is_valid = false;
95 }
96
97 PRAGMA_DIAG_PUSH
98 PRAGMA_DIAG_IGNORED("-Wunused-parameter")
99
100 void jtag2::resetProgram(bool possible_nSRST_ignored)
101 {
102 if (proto == PROTO_DW) {
103 /* The JTAG ICE mkII and Dragon do not respond correctly to
104 * the CMND_RESET command while in debugWire mode. */
105 interruptProgram();
106 setProgramCounter(0);
107 } else {
108 uchar cmd[2] = { CMND_RESET, 0x01 };
109 uchar *resp;
110 int respSize;
111
112 doJtagCommand(cmd, 2, resp, respSize);
113 delete [] resp;
114
115 /* Await the BREAK event that is posted by the ICE. */
116 bool bp, gdb;
117 expectEvent(bp, gdb);
118 }
119 }
120
121 PRAGMA_DIAG_POP
122
123 void jtag2::interruptProgram(void)
124 {
125 uchar cmd[2] = { CMND_FORCED_STOP, 0x01 };
126 uchar *resp;
127 int respSize;
128
129 doJtagCommand(cmd, 2, resp, respSize);
130 delete [] resp;
131
132 bool bp, gdb;
133 expectEvent(bp, gdb);
134 }
135
136 void jtag2::resumeProgram(void)
137 {
138 xmegaSendBPs();
139
140 doSimpleJtagCommand(CMND_GO);
141
142 cached_pc_is_valid = false;
143 }
144
145 void jtag2::expectEvent(bool &breakpoint, bool &gdbInterrupt)
146 {
147 uchar *evtbuf;
148 int evtSize;
149 unsigned short seqno;
150
151 evtSize = recvFrame(evtbuf, seqno);
152 if (evtSize >= 0) {
153 // XXX if not event, should push frame back into queue...
154 // We really need a queue of received frames.
155 if (seqno != 0xffff)
156 debugOut("Expected event packet, got other response");
157 else if (!nonbreaking_events[evtbuf[8] - EVT_BREAK])
158 {
159 switch (evtbuf[8])
160 {
161 // Program stopped at some kind of breakpoint.
162 case EVT_BREAK:
163 cached_pc = 2 * b4_to_u32(evtbuf + 9);
164 cached_pc_is_valid = true;
165 /* FALLTHROUGH */
166 case EVT_EXT_RESET:
167 case EVT_PDSB_BREAK:
168 case EVT_PDSMB_BREAK:
169 case EVT_PROGRAM_BREAK:
170 breakpoint = true;
171 break;
172
173 case EVT_IDR_DIRTY:
174 // The program is still running at IDR dirty, so
175 // pretend a user break;
176 gdbInterrupt = true;
177 printf("\nIDR dirty: 0x%02x\n", evtbuf[9]);
178 break;
179
180 // Fatal debugWire errors, cannot continue
181 case EVT_ERROR_PHY_FORCE_BREAK_TIMEOUT:
182 case EVT_ERROR_PHY_MAX_BIT_LENGTH_DIFF:
183 case EVT_ERROR_PHY_OPT_RECEIVE_TIMEOUT:
184 case EVT_ERROR_PHY_OPT_RECEIVED_BREAK:
185 case EVT_ERROR_PHY_RECEIVED_BREAK:
186 case EVT_ERROR_PHY_RECEIVE_TIMEOUT:
187 case EVT_ERROR_PHY_RELEASE_BREAK_TIMEOUT:
188 case EVT_ERROR_PHY_SYNC_OUT_OF_RANGE:
189 case EVT_ERROR_PHY_SYNC_TIMEOUT:
190 case EVT_ERROR_PHY_SYNC_TIMEOUT_BAUD:
191 case EVT_ERROR_PHY_SYNC_WAIT_TIMEOUT:
192 gdbInterrupt = true;
193 printf("\nFatal debugWIRE communication event: 0x%02x\n",
194 evtbuf[8]);
195 break;
196
197 // Other fatal errors, user could mask them off
198 case EVT_ICE_POWER_ERROR_STATE:
199 gdbInterrupt = true;
200 printf("\nJTAG ICE mkII power failure\n");
201 break;
202
203 case EVT_TARGET_POWER_OFF:
204 gdbInterrupt = true;
205 printf("\nTarget power turned off\n");
206 break;
207
208 case EVT_TARGET_POWER_ON:
209 gdbInterrupt = true;
210 printf("\nTarget power returned\n");
211 break;
212
213 case EVT_TARGET_SLEEP:
214 gdbInterrupt = true;
215 printf("\nTarget went to sleep\n");
216 break;
217
218 case EVT_TARGET_WAKEUP:
219 gdbInterrupt = true;
220 printf("\nTarget went out of sleep\n");
221 break;
222
223 // Events where we want to continue
224 case EVT_NONE:
225 case EVT_RUN:
226 break;
227
228 default:
229 gdbInterrupt = true;
230 printf("\nUnhandled JTAG ICE mkII event: 0x%0x2\n",
231 evtbuf[8]);
232 }
233 }
234 delete [] evtbuf;
235 }
236 }
237
238 bool jtag2::eventLoop(void)
239 {
240 int maxfd;
241 fd_set readfds;
242 bool breakpoint = false, gdbInterrupt = false;
243
244 // Now that we are "going", wait for either a response from the JTAG
245 // box or a nudge from GDB.
246
247 for (;;)
248 {
249 debugOut("Waiting for input.\n");
250
251 // Check for input from JTAG ICE (breakpoint, sleep, info, power)
252 // or gdb (user break)
253 FD_ZERO (&readfds);
254 if (gdbFileDescriptor != -1)
255 FD_SET (gdbFileDescriptor, &readfds);
256 FD_SET (jtagBox, &readfds);
257 if (gdbFileDescriptor != -1)
258 maxfd = jtagBox > gdbFileDescriptor ? jtagBox : gdbFileDescriptor;
259 else
260 maxfd = jtagBox;
261
262 int numfds = select(maxfd + 1, &readfds, 0, 0, 0);
263 if (numfds < 0)
264 throw jtag_exception("GDB/JTAG ICE communications failure");
265
266 if (gdbFileDescriptor != -1 && FD_ISSET(gdbFileDescriptor, &readfds))
267 {
268 int c = getDebugChar();
269 if (c == 3) // interrupt
270 {
271 debugOut("interrupted by GDB\n");
272 gdbInterrupt = true;
273 }
274 else
275 debugOut("Unexpected GDB input `%02x'\n", c);
276 }
277
278 if (FD_ISSET(jtagBox, &readfds))
279 {
280 expectEvent(breakpoint, gdbInterrupt);
281 }
282
283 // We give priority to user interrupts
284 if (gdbInterrupt)
285 return false;
286 if (breakpoint)
287 return true;
288 }
289 }
290
291
292 void jtag2::jtagSingleStep(void)
293 {
294 uchar cmd[3] = { CMND_SINGLE_STEP,
295 0x01, 0x01 };
296 uchar *resp;
297 int respSize, i = 2;
298
299 xmegaSendBPs();
300
301 cached_pc_is_valid = false;
302
303 do
304 {
305 try
306 {
307 doJtagCommand(cmd, 3, resp, respSize);
308 }
309 catch (jtag_io_exception& e)
310 {
311 if (e.get_response() != RSP_ILLEGAL_MCU_STATE)
312 throw;
313 continue;
314 }
315 delete [] resp;
316 break;
317 }
318 while (--i >= 0);
319 if (i < 0)
320 throw jtag_exception("Single-step failed");
321
322 bool bp, gdb;
323 expectEvent(bp, gdb);
324 }
325
326 void jtag2::parseEvents(const char *evtlist)
327 {
328 memset(nonbreaking_events, 0, sizeof nonbreaking_events);
329
330 const struct
331 {
332 uchar num;
333 const char *name;
334 } evttable[] =
335 {
336 { EVT_BREAK, "break" },
337 { EVT_DEBUG, "debug" },
338 { EVT_ERROR_PHY_FORCE_BREAK_TIMEOUT, "error_phy_force_break_timeout" },
339 { EVT_ERROR_PHY_MAX_BIT_LENGTH_DIFF, "error_phy_max_bit_length_diff" },
340 { EVT_ERROR_PHY_OPT_RECEIVE_TIMEOUT, "error_phy_opt_receive_timeout" },
341 { EVT_ERROR_PHY_OPT_RECEIVED_BREAK, "error_phy_opt_received_break" },
342 { EVT_ERROR_PHY_RECEIVED_BREAK, "error_phy_received_break" },
343 { EVT_ERROR_PHY_RECEIVE_TIMEOUT, "error_phy_receive_timeout" },
344 { EVT_ERROR_PHY_RELEASE_BREAK_TIMEOUT, "error_phy_release_break_timeout" },
345 { EVT_ERROR_PHY_SYNC_OUT_OF_RANGE, "error_phy_sync_out_of_range" },
346 { EVT_ERROR_PHY_SYNC_TIMEOUT, "error_phy_sync_timeout" },
347 { EVT_ERROR_PHY_SYNC_TIMEOUT_BAUD, "error_phy_sync_timeout_baud" },
348 { EVT_ERROR_PHY_SYNC_WAIT_TIMEOUT, "error_phy_sync_wait_timeout" },
349 { EVT_RESULT_PHY_NO_ACTIVITY, "result_phy_no_activity" },
350 { EVT_EXT_RESET, "ext_reset" },
351 { EVT_ICE_POWER_ERROR_STATE, "ice_power_error_state" },
352 { EVT_ICE_POWER_OK, "ice_power_ok" },
353 { EVT_IDR_DIRTY, "idr_dirty" },
354 { EVT_NONE, "none" },
355 { EVT_PDSB_BREAK, "pdsb_break" },
356 { EVT_PDSMB_BREAK, "pdsmb_break" },
357 { EVT_PROGRAM_BREAK, "program_break" },
358 { EVT_RUN, "run" },
359 { EVT_TARGET_POWER_OFF, "target_power_off" },
360 { EVT_TARGET_POWER_ON, "target_power_on" },
361 { EVT_TARGET_SLEEP, "target_sleep" },
362 { EVT_TARGET_WAKEUP, "target_wakeup" },
363 };
364
365 // parse the given comma-separated string
366 const char *cp1, *cp2;
367 cp1 = evtlist;
368 while (*cp1 != '\0')
369 {
370 while (isspace(*cp1) || *cp1 == ',')
371 cp1++;
372 cp2 = cp1;
373 while (*cp2 != '\0' && *cp2 != ',')
374 cp2++;
375 size_t l = cp2 - cp1;
376 uchar evtval = 0;
377
378 // Now, cp1 points to the name to parse, of length l
379 for (unsigned int i = 0; i < sizeof evttable / sizeof evttable[0]; i++)
380 {
381 if (strncmp(evttable[i].name, cp1, l) == 0)
382 {
383 evtval = evttable[i].num;
384 break;
385 }
386 }
387 if (evtval == 0)
388 {
389 fprintf(stderr, "Warning: event name %.*s not matched\n",
390 (int)l, cp1);
391 }
392 else
393 {
394 nonbreaking_events[evtval - EVT_BREAK] = true;
395 }
396
397 cp1 = cp2;
398 }
399 }
400
401 bool jtag2::jtagContinue(void)
402 {
403 updateBreakpoints(); // download new bp configuration
404
405 xmegaSendBPs();
406
407 doSimpleJtagCommand(CMND_GO);
408
409 return eventLoop();
410 }
411