Select one of the symbols to view example projects that use it.
 
Outline
#include "config.h"
#include <jtag/adapter.h>
#include <jtag/interface.h>
#include <jtag/swd.h>
#include <transport/transport.h>
#include <helper/time_support.h>
#include <helper/log.h>
#include <helper/nvp.h>
#include <windows.h>
#include <assert.h>
#include "mpsse.h"
#define JTAG_MODE
#define JTAG_MODE_ALT
#define SWD_MODE
ftdi_device_desc
ftdi_channel
ftdi_jtag_mode
swd_mode
#define MAX_USB_IDS
ftdi_vid
ftdi_pid
mpsse_ctx
signal
signals
swd_cmd_queue
swd_cmd_queue_length
swd_cmd_queue_alloced
queued_retval
freq
output
direction
jtag_output_init
jtag_direction_init
find_signal_by_name(const char *)
create_signal(const char *)
ftdi_set_signal(const struct signal *, char)
ftdi_get_signal(const struct signal *, uint16_t *)
move_to_state(tap_state_t)
ftdi_speed(int)
ftdi_speed_div(int, int *)
ftdi_khz(int, int *)
ftdi_end_state(tap_state_t)
ftdi_execute_runtest(struct jtag_command *)
ftdi_execute_statemove(struct jtag_command *)
ftdi_execute_tms(struct jtag_command *)
ftdi_execute_pathmove(struct jtag_command *)
ftdi_execute_scan(struct jtag_command *)
ftdi_reset(int, int)
ftdi_execute_sleep(struct jtag_command *)
ftdi_execute_stableclocks(struct jtag_command *)
ftdi_execute_command(struct jtag_command *)
ftdi_execute_queue(struct jtag_command *)
ftdi_initialize()
ftdi_quit()
ftdi_handle_device_desc_command(struct command_invocation *)
ftdi_handle_channel_command(struct command_invocation *)
ftdi_handle_layout_init_command(struct command_invocation *)
ftdi_handle_layout_signal_command(struct command_invocation *)
ftdi_handle_set_signal_command(struct command_invocation *)
ftdi_handle_get_signal_command(struct command_invocation *)
ftdi_handle_vid_pid_command(struct command_invocation *)
ftdi_handle_tdo_sample_edge_command(struct command_invocation *)
ftdi_subcommand_handlers
ftdi_command_handlers
create_default_signal(const char *, uint16_t)
create_signals()
ftdi_swd_init()
ftdi_swd_swdio_en(bool)
ftdi_swd_run_queue()
ftdi_swd_queue_cmd(uint8_t, uint32_t *, uint32_t, uint32_t)
ftdi_swd_read_reg(uint8_t, uint32_t *, uint32_t)
ftdi_swd_write_reg(uint8_t, uint32_t, uint32_t)
ftdi_swd_switch_seq(enum swd_special_seq)
ftdi_swd
ftdi_transports
ftdi_interface
ftdi_adapter_driver
Files
loading...
SourceVuDevelopment ToolsOpenOCDsrc/jtag/drivers/ftdi.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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
// SPDX-License-Identifier: GPL-2.0-or-later /************************************************************************** * Copyright (C) 2012 by Andreas Fritiofson * * andreas.fritiofson@gmail.com * ***************************************************************************//* ... */ /** * @file * JTAG adapters based on the FT2232 full and high speed USB parts are * popular low cost JTAG debug solutions. Many FT2232 based JTAG adapters * are discrete, but development boards may integrate them as alternatives * to more capable (and expensive) third party JTAG pods. * * JTAG uses only one of the two communications channels ("MPSSE engines") * on these devices. Adapters based on FT4232 parts have four ports/channels * (A/B/C/D), instead of just two (A/B). * * Especially on development boards integrating one of these chips (as * opposed to discrete pods/dongles), the additional channels can be used * for a variety of purposes, but OpenOCD only uses one channel at a time. * * - As a USB-to-serial adapter for the target's console UART ... * which may be able to support ROM boot loaders that load initial * firmware images to flash (or SRAM). * * - On systems which support ARM's SWD in addition to JTAG, or instead * of it, that second port can be used for reading SWV/SWO trace data. * * - Additional JTAG links, e.g. to a CPLD or * FPGA. * * FT2232 based JTAG adapters are "dumb" not "smart", because most JTAG * request/response interactions involve round trips over the USB link. * A "smart" JTAG adapter has intelligence close to the scan chain, so it * can for example poll quickly for a status change (usually taking on the * order of microseconds not milliseconds) before beginning a queued * transaction which require the previous one to have completed. * * There are dozens of adapters of this type, differing in details which * this driver needs to understand. Those "layout" details are required * as part of FT2232 driver configuration. * * This code uses information contained in the MPSSE specification which was * found here: * https://www.ftdichip.com/Support/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf * Hereafter this is called the "MPSSE Spec". * * The datasheet for the ftdichip.com's FT2232H part is here: * https://www.ftdichip.com/Support/Documents/DataSheets/ICs/DS_FT2232H.pdf * * Also note the issue with code 0x4b (clock data to TMS) noted in * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html * which can affect longer JTAG state paths. *//* ... */ #ifdef HAVE_CONFIG_H #include "config.h" #endif /* project specific includes */ #include <jtag/adapter.h> #include <jtag/interface.h> #include <jtag/swd.h> #include <transport/transport.h> #include <helper/time_support.h> #include <helper/log.h> #include <helper/nvp.h> 7 includes #if IS_CYGWIN == 1 #include <windows.h> #endif #include <assert.h> /* FTDI access library includes */ #include "mpsse.h" #define JTAG_MODE (LSB_FIRST | POS_EDGE_IN | NEG_EDGE_OUT) #define JTAG_MODE_ALT (LSB_FIRST | NEG_EDGE_IN | NEG_EDGE_OUT) #define SWD_MODE (LSB_FIRST | POS_EDGE_IN | NEG_EDGE_OUT) static char *ftdi_device_desc; static uint8_t ftdi_channel; static uint8_t ftdi_jtag_mode = JTAG_MODE; static bool swd_mode; #define MAX_USB_IDS 8 /* vid = pid = 0 marks the end of the list */ static uint16_t ftdi_vid[MAX_USB_IDS + 1] = { 0 }; static uint16_t ftdi_pid[MAX_USB_IDS + 1] = { 0 }; static struct mpsse_ctx *mpsse_ctx; struct signal { const char *name; uint16_t data_mask; uint16_t input_mask; uint16_t oe_mask; bool invert_data; bool invert_input; bool invert_oe; struct signal *next; ...}; static struct signal *signals; /* FIXME: Where to store per-instance data? We need an SWD context. */ static struct swd_cmd_queue_entry { uint8_t cmd; uint32_t *dst; uint8_t trn_ack_data_parity_trn[DIV_ROUND_UP(4 + 3 + 32 + 1 + 4, 8)]; ...} *swd_cmd_queue; static size_t swd_cmd_queue_length; static size_t swd_cmd_queue_alloced; static int queued_retval; static int freq; static uint16_t output; static uint16_t direction; static uint16_t jtag_output_init; static uint16_t jtag_direction_init; static int ftdi_swd_switch_seq(enum swd_special_seq seq); static struct signal *find_signal_by_name(const char *name) { for (struct signal *sig = signals; sig; sig = sig->next) { if (strcmp(name, sig->name) == 0) return sig; }for (struct signal *sig = signals; sig; sig = sig->next) { ... } return NULL; }{ ... } static struct signal *create_signal(const char *name) { struct signal **psig = &signals; while (*psig) psig = &(*psig)->next; *psig = calloc(1, sizeof(**psig)); if (!*psig) return NULL; (*psig)->name = strdup(name); if (!(*psig)->name) { free(*psig); *psig = NULL; }if (!(*psig)->name) { ... } return *psig; }{ ... } static int ftdi_set_signal(const struct signal *s, char value) { bool data; bool oe; if (s->data_mask == 0 && s->oe_mask == 0) { LOG_ERROR("interface doesn't provide signal '%s'", s->name); return ERROR_FAIL; }if (s->data_mask == 0 && s->oe_mask == 0) { ... } switch (value) { case '0': data = s->invert_data; oe = !s->invert_oe; break;case '0': case '1': if (s->data_mask == 0) { LOG_ERROR("interface can't drive '%s' high", s->name); return ERROR_FAIL; }if (s->data_mask == 0) { ... } data = !s->invert_data; oe = !s->invert_oe; break;case '1': case 'z': case 'Z': if (s->oe_mask == 0) { LOG_ERROR("interface can't tri-state '%s'", s->name); return ERROR_FAIL; }if (s->oe_mask == 0) { ... } data = s->invert_data; oe = s->invert_oe; break;case 'Z': default: LOG_ERROR("invalid signal level specifier \'%c\'(0x%02x)", value, value); return ERROR_FAIL;default }switch (value) { ... } uint16_t old_output = output; uint16_t old_direction = direction; output = data ? output | s->data_mask : output & ~s->data_mask; if (s->oe_mask == s->data_mask) direction = oe ? direction | s->oe_mask : direction & ~s->oe_mask; else output = oe ? output | s->oe_mask : output & ~s->oe_mask; if ((output & 0xff) != (old_output & 0xff) || (direction & 0xff) != (old_direction & 0xff)) mpsse_set_data_bits_low_byte(mpsse_ctx, output & 0xff, direction & 0xff); if ((output >> 8 != old_output >> 8) || (direction >> 8 != old_direction >> 8)) mpsse_set_data_bits_high_byte(mpsse_ctx, output >> 8, direction >> 8); return ERROR_OK; }{ ... } static int ftdi_get_signal(const struct signal *s, uint16_t *value_out) { uint8_t data_low = 0; uint8_t data_high = 0; if (s->input_mask == 0) { LOG_ERROR("interface doesn't provide signal '%s'", s->name); return ERROR_FAIL; }if (s->input_mask == 0) { ... } if (s->input_mask & 0xff) mpsse_read_data_bits_low_byte(mpsse_ctx, &data_low); if (s->input_mask >> 8) mpsse_read_data_bits_high_byte(mpsse_ctx, &data_high); mpsse_flush(mpsse_ctx); *value_out = (((uint16_t)data_high) << 8) | data_low; if (s->invert_input) *value_out = ~(*value_out); *value_out &= s->input_mask; return ERROR_OK; }{ ... } /** * Function move_to_state * moves the TAP controller from the current state to a * \a goal_state through a path given by tap_get_tms_path(). State transition * logging is performed by delegation to clock_tms(). * * @param goal_state is the destination state for the move. *//* ... */ static void move_to_state(tap_state_t goal_state) { tap_state_t start_state = tap_get_state(); /* goal_state is 1/2 of a tuple/pair of states which allow convenient lookup of the required TMS pattern to move to this state from the start state. *//* ... */ /* do the 2 lookups */ uint8_t tms_bits = tap_get_tms_path(start_state, goal_state); int tms_count = tap_get_tms_path_len(start_state, goal_state); assert(tms_count <= 8); LOG_DEBUG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state)); /* Track state transitions step by step */ for (int i = 0; i < tms_count; i++) tap_set_state(tap_state_transition(tap_get_state(), (tms_bits >> i) & 1)); mpsse_clock_tms_cs_out(mpsse_ctx, &tms_bits, 0, tms_count, false, ftdi_jtag_mode); }{ ... } static int ftdi_speed(int speed) { int retval; retval = mpsse_set_frequency(mpsse_ctx, speed); if (retval < 0) { LOG_ERROR("couldn't set FTDI TCK speed"); return retval; }if (retval < 0) { ... } if (!swd_mode && speed >= 10000000 && ftdi_jtag_mode != JTAG_MODE_ALT) LOG_INFO("ftdi: if you experience problems at higher adapter clocks, try " "the command \"ftdi tdo_sample_edge falling\""); return ERROR_OK; }{ ... } static int ftdi_speed_div(int speed, int *khz) { *khz = speed / 1000; return ERROR_OK; }{ ... } static int ftdi_khz(int khz, int *jtag_speed) { if (khz == 0 && !mpsse_is_high_speed(mpsse_ctx)) { LOG_DEBUG("RCLK not supported"); return ERROR_FAIL; }if (khz == 0 && !mpsse_is_high_speed(mpsse_ctx)) { ... } *jtag_speed = khz * 1000; return ERROR_OK; }{ ... } static void ftdi_end_state(tap_state_t state) { if (tap_is_state_stable(state)) tap_set_end_state(state); else { LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state)); exit(-1); }else { ... } }{ ... } static void ftdi_execute_runtest(struct jtag_command *cmd) { uint8_t zero = 0; LOG_DEBUG_IO("runtest %u cycles, end in %s", cmd->cmd.runtest->num_cycles, tap_state_name(cmd->cmd.runtest->end_state)); if (tap_get_state() != TAP_IDLE) move_to_state(TAP_IDLE); /* TODO: Reuse ftdi_execute_stableclocks */ unsigned int i = cmd->cmd.runtest->num_cycles; while (i > 0) { /* there are no state transitions in this code, so omit state tracking */ unsigned this_len = i > 7 ? 7 : i; mpsse_clock_tms_cs_out(mpsse_ctx, &zero, 0, this_len, false, ftdi_jtag_mode); i -= this_len; }while (i > 0) { ... } ftdi_end_state(cmd->cmd.runtest->end_state); if (tap_get_state() != tap_get_end_state()) move_to_state(tap_get_end_state()); LOG_DEBUG_IO("runtest: %u, end in %s", cmd->cmd.runtest->num_cycles, tap_state_name(tap_get_end_state())); }{ ... } static void ftdi_execute_statemove(struct jtag_command *cmd) { LOG_DEBUG_IO("statemove end in %s", tap_state_name(cmd->cmd.statemove->end_state)); ftdi_end_state(cmd->cmd.statemove->end_state); /* shortest-path move to desired end state */ if (tap_get_state() != tap_get_end_state() || tap_get_end_state() == TAP_RESET) move_to_state(tap_get_end_state()); }{ ... } /** * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG * (or SWD) state machine. REVISIT: Not the best method, perhaps. *//* ... */ static void ftdi_execute_tms(struct jtag_command *cmd) { LOG_DEBUG_IO("TMS: %u bits", cmd->cmd.tms->num_bits); /* TODO: Missing tap state tracking, also missing from ft2232.c! */ mpsse_clock_tms_cs_out(mpsse_ctx, cmd->cmd.tms->bits, 0, cmd->cmd.tms->num_bits, false, ftdi_jtag_mode); }{ ... } static void ftdi_execute_pathmove(struct jtag_command *cmd) { tap_state_t *path = cmd->cmd.pathmove->path; unsigned int num_states = cmd->cmd.pathmove->num_states; LOG_DEBUG_IO("pathmove: %u states, current: %s end: %s", num_states, tap_state_name(tap_get_state()), tap_state_name(path[num_states-1])); int state_count = 0; unsigned bit_count = 0; uint8_t tms_byte = 0; LOG_DEBUG_IO("-"); /* this loop verifies that the path is legal and logs each state in the path */ while (num_states--) { /* either TMS=0 or TMS=1 must work ... */ if (tap_state_transition(tap_get_state(), false) == path[state_count]) buf_set_u32(&tms_byte, bit_count++, 1, 0x0); else if (tap_state_transition(tap_get_state(), true) == path[state_count]) { buf_set_u32(&tms_byte, bit_count++, 1, 0x1); /* ... or else the caller goofed BADLY */ }else if (tap_state_transition(tap_get_state(), true) == path[state_count]) { ... } else { LOG_ERROR("BUG: %s -> %s isn't a valid " "TAP state transition", tap_state_name(tap_get_state()), tap_state_name(path[state_count])); exit(-1); }else { ... } tap_set_state(path[state_count]); state_count++; if (bit_count == 7 || num_states == 0) { mpsse_clock_tms_cs_out(mpsse_ctx, &tms_byte, 0, bit_count, false, ftdi_jtag_mode); bit_count = 0; }if (bit_count == 7 || num_states == 0) { ... } }while (num_states--) { ... } tap_set_end_state(tap_get_state()); }{ ... } static void ftdi_execute_scan(struct jtag_command *cmd) { LOG_DEBUG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", jtag_scan_type(cmd->cmd.scan)); /* Make sure there are no trailing fields with num_bits == 0, or the logic below will fail. */ while (cmd->cmd.scan->num_fields > 0 && cmd->cmd.scan->fields[cmd->cmd.scan->num_fields - 1].num_bits == 0) { cmd->cmd.scan->num_fields--; LOG_DEBUG_IO("discarding trailing empty field"); }while (cmd->cmd.scan->num_fields > 0 && cmd->cmd.scan->fields[cmd->cmd.scan->num_fields - 1].num_bits == 0) { ... } if (!cmd->cmd.scan->num_fields) { LOG_DEBUG_IO("empty scan, doing nothing"); return; }if (!cmd->cmd.scan->num_fields) { ... } if (cmd->cmd.scan->ir_scan) { if (tap_get_state() != TAP_IRSHIFT) move_to_state(TAP_IRSHIFT); }if (cmd->cmd.scan->ir_scan) { ... } else { if (tap_get_state() != TAP_DRSHIFT) move_to_state(TAP_DRSHIFT); }else { ... } ftdi_end_state(cmd->cmd.scan->end_state); struct scan_field *field = cmd->cmd.scan->fields; unsigned scan_size = 0; for (unsigned int i = 0; i < cmd->cmd.scan->num_fields; i++, field++) { scan_size += field->num_bits; LOG_DEBUG_IO("%s%s field %u/%u %u bits", field->in_value ? "in" : "", field->out_value ? "out" : "", i, cmd->cmd.scan->num_fields, field->num_bits); if (i == cmd->cmd.scan->num_fields - 1 && tap_get_state() != tap_get_end_state()) { /* Last field, and we're leaving IRSHIFT/DRSHIFT. Clock last bit during tap * movement. This last field can't have length zero, it was checked above. *//* ... */ mpsse_clock_data(mpsse_ctx, field->out_value, 0, field->in_value, 0, field->num_bits - 1, ftdi_jtag_mode); uint8_t last_bit = 0; if (field->out_value) bit_copy(&last_bit, 0, field->out_value, field->num_bits - 1, 1); /* If endstate is TAP_IDLE, clock out 1-1-0 (->EXIT1 ->UPDATE ->IDLE) * Otherwise, clock out 1-0 (->EXIT1 ->PAUSE) *//* ... */ uint8_t tms_bits = 0x03; mpsse_clock_tms_cs(mpsse_ctx, &tms_bits, 0, field->in_value, field->num_bits - 1, 1, last_bit, ftdi_jtag_mode); tap_set_state(tap_state_transition(tap_get_state(), 1)); if (tap_get_end_state() == TAP_IDLE) { mpsse_clock_tms_cs_out(mpsse_ctx, &tms_bits, 1, 2, last_bit, ftdi_jtag_mode); tap_set_state(tap_state_transition(tap_get_state(), 1)); tap_set_state(tap_state_transition(tap_get_state(), 0)); }if (tap_get_end_state() == TAP_IDLE) { ... } else { mpsse_clock_tms_cs_out(mpsse_ctx, &tms_bits, 2, 1, last_bit, ftdi_jtag_mode); tap_set_state(tap_state_transition(tap_get_state(), 0)); }else { ... } }if (i == cmd->cmd.scan->num_fields - 1 && tap_get_state() != tap_get_end_state()) { ... } else mpsse_clock_data(mpsse_ctx, field->out_value, 0, field->in_value, 0, field->num_bits, ftdi_jtag_mode); }for (unsigned int i = 0; i < cmd->cmd.scan->num_fields; i++, field++) { ... } if (tap_get_state() != tap_get_end_state()) move_to_state(tap_get_end_state()); LOG_DEBUG_IO("%s scan, %i bits, end in %s", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, tap_state_name(tap_get_end_state())); }{ ... } static int ftdi_reset(int trst, int srst) { struct signal *sig_ntrst = find_signal_by_name("nTRST"); struct signal *sig_nsrst = find_signal_by_name("nSRST"); LOG_DEBUG_IO("reset trst: %i srst %i", trst, srst); if (!swd_mode) { if (trst == 1) { if (sig_ntrst) ftdi_set_signal(sig_ntrst, '0'); else LOG_ERROR("Can't assert TRST: nTRST signal is not defined"); }if (trst == 1) { ... } else if (sig_ntrst && jtag_get_reset_config() & RESET_HAS_TRST && trst == 0) { if (jtag_get_reset_config() & RESET_TRST_OPEN_DRAIN) ftdi_set_signal(sig_ntrst, 'z'); else ftdi_set_signal(sig_ntrst, '1'); }else if (sig_ntrst && jtag_get_reset_config() & RESET_HAS_TRST && trst == 0) { ... } }if (!swd_mode) { ... } if (srst == 1) { if (sig_nsrst) ftdi_set_signal(sig_nsrst, '0'); else LOG_ERROR("Can't assert SRST: nSRST signal is not defined"); }if (srst == 1) { ... } else if (sig_nsrst && jtag_get_reset_config() & RESET_HAS_SRST && srst == 0) { if (jtag_get_reset_config() & RESET_SRST_PUSH_PULL) ftdi_set_signal(sig_nsrst, '1'); else ftdi_set_signal(sig_nsrst, 'z'); }else if (sig_nsrst && jtag_get_reset_config() & RESET_HAS_SRST && srst == 0) { ... } return mpsse_flush(mpsse_ctx); }{ ... } static void ftdi_execute_sleep(struct jtag_command *cmd) { LOG_DEBUG_IO("sleep %" PRIu32, cmd->cmd.sleep->us); mpsse_flush(mpsse_ctx); jtag_sleep(cmd->cmd.sleep->us); LOG_DEBUG_IO("sleep %" PRIu32 " usec while in %s", cmd->cmd.sleep->us, tap_state_name(tap_get_state())); }{ ... } static void ftdi_execute_stableclocks(struct jtag_command *cmd) { /* this is only allowed while in a stable state. A check for a stable * state was done in jtag_add_clocks() *//* ... */ unsigned int num_cycles = cmd->cmd.stableclocks->num_cycles; /* 7 bits of either ones or zeros. */ uint8_t tms = tap_get_state() == TAP_RESET ? 0x7f : 0x00; /* TODO: Use mpsse_clock_data with in=out=0 for this, if TMS can be set to * the correct level and remain there during the scan *//* ... */ while (num_cycles > 0) { /* there are no state transitions in this code, so omit state tracking */ unsigned this_len = num_cycles > 7 ? 7 : num_cycles; mpsse_clock_tms_cs_out(mpsse_ctx, &tms, 0, this_len, false, ftdi_jtag_mode); num_cycles -= this_len; }while (num_cycles > 0) { ... } LOG_DEBUG_IO("clocks %u while in %s", cmd->cmd.stableclocks->num_cycles, tap_state_name(tap_get_state())); }{ ... } static void ftdi_execute_command(struct jtag_command *cmd) { switch (cmd->type) { case JTAG_RUNTEST: ftdi_execute_runtest(cmd); break;case JTAG_RUNTEST: case JTAG_TLR_RESET: ftdi_execute_statemove(cmd); break;case JTAG_TLR_RESET: case JTAG_PATHMOVE: ftdi_execute_pathmove(cmd); break;case JTAG_PATHMOVE: case JTAG_SCAN: ftdi_execute_scan(cmd); break;case JTAG_SCAN: case JTAG_SLEEP: ftdi_execute_sleep(cmd); break;case JTAG_SLEEP: case JTAG_STABLECLOCKS: ftdi_execute_stableclocks(cmd); break;case JTAG_STABLECLOCKS: case JTAG_TMS: ftdi_execute_tms(cmd); break;case JTAG_TMS: default: LOG_ERROR("BUG: unknown JTAG command type encountered: %d", cmd->type); break;default }switch (cmd->type) { ... } }{ ... } static int ftdi_execute_queue(struct jtag_command *cmd_queue) { /* blink, if the current layout has that feature */ struct signal *led = find_signal_by_name("LED"); if (led) ftdi_set_signal(led, '1'); for (struct jtag_command *cmd = cmd_queue; cmd; cmd = cmd->next) { /* fill the write buffer with the desired command */ ftdi_execute_command(cmd); }for (struct jtag_command *cmd = cmd_queue; cmd; cmd = cmd->next) { ... } if (led) ftdi_set_signal(led, '0'); int retval = mpsse_flush(mpsse_ctx); if (retval != ERROR_OK) LOG_ERROR("error while flushing MPSSE queue: %d", retval); return retval; }{ ... } static int ftdi_initialize(void) { if (tap_get_tms_path_len(TAP_IRPAUSE, TAP_IRPAUSE) == 7) LOG_DEBUG("ftdi interface using 7 step jtag state transitions"); else LOG_DEBUG("ftdi interface using shortest path jtag state transitions"); if (!ftdi_vid[0] && !ftdi_pid[0]) { LOG_ERROR("Please specify ftdi vid_pid"); return ERROR_JTAG_INIT_FAILED; }if (!ftdi_vid[0] && !ftdi_pid[0]) { ... } mpsse_ctx = mpsse_open(ftdi_vid, ftdi_pid, ftdi_device_desc, adapter_get_required_serial(), adapter_usb_get_location(), ftdi_channel); if (!mpsse_ctx) return ERROR_JTAG_INIT_FAILED; output = jtag_output_init; direction = jtag_direction_init; if (swd_mode) { struct signal *sig = find_signal_by_name("SWD_EN"); if (!sig) { LOG_ERROR("SWD mode is active but SWD_EN signal is not defined"); return ERROR_JTAG_INIT_FAILED; }if (!sig) { ... } /* A dummy SWD_EN would have zero mask */ if (sig->data_mask) ftdi_set_signal(sig, '1'); }if (swd_mode) { ... } mpsse_set_data_bits_low_byte(mpsse_ctx, output & 0xff, direction & 0xff); mpsse_set_data_bits_high_byte(mpsse_ctx, output >> 8, direction >> 8); mpsse_loopback_config(mpsse_ctx, false); freq = mpsse_set_frequency(mpsse_ctx, adapter_get_speed_khz() * 1000); return mpsse_flush(mpsse_ctx); }{ ... } static int ftdi_quit(void) { mpsse_close(mpsse_ctx); struct signal *sig = signals; while (sig) { struct signal *next = sig->next; free((void *)sig->name); free(sig); sig = next; }while (sig) { ... } free(ftdi_device_desc); free(swd_cmd_queue); return ERROR_OK; }{ ... } COMMAND_HANDLER(ftdi_handle_device_desc_command) { if (CMD_ARGC == 1) { free(ftdi_device_desc); ftdi_device_desc = strdup(CMD_ARGV[0]); }if (CMD_ARGC == 1) { ... } else { LOG_ERROR("expected exactly one argument to ftdi device_desc <description>"); }else { ... } return ERROR_OK; }{ ... } COMMAND_HANDLER(ftdi_handle_channel_command) { if (CMD_ARGC == 1) COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0], ftdi_channel); else return ERROR_COMMAND_SYNTAX_ERROR; return ERROR_OK; }{ ... } COMMAND_HANDLER(ftdi_handle_layout_init_command) { if (CMD_ARGC != 2) return ERROR_COMMAND_SYNTAX_ERROR; COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], jtag_output_init); COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], jtag_direction_init); return ERROR_OK; }{ ... } COMMAND_HANDLER(ftdi_handle_layout_signal_command) { if (CMD_ARGC < 1) return ERROR_COMMAND_SYNTAX_ERROR; bool invert_data = false; uint16_t data_mask = 0; bool invert_input = false; uint16_t input_mask = 0; bool invert_oe = false; uint16_t oe_mask = 0; for (unsigned i = 1; i < CMD_ARGC; i += 2) { if (strcmp("-data", CMD_ARGV[i]) == 0) { invert_data = false; COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], data_mask); }if (strcmp("-data", CMD_ARGV[i]) == 0) { ... } else if (strcmp("-ndata", CMD_ARGV[i]) == 0) { invert_data = true; COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], data_mask); }else if (strcmp("-ndata", CMD_ARGV[i]) == 0) { ... } else if (strcmp("-input", CMD_ARGV[i]) == 0) { invert_input = false; COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], input_mask); }else if (strcmp("-input", CMD_ARGV[i]) == 0) { ... } else if (strcmp("-ninput", CMD_ARGV[i]) == 0) { invert_input = true; COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], input_mask); }else if (strcmp("-ninput", CMD_ARGV[i]) == 0) { ... } else if (strcmp("-oe", CMD_ARGV[i]) == 0) { invert_oe = false; COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], oe_mask); }else if (strcmp("-oe", CMD_ARGV[i]) == 0) { ... } else if (strcmp("-noe", CMD_ARGV[i]) == 0) { invert_oe = true; COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], oe_mask); }else if (strcmp("-noe", CMD_ARGV[i]) == 0) { ... } else if (!strcmp("-alias", CMD_ARGV[i]) || !strcmp("-nalias", CMD_ARGV[i])) { if (!strcmp("-nalias", CMD_ARGV[i])) { invert_data = true; invert_input = true; }if (!strcmp("-nalias", CMD_ARGV[i])) { ... } struct signal *sig = find_signal_by_name(CMD_ARGV[i + 1]); if (!sig) { LOG_ERROR("signal %s is not defined", CMD_ARGV[i + 1]); return ERROR_FAIL; }if (!sig) { ... } data_mask = sig->data_mask; input_mask = sig->input_mask; oe_mask = sig->oe_mask; invert_input ^= sig->invert_input; invert_oe = sig->invert_oe; invert_data ^= sig->invert_data; }else if (!strcmp("-alias", CMD_ARGV[i]) || !strcmp("-nalias", CMD_ARGV[i])) { ... } else { LOG_ERROR("unknown option '%s'", CMD_ARGV[i]); return ERROR_COMMAND_SYNTAX_ERROR; }else { ... } }for (unsigned i = 1; i < CMD_ARGC; i += 2) { ... } struct signal *sig; sig = find_signal_by_name(CMD_ARGV[0]); if (!sig) sig = create_signal(CMD_ARGV[0]); if (!sig) { LOG_ERROR("failed to create signal %s", CMD_ARGV[0]); return ERROR_FAIL; }if (!sig) { ... } sig->invert_data = invert_data; sig->data_mask = data_mask; sig->invert_input = invert_input; sig->input_mask = input_mask; sig->invert_oe = invert_oe; sig->oe_mask = oe_mask; return ERROR_OK; }{ ... } COMMAND_HANDLER(ftdi_handle_set_signal_command) { if (CMD_ARGC < 2) return ERROR_COMMAND_SYNTAX_ERROR; struct signal *sig; sig = find_signal_by_name(CMD_ARGV[0]); if (!sig) { LOG_ERROR("interface configuration doesn't define signal '%s'", CMD_ARGV[0]); return ERROR_FAIL; }if (!sig) { ... } switch (*CMD_ARGV[1]) { case '0': case '1': case 'z': case 'Z': /* single character level specifier only */ if (CMD_ARGV[1][1] == '\0') { ftdi_set_signal(sig, *CMD_ARGV[1]); break; }if (CMD_ARGV[1][1] == '\0') { ... } /* fallthrough */case 'Z': default: LOG_ERROR("unknown signal level '%s', use 0, 1 or z", CMD_ARGV[1]); return ERROR_COMMAND_ARGUMENT_INVALID;default }switch (*CMD_ARGV[1]) { ... } return mpsse_flush(mpsse_ctx); }{ ... } COMMAND_HANDLER(ftdi_handle_get_signal_command) { if (CMD_ARGC < 1) return ERROR_COMMAND_SYNTAX_ERROR; struct signal *sig; uint16_t sig_data = 0; sig = find_signal_by_name(CMD_ARGV[0]); if (!sig) { command_print(CMD, "interface configuration doesn't define signal '%s'", CMD_ARGV[0]); return ERROR_FAIL; }if (!sig) { ... } int ret = ftdi_get_signal(sig, &sig_data); if (ret != ERROR_OK) return ret; command_print(CMD, "%#06x", sig_data); return ERROR_OK; }{ ... } COMMAND_HANDLER(ftdi_handle_vid_pid_command) { if (CMD_ARGC > MAX_USB_IDS * 2) { LOG_WARNING("ignoring extra IDs in ftdi vid_pid " "(maximum is %d pairs)", MAX_USB_IDS); CMD_ARGC = MAX_USB_IDS * 2; }if (CMD_ARGC > MAX_USB_IDS * 2) { ... } if (CMD_ARGC < 2 || (CMD_ARGC & 1)) { LOG_WARNING("incomplete ftdi vid_pid configuration directive"); if (CMD_ARGC < 2) return ERROR_COMMAND_SYNTAX_ERROR; /* remove the incomplete trailing id */ CMD_ARGC -= 1; }if (CMD_ARGC < 2 || (CMD_ARGC & 1)) { ... } unsigned i; for (i = 0; i < CMD_ARGC; i += 2) { COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ftdi_vid[i >> 1]); COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ftdi_pid[i >> 1]); }for (i = 0; i < CMD_ARGC; i += 2) { ... } /* * Explicitly terminate, in case there are multiples instances of * ftdi vid_pid. *//* ... */ ftdi_vid[i >> 1] = ftdi_pid[i >> 1] = 0; return ERROR_OK; }{ ... } COMMAND_HANDLER(ftdi_handle_tdo_sample_edge_command) { const struct nvp *n; static const struct nvp nvp_ftdi_jtag_modes[] = { { .name = "rising", .value = JTAG_MODE }, { .name = "falling", .value = JTAG_MODE_ALT }, { .name = NULL, .value = -1 }, ...}; if (CMD_ARGC > 0) { n = nvp_name2value(nvp_ftdi_jtag_modes, CMD_ARGV[0]); if (!n->name) return ERROR_COMMAND_SYNTAX_ERROR; ftdi_jtag_mode = n->value; }if (CMD_ARGC > 0) { ... } n = nvp_value2name(nvp_ftdi_jtag_modes, ftdi_jtag_mode); command_print(CMD, "ftdi samples TDO on %s edge of TCK", n->name); return ERROR_OK; }{ ... } static const struct command_registration ftdi_subcommand_handlers[] = { { .name = "device_desc", .handler = &ftdi_handle_device_desc_command, .mode = COMMAND_CONFIG, .help = "set the USB device description of the FTDI device", .usage = "description_string", ...}, { .name = "channel", .handler = &ftdi_handle_channel_command, .mode = COMMAND_CONFIG, .help = "set the channel of the FTDI device that is used as JTAG", .usage = "(0-3)", ...}, { .name = "layout_init", .handler = &ftdi_handle_layout_init_command, .mode = COMMAND_CONFIG, .help = "initialize the FTDI GPIO signals used " "to control output-enables and reset signals", .usage = "data direction", ...}, { .name = "layout_signal", .handler = &ftdi_handle_layout_signal_command, .mode = COMMAND_ANY, .help = "define a signal controlled by one or more FTDI GPIO as data " "and/or output enable", .usage = "name [-data mask|-ndata mask] [-oe mask|-noe mask] [-alias|-nalias name]", ...}, { .name = "set_signal", .handler = &ftdi_handle_set_signal_command, .mode = COMMAND_EXEC, .help = "control a layout-specific signal", .usage = "name (1|0|z)", ...}, { .name = "get_signal", .handler = &ftdi_handle_get_signal_command, .mode = COMMAND_EXEC, .help = "read the value of a layout-specific signal", .usage = "name", ...}, { .name = "vid_pid", .handler = &ftdi_handle_vid_pid_command, .mode = COMMAND_CONFIG, .help = "the vendor ID and product ID of the FTDI device", .usage = "(vid pid)*", ...}, { .name = "tdo_sample_edge", .handler = &ftdi_handle_tdo_sample_edge_command, .mode = COMMAND_ANY, .help = "set which TCK clock edge is used for sampling TDO " "- default is rising-edge (Setting to falling-edge may " "allow signalling speed increase)", .usage = "(rising|falling)", ...}, COMMAND_REGISTRATION_DONE ...}; static const struct command_registration ftdi_command_handlers[] = { { .name = "ftdi", .mode = COMMAND_ANY, .help = "perform ftdi management", .chain = ftdi_subcommand_handlers, .usage = "", ...}, COMMAND_REGISTRATION_DONE ...}; static int create_default_signal(const char *name, uint16_t data_mask) { struct signal *sig = create_signal(name); if (!sig) { LOG_ERROR("failed to create signal %s", name); return ERROR_FAIL; }if (!sig) { ... } sig->invert_data = false; sig->data_mask = data_mask; sig->invert_oe = false; sig->oe_mask = 0; return ERROR_OK; }{ ... } static int create_signals(void) { if (create_default_signal("TCK", 0x01) != ERROR_OK) return ERROR_FAIL; if (create_default_signal("TDI", 0x02) != ERROR_OK) return ERROR_FAIL; if (create_default_signal("TDO", 0x04) != ERROR_OK) return ERROR_FAIL; if (create_default_signal("TMS", 0x08) != ERROR_OK) return ERROR_FAIL; return ERROR_OK; }{ ... } static int ftdi_swd_init(void) { LOG_INFO("FTDI SWD mode enabled"); swd_mode = true; if (create_signals() != ERROR_OK) return ERROR_FAIL; swd_cmd_queue_alloced = 10; swd_cmd_queue = malloc(swd_cmd_queue_alloced * sizeof(*swd_cmd_queue)); return swd_cmd_queue ? ERROR_OK : ERROR_FAIL; }{ ... } static void ftdi_swd_swdio_en(bool enable) { struct signal *oe = find_signal_by_name("SWDIO_OE"); if (oe) { if (oe->data_mask) ftdi_set_signal(oe, enable ? '1' : '0'); else { /* Sets TDI/DO pin to input during rx when both pins are connected to SWDIO *//* ... */ if (enable) direction |= jtag_direction_init & 0x0002U; else direction &= ~0x0002U; mpsse_set_data_bits_low_byte(mpsse_ctx, output & 0xff, direction & 0xff); }else { ... } }if (oe) { ... } }{ ... } /** * Flush the MPSSE queue and process the SWD transaction queue * @return *//* ... */ static int ftdi_swd_run_queue(void) { LOG_DEBUG_IO("Executing %zu queued transactions", swd_cmd_queue_length); int retval; struct signal *led = find_signal_by_name("LED"); if (queued_retval != ERROR_OK) { LOG_DEBUG_IO("Skipping due to previous errors: %d", queued_retval); goto skip; }if (queued_retval != ERROR_OK) { ... } /* A transaction must be followed by another transaction or at least 8 idle cycles to * ensure that data is clocked through the AP. *//* ... */ mpsse_clock_data_out(mpsse_ctx, NULL, 0, 8, SWD_MODE); /* Terminate the "blink", if the current layout has that feature */ if (led) ftdi_set_signal(led, '0'); queued_retval = mpsse_flush(mpsse_ctx); if (queued_retval != ERROR_OK) { LOG_ERROR("MPSSE failed"); goto skip; }if (queued_retval != ERROR_OK) { ... } for (size_t i = 0; i < swd_cmd_queue_length; i++) { int ack = buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1, 3); /* Devices do not reply to DP_TARGETSEL write cmd, ignore received ack */ bool check_ack = swd_cmd_returns_ack(swd_cmd_queue[i].cmd); LOG_CUSTOM_LEVEL((check_ack && ack != SWD_ACK_OK) ? LOG_LVL_DEBUG : LOG_LVL_DEBUG_IO, "%s%s %s %s reg %X = %08" PRIx32, check_ack ? "" : "ack ignored ", ack == SWD_ACK_OK ? "OK" : ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK", swd_cmd_queue[i].cmd & SWD_CMD_APNDP ? "AP" : "DP", swd_cmd_queue[i].cmd & SWD_CMD_RNW ? "read" : "write", (swd_cmd_queue[i].cmd & SWD_CMD_A32) >> 1, buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3 + (swd_cmd_queue[i].cmd & SWD_CMD_RNW ? 0 : 1), 32)); if (ack != SWD_ACK_OK && check_ack) { queued_retval = swd_ack_to_error_code(ack); goto skip; }if (ack != SWD_ACK_OK && check_ack) { ... } else if (swd_cmd_queue[i].cmd & SWD_CMD_RNW) { uint32_t data = buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3, 32); int parity = buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3 + 32, 1); if (parity != parity_u32(data)) { LOG_ERROR("SWD Read data parity mismatch"); queued_retval = ERROR_FAIL; goto skip; }if (parity != parity_u32(data)) { ... } if (swd_cmd_queue[i].dst) *swd_cmd_queue[i].dst = data; }else if (swd_cmd_queue[i].cmd & SWD_CMD_RNW) { ... } }for (size_t i = 0; i < swd_cmd_queue_length; i++) { ... } skip: swd_cmd_queue_length = 0; retval = queued_retval; queued_retval = ERROR_OK; /* Queue a new "blink" */ if (led && retval == ERROR_OK) ftdi_set_signal(led, '1'); return retval; }{ ... } static void ftdi_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk) { if (swd_cmd_queue_length >= swd_cmd_queue_alloced) { /* Not enough room in the queue. Run the queue and increase its size for next time. * Note that it's not possible to avoid running the queue here, because mpsse contains * pointers into the queue which may be invalid after the realloc. *//* ... */ queued_retval = ftdi_swd_run_queue(); struct swd_cmd_queue_entry *q = realloc(swd_cmd_queue, swd_cmd_queue_alloced * 2 * sizeof(*swd_cmd_queue)); if (q) { swd_cmd_queue = q; swd_cmd_queue_alloced *= 2; LOG_DEBUG("Increased SWD command queue to %zu elements", swd_cmd_queue_alloced); }if (q) { ... } }if (swd_cmd_queue_length >= swd_cmd_queue_alloced) { ... } if (queued_retval != ERROR_OK) return; size_t i = swd_cmd_queue_length++; swd_cmd_queue[i].cmd = cmd | SWD_CMD_START | SWD_CMD_PARK; mpsse_clock_data_out(mpsse_ctx, &swd_cmd_queue[i].cmd, 0, 8, SWD_MODE); if (swd_cmd_queue[i].cmd & SWD_CMD_RNW) { /* Queue a read transaction */ swd_cmd_queue[i].dst = dst; ftdi_swd_swdio_en(false); mpsse_clock_data_in(mpsse_ctx, swd_cmd_queue[i].trn_ack_data_parity_trn, 0, 1 + 3 + 32 + 1 + 1, SWD_MODE); ftdi_swd_swdio_en(true); }if (swd_cmd_queue[i].cmd & SWD_CMD_RNW) { ... } else { /* Queue a write transaction */ ftdi_swd_swdio_en(false); mpsse_clock_data_in(mpsse_ctx, swd_cmd_queue[i].trn_ack_data_parity_trn, 0, 1 + 3 + 1, SWD_MODE); ftdi_swd_swdio_en(true); buf_set_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3 + 1, 32, data); buf_set_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3 + 1 + 32, 1, parity_u32(data)); mpsse_clock_data_out(mpsse_ctx, swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3 + 1, 32 + 1, SWD_MODE); }else { ... } /* Insert idle cycles after AP accesses to avoid WAIT */ if (cmd & SWD_CMD_APNDP) mpsse_clock_data_out(mpsse_ctx, NULL, 0, ap_delay_clk, SWD_MODE); }{ ... } static void ftdi_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk) { assert(cmd & SWD_CMD_RNW); ftdi_swd_queue_cmd(cmd, value, 0, ap_delay_clk); }{ ... } static void ftdi_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk) { assert(!(cmd & SWD_CMD_RNW)); ftdi_swd_queue_cmd(cmd, NULL, value, ap_delay_clk); }{ ... } static int ftdi_swd_switch_seq(enum swd_special_seq seq) { switch (seq) { case LINE_RESET: LOG_DEBUG("SWD line reset"); ftdi_swd_swdio_en(true); mpsse_clock_data_out(mpsse_ctx, swd_seq_line_reset, 0, swd_seq_line_reset_len, SWD_MODE); break;case LINE_RESET: case JTAG_TO_SWD: LOG_DEBUG("JTAG-to-SWD"); ftdi_swd_swdio_en(true); mpsse_clock_data_out(mpsse_ctx, swd_seq_jtag_to_swd, 0, swd_seq_jtag_to_swd_len, SWD_MODE); break;case JTAG_TO_SWD: case JTAG_TO_DORMANT: LOG_DEBUG("JTAG-to-DORMANT"); ftdi_swd_swdio_en(true); mpsse_clock_data_out(mpsse_ctx, swd_seq_jtag_to_dormant, 0, swd_seq_jtag_to_dormant_len, SWD_MODE); break;case JTAG_TO_DORMANT: case SWD_TO_JTAG: LOG_DEBUG("SWD-to-JTAG"); ftdi_swd_swdio_en(true); mpsse_clock_data_out(mpsse_ctx, swd_seq_swd_to_jtag, 0, swd_seq_swd_to_jtag_len, SWD_MODE); break;case SWD_TO_JTAG: case SWD_TO_DORMANT: LOG_DEBUG("SWD-to-DORMANT"); ftdi_swd_swdio_en(true); mpsse_clock_data_out(mpsse_ctx, swd_seq_swd_to_dormant, 0, swd_seq_swd_to_dormant_len, SWD_MODE); break;case SWD_TO_DORMANT: case DORMANT_TO_SWD: LOG_DEBUG("DORMANT-to-SWD"); ftdi_swd_swdio_en(true); mpsse_clock_data_out(mpsse_ctx, swd_seq_dormant_to_swd, 0, swd_seq_dormant_to_swd_len, SWD_MODE); break;case DORMANT_TO_SWD: case DORMANT_TO_JTAG: LOG_DEBUG("DORMANT-to-JTAG"); ftdi_swd_swdio_en(true); mpsse_clock_data_out(mpsse_ctx, swd_seq_dormant_to_jtag, 0, swd_seq_dormant_to_jtag_len, SWD_MODE); break;case DORMANT_TO_JTAG: default: LOG_ERROR("Sequence %d not supported", seq); return ERROR_FAIL;default }switch (seq) { ... } return ERROR_OK; }{ ... } static const struct swd_driver ftdi_swd = { .init = ftdi_swd_init, .switch_seq = ftdi_swd_switch_seq, .read_reg = ftdi_swd_read_reg, .write_reg = ftdi_swd_write_reg, .run = ftdi_swd_run_queue, ...}; static const char * const ftdi_transports[] = { "jtag", "swd", NULL }; static struct jtag_interface ftdi_interface = { .supported = DEBUG_CAP_TMS_SEQ, .execute_queue = ftdi_execute_queue, ...}; struct adapter_driver ftdi_adapter_driver = { .name = "ftdi", .transports = ftdi_transports, .commands = ftdi_command_handlers, .init = ftdi_initialize, .quit = ftdi_quit, .reset = ftdi_reset, .speed = ftdi_speed, .khz = ftdi_khz, .speed_div = ftdi_speed_div, .jtag_ops = &ftdi_interface, .swd_ops = &ftdi_swd, ...};
Details
Show:
from
Types: Columns:
Click anywhere in the source to view detailed information here...