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
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
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
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
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
269
270
285
286
291
292
293
294
295
296
297
298
299
300
301
305
306
307
308
312
313
314
315
316
317
318
319
320
323
328
329
330
331
332
333
334
335
338
347
348
349
350
351
352
353
354
355
356
357
358
359
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
412
413
414
415
416
417
418
419
420
421
422
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
471
472
473
474
475
476
477
478
479
480
481
482
483
487
488
492
493
494
495
496
497
498
499
500
501
502
503
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
525
526
527
528
529
530
531
532
533
534
535
536
537
541
542
543
544
545
546
547
548
549
550
551
552
553
554
558
559
560
561
562
563
564
565
566
567
568
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
599
600
601
602
603
608
609
610
611
612
613
614
615
616
620
621
622
623
624
628
629
630
631
632
633
634
635
636
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
670
671
672
673
674
675
676
677
683
684
685
686
687
688
689
690
691
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
720
721
722
723
724
725
726
727
728
729
736
737
738
739
740
741
742
743
744
745
746
747
748
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
798
799
803
804
805
806
807
808
809
810
811
812
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
848
849
850
851
852
856
857
858
859
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
885
886
897
898
899
903
904
905
906
907
908
919
920
921
928
929
930
931
938
943
944
951
952
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1051
1052
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
1104
1105
1112
1113
1114
1115
1120
1121
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
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
1234
1235
1236
1237
1238
1240
1241
1242
1243
1244
1245
1246
1251
1256
1261
1266
1271
1276
1281
1284
1285
1286
1287
1288
1289
1290
1292
1293
1294
1295
1296
1297
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1339
1340
1344
1345
1346
1347
1348
1349
1350
1351
1352
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1406
1407
1409
1410
1411
1413
1414
1415
1416
1417
1420
1421
1422
1423
1424
1431
1432
1434
1435
1436
1437
1438
1439
1440
1448
1449
1450
1451
1452
1453
1454
1455
1456
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1524
1525
1526
1527
1528
1529
1530
1531
1537
1543
1544
1545
1546
1547
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
1582
1583
1584
1585
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1618
1619
1620
1624
1625
1626
1627
1628
1629
1630
1631
1632
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
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
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1754
1755
1759
1760
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1916
1917
1922
1923
1925
1926
1927
1928
1932
1936
1939
1942
1945
1948
1951
1954
1955
1956
1957
1958
1959
1960
1961
1965
1966
1967
1968
1969
1970
1971
1980
1981
1986
1987
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2036
2037
2042
2043
2044
2054
2055
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2104
2105
2106
2107
2108
2109
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2193
2194
2198
2199
2200
2201
2202
2203
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <transport/transport.h>
#include "helper/replacements.h"
#include <jtag/adapter.h>
#include <jtag/swd.h>
#include <jtag/interface.h>
#include <jtag/commands.h>
#include <jtag/tcl.h>
#include <target/cortex_m.h>
#include "cmsis_dap.h"
#include "libusb_helper.h"
10 includes
static const struct cmsis_dap_backend *const cmsis_dap_backends[] = {
#if BUILD_CMSIS_DAP_USB == 1
&cmsis_dap_usb_backend,
#endif
#if BUILD_CMSIS_DAP_HID == 1
&cmsis_dap_hid_backend,
#endif
...};
/* ... */
#define MAX_USB_IDS 8
static uint16_t cmsis_dap_vid[MAX_USB_IDS + 1] = { 0 };
static uint16_t cmsis_dap_pid[MAX_USB_IDS + 1] = { 0 };
static int cmsis_dap_backend = -1;
static bool swd_mode;
#define CMD_DAP_INFO 0x00
#define CMD_DAP_LED 0x01
#define CMD_DAP_CONNECT 0x02
#define CMD_DAP_DISCONNECT 0x03
#define CMD_DAP_WRITE_ABORT 0x08
#define CMD_DAP_DELAY 0x09
#define CMD_DAP_RESET_TARGET 0x0A
#define INFO_ID_VENDOR 0x01
#define INFO_ID_PRODUCT 0x02
#define INFO_ID_SERNUM 0x03
#define INFO_ID_FW_VER 0x04
#define INFO_ID_TD_VEND 0x05
#define INFO_ID_TD_NAME 0x06
#define INFO_ID_CAPS 0xf0
#define INFO_ID_PKT_CNT 0xfe
#define INFO_ID_PKT_SZ 0xff
#define INFO_ID_SWO_BUF_SZ 0xfd
#define INFO_CAPS_SWD BIT(0)
#define INFO_CAPS_JTAG BIT(1)
#define INFO_CAPS_SWO_UART BIT(2)
#define INFO_CAPS_SWO_MANCHESTER BIT(3)
#define INFO_CAPS_ATOMIC_CMDS BIT(4)
#define INFO_CAPS_TEST_DOMAIN_TIMER BIT(5)
#define INFO_CAPS_SWO_STREAMING_TRACE BIT(6)
#define INFO_CAPS_UART_PORT BIT(7)
#define INFO_CAPS_USB_COM_PORT BIT(8)
#define INFO_CAPS__NUM_CAPS 9
#define LED_ID_CONNECT 0x00
#define LED_ID_RUN 0x01
#define LED_OFF 0x00
#define LED_ON 0x01
#define CONNECT_DEFAULT 0x00
#define CONNECT_SWD 0x01
#define CONNECT_JTAG 0x02
#define CMD_DAP_DELAY 0x09
#define CMD_DAP_SWJ_PINS 0x10
#define CMD_DAP_SWJ_CLOCK 0x11
#define CMD_DAP_SWJ_SEQ 0x12
/* ... */
#define SWJ_PIN_TCK (1<<0)
#define SWJ_PIN_TMS (1<<1)
#define SWJ_PIN_TDI (1<<2)
#define SWJ_PIN_TDO (1<<3)
#define SWJ_PIN_TRST (1<<5)
#define SWJ_PIN_SRST (1<<7)
#define CMD_DAP_SWD_CONFIGURE 0x13
#define CMD_DAP_SWD_SEQUENCE 0x1D
#define CMD_DAP_JTAG_SEQ 0x14
#define CMD_DAP_JTAG_CONFIGURE 0x15
#define CMD_DAP_JTAG_IDCODE 0x16
#define DAP_JTAG_SEQ_TCK 0x3F
#define DAP_JTAG_SEQ_TMS 0x40
#define DAP_JTAG_SEQ_TDO 0x80
#define CMD_DAP_TFER_CONFIGURE 0x04
#define CMD_DAP_TFER 0x05
#define CMD_DAP_TFER_BLOCK 0x06
#define CMD_DAP_TFER_ABORT 0x07
/* ... */
#define CMD_DAP_TFER_BLOCK_MIN_OPS 4
#define DAP_OK 0
#define DAP_ERROR 0xFF
#define CMD_DAP_SWO_TRANSPORT 0x17
#define CMD_DAP_SWO_MODE 0x18
#define CMD_DAP_SWO_BAUDRATE 0x19
#define CMD_DAP_SWO_CONTROL 0x1A
#define CMD_DAP_SWO_STATUS 0x1B
#define CMD_DAP_SWO_DATA 0x1C
#define CMD_DAP_SWO_EX_STATUS 0x1E
#define DAP_SWO_TRANSPORT_NONE 0
#define DAP_SWO_TRANSPORT_DATA 1
#define DAP_SWO_TRANSPORT_WINUSB 2
#define DAP_SWO_MODE_OFF 0
#define DAP_SWO_MODE_UART 1
#define DAP_SWO_MODE_MANCHESTER 2
#define DAP_SWO_CONTROL_STOP 0
#define DAP_SWO_CONTROL_START 1
#define DAP_SWO_STATUS_CAPTURE_INACTIVE 0
#define DAP_SWO_STATUS_CAPTURE_ACTIVE 1
#define DAP_SWO_STATUS_CAPTURE_MASK BIT(0)
#define DAP_SWO_STATUS_STREAM_ERROR_MASK BIT(6)
#define DAP_SWO_STATUS_BUFFER_OVERRUN_MASK BIT(7)
79 defines
/* ... */
static const char * const info_caps_str[INFO_CAPS__NUM_CAPS] = {
"SWD supported",
"JTAG supported",
"SWO-UART supported",
"SWO-MANCHESTER supported",
"Atomic commands supported",
"Test domain timer supported",
"SWO streaming trace supported",
"UART communication port supported",
"UART via USB COM port supported",
...};
struct pending_scan_result {
unsigned int first;
unsigned int length;
uint8_t *buffer;
unsigned int buffer_offset;
...};
enum cmsis_dap_blocking {
CMSIS_DAP_NON_BLOCKING,
CMSIS_DAP_BLOCKING
...};
static unsigned int pending_queue_len;
static unsigned int tfer_max_command_size;
static unsigned int tfer_max_response_size;
#define MAX_PENDING_SCAN_RESULTS 256
static int pending_scan_result_count;
static struct pending_scan_result pending_scan_results[MAX_PENDING_SCAN_RESULTS];
#define QUEUED_SEQ_BUF_LEN (cmsis_dap_handle->packet_usable_size - 3)
static int queued_seq_count;
static int queued_seq_buf_end;
static int queued_seq_tdo_ptr;
static uint8_t queued_seq_buf[1024];
static int queued_retval;
static uint8_t output_pins = SWJ_PIN_SRST | SWJ_PIN_TRST;
static struct cmsis_dap *cmsis_dap_handle;
static int cmsis_dap_quit(void);
static int cmsis_dap_open(void)
{
const struct cmsis_dap_backend *backend = NULL;
struct cmsis_dap *dap = calloc(1, sizeof(struct cmsis_dap));
if (!dap) {
LOG_ERROR("unable to allocate memory");
return ERROR_FAIL;
}if (!dap) { ... }
if (cmsis_dap_backend >= 0) {
backend = cmsis_dap_backends[cmsis_dap_backend];
if (backend->open(dap, cmsis_dap_vid, cmsis_dap_pid, adapter_get_required_serial()) != ERROR_OK)
backend = NULL;
}if (cmsis_dap_backend >= 0) { ... } else {
for (unsigned int i = 0; i < ARRAY_SIZE(cmsis_dap_backends); i++) {
backend = cmsis_dap_backends[i];
if (backend->open(dap, cmsis_dap_vid, cmsis_dap_pid, adapter_get_required_serial()) == ERROR_OK)
break;
else
backend = NULL;
}for (unsigned int i = 0; i < ARRAY_SIZE(cmsis_dap_backends); i++) { ... }
}else { ... }
if (!backend) {
LOG_ERROR("unable to find a matching CMSIS-DAP device");
free(dap);
return ERROR_FAIL;
}if (!backend) { ... }
dap->backend = backend;
cmsis_dap_handle = dap;
return ERROR_OK;
}{ ... }
static void cmsis_dap_close(struct cmsis_dap *dap)
{
if (dap->backend) {
dap->backend->close(dap);
dap->backend = NULL;
}if (dap->backend) { ... }
free(dap->packet_buffer);
for (unsigned int i = 0; i < MAX_PENDING_REQUESTS; i++) {
free(dap->pending_fifo[i].transfers);
dap->pending_fifo[i].transfers = NULL;
}for (unsigned int i = 0; i < MAX_PENDING_REQUESTS; i++) { ... }
free(cmsis_dap_handle);
cmsis_dap_handle = NULL;
}{ ... }
static void cmsis_dap_flush_read(struct cmsis_dap *dap)
{
unsigned int i;
/* ... */
for (i = 0; i < 64; i++) {
int retval = dap->backend->read(dap, 10, NULL);
if (retval == ERROR_TIMEOUT_REACHED)
break;
}for (i = 0; i < 64; i++) { ... }
if (i)
LOG_DEBUG("Flushed %u packets", i);
}{ ... }
static int cmsis_dap_xfer(struct cmsis_dap *dap, int txlen)
{
if (dap->write_count + dap->read_count) {
LOG_ERROR("internal: queue not empty before xfer");
}if (dap->write_count + dap->read_count) { ... }
if (dap->pending_fifo_block_count) {
LOG_ERROR("pending %u blocks, flushing", dap->pending_fifo_block_count);
while (dap->pending_fifo_block_count) {
dap->backend->read(dap, 10, NULL);
dap->pending_fifo_block_count--;
}while (dap->pending_fifo_block_count) { ... }
dap->pending_fifo_put_idx = 0;
dap->pending_fifo_get_idx = 0;
}if (dap->pending_fifo_block_count) { ... }
uint8_t current_cmd = dap->command[0];
int retval = dap->backend->write(dap, txlen, LIBUSB_TIMEOUT_MS);
if (retval < 0)
return retval;
retval = dap->backend->read(dap, LIBUSB_TIMEOUT_MS, NULL);
if (retval < 0)
return retval;
uint8_t *resp = dap->response;
if (resp[0] == DAP_ERROR) {
LOG_ERROR("CMSIS-DAP command 0x%" PRIx8 " not implemented", current_cmd);
return ERROR_NOT_IMPLEMENTED;
}if (resp[0] == DAP_ERROR) { ... }
if (resp[0] != current_cmd) {
LOG_ERROR("CMSIS-DAP command mismatch. Sent 0x%" PRIx8
" received 0x%" PRIx8, current_cmd, resp[0]);
dap->backend->cancel_all(dap);
cmsis_dap_flush_read(dap);
return ERROR_FAIL;
}if (resp[0] != current_cmd) { ... }
return ERROR_OK;
}{ ... }
static int cmsis_dap_cmd_dap_swj_pins(uint8_t pins, uint8_t mask, uint32_t delay, uint8_t *input)
{
uint8_t *command = cmsis_dap_handle->command;
command[0] = CMD_DAP_SWJ_PINS;
command[1] = pins;
command[2] = mask;
h_u32_to_le(&command[3], delay);
int retval = cmsis_dap_xfer(cmsis_dap_handle, 7);
if (retval != ERROR_OK) {
LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_PINS failed.");
return ERROR_JTAG_DEVICE_ERROR;
}if (retval != ERROR_OK) { ... }
if (input)
*input = cmsis_dap_handle->response[1];
return ERROR_OK;
}{ ... }
static int cmsis_dap_cmd_dap_swj_clock(uint32_t swj_clock)
{
uint8_t *command = cmsis_dap_handle->command;
swj_clock *= 1000;
command[0] = CMD_DAP_SWJ_CLOCK;
h_u32_to_le(&command[1], swj_clock);
int retval = cmsis_dap_xfer(cmsis_dap_handle, 5);
if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_CLOCK failed.");
return ERROR_JTAG_DEVICE_ERROR;
}if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) { ... }
return ERROR_OK;
}{ ... }
static int cmsis_dap_cmd_dap_swj_sequence(uint8_t s_len, const uint8_t *sequence)
{
uint8_t *command = cmsis_dap_handle->command;
#ifdef CMSIS_DAP_JTAG_DEBUG
LOG_DEBUG("cmsis-dap TMS sequence: len=%d", s_len);
for (unsigned int i = 0; i < DIV_ROUND_UP(s_len, 8); ++i)
printf("%02X ", sequence[i]);
printf("\n");/* ... */
#endif
command[0] = CMD_DAP_SWJ_SEQ;
command[1] = s_len;
bit_copy(&command[2], 0, sequence, 0, s_len);
int retval = cmsis_dap_xfer(cmsis_dap_handle, 2 + DIV_ROUND_UP(s_len, 8));
if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK)
return ERROR_FAIL;
return ERROR_OK;
}{ ... }
static int cmsis_dap_cmd_dap_info(uint8_t info, uint8_t **data)
{
uint8_t *command = cmsis_dap_handle->command;
command[0] = CMD_DAP_INFO;
command[1] = info;
int retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
if (retval != ERROR_OK) {
LOG_ERROR("CMSIS-DAP command CMD_INFO failed.");
return ERROR_JTAG_DEVICE_ERROR;
}if (retval != ERROR_OK) { ... }
*data = &cmsis_dap_handle->response[1];
return ERROR_OK;
}{ ... }
static int cmsis_dap_cmd_dap_led(uint8_t led, uint8_t state)
{
uint8_t *command = cmsis_dap_handle->command;
command[0] = CMD_DAP_LED;
command[1] = led;
command[2] = state;
int retval = cmsis_dap_xfer(cmsis_dap_handle, 3);
if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
LOG_ERROR("CMSIS-DAP command CMD_LED failed.");
return ERROR_JTAG_DEVICE_ERROR;
}if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) { ... }
return ERROR_OK;
}{ ... }
static int cmsis_dap_cmd_dap_connect(uint8_t mode)
{
uint8_t *command = cmsis_dap_handle->command;
command[0] = CMD_DAP_CONNECT;
command[1] = mode;
int retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
if (retval != ERROR_OK) {
LOG_ERROR("CMSIS-DAP command CMD_CONNECT failed.");
return ERROR_JTAG_DEVICE_ERROR;
}if (retval != ERROR_OK) { ... }
if (cmsis_dap_handle->response[1] != mode) {
LOG_ERROR("CMSIS-DAP failed to connect in mode (%d)", mode);
return ERROR_JTAG_DEVICE_ERROR;
}if (cmsis_dap_handle->response[1] != mode) { ... }
return ERROR_OK;
}{ ... }
static int cmsis_dap_cmd_dap_disconnect(void)
{
uint8_t *command = cmsis_dap_handle->command;
command[0] = CMD_DAP_DISCONNECT;
int retval = cmsis_dap_xfer(cmsis_dap_handle, 1);
if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
LOG_ERROR("CMSIS-DAP command CMD_DISCONNECT failed.");
return ERROR_JTAG_DEVICE_ERROR;
}if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) { ... }
return ERROR_OK;
}{ ... }
static int cmsis_dap_cmd_dap_tfer_configure(uint8_t idle, uint16_t retry_count, uint16_t match_retry)
{
uint8_t *command = cmsis_dap_handle->command;
command[0] = CMD_DAP_TFER_CONFIGURE;
command[1] = idle;
h_u16_to_le(&command[2], retry_count);
h_u16_to_le(&command[4], match_retry);
int retval = cmsis_dap_xfer(cmsis_dap_handle, 6);
if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
LOG_ERROR("CMSIS-DAP command CMD_TFER_Configure failed.");
return ERROR_JTAG_DEVICE_ERROR;
}if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) { ... }
return ERROR_OK;
}{ ... }
static int cmsis_dap_cmd_dap_swd_configure(uint8_t cfg)
{
uint8_t *command = cmsis_dap_handle->command;
command[0] = CMD_DAP_SWD_CONFIGURE;
command[1] = cfg;
int retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
LOG_ERROR("CMSIS-DAP command CMD_SWD_Configure failed.");
return ERROR_JTAG_DEVICE_ERROR;
}if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) { ... }
return ERROR_OK;
}{ ... }
#if 0
static int cmsis_dap_cmd_dap_delay(uint16_t delay_us)
{
uint8_t *command = cmsis_dap_handle->command;
command[0] = CMD_DAP_DELAY;
h_u16_to_le(&command[1], delay_us);
int retval = cmsis_dap_xfer(cmsis_dap_handle, 3);
if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
LOG_ERROR("CMSIS-DAP command CMD_Delay failed.");
return ERROR_JTAG_DEVICE_ERROR;
}if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) { ... }
return ERROR_OK;
}cmsis_dap_cmd_dap_delay (uint16_t delay_us) { ... }
/* ... */#endif
static int cmsis_dap_metacmd_targetsel(uint32_t instance_id)
{
uint8_t *command = cmsis_dap_handle->command;
const uint32_t SEQ_RD = 0x80, SEQ_WR = 0x00;
/* ... */
LOG_DEBUG_IO("DP write reg TARGETSEL %" PRIx32, instance_id);
size_t idx = 0;
command[idx++] = CMD_DAP_SWD_SEQUENCE;
command[idx++] = 3;
command[idx++] = SEQ_WR | 8;
command[idx++] = SWD_CMD_START | swd_cmd(false, false, DP_TARGETSEL) | SWD_CMD_STOP | SWD_CMD_PARK;
command[idx++] = SEQ_RD | 5;
command[idx++] = SEQ_WR | (32 + 1);
h_u32_to_le(command + idx, instance_id);
idx += 4;
command[idx++] = parity_u32(instance_id);
int retval = cmsis_dap_xfer(cmsis_dap_handle, idx);
if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
LOG_ERROR("CMSIS-DAP command SWD_Sequence failed.");
return ERROR_JTAG_DEVICE_ERROR;
}if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) { ... }
return ERROR_OK;
}{ ... }
/* ... */
static int cmsis_dap_cmd_dap_swo_transport(uint8_t transport)
{
uint8_t *command = cmsis_dap_handle->command;
command[0] = CMD_DAP_SWO_TRANSPORT;
command[1] = transport;
int retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
LOG_ERROR("CMSIS-DAP: command CMD_SWO_Transport(%d) failed.", transport);
return ERROR_JTAG_DEVICE_ERROR;
}if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) { ... }
return ERROR_OK;
}{ ... }
/* ... */
static int cmsis_dap_cmd_dap_swo_mode(uint8_t mode)
{
uint8_t *command = cmsis_dap_handle->command;
command[0] = CMD_DAP_SWO_MODE;
command[1] = mode;
int retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
LOG_ERROR("CMSIS-DAP: command CMD_SWO_Mode(%d) failed.", mode);
return ERROR_JTAG_DEVICE_ERROR;
}if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) { ... }
return ERROR_OK;
}{ ... }
/* ... */
static int cmsis_dap_cmd_dap_swo_baudrate(
uint32_t in_baudrate,
uint32_t *dev_baudrate)
{
uint8_t *command = cmsis_dap_handle->command;
command[0] = CMD_DAP_SWO_BAUDRATE;
h_u32_to_le(&command[1], in_baudrate);
int retval = cmsis_dap_xfer(cmsis_dap_handle, 5);
uint32_t rvbr = le_to_h_u32(&cmsis_dap_handle->response[1]);
if (retval != ERROR_OK || rvbr == 0) {
LOG_ERROR("CMSIS-DAP: command CMD_SWO_Baudrate(%u) -> %u failed.", in_baudrate, rvbr);
if (dev_baudrate)
*dev_baudrate = 0;
return ERROR_JTAG_DEVICE_ERROR;
}if (retval != ERROR_OK || rvbr == 0) { ... }
if (dev_baudrate)
*dev_baudrate = rvbr;
return ERROR_OK;
}{ ... }
/* ... */
static int cmsis_dap_cmd_dap_swo_control(uint8_t control)
{
uint8_t *command = cmsis_dap_handle->command;
command[0] = CMD_DAP_SWO_CONTROL;
command[1] = control;
int retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
LOG_ERROR("CMSIS-DAP: command CMD_SWO_Control(%d) failed.", control);
return ERROR_JTAG_DEVICE_ERROR;
}if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) { ... }
return ERROR_OK;
}{ ... }
/* ... */
static int cmsis_dap_cmd_dap_swo_status(
uint8_t *trace_status,
size_t *trace_count)
{
uint8_t *command = cmsis_dap_handle->command;
command[0] = CMD_DAP_SWO_STATUS;
int retval = cmsis_dap_xfer(cmsis_dap_handle, 1);
if (retval != ERROR_OK) {
LOG_ERROR("CMSIS-DAP: command CMD_SWO_Status failed.");
return ERROR_JTAG_DEVICE_ERROR;
}if (retval != ERROR_OK) { ... }
if (trace_status)
*trace_status = cmsis_dap_handle->response[1];
if (trace_count)
*trace_count = le_to_h_u32(&cmsis_dap_handle->response[2]);
return ERROR_OK;
}{ ... }
/* ... */
static int cmsis_dap_cmd_dap_swo_data(
size_t max_trace_count,
uint8_t *trace_status,
size_t *trace_count,
uint8_t *data)
{
uint8_t *command = cmsis_dap_handle->command;
command[0] = CMD_DAP_SWO_DATA;
h_u16_to_le(&command[1], max_trace_count);
int retval = cmsis_dap_xfer(cmsis_dap_handle, 3);
if (retval != ERROR_OK) {
LOG_ERROR("CMSIS-DAP: command CMD_SWO_Data failed.");
return ERROR_JTAG_DEVICE_ERROR;
}if (retval != ERROR_OK) { ... }
*trace_status = cmsis_dap_handle->response[1];
*trace_count = le_to_h_u16(&cmsis_dap_handle->response[2]);
if (*trace_count > 0)
memcpy(data, &cmsis_dap_handle->response[4], *trace_count);
return ERROR_OK;
}{ ... }
static void cmsis_dap_swd_discard_all_pending(struct cmsis_dap *dap)
{
for (unsigned int i = 0; i < MAX_PENDING_REQUESTS; i++)
dap->pending_fifo[i].transfer_count = 0;
dap->pending_fifo_put_idx = 0;
dap->pending_fifo_get_idx = 0;
dap->pending_fifo_block_count = 0;
}{ ... }
static void cmsis_dap_swd_cancel_transfers(struct cmsis_dap *dap)
{
dap->backend->cancel_all(dap);
cmsis_dap_flush_read(dap);
cmsis_dap_swd_discard_all_pending(dap);
}{ ... }
static void cmsis_dap_swd_write_from_queue(struct cmsis_dap *dap)
{
uint8_t *command = dap->command;
struct pending_request_block *block = &dap->pending_fifo[dap->pending_fifo_put_idx];
assert(dap->write_count + dap->read_count == block->transfer_count);
dap->write_count = 0;
dap->read_count = 0;
LOG_DEBUG_IO("Executing %d queued transactions from FIFO index %u%s",
block->transfer_count, dap->pending_fifo_put_idx,
cmsis_dap_handle->swd_cmds_differ ? "" : ", same swd ops");
if (queued_retval != ERROR_OK) {
LOG_DEBUG("Skipping due to previous errors: %d", queued_retval);
goto skip;
}if (queued_retval != ERROR_OK) { ... }
if (block->transfer_count == 0) {
LOG_ERROR("internal: write an empty queue?!");
goto skip;
}if (block->transfer_count == 0) { ... }
bool block_cmd = !cmsis_dap_handle->swd_cmds_differ
&& block->transfer_count >= CMD_DAP_TFER_BLOCK_MIN_OPS;
block->command = block_cmd ? CMD_DAP_TFER_BLOCK : CMD_DAP_TFER;
command[0] = block->command;
command[1] = 0x00;
unsigned int idx;
if (block_cmd) {
h_u16_to_le(&command[2], block->transfer_count);
idx = 4;
}if (block_cmd) { ... } else {
command[2] = block->transfer_count;
idx = 3;
}else { ... }
for (unsigned int i = 0; i < block->transfer_count; i++) {
struct pending_transfer_result *transfer = &(block->transfers[i]);
uint8_t cmd = transfer->cmd;
uint32_t data = transfer->data;
LOG_DEBUG_IO("%s %s reg %x %" PRIx32,
cmd & SWD_CMD_APNDP ? "AP" : "DP",
cmd & SWD_CMD_RNW ? "read" : "write",
(cmd & SWD_CMD_A32) >> 1, data);
/* ... */
if (!(cmd & SWD_CMD_RNW) &&
!(cmd & SWD_CMD_APNDP) &&
(cmd & SWD_CMD_A32) >> 1 == DP_CTRL_STAT &&
(data & CORUNDETECT)) {
LOG_DEBUG("refusing to enable sticky overrun detection");
data &= ~CORUNDETECT;
}if (!(cmd & SWD_CMD_RNW) && !(cmd & SWD_CMD_APNDP) && (cmd & SWD_CMD_A32) >> 1 == DP_CTRL_STAT && (data & CORUNDETECT)) { ... }
if (!block_cmd || i == 0)
command[idx++] = (cmd >> 1) & 0x0f;
if (!(cmd & SWD_CMD_RNW)) {
h_u32_to_le(&command[idx], data);
idx += 4;
}if (!(cmd & SWD_CMD_RNW)) { ... }
}for (unsigned int i = 0; i < block->transfer_count; i++) { ... }
int retval = dap->backend->write(dap, idx, LIBUSB_TIMEOUT_MS);
if (retval < 0) {
queued_retval = retval;
goto skip;
}if (retval < 0) { ... }
unsigned int packet_count = dap->quirk_mode ? 1 : dap->packet_count;
dap->pending_fifo_put_idx = (dap->pending_fifo_put_idx + 1) % packet_count;
dap->pending_fifo_block_count++;
if (dap->pending_fifo_block_count > packet_count)
LOG_ERROR("internal: too much pending writes %u", dap->pending_fifo_block_count);
return;
skip:
block->transfer_count = 0;
}{ ... }
static void cmsis_dap_swd_read_process(struct cmsis_dap *dap, enum cmsis_dap_blocking blocking)
{
int retval;
struct pending_request_block *block = &dap->pending_fifo[dap->pending_fifo_get_idx];
if (dap->pending_fifo_block_count == 0) {
LOG_ERROR("internal: no pending write when reading?!");
return;
}if (dap->pending_fifo_block_count == 0) { ... }
if (queued_retval != ERROR_OK) {
retval = dap->backend->read(dap, 10, NULL);
if (retval == ERROR_TIMEOUT_REACHED || retval == 0) {
/* ... */
cmsis_dap_swd_discard_all_pending(dap);
return;
}if (retval == ERROR_TIMEOUT_REACHED || retval == 0) { ... }
goto skip;
}if (queued_retval != ERROR_OK) { ... }
struct timeval tv = {
.tv_sec = 0,
.tv_usec = 0
...};
retval = dap->backend->read(dap, LIBUSB_TIMEOUT_MS, blocking ? NULL : &tv);
bool timeout = (retval == ERROR_TIMEOUT_REACHED || retval == 0);
if (timeout && blocking == CMSIS_DAP_NON_BLOCKING)
return;
if (retval <= 0) {
LOG_DEBUG("error reading adapter response");
queued_retval = ERROR_FAIL;
if (timeout) {
/* ... */
cmsis_dap_swd_discard_all_pending(dap);
return;
}if (timeout) { ... }
goto skip;
}if (retval <= 0) { ... }
uint8_t *resp = dap->response;
if (resp[0] != block->command) {
LOG_ERROR("CMSIS-DAP command mismatch. Expected 0x%x received 0x%" PRIx8,
block->command, resp[0]);
cmsis_dap_swd_cancel_transfers(dap);
queued_retval = ERROR_FAIL;
return;
}if (resp[0] != block->command) { ... }
unsigned int transfer_count;
unsigned int idx;
if (block->command == CMD_DAP_TFER_BLOCK) {
transfer_count = le_to_h_u16(&resp[1]);
idx = 3;
}if (block->command == CMD_DAP_TFER_BLOCK) { ... } else {
transfer_count = resp[1];
idx = 2;
}else { ... }
if (resp[idx] & 0x08) {
LOG_DEBUG("CMSIS-DAP Protocol Error @ %d (wrong parity)", transfer_count);
queued_retval = ERROR_FAIL;
goto skip;
}if (resp[idx] & 0x08) { ... }
uint8_t ack = resp[idx++] & 0x07;
if (ack != SWD_ACK_OK) {
LOG_DEBUG("SWD ack not OK @ %d %s", transfer_count,
ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK");
queued_retval = swd_ack_to_error_code(ack);
goto skip;
}if (ack != SWD_ACK_OK) { ... }
if (block->transfer_count != transfer_count) {
LOG_ERROR("CMSIS-DAP transfer count mismatch: expected %d, got %d",
block->transfer_count, transfer_count);
cmsis_dap_swd_cancel_transfers(dap);
queued_retval = ERROR_FAIL;
return;
}if (block->transfer_count != transfer_count) { ... }
LOG_DEBUG_IO("Received results of %d queued transactions FIFO index %u, %s mode",
transfer_count, dap->pending_fifo_get_idx,
blocking ? "blocking" : "nonblocking");
for (unsigned int i = 0; i < transfer_count; i++) {
struct pending_transfer_result *transfer = &(block->transfers[i]);
if (transfer->cmd & SWD_CMD_RNW) {
static uint32_t last_read;
uint32_t data = le_to_h_u32(&resp[idx]);
uint32_t tmp = data;
idx += 4;
LOG_DEBUG_IO("Read result: %" PRIx32, data);
if ((transfer->cmd & SWD_CMD_APNDP) ||
((transfer->cmd & SWD_CMD_A32) >> 1 == DP_RDBUFF)) {
tmp = last_read;
last_read = data;
}if ((transfer->cmd & SWD_CMD_APNDP) || ((transfer->cmd & SWD_CMD_A32) >> 1 == DP_RDBUFF)) { ... }
if (transfer->buffer)
*(uint32_t *)(transfer->buffer) = tmp;
}if (transfer->cmd & SWD_CMD_RNW) { ... }
}for (unsigned int i = 0; i < transfer_count; i++) { ... }
skip:
block->transfer_count = 0;
if (!dap->quirk_mode && dap->packet_count > 1)
dap->pending_fifo_get_idx = (dap->pending_fifo_get_idx + 1) % dap->packet_count;
dap->pending_fifo_block_count--;
}{ ... }
static int cmsis_dap_swd_run_queue(void)
{
if (cmsis_dap_handle->write_count + cmsis_dap_handle->read_count) {
if (cmsis_dap_handle->pending_fifo_block_count)
cmsis_dap_swd_read_process(cmsis_dap_handle, CMSIS_DAP_NON_BLOCKING);
cmsis_dap_swd_write_from_queue(cmsis_dap_handle);
}if (cmsis_dap_handle->write_count + cmsis_dap_handle->read_count) { ... }
while (cmsis_dap_handle->pending_fifo_block_count)
cmsis_dap_swd_read_process(cmsis_dap_handle, CMSIS_DAP_BLOCKING);
cmsis_dap_handle->pending_fifo_put_idx = 0;
cmsis_dap_handle->pending_fifo_get_idx = 0;
int retval = queued_retval;
queued_retval = ERROR_OK;
return retval;
}{ ... }
static unsigned int cmsis_dap_tfer_cmd_size(unsigned int write_count,
unsigned int read_count, bool block_tfer)
{
unsigned int size;
if (block_tfer) {
size = 5;
size += write_count * 4;
}if (block_tfer) { ... } else {
size = 3;
size += write_count * (1 + 4);
size += read_count;
}else { ... }
return size;
}{ ... }
static unsigned int cmsis_dap_tfer_resp_size(unsigned int write_count,
unsigned int read_count, bool block_tfer)
{
unsigned int size;
if (block_tfer)
size = 4;
else
size = 3;
size += read_count * 4;
return size;
}{ ... }
static void cmsis_dap_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data)
{
if (swd_cmd(false, false, DP_TARGETSEL) == cmd) {
queued_retval = cmsis_dap_swd_run_queue();
cmsis_dap_metacmd_targetsel(data);
return;
}if (swd_cmd(false, false, DP_TARGETSEL) == cmd) { ... }
/* ... */
unsigned int write_count = cmsis_dap_handle->write_count;
unsigned int read_count = cmsis_dap_handle->read_count;
bool block_cmd;
if (write_count + read_count < CMD_DAP_TFER_BLOCK_MIN_OPS)
block_cmd = false;
else
block_cmd = !cmsis_dap_handle->swd_cmds_differ
&& cmd == cmsis_dap_handle->common_swd_cmd;
if (cmd & SWD_CMD_RNW)
read_count++;
else
write_count++;
unsigned int cmd_size = cmsis_dap_tfer_cmd_size(write_count, read_count,
block_cmd);
unsigned int resp_size = cmsis_dap_tfer_resp_size(write_count, read_count,
block_cmd);
unsigned int max_transfer_count = block_cmd ? 65535 : 255;
if (cmd_size > tfer_max_command_size
|| resp_size > tfer_max_response_size
|| write_count + read_count > max_transfer_count) {
if (cmsis_dap_handle->pending_fifo_block_count)
cmsis_dap_swd_read_process(cmsis_dap_handle, CMSIS_DAP_NON_BLOCKING);
cmsis_dap_swd_write_from_queue(cmsis_dap_handle);
unsigned int packet_count = cmsis_dap_handle->quirk_mode ? 1 : cmsis_dap_handle->packet_count;
if (cmsis_dap_handle->pending_fifo_block_count >= packet_count)
cmsis_dap_swd_read_process(cmsis_dap_handle, CMSIS_DAP_BLOCKING);
}if (cmd_size > tfer_max_command_size || resp_size > tfer_max_response_size || write_count + read_count > max_transfer_count) { ... }
assert(cmsis_dap_handle->pending_fifo[cmsis_dap_handle->pending_fifo_put_idx].transfer_count < pending_queue_len);
if (queued_retval != ERROR_OK)
return;
struct pending_request_block *block = &cmsis_dap_handle->pending_fifo[cmsis_dap_handle->pending_fifo_put_idx];
struct pending_transfer_result *transfer = &(block->transfers[block->transfer_count]);
transfer->data = data;
transfer->cmd = cmd;
if (block->transfer_count == 0) {
cmsis_dap_handle->swd_cmds_differ = false;
cmsis_dap_handle->common_swd_cmd = cmd;
}if (block->transfer_count == 0) { ... } else if (cmd != cmsis_dap_handle->common_swd_cmd) {
cmsis_dap_handle->swd_cmds_differ = true;
}else if (cmd != cmsis_dap_handle->common_swd_cmd) { ... }
if (cmd & SWD_CMD_RNW) {
transfer->buffer = dst;
cmsis_dap_handle->read_count++;
}if (cmd & SWD_CMD_RNW) { ... } else {
cmsis_dap_handle->write_count++;
}else { ... }
block->transfer_count++;
}{ ... }
static void cmsis_dap_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
{
assert(!(cmd & SWD_CMD_RNW));
cmsis_dap_swd_queue_cmd(cmd, NULL, value);
}{ ... }
static void cmsis_dap_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
{
assert(cmd & SWD_CMD_RNW);
cmsis_dap_swd_queue_cmd(cmd, value, 0);
}{ ... }
static int cmsis_dap_get_serial_info(void)
{
uint8_t *data;
int retval = cmsis_dap_cmd_dap_info(INFO_ID_SERNUM, &data);
if (retval != ERROR_OK)
return retval;
if (data[0])
LOG_INFO("CMSIS-DAP: Serial# = %s", &data[1]);
return ERROR_OK;
}{ ... }
static int cmsis_dap_get_version_info(void)
{
uint8_t *data;
int retval = cmsis_dap_cmd_dap_info(INFO_ID_FW_VER, &data);
if (retval != ERROR_OK)
return retval;
if (data[0])
LOG_INFO("CMSIS-DAP: FW Version = %s", &data[1]);
return ERROR_OK;
}{ ... }
static int cmsis_dap_get_caps_info(void)
{
uint8_t *data;
int retval = cmsis_dap_cmd_dap_info(INFO_ID_CAPS, &data);
if (retval != ERROR_OK)
return retval;
if (data[0] == 1 || data[0] == 2) {
uint16_t caps = data[1];
if (data[0] == 2)
caps |= (uint16_t)data[2] << 8;
cmsis_dap_handle->caps = caps;
for (unsigned int i = 0; i < INFO_CAPS__NUM_CAPS; ++i) {
if (caps & BIT(i))
LOG_INFO("CMSIS-DAP: %s", info_caps_str[i]);
}for (unsigned int i = 0; i < INFO_CAPS__NUM_CAPS; ++i) { ... }
}if (data[0] == 1 || data[0] == 2) { ... }
return ERROR_OK;
}{ ... }
static int cmsis_dap_get_swo_buf_sz(uint32_t *swo_buf_sz)
{
uint8_t *data;
int retval = cmsis_dap_cmd_dap_info(INFO_ID_SWO_BUF_SZ, &data);
if (retval != ERROR_OK)
return retval;
if (data[0] != 4)
return ERROR_FAIL;
*swo_buf_sz = le_to_h_u32(&data[1]);
LOG_INFO("CMSIS-DAP: SWO Trace Buffer Size = %u bytes", *swo_buf_sz);
return ERROR_OK;
}{ ... }
static int cmsis_dap_get_status(void)
{
uint8_t d;
int retval = cmsis_dap_cmd_dap_swj_pins(0, 0, 0, &d);
if (retval == ERROR_OK) {
LOG_INFO("SWCLK/TCK = %d SWDIO/TMS = %d TDI = %d TDO = %d nTRST = %d nRESET = %d",
(d & SWJ_PIN_TCK) ? 1 : 0,
(d & SWJ_PIN_TMS) ? 1 : 0,
(d & SWJ_PIN_TDI) ? 1 : 0,
(d & SWJ_PIN_TDO) ? 1 : 0,
(d & SWJ_PIN_TRST) ? 1 : 0,
(d & SWJ_PIN_SRST) ? 1 : 0);
}if (retval == ERROR_OK) { ... }
return retval;
}{ ... }
static int cmsis_dap_swd_switch_seq(enum swd_special_seq seq)
{
const uint8_t *s;
unsigned int s_len;
int retval;
if (swd_mode)
queued_retval = cmsis_dap_swd_run_queue();
if (cmsis_dap_handle->quirk_mode && seq != LINE_RESET &&
(output_pins & (SWJ_PIN_SRST | SWJ_PIN_TRST))
== (SWJ_PIN_SRST | SWJ_PIN_TRST)) {
/* ... */
cmsis_dap_cmd_dap_disconnect();
/* ... */
retval = cmsis_dap_cmd_dap_connect(CONNECT_SWD);
if (retval != ERROR_OK)
return retval;
}if (cmsis_dap_handle->quirk_mode && seq != LINE_RESET && (output_pins & (SWJ_PIN_SRST | SWJ_PIN_TRST)) == (SWJ_PIN_SRST | SWJ_PIN_TRST)) { ... }
switch (seq) {
case LINE_RESET:
LOG_DEBUG_IO("SWD line reset");
s = swd_seq_line_reset;
s_len = swd_seq_line_reset_len;
break;case LINE_RESET:
case JTAG_TO_SWD:
LOG_DEBUG("JTAG-to-SWD");
s = swd_seq_jtag_to_swd;
s_len = swd_seq_jtag_to_swd_len;
break;case JTAG_TO_SWD:
case JTAG_TO_DORMANT:
LOG_DEBUG("JTAG-to-DORMANT");
s = swd_seq_jtag_to_dormant;
s_len = swd_seq_jtag_to_dormant_len;
break;case JTAG_TO_DORMANT:
case SWD_TO_JTAG:
LOG_DEBUG("SWD-to-JTAG");
s = swd_seq_swd_to_jtag;
s_len = swd_seq_swd_to_jtag_len;
break;case SWD_TO_JTAG:
case SWD_TO_DORMANT:
LOG_DEBUG("SWD-to-DORMANT");
s = swd_seq_swd_to_dormant;
s_len = swd_seq_swd_to_dormant_len;
break;case SWD_TO_DORMANT:
case DORMANT_TO_SWD:
LOG_DEBUG("DORMANT-to-SWD");
s = swd_seq_dormant_to_swd;
s_len = swd_seq_dormant_to_swd_len;
break;case DORMANT_TO_SWD:
case DORMANT_TO_JTAG:
LOG_DEBUG("DORMANT-to-JTAG");
s = swd_seq_dormant_to_jtag;
s_len = swd_seq_dormant_to_jtag_len;
break;case DORMANT_TO_JTAG:
default:
LOG_ERROR("Sequence %d not supported", seq);
return ERROR_FAIL;default
}switch (seq) { ... }
retval = cmsis_dap_cmd_dap_swj_sequence(s_len, s);
if (retval != ERROR_OK)
return retval;
/* ... */
return cmsis_dap_cmd_dap_swj_clock(adapter_get_speed_khz());
}{ ... }
static int cmsis_dap_swd_open(void)
{
if (!(cmsis_dap_handle->caps & INFO_CAPS_SWD)) {
LOG_ERROR("CMSIS-DAP: SWD not supported");
return ERROR_JTAG_DEVICE_ERROR;
}if (!(cmsis_dap_handle->caps & INFO_CAPS_SWD)) { ... }
int retval = cmsis_dap_cmd_dap_connect(CONNECT_SWD);
if (retval != ERROR_OK)
return retval;
LOG_INFO("CMSIS-DAP: Interface Initialised (SWD)");
return ERROR_OK;
}{ ... }
static int cmsis_dap_init(void)
{
uint8_t *data;
int retval = cmsis_dap_open();
if (retval != ERROR_OK)
return retval;
cmsis_dap_flush_read(cmsis_dap_handle);
retval = cmsis_dap_get_caps_info();
if (retval != ERROR_OK)
return retval;
retval = cmsis_dap_get_version_info();
if (retval != ERROR_OK)
return retval;
retval = cmsis_dap_get_serial_info();
if (retval != ERROR_OK)
return retval;
if (swd_mode) {
retval = cmsis_dap_swd_open();
if (retval != ERROR_OK)
return retval;
}if (swd_mode) { ... } else {
if (!(cmsis_dap_handle->caps & INFO_CAPS_JTAG)) {
LOG_ERROR("CMSIS-DAP: JTAG not supported");
return ERROR_JTAG_DEVICE_ERROR;
}if (!(cmsis_dap_handle->caps & INFO_CAPS_JTAG)) { ... }
retval = cmsis_dap_cmd_dap_connect(CONNECT_JTAG);
if (retval != ERROR_OK)
return retval;
LOG_INFO("CMSIS-DAP: Interface Initialised (JTAG)");
}else { ... }
/* ... */
cmsis_dap_handle->packet_count = 1;
retval = cmsis_dap_cmd_dap_info(INFO_ID_PKT_SZ, &data);
if (retval != ERROR_OK)
goto init_err;
if (data[0] == 2) {
uint16_t pkt_sz = data[1] + (data[2] << 8);
if (pkt_sz != cmsis_dap_handle->packet_size) {
cmsis_dap_handle->backend->packet_buffer_free(cmsis_dap_handle);
retval = cmsis_dap_handle->backend->packet_buffer_alloc(cmsis_dap_handle, pkt_sz);
if (retval != ERROR_OK)
goto init_err;
LOG_DEBUG("CMSIS-DAP: Packet Size = %" PRIu16, pkt_sz);
}if (pkt_sz != cmsis_dap_handle->packet_size) { ... }
}if (data[0] == 2) { ... }
/* ... */
tfer_max_command_size = cmsis_dap_handle->packet_usable_size;
tfer_max_response_size = cmsis_dap_handle->packet_usable_size;
unsigned int max_reads = tfer_max_response_size / 4;
pending_queue_len = max_reads + (tfer_max_command_size - max_reads) / 5;
cmsis_dap_handle->write_count = 0;
cmsis_dap_handle->read_count = 0;
retval = cmsis_dap_cmd_dap_info(INFO_ID_PKT_CNT, &data);
if (retval != ERROR_OK)
goto init_err;
if (data[0] == 1) {
unsigned int pkt_cnt = data[1];
if (pkt_cnt > 1)
cmsis_dap_handle->packet_count = MIN(MAX_PENDING_REQUESTS, pkt_cnt);
LOG_DEBUG("CMSIS-DAP: Packet Count = %u", pkt_cnt);
}if (data[0] == 1) { ... }
LOG_DEBUG("Allocating FIFO for %u pending packets", cmsis_dap_handle->packet_count);
for (unsigned int i = 0; i < cmsis_dap_handle->packet_count; i++) {
cmsis_dap_handle->pending_fifo[i].transfers = malloc(pending_queue_len
* sizeof(struct pending_transfer_result));
if (!cmsis_dap_handle->pending_fifo[i].transfers) {
LOG_ERROR("Unable to allocate memory for CMSIS-DAP queue");
retval = ERROR_FAIL;
goto init_err;
}if (!cmsis_dap_handle->pending_fifo[i].transfers) { ... }
}for (unsigned int i = 0; i < cmsis_dap_handle->packet_count; i++) { ... }
/* ... */
(void)cmsis_dap_get_status();
/* ... */
retval = cmsis_dap_cmd_dap_swj_clock(adapter_get_speed_khz());
if (retval != ERROR_OK)
goto init_err;
/* ... */
retval = cmsis_dap_cmd_dap_tfer_configure(0, 64, 0);
if (retval != ERROR_OK)
goto init_err;
if (swd_mode) {
/* ... */
retval = cmsis_dap_cmd_dap_swd_configure(0);
if (retval != ERROR_OK)
goto init_err;
}if (swd_mode) { ... }
/* ... */
(void)cmsis_dap_cmd_dap_led(LED_ID_CONNECT, LED_ON);
(void)cmsis_dap_cmd_dap_led(LED_ID_RUN, LED_ON);
enum reset_types jtag_reset_config = jtag_get_reset_config();
if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
if (jtag_reset_config & RESET_SRST_NO_GATING) {
retval = cmsis_dap_cmd_dap_swj_pins(0, SWJ_PIN_SRST, 0, NULL);
if (retval != ERROR_OK)
goto init_err;
LOG_INFO("Connecting under reset");
}if (jtag_reset_config & RESET_SRST_NO_GATING) { ... }
}if (jtag_reset_config & RESET_CNCT_UNDER_SRST) { ... }
LOG_INFO("CMSIS-DAP: Interface ready");
return ERROR_OK;
init_err:
cmsis_dap_quit();
return retval;
}{ ... }
static int cmsis_dap_swd_init(void)
{
swd_mode = true;
return ERROR_OK;
}{ ... }
static int cmsis_dap_quit(void)
{
cmsis_dap_cmd_dap_disconnect();
cmsis_dap_cmd_dap_led(LED_ID_RUN, LED_OFF);
cmsis_dap_cmd_dap_led(LED_ID_CONNECT, LED_OFF);
cmsis_dap_close(cmsis_dap_handle);
return ERROR_OK;
}{ ... }
static int cmsis_dap_reset(int trst, int srst)
{
/* ... */
output_pins = 0;
if (!srst)
output_pins |= SWJ_PIN_SRST;
if (!trst)
output_pins |= SWJ_PIN_TRST;
int retval = cmsis_dap_cmd_dap_swj_pins(output_pins,
SWJ_PIN_TRST | SWJ_PIN_SRST, 0, NULL);
if (retval != ERROR_OK)
LOG_ERROR("CMSIS-DAP: Interface reset failed");
return retval;
}{ ... }
static void cmsis_dap_execute_sleep(struct jtag_command *cmd)
{
#if 0
int retval = cmsis_dap_cmd_dap_delay(cmd->cmd.sleep->us);
if (retval != ERROR_OK)/* ... */
#endif
jtag_sleep(cmd->cmd.sleep->us);
}{ ... }
static int cmsis_dap_execute_tlr_reset(struct jtag_command *cmd)
{
LOG_INFO("cmsis-dap JTAG TLR_RESET");
uint8_t seq = 0xff;
int retval = cmsis_dap_cmd_dap_swj_sequence(8, &seq);
if (retval == ERROR_OK)
tap_set_state(TAP_RESET);
return retval;
}{ ... }
static void cmsis_dap_end_state(tap_state_t state)
{
if (tap_is_state_stable(state))
tap_set_end_state(state);
else {
LOG_ERROR("BUG: %i is not a valid end state", state);
exit(-1);
}else { ... }
}{ ... }
#ifdef SPRINT_BINARY
static void sprint_binary(char *s, const uint8_t *buf, unsigned int offset, unsigned int len)
{
if (!len)
return;
/* ... */
for (unsigned int i = offset; i < offset + len; ++i) {
uint8_t c = buf[i / 8], mask = 1 << (i % 8);
if ((i != offset) && !(i % 8))
putchar(' ');
*s++ = (c & mask) ? '1' : '0';
}for (unsigned int i = offset; i < offset + len; ++i) { ... }
*s = 0;
}sprint_binary (char *s, const uint8_t *buf, unsigned int offset, unsigned int len) { ... }
/* ... */#endif
#ifdef CMSIS_DAP_JTAG_DEBUG
static void debug_parse_cmsis_buf(const uint8_t *cmd, int cmdlen)
{
printf("cmsis-dap buffer (%d b): ", cmdlen);
for (int i = 0; i < cmdlen; ++i)
printf(" %02x", cmd[i]);
printf("\n");
switch (cmd[0]) {
case CMD_DAP_JTAG_SEQ: {
printf("cmsis-dap jtag sequence command %02x (n=%d)\n", cmd[0], cmd[1]);
/* ... */
int pos = 2;
for (int seq = 0; seq < cmd[1]; ++seq) {
uint8_t info = cmd[pos++];
int len = info & DAP_JTAG_SEQ_TCK;
if (len == 0)
len = 64;
printf(" sequence %d starting %d: info %02x (len=%d tms=%d read_tdo=%d): ",
seq, pos, info, len, info & DAP_JTAG_SEQ_TMS, info & DAP_JTAG_SEQ_TDO);
for (int i = 0; i < DIV_ROUND_UP(len, 8); ++i)
printf(" %02x", cmd[pos+i]);
pos += DIV_ROUND_UP(len, 8);
printf("\n");
}for (int seq = 0; seq < cmd[1]; ++seq) { ... }
if (pos != cmdlen) {
printf("BUFFER LENGTH MISMATCH looks like %d but %d specified", pos, cmdlen);
exit(-1);
}if (pos != cmdlen) { ... }
break;
...}case CMD_DAP_JTAG_SEQ:
default:
LOG_DEBUG("unknown cmsis-dap command %02x", cmd[1]);
break;default
}switch (cmd[0]) { ... }
}debug_parse_cmsis_buf (const uint8_t *cmd, int cmdlen) { ... }
/* ... */#endif
static void cmsis_dap_flush(void)
{
if (!queued_seq_count)
return;
LOG_DEBUG_IO("Flushing %d queued sequences (%d bytes) with %d pending scan results to capture",
queued_seq_count, queued_seq_buf_end, pending_scan_result_count);
uint8_t *command = cmsis_dap_handle->command;
command[0] = CMD_DAP_JTAG_SEQ;
command[1] = queued_seq_count;
memcpy(&command[2], queued_seq_buf, queued_seq_buf_end);
#ifdef CMSIS_DAP_JTAG_DEBUG
debug_parse_cmsis_buf(command, queued_seq_buf_end + 2);
#endif
int retval = cmsis_dap_xfer(cmsis_dap_handle, queued_seq_buf_end + 2);
uint8_t *resp = cmsis_dap_handle->response;
if (retval != ERROR_OK || resp[1] != DAP_OK) {
LOG_ERROR("CMSIS-DAP command CMD_DAP_JTAG_SEQ failed.");
exit(-1);
}if (retval != ERROR_OK || resp[1] != DAP_OK) { ... }
#ifdef CMSIS_DAP_JTAG_DEBUG
LOG_DEBUG_IO("USB response buf:");
for (int c = 0; c < queued_seq_buf_end + 3; ++c)
printf("%02X ", resp[c]);
printf("\n");/* ... */
#endif
for (int i = 0; i < pending_scan_result_count; ++i) {
struct pending_scan_result *scan = &pending_scan_results[i];
LOG_DEBUG_IO("Copying pending_scan_result %d/%d: %d bits from byte %d -> buffer + %d bits",
i, pending_scan_result_count, scan->length, scan->first + 2, scan->buffer_offset);
#ifdef CMSIS_DAP_JTAG_DEBUG
for (uint32_t b = 0; b < DIV_ROUND_UP(scan->length, 8); ++b)
printf("%02X ", resp[2+scan->first+b]);
printf("\n");/* ... */
#endif
bit_copy(scan->buffer, scan->buffer_offset, &resp[2 + scan->first], 0, scan->length);
}for (int i = 0; i < pending_scan_result_count; ++i) { ... }
queued_seq_count = 0;
queued_seq_buf_end = 0;
queued_seq_tdo_ptr = 0;
pending_scan_result_count = 0;
}{ ... }
/* ... */
static void cmsis_dap_add_jtag_sequence(unsigned int s_len, const uint8_t *sequence,
unsigned int s_offset, bool tms,
uint8_t *tdo_buffer, unsigned int tdo_buffer_offset)
{
LOG_DEBUG_IO("[at %d] %u bits, tms %s, seq offset %u, tdo buf %p, tdo offset %u",
queued_seq_buf_end,
s_len, tms ? "HIGH" : "LOW", s_offset, tdo_buffer, tdo_buffer_offset);
if (s_len == 0)
return;
if (s_len > 64) {
LOG_DEBUG_IO("START JTAG SEQ SPLIT");
for (unsigned <