Debian Codegraph @

src/jtag3run.cc

raw symbols SCIP
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 target execution handling for the JTAGICE3 protocol.
19 *
20 * $Id: jtag3run.cc 382 2020-08-31 22:23:28Z joerg_wunsch $
21 */
22
23
24 #include <ctype.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <sys/stat.h>
30 #include <sys/time.h>
31 #include <termios.h>
32 #include <fcntl.h>
33 #include <string.h>
34
35 #include "avarice.h"
36 #include "jtag.h"
37 #include "jtag3.h"
38 #include "remote.h"
39
40 unsigned long jtag3::getProgramCounter(void)
41 {
42 if (cached_pc_is_valid)
43 return cached_pc;
44
45 uchar *resp;
46 int respsize;
47 uchar cmd[] = { SCOPE_AVR, CMD3_READ_PC, 0 };
48 int cnt = 0;
49
50 again:
51 try
52 {
53 doJtagCommand(cmd, sizeof cmd, "read PC", resp, respsize);
54 }
55 catch (jtag_io_exception& e)
56 {
57 cnt++;
58 if (e.get_response() == RSP3_FAIL_WRONG_MODE &&
59 cnt < 2)
60 {
61 interruptProgram();
62 goto again;
63 }
64 fprintf(stderr, "cannot read program counter: %s\n",
65 e.what());
66 throw;
67 }
68 catch (jtag_exception& e)
69 {
70 fprintf(stderr, "cannot read program counter: %s\n",
71 e.what());
72 throw;
73 }
74
75 unsigned long result = b4_to_u32(resp + 3);
76 delete [] resp;
77
78 // The JTAG box sees program memory as 16-bit wide locations. GDB
79 // sees bytes. As such, double the PC value.
80 result *= 2;
81
82 cached_pc_is_valid = true;
83 return cached_pc = result;
84 }
85
86 void jtag3::setProgramCounter(unsigned long pc)
87 {
88 uchar *resp;
89 int respsize;
90 uchar cmd[7] = { SCOPE_AVR, CMD3_WRITE_PC };
91
92 u32_to_b4(cmd + 3, pc / 2);
93
94 try
95 {
96 doJtagCommand(cmd, sizeof cmd, "write PC", resp, respsize);
97 }
98 catch (jtag_exception& e)
99 {
100 fprintf(stderr, "cannot write program counter: %s\n",
101 e.what());
102 throw;
103 }
104
105 delete [] resp;
106
107 cached_pc_is_valid = false;
108 }
109
110 PRAGMA_DIAG_PUSH
111 PRAGMA_DIAG_IGNORED("-Wunused-parameter")
112
113 void jtag3::resetProgram(bool possible_nSRST_ignored)
114 {
115 uchar cmd[] = { SCOPE_AVR, CMD3_RESET, 0, 0x01 };
116 uchar *resp;
117 int respsize;
118
119 doJtagCommand(cmd, sizeof cmd, "reset", resp, respsize);
120 delete [] resp;
121
122 /* Await the BREAK event that is posted by the ICE. */
123 bool bp, gdb;
124 expectEvent(bp, gdb);
125
126 /* The PC value in the event returned after a RESET is the
127 * PC where the reset actually hit, so ignore it. */
128 cached_pc_is_valid = false;
129 }
130
131 PRAGMA_DIAG_POP
132
133 void jtag3::interruptProgram(void)
134 {
135 uchar cmd[] = { SCOPE_AVR, CMD3_STOP, 0, 0x01 };
136 uchar *resp;
137 int respsize;
138
139 doJtagCommand(cmd, sizeof cmd, "stop", resp, respsize);
140 delete [] resp;
141
142 bool bp, gdb;
143 expectEvent(bp, gdb);
144 }
145
146 void jtag3::resumeProgram(void)
147 {
148 xmegaSendBPs();
149
150 doSimpleJtagCommand(CMD3_CLEANUP, "cleanup");
151
152 doSimpleJtagCommand(CMD3_GO, "go");
153
154 cached_pc_is_valid = false;
155 }
156
157 void jtag3::expectEvent(bool &breakpoint, bool &gdbInterrupt)
158 {
159 uchar *evtbuf;
160 int evtsize;
161 unsigned short seqno;
162
163 if (cached_event != NULL)
164 {
165 evtbuf = cached_event;
166 cached_event = NULL;
167 }
168 else
169 {
170 evtsize = recvFrame(evtbuf, seqno);
171 if (evtsize > 0) {
172 // XXX if not event, should push frame back into queue...
173 // We really need a queue of received frames.
174 if (seqno != 0xffff)
175 {
176 debugOut("Expected event packet, got other response");
177 return;
178 }
179 }
180 else
181 {
182 debugOut("Timed out waiting for an event");
183 return;
184 }
185 }
186
187 breakpoint = gdbInterrupt = false;
188
189 switch ((evtbuf[0] << 8) | evtbuf[1])
190 {
191 // Program stopped at some kind of breakpoint.
192 // On Xmega, byte 7 denotes the reason:
193 // 0x01 soft BP
194 // 0x10 hard BP (byte 8 contains BP #, or 3 for data BP)
195 // 0x20, 0x21 "run to address" or single-step
196 // 0x40 reset, leave progmode etc.
197 // On megaAVR, byte 6 , byte 7 are likely the "break status
198 // register" (MSB, LSB), see here:
199 // http://people.ece.cornell.edu/land/courses/ece4760/FinalProjects/s2009/jgs33_rrw32/Final%20Paper/index.html
200 // The bits do not fully match that description but to
201 // a good degree.
202 case (SCOPE_AVR << 8) | EVT3_BREAK:
203 if ((!is_xmega && evtbuf[7] != 0) ||
204 (is_xmega && evtbuf[7] != 0x40))
205 {
206 // program breakpoint
207 cached_pc = 2 * b4_to_u32(evtbuf + 2);
208 cached_pc_is_valid = true;
209 breakpoint = true;
210 debugOut("caching PC: 0x%04x\n", cached_pc);
211 }
212 else
213 {
214 debugOut("ignoring break event\n");
215 }
216 break;
217
218 case (SCOPE_AVR << 8) | EVT3_IDR:
219 statusOut("IDR dirty: 0x%02x\n", evtbuf[3]);
220 break;
221
222 case (SCOPE_GENERAL << 8) | EVT3_POWER:
223 if (evtbuf[3] == 0)
224 {
225 gdbInterrupt = true;
226 statusOut("\nTarget power turned off\n");
227 }
228 else
229 {
230 statusOut("\nTarget power returned\n");
231 }
232 break;
233
234 case (SCOPE_GENERAL << 8) | EVT3_SLEEP:
235 if (evtbuf[3] == 0)
236 {
237 //gdbInterrupt = true;
238 statusOut("\nTarget went to sleep\n");
239 }
240 else
241 {
242 //gdbInterrupt = true;
243 statusOut("\nTarget went out of sleep\n");
244 }
245 break;
246
247 default:
248 gdbInterrupt = true;
249 statusOut("\nUnhandled JTAGICE3 event: 0x%02x, 0x%02x\n",
250 evtbuf[0], evtbuf[1]);
251 }
252
253 delete [] evtbuf;
254 }
255
256 bool jtag3::eventLoop(void)
257 {
258 int maxfd;
259 fd_set readfds;
260 bool breakpoint = false, gdbInterrupt = false;
261
262 // Now that we are "going", wait for either a response from the JTAG
263 // box or a nudge from GDB.
264
265 for (;;)
266 {
267 debugOut("Waiting for input.\n");
268
269 // Check for input from JTAG ICE (breakpoint, sleep, info, power)
270 // or gdb (user break)
271 FD_ZERO (&readfds);
272 if (gdbFileDescriptor != -1)
273 FD_SET (gdbFileDescriptor, &readfds);
274 FD_SET (jtagBox, &readfds);
275 if (gdbFileDescriptor != -1)
276 maxfd = jtagBox > gdbFileDescriptor ? jtagBox : gdbFileDescriptor;
277 else
278 maxfd = jtagBox;
279
280 int numfds = select(maxfd + 1, &readfds, 0, 0, 0);
281 if (numfds < 0)
282 throw jtag_exception("GDB/JTAG ICE communications failure");
283
284 if (gdbFileDescriptor != -1 && FD_ISSET(gdbFileDescriptor, &readfds))
285 {
286 int c = getDebugChar();
287 if (c == 3) // interrupt
288 {
289 debugOut("interrupted by GDB\n");
290 gdbInterrupt = true;
291 }
292 else
293 debugOut("Unexpected GDB input `%02x'\n", c);
294 }
295
296 if (FD_ISSET(jtagBox, &readfds))
297 {
298 expectEvent(breakpoint, gdbInterrupt);
299 }
300
301 // We give priority to user interrupts
302 if (gdbInterrupt)
303 return false;
304 if (breakpoint)
305 return true;
306 }
307 }
308
309
310 void jtag3::jtagSingleStep(void)
311 {
312 uchar cmd[] = { SCOPE_AVR, CMD3_STEP,
313 0, 0x01, 0x01 };
314 uchar *resp;
315 int respsize;
316
317 xmegaSendBPs();
318
319 cached_pc_is_valid = false;
320
321 try
322 {
323 doJtagCommand(cmd, sizeof cmd, "single-step", resp, respsize);
324 }
325 catch (jtag_io_exception& e)
326 {
327 if (e.get_response() != RSP3_FAIL_WRONG_MODE)
328 throw;
329 }
330 delete [] resp;
331
332 bool bp, gdb;
333 expectEvent(bp, gdb);
334 }
335
336 void jtag3::parseEvents(const char *evtlist)
337 {
338 #if 0
339 memset(nonbreaking_events, 0, sizeof nonbreaking_events);
340
341 const struct
342 {
343 uchar scope, num;
344 const char *name;
345 } evttable[] =
346 {
347 { SCOPE_AVR, EVT3_BREAK, "break" },
348 { SCOPE_GENERAL, EVT3_SLEEP, "sleep" },
349 { SCOPE_GENERAL, EVT3_POWER, "power" },
350 };
351
352 // parse the given comma-separated string
353 const char *cp1, *cp2;
354 cp1 = evtlist;
355 while (*cp1 != '\0')
356 {
357 while (isspace(*cp1) || *cp1 == ',')
358 cp1++;
359 cp2 = cp1;
360 while (*cp2 != '\0' && *cp2 != ',')
361 cp2++;
362 size_t l = cp2 - cp1;
363 uchar evtval = 0;
364
365 // Now, cp1 points to the name to parse, of length l
366 for (unsigned int i = 0; i < sizeof evttable / sizeof evttable[0]; i++)
367 {
368 if (strncmp(evttable[i].name, cp1, l) == 0)
369 {
370 evtval = evttable[i].num;
371 break;
372 }
373 }
374 if (evtval == 0)
375 {
376 fprintf(stderr, "Warning: event name %.*s not matched\n",
377 (int)l, cp1);
378 }
379 else
380 {
381 nonbreaking_events[evtval - EVT_BREAK] = true;
382 }
383
384 cp1 = cp2;
385 }
386 #endif
387 }
388
389 bool jtag3::jtagContinue(void)
390 {
391 updateBreakpoints(); // download new bp configuration
392
393 xmegaSendBPs();
394
395 if (cached_event != NULL)
396 {
397 delete [] cached_event;
398 cached_event = NULL;
399 }
400
401 doSimpleJtagCommand(CMD3_GO, "go");
402
403 return eventLoop();
404 }
405