1
2
3
6
7
8
9
10
11
19
20
21
22
23
24
25
26
27
28
30
31
32
33
34
35
36
37
40
41
42
43
44
48
49
50
51
54
55
56
59
60
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
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
272
273
278
279
280
281
282
283
284
285
286
287
288
294
295
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
336
337
338
339
340
341
342
343
350
351
352
353
354
355
356
357
358
372
373
374
375
376
377
378
379
380
381
386
387
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
415
416
417
428
429
430
431
432
433
434
435
436
437
438
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
534
535
536
541
542
543
544
545
546
547
548
549
553
554
555
556
557
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
627
628
629
630
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
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
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
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
1084
1085
1090
1091
1092
1093
1094
1095
1096
1097
1100
1101
1102
1103
1104
1105
1106
1107
1108
1116
1117
1118
1119
1120
1121
1122
1123
1124
1128
1129
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
1165
1166
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
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
1232
1233
1234
1235
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1330
1331
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1351
1352
1357
1358
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1384
1385
1386
1390
1391
1392
1393
1394
1398
1402
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1433
1434
1435
1436
1437
1438
1439
1440
1441
1446
1447
1448
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1480
1481
1482
1483
1484
1485
1486
1487
1488
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1510
1511
1517
1518
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1590
1591
1592
1593
1594
1595
1596
1597
1598
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1634
1635
1636
1643
1644
1645
1646
1647
1648
1652
1653
1654
1655
1656
1657
1658
1659
1660
1664
1665
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1685
1686
1687
1688
1689
1690
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1816
1820
1824
1827
1830
1833
1834
1838
1839
1840
1841
1842
1843
1844
1845
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1865
1866
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1908
1909
1910
1914
1915
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1980
1981
1982
1983
1984
1989
1990
1995
1996
2001
2002
2003
2004
2005
2006
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <transport/transport.h>
#include <jtag/adapter.h>
#include <jtag/swd.h>
#include <jtag/interface.h>
#include <jtag/commands.h>
#include <jtag/tcl.h>
#include <libusb.h>
7 includes
#define XDS110_MIN_VOLTAGE 1800
#define XDS110_MAX_VOLTAGE 3600
#define XDS110_STAND_ALONE_ID 0x21
#define OCD_FIRMWARE_VERSION 0x02030011
#define OCD_FIRMWARE_UPGRADE \
"XDS110: upgrade to version 2.3.0.11+ for improved support"...
#define FAST_TCK_FIRMWARE_VERSION 0x03000000
#define FAST_TCK_PLUS_FIRMWARE_VERSION 0x03000003
/* ... */
#define MAX_PACKET 1024
/* ... */
#define MAX_DATA_BLOCK 4096
9 defines
#ifndef USB_PAYLOAD_SIZE
#define USB_PAYLOAD_SIZE (MAX_DATA_BLOCK + 60)
/* ... */#endif
#define MAX_RESULT_QUEUE (MAX_DATA_BLOCK / 4)
/* ... */
/* ... */
#define DEFAULT_ATTEMPTS (1)
#define DEFAULT_TIMEOUT (4000)
#define SC_ERR_NONE 0
#define SC_ERR_XDS110_FAIL -261
#define SC_ERR_SWD_WAIT -613
#define SC_ERR_SWD_FAULT -614
#define SC_ERR_SWD_PROTOCOL -615
#define SC_ERR_SWD_PARITY -616
#define SC_ERR_SWD_DEVICE_ID -617
#define XDS110_MIN_TCK_SPEED 100
#define XDS110_MAX_SLOW_TCK_SPEED 2500
#define XDS110_MAX_FAST_TCK_SPEED 14000
#define XDS110_DEFAULT_TCK_SPEED 2500
#define FAST_TCK_DELAY_14000_KHZ 0
#define FAST_TCK_DELAY_10000_KHZ 0xfffffffd
#define FAST_TCK_DELAY_12000_KHZ 0xfffffffe
#define FAST_TCK_DELAY_8500_KHZ 1
#define FAST_TCK_DELAY_5500_KHZ 2
#define MODE_JTAG 1
#define XDS_JTAG_STATE_RESET 1
#define XDS_JTAG_STATE_IDLE 2
#define XDS_JTAG_STATE_SHIFT_DR 3
#define XDS_JTAG_STATE_SHIFT_IR 4
#define XDS_JTAG_STATE_PAUSE_DR 5
#define XDS_JTAG_STATE_PAUSE_IR 6
#define XDS_JTAG_STATE_EXIT1_DR 8
#define XDS_JTAG_STATE_EXIT1_IR 9
#define XDS_JTAG_STATE_EXIT2_DR 10
#define XDS_JTAG_STATE_EXIT2_IR 11
#define XDS_JTAG_STATE_SELECT_DR 12
#define XDS_JTAG_STATE_SELECT_IR 13
#define XDS_JTAG_STATE_UPDATE_DR 14
#define XDS_JTAG_STATE_UPDATE_IR 15
#define XDS_JTAG_STATE_CAPTURE_DR 16
#define XDS_JTAG_STATE_CAPTURE_IR 17
#define XDS_JTAG_TRANSIT_QUICKEST 1
#define XDS_JTAG_TRANSIT_VIA_CAPTURE 2
#define XDS_JTAG_TRANSIT_VIA_IDLE 3
#define DAP_AP 0
#define DAP_DP 1
#define DAP_DP_IDCODE 0x0
#define DAP_DP_ABORT 0x0
#define DAP_DP_STAT 0x4
#define DAP_DP_CTRL 0x4
#define DAP_DP_ADDR 0x8
#define DAP_DP_RESEND 0x8
#define DAP_DP_SELECT 0x8
#define DAP_DP_RDBUFF 0xc
#define DAP_AP_CSW 0x00
#define DAP_AP_TAR 0x04
#define DAP_AP_DRW 0x0C
#define DAP_AP_BD0 0x10
#define DAP_AP_BD1 0x14
#define DAP_AP_BD2 0x18
#define DAP_AP_BD3 0x1C
#define DAP_AP_RTBL 0xF8
#define DAP_AP_IDR 0xFC
#define XDS_OUT_LEN 1
#define XDS_IN_LEN 4
#define XDS_CONNECT 0x01
#define XDS_DISCONNECT 0x02
#define XDS_VERSION 0x03
#define XDS_SET_TCK 0x04
#define XDS_SET_TRST 0x05
#define XDS_CYCLE_TCK 0x07
#define XDS_GOTO_STATE 0x09
#define XDS_JTAG_SCAN 0x0c
#define XDS_SET_SRST 0x0e
#define CMAPI_CONNECT 0x0f
#define CMAPI_DISCONNECT 0x10
#define CMAPI_ACQUIRE 0x11
#define CMAPI_RELEASE 0x12
#define CMAPI_REG_READ 0x15
#define CMAPI_REG_WRITE 0x16
#define SWD_CONNECT 0x17
#define SWD_DISCONNECT 0x18
#define CJTAG_CONNECT 0x2b
#define CJTAG_DISCONNECT 0x2c
#define XDS_SET_SUPPLY 0x32
#define OCD_DAP_REQUEST 0x3a
#define OCD_SCAN_REQUEST 0x3b
#define OCD_PATHMOVE 0x3c
#define CMD_IR_SCAN 1
#define CMD_DR_SCAN 2
#define CMD_RUNTEST 3
#define CMD_STABLECLOCKS 4
87 defines
static const uint32_t xds_jtag_state[] = {
XDS_JTAG_STATE_EXIT2_DR,
XDS_JTAG_STATE_EXIT1_DR,
XDS_JTAG_STATE_SHIFT_DR,
XDS_JTAG_STATE_PAUSE_DR,
XDS_JTAG_STATE_SELECT_IR,
XDS_JTAG_STATE_UPDATE_DR,
XDS_JTAG_STATE_CAPTURE_DR,
XDS_JTAG_STATE_SELECT_DR,
XDS_JTAG_STATE_EXIT2_IR,
XDS_JTAG_STATE_EXIT1_IR,
XDS_JTAG_STATE_SHIFT_IR,
XDS_JTAG_STATE_PAUSE_IR,
XDS_JTAG_STATE_IDLE,
XDS_JTAG_STATE_UPDATE_IR,
XDS_JTAG_STATE_CAPTURE_IR,
XDS_JTAG_STATE_RESET,
...};
struct scan_result {
bool first;
uint8_t *buffer;
uint32_t num_bits;
...};
struct xds110_info {
struct libusb_context *ctx;
struct libusb_device_handle *dev;
unsigned char read_payload[USB_PAYLOAD_SIZE];
unsigned char write_packet[3];
unsigned char write_payload[USB_PAYLOAD_SIZE];
uint16_t vid;
uint16_t pid;
uint8_t interface;
uint8_t endpoint_in;
uint8_t endpoint_out;
bool is_connected;
bool is_cmapi_connected;
bool is_cmapi_acquired;
bool is_swd_mode;
bool is_ap_dirty;
uint32_t select;
uint32_t rdbuff;
bool use_rdbuff;
uint32_t speed;
uint32_t delay_count;
uint32_t voltage;
uint32_t firmware;
uint16_t hardware;
unsigned char txn_requests[MAX_DATA_BLOCK];
uint32_t *txn_dap_results[MAX_DATA_BLOCK / 4];
struct scan_result txn_scan_results[MAX_DATA_BLOCK / 4];
uint32_t txn_request_size;
uint32_t txn_result_size;
uint32_t txn_result_count;
...};
static struct xds110_info xds110 = {
.ctx = NULL,
.dev = NULL,
.vid = 0,
.pid = 0,
.interface = 0,
.endpoint_in = 0,
.endpoint_out = 0,
.is_connected = false,
.is_cmapi_connected = false,
.is_cmapi_acquired = false,
.is_swd_mode = false,
.is_ap_dirty = false,
.speed = XDS110_DEFAULT_TCK_SPEED,
.delay_count = 0,
.voltage = 0,
.firmware = 0,
.hardware = 0,
.txn_request_size = 0,
.txn_result_size = 0,
.txn_result_count = 0
...};
static inline void xds110_set_u32(uint8_t *buffer, uint32_t value)
{
buffer[3] = (value >> 24) & 0xff;
buffer[2] = (value >> 16) & 0xff;
buffer[1] = (value >> 8) & 0xff;
buffer[0] = (value >> 0) & 0xff;
}{ ... }
static inline void xds110_set_u16(uint8_t *buffer, uint16_t value)
{
buffer[1] = (value >> 8) & 0xff;
buffer[0] = (value >> 0) & 0xff;
}{ ... }
static inline uint32_t xds110_get_u32(uint8_t *buffer)
{
uint32_t value = (((uint32_t)buffer[3]) << 24) |
(((uint32_t)buffer[2]) << 16) |
(((uint32_t)buffer[1]) << 8) |
(((uint32_t)buffer[0]) << 0);
return value;
}{ ... }
static inline uint16_t xds110_get_u16(uint8_t *buffer)
{
uint16_t value = (((uint32_t)buffer[1]) << 8) |
(((uint32_t)buffer[0]) << 0);
return value;
}{ ... }
/* ... */
static bool usb_connect(void)
{
struct libusb_context *ctx = NULL;
struct libusb_device **list = NULL;
struct libusb_device_handle *dev = NULL;
struct libusb_device_descriptor desc;
uint16_t vids[] = { 0x0451, 0x0451, 0x1cbe };
uint16_t pids[] = { 0xbef3, 0xbef4, 0x02a5 };
uint8_t interfaces[] = { 2, 2, 0 };
uint8_t endpoints_in[] = { 3, 3, 1 };
uint8_t endpoints_out[] = { 2, 2, 1 };
ssize_t count = 0;
ssize_t i = 0;
int result = 0;
bool found = false;
uint32_t device = 0;
bool match = false;
result = libusb_init(&ctx);
if (result == 0) {
count = libusb_get_device_list(ctx, &list);
if (count <= 0) {
result = -1;
list = NULL;
}if (count <= 0) { ... }
}if (result == 0) { ... }
if (result == 0) {
for (i = 0; i < count; i++) {
libusb_get_device_descriptor(list[i], &desc);
match = false;
for (device = 0; device < ARRAY_SIZE(vids); device++) {
if (desc.idVendor == vids[device] &&
desc.idProduct == pids[device]) {
match = true;
break;
}if (desc.idVendor == vids[device] && desc.idProduct == pids[device]) { ... }
}for (device = 0; device < ARRAY_SIZE(vids); device++) { ... }
if (match) {
result = libusb_open(list[i], &dev);
if (result == 0) {
const int max_data = 256;
unsigned char data[max_data + 1];
*data = '\0';
if (!adapter_get_required_serial()) {
found = true;
break;
}if (!adapter_get_required_serial()) { ... } else {
result = libusb_get_string_descriptor_ascii(dev,
desc.iSerialNumber, data, max_data);
if (result > 0 &&
strcmp((char *)data, adapter_get_required_serial()) == 0) {
found = true;
break;
}if (result > 0 && strcmp((char *)data, adapter_get_required_serial()) == 0) { ... }
}else { ... }
libusb_close(dev);
dev = NULL;
}if (result == 0) { ... }
}if (match) { ... }
}for (i = 0; i < count; i++) { ... }
}if (result == 0) { ... }
/* ... */
if (list) {
libusb_free_device_list(list, 1);
}if (list) { ... }
if (found) {
xds110.vid = vids[device];
xds110.pid = pids[device];
xds110.interface = interfaces[device];
xds110.endpoint_in = endpoints_in[device] | LIBUSB_ENDPOINT_IN;
xds110.endpoint_out = endpoints_out[device] | LIBUSB_ENDPOINT_OUT;
xds110.ctx = ctx;
xds110.dev = dev;
(void)libusb_set_auto_detach_kernel_driver(dev, 1);
result = libusb_claim_interface(dev, xds110.interface);
}if (found) { ... } else {
result = -1;
}else { ... }
if (result != 0) {
if (dev) {
(void)libusb_release_interface(dev, xds110.interface);
libusb_close(dev);
}if (dev) { ... }
if (ctx)
libusb_exit(ctx);
xds110.ctx = NULL;
xds110.dev = NULL;
}if (result != 0) { ... }
if (result == 0)
LOG_INFO("XDS110: connected");
else
LOG_ERROR("XDS110: failed to connect");
return (result == 0) ? true : false;
}{ ... }
static void usb_disconnect(void)
{
if (xds110.dev) {
(void)libusb_release_interface(xds110.dev, xds110.interface);
libusb_close(xds110.dev);
xds110.dev = NULL;
}if (xds110.dev) { ... }
if (xds110.ctx) {
libusb_exit(xds110.ctx);
xds110.ctx = NULL;
}if (xds110.ctx) { ... }
LOG_INFO("XDS110: disconnected");
}{ ... }
static bool usb_read(unsigned char *buffer, int size, int *bytes_read,
int timeout)
{
int result;
if (!xds110.dev || !buffer || !bytes_read)
return false;
if (timeout == 0)
timeout = DEFAULT_TIMEOUT;
result = libusb_bulk_transfer(xds110.dev, xds110.endpoint_in, buffer, size,
bytes_read, timeout);
return (result == 0) ? true : false;
}{ ... }
static bool usb_write(unsigned char *buffer, int size, int *written)
{
int bytes_written = 0;
int result = LIBUSB_SUCCESS;
int retries = 0;
if (!xds110.dev || !buffer)
return false;
result = libusb_bulk_transfer(xds110.dev, xds110.endpoint_out, buffer,
size, &bytes_written, 0);
while (result == LIBUSB_ERROR_PIPE && retries < 3) {
libusb_clear_halt(xds110.dev, xds110.endpoint_out);
result = libusb_bulk_transfer(xds110.dev, xds110.endpoint_out, buffer,
size, &bytes_written, 0);
retries++;
}while (result == LIBUSB_ERROR_PIPE && retries < 3) { ... }
if (written)
*written = bytes_written;
return (result == 0 && size == bytes_written) ? true : false;
}{ ... }
static bool usb_get_response(uint32_t *total_bytes_read, uint32_t timeout)
{
static unsigned char buffer[MAX_PACKET];
int bytes_read;
uint16_t size;
uint16_t count;
bool success;
size = 0;
success = true;
while (success) {
success = usb_read(buffer, sizeof(buffer), &bytes_read, timeout);
if (success) {
/* ... */
if (bytes_read >= 7 && '*' == buffer[0]) {
size = xds110_get_u16(&buffer[1]);
if (USB_PAYLOAD_SIZE >= size && 4 <= size) {
if ((bytes_read - 3) <= size) {
break;
}if ((bytes_read - 3) <= size) { ... }
}if (USB_PAYLOAD_SIZE >= size && 4 <= size) { ... }
}if (bytes_read >= 7 && '*' == buffer[0]) { ... }
}if (success) { ... }
/* ... */
}while (success) { ... }
if (!success) {
if (total_bytes_read)
*total_bytes_read = 0;
return false;
}if (!success) { ... }
count = 0;
bytes_read -= 3;
memcpy((void *)&xds110.read_payload[count], (void *)&buffer[3], bytes_read);
count += bytes_read;
/* ... */
if (timeout > 500)
timeout = 500;
while ((count < size) && success) {
success = usb_read(buffer, sizeof(buffer), &bytes_read, timeout);
if (success) {
if ((count + bytes_read) > size) {
success = false;
}if ((count + bytes_read) > size) { ... } else {
memcpy((void *)&xds110.read_payload[count], (void *)buffer,
bytes_read);
count += bytes_read;
}else { ... }
}if (success) { ... }
}while ((count < size) && success) { ... }
if (!success)
count = 0;
if (total_bytes_read)
*total_bytes_read = count;
return success;
}{ ... }
static bool usb_send_command(uint16_t size)
{
if (size > USB_PAYLOAD_SIZE)
return false;
xds110.write_packet[0] = '*';
xds110_set_u16(&xds110.write_packet[1], size);
size += 3;
return usb_write(xds110.write_packet, (int)size, NULL);
}{ ... }
/* ... */
static bool xds_execute(uint32_t out_length, uint32_t in_length,
uint32_t attempts, uint32_t timeout)
{
bool done = false;
bool success = true;
int error = 0;
uint32_t bytes_read = 0;
if (!xds110.dev)
return false;
while (!done && attempts > 0) {
attempts--;
success = usb_send_command(out_length);
if (success) {
success = usb_get_response(&bytes_read, timeout);
}if (success) { ... }
if (success) {
if (bytes_read != in_length) {
success = false;
LOG_DEBUG("XDS110: command 0x%02x return %" PRIu32 " bytes, expected %" PRIu32,
xds110.write_payload[0], bytes_read, in_length);
}if (bytes_read != in_length) { ... } else {
error = (int)xds110_get_u32(&xds110.read_payload[0]);
done = true;
if (error != SC_ERR_NONE)
LOG_DEBUG("XDS110: command 0x%02x returned error %d",
xds110.write_payload[0], error);
}else { ... }
}if (success) { ... }
}while (!done && attempts > 0) { ... }
if (!success)
error = SC_ERR_XDS110_FAIL;
if (error != 0)
success = false;
return success;
}{ ... }
static bool xds_connect(void)
{
bool success;
xds110.write_payload[0] = XDS_CONNECT;
success = xds_execute(XDS_OUT_LEN, XDS_IN_LEN, DEFAULT_ATTEMPTS,
DEFAULT_TIMEOUT);
return success;
}{ ... }
static bool xds_disconnect(void)
{
bool success;
xds110.write_payload[0] = XDS_DISCONNECT;
success = xds_execute(XDS_OUT_LEN, XDS_IN_LEN, DEFAULT_ATTEMPTS,
DEFAULT_TIMEOUT);
return success;
}{ ... }
static bool xds_version(uint32_t *firmware_id, uint16_t *hardware_id)
{
uint8_t *fw_id_pntr = &xds110.read_payload[XDS_IN_LEN + 0];
uint8_t *hw_id_pntr = &xds110.read_payload[XDS_IN_LEN + 4];
bool success;
xds110.write_payload[0] = XDS_VERSION;
success = xds_execute(XDS_OUT_LEN, XDS_IN_LEN + 6, DEFAULT_ATTEMPTS,
DEFAULT_TIMEOUT);
if (success) {
if (firmware_id)
*firmware_id = xds110_get_u32(fw_id_pntr);
if (hardware_id)
*hardware_id = xds110_get_u16(hw_id_pntr);
}if (success) { ... }
return success;
}{ ... }
static bool xds_set_tck_delay(uint32_t delay)
{
uint8_t *delay_pntr = &xds110.write_payload[XDS_OUT_LEN + 0];
bool success;
xds110.write_payload[0] = XDS_SET_TCK;
xds110_set_u32(delay_pntr, delay);
success = xds_execute(XDS_OUT_LEN + 4, XDS_IN_LEN, DEFAULT_ATTEMPTS,
DEFAULT_TIMEOUT);
return success;
}{ ... }
static bool xds_set_trst(uint8_t trst)
{
uint8_t *trst_pntr = &xds110.write_payload[XDS_OUT_LEN + 0];
bool success;
xds110.write_payload[0] = XDS_SET_TRST;
*trst_pntr = trst;
success = xds_execute(XDS_OUT_LEN + 1, XDS_IN_LEN, DEFAULT_ATTEMPTS,
DEFAULT_TIMEOUT);
return success;
}{ ... }
static bool xds_cycle_tck(uint32_t count)
{
uint8_t *count_pntr = &xds110.write_payload[XDS_OUT_LEN + 0];
bool success;
xds110.write_payload[0] = XDS_CYCLE_TCK;
xds110_set_u32(count_pntr, count);
success = xds_execute(XDS_OUT_LEN + 4, XDS_IN_LEN, DEFAULT_ATTEMPTS,
DEFAULT_TIMEOUT);
return success;
}{ ... }
static bool xds_goto_state(uint32_t state)
{
uint8_t *state_pntr = &xds110.write_payload[XDS_OUT_LEN + 0];
uint8_t *transit_pntr = &xds110.write_payload[XDS_OUT_LEN+4];
bool success;
xds110.write_payload[0] = XDS_GOTO_STATE;
xds110_set_u32(state_pntr, state);
xds110_set_u32(transit_pntr, XDS_JTAG_TRANSIT_QUICKEST);
success = xds_execute(XDS_OUT_LEN+8, XDS_IN_LEN, DEFAULT_ATTEMPTS,
DEFAULT_TIMEOUT);
return success;
}{ ... }
static bool xds_jtag_scan(uint32_t shift_state, uint16_t shift_bits,
uint32_t end_state, uint8_t *data_out, uint8_t *data_in)
{
uint8_t *bits_pntr = &xds110.write_payload[XDS_OUT_LEN + 0];
uint8_t *path_pntr = &xds110.write_payload[XDS_OUT_LEN + 2];
uint8_t *trans1_pntr = &xds110.write_payload[XDS_OUT_LEN + 3];
uint8_t *end_pntr = &xds110.write_payload[XDS_OUT_LEN + 4];
uint8_t *trans2_pntr = &xds110.write_payload[XDS_OUT_LEN + 5];
uint8_t *pre_pntr = &xds110.write_payload[XDS_OUT_LEN + 6];
uint8_t *pos_pntr = &xds110.write_payload[XDS_OUT_LEN + 8];
uint8_t *delay_pntr = &xds110.write_payload[XDS_OUT_LEN + 10];
uint8_t *rep_pntr = &xds110.write_payload[XDS_OUT_LEN + 12];
uint8_t *out_pntr = &xds110.write_payload[XDS_OUT_LEN + 14];
uint8_t *in_pntr = &xds110.write_payload[XDS_OUT_LEN + 16];
uint8_t *data_out_pntr = &xds110.write_payload[XDS_OUT_LEN + 18];
uint8_t *data_in_pntr = &xds110.read_payload[XDS_IN_LEN+0];
uint16_t total_bytes = DIV_ROUND_UP(shift_bits, 8);
bool success;
xds110.write_payload[0] = XDS_JTAG_SCAN;
xds110_set_u16(bits_pntr, shift_bits);
*path_pntr = (uint8_t)(shift_state & 0xff);
*trans1_pntr = (uint8_t)XDS_JTAG_TRANSIT_QUICKEST;
*end_pntr = (uint8_t)(end_state & 0xff);
*trans2_pntr = (uint8_t)XDS_JTAG_TRANSIT_QUICKEST;
xds110_set_u16(pre_pntr, 0);
xds110_set_u16(pos_pntr, 0);
xds110_set_u16(delay_pntr, 0);
xds110_set_u16(rep_pntr, 1);
xds110_set_u16(out_pntr, total_bytes);
xds110_set_u16(in_pntr, total_bytes);
memcpy((void *)data_out_pntr, (void *)data_out, total_bytes);
success = xds_execute(XDS_OUT_LEN + 18 + total_bytes,
XDS_IN_LEN + total_bytes, DEFAULT_ATTEMPTS, DEFAULT_TIMEOUT);
if (success)
memcpy((void *)data_in, (void *)data_in_pntr, total_bytes);
return success;
}{ ... }
static bool xds_set_srst(uint8_t srst)
{
uint8_t *srst_pntr = &xds110.write_payload[XDS_OUT_LEN + 0];
bool success;
xds110.write_payload[0] = XDS_SET_SRST;
*srst_pntr = srst;
success = xds_execute(XDS_OUT_LEN + 1, XDS_IN_LEN, DEFAULT_ATTEMPTS,
DEFAULT_TIMEOUT);
return success;
}{ ... }
static bool cmapi_connect(uint32_t *idcode)
{
uint8_t *idcode_pntr = &xds110.read_payload[XDS_IN_LEN + 0];
bool success;
xds110.write_payload[0] = CMAPI_CONNECT;
success = xds_execute(XDS_OUT_LEN, XDS_IN_LEN+4, DEFAULT_ATTEMPTS,
DEFAULT_TIMEOUT);
if (success) {
if (idcode)
*idcode = xds110_get_u32(idcode_pntr);
}if (success) { ... }
return success;
}{ ... }
static bool cmapi_disconnect(void)
{
bool success;
xds110.write_payload[0] = CMAPI_DISCONNECT;
success = xds_execute(XDS_OUT_LEN, XDS_IN_LEN, DEFAULT_ATTEMPTS,
DEFAULT_TIMEOUT);
return success;
}{ ... }
static bool cmapi_acquire(void)
{
bool success;
xds110.write_payload[0] = CMAPI_ACQUIRE;
success = xds_execute(XDS_OUT_LEN, XDS_IN_LEN, DEFAULT_ATTEMPTS,
DEFAULT_TIMEOUT);
return success;
}{ ... }
static bool cmapi_release(void)
{
bool success;
xds110.write_payload[0] = CMAPI_RELEASE;
success = xds_execute(XDS_OUT_LEN, XDS_IN_LEN, DEFAULT_ATTEMPTS,
DEFAULT_TIMEOUT);
return success;
}{ ... }
static bool cmapi_read_dap_reg(uint32_t type, uint32_t ap_num,
uint32_t address, uint32_t *value)
{
uint8_t *type_pntr = &xds110.write_payload[XDS_OUT_LEN + 0];
uint8_t *ap_num_pntr = &xds110.write_payload[XDS_OUT_LEN + 1];
uint8_t *address_pntr = &xds110.write_payload[XDS_OUT_LEN + 2];
uint8_t *value_pntr = &xds110.read_payload[XDS_IN_LEN + 0];
bool success;
xds110.write_payload[0] = CMAPI_REG_READ;
*type_pntr = (uint8_t)(type & 0xff);
*ap_num_pntr = (uint8_t)(ap_num & 0xff);
*address_pntr = (uint8_t)(address & 0xff);
success = xds_execute(XDS_OUT_LEN + 3, XDS_IN_LEN + 4, DEFAULT_ATTEMPTS,
DEFAULT_TIMEOUT);
if (success) {
if (value)
*value = xds110_get_u32(value_pntr);
}if (success) { ... }
return success;
}{ ... }
static bool cmapi_write_dap_reg(uint32_t type, uint32_t ap_num,
uint32_t address, uint32_t *value)
{
uint8_t *type_pntr = &xds110.write_payload[XDS_OUT_LEN + 0];
uint8_t *ap_num_pntr = &xds110.write_payload[XDS_OUT_LEN + 1];
uint8_t *address_pntr = &xds110.write_payload[XDS_OUT_LEN + 2];
uint8_t *value_pntr = &xds110.write_payload[XDS_OUT_LEN + 3];
bool success;
if (!value)
return false;
xds110.write_payload[0] = CMAPI_REG_WRITE;
*type_pntr = (uint8_t)(type & 0xff);
*ap_num_pntr = (uint8_t)(ap_num & 0xff);
*address_pntr = (uint8_t)(address & 0xff);
xds110_set_u32(value_pntr, *value);
success = xds_execute(XDS_OUT_LEN + 7, XDS_IN_LEN, DEFAULT_ATTEMPTS,
DEFAULT_TIMEOUT);
return success;
}{ ... }
static bool swd_connect(void)
{
bool success;
xds110.write_payload[0] = SWD_CONNECT;
success = xds_execute(XDS_OUT_LEN, XDS_IN_LEN, DEFAULT_ATTEMPTS,
DEFAULT_TIMEOUT);
return success;
}{ ... }
static bool swd_disconnect(void)
{
bool success;
xds110.write_payload[0] = SWD_DISCONNECT;
success = xds_execute(XDS_OUT_LEN, XDS_IN_LEN, DEFAULT_ATTEMPTS,
DEFAULT_TIMEOUT);
return success;
}{ ... }
static bool cjtag_connect(uint32_t format)
{
uint8_t *format_pntr = &xds110.write_payload[XDS_OUT_LEN + 0];
bool success;
xds110.write_payload[0] = CJTAG_CONNECT;
xds110_set_u32(format_pntr, format);
success = xds_execute(XDS_OUT_LEN + 4, XDS_IN_LEN, DEFAULT_ATTEMPTS,
DEFAULT_TIMEOUT);
return success;
}{ ... }
static bool cjtag_disconnect(void)
{
bool success;
xds110.write_payload[0] = CJTAG_DISCONNECT;
success = xds_execute(XDS_OUT_LEN, XDS_IN_LEN, DEFAULT_ATTEMPTS,
DEFAULT_TIMEOUT);
return success;
}{ ... }
static bool xds_set_supply(uint32_t voltage)
{
uint8_t *volts_pntr = &xds110.write_payload[XDS_OUT_LEN + 0];
uint8_t *source_pntr = &xds110.write_payload[XDS_OUT_LEN + 4];
bool success;
xds110.write_payload[0] = XDS_SET_SUPPLY;
xds110_set_u32(volts_pntr, voltage);
*source_pntr = (uint8_t)(voltage != 0 ? 1 : 0);
success = xds_execute(XDS_OUT_LEN + 5, XDS_IN_LEN, DEFAULT_ATTEMPTS,
DEFAULT_TIMEOUT);
return success;
}{ ... }
static bool ocd_dap_request(uint8_t *dap_requests, uint32_t request_size,
uint32_t *dap_results, uint32_t result_count)
{
uint8_t *request_pntr = &xds110.write_payload[XDS_OUT_LEN + 0];
uint8_t *result_pntr = &xds110.read_payload[XDS_IN_LEN + 0];
bool success;
if (!dap_requests || !dap_results)
return false;
xds110.write_payload[0] = OCD_DAP_REQUEST;
memcpy((void *)request_pntr, (void *)dap_requests, request_size);
success = xds_execute(XDS_OUT_LEN + request_size,
XDS_IN_LEN + (result_count * 4), DEFAULT_ATTEMPTS,
DEFAULT_TIMEOUT);
if (success && (result_count > 0))
memcpy((void *)dap_results, (void *)result_pntr, result_count * 4);
return success;
}{ ... }
static bool ocd_scan_request(uint8_t *scan_requests, uint32_t request_size,
uint8_t *scan_results, uint32_t result_size)
{
uint8_t *request_pntr = &xds110.write_payload[XDS_OUT_LEN + 0];
uint8_t *result_pntr = &xds110.read_payload[XDS_IN_LEN + 0];
bool success;
if (!scan_requests || !scan_results)
return false;
xds110.write_payload[0] = OCD_SCAN_REQUEST;
memcpy((void *)request_pntr, (void *)scan_requests, request_size);
success = xds_execute(XDS_OUT_LEN + request_size,
XDS_IN_LEN + result_size, DEFAULT_ATTEMPTS,
DEFAULT_TIMEOUT);
if (success && (result_size > 0))
memcpy((void *)scan_results, (void *)result_pntr, result_size);
return success;
}{ ... }
static bool ocd_pathmove(uint32_t num_states, uint8_t *path)
{
uint8_t *num_pntr = &xds110.write_payload[XDS_OUT_LEN + 0];
uint8_t *path_pntr = &xds110.write_payload[XDS_OUT_LEN + 4];
bool success;
if (!path)
return false;
xds110.write_payload[0] = OCD_PATHMOVE;
xds110_set_u32(num_pntr, num_states);
memcpy((void *)path_pntr, (void *)path, num_states);
success = xds_execute(XDS_OUT_LEN + 4 + num_states, XDS_IN_LEN,
DEFAULT_ATTEMPTS, DEFAULT_TIMEOUT);
return success;
}{ ... }
/* ... */
static int xds110_swd_init(void)
{
xds110.is_swd_mode = true;
return ERROR_OK;
}{ ... }
static int xds110_swd_switch_seq(enum swd_special_seq seq)
{
uint32_t idcode;
bool success;
switch (seq) {
case LINE_RESET:
LOG_ERROR("Sequence SWD line reset (%d) not supported", seq);
return ERROR_FAIL;case LINE_RESET:
case JTAG_TO_SWD:
LOG_DEBUG("JTAG-to-SWD");
xds110.is_swd_mode = false;
xds110.is_cmapi_connected = false;
xds110.is_cmapi_acquired = false;
success = swd_connect();
if (success) {
xds110.is_swd_mode = true;
success = cmapi_connect(&idcode);
if (success) {
xds110.is_cmapi_connected = true;
success = cmapi_acquire();
}if (success) { ... }
}if (success) { ... }
break;case JTAG_TO_SWD:
case SWD_TO_JTAG:
LOG_DEBUG("SWD-to-JTAG");
xds110.is_swd_mode = false;
xds110.is_cmapi_connected = false;
xds110.is_cmapi_acquired = false;
success = swd_disconnect();
if (success) {
success = cjtag_connect(MODE_JTAG);
}if (success) { ... }
break;case SWD_TO_JTAG:
default:
LOG_ERROR("Sequence %d not supported", seq);
return ERROR_FAIL;default
}switch (seq) { ... }
if (success)
return ERROR_OK;
else
return ERROR_FAIL;
}{ ... }
static bool xds110_legacy_read_reg(uint8_t cmd, uint32_t *value)
{
bool is_read_request = (0 != (SWD_CMD_RNW & cmd));
uint32_t type = (0 != (SWD_CMD_APNDP & cmd)) ? DAP_AP : DAP_DP;
uint32_t ap_num = (xds110.select & 0xff000000) >> 24;
uint32_t address = ((cmd & SWD_CMD_A32) >> 1);
uint32_t bank = (xds110.select & 0x000000f0);
uint32_t reg_value = 0;
uint32_t temp_value = 0;
bool success;
if (!is_read_request)
return false;
if (type == DAP_AP) {
address |= bank;
}if (type == DAP_AP) { ... }
if (DAP_DP == type && DAP_DP_RDBUFF == address && xds110.use_rdbuff) {
reg_value = xds110.rdbuff;
success = true;
}if (DAP_DP == type && DAP_DP_RDBUFF == address && xds110.use_rdbuff) { ... } else if (DAP_AP == type && DAP_AP_DRW == address && xds110.use_rdbuff) {
reg_value = xds110.rdbuff;
success = cmapi_read_dap_reg(type, ap_num, address, &temp_value);
}else if (DAP_AP == type && DAP_AP_DRW == address && xds110.use_rdbuff) { ... } else {
success = cmapi_read_dap_reg(type, ap_num, address, &temp_value);
if (success)
reg_value = temp_value;
}else { ... }
xds110.use_rdbuff = false;
if (!success)
LOG_ERROR("XDS110: failed to read DAP register");
else if (value)
*value = reg_value;
if (success && DAP_AP == type) {
/* ... */
xds110.use_rdbuff = true;
xds110.rdbuff = temp_value;
}if (success && DAP_AP == type) { ... }
return success;
}{ ... }
static bool xds110_legacy_write_reg(uint8_t cmd, uint32_t value)
{
bool is_read_request = (0 != (SWD_CMD_RNW & cmd));
uint32_t type = (0 != (SWD_CMD_APNDP & cmd)) ? DAP_AP : DAP_DP;
uint32_t ap_num = (xds110.select & 0xff000000) >> 24;
uint32_t address = ((cmd & SWD_CMD_A32) >> 1);
uint32_t bank = (xds110.select & 0x000000f0);
bool success;
if (is_read_request)
return false;
xds110.use_rdbuff = false;
if (type == DAP_AP) {
address |= bank;
xds110.is_ap_dirty = true;
}if (type == DAP_AP) { ... } else if (address == DAP_DP_SELECT) {
xds110.is_ap_dirty = true;
}else if (address == DAP_DP_SELECT) { ... }
success = cmapi_write_dap_reg(type, ap_num, address, &value);
if (!success) {
LOG_ERROR("XDS110: failed to write DAP register");
}if (!success) { ... } else {
/* ... */
if ((type == DAP_DP) && (address == DAP_DP_SELECT))
xds110.select = value;
}else { ... }
return success;
}{ ... }
static int xds110_swd_run_queue(void)
{
static uint32_t dap_results[MAX_RESULT_QUEUE];
uint8_t cmd;
uint32_t request;
uint32_t result;
uint32_t value;
bool success = true;
if (xds110.txn_request_size == 0)
return ERROR_OK;
xds110.txn_requests[xds110.txn_request_size++] = 0;
if (xds110.firmware >= OCD_FIRMWARE_VERSION) {
success = ocd_dap_request(xds110.txn_requests,
xds110.txn_request_size, dap_results, xds110.txn_result_count);
}if (xds110.firmware >= OCD_FIRMWARE_VERSION) { ... } else {
request = 0;
result = 0;
while (xds110.txn_requests[request] != 0) {
cmd = xds110.txn_requests[request++];
if (0 == (SWD_CMD_RNW & cmd)) {
value = (uint32_t)(xds110.txn_requests[request++]) << 0;
value |= (uint32_t)(xds110.txn_requests[request++]) << 8;
value |= (uint32_t)(xds110.txn_requests[request++]) << 16;
value |= (uint32_t)(xds110.txn_requests[request++]) << 24;
if (success)
success = xds110_legacy_write_reg(cmd, value);
}if (0 == (SWD_CMD_RNW & cmd)) { ... } else {
value = 0;
if (success)
success = xds110_legacy_read_reg(cmd, &value);
dap_results[result++] = value;
}else { ... }
}while (xds110.txn_requests[request] != 0) { ... }
}else { ... }
for (result = 0; result < xds110.txn_result_count; result++)
if (xds110.txn_dap_results[result])
*xds110.txn_dap_results[result] = dap_results[result];
xds110.txn_request_size = 0;
xds110.txn_result_size = 0;
xds110.txn_result_count = 0;
return (success) ? ERROR_OK : ERROR_FAIL;
}{ ... }
static void xds110_swd_queue_cmd(uint8_t cmd, uint32_t *value)
{
bool is_read_request = (0 != (SWD_CMD_RNW & cmd));
uint32_t type = (0 != (SWD_CMD_APNDP & cmd)) ? DAP_AP : DAP_DP;
uint32_t address = ((cmd & SWD_CMD_A32) >> 1);
uint32_t request_size = (is_read_request) ? 1 : 5;
if (((xds110.txn_request_size + request_size + 1) > MAX_DATA_BLOCK) ||
((xds110.txn_result_count + 1) > MAX_RESULT_QUEUE))
xds110_swd_run_queue();
cmd |= SWD_CMD_START;
if (is_read_request) {
xds110.txn_requests[xds110.txn_request_size++] = cmd;
xds110.txn_dap_results[xds110.txn_result_count++] = value;
xds110.txn_result_size += 4;
}if (is_read_request) { ... } else {
if (DAP_DP == type && DAP_DP_CTRL == address &&
(*value & CORUNDETECT)) {
LOG_DEBUG("XDS110: refusing to enable sticky overrun detection");
*value &= ~CORUNDETECT;
}if (DAP_DP == type && DAP_DP_CTRL == address && (*value & CORUNDETECT)) { ... }
xds110.txn_requests[xds110.txn_request_size++] = cmd;
xds110.txn_requests[xds110.txn_request_size++] = (*value >> 0) & 0xff;
xds110.txn_requests[xds110.txn_request_size++] = (*value >> 8) & 0xff;
xds110.txn_requests[xds110.txn_request_size++] = (*value >> 16) & 0xff;
xds110.txn_requests[xds110.txn_request_size++] = (*value >> 24) & 0xff;
}else { ... }
}{ ... }
static void xds110_swd_read_reg(uint8_t cmd, uint32_t *value,
uint32_t ap_delay_clk)
{
assert(cmd & SWD_CMD_RNW);
xds110_swd_queue_cmd(cmd, value);
}{ ... }
static void xds110_swd_write_reg(uint8_t cmd, uint32_t value,
uint32_t ap_delay_clk)
{
assert(!(cmd & SWD_CMD_RNW));
xds110_swd_queue_cmd(cmd, &value);
}{ ... }
/* ... */
static void xds110_show_info(void)
{
uint32_t firmware = xds110.firmware;
LOG_INFO("XDS110: vid/pid = %04x/%04x", xds110.vid, xds110.pid);
LOG_INFO("XDS110: firmware version = %" PRIu32 ".%" PRIu32 ".%" PRIu32 ".%" PRIu32,
(((firmware >> 28) & 0xf) * 10) + ((firmware >> 24) & 0xf),
(((firmware >> 20) & 0xf) * 10) + ((firmware >> 16) & 0xf),
(((firmware >> 12) & 0xf) * 10) + ((firmware >> 8) & 0xf),
(((firmware >> 4) & 0xf) * 10) + ((firmware >> 0) & 0xf));
LOG_INFO("XDS110: hardware version = 0x%04x", xds110.hardware);
if (adapter_get_required_serial())
LOG_INFO("XDS110: serial number = %s", adapter_get_required_serial());
if (xds110.is_swd_mode) {
LOG_INFO("XDS110: connected to target via SWD");
LOG_INFO("XDS110: SWCLK set to %" PRIu32 " kHz", xds110.speed);
}if (xds110.is_swd_mode) { ... } else {
LOG_INFO("XDS110: connected to target via JTAG");
LOG_INFO("XDS110: TCK set to %" PRIu32 " kHz", xds110.speed);
}else { ... }
if (firmware < OCD_FIRMWARE_VERSION) {
LOG_WARNING("XDS110: the firmware is not optimized for OpenOCD");
LOG_WARNING(OCD_FIRMWARE_UPGRADE);
}if (firmware < OCD_FIRMWARE_VERSION) { ... }
}{ ... }
static int xds110_quit(void)
{
if (xds110.is_cmapi_acquired) {
(void)cmapi_release();
xds110.is_cmapi_acquired = false;
}if (xds110.is_cmapi_acquired) { ... }
if (xds110.is_cmapi_connected) {
(void)cmapi_disconnect();
xds110.is_cmapi_connected = false;
}if (xds110.is_cmapi_connected) { ... }
if (xds110.is_connected) {
if (xds110.is_swd_mode) {
(void)swd_disconnect();
}if (xds110.is_swd_mode) { ... } else {
(void)cjtag_disconnect();
}else { ... }
(void)xds_disconnect();
xds110.is_connected = false;
}if (xds110.is_connected) { ... }
usb_disconnect();
return ERROR_OK;
}{ ... }
static int xds110_init(void)
{
bool success;
success = usb_connect();
if (success) {
success = xds_connect();
if (success)
xds110.is_connected = true;
}if (success) { ... }
if (success) {
uint32_t firmware;
uint16_t hardware;
success = xds_version(&firmware, &hardware);
if (success) {
xds110.firmware = firmware;
xds110.hardware = hardware;
}if (success) { ... }
}if (success) { ... }
if (success) {
if (xds110.hardware == XDS110_STAND_ALONE_ID) {
success = xds_set_supply(xds110.voltage);
alive_sleep(1500);
}if (xds110.hardware == XDS110_STAND_ALONE_ID) { ... } else if (xds110.voltage != 0) {
LOG_WARNING(
"XDS110: ignoring supply voltage, not supported on this probe");
}else if (xds110.voltage != 0) { ... }
}if (success) { ... }
if (success) {
success = xds_set_trst(0);
if (success)
success = xds_cycle_tck(50);
if (success)
success = xds_set_trst(1);
if (success)
success = xds_cycle_tck(50);
}if (success) { ... }
if (success) {
if (xds110.is_swd_mode) {
success = swd_connect();
}if (xds110.is_swd_mode) { ... } else {
success = cjtag_connect(MODE_JTAG);
}else { ... }
}if (success) { ... }
if (success && xds110.is_swd_mode) {
uint32_t idcode;
success = cmapi_connect(&idcode);
if (success) {
xds110.is_cmapi_connected = true;
success = cmapi_acquire();
if (success)
xds110.is_cmapi_acquired = true;
}if (success) { ... }
}if (success && xds110.is_swd_mode) { ... }
if (!success)
xds110_quit();
if (success)
xds110_show_info();
return (success) ? ERROR_OK : ERROR_FAIL;
}{ ... }
static void xds110_legacy_scan(uint32_t shift_state, uint32_t total_bits,
uint32_t end_state, uint8_t *data_out, uint8_t *data_in)
{
(void)xds_jtag_scan(shift_state, total_bits, end_state, data_out, data_in);
}{ ... }
static void xds110_legacy_runtest(uint32_t clocks, uint32_t end_state)
{
xds_goto_state(XDS_JTAG_STATE_IDLE);
xds_cycle_tck(clocks);
xds_goto_state(end_state);
}{ ... }
static void xds110_legacy_stableclocks(uint32_t clocks)
{
xds_cycle_tck(clocks);
}{ ... }
static void xds110_flush(void)
{
uint8_t command;
uint32_t clocks;
uint32_t shift_state;
uint32_t end_state;
uint32_t bits;
uint32_t bytes;
uint32_t request;
uint32_t result;
uint8_t *data_out;
uint8_t data_in[MAX_DATA_BLOCK];
uint8_t *data_pntr;
if (xds110.txn_request_size == 0)
return;
xds110.txn_requests[xds110.txn_request_size++] = 0;
if (xds110.firmware >= OCD_FIRMWARE_VERSION) {
(void)ocd_scan_request(xds110.txn_requests, xds110.txn_request_size,
data_in, xds110.txn_result_size);
}if (xds110.firmware >= OCD_FIRMWARE_VERSION) { ... } else {
request = 0;
result = 0;
while (xds110.txn_requests[request] != 0) {
command = xds110.txn_requests[request++];
switch (command) {
case CMD_IR_SCAN:
case CMD_DR_SCAN:
if (command == CMD_IR_SCAN)
shift_state = XDS_JTAG_STATE_SHIFT_IR;
else
shift_state = XDS_JTAG_STATE_SHIFT_DR;
end_state = (uint32_t)(xds110.txn_requests[request++]);
bits = (uint32_t)(xds110.txn_requests[request++]) << 0;
bits |= (uint32_t)(xds110.txn_requests[request++]) << 8;
data_out = &xds110.txn_requests[request];
bytes = DIV_ROUND_UP(bits, 8);
xds110_legacy_scan(shift_state, bits, end_state, data_out,
&data_in[result]);
result += bytes;
request += bytes;
break;case CMD_DR_SCAN:
case CMD_RUNTEST:
clocks = (uint32_t)(xds110.txn_requests[request++]) << 0;
clocks |= (uint32_t)(xds110.txn_requests[request++]) << 8;
clocks |= (uint32_t)(xds110.txn_requests[request++]) << 16;
clocks |= (uint32_t)(xds110.txn_requests[request++]) << 24;
end_state = (uint32_t)xds110.txn_requests[request++];
xds110_legacy_runtest(clocks, end_state);
break;case CMD_RUNTEST:
case CMD_STABLECLOCKS:
clocks = (uint32_t)(xds110.txn_requests[request++]) << 0;
clocks |= (uint32_t)(xds110.txn_requests[request++]) << 8;
clocks |= (uint32_t)(xds110.txn_requests[request++]) << 16;
clocks |= (uint32_t)(xds110.txn_requests[request++]) << 24;
xds110_legacy_stableclocks(clocks);
break;case CMD_STABLECLOCKS:
default:
LOG_ERROR("BUG: unknown JTAG command type 0x%x encountered",
command);
exit(-1);
break;default
}switch (command) { ... }
}while (xds110.txn_requests[request] != 0) { ... }
}else { ... }
bits = 0;
data_pntr = data_in;
for (result = 0; result < xds110.txn_result_count; result++) {
if (xds110.txn_scan_results[result].first) {
if (bits != 0) {
bytes = DIV_ROUND_UP(bits, 8);
data_pntr += bytes;
}if (bits != 0) { ... }
bits = 0;
}if (xds110.txn_scan_results[result].first) { ... }
if (xds110.txn_scan_results[result].buffer)
bit_copy(xds110.txn_scan_results[result].buffer, 0, data_pntr,
bits, xds110.txn_scan_results[result].num_bits);
bits += xds110.txn_scan_results[result].num_bits;
}for (result = 0; result < xds110.txn_result_count; result++) { ... }
xds110.txn_request_size = 0;
xds110.txn_result_size = 0;
xds110.txn_result_count = 0;
}{ ... }
static int xds110_reset(int trst, int srst)
{
uint8_t value;
bool success;
int retval = ERROR_OK;
if (trst != -1) {
if (trst == 0) {
value = 1;
}if (trst == 0) { ... } else {
value = 0;
}else { ... }
success = xds_set_trst(value);
if (!success)
retval = ERROR_FAIL;
}if (trst != -1) { ... }
if (srst != -1) {
if (srst == 0) {
value = 1;
}if (srst == 0) { ... } else {
value = 0;
}else { ... }
success = xds_set_srst(value);
if (!success)
retval = ERROR_FAIL;
if (success && !xds110.is_swd_mode) {
success = xds_cycle_tck(xds110.speed * 50);
}if (success && !xds110.is_swd_mode) { ... }
if (!success)
retval = ERROR_FAIL;
}if (srst != -1) { ... }
return retval;
}{ ... }
static void xds110_execute_sleep(struct jtag_command *cmd)
{
jtag_sleep(cmd->cmd.sleep->us);
}{ ... }
static void xds110_execute_tlr_reset(struct jtag_command *cmd)
{
(void)xds_goto_state(XDS_JTAG_STATE_RESET);
}{ ... }
static void xds110_execute_pathmove(struct jtag_command *cmd)
{
uint32_t num_states;
uint8_t *path;
num_states = (uint32_t)cmd->cmd.pathmove->num_states;
if (num_states == 0)
return;
path = malloc(num_states * sizeof(uint8_t));
if (!path) {
LOG_ERROR("XDS110: unable to allocate memory");
return;
}if (!path) { ... }
for (unsigned int i = 0; i < num_states; i++)
path[i] = (uint8_t)xds_jtag_state[cmd->cmd.pathmove->path[i]];
if (xds110.firmware >= OCD_FIRMWARE_VERSION) {
(void)ocd_pathmove(num_states, path);
}if (xds110.firmware >= OCD_FIRMWARE_VERSION) { ... } else {
LOG_ERROR("XDS110: the firmware does not support pathmove command");
LOG_ERROR(OCD_FIRMWARE_UPGRADE);
exit(-1);
}else { ... }
free((void *)path);
}{ ... }
static void xds110_queue_scan(struct jtag_command *cmd)
{
uint32_t offset;
uint32_t total_fields;
uint32_t total_bits;
uint32_t total_bytes;
uint8_t end_state;
uint8_t *buffer;
total_bits = 0;
total_fields = 0;
for (unsigned int i = 0; i < cmd->cmd.scan->num_fields; i++) {
total_fields++;
total_bits += (uint32_t)cmd->cmd.scan->fields[i].num_bits;
}for (unsigned int i = 0; i < cmd->cmd.scan->num_fields; i++) { ... }
if (total_bits == 0)
return;
total_bytes = DIV_ROUND_UP(total_bits, 8);
if (((xds110.txn_request_size + 1 + total_bytes + sizeof(end_state) + 1)
> MAX_DATA_BLOCK) || ((xds110.txn_result_count + total_fields) >
MAX_RESULT_QUEUE))
xds110_flush();
if ((1 + total_bytes + sizeof(end_state) + 1) > MAX_DATA_BLOCK) {
LOG_ERROR("BUG: JTAG scan request is too large to handle (%" PRIu32 " bits)",
total_bits);
exit(-1);
}if ((1 + total_bytes + sizeof(end_state) + 1) > MAX_DATA_BLOCK) { ... }
if (cmd->cmd.scan->ir_scan)
xds110.txn_requests[xds110.txn_request_size++] = CMD_IR_SCAN;
else
xds110.txn_requests[xds110.txn_request_size++] = CMD_DR_SCAN;
end_state = (uint8_t)xds_jtag_state[cmd->cmd.scan->end_state];
xds110.txn_requests[xds110.txn_request_size++] = end_state;
xds110.txn_requests[xds110.txn_request_size++] = (total_bits >> 0) & 0xff;
xds110.txn_requests[xds110.txn_request_size++] = (total_bits >> 8) & 0xff;
offset = 0;
buffer = &xds110.txn_requests[xds110.txn_request_size];
memset((void *)buffer, 0x00, total_bytes);
for (unsigned int i = 0; i < cmd->cmd.scan->num_fields; i++) {
if (cmd->cmd.scan->fields[i].out_value) {
bit_copy(buffer, offset, cmd->cmd.scan->fields[i].out_value, 0,
cmd->cmd.scan->fields[i].num_bits);
}if (cmd->cmd.scan->fields[i].out_value) { ... }
offset += cmd->cmd.scan->fields[i].num_bits;
xds110.txn_scan_results[xds110.txn_result_count].first = (i == 0);
xds110.txn_scan_results[xds110.txn_result_count].num_bits =
cmd->cmd.scan->fields[i].num_bits;
xds110.txn_scan_results[xds110.txn_result_count++].buffer =
cmd->cmd.scan->fields[i].in_value;
}for (unsigned int i = 0; i < cmd->cmd.scan->num_fields; i++) { ... }
xds110.txn_request_size += total_bytes;
xds110.txn_result_size += total_bytes;
}{ ... }
static void xds110_queue_runtest(struct jtag_command *cmd)
{
uint32_t clocks = cmd->cmd.stableclocks->num_cycles;
uint8_t end_state = (uint8_t)xds_jtag_state[cmd->cmd.runtest->end_state];
if ((xds110.txn_request_size + 1 + sizeof(clocks) + sizeof(end_state) + 1)
> MAX_DATA_BLOCK)
xds110_flush();
xds110.txn_requests[xds110.txn_request_size++] = CMD_RUNTEST;
xds110.txn_requests[xds110.txn_request_size++] = (clocks >> 0) & 0xff;
xds110.txn_requests[xds110.txn_request_size++] = (clocks >> 8) & 0xff;
xds110.txn_requests[xds110.txn_request_size++] = (clocks >> 16) & 0xff;
xds110.txn_requests[xds110.txn_request_size++] = (clocks >> 24) & 0xff;
xds110.txn_requests[xds110.txn_request_size++] = end_state;
}{ ... }
static void xds110_queue_stableclocks(struct jtag_command *cmd)
{
uint32_t clocks = cmd->cmd.stableclocks->num_cycles;
if ((xds110.txn_request_size + 1 + sizeof(clocks) + 1) > MAX_DATA_BLOCK)
xds110_flush();
xds110.txn_requests[xds110.txn_request_size++] = CMD_STABLECLOCKS;
xds110.txn_requests[xds110.txn_request_size++] = (clocks >> 0) & 0xff;
xds110.txn_requests[xds110.txn_request_size++] = (clocks >> 8) & 0xff;
xds110.txn_requests[xds110.txn_request_size++] = (clocks >> 16) & 0xff;
xds110.txn_requests[xds110.txn_request_size++] = (clocks >> 24) & 0xff;
}{ ... }
static void xds110_execute_command(struct jtag_command *cmd)
{
switch (cmd->type) {
case JTAG_SLEEP:
xds110_flush();
xds110_execute_sleep(cmd);
break;case JTAG_SLEEP:
case JTAG_TLR_RESET:
xds110_flush();
xds110_execute_tlr_reset(cmd);
break;case JTAG_TLR_RESET:
case JTAG_PATHMOVE:
xds110_flush();
xds110_execute_pathmove(cmd);
break;case JTAG_PATHMOVE:
case JTAG_SCAN:
xds110_queue_scan(cmd);
break;case JTAG_SCAN:
case JTAG_RUNTEST:
xds110_queue_runtest(cmd);
break;case JTAG_RUNTEST:
case JTAG_STABLECLOCKS:
xds110_queue_stableclocks(cmd);
break;case JTAG_STABLECLOCKS:
case JTAG_TMS:
default:
LOG_ERROR("BUG: unknown JTAG command type 0x%x encountered",
cmd->type);
exit(-1);default
}switch (cmd->type) { ... }
}{ ... }
static int xds110_execute_queue(struct jtag_command *cmd_queue)
{
struct jtag_command *cmd = cmd_queue;
while (cmd) {
xds110_execute_command(cmd);
cmd = cmd->next;
}while (cmd) { ... }
xds110_flush();
return ERROR_OK;
}{ ... }
static int xds110_speed(int speed)
{
double freq_to_use;
uint32_t delay_count;
bool success;
if (speed == 0) {
LOG_INFO("XDS110: RTCK not supported");
return ERROR_JTAG_NOT_IMPLEMENTED;
}if (speed == 0) { ... }
if (speed < XDS110_MIN_TCK_SPEED) {
LOG_INFO("XDS110: increase speed request: %d kHz to %d kHz minimum",
speed, XDS110_MIN_TCK_SPEED);
speed = XDS110_MIN_TCK_SPEED;
}if (speed < XDS110_MIN_TCK_SPEED) { ... }
if (xds110.firmware < FAST_TCK_FIRMWARE_VERSION) {
if (speed >= XDS110_MAX_SLOW_TCK_SPEED) {
if (speed > XDS110_MAX_SLOW_TCK_SPEED) {
LOG_INFO(
"XDS110: reduce speed request: %d kHz to %d kHz maximum",
speed, XDS110_MAX_SLOW_TCK_SPEED);
speed = XDS110_MAX_SLOW_TCK_SPEED;
}if (speed > XDS110_MAX_SLOW_TCK_SPEED) { ... }
delay_count = 0;
}if (speed >= XDS110_MAX_SLOW_TCK_SPEED) { ... } else {
const double XDS110_TCK_PULSE_INCREMENT = 66.0;
freq_to_use = speed * 1000;
delay_count = 0;
double one_giga = 1000000000;
double max_freq_pulse_duration = one_giga /
(XDS110_MAX_SLOW_TCK_SPEED * 1000);
double freq_to_pulse_width_in_ns = one_giga / freq_to_use;
/* ... */
double current_value = max_freq_pulse_duration;
while (current_value < freq_to_pulse_width_in_ns) {
current_value += XDS110_TCK_PULSE_INCREMENT;
++delay_count;
}while (current_value < freq_to_pulse_width_in_ns) { ... }
/* ... */
if (delay_count) {
double diff_freq_1 = freq_to_use -
(one_giga / (max_freq_pulse_duration +
(XDS110_TCK_PULSE_INCREMENT * delay_count)));
double diff_freq_2 = (one_giga / (max_freq_pulse_duration +
(XDS110_TCK_PULSE_INCREMENT * (delay_count - 1)))) -
freq_to_use;
if (diff_freq_1 > diff_freq_2)
--delay_count;
}if (delay_count) { ... }
}else { ... }
}if (xds110.firmware < FAST_TCK_FIRMWARE_VERSION) { ... } else {
if (speed >= XDS110_MAX_FAST_TCK_SPEED) {
if (speed > XDS110_MAX_FAST_TCK_SPEED) {
LOG_INFO(
"XDS110: reduce speed request: %d kHz to %d kHz maximum",
speed, XDS110_MAX_FAST_TCK_SPEED);
speed = XDS110_MAX_FAST_TCK_SPEED;
}if (speed > XDS110_MAX_FAST_TCK_SPEED) { ... }
delay_count = 0;
}if (speed >= XDS110_MAX_FAST_TCK_SPEED) { ... } else if (speed >= 12000 && xds110.firmware >=
FAST_TCK_PLUS_FIRMWARE_VERSION) {
delay_count = FAST_TCK_DELAY_12000_KHZ;
}else if (speed >= 12000 && xds110.firmware >= FAST_TCK_PLUS_FIRMWARE_VERSION) { ... } else if (speed >= 10000 && xds110.firmware >=
FAST_TCK_PLUS_FIRMWARE_VERSION) {
delay_count = FAST_TCK_DELAY_10000_KHZ;
}else if (speed >= 10000 && xds110.firmware >= FAST_TCK_PLUS_FIRMWARE_VERSION) { ... } else if (speed >= 8500) {
delay_count = FAST_TCK_DELAY_8500_KHZ;
}else if (speed >= 8500) { ... } else if (speed >= 5500) {
delay_count = FAST_TCK_DELAY_5500_KHZ;
}else if (speed >= 5500) { ... } else {
const double m = 17100000.0;
const double b = -1.02;
freq_to_use = speed * 1000;
double period = 1.0/freq_to_use;
double delay = m * period + b;
if (delay < 1.0)
delay_count = 1;
else
delay_count = (uint32_t)delay;
}else { ... }
}else { ... }
success = xds_set_tck_delay(delay_count);
if (success) {
xds110.delay_count = delay_count;
xds110.speed = speed;
}if (success) { ... }
return (success) ? ERROR_OK : ERROR_FAIL;
}{ ... }
static int xds110_speed_div(int speed, int *khz)
{
*khz = speed;
return ERROR_OK;
}{ ... }
static int xds110_khz(int khz, int *jtag_speed)
{
*jtag_speed = khz;
return ERROR_OK;
}{ ... }
COMMAND_HANDLER(xds110_handle_info_command)
{
xds110_show_info();
return ERROR_OK;
}{ ... }
COMMAND_HANDLER(xds110_handle_supply_voltage_command)
{
uint32_t voltage = 0;
if (CMD_ARGC == 1) {
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], voltage);
if (voltage == 0 || (voltage >= XDS110_MIN_VOLTAGE && voltage
<= XDS110_MAX_VOLTAGE)) {
xds110.voltage = voltage;
}if (voltage == 0 || (voltage >= XDS110_MIN_VOLTAGE && voltage <= XDS110_MAX_VOLTAGE)) { ... } else {
LOG_ERROR("XDS110: voltage must be 0 or between %d and %d "
"millivolts", XDS110_MIN_VOLTAGE, XDS110_MAX_VOLTAGE);
return ERROR_FAIL;
}else { ... }
xds110.voltage = voltage;
}if (CMD_ARGC == 1) { ... } else
return ERROR_COMMAND_SYNTAX_ERROR;
return ERROR_OK;
}{ ... }
static const struct command_registration xds110_subcommand_handlers[] = {
{
.name = "info",
.handler = &xds110_handle_info_command,
.mode = COMMAND_EXEC,
.help = "show XDS110 info",
.usage = "",
...},
{
.name = "supply",
.handler = &xds110_handle_supply_voltage_command,
.mode = COMMAND_CONFIG,
.help = "set the XDS110 probe supply voltage",
.usage = "voltage_in_millivolts",
...},
COMMAND_REGISTRATION_DONE
...};
static const struct command_registration xds110_command_handlers[] = {
{
.name = "xds110",
.mode = COMMAND_ANY,
.help = "perform XDS110 management",
.usage = "",
.chain = xds110_subcommand_handlers,
...},
COMMAND_REGISTRATION_DONE
...};
static const struct swd_driver xds110_swd_driver = {
.init = xds110_swd_init,
.switch_seq = xds110_swd_switch_seq,
.read_reg = xds110_swd_read_reg,
.write_reg = xds110_swd_write_reg,
.run = xds110_swd_run_queue,
...};
static const char * const xds110_transport[] = { "swd", "jtag", NULL };
static struct jtag_interface xds110_interface = {
.execute_queue = xds110_execute_queue,
...};
struct adapter_driver xds110_adapter_driver = {
.name = "xds110",
.transports = xds110_transport,
.commands = xds110_command_handlers,
.init = xds110_init,
.quit = xds110_quit,
.reset = xds110_reset,
.speed = xds110_speed,
.khz = xds110_khz,
.speed_div = xds110_speed_div,
.jtag_ops = &xds110_interface,
.swd_ops = &xds110_swd_driver,
...};