-
Notifications
You must be signed in to change notification settings - Fork 0
/
jtag3.c
2251 lines (1906 loc) · 57.9 KB
/
jtag3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* avrdude - A Downloader/Uploader for AVR device programmers
* Copyright (C) 2012 Joerg Wunsch <[email protected]>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* $Id: jtag3.c 1294 2014-03-12 23:03:18Z joerg_wunsch $ */
/*
* avrdude interface for Atmel JTAGICE3 programmer
*/
#include "ac_cfg.h"
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#include "avrdude.h"
#include "avr.h"
#include "crc16.h"
#include "pgm.h"
#include "jtag3.h"
#include "jtag3_private.h"
#include "serial.h"
#include "usbdevs.h"
/*
* Private data for this programmer.
*/
struct pdata
{
unsigned short command_sequence; /* Next cmd seqno to issue. */
/*
* See jtag3_read_byte() for an explanation of the flash and
* EEPROM page caches.
*/
unsigned char *flash_pagecache;
unsigned long flash_pageaddr;
unsigned int flash_pagesize;
unsigned char *eeprom_pagecache;
unsigned long eeprom_pageaddr;
unsigned int eeprom_pagesize;
int prog_enabled; /* Cached value of PROGRAMMING status. */
/* JTAG chain stuff */
unsigned char jtagchain[4];
/* Start address of Xmega boot area */
unsigned long boot_start;
/* Function to set the appropriate clock parameter */
int (*set_sck)(PROGRAMMER *, unsigned char *);
};
#define PDATA(pgm) ((struct pdata *)(pgm->cookie))
/*
* pgm->flag is marked as "for private use of the programmer".
* The following defines this programmer's use of that field.
*/
#define PGM_FL_IS_DW (0x0001)
#define PGM_FL_IS_PDI (0x0002)
#define PGM_FL_IS_JTAG (0x0004)
#define PGM_FL_IS_EDBG (0x0008)
static int jtag3_open(PROGRAMMER * pgm, char * port);
static int jtag3_edbg_prepare(PROGRAMMER * pgm);
static int jtag3_edbg_signoff(PROGRAMMER * pgm);
static int jtag3_edbg_send(PROGRAMMER * pgm, unsigned char * data, size_t len);
static int jtag3_edbg_recv_frame(PROGRAMMER * pgm, unsigned char **msg);
static int jtag3_initialize(PROGRAMMER * pgm, AVRPART * p);
static int jtag3_chip_erase(PROGRAMMER * pgm, AVRPART * p);
static int jtag3_read_byte(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
unsigned long addr, unsigned char * value);
static int jtag3_write_byte(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
unsigned long addr, unsigned char data);
static int jtag3_set_sck_period(PROGRAMMER * pgm, double v);
static void jtag3_print_parms1(PROGRAMMER * pgm, const char * p);
static int jtag3_paged_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
unsigned int page_size,
unsigned int addr, unsigned int n_bytes);
static unsigned char jtag3_memtype(PROGRAMMER * pgm, AVRPART * p, unsigned long addr);
static unsigned int jtag3_memaddr(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m, unsigned long addr);
void jtag3_setup(PROGRAMMER * pgm)
{
if ((pgm->cookie = malloc(sizeof(struct pdata))) == 0) {
fprintf(stderr,
"%s: jtag3_setup(): Out of memory allocating private data\n",
progname);
exit(1);
}
memset(pgm->cookie, 0, sizeof(struct pdata));
}
void jtag3_teardown(PROGRAMMER * pgm)
{
free(pgm->cookie);
}
static unsigned long
b4_to_u32(unsigned char *b)
{
unsigned long l;
l = b[0];
l += (unsigned)b[1] << 8;
l += (unsigned)b[2] << 16;
l += (unsigned)b[3] << 24;
return l;
}
static void
u32_to_b4(unsigned char *b, unsigned long l)
{
b[0] = l & 0xff;
b[1] = (l >> 8) & 0xff;
b[2] = (l >> 16) & 0xff;
b[3] = (l >> 24) & 0xff;
}
static unsigned short
b2_to_u16(unsigned char *b)
{
unsigned short l;
l = b[0];
l += (unsigned)b[1] << 8;
return l;
}
static void
u16_to_b2(unsigned char *b, unsigned short l)
{
b[0] = l & 0xff;
b[1] = (l >> 8) & 0xff;
}
static void jtag3_print_data(unsigned char *b, size_t s)
{
int i;
if (s < 2)
return;
for (i = 0; i < s; i++) {
fprintf(stderr, "0x%02x", b[i]);
if (i % 16 == 15)
putc('\n', stderr);
else
putc(' ', stderr);
}
if (i % 16 != 0)
putc('\n', stderr);
}
static void jtag3_prmsg(PROGRAMMER * pgm, unsigned char * data, size_t len)
{
int i;
if (verbose >= 4) {
fprintf(stderr, "Raw message:\n");
for (i = 0; i < len; i++) {
fprintf(stderr, "%02x ", data[i]);
if (i % 16 == 15)
putc('\n', stderr);
else
putc(' ', stderr);
}
if (i % 16 != 0)
putc('\n', stderr);
}
switch (data[0]) {
case SCOPE_INFO:
fprintf(stderr, "[info] ");
break;
case SCOPE_GENERAL:
fprintf(stderr, "[general] ");
break;
case SCOPE_AVR_ISP:
fprintf(stderr, "[AVRISP] ");
jtag3_print_data(data + 1, len - 1);
return;
case SCOPE_AVR:
fprintf(stderr, "[AVR] ");
break;
default:
fprintf(stderr, "[scope 0x%02x] ", data[0]);
break;
}
switch (data[1]) {
case RSP3_OK:
fprintf(stderr, "OK\n");
break;
case RSP3_FAILED:
fprintf(stderr, "FAILED");
if (len > 3)
{
char reason[50];
sprintf(reason, "0x%02x", data[3]);
switch (data[3])
{
case RSP3_FAIL_NO_ANSWER:
strcpy(reason, "target does not answer");
break;
case RSP3_FAIL_NO_TARGET_POWER:
strcpy(reason, "no target power");
break;
case RSP3_FAIL_NOT_UNDERSTOOD:
strcpy(reason, "command not understood");
break;
case RSP3_FAIL_WRONG_MODE:
strcpy(reason, "wrong (programming) mode");
break;
case RSP3_FAIL_PDI:
strcpy(reason, "PDI failure");
break;
case RSP3_FAIL_UNSUPP_MEMORY:
strcpy(reason, "unsupported memory type");
break;
case RSP3_FAIL_WRONG_LENGTH:
strcpy(reason, "wrong length in memory access");
break;
case RSP3_FAIL_DEBUGWIRE:
strcpy(reason, "debugWIRE communication failed");
break;
}
fprintf(stderr, ", reason: %s\n", reason);
}
else
{
fprintf(stderr, ", unspecified reason\n");
}
break;
case RSP3_DATA:
fprintf(stderr, "Data returned:\n");
jtag3_print_data(data + 2, len - 2);
break;
case RSP3_INFO:
fprintf(stderr, "Info returned:\n");
for (i = 2; i < len; i++) {
if (isprint(data[i]))
putc(data[i], stderr);
else
fprintf(stderr, "\\%03o", data[i]);
}
putc('\n', stderr);
break;
case RSP3_PC:
if (len < 7)
{
fprintf(stderr, "PC reply too short\n");
}
else
{
unsigned long pc = (data[6] << 24) | (data[5] << 16)
| (data[4] << 8) | data[3];
fprintf(stderr, "PC 0x%0lx\n", pc);
}
break;
default:
fprintf(stderr, "unknown message 0x%02x\n", data[1]);
}
}
static void jtag3_prevent(PROGRAMMER * pgm, unsigned char * data, size_t len)
{
int i;
if (verbose >= 4) {
fprintf(stderr, "Raw event:\n");
for (i = 0; i < len; i++) {
fprintf(stderr, "%02x ", data[i]);
if (i % 16 == 15)
putc('\n', stderr);
else
putc(' ', stderr);
}
if (i % 16 != 0)
putc('\n', stderr);
}
fprintf(stderr, "Event serial 0x%04x, ",
(data[3] << 8) | data[2]);
switch (data[4]) {
case SCOPE_INFO:
fprintf(stderr, "[info] ");
break;
case SCOPE_GENERAL:
fprintf(stderr, "[general] ");
break;
case SCOPE_AVR:
fprintf(stderr, "[AVR] ");
break;
default:
fprintf(stderr, "[scope 0x%02x] ", data[0]);
break;
}
switch (data[5]) {
case EVT3_BREAK:
fprintf(stderr, "BREAK");
if (len >= 11) {
fprintf(stderr, ", PC = 0x%lx, reason ", b4_to_u32(data + 6));
switch (data[10]) {
case 0x00:
fprintf(stderr, "unspecified");
break;
case 0x01:
fprintf(stderr, "program break");
break;
case 0x02:
fprintf(stderr, "data break PDSB");
break;
case 0x03:
fprintf(stderr, "data break PDMSB");
break;
default:
fprintf(stderr, "unknown: 0x%02x", data[10]);
}
/* There are two more bytes of data which always appear to be
* 0x01, 0x00. Purpose unknown. */
}
break;
case EVT3_SLEEP:
if (len >= 8 && data[7] == 0)
fprintf(stderr, "sleeping");
else if (len >= 8 && data[7] == 1)
fprintf(stderr, "wakeup");
else
fprintf(stderr, "unknown SLEEP event");
break;
case EVT3_POWER:
if (len >= 8 && data[7] == 0)
fprintf(stderr, "power-down");
else if (len >= 8 && data[7] == 1)
fprintf(stderr, "power-up");
else
fprintf(stderr, "unknown POWER event");
break;
default:
fprintf(stderr, "UNKNOWN 0x%02x", data[5]);
break;
}
putc('\n', stderr);
}
int jtag3_send(PROGRAMMER * pgm, unsigned char * data, size_t len)
{
unsigned char *buf;
if (pgm->flag & PGM_FL_IS_EDBG)
return jtag3_edbg_send(pgm, data, len);
if (verbose >= 3)
fprintf(stderr, "\n%s: jtag3_send(): sending %lu bytes\n",
progname, (unsigned long)len);
if ((buf = malloc(len + 4)) == NULL)
{
fprintf(stderr, "%s: jtag3_send(): out of memory",
progname);
return -1;
}
buf[0] = TOKEN;
buf[1] = 0; /* dummy */
u16_to_b2(buf + 2, PDATA(pgm)->command_sequence);
memcpy(buf + 4, data, len);
if (serial_send(&pgm->fd, buf, len + 4) != 0) {
fprintf(stderr,
"%s: jtag3_send(): failed to send command to serial port\n",
progname);
return -1;
}
free(buf);
return 0;
}
static int jtag3_edbg_send(PROGRAMMER * pgm, unsigned char * data, size_t len)
{
unsigned char buf[USBDEV_MAX_XFER_3];
unsigned char status[USBDEV_MAX_XFER_3];
int rv;
if (verbose >= 4)
{
memset(buf, 0, USBDEV_MAX_XFER_3);
memset(status, 0, USBDEV_MAX_XFER_3);
}
if (verbose >= 3)
fprintf(stderr, "\n%s: jtag3_edbg_send(): sending %lu bytes\n",
progname, (unsigned long)len);
if (len + 8 > USBDEV_MAX_XFER_3)
{
fprintf(stderr,
"%s: jtag3_edbg_send(): Fragmentation not (yet) implemented!\n",
progname);
return -1;
}
buf[0] = EDBG_VENDOR_AVR_CMD;
buf[1] = (1 << 4) | 1; /* first out of a total of 1 fragments */
buf[2] = (len + 4) >> 8;
buf[3] = (len + 4) & 0xff;
buf[4] = TOKEN;
buf[5] = 0; /* dummy */
u16_to_b2(buf + 6, PDATA(pgm)->command_sequence);
memcpy(buf + 8, data, len);
if (serial_send(&pgm->fd, buf, USBDEV_MAX_XFER_3) != 0) {
fprintf(stderr,
"%s: jtag3_edbg_send(): failed to send command to serial port\n",
progname);
return -1;
}
rv = serial_recv(&pgm->fd, status, USBDEV_MAX_XFER_3);
if (rv < 0) {
/* timeout in receive */
if (verbose > 1)
fprintf(stderr,
"%s: jtag3_edbg_send(): Timeout receiving packet\n",
progname);
return -1;
}
if (status[0] != EDBG_VENDOR_AVR_CMD || status[1] != 0x01)
{
/* what to do in this case? */
fprintf(stderr,
"%s: jtag3_edbg_send(): Unexpected response 0x%02x, 0x%02x\n",
progname, status[0], status[1]);
}
return 0;
}
/*
* Send out all the CMSIS-DAP stuff needed to prepare the ICE.
*/
static int jtag3_edbg_prepare(PROGRAMMER * pgm)
{
unsigned char buf[USBDEV_MAX_XFER_3];
unsigned char status[USBDEV_MAX_XFER_3];
int rv;
if (verbose >= 3)
fprintf(stderr, "\n%s: jtag3_edbg_prepare()\n",
progname);
if (verbose >= 4)
memset(buf, 0, USBDEV_MAX_XFER_3);
buf[0] = CMSISDAP_CMD_CONNECT;
buf[1] = CMSISDAP_CONN_SWD;
if (serial_send(&pgm->fd, buf, USBDEV_MAX_XFER_3) != 0) {
fprintf(stderr,
"%s: jtag3_edbg_prepare(): failed to send command to serial port\n",
progname);
return -1;
}
rv = serial_recv(&pgm->fd, status, USBDEV_MAX_XFER_3);
if (rv != USBDEV_MAX_XFER_3) {
fprintf(stderr,
"%s: jtag3_edbg_prepare(): failed to read from serial port (%d)\n",
progname, rv);
return -1;
}
if (status[0] != CMSISDAP_CMD_CONNECT ||
status[1] == 0)
fprintf(stderr,
"%s: jtag3_edbg_prepare(): unexpected response 0x%02x, 0x%02x\n",
progname, status[0], status[1]);
if (verbose >= 2)
fprintf(stderr,
"%s: jtag3_edbg_prepare(): connection status 0x%02x\n",
progname, status[1]);
buf[0] = CMSISDAP_CMD_LED;
buf[1] = CMSISDAP_LED_CONNECT;
buf[2] = 1;
if (serial_send(&pgm->fd, buf, USBDEV_MAX_XFER_3) != 0) {
fprintf(stderr,
"%s: jtag3_edbg_prepare(): failed to send command to serial port\n",
progname);
return -1;
}
rv = serial_recv(&pgm->fd, status, USBDEV_MAX_XFER_3);
if (rv != USBDEV_MAX_XFER_3) {
fprintf(stderr,
"%s: jtag3_edbg_prepare(): failed to read from serial port (%d)\n",
progname, rv);
return -1;
}
if (status[0] != CMSISDAP_CMD_LED ||
status[1] != 0)
fprintf(stderr,
"%s: jtag3_edbg_prepare(): unexpected response 0x%02x, 0x%02x\n",
progname, status[0], status[1]);
return 0;
}
/*
* Send out all the CMSIS-DAP stuff when signing off.
*/
static int jtag3_edbg_signoff(PROGRAMMER * pgm)
{
unsigned char buf[USBDEV_MAX_XFER_3];
unsigned char status[USBDEV_MAX_XFER_3];
int rv;
if (verbose >= 3)
fprintf(stderr, "\n%s: jtag3_edbg_signoff()\n",
progname);
if (verbose >= 4)
memset(buf, 0, USBDEV_MAX_XFER_3);
buf[0] = CMSISDAP_CMD_LED;
buf[1] = CMSISDAP_LED_CONNECT;
buf[2] = 0;
if (serial_send(&pgm->fd, buf, USBDEV_MAX_XFER_3) != 0) {
fprintf(stderr,
"%s: jtag3_edbg_signoff(): failed to send command to serial port\n",
progname);
return -1;
}
rv = serial_recv(&pgm->fd, status, USBDEV_MAX_XFER_3);
if (rv != USBDEV_MAX_XFER_3) {
fprintf(stderr,
"%s: jtag3_edbg_signoff(): failed to read from serial port (%d)\n",
progname, rv);
return -1;
}
if (status[0] != CMSISDAP_CMD_LED ||
status[1] != 0)
fprintf(stderr,
"%s: jtag3_edbg_signoff(): unexpected response 0x%02x, 0x%02x\n",
progname, status[0], status[1]);
buf[0] = CMSISDAP_CMD_DISCONNECT;
if (serial_send(&pgm->fd, buf, USBDEV_MAX_XFER_3) != 0) {
fprintf(stderr,
"%s: jtag3_edbg_signoff(): failed to send command to serial port\n",
progname);
return -1;
}
rv = serial_recv(&pgm->fd, status, USBDEV_MAX_XFER_3);
if (rv != USBDEV_MAX_XFER_3) {
fprintf(stderr,
"%s: jtag3_edbg_signoff(): failed to read from serial port (%d)\n",
progname, rv);
return -1;
}
if (status[0] != CMSISDAP_CMD_DISCONNECT ||
status[1] != 0)
fprintf(stderr,
"%s: jtag3_edbg_signoff(): unexpected response 0x%02x, 0x%02x\n",
progname, status[0], status[1]);
return 0;
}
static int jtag3_drain(PROGRAMMER * pgm, int display)
{
return serial_drain(&pgm->fd, display);
}
/*
* Receive one frame, return it in *msg. Received sequence number is
* returned in seqno. Any valid frame will be returned, regardless
* whether it matches the expected sequence number, including event
* notification frames (seqno == 0xffff).
*
* Caller must eventually free the buffer.
*/
static int jtag3_recv_frame(PROGRAMMER * pgm, unsigned char **msg) {
int rv;
unsigned char *buf = NULL;
if (pgm->flag & PGM_FL_IS_EDBG)
return jtag3_edbg_recv_frame(pgm, msg);
if (verbose >= 4)
fprintf(stderr, "%s: jtag3_recv():\n", progname);
if ((buf = malloc(pgm->fd.usb.max_xfer)) == NULL) {
fprintf(stderr, "%s: jtag3_recv(): out of memory\n",
progname);
return -1;
}
if (verbose >= 4)
memset(buf, 0, pgm->fd.usb.max_xfer);
rv = serial_recv(&pgm->fd, buf, pgm->fd.usb.max_xfer);
if (rv < 0) {
/* timeout in receive */
if (verbose > 1)
fprintf(stderr,
"%s: jtag3_recv(): Timeout receiving packet\n",
progname);
free(buf);
return -1;
}
*msg = buf;
return rv;
}
static int jtag3_edbg_recv_frame(PROGRAMMER * pgm, unsigned char **msg) {
int rv, len;
unsigned char *buf = NULL;
if (verbose >= 4)
fprintf(stderr, "%s: jtag3_edbg_recv():\n", progname);
if ((buf = malloc(USBDEV_MAX_XFER_3)) == NULL) {
fprintf(stderr, "%s: jtag3_edbg_recv(): out of memory\n",
progname);
return -1;
}
buf[0] = EDBG_VENDOR_AVR_RSP;
if (serial_send(&pgm->fd, buf, USBDEV_MAX_XFER_3) != 0) {
fprintf(stderr,
"%s: jtag3_edbg_recv(): error sending CMSIS-DAP vendor command\n",
progname);
return -1;
}
rv = serial_recv(&pgm->fd, buf, USBDEV_MAX_XFER_3);
if (rv < 0) {
/* timeout in receive */
if (verbose > 1)
fprintf(stderr,
"%s: jtag3_edbg_recv(): Timeout receiving packet\n",
progname);
free(buf);
return -1;
}
if (buf[0] != EDBG_VENDOR_AVR_RSP ||
buf[1] != ((1 << 4) | 1)) {
fprintf(stderr,
"%s: jtag3_edbg_recv(): Unexpected response 0x%02x, 0x%02x\n",
progname, buf[0], buf[1]);
return -1;
}
/* calculate length from response; CMSIS-DAP response might be larger */
len = (buf[2] << 8) | buf[3];
if (len > rv + 4) {
fprintf(stderr,
"%s: jtag3_edbg_recv(): Unexpected length value (%d > %d)\n",
progname, len, rv + 4);
len = rv + 4;
}
memmove(buf, buf + 4, len);
*msg = buf;
return rv;
}
int jtag3_recv(PROGRAMMER * pgm, unsigned char **msg) {
unsigned short r_seqno;
int rv;
for (;;) {
if ((rv = jtag3_recv_frame(pgm, msg)) <= 0)
return rv;
if ((rv & USB_RECV_FLAG_EVENT) != 0) {
if (verbose >= 3)
jtag3_prevent(pgm, *msg, rv & USB_RECV_LENGTH_MASK);
free(*msg);
continue;
}
rv &= USB_RECV_LENGTH_MASK;
r_seqno = ((*msg)[2] << 8) | (*msg)[1];
if (verbose >= 3)
fprintf(stderr, "%s: jtag3_recv(): "
"Got message seqno %d (command_sequence == %d)\n",
progname, r_seqno, PDATA(pgm)->command_sequence);
if (r_seqno == PDATA(pgm)->command_sequence) {
if (++(PDATA(pgm)->command_sequence) == 0xffff)
PDATA(pgm)->command_sequence = 0;
/*
* We move the payload to the beginning of the buffer, to make
* the job easier for the caller. We have to return the
* original pointer though, as the caller must free() it.
*/
rv -= 3;
memmove(*msg, *msg + 3, rv);
return rv;
}
if (verbose >= 2)
fprintf(stderr, "%s: jtag3_recv(): "
"got wrong sequence number, %u != %u\n",
progname, r_seqno, PDATA(pgm)->command_sequence);
free(*msg);
}
}
int jtag3_command(PROGRAMMER *pgm, unsigned char *cmd, unsigned int cmdlen,
unsigned char **resp, const char *descr)
{
int status;
unsigned char c;
if (verbose >= 2)
fprintf(stderr, "%s: Sending %s command: ",
progname, descr);
jtag3_send(pgm, cmd, cmdlen);
status = jtag3_recv(pgm, resp);
if (status <= 0) {
if (verbose >= 2)
putc('\n', stderr);
fprintf(stderr,
"%s: %s command: timeout/error communicating with programmer (status %d)\n",
progname, descr, status);
return -1;
} else if (verbose >= 3) {
putc('\n', stderr);
jtag3_prmsg(pgm, *resp, status);
} else if (verbose == 2)
fprintf(stderr, "0x%02x (%d bytes msg)\n", (*resp)[1], status);
c = (*resp)[1];
if ((c & RSP3_STATUS_MASK) != RSP3_OK) {
fprintf(stderr,
"%s: bad response to %s command: 0x%02x\n",
progname, descr, c);
free(*resp);
resp = 0;
return -1;
}
return status;
}
int jtag3_getsync(PROGRAMMER * pgm, int mode) {
unsigned char buf[3], *resp;
if (verbose >= 3)
fprintf(stderr, "%s: jtag3_getsync()\n", progname);
if (pgm->flag & PGM_FL_IS_EDBG) {
if (jtag3_edbg_prepare(pgm) < 0)
return -1;
}
/* Get the sign-on information. */
buf[0] = SCOPE_GENERAL;
buf[1] = CMD3_SIGN_ON;
buf[2] = 0;
if (jtag3_command(pgm, buf, 3, &resp, "sign-on") < 0)
return -1;
free(resp);
return 0;
}
/*
* issue the 'chip erase' command to the AVR device
*/
static int jtag3_chip_erase(PROGRAMMER * pgm, AVRPART * p)
{
unsigned char buf[8], *resp;
buf[0] = SCOPE_AVR;
buf[1] = CMD3_ERASE_MEMORY;
buf[2] = 0;
buf[3] = XMEGA_ERASE_CHIP;
buf[4] = buf[5] = buf[6] = buf[7] = 0; /* page address */
if (jtag3_command(pgm, buf, 8, &resp, "chip erase") < 0)
return -1;
free(resp);
return 0;
}
/*
* There is no chip erase functionality in debugWire mode.
*/
static int jtag3_chip_erase_dw(PROGRAMMER * pgm, AVRPART * p)
{
fprintf(stderr, "%s: Chip erase not supported in debugWire mode\n",
progname);
return 0;
}
static int jtag3_program_enable_dummy(PROGRAMMER * pgm, AVRPART * p)
{
return 0;
}
static int jtag3_program_enable(PROGRAMMER * pgm)
{
unsigned char buf[3], *resp;
int use_ext_reset;
if (PDATA(pgm)->prog_enabled)
return 0;
for (use_ext_reset = 0; use_ext_reset <= 1; use_ext_reset++) {
buf[0] = SCOPE_AVR;
buf[1] = CMD3_ENTER_PROGMODE;
buf[2] = 0;
if (jtag3_command(pgm, buf, 3, &resp, "enter progmode") >= 0) {
free(resp);
break;
}
/* XXX activate external reset here */
if (verbose > 0)
fprintf(stderr,
"%s: retrying with external reset applied\n",
progname);
}
PDATA(pgm)->prog_enabled = 1;
return 0;
}
static int jtag3_program_disable(PROGRAMMER * pgm)
{
unsigned char buf[3], *resp;
if (!PDATA(pgm)->prog_enabled)
return 0;
buf[0] = SCOPE_AVR;
buf[1] = CMD3_LEAVE_PROGMODE;
buf[2] = 0;
if (jtag3_command(pgm, buf, 3, &resp, "leave progmode") < 0)
return -1;
free(resp);
PDATA(pgm)->prog_enabled = 0;
return 0;
}
static int jtag3_set_sck_xmega_pdi(PROGRAMMER *pgm, unsigned char *clk)
{
return jtag3_setparm(pgm, SCOPE_AVR, 1, PARM3_CLK_XMEGA_PDI, clk, 2);
}
static int jtag3_set_sck_xmega_jtag(PROGRAMMER *pgm, unsigned char *clk)
{
return jtag3_setparm(pgm, SCOPE_AVR, 1, PARM3_CLK_XMEGA_JTAG, clk, 2);
}
static int jtag3_set_sck_mega_jtag(PROGRAMMER *pgm, unsigned char *clk)
{
return jtag3_setparm(pgm, SCOPE_AVR, 1, PARM3_CLK_MEGA_PROG, clk, 2);
}
/*
* initialize the AVR device and prepare it to accept commands
*/
static int jtag3_initialize(PROGRAMMER * pgm, AVRPART * p)
{
unsigned char conn = 0, parm[4];
const char *ifname;
unsigned char cmd[4], *resp;
int status;
/*
* At least, as of firmware 2.12, the JTAGICE3 doesn't handle
* splitting packets correctly. On a large transfer, the first
* split packets are correct, but remaining packets contain just
* garbage.
*
* We move the check here so in case future firmware versions fix
* this, the check below can be made dependended on the actual
* firmware level. Retrieving the firmware version can always be
* accomplished with USB 1.1 (64 byte max) packets.
*
* Allow to override the check by -F (so users could try on newer
* firmware), but warn loudly.
*/
if (jtag3_getparm(pgm, SCOPE_GENERAL, 0, PARM3_FW_MAJOR, parm, 2) < 0)
return -1;
if (pgm->fd.usb.max_xfer < USBDEV_MAX_XFER_3 && (pgm->flag & PGM_FL_IS_EDBG) == 0) {
fprintf(stderr,
"%s: the JTAGICE3's firmware %d.%d is broken on USB 1.1 connections, sorry\n",
progname, parm[0], parm[1]);
if (ovsigck) {
fprintf(stderr,
"%s: forced to continue by option -F; THIS PUTS THE DEVICE'S DATA INTEGRITY AT RISK!\n",
progname);
} else {
return -1;
}
}
if (pgm->flag & PGM_FL_IS_DW) {
ifname = "debugWire";
if (p->flags & AVRPART_HAS_DW)
conn = PARM3_CONN_DW;
} else if (pgm->flag & PGM_FL_IS_PDI) {
ifname = "PDI";
if (p->flags & AVRPART_HAS_PDI)
conn = PARM3_CONN_PDI;
} else {
ifname = "JTAG";
if (p->flags & AVRPART_HAS_JTAG)
conn = PARM3_CONN_JTAG;
}
if (conn == 0) {
fprintf(stderr, "%s: jtag3_initialize(): part %s has no %s interface\n",
progname, p->desc, ifname);
return -1;
}