Debian Codegraph @

src/jtag2io.cc

raw symbols SCIP
1 /*
2 * avarice - The "avarice" program.
3 * Copyright (C) 2001 Scott Finneran
4 * Copyright (C) 2002, 2003, 2004 Intel Corporation
5 * Copyright (C) 2005, 2006, 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 the basic IO handling for the mkII protocol.
21 *
22 * $Id: jtag2io.cc 391 2021-08-23 20:57:14Z joerg_wunsch $
23 */
24
25
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31 #include <sys/time.h>
32 #include <termios.h>
33 #include <fcntl.h>
34 #include <signal.h>
35 #include <string.h>
36 #include <errno.h>
37
38 #include "avarice.h"
39 #include "crc16.h"
40 #include "jtag.h"
41 #include "jtag2.h"
42 #include "jtag2_defs.h"
43
44 jtag_io_exception::jtag_io_exception(unsigned int code)
45 {
46 static char buffer[50];
47 response_code = code;
48
49 switch (code)
50 {
51 case RSP_DEBUGWIRE_SYNC_FAILED:
52 reason = "DEBUGWIRE SYNC FAILED"; break;
53 case RSP_FAILED:
54 reason = "FAILED"; break;
55 case RSP_GET_BREAK:
56 reason = "GET BREAK"; break;
57 case RSP_ILLEGAL_BREAKPOINT:
58 reason = "ILLEGAL BREAKPOINT"; break;
59 case RSP_ILLEGAL_COMMAND:
60 reason = "ILLEGAL COMMAND"; break;
61 case RSP_ILLEGAL_EMULATOR_MODE:
62 reason = "ILLEGAL EMULATOR MODE"; break;
63 case RSP_ILLEGAL_JTAG_ID:
64 reason = "ILLEGAL JTAG ID"; break;
65 case RSP_ILLEGAL_MCU_STATE:
66 reason = "ILLEGAL MCU STATE"; break;
67 case RSP_ILLEGAL_MEMORY_TYPE:
68 reason = "ILLEGAL MEMORY TYPE"; break;
69 case RSP_ILLEGAL_MEMORY_RANGE:
70 reason = "ILLEGAL MEMORY RANGE"; break;
71 case RSP_ILLEGAL_PARAMETER:
72 reason = "ILLEGAL PARAMETER"; break;
73 case RSP_ILLEGAL_POWER_STATE:
74 reason = "ILLEGAL POWER STATE"; break;
75 case RSP_ILLEGAL_VALUE:
76 reason = "ILLEGAL VALUE"; break;
77 case RSP_NO_TARGET_POWER:
78 reason = "NO TARGET POWER"; break;
79 case RSP_SET_N_PARAMETERS:
80 reason = "SET N PARAMETERS"; break;
81 default:
82 snprintf(buffer, sizeof buffer, "Unknown response code 0x%0x", code);
83 reason = buffer;
84 }
85 }
86
87
88 jtag2::~jtag2(void)
89 {
90 // Terminate connection to JTAG box.
91 if (signedIn)
92 {
93 try
94 {
95 if (debug_active)
96 doSimpleJtagCommand(CMND_RESTORE_TARGET);
97 }
98 catch (jtag_exception&)
99 {
100 // just proceed with the sign-off
101 }
102 doSimpleJtagCommand(CMND_SIGN_OFF);
103 signedIn = false;
104 }
105 }
106
107
108 /*
109 * Send one frame. Adds the required preamble and CRC, and ensures
110 * the frame could be written correctly.
111 */
112 void jtag2::sendFrame(uchar *command, int commandSize)
113 {
114 unsigned char *buf = new unsigned char[commandSize + 10];
115
116 buf[0] = MESSAGE_START;
117 u16_to_b2(buf + 1, command_sequence);
118 u32_to_b4(buf + 3, commandSize);
119 buf[7] = TOKEN;
120 memcpy(buf + 8, command, commandSize);
121
122 crcappend(buf, commandSize + 8);
123
124 int count = safewrite(buf, commandSize + 10);
125
126 delete [] buf;
127
128 if (count < 0)
129 throw jtag_exception();
130 else if (count != commandSize + 10)
131 // this shouldn't happen
132 throw jtag_exception("Invalid write size");
133 }
134
135 /*
136 * Receive one frame, return it in &msg. Received sequence number is
137 * returned in &seqno. Any valid frame will be returned, regardless
138 * whether it matches the expected sequence number, including event
139 * notification frames (seqno == 0xffff).
140 *
141 * Caller must eventually free the buffer.
142 */
143 int jtag2::recvFrame(unsigned char *&msg, unsigned short &seqno)
144 {
145 enum states {
146 sSTART, sSEQNUM1, sSEQNUM2, sSIZE1, sSIZE2, sSIZE3, sSIZE4,
147 sTOKEN, sDATA, sCSUM1, sCSUM2, sDONE
148 } state = sSTART;
149 unsigned int msglen = 0, l = 0;
150 unsigned int headeridx = 0;
151 bool ignorpkt = false;
152 int rv;
153 unsigned char c, *buf = NULL, header[8];
154 unsigned short r_seqno = 0;
155 unsigned short checksum = 0;
156
157 msg = NULL;
158
159 while (state != sDONE) {
160 if (state == sDATA) {
161 debugOut("sDATA: reading %d bytes\n", msglen);
162 rv = 0;
163 if (ignorpkt) {
164 /* skip packet's contents */
165 for(l = 0; l < msglen; l++) {
166 rv += timeout_read(&c, 1, JTAG_RESPONSE_TIMEOUT);
167 debugOut("ign: 0x%02x\n", c);
168 }
169 } else {
170 rv += timeout_read(buf + 8, msglen, JTAG_RESPONSE_TIMEOUT);
171 debugOut("read: ");
172 for (l = 0; l < msglen; l++) {
173 debugOut(" %02x", buf[l + 8]);
174 }
175 debugOut("\n");
176 }
177 if (rv == 0)
178 /* timeout */
179 break;
180 } else {
181 rv = timeout_read(&c, 1, JTAG_RESPONSE_TIMEOUT);
182 if (rv == 0) {
183 /* timeout */
184 debugOut("recv: timeout\n");
185 break;
186 }
187 debugOut("recv: 0x%02x\n", c);
188 }
189 checksum ^= c;
190
191 if (state < sDATA)
192 header[headeridx++] = c;
193
194 switch (state) {
195 case sSTART:
196 if (c == MESSAGE_START) {
197 state = sSEQNUM1;
198 } else {
199 headeridx = 0;
200 }
201 break;
202 case sSEQNUM1:
203 r_seqno = c;
204 state = sSEQNUM2;
205 break;
206 case sSEQNUM2:
207 r_seqno |= ((unsigned)c << 8);
208 state = sSIZE1;
209 break;
210 case sSIZE1:
211 state = sSIZE2;
212 goto domsglen;
213 case sSIZE2:
214 state = sSIZE3;
215 goto domsglen;
216 case sSIZE3:
217 state = sSIZE4;
218 goto domsglen;
219 case sSIZE4:
220 state = sTOKEN;
221 domsglen:
222 msglen >>= 8;
223 msglen |= ((unsigned)c << 24);
224 break;
225 case sTOKEN:
226 if (c == TOKEN) {
227 state = sDATA;
228 if (msglen > MAX_MESSAGE) {
229 printf("msglen %u exceeds max message size %u, ignoring message\n",
230 msglen, MAX_MESSAGE);
231 state = sSTART;
232 headeridx = 0;
233 } else {
234 buf = new unsigned char[msglen + 10];
235 memcpy(buf, header, 8);
236 }
237 } else {
238 state = sSTART;
239 headeridx = 0;
240 }
241 break;
242 case sDATA:
243 /* The entire payload has been read above. */
244 l = msglen + 8;
245 state = sCSUM1;
246 break;
247 case sCSUM1:
248 buf[l++] = c;
249 state = sCSUM2;
250 break;
251 case sCSUM2:
252 buf[l++] = c;
253 if (crcverify(buf, msglen + 10)) {
254 debugOut("CRC OK");
255 state = sDONE;
256 } else {
257 debugOut("checksum error");
258 delete [] buf;
259 return -1;
260 }
261 break;
262 default:
263 debugOut("unknown state");
264 delete [] buf;
265 return -1;
266 }
267 }
268
269 seqno = r_seqno;
270 msg = buf;
271
272 return (int)msglen;
273 }
274
275 /*
276 * Try receiving frames, until we get the reply we are expecting.
277 * Caller must delete[] the msg after processing it.
278 */
279 int jtag2::recv(uchar *&msg)
280 {
281 unsigned short r_seqno;
282 int rv;
283
284 for (;;) {
285 if ((rv = recvFrame(msg, r_seqno)) <= 0)
286 return rv;
287 debugOut("\nGot message seqno %d (command_sequence == %d)\n",
288 r_seqno, command_sequence);
289 if (r_seqno == command_sequence) {
290 if (++command_sequence == 0xffff)
291 command_sequence = 0;
292 /*
293 * We move the payload to the beginning of the buffer, to make
294 * the job easier for the caller. We have to return the
295 * original pointer though, as the caller must free() it.
296 */
297 memmove(msg, msg + 8, rv);
298 return rv;
299 }
300 if (r_seqno == 0xffff) {
301 debugOut("\ngot asynchronous event: 0x%02x\n",
302 msg[8]);
303 // XXX should we queue that event up somewhere?
304 // How to process it? Register event handlers
305 // for interesting events?
306 // For now, the only place that cares is jtagContinue
307 // and it just calls recvFrame and handles events directly.
308 } else {
309 debugOut("\ngot wrong sequence number, %u != %u\n",
310 r_seqno, command_sequence);
311 }
312 delete [] msg;
313 }
314 }
315
316 /** Send a command to the jtag, and check result.
317
318 Increase *tries, abort if reaches MAX_JTAG_COMM_ATTEMPS
319
320 Reads first response byte. If no response is received within
321 JTAG_RESPONSE_TIMEOUT, returns false. If response is
322 positive returns true, otherwise returns false.
323
324 If response is positive, message (including response code) is
325 returned in &msg, caller must delete [] it. The message size is
326 returned in &msgsize.
327 **/
328
329 bool jtag2::sendJtagCommand(uchar *command, int commandSize, int &tries,
330 uchar *&msg, int &msgsize, bool verify)
331 {
332 if (tries++ >= MAX_JTAG_COMM_ATTEMPS)
333 throw jtag_exception("JTAG communication failed");
334
335 debugOut("\ncommand[0x%02x, %d]: ", command[0], tries);
336
337 for (int i = 0; i < commandSize; i++)
338 debugOut("%.2X ", command[i]);
339
340 debugOut("\n");
341
342 sendFrame(command, commandSize);
343
344 msgsize = recv(msg);
345 if (verify && msgsize == 0)
346 throw jtag_exception("no response received");
347 else if (msgsize < 1)
348 return false;
349
350 debugOut("response: ");
351 for (int i = 0; i < msgsize; i++)
352 {
353 debugOut("%.2X ", msg[i]);
354 }
355 debugOut("\n");
356
357 unsigned char c = msg[0];
358
359 if (c >= RSP_OK && c < RSP_FAILED)
360 return true;
361
362 return false;
363 }
364
365
366 void jtag2::doJtagCommand(uchar *command, int commandSize,
367 uchar *&response, int &responseSize,
368 bool retryOnTimeout)
369 {
370 int sizeseen = 0;
371 uchar code = 0;
372
373 for (int tryCount = 0; tryCount < 8; tryCount++)
374 {
375 if (sendJtagCommand(command, commandSize, tryCount, response, responseSize, false))
376 return;
377
378 if (!retryOnTimeout)
379 {
380 if (responseSize == 0)
381 throw jtag_timeout_exception();
382 else
383 throw jtag_io_exception(response[0]);
384 }
385
386 if (responseSize > 0 && response[0] > RSP_FAILED)
387 // no point in retrying failures other than FAILED
388 throw jtag_io_exception(response[0]);
389
390 if (responseSize > 0)
391 {
392 sizeseen = responseSize;
393 code = response[0];
394 }
395
396 #ifdef HAVE_LIBUSB
397 if (tryCount == 4 && responseSize == 0 && is_usb)
398 {
399 /* signal the USB daemon to reset the EPs */
400 debugOut("Resetting EPs...\n");
401 resetUSB();
402 }
403 #endif
404 }
405 if (sizeseen > 0)
406 throw jtag_io_exception(code);
407 else
408 throw jtag_timeout_exception();
409 }
410
411 void jtag2::doSimpleJtagCommand(uchar command)
412 {
413 int tryCount = 0, dummy;
414 uchar *replydummy;
415
416 // Send command until we get an OK response
417 for (;;)
418 {
419 if (sendJtagCommand(&command, 1, tryCount, replydummy, dummy, false)) {
420 if (replydummy == NULL)
421 throw jtag_io_exception();
422 if (dummy != 1)
423 throw jtag_exception("Unexpected response size in doSimpleJtagCommand");
424 if (replydummy[0] != RSP_OK)
425 throw jtag_io_exception(replydummy[0]);
426 delete [] replydummy;
427 return;
428 }
429 }
430 }
431
432 /** Set PC and JTAG ICE bitrate to BIT_RATE_xxx specified by 'newBitRate' **/
433 void jtag2::changeBitRate(int newBitRate)
434 {
435 // Don't try to change the speed of an USB connection.
436 // For the AVR Dragon, that would even result in the parameter
437 // change below being rejected.
438 if (is_usb)
439 return;
440
441 uchar jtagrate;
442
443 switch (newBitRate) {
444 case 9600:
445 jtagrate = PAR_BAUD_9600;
446 break;
447 case 19200:
448 jtagrate = PAR_BAUD_19200;
449 break;
450 case 38400:
451 jtagrate = PAR_BAUD_38400;
452 break;
453 case 57600:
454 jtagrate = PAR_BAUD_57600;
455 break;
456 case 115200:
457 jtagrate = PAR_BAUD_115200;
458 break;
459 }
460 setJtagParameter(PAR_BAUD_RATE, &jtagrate, 1);
461 changeLocalBitRate(newBitRate);
462 }
463
464 /** Set the JTAG ICE device descriptor data for specified device type **/
465 void jtag2::setDeviceDescriptor(jtag_device_def_type *dev)
466 {
467 uchar *response;
468 uchar *command;
469 int respSize;
470
471 if (is_xmega && has_full_xmega_support)
472 command = (uchar *)&dev->dev_desc3;
473 else
474 command = (uchar *)&dev->dev_desc2;
475
476 try
477 {
478 doJtagCommand(command, devdescrlen, response, respSize);
479 }
480 catch (jtag_exception& e)
481 {
482 fprintf(stderr, "JTAG ICE: Failed to set device description: %s\n",
483 e.what());
484 throw;
485 }
486 }
487
488 /** Attempt to synchronise with JTAG at specified bitrate **/
489 bool jtag2::synchroniseAt(int bitrate)
490 {
491 debugOut("Attempting synchronisation at bitrate %d\n", bitrate);
492
493 changeLocalBitRate(bitrate);
494
495 int tries = 0;
496 uchar *signonmsg, signoncmd = CMND_GET_SIGN_ON;
497 int msgsize;
498
499 while (tries < MAX_JTAG_SYNC_ATTEMPS)
500 {
501 if (sendJtagCommand(&signoncmd, 1, tries, signonmsg, msgsize, false)) {
502 if (signonmsg[0] != RSP_SIGN_ON || msgsize <= 17)
503 throw jtag_exception("Unexpected response to sign-on command");
504 signonmsg[msgsize - 1] = '\0';
505 statusOut("Found a device: %s\n", signonmsg + 16);
506 statusOut("Serial number: %02x:%02x:%02x:%02x:%02x:%02x\n",
507 signonmsg[10], signonmsg[11], signonmsg[12],
508 signonmsg[13], signonmsg[14], signonmsg[15]);
509 debugOut("JTAG ICE mkII sign-on message:\n");
510 debugOut("Communications protocol version: %u\n",
511 (unsigned)signonmsg[1]);
512 debugOut("M_MCU:\n");
513 debugOut(" boot-loader FW version: %u\n",
514 (unsigned)signonmsg[2]);
515 debugOut(" firmware version: %u.%02u\n",
516 (unsigned)signonmsg[4], (unsigned)signonmsg[3]);
517 debugOut(" hardware version: %u\n",
518 (unsigned)signonmsg[5]);
519 debugOut("S_MCU:\n");
520 debugOut(" boot-loader FW version: %u\n",
521 (unsigned)signonmsg[6]);
522 debugOut(" firmware version: %u.%02u\n",
523 (unsigned)signonmsg[8], (unsigned)signonmsg[7]);
524 debugOut(" hardware version: %u\n",
525 (unsigned)signonmsg[9]);
526
527 // The AVR Dragon always uses the full device descriptor.
528 if (emu_type == EMULATOR_JTAGICE)
529 {
530 unsigned short fwver = ((unsigned)signonmsg[8] << 8) | (unsigned)signonmsg[7];
531
532 // Check the S_MCU firmware version to know which format
533 // of the device descriptor to send.
534 #define FWVER(maj, min) ((maj << 8) | (min))
535 if (fwver < FWVER(3, 16)) {
536 devdescrlen -= 2;
537 fprintf(stderr,
538 "Warning: S_MCU firmware version might be "
539 "too old to work correctly\n ");
540 } else if (fwver < FWVER(4, 0)) {
541 devdescrlen -= 2;
542 }
543 #undef FWVER
544 }
545
546 has_full_xmega_support = (unsigned)signonmsg[8] >= 7;
547 if (is_xmega)
548 {
549 if (has_full_xmega_support)
550 {
551 devdescrlen = sizeof(xmega_device_desc_type);
552 }
553 else
554 {
555 fprintf(stderr,
556 "Warning, S_MCU firmware version (%u.%02u) too old to work "
557 "correctly for Xmega devices, >= 7.x required\n",
558 (unsigned)signonmsg[8], (unsigned)signonmsg[7]);
559 softbp_only = true;
560 }
561 }
562
563 delete [] signonmsg;
564 return true;
565 }
566 }
567 return false;
568 }
569
570 /** Attempt to synchronise with JTAG ICE at all possible bit rates **/
571 void jtag2::startJtagLink(void)
572 {
573 static int bitrates[] =
574 { 19200, 115200, 57600, 38400, 9600 };
575
576 for (unsigned int i = 0; i < sizeof bitrates / sizeof *bitrates; i++)
577 if (synchroniseAt(bitrates[i])) {
578 uchar val;
579
580 signedIn = true;
581
582 if (proto == PROTO_JTAG && apply_nSRST) {
583 val = 0x01;
584 setJtagParameter(PAR_EXTERNAL_RESET, &val, 1);
585 }
586
587 const char *protoName = "unknown";
588 switch (proto)
589 {
590 case PROTO_JTAG:
591 if (is_xmega)
592 val = EMULATOR_MODE_JTAG_XMEGA;
593 else
594 val = EMULATOR_MODE_JTAG;
595 protoName = "JTAG";
596 break;
597
598 case PROTO_DW:
599 val = EMULATOR_MODE_DEBUGWIRE;
600 protoName = "debugWIRE";
601 softbp_only = true;
602 break;
603
604 case PROTO_PDI:
605 val = EMULATOR_MODE_PDI;
606 protoName = "PDI";
607 break;
608 }
609 try
610 {
611 setJtagParameter(PAR_EMULATOR_MODE, &val, 1);
612 debug_active = true;
613 }
614 catch (jtag_io_exception&)
615 {
616 fprintf(stderr,
617 "Failed to activate %s debugging protocol\n",
618 protoName);
619 throw;
620 }
621
622 return;
623 }
624
625 throw jtag_exception("Failed to synchronise with the JTAG ICE (is it connected and powered?)");
626 }
627
628 /** Device automatic configuration
629 Determines the device being controlled by the JTAG ICE and configures
630 the system accordingly.
631
632 May be overridden by command line parameter.
633
634 */
635 void jtag2::deviceAutoConfig(void)
636 {
637 unsigned int device_id;
638 uchar *resp;
639 int respSize;
640 jtag_device_def_type *pDevice = deviceDefinitions;
641
642 // Auto config
643 debugOut("Automatic device detection: ");
644
645 /* Set daisy chain information */
646 configDaisyChain();
647
648 /* Read in the JTAG device ID to determine device */
649 if (proto == PROTO_DW)
650 {
651 getJtagParameter(PAR_TARGET_SIGNATURE, resp, respSize);
652 if (respSize < 3)
653 throw jtag_exception("Invalid response size to PAR_TARGET_SIGNATURE");
654 device_id = resp[1] | (resp[2] << 8);
655 delete [] resp;
656
657 statusOut("Reported debugWire device ID: 0x%0X\n", device_id);
658 }
659 else if (proto == PROTO_PDI)
660 {
661 resp = jtagRead(SIG_SPACE_ADDR_OFFSET, 3);
662 device_id = resp[2] | (resp[1] << 8);
663 delete [] resp;
664
665 statusOut("Reported PDI device ID: 0x%0X\n", device_id);
666 }
667 else
668 {
669 getJtagParameter(PAR_JTAGID, resp, respSize);
670 if (respSize < 5)
671 throw jtag_exception("Invalid response size to PAR_TARGET_SIGNATURE");
672 device_id = resp[1] | (resp[2] << 8) | (resp[3] << 16) | resp[4] << 24;
673 delete [] resp;
674
675 debugOut("JTAG id = 0x%0X : Ver = 0x%0x : Device = 0x%0x : Manuf = 0x%0x\n",
676 device_id,
677 (device_id & 0xF0000000) >> 28,
678 (device_id & 0x0FFFF000) >> 12,
679 (device_id & 0x00000FFE) >> 1);
680
681 device_id = (device_id & 0x0FFFF000) >> 12;
682 statusOut("Reported JTAG device ID: 0x%0X\n", device_id);
683 }
684
685 if (device_name == 0)
686 {
687 while (pDevice->name)
688 {
689 if (pDevice->device_id == device_id)
690 break;
691
692 pDevice++;
693 }
694 if (pDevice->name == 0)
695 {
696 unknownDevice(device_id);
697 throw jtag_exception();
698 }
699 }
700 else
701 {
702 debugOut("Looking for device: %s\n", device_name);
703
704 while (pDevice->name)
705 {
706 if (strcasecmp(pDevice->name, device_name) == 0)
707 break;
708
709 pDevice++;
710 }
711 if (pDevice->name == 0)
712 {
713 unknownDevice(device_id, false);
714 throw jtag_exception();
715 }
716 }
717
718 if (device_name)
719 {
720 if (device_id != pDevice->device_id)
721 {
722 statusOut("Configured for device ID: 0x%0X %s -- FORCED with %s\n",
723 pDevice->device_id, pDevice->name, device_name);
724 }
725 else
726 {
727 statusOut("Configured for device ID: 0x%0X %s -- Matched with "
728 "%s\n", pDevice->device_id, pDevice->name, device_name);
729 }
730 }
731 else
732 {
733 statusOut("Configured for device ID: 0x%0X %s\n",
734 pDevice->device_id, pDevice->name);
735 }
736
737 device_name = (char*)pDevice->name;
738
739 deviceDef = pDevice;
740
741 setDeviceDescriptor(pDevice);
742 }
743
744
745 void jtag2::initJtagBox(void)
746 {
747 statusOut("JTAG config starting.\n");
748
749 if (device_name != 0)
750 {
751 jtag_device_def_type *pDevice = deviceDefinitions;
752
753 while (pDevice->name)
754 {
755 if (strcasecmp(pDevice->name, device_name) == 0)
756 break;
757
758 pDevice++;
759 }
760
761 if (pDevice->name != 0)
762 {
763 // If a device name has been specified on the command-line,
764 // this overrides the is_xmega setting.
765 is_xmega = pDevice->is_xmega;
766 }
767 }
768
769 startJtagLink();
770 changeBitRate(115200);
771
772 interruptProgram();
773
774 deviceAutoConfig();
775
776 // Clear out the breakpoints.
777 deleteAllBreakpoints();
778
779 statusOut("JTAG config complete.\n");
780 }
781
782
783 void jtag2::initJtagOnChipDebugging(unsigned long bitrate)
784 {
785 statusOut("Preparing the target device for On Chip Debugging.\n");
786
787 if (proto == PROTO_JTAG)
788 {
789 uchar br;
790 if (bitrate >= 6400000)
791 br = 0;
792 else if (bitrate >= 2800000)
793 br = 1;
794 else if (bitrate >= 20900)
795 br = (unsigned char)(5.35e6 / (double)bitrate);
796 else
797 br = 255;
798 // Set JTAG bitrate
799 setJtagParameter(PAR_OCD_JTAG_CLK, &br, 1);
800 }
801
802 // Ensure on-chip debug enable fuse is enabled ie '0'
803
804 // The enableProgramming()/disableProgramming() pair might seem to
805 // be not needed (as the fuse read/write operations would enforce
806 // going to programming mode anyway), but for devices that don't
807 // feature an OCDEN fuse (i.e., Xmega devices),
808 // jtagActivateOcdenFuse() bails out immediately. At least with
809 // firmware 7.13, the ICE seems to become totally upset then when
810 // debugging an Xmega device without having went through a
811 // programming-mode cycle before. Upon a reset command, it
812 // confirms the reset, but the target happily proceeds in RUNNING
813 // state.
814 enableProgramming();
815 jtagActivateOcdenFuse();
816 disableProgramming();
817
818 resetProgram();
819 uchar timers = 0; // stopped
820 if (!is_xmega)
821 setJtagParameter(PAR_TIMERS_RUNNING, &timers, 1);
822 }
823
824 void jtag2::configDaisyChain(void)
825 {
826 unsigned char buf[4];
827
828 if ((dchain.units_before > 0) ||
829 (dchain.units_after > 0) ||
830 (dchain.bits_before > 0) ||
831 (dchain.bits_after > 0) ){
832 buf[0] = dchain.units_before;
833 buf[1] = dchain.units_after;
834 buf[2] = dchain.bits_before;
835 buf[3] = dchain.bits_after;
836 setJtagParameter(PAR_DAISY_CHAIN_INFO, buf, 4);
837 }
838 }