-
Notifications
You must be signed in to change notification settings - Fork 51
/
adapter-pickit2.c
1278 lines (1175 loc) · 44.7 KB
/
adapter-pickit2.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
/*
* Interface to PIC32 ICSP port using Microchip PICkit2/PICkit3 USB adapter.
*
* To use PICkit3, you would need to upgrade the firmware
* using free PICkit 3 Scripting Tool from Microchip.
*
* Copyright (C) 2011-2013 Serge Vakulenko
*
* This file is part of PIC32PROG project, which is distributed
* under the terms of the GNU General Public License (GPL).
* See the accompanying file "COPYING" for more details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include "adapter.h"
#include "hidapi.h"
#include "pickit2.h"
#include "pic32.h"
typedef struct {
/* Common part */
adapter_t adapter;
int is_pk3;
const char *name;
/* Device handle for libusb. */
hid_device *hiddev;
unsigned char reply [64];
unsigned use_executive;
unsigned serial_execution_mode;
} pickit_adapter_t;
/*
* Programmable power voltages.
*/
#define VDD_VOLTAGE 3.3 /* Power supply */
#define VDD_LIMIT 2.81
#define VPP_VOLTAGE 3.28 /* Reset voltage */
#define VPP_LIMIT 2.26
/*
* Identifiers of USB adapter.
*/
#define MICROCHIP_VID 0x04d8
#define PICKIT2_PID 0x0033 /* Microchip PICkit 2 */
#define PICKIT3_PID 0x900a /* Microchip PICkit 3 */
#define ONBOARD_PID 0x8107 /* Onboard Programmer */
#define CHIPKIT_PID 0x8108 /* chipKIT Programmer */
/*
* USB endpoints.
*/
#define OUT_EP 0x01
#define IN_EP 0x81
#define IFACE 0
#define TIMO_MSEC 1000
#define WORD_AS_BYTES(w) (unsigned char) (w), \
(unsigned char) ((w) >> 8), \
(unsigned char) ((w) >> 16), \
(unsigned char) ((w) >> 24)
static void pickit_send_buf(pickit_adapter_t *a, unsigned char *buf, unsigned nbytes)
{
if (debug_level > 1) {
int k;
fprintf(stderr, "---Send");
for (k=0; k<nbytes; ++k) {
if (k != 0 && (k & 15) == 0)
fprintf(stderr, "\n ");
fprintf(stderr, " %02x", buf[k]);
}
fprintf(stderr, "\n");
}
hid_write(a->hiddev, buf, 64);
}
static void pickit_send(pickit_adapter_t *a, unsigned argc, ...)
{
va_list ap;
unsigned i;
unsigned char buf [64];
memset(buf, CMD_END_OF_BUFFER, 64);
va_start(ap, argc);
for (i=0; i<argc; ++i)
buf[i] = va_arg(ap, int);
va_end(ap);
pickit_send_buf(a, buf, i);
}
static void pickit_recv(pickit_adapter_t *a)
{
if (hid_read(a->hiddev, a->reply, 64) != 64) {
fprintf(stderr, "%s: error receiving packet\n", a->name);
exit(-1);
}
if (debug_level > 1) {
int k;
fprintf(stderr, "--->>>>");
for (k=0; k<64; ++k) {
if (k != 0 && (k & 15) == 0)
fprintf(stderr, "\n ");
fprintf(stderr, " %02x", a->reply[k]);
}
fprintf(stderr, "\n");
}
}
static void check_timeout(pickit_adapter_t *a, const char *message)
{
unsigned status;
pickit_send(a, 1, CMD_READ_STATUS);
pickit_recv(a);
status = a->reply[0] | a->reply[1] << 8;
if (status & STATUS_ICD_TIMEOUT) {
fprintf(stderr, "%s: timed out at %s, status = %04x\n",
a->name, message, status);
exit(-1);
}
}
/*
* Put device to serial execution mode.
*/
static void serial_execution(pickit_adapter_t *a)
{
if (a->serial_execution_mode)
return;
a->serial_execution_mode = 1;
// Enter serial execution.
if (debug_level > 0)
fprintf(stderr, "%s: enter serial execution\n", a->name);
pickit_send(a, 29, CMD_EXECUTE_SCRIPT, 27,
SCRIPT_JT2_SENDCMD, TAP_SW_MTAP,
SCRIPT_JT2_SENDCMD, MTAP_COMMAND,
SCRIPT_JT2_XFERDATA8_LIT, MCHP_STATUS,
SCRIPT_JT2_SENDCMD, TAP_SW_MTAP,
SCRIPT_JT2_SENDCMD, MTAP_COMMAND,
SCRIPT_JT2_XFERDATA8_LIT, MCHP_ASSERT_RST,
SCRIPT_JT2_SENDCMD, TAP_SW_ETAP,
SCRIPT_JT2_SETMODE, 6, 0x1F,
SCRIPT_JT2_SENDCMD, ETAP_EJTAGBOOT,
SCRIPT_JT2_SENDCMD, TAP_SW_MTAP,
SCRIPT_JT2_SENDCMD, MTAP_COMMAND,
SCRIPT_JT2_XFERDATA8_LIT, MCHP_DEASSERT_RST,
SCRIPT_JT2_XFERDATA8_LIT, MCHP_FLASH_ENABLE);
}
static void
step1_6_mm(pickit_adapter_t *a, unsigned nwords)
{
if (debug_level > 0)
fprintf(stderr, "%s: download PE loader\n", a->name);
pickit_send(a, 20, CMD_CLEAR_DOWNLOAD_BUFFER,
CMD_DOWNLOAD_DATA, 8, //----------------- step 1
WORD_AS_BYTES(0xa00041a4),
WORD_AS_BYTES(0x02005084),
CMD_EXECUTE_SCRIPT, 7, //----------------- execute
SCRIPT_JT2_SENDCMD, TAP_SW_ETAP,
SCRIPT_JT2_SETMODE, 6, 0x1F,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF);
check_timeout(a, "step1");
// Download the PE loader
int i;
for (i=0; i<PIC32_PEMM_LOADER_LEN; i+=2) {
pickit_send(a, 20, CMD_CLEAR_DOWNLOAD_BUFFER,
CMD_DOWNLOAD_DATA, 12, //------------- step 5
WORD_AS_BYTES((0x41A6
| pic32_pemm_loader[i] << 16)),
WORD_AS_BYTES((0x50c6
| pic32_pemm_loader[i+1] << 16)),
WORD_AS_BYTES(0x6e42eb40),
CMD_EXECUTE_SCRIPT, 3, //------------- execute
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF);
check_timeout(a, "step5");
}
// Jump to PE loader
pickit_send(a, 47, CMD_CLEAR_DOWNLOAD_BUFFER,
CMD_DOWNLOAD_DATA, 20, //----------------- step 6
WORD_AS_BYTES(0xa00041b9),
WORD_AS_BYTES(0x02015339),
WORD_AS_BYTES(0x0c004599),
WORD_AS_BYTES(0x0c000c00),
WORD_AS_BYTES(0x0c000c00),
CMD_EXECUTE_SCRIPT, 22, //----------------- execute
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_SENDCMD, TAP_SW_ETAP,
SCRIPT_JT2_SETMODE, 6, 0x1F,
SCRIPT_JT2_SENDCMD, ETAP_FASTDATA,
SCRIPT_JT2_XFRFASTDAT_LIT,
0, 3, 0, 0xA0, // PE_ADDRESS = 0xA000_0300
SCRIPT_JT2_XFRFASTDAT_LIT,
(unsigned char) nwords, // PE_SIZE
(unsigned char) (nwords >> 8),
0, 0);
check_timeout(a, "step6");
}
static void
step1_6_mz(pickit_adapter_t *a, unsigned nwords)
{
if (debug_level > 0)
fprintf(stderr, "%s: download PE loader\n", a->name);
pickit_send(a, 45, CMD_CLEAR_DOWNLOAD_BUFFER,
CMD_DOWNLOAD_DATA, 28, //----------------- step 1
WORD_AS_BYTES(0x3c04bf88), // lui a0, 0xbf88
WORD_AS_BYTES(0x34842000), // ori a0, 0x2000 - address of BMXCON
WORD_AS_BYTES(0x3c05001f), // lui a1, 0x1f
WORD_AS_BYTES(0x34a50040), // ori a1, 0x40 - a1 has 001f0040
WORD_AS_BYTES(0xac850000), // sw a1, 0(a0) - BMXCON initialized
//----------------- step 2
WORD_AS_BYTES(0x34050800), // li a1, 0x800 - a1 has 00000800
WORD_AS_BYTES(0xac850010), // sw a1, 16(a0) - BMXDKPBA initialized
CMD_EXECUTE_SCRIPT, 12, //----------------- execute
SCRIPT_JT2_SENDCMD, TAP_SW_ETAP,
SCRIPT_JT2_SETMODE, 6, 0x1F,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF);
check_timeout(a, "step1");
pickit_send(a, 30, CMD_CLEAR_DOWNLOAD_BUFFER,
CMD_DOWNLOAD_DATA, 20, //----------------- step 3
WORD_AS_BYTES(0x8c850040), // lw a1, 64(a0) - load BMXDMSZ
WORD_AS_BYTES(0xac850020), // sw a1, 32(a0) - BMXDUDBA initialized
WORD_AS_BYTES(0xac850030), // sw a1, 48(a0) - BMXDUPBA initialized
//----------------- step 4
WORD_AS_BYTES(0x3c04a000), // lui a0, 0xa000
WORD_AS_BYTES(0x34840800), // ori a0, 0x800 - a0 has a0000800
CMD_EXECUTE_SCRIPT, 5, //----------------- execute
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF);
check_timeout(a, "step3");
// Download the PE loader
int i;
for (i=0; i<PIC32_PE_LOADER_LEN; i+=2) {
pickit_send(a, 25, CMD_CLEAR_DOWNLOAD_BUFFER,
CMD_DOWNLOAD_DATA, 16, //------------- step 5
WORD_AS_BYTES((0x3c060000 // lui a2, PE_loader_hi++
| pic32_pe_loader[i])),
WORD_AS_BYTES((0x34c60000 // ori a2, PE_loader_lo++
| pic32_pe_loader[i+1])),
WORD_AS_BYTES(0xac860000), // sw a2, 0(a0)
WORD_AS_BYTES(0x24840004), // addiu a0, 4
CMD_EXECUTE_SCRIPT, 4, //------------- execute
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF);
check_timeout(a, "step5");
}
// Jump to PE loader
pickit_send(a, 42, CMD_CLEAR_DOWNLOAD_BUFFER,
CMD_DOWNLOAD_DATA, 16, //----------------- step 6
WORD_AS_BYTES(0x3c19a000), // lui t9, 0xa000
WORD_AS_BYTES(0x37390800), // ori t9, 0x800 - t9 has a0000800
WORD_AS_BYTES(0x03200008), // jr t9
WORD_AS_BYTES(0x00000000), // nop
CMD_EXECUTE_SCRIPT, 21, //----------------- execute
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_SENDCMD, TAP_SW_ETAP,
SCRIPT_JT2_SETMODE, 6, 0x1F,
SCRIPT_JT2_SENDCMD, ETAP_FASTDATA,
SCRIPT_JT2_XFRFASTDAT_LIT,
0, 9, 0, 0xA0, // PE_ADDRESS = 0xA000_0900
SCRIPT_JT2_XFRFASTDAT_LIT,
(unsigned char) nwords, // PE_SIZE
(unsigned char) (nwords >> 8),
0, 0);
check_timeout(a, "step6");
}
/*
* Download programming executive (PE).
*/
static void pickit_load_executive(adapter_t *adapter,
const unsigned *pe, unsigned nwords,
unsigned pe_version)
{
pickit_adapter_t *a = (pickit_adapter_t*) adapter;
int i;
a->name = a->adapter.family_name;
//fprintf(stderr, "%s: load_executive\n", a->name);
a->use_executive = 1;
serial_execution(a);
/* Round nwords to 10, to be compatible with routine below.
* This is why there are extra 0s in the executive file for padding. */
nwords = (nwords%10) == 0 ? nwords : nwords + 10 - (nwords%10);
if (a->adapter.family_name_short == FAMILY_MM)
step1_6_mm(a, nwords);
else
step1_6_mz(a, nwords);
// Download the PE itself (step 7-B)
if (debug_level > 0)
fprintf(stderr, "%s: download PE code\n", a->name);
int nloops = (nwords + 9) / 10;
for (i=0; i<nloops; i++, pe+=10) { // download 10 words at a time
pickit_send(a, 55, CMD_CLEAR_DOWNLOAD_BUFFER,
CMD_DOWNLOAD_DATA, 40,
WORD_AS_BYTES(pe[0]),
WORD_AS_BYTES(pe[1]),
WORD_AS_BYTES(pe[2]),
WORD_AS_BYTES(pe[3]),
WORD_AS_BYTES(pe[4]),
WORD_AS_BYTES(pe[5]),
WORD_AS_BYTES(pe[6]),
WORD_AS_BYTES(pe[7]),
WORD_AS_BYTES(pe[8]),
WORD_AS_BYTES(pe[9]),
CMD_EXECUTE_SCRIPT, 10, // execute
SCRIPT_JT2_XFRFASTDAT_BUF,
SCRIPT_JT2_XFRFASTDAT_BUF,
SCRIPT_JT2_XFRFASTDAT_BUF,
SCRIPT_JT2_XFRFASTDAT_BUF,
SCRIPT_JT2_XFRFASTDAT_BUF,
SCRIPT_JT2_XFRFASTDAT_BUF,
SCRIPT_JT2_XFRFASTDAT_BUF,
SCRIPT_JT2_XFRFASTDAT_BUF,
SCRIPT_JT2_XFRFASTDAT_BUF,
SCRIPT_JT2_XFRFASTDAT_BUF);
check_timeout(a, "step7");
}
mdelay(100);
// Download the PE instructions
pickit_send(a, 15, CMD_CLEAR_DOWNLOAD_BUFFER,
CMD_DOWNLOAD_DATA, 8,
WORD_AS_BYTES(0x00000000), // step 8 - jump to PE
WORD_AS_BYTES(0xDEAD0000),
CMD_EXECUTE_SCRIPT, 2, // execute
SCRIPT_JT2_XFRFASTDAT_BUF,
SCRIPT_JT2_XFRFASTDAT_BUF);
check_timeout(a, "step8");
mdelay(100);
pickit_send(a, 11, CMD_CLEAR_UPLOAD_BUFFER,
CMD_EXECUTE_SCRIPT, 8,
SCRIPT_JT2_SENDCMD, ETAP_FASTDATA,
SCRIPT_JT2_XFRFASTDAT_LIT,
0x00, 0x00, // length = 0
0x07, 0x00, // EXEC_VERSION
SCRIPT_JT2_GET_PE_RESP);
check_timeout(a, "EXEC_VERSION");
pickit_send(a, 1, CMD_UPLOAD_DATA);
pickit_recv(a);
unsigned version = a->reply[3] | (a->reply[4] << 8);
if (version != 0x0007) { // command echo
fprintf(stderr, "%s: bad PE reply = %04x\n", a->name, version);
exit(-1);
}
version = a->reply[1] | (a->reply[2] << 8);
if (version != pe_version) {
fprintf(stderr, "%s: bad PE version = %04x, expected %04x\n",
a->name, version, pe_version);
exit(-1);
}
if (debug_level > 0)
fprintf(stderr, "%s: PE version = %04x\n", a->name, version);
}
#if 0
int pe_blank_check(pickit_adapter_t *a,
unsigned int start, unsigned int nbytes)
{
pickit_send(a, 21, CMD_CLEAR_UPLOAD_BUFFER, CMD_EXECUTE_SCRIPT, 18,
SCRIPT_JT2_SENDCMD, ETAP_FASTDATA,
SCRIPT_JT2_XFRFASTDAT_LIT,
0x00, 0x00,
0x06, 0x00, // BLANK_CHECK
SCRIPT_JT2_XFRFASTDAT_LIT,
(unsigned char) start,
(unsigned char) (start >> 8),
(unsigned char) (start >> 16),
(unsigned char) (start >> 24),
SCRIPT_JT2_XFRFASTDAT_LIT,
(unsigned char) nbytes,
(unsigned char) (nbytes >> 8),
(unsigned char) (nbytes >> 16),
(unsigned char) (nbytes >> 24),
SCRIPT_JT2_GET_PE_RESP);
check_timeout(a, "BLANK_CHECK");
pickit_send(a, 1, CMD_UPLOAD_DATA);
pickit_recv(a);
if (a->reply[3] != 6 || a->reply[1] != 0) { // response code 0 = success
return 0;
}
return 1;
}
int pe_get_crc(pickit_adapter_t *a,
unsigned int start, unsigned int nbytes)
{
pickit_send(a, 22, CMD_CLEAR_UPLOAD_BUFFER, CMD_EXECUTE_SCRIPT, 19,
SCRIPT_JT2_SENDCMD, ETAP_FASTDATA,
SCRIPT_JT2_XFRFASTDAT_LIT,
0x00, 0x00,
0x08, 0x00, // GET_CRC
SCRIPT_JT2_XFRFASTDAT_LIT,
(unsigned char) start,
(unsigned char) (start >> 8),
(unsigned char) (start >> 16),
(unsigned char) (start >> 24),
SCRIPT_JT2_XFRFASTDAT_LIT,
(unsigned char) nbytes,
(unsigned char) (nbytes >> 8),
(unsigned char) (nbytes >> 16),
(unsigned char) (nbytes >> 24),
SCRIPT_JT2_GET_PE_RESP,
SCRIPT_JT2_GET_PE_RESP);
check_timeout(a, "GET_CRC");
pickit_send(a, 1, CMD_UPLOAD_DATA);
pickit_recv(a);
if (a->reply[3] != 8 || a->reply[1] != 0) { // response code 0 = success
return 0;
}
int crc = a->reply[5] | (a->reply[6] << 8);
return crc;
}
#endif
static void pickit_finish(pickit_adapter_t *a, int power_on)
{
/* Exit programming mode. */
pickit_send(a, 18, CMD_CLEAR_UPLOAD_BUFFER, CMD_EXECUTE_SCRIPT, 15,
SCRIPT_JT2_SETMODE, 5, 0x1f,
SCRIPT_VPP_OFF,
SCRIPT_MCLR_GND_ON,
SCRIPT_VPP_PWM_OFF,
SCRIPT_SET_ICSP_PINS, 6, // set PGC high, PGD input
SCRIPT_SET_ICSP_PINS, 2, // set PGC low, PGD input
SCRIPT_SET_ICSP_PINS, 3, // set PGC and PGD as input
SCRIPT_DELAY_LONG, 10, // 50 msec
SCRIPT_BUSY_LED_OFF);
if (! power_on) {
/* Detach power from the board. */
pickit_send(a, 4, CMD_EXECUTE_SCRIPT, 2,
SCRIPT_VDD_OFF,
SCRIPT_VDD_GND_ON);
}
/* Disable reset. */
pickit_send(a, 3, CMD_EXECUTE_SCRIPT, 1,
SCRIPT_MCLR_GND_OFF);
/* Read board status. */
check_timeout(a, "finish");
}
static void pickit_close(adapter_t *adapter, int power_on)
{
pickit_adapter_t *a = (pickit_adapter_t*) adapter;
//fprintf(stderr, "%s: close\n", a->name);
pickit_finish(a, power_on);
free(a);
}
/*
* Read the Device Identification code
*/
static unsigned pickit_get_idcode(adapter_t *adapter)
{
pickit_adapter_t *a = (pickit_adapter_t*) adapter;
unsigned idcode;
/* Read device id. */
pickit_send(a, 12, CMD_CLEAR_UPLOAD_BUFFER, CMD_EXECUTE_SCRIPT, 9,
SCRIPT_JT2_SENDCMD, TAP_SW_MTAP,
SCRIPT_JT2_SENDCMD, MTAP_IDCODE,
SCRIPT_JT2_XFERDATA32_LIT, 0, 0, 0, 0);
pickit_send(a, 1, CMD_UPLOAD_DATA);
pickit_recv(a);
//fprintf(stderr, "%s: read id, %d bytes: %02x %02x %02x %02x\n", a->name,
// a->reply[0], a->reply[1], a->reply[2], a->reply[3], a->reply[4]);
if (a->reply[0] != 4)
return 0;
idcode = a->reply[1] | a->reply[2] << 8 | a->reply[3] << 16 | a->reply[4] << 24;
return idcode;
}
/*
* Read a word from memory (without PE).
*/
static unsigned pickit_read_word(adapter_t *adapter, unsigned addr)
{
pickit_adapter_t *a = (pickit_adapter_t*) adapter;
unsigned times = (a->serial_execution_mode)?0:1;
serial_execution(a);
unsigned word1 = 0;
unsigned word2 = 0;
unsigned addr_lo = addr & 0xFFFF;
unsigned addr_hi = (addr >> 16) & 0xFFFF;
unsigned value = 0;
do{
if (FAMILY_MX1 == a->adapter.family_name_short
|| FAMILY_MX3 == a->adapter.family_name_short
|| FAMILY_MK == a->adapter.family_name_short
|| FAMILY_MZ == a->adapter.family_name_short
)
{
pickit_send(a, 49, CMD_CLEAR_DOWNLOAD_BUFFER,
CMD_CLEAR_UPLOAD_BUFFER,
CMD_DOWNLOAD_DATA, 24,
WORD_AS_BYTES(0x3c13ff20), // lui s3, 0xFF20
WORD_AS_BYTES(0x3c080000 | addr_hi), // lui t0, addr_hi
WORD_AS_BYTES(0x35080000 | addr_lo), // ori t0, addr_lo
WORD_AS_BYTES(0x8d090000), // lw t1, 0(t0)
WORD_AS_BYTES(0xae690000), // sw t1, 0(s3)
WORD_AS_BYTES(0), // nop
CMD_EXECUTE_SCRIPT, 18,
SCRIPT_JT2_SENDCMD, TAP_SW_ETAP,
SCRIPT_JT2_SETMODE, 6, 0x1F,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_SENDCMD, ETAP_FASTDATA, // read FastData
SCRIPT_JT2_XFERDATA32_LIT, 0, 0, 0, 0,
CMD_UPLOAD_DATA);
pickit_recv(a);
if (a->reply[0] != 4) {
fprintf(stderr, "%s: read word %08x: bad reply length=%u\n",
a->name, addr, a->reply[0]);
exit(-1);
}
word1 = a->reply[1] | (a->reply[2] << 8) |
(a->reply[3] << 16) | (a->reply[4] << 24);
pickit_send(a, 54, CMD_CLEAR_DOWNLOAD_BUFFER,
CMD_CLEAR_UPLOAD_BUFFER,
CMD_DOWNLOAD_DATA, 28,
WORD_AS_BYTES(0x3c13ff20), // lui s3, 0xFF20
WORD_AS_BYTES(0x3c080000 | addr_hi), // lui t0, addr_hi
WORD_AS_BYTES(0x35080000 | addr_lo), // ori t0, addr_lo
WORD_AS_BYTES(0x8d090000), // lw t1, 0(t0)
WORD_AS_BYTES(0x00094842), // srl t1, 1
WORD_AS_BYTES(0xae690004), // sw t1, 4(s3)
WORD_AS_BYTES(0), // nop
CMD_EXECUTE_SCRIPT, 19,
SCRIPT_JT2_SENDCMD, TAP_SW_ETAP,
SCRIPT_JT2_SETMODE, 6, 0x1F,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_SENDCMD, ETAP_FASTDATA, // read FastData
SCRIPT_JT2_XFERDATA32_LIT, 0, 0, 0, 0,
CMD_UPLOAD_DATA);
pickit_recv(a);
word2 = a->reply[1] | (a->reply[2] << 8) |
(a->reply[3] << 16) | (a->reply[4] << 24);
/* First word of reply contains 31 bits of value in MSB; LSB bit is garbage.
* Second word contains the MSB bit of the value. */
value = (word1 >> 1) | (word2 & 0x80000000);
}
else{
/* Else PIC32MM family */
/* Like above, get 31 bits, and then the one MSB separately.
* Issue could be how Pickit code transfers things - there is 1 CLK
* per packet (fastdata?) missing compared to reference implementation */
pickit_send(a, 54, CMD_CLEAR_DOWNLOAD_BUFFER,
CMD_CLEAR_UPLOAD_BUFFER,
CMD_DOWNLOAD_DATA, 28,
WORD_AS_BYTES(0xFF2041B3), // lui s3, FAST_DATA_REG(32:16). Set address of fastdata register
WORD_AS_BYTES(0x000041A8 | (addr_hi<<16)), // lui t0, DATA_ADDRESS(31:16)
WORD_AS_BYTES(0x00005108 | (addr_lo<<16)), // ori t0, DATA_ADDRESS(15:0)
WORD_AS_BYTES(0x0000FD28), // lw t1, 0(t0) - read data
WORD_AS_BYTES(0x0000F933), // sw t1, 0(s3) - store data to fast register
WORD_AS_BYTES(0x0c000c00), // nop
WORD_AS_BYTES(0x0c000c00), // nop
CMD_EXECUTE_SCRIPT, 19,
SCRIPT_JT2_SENDCMD, TAP_SW_ETAP,
SCRIPT_JT2_SETMODE, 6, 0x1F,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_SENDCMD, ETAP_FASTDATA, // read FastData
SCRIPT_JT2_XFERDATA32_LIT, 0, 0, 0, 0,
CMD_UPLOAD_DATA);
pickit_recv(a);
if (a->reply[0] != 4) {
fprintf(stderr, "%s: read word %08x: bad reply length=%u\n",
a->name, addr, a->reply[0]);
exit(-1);
}
word1 = a->reply[1] | (a->reply[2] << 8) |
(a->reply[3] << 16) | (a->reply[4] << 24);
pickit_send(a, 59, CMD_CLEAR_DOWNLOAD_BUFFER,
CMD_CLEAR_UPLOAD_BUFFER,
CMD_DOWNLOAD_DATA, 32,
WORD_AS_BYTES(0xFF2041B3), // lui s3, FAST_DATA_REG(32:16). Set address of fastdata register
WORD_AS_BYTES(0x000041A8 | (addr_hi<<16)), // lui t0, DATA_ADDRESS(31:16)
WORD_AS_BYTES(0x00005108 | (addr_lo<<16)), // ori t0, DATA_ADDRESS(15:0)
WORD_AS_BYTES(0x0000FD28), // lw t1, 0(t0) - read data
WORD_AS_BYTES(0x08400129), // srl t1, 1
WORD_AS_BYTES(0x0000F933), // sw t1, 0(s3) - store data to fast register
WORD_AS_BYTES(0x0c000c00), // nop
WORD_AS_BYTES(0x0c000c00), // nop
CMD_EXECUTE_SCRIPT, 20,
SCRIPT_JT2_SENDCMD, TAP_SW_ETAP,
SCRIPT_JT2_SETMODE, 6, 0x1F,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_XFERINST_BUF,
SCRIPT_JT2_SENDCMD, ETAP_FASTDATA, // read FastData
SCRIPT_JT2_XFERDATA32_LIT, 0, 0, 0, 0,
CMD_UPLOAD_DATA);
pickit_recv(a);
if (a->reply[0] != 4) {
fprintf(stderr, "%s: read word %08x: bad reply length=%u\n",
a->name, addr, a->reply[0]);
exit(-1);
}
word2 = a->reply[1] | (a->reply[2] << 8) |
(a->reply[3] << 16) | (a->reply[4] << 24);
value = (word1 >> 1) | (word2 & 0x80000000);
}
}while(times-- > 0);
if (a->reply[0] != 4) {
fprintf(stderr, "%s: read word %08x: bad reply length=%u\n",
a->name, addr, a->reply[0]);
exit(-1);
}
if (debug_level > 0)
fprintf(stderr, "%s: %08x -> %08x\n", __func__, addr, value);
return value;
}
/*
* Read a block of memory, multiple of 1 kbyte.
*/
static void pickit_read_data(adapter_t *adapter,
unsigned addr, unsigned nwords, unsigned *data)
{
pickit_adapter_t *a = (pickit_adapter_t*) adapter;
unsigned char buf [64];
unsigned words_read;
//fprintf(stderr, "%s: read %d bytes from %08x\n", a->name, nwords*4, addr);
if (! a->use_executive) {
/* Without PE. */
for (; nwords > 0; nwords--) {
*data++ = pickit_read_word(adapter, addr);
addr += 4;
}
return;
}
/* Use PE to read memory. */
for (words_read = 0; words_read < nwords; ) {
/* Download addresses for 8 script runs. */
unsigned i, k = 0;
memset(buf, CMD_END_OF_BUFFER, 64);
buf[k++] = CMD_CLEAR_DOWNLOAD_BUFFER;
buf[k++] = CMD_DOWNLOAD_DATA;
buf[k++] = 8 * 4;
for (i = 0; i < 8; i++) {
unsigned address = addr + words_read*4 + i*32*4;
buf[k++] = address;
buf[k++] = address >> 8;
buf[k++] = address >> 16;
buf[k++] = address >> 24;
}
pickit_send_buf(a, buf, k);
for (k = 0; k < 8; k++) {
/* Read progmem. */
pickit_send(a, 17, CMD_CLEAR_UPLOAD_BUFFER,
CMD_EXECUTE_SCRIPT, 13,
SCRIPT_JT2_SENDCMD, ETAP_FASTDATA,
SCRIPT_JT2_XFRFASTDAT_LIT,
0x20, 0, 1, 0, // READ
SCRIPT_JT2_XFRFASTDAT_BUF,
SCRIPT_JT2_WAIT_PE_RESP,
SCRIPT_JT2_GET_PE_RESP,
SCRIPT_LOOP, 1, 31,
CMD_UPLOAD_DATA_NOLEN);
pickit_recv(a);
memcpy(data, a->reply, 64);
//fprintf(stderr, " ...%08x...\n", data[0]);
data += 64/4;
words_read += 64/4;
/* Get second half of upload buffer. */
pickit_send(a, 1, CMD_UPLOAD_DATA_NOLEN);
pickit_recv(a);
memcpy(data, a->reply, 64);
data += 64/4;
words_read += 64/4;
}
}
}
/*
* Put data to download buffer.
* Max 15 words (60 bytes).
*/
static void download_data(pickit_adapter_t *a,
unsigned *data, unsigned nwords, int clear_flag)
{
unsigned char buf [64];
unsigned i, k = 0;
memset(buf, CMD_END_OF_BUFFER, 64);
if (clear_flag)
buf[k++] = CMD_CLEAR_DOWNLOAD_BUFFER;
buf[k++] = CMD_DOWNLOAD_DATA;
buf[k++] = nwords * 4;
for (i=0; i<nwords; i++) {
unsigned word = *data++;
buf[k++] = word;
buf[k++] = word >> 8;
buf[k++] = word >> 16;
buf[k++] = word >> 24;
}
pickit_send_buf(a, buf, k);
}
/*
* Write a word to flash memory.
*/
static void pickit_program_word(adapter_t *adapter,
unsigned addr, unsigned word)
{
pickit_adapter_t *a = (pickit_adapter_t*) adapter;
if (debug_level > 0)
fprintf(stderr, "%s: program word at %08x: %08x\n", a->name, addr, word);
if (! a->use_executive) {
/* Without PE. */
fprintf(stderr, "%s: slow flash write not implemented yet.\n", a->name);
exit(-1);
}
/* Use PE to write flash memory. */
pickit_send(a, 22, CMD_CLEAR_UPLOAD_BUFFER,
CMD_EXECUTE_SCRIPT, 18,
SCRIPT_JT2_SENDCMD, ETAP_FASTDATA,
SCRIPT_JT2_XFRFASTDAT_LIT,
2, 0, 3, 0, // WORD_PROGRAM
SCRIPT_JT2_XFRFASTDAT_LIT,
(unsigned char) addr,
(unsigned char) (addr >> 8),
(unsigned char) (addr >> 16),
(unsigned char) (addr >> 24),
SCRIPT_JT2_XFRFASTDAT_LIT,
(unsigned char) word,
(unsigned char) (word >> 8),
(unsigned char) (word >> 16),
(unsigned char) (word >> 24),
SCRIPT_JT2_GET_PE_RESP,
CMD_UPLOAD_DATA);
pickit_recv(a);
//fprintf(stderr, "%s: word program PE response %u bytes: %02x...\n",
// a->name, a->reply[0], a->reply[1]);
if (a->reply[0] != 4 || a->reply[1] != 0) { // response code 0 = success
fprintf(stderr, "%s: failed to program word %08x at %08x, reply = %02x-%02x-%02x-%02x-%02x\n",
a->name, word, addr, a->reply[0], a->reply[1], a->reply[2], a->reply[3], a->reply[4]);
exit(-1);
}
}
/*
* Write two words to flash memory.
*/
static void pickit_program_double_word(adapter_t *adapter,
unsigned addr, unsigned word0, unsigned word1)
{
pickit_adapter_t *a = (pickit_adapter_t*) adapter;
if (debug_level > 0)
fprintf(stderr, "%s: program words at %08x: %08x %08x\n", a->name, addr, word0, word1);
if (! a->use_executive) {
/* Without PE. */
fprintf(stderr, "%s: slow flash write not implemented yet.\n", a->name);
exit(-1);
}
/* Use PE to write flash memory. */
pickit_send(a, 27, CMD_CLEAR_UPLOAD_BUFFER,
CMD_EXECUTE_SCRIPT, 23,
SCRIPT_JT2_SENDCMD, ETAP_FASTDATA,
SCRIPT_JT2_XFRFASTDAT_LIT,
0, 0, PE_DOUBLE_WORD_PGRM, 0,
SCRIPT_JT2_XFRFASTDAT_LIT,
(unsigned char) addr,
(unsigned char) (addr >> 8),
(unsigned char) (addr >> 16),
(unsigned char) (addr >> 24),
SCRIPT_JT2_XFRFASTDAT_LIT,
(unsigned char) word0,
(unsigned char) (word0 >> 8),
(unsigned char) (word0 >> 16),
(unsigned char) (word0 >> 24),
SCRIPT_JT2_XFRFASTDAT_LIT,
(unsigned char) word1,
(unsigned char) (word1 >> 8),
(unsigned char) (word1 >> 16),
(unsigned char) (word1 >> 24),
SCRIPT_JT2_GET_PE_RESP,
CMD_UPLOAD_DATA);
pickit_recv(a);
//fprintf(stderr, "%s: word program PE response %u bytes: %02x...\n",
// a->name, a->reply[0], a->reply[1]);
if (a->reply[0] != 4 || a->reply[1] != 0) { // response code 0 = success
fprintf(stderr, "%s: failed to program words %08x %08x at %08x, reply = %02x-%02x-%02x-%02x-%02x\n",
a->name, word0, word1, addr, a->reply[0], a->reply[1], a->reply[2], a->reply[3], a->reply[4]);
exit(-1);
}
}
/*
* Write 4 words to flash memory.
*/
static void pickit_program_quad_word(adapter_t *adapter, unsigned addr,
unsigned word0, unsigned word1, unsigned word2, unsigned word3)
{
pickit_adapter_t *a = (pickit_adapter_t*) adapter;
if (debug_level > 0)
fprintf(stderr, "%s: program quad word at %08x: %08x-%08x-%08x-%08x\n",
a->name, addr, word0, word1, word2, word3);
if (! a->use_executive) {
/* Without PE. */
fprintf(stderr, "%s: slow flash write not implemented yet.\n", a->name);
exit(-1);
}
/* Use PE to write flash memory. */
pickit_send(a, 37, CMD_CLEAR_UPLOAD_BUFFER,
CMD_EXECUTE_SCRIPT, 33,
SCRIPT_JT2_SENDCMD, ETAP_FASTDATA,
SCRIPT_JT2_XFRFASTDAT_LIT,
0, 0, 13, 0, // QUAD_WORD_PROGRAM
SCRIPT_JT2_XFRFASTDAT_LIT,
(unsigned char) addr,
(unsigned char) (addr >> 8),
(unsigned char) (addr >> 16),
(unsigned char) (addr >> 24),
SCRIPT_JT2_XFRFASTDAT_LIT,
(unsigned char) word0,
(unsigned char) (word0 >> 8),
(unsigned char) (word0 >> 16),
(unsigned char) (word0 >> 24),
SCRIPT_JT2_XFRFASTDAT_LIT,
(unsigned char) word1,
(unsigned char) (word1 >> 8),
(unsigned char) (word1 >> 16),
(unsigned char) (word1 >> 24),
SCRIPT_JT2_XFRFASTDAT_LIT,
(unsigned char) word2,
(unsigned char) (word2 >> 8),
(unsigned char) (word2 >> 16),
(unsigned char) (word2 >> 24),
SCRIPT_JT2_XFRFASTDAT_LIT,
(unsigned char) word3,
(unsigned char) (word3 >> 8),
(unsigned char) (word3 >> 16),
(unsigned char) (word3 >> 24),
SCRIPT_JT2_GET_PE_RESP,
CMD_UPLOAD_DATA);
pickit_recv(a);
//fprintf(stderr, "%s: word program PE response %u bytes: %02x...\n",
// a->name, a->reply[0], a->reply[1]);
if (a->reply[0] != 4 || a->reply[1] != 0) { // response code 0 = success
fprintf(stderr, "%s: failed to program quad word at %08x, reply = %02x-%02x-%02x-%02x-%02x\n",
a->name, addr, a->reply[0], a->reply[1], a->reply[2], a->reply[3], a->reply[4]);
exit(-1);
}
}
/*
* Flash write row of memory.
*/
static void pickit_program_row(adapter_t *adapter, unsigned addr,
unsigned *data, unsigned words_per_row)
{
pickit_adapter_t *a = (pickit_adapter_t*) adapter;
unsigned i;
if (debug_level > 0)
fprintf(stderr, "%s: row program %u words at %08x\n",
a->name, words_per_row, addr);
if (! a->use_executive) {
/* Without PE. */
fprintf(stderr, "%s: slow flash write not implemented yet.\n", a->name);
exit(-1);
}
/* Use PE to write flash memory. */
pickit_send(a, 15, CMD_CLEAR_UPLOAD_BUFFER,
CMD_EXECUTE_SCRIPT, 12,
SCRIPT_JT2_SENDCMD, ETAP_FASTDATA,
SCRIPT_JT2_XFRFASTDAT_LIT,
words_per_row, 0, 0, 0, // PROGRAM ROW
SCRIPT_JT2_XFRFASTDAT_LIT,
(unsigned char) addr,
(unsigned char) (addr >> 8),
(unsigned char) (addr >> 16),
(unsigned char) (addr >> 24));
/* Download data. */
if (words_per_row == 32) {
/* MX1/2 family. */
download_data(a, data, 15, 1);
download_data(a, data+15, 15, 0);
pickit_send(a, 18,
CMD_DOWNLOAD_DATA, 2*4,
WORD_AS_BYTES(data[30]),
WORD_AS_BYTES(data[31]),
CMD_EXECUTE_SCRIPT, 6, // execute
SCRIPT_JT2_SENDCMD, ETAP_FASTDATA,
SCRIPT_JT2_XFRFASTDAT_BUF,
SCRIPT_LOOP, 1, 31);
} else {
/* MX3/4/5/6/7 or MZ family. */
for (i = 0; i < words_per_row/64; i++) {
/* Download 256 bytes of data. */
download_data(a, data, 15, 1);
download_data(a, data+15, 15, 0);
download_data(a, data+30, 15, 0);
download_data(a, data+45, 15, 0);
pickit_send(a, 26,
CMD_DOWNLOAD_DATA, 4*4,
WORD_AS_BYTES(data[60]),
WORD_AS_BYTES(data[61]),
WORD_AS_BYTES(data[62]),
WORD_AS_BYTES(data[63]),
CMD_EXECUTE_SCRIPT, 6, // execute
SCRIPT_JT2_SENDCMD, ETAP_FASTDATA,
SCRIPT_JT2_XFRFASTDAT_BUF,
SCRIPT_LOOP, 1, 63);
data += 64;
}
}