1
2
3
7
8
9
10
11
12
21
23
24
25
27
28
29
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
64
65
66
67
68
69
70
71
72
73
74
75
76
80
81
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
391
392
393
394
395
396
400
401
402
407
408
409
410
411
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
473
474
475
476
477
478
479
480
481
482
483
484
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
524
525
526
536
537
542
543
544
545
546
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
585
586
587
588
589
594
595
596
597
598
599
600
601
602
603
604
605
606
607
609
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
651
652
653
654
655
656
657
658
659
660
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
739
740
741
742
743
744
750
751
752
753
755
757
759
761
763
765
767
769
771
773
775
777
779
781
783
785
787
789
790
791
792
797
798
799
800
801
802
803
804
805
806
807
808
809
810
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
869
875
876
877
878
879
880
881
882
883
910
911
915
916
917
918
919
920
921
922
923
924
928
929
930
935
936
937
938
939
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
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
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
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
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1147
1148
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1293
1296
1299
1302
1305
1308
1309
1310
1313
1314
1315
1316
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1356
1362
1368
1374
1380
1382
1383
1384
1385
1386
1387
1388
1389
1390
1396
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1535
1536
1537
1538
1539
1540
1541
1542
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
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1653
1654
1655
1656
1657
1658
1659
1660
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
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
1717
1718
1719
1720
1721
1722
1723
1724
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1769
1770
1776
1777
1778
1779
1780
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1864
1865
1866
1869
1870
1871
1872
1873
1874
1878
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1919
1922
1925
1928
1931
1934
1937
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1980
1981
1983
1984
1985
1991
1992
1996
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
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
2064
2065
2066
2067
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2122
2123
2124
2125
2133
2134
2135
2136
2137
2138
2139
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
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
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <math.h>
#include "helper/system.h"
#include <jtag/interface.h>
#include <jtag/commands.h>
#include <target/image.h>
#include <libusb.h>
#include "libusb_helper.h"
#include "OpenULINK/include/msgtypes.h"
8 includes
/* ... */
#define ULINK_VID 0xC251
/* ... */
#define ULINK_PID 0x2710
/* ... */
#define CPUCS_REG 0x7F92
#define REQUEST_FIRMWARE_LOAD 0xA0
#define CPU_RESET 0x01
#define CPU_START 0x00
#define FIRMWARE_ADDR 0x0000
#define USB_INTERFACE 0
#define ULINK_RENUMERATION_DELAY 1500000
#define ULINK_FIRMWARE_FILE PKGDATADIR "/OpenULINK/ulink_firmware.hex"
#define SECTION_BUFFERSIZE 8192
#define SPLIT_SCAN_THRESHOLD 10
12 defines
enum ulink_type {
/* ... */
ULINK_1,
ULINK_2,
ULINK_PRO,
ULINK_ME
...};
enum ulink_payload_direction {
PAYLOAD_DIRECTION_OUT,
PAYLOAD_DIRECTION_IN
...};
enum ulink_delay_type {
DELAY_CLOCK_TCK,
DELAY_CLOCK_TMS,
DELAY_SCAN_IN,
DELAY_SCAN_OUT,
DELAY_SCAN_IO
...};
/* ... */
struct ulink_cmd {
uint8_t id;
uint8_t *payload_out;
uint8_t payload_out_size;
uint8_t *payload_in_start;
uint8_t *payload_in;
uint8_t payload_in_size;
bool needs_postprocessing;
bool free_payload_in_start;
struct jtag_command *cmd_origin;
struct ulink_cmd *next;
...};
struct ulink {
struct libusb_context *libusb_ctx;
struct libusb_device_handle *usb_device_handle;
enum ulink_type type;
unsigned int ep_in;
unsigned int ep_out;
int delay_scan_in;
int delay_scan_out;
int delay_scan_io;
int delay_clock_tck;
int delay_clock_tms;
int commands_in_queue;
struct ulink_cmd *queue_start;
struct ulink_cmd *queue_end;
...};
static int ulink_usb_open(struct ulink **device);
static int ulink_usb_close(struct ulink **device);
static int ulink_cpu_reset(struct ulink *device, unsigned char reset_bit);
static int ulink_load_firmware_and_renumerate(struct ulink **device, const char *filename,
uint32_t delay);
static int ulink_load_firmware(struct ulink *device, const char *filename);
static int ulink_write_firmware_section(struct ulink *device,
struct image *firmware_image, int section_index);
static void ulink_print_signal_states(uint8_t input_signals, uint8_t output_signals);
static int ulink_allocate_payload(struct ulink_cmd *ulink_cmd, int size,
enum ulink_payload_direction direction);
static int ulink_get_queue_size(struct ulink *device,
enum ulink_payload_direction direction);
static void ulink_clear_queue(struct ulink *device);
static int ulink_append_queue(struct ulink *device, struct ulink_cmd *ulink_cmd);
static int ulink_execute_queued_commands(struct ulink *device, int timeout);
static void ulink_print_queue(struct ulink *device);
static int ulink_append_scan_cmd(struct ulink *device,
enum scan_type scan_type,
int scan_size_bits,
uint8_t *tdi,
uint8_t *tdo_start,
uint8_t *tdo,
uint8_t tms_count_start,
uint8_t tms_sequence_start,
uint8_t tms_count_end,
uint8_t tms_sequence_end,
struct jtag_command *origin,
bool postprocess);
static int ulink_append_clock_tms_cmd(struct ulink *device, uint8_t count,
uint8_t sequence);
static int ulink_append_clock_tck_cmd(struct ulink *device, uint16_t count);
static int ulink_append_get_signals_cmd(struct ulink *device);
static int ulink_append_set_signals_cmd(struct ulink *device, uint8_t low,
uint8_t high);
static int ulink_append_sleep_cmd(struct ulink *device, uint32_t us);
static int ulink_append_configure_tck_cmd(struct ulink *device,
int delay_scan_in,
int delay_scan_out,
int delay_scan_io,
int delay_tck,
int delay_tms);
static int __attribute__((unused)) ulink_append_led_cmd(struct ulink *device, uint8_t led_state);
static int ulink_append_test_cmd(struct ulink *device);
static int ulink_calculate_delay(enum ulink_delay_type type, long f, int *delay);
static void ulink_set_end_state(tap_state_t endstate);
static int ulink_queue_statemove(struct ulink *device);
static int ulink_queue_scan(struct ulink *device, struct jtag_command *cmd);
static int ulink_queue_tlr_reset(struct ulink *device, struct jtag_command *cmd);
static int ulink_queue_runtest(struct ulink *device, struct jtag_command *cmd);
static int ulink_queue_reset(struct ulink *device, struct jtag_command *cmd);
static int ulink_queue_pathmove(struct ulink *device, struct jtag_command *cmd);
static int ulink_queue_sleep(struct ulink *device, struct jtag_command *cmd);
static int ulink_queue_stableclocks(struct ulink *device, struct jtag_command *cmd);
static int ulink_post_process_scan(struct ulink_cmd *ulink_cmd);
static int ulink_post_process_queue(struct ulink *device);
static int ulink_execute_queue(struct jtag_command *cmd_queue);
static int ulink_khz(int khz, int *jtag_speed);
static int ulink_speed(int speed);
static int ulink_speed_div(int speed, int *khz);
static int ulink_init(void);
static int ulink_quit(void);
Function Prototypes
static struct ulink *ulink_handle;
Global Variables
/* ... */
static int ulink_usb_open(struct ulink **device)
{
ssize_t num_devices, i;
bool found;
struct libusb_device **usb_devices;
struct libusb_device_descriptor usb_desc;
struct libusb_device_handle *usb_device_handle;
num_devices = libusb_get_device_list((*device)->libusb_ctx, &usb_devices);
if (num_devices <= 0)
return ERROR_FAIL;
found = false;
for (i = 0; i < num_devices; i++) {
if (libusb_get_device_descriptor(usb_devices[i], &usb_desc) != 0)
continue;
else if (usb_desc.idVendor == ULINK_VID && usb_desc.idProduct == ULINK_PID) {
found = true;
break;
}else if (usb_desc.idVendor == ULINK_VID && usb_desc.idProduct == ULINK_PID) { ... }
}for (i = 0; i < num_devices; i++) { ... }
if (!found)
return ERROR_FAIL;
if (libusb_open(usb_devices[i], &usb_device_handle) != 0)
return ERROR_FAIL;
libusb_free_device_list(usb_devices, 1);
(*device)->usb_device_handle = usb_device_handle;
(*device)->type = ULINK_1;
return ERROR_OK;
}{ ... }
/* ... */
static int ulink_usb_close(struct ulink **device)
{
if (libusb_release_interface((*device)->usb_device_handle, 0) != 0)
return ERROR_FAIL;
libusb_close((*device)->usb_device_handle);
(*device)->usb_device_handle = NULL;
return ERROR_OK;
}{ ... }
USB helper functions
/* ... */
static int ulink_cpu_reset(struct ulink *device, unsigned char reset_bit)
{
int ret;
ret = libusb_control_transfer(device->usb_device_handle,
(LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE),
REQUEST_FIRMWARE_LOAD, CPUCS_REG, 0, &reset_bit, 1, LIBUSB_TIMEOUT_MS);
/* ... */
if (ret != 1)
return ERROR_FAIL;
return ERROR_OK;
}{ ... }
/* ... */
static int ulink_load_firmware_and_renumerate(struct ulink **device,
const char *filename, uint32_t delay)
{
int ret;
/* ... */
ret = ulink_load_firmware(*device, filename);
if (ret != ERROR_OK)
return ret;
ret = ulink_usb_close(device);
if (ret != ERROR_OK)
return ret;
usleep(delay);
ret = ulink_usb_open(device);
if (ret != ERROR_OK)
return ret;
return ERROR_OK;
}{ ... }
/* ... */
static int ulink_load_firmware(struct ulink *device, const char *filename)
{
struct image ulink_firmware_image;
int ret;
ret = ulink_cpu_reset(device, CPU_RESET);
if (ret != ERROR_OK) {
LOG_ERROR("Could not halt ULINK CPU");
return ret;
}if (ret != ERROR_OK) { ... }
ulink_firmware_image.base_address = 0;
ulink_firmware_image.base_address_set = false;
ret = image_open(&ulink_firmware_image, filename, "ihex");
if (ret != ERROR_OK) {
LOG_ERROR("Could not load firmware image");
return ret;
}if (ret != ERROR_OK) { ... }
for (unsigned int i = 0; i < ulink_firmware_image.num_sections; i++) {
ret = ulink_write_firmware_section(device, &ulink_firmware_image, i);
if (ret != ERROR_OK)
return ret;
}for (unsigned int i = 0; i < ulink_firmware_image.num_sections; i++) { ... }
image_close(&ulink_firmware_image);
ret = ulink_cpu_reset(device, CPU_START);
if (ret != ERROR_OK) {
LOG_ERROR("Could not restart ULINK CPU");
return ret;
}if (ret != ERROR_OK) { ... }
return ERROR_OK;
}{ ... }
/* ... */
static int ulink_write_firmware_section(struct ulink *device,
struct image *firmware_image, int section_index)
{
uint16_t addr, size, bytes_remaining, chunk_size;
uint8_t data[SECTION_BUFFERSIZE];
uint8_t *data_ptr = data;
size_t size_read;
int ret;
size = (uint16_t)firmware_image->sections[section_index].size;
addr = (uint16_t)firmware_image->sections[section_index].base_address;
LOG_DEBUG("section %02i at addr 0x%04x (size 0x%04x)", section_index, addr,
size);
ret = image_read_section(firmware_image, section_index, 0, size, data,
&size_read);
if ((ret != ERROR_OK) || (size_read != size)) {
/* ... */
return ERROR_FAIL;
}if ((ret != ERROR_OK) || (size_read != size)) { ... }
bytes_remaining = size;
while (bytes_remaining > 0) {
if (bytes_remaining > 64)
chunk_size = 64;
else
chunk_size = bytes_remaining;
ret = libusb_control_transfer(device->usb_device_handle,
(LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE),
REQUEST_FIRMWARE_LOAD, addr, FIRMWARE_ADDR, (unsigned char *)data_ptr,
chunk_size, LIBUSB_TIMEOUT_MS);
if (ret != (int)chunk_size) {
return ERROR_FAIL;
}if (ret != (int)chunk_size) { ... }
bytes_remaining -= chunk_size;
addr += chunk_size;
data_ptr += chunk_size;
}while (bytes_remaining > 0) { ... }
return ERROR_OK;
}{ ... }
ULINK CPU (EZ-USB) specific functions
/* ... */
static void ulink_print_signal_states(uint8_t input_signals, uint8_t output_signals)
{
LOG_INFO("ULINK signal states: TDI: %i, TDO: %i, TMS: %i, TCK: %i, TRST: %i,"
" SRST: %i",
(output_signals & SIGNAL_TDI ? 1 : 0),
(input_signals & SIGNAL_TDO ? 1 : 0),
(output_signals & SIGNAL_TMS ? 1 : 0),
(output_signals & SIGNAL_TCK ? 1 : 0),
(output_signals & SIGNAL_TRST ? 0 : 1),
(output_signals & SIGNAL_RESET ? 0 : 1));
}{ ... }
Generic helper functions
/* ... */
static int ulink_allocate_payload(struct ulink_cmd *ulink_cmd, int size,
enum ulink_payload_direction direction)
{
uint8_t *payload;
payload = calloc(size, sizeof(uint8_t));
if (!payload) {
LOG_ERROR("Could not allocate OpenULINK command payload: out of memory");
return ERROR_FAIL;
}if (!payload) { ... }
switch (direction) {
case PAYLOAD_DIRECTION_OUT:
if (ulink_cmd->payload_out) {
LOG_ERROR("BUG: Duplicate payload allocation for OpenULINK command");
free(payload);
return ERROR_FAIL;
}if (ulink_cmd->payload_out) { ... } else {
ulink_cmd->payload_out = payload;
ulink_cmd->payload_out_size = size;
}else { ... }
break;case PAYLOAD_DIRECTION_OUT:
case PAYLOAD_DIRECTION_IN:
if (ulink_cmd->payload_in_start) {
LOG_ERROR("BUG: Duplicate payload allocation for OpenULINK command");
free(payload);
return ERROR_FAIL;
}if (ulink_cmd->payload_in_start) { ... } else {
ulink_cmd->payload_in_start = payload;
ulink_cmd->payload_in = payload;
ulink_cmd->payload_in_size = size;
/* ... */
ulink_cmd->free_payload_in_start = true;
}else { ... }
break;case PAYLOAD_DIRECTION_IN:
}switch (direction) { ... }
return ERROR_OK;
}{ ... }
OpenULINK command generation helper functions
/* ... */
static int ulink_get_queue_size(struct ulink *device,
enum ulink_payload_direction direction)
{
struct ulink_cmd *current = device->queue_start;
int sum = 0;
while (current) {
switch (direction) {
case PAYLOAD_DIRECTION_OUT:
sum += current->payload_out_size + 1;
break;case PAYLOAD_DIRECTION_OUT:
case PAYLOAD_DIRECTION_IN:
sum += current->payload_in_size;
break;case PAYLOAD_DIRECTION_IN:
}switch (direction) { ... }
current = current->next;
}while (current) { ... }
return sum;
}{ ... }
/* ... */
static void ulink_clear_queue(struct ulink *device)
{
struct ulink_cmd *current = device->queue_start;
struct ulink_cmd *next = NULL;
while (current) {
next = current->next;
free(current->payload_out);
current->payload_out = NULL;
/* ... */
if (current->free_payload_in_start == true) {
free(current->payload_in_start);
current->payload_in_start = NULL;
current->payload_in = NULL;
}if (current->free_payload_in_start == true) { ... }
free(current);
current = next;
}while (current) { ... }
device->commands_in_queue = 0;
device->queue_start = NULL;
device->queue_end = NULL;
}{ ... }
/* ... */
static int ulink_append_queue(struct ulink *device, struct ulink_cmd *ulink_cmd)
{
int newsize_out, newsize_in;
int ret = ERROR_OK;
newsize_out = ulink_get_queue_size(device, PAYLOAD_DIRECTION_OUT) + 1
+ ulink_cmd->payload_out_size;
newsize_in = ulink_get_queue_size(device, PAYLOAD_DIRECTION_IN)
+ ulink_cmd->payload_in_size;
if ((newsize_out > 64) || (newsize_in > 64)) {
/* ... */
ret = ulink_execute_queued_commands(device, LIBUSB_TIMEOUT_MS);
if (ret == ERROR_OK)
ret = ulink_post_process_queue(device);
if (ret == ERROR_OK)
ulink_clear_queue(device);
}if ((newsize_out > 64) || (newsize_in > 64)) { ... }
if (!device->queue_start) {
device->commands_in_queue = 1;
device->queue_start = ulink_cmd;
device->queue_end = ulink_cmd;
}if (!device->queue_start) { ... } else {
device->commands_in_queue++;
device->queue_end->next = ulink_cmd;
device->queue_end = ulink_cmd;
}else { ... }
if (ret != ERROR_OK)
ulink_clear_queue(device);
return ret;
}{ ... }
/* ... */
static int ulink_execute_queued_commands(struct ulink *device, int timeout)
{
struct ulink_cmd *current;
int ret, i, index_out, index_in, count_out, count_in, transferred;
uint8_t buffer[64];
if (LOG_LEVEL_IS(LOG_LVL_DEBUG_IO))
ulink_print_queue(device);
index_out = 0;
count_out = 0;
count_in = 0;
for (current = device->queue_start; current; current = current->next) {
buffer[index_out] = current->id;
index_out++;
count_out++;
for (i = 0; i < current->payload_out_size; i++)
buffer[index_out + i] = current->payload_out[i];
index_out += current->payload_out_size;
count_in += current->payload_in_size;
count_out += current->payload_out_size;
}for (current = device->queue_start; current; current = current->next) { ... }
ret = libusb_bulk_transfer(device->usb_device_handle, device->ep_out,
(unsigned char *)buffer, count_out, &transferred, timeout);
if (ret != 0)
return ERROR_FAIL;
if (transferred != count_out)
return ERROR_FAIL;
if (count_in > 0) {
ret = libusb_bulk_transfer(device->usb_device_handle, device->ep_in,
(unsigned char *)buffer, 64, &transferred, timeout);
if (ret != 0)
return ERROR_FAIL;
if (transferred != count_in)
return ERROR_FAIL;
index_in = 0;
for (current = device->queue_start; current; current = current->next) {
for (i = 0; i < current->payload_in_size; i++) {
current->payload_in[i] = buffer[index_in];
index_in++;
}for (i = 0; i < current->payload_in_size; i++) { ... }
}for (current = device->queue_start; current; current = current->next) { ... }
}if (count_in > 0) { ... }
return ERROR_OK;
}{ ... }
/* ... */
static const char *ulink_cmd_id_string(uint8_t id)
{
switch (id) {
case CMD_SCAN_IN:
return "CMD_SCAN_IN";case CMD_SCAN_IN:
case CMD_SLOW_SCAN_IN:
return "CMD_SLOW_SCAN_IN";case CMD_SLOW_SCAN_IN:
case CMD_SCAN_OUT:
return "CMD_SCAN_OUT";case CMD_SCAN_OUT:
case CMD_SLOW_SCAN_OUT:
return "CMD_SLOW_SCAN_OUT";case CMD_SLOW_SCAN_OUT:
case CMD_SCAN_IO:
return "CMD_SCAN_IO";case CMD_SCAN_IO:
case CMD_SLOW_SCAN_IO:
return "CMD_SLOW_SCAN_IO";case CMD_SLOW_SCAN_IO:
case CMD_CLOCK_TMS:
return "CMD_CLOCK_TMS";case CMD_CLOCK_TMS:
case CMD_SLOW_CLOCK_TMS:
return "CMD_SLOW_CLOCK_TMS";case CMD_SLOW_CLOCK_TMS:
case CMD_CLOCK_TCK:
return "CMD_CLOCK_TCK";case CMD_CLOCK_TCK:
case CMD_SLOW_CLOCK_TCK:
return "CMD_SLOW_CLOCK_TCK";case CMD_SLOW_CLOCK_TCK:
case CMD_SLEEP_US:
return "CMD_SLEEP_US";case CMD_SLEEP_US:
case CMD_SLEEP_MS:
return "CMD_SLEEP_MS";case CMD_SLEEP_MS:
case CMD_GET_SIGNALS:
return "CMD_GET_SIGNALS";case CMD_GET_SIGNALS:
case CMD_SET_SIGNALS:
return "CMD_SET_SIGNALS";case CMD_SET_SIGNALS:
case CMD_CONFIGURE_TCK_FREQ:
return "CMD_CONFIGURE_TCK_FREQ";case CMD_CONFIGURE_TCK_FREQ:
case CMD_SET_LEDS:
return "CMD_SET_LEDS";case CMD_SET_LEDS:
case CMD_TEST:
return "CMD_TEST";case CMD_TEST:
default:
return "CMD_UNKNOWN";default
}switch (id) { ... }
}{ ... }
/* ... */
static void ulink_print_command(struct ulink_cmd *ulink_cmd)
{
int i;
printf(" %-22s | OUT size = %i, bytes = 0x",
ulink_cmd_id_string(ulink_cmd->id), ulink_cmd->payload_out_size);
for (i = 0; i < ulink_cmd->payload_out_size; i++)
printf("%02X ", ulink_cmd->payload_out[i]);
printf("\n | IN size = %i\n",
ulink_cmd->payload_in_size);
}{ ... }
/* ... */
static void ulink_print_queue(struct ulink *device)
{
struct ulink_cmd *current;
printf("OpenULINK command queue:\n");
for (current = device->queue_start; current; current = current->next)
ulink_print_command(current);
}{ ... }
/* ... */
static int ulink_append_scan_cmd(struct ulink *device, enum scan_type scan_type,
int scan_size_bits, uint8_t *tdi, uint8_t *tdo_start, uint8_t *tdo,
uint8_t tms_count_start, uint8_t tms_sequence_start, uint8_t tms_count_end,
uint8_t tms_sequence_end, struct jtag_command *origin, bool postprocess)
{
struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
int ret, i, scan_size_bytes;
uint8_t bits_last_byte;
if (!cmd)
return ERROR_FAIL;
/* ... */
if (scan_size_bits > (58 * 8)) {
LOG_ERROR("BUG: Tried to create CMD_SCAN_IO OpenULINK command with too"
" large payload");
free(cmd);
return ERROR_FAIL;
}if (scan_size_bits > (58 * 8)) { ... }
scan_size_bytes = DIV_ROUND_UP(scan_size_bits, 8);
bits_last_byte = scan_size_bits % 8;
if (bits_last_byte == 0)
bits_last_byte = 8;
switch (scan_type) {
case SCAN_IN:
if (device->delay_scan_in < 0)
cmd->id = CMD_SCAN_IN;
else
cmd->id = CMD_SLOW_SCAN_IN;
ret = ulink_allocate_payload(cmd, 5, PAYLOAD_DIRECTION_OUT);
break;case SCAN_IN:
case SCAN_OUT:
if (device->delay_scan_out < 0)
cmd->id = CMD_SCAN_OUT;
else
cmd->id = CMD_SLOW_SCAN_OUT;
ret = ulink_allocate_payload(cmd, scan_size_bytes + 5, PAYLOAD_DIRECTION_OUT);
break;case SCAN_OUT:
case SCAN_IO:
if (device->delay_scan_io < 0)
cmd->id = CMD_SCAN_IO;
else
cmd->id = CMD_SLOW_SCAN_IO;
ret = ulink_allocate_payload(cmd, scan_size_bytes + 5, PAYLOAD_DIRECTION_OUT);
break;case SCAN_IO:
default:
LOG_ERROR("BUG: ulink_append_scan_cmd() encountered an unknown scan type");
ret = ERROR_FAIL;
break;default
}switch (scan_type) { ... }
if (ret != ERROR_OK) {
free(cmd);
return ret;
}if (ret != ERROR_OK) { ... }
cmd->payload_out[0] = scan_size_bytes & 0xFF;
cmd->payload_out[1] = bits_last_byte & 0xFF;
cmd->payload_out[2] = ((tms_count_start & 0x0F) << 4) | (tms_count_end & 0x0F);
cmd->payload_out[3] = tms_sequence_start;
cmd->payload_out[4] = tms_sequence_end;
if ((scan_type == SCAN_OUT) || (scan_type == SCAN_IO)) {
for (i = 0; i < scan_size_bytes; i++)
cmd->payload_out[i + 5] = tdi[i];
}if ((scan_type == SCAN_OUT) || (scan_type == SCAN_IO)) { ... }
if ((scan_type == SCAN_IN) || (scan_type == SCAN_IO)) {
cmd->payload_in_start = tdo_start;
cmd->payload_in = tdo;
cmd->payload_in_size = scan_size_bytes;
}if ((scan_type == SCAN_IN) || (scan_type == SCAN_IO)) { ... }
cmd->needs_postprocessing = postprocess;
cmd->cmd_origin = origin;
/* ... */
cmd->free_payload_in_start = postprocess;
return ulink_append_queue(device, cmd);
}{ ... }
/* ... */
static int ulink_append_clock_tms_cmd(struct ulink *device, uint8_t count,
uint8_t sequence)
{
struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
int ret;
if (!cmd)
return ERROR_FAIL;
if (device->delay_clock_tms < 0)
cmd->id = CMD_CLOCK_TMS;
else
cmd->id = CMD_SLOW_CLOCK_TMS;
ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
if (ret != ERROR_OK) {
free(cmd);
return ret;
}if (ret != ERROR_OK) { ... }
cmd->payload_out[0] = count;
cmd->payload_out[1] = sequence;
return ulink_append_queue(device, cmd);
}{ ... }
/* ... */
static int ulink_append_clock_tck_cmd(struct ulink *device, uint16_t count)
{
struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
int ret;
if (!cmd)
return ERROR_FAIL;
if (device->delay_clock_tck < 0)
cmd->id = CMD_CLOCK_TCK;
else
cmd->id = CMD_SLOW_CLOCK_TCK;
ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
if (ret != ERROR_OK) {
free(cmd);
return ret;
}if (ret != ERROR_OK) { ... }
cmd->payload_out[0] = count & 0xff;
cmd->payload_out[1] = (count >> 8) & 0xff;
return ulink_append_queue(device, cmd);
}{ ... }
/* ... */
static int ulink_append_get_signals_cmd(struct ulink *device)
{
struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
int ret;
if (!cmd)
return ERROR_FAIL;
cmd->id = CMD_GET_SIGNALS;
cmd->needs_postprocessing = true;
ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_IN);
if (ret != ERROR_OK) {
free(cmd);
return ret;
}if (ret != ERROR_OK) { ... }
return ulink_append_queue(device, cmd);
}{ ... }
/* ... */
static int ulink_append_set_signals_cmd(struct ulink *device, uint8_t low,
uint8_t high)
{
struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
int ret;
if (!cmd)
return ERROR_FAIL;
cmd->id = CMD_SET_SIGNALS;
ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
if (ret != ERROR_OK) {
free(cmd);
return ret;
}if (ret != ERROR_OK) { ... }
cmd->payload_out[0] = low;
cmd->payload_out[1] = high;
return ulink_append_queue(device, cmd);
}{ ... }
/* ... */
static int ulink_append_sleep_cmd(struct ulink *device, uint32_t us)
{
struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
int ret;
if (!cmd)
return ERROR_FAIL;
cmd->id = CMD_SLEEP_US;
ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
if (ret != ERROR_OK) {
free(cmd);
return ret;
}if (ret != ERROR_OK) { ... }
cmd->payload_out[0] = us & 0x00ff;
cmd->payload_out[1] = (us >> 8) & 0x00ff;
return ulink_append_queue(device, cmd);
}{ ... }
/* ... */
static int ulink_append_configure_tck_cmd(struct ulink *device, int delay_scan_in,
int delay_scan_out, int delay_scan_io, int delay_tck, int delay_tms)
{
struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
int ret;
if (!cmd)
return ERROR_FAIL;
cmd->id = CMD_CONFIGURE_TCK_FREQ;
/* ... */
ret = ulink_allocate_payload(cmd, 5, PAYLOAD_DIRECTION_OUT);
if (ret != ERROR_OK) {
free(cmd);
return ret;
}if (ret != ERROR_OK) { ... }
if (delay_scan_in < 0)
cmd->payload_out[0] = 0;
else
cmd->payload_out[0] = (uint8_t)delay_scan_in;
if (delay_scan_out < 0)
cmd->payload_out[1] = 0;
else
cmd->payload_out[1] = (uint8_t)delay_scan_out;
if (delay_scan_io < 0)
cmd->payload_out[2] = 0;
else
cmd->payload_out[2] = (uint8_t)delay_scan_io;
if (delay_tck < 0)
cmd->payload_out[3] = 0;
else
cmd->payload_out[3] = (uint8_t)delay_tck;
if (delay_tms < 0)
cmd->payload_out[4] = 0;
else
cmd->payload_out[4] = (uint8_t)delay_tms;
return ulink_append_queue(device, cmd);
}{ ... }
/* ... */
static int ulink_append_led_cmd(struct ulink *device, uint8_t led_state)
{
struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
int ret;
if (!cmd)
return ERROR_FAIL;
cmd->id = CMD_SET_LEDS;
ret = ulink_allocate_payload(cmd, 1, PAYLOAD_DIRECTION_OUT);
if (ret != ERROR_OK) {
free(cmd);
return ret;
}if (ret != ERROR_OK) { ... }
cmd->payload_out[0] = led_state;
return ulink_append_queue(device, cmd);
}{ ... }
/* ... */
static int ulink_append_test_cmd(struct ulink *device)
{
struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
int ret;
if (!cmd)
return ERROR_FAIL;
cmd->id = CMD_TEST;
ret = ulink_allocate_payload(cmd, 1, PAYLOAD_DIRECTION_OUT);
if (ret != ERROR_OK) {
free(cmd);
return ret;
}if (ret != ERROR_OK) { ... }
cmd->payload_out[0] = 0xAA;
return ulink_append_queue(device, cmd);
}{ ... }
OpenULINK command queue helper functions
/* ... */
static int ulink_calculate_delay(enum ulink_delay_type type, long f, int *delay)
{
float t, x, x_ceil;
t = 1.0 / (float)(f);
switch (type) {
case DELAY_CLOCK_TCK:
x = (t - (float)(6E-6)) / (float)(4E-6);
break;case DELAY_CLOCK_TCK:
case DELAY_CLOCK_TMS:
x = (t - (float)(8.5E-6)) / (float)(4E-6);
break;case DELAY_CLOCK_TMS:
case DELAY_SCAN_IN:
x = (t - (float)(8.8308E-6)) / (float)(4E-6);
break;case DELAY_SCAN_IN:
case DELAY_SCAN_OUT:
x = (t - (float)(1.0527E-5)) / (float)(4E-6);
break;case DELAY_SCAN_OUT:
case DELAY_SCAN_IO:
x = (t - (float)(1.3132E-5)) / (float)(4E-6);
break;case DELAY_SCAN_IO:
default:
return ERROR_FAIL;
break;default
}switch (type) { ... }
/* ... */
if (x < 0)
x = 0;
/* ... */
x_ceil = ceilf(x);
if (x_ceil > 255)
return ERROR_FAIL;
*delay = (int)x_ceil;
return ERROR_OK;
}{ ... }
/* ... */
static long ulink_calculate_frequency(enum ulink_delay_type type, int delay)
{
float t, f_float;
if (delay > 255)
return 0;
switch (type) {
case DELAY_CLOCK_TCK:
if (delay < 0)
t = (float)(2.666E-6);
else
t = (float)(4E-6) * (float)(delay) + (float)(6E-6);
break;case DELAY_CLOCK_TCK:
case DELAY_CLOCK_TMS:
if (delay < 0)
t = (float)(5.666E-6);
else
t = (float)(4E-6) * (float)(delay) + (float)(8.5E-6);
break;case DELAY_CLOCK_TMS:
case DELAY_SCAN_IN:
if (delay < 0)
t = (float)(5.5E-6);
else
t = (float)(4E-6) * (float)(delay) + (float)(8.8308E-6);
break;case DELAY_SCAN_IN:
case DELAY_SCAN_OUT:
if (delay < 0)
t = (float)(7.0E-6);
else
t = (float)(4E-6) * (float)(delay) + (float)(1.0527E-5);
break;case DELAY_SCAN_OUT:
case DELAY_SCAN_IO:
if (delay < 0)
t = (float)(9.926E-6);
else
t = (float)(4E-6) * (float)(delay) + (float)(1.3132E-5);
break;case DELAY_SCAN_IO:
default:
return 0;default
}switch (type) { ... }
f_float = 1.0 / t;
return roundf(f_float);
}{ ... }
OpenULINK TCK frequency helper functions
/* ... */
static void ulink_set_end_state(tap_state_t endstate)
{
if (tap_is_state_stable(endstate))
tap_set_end_state(endstate);
else {
LOG_ERROR("BUG: %s is not a valid end state", tap_state_name(endstate));
exit(EXIT_FAILURE);
}else { ... }
}{ ... }
/* ... */
static int ulink_queue_statemove(struct ulink *device)
{
uint8_t tms_sequence, tms_count;
int ret;
if (tap_get_state() == tap_get_end_state()) {
return ERROR_OK;
}if (tap_get_state() == tap_get_end_state()) { ... }
tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
ret = ulink_append_clock_tms_cmd(device, tms_count, tms_sequence);
if (ret == ERROR_OK)
tap_set_state(tap_get_end_state());
return ret;
}{ ... }
/* ... */
static int ulink_queue_scan(struct ulink *device, struct jtag_command *cmd)
{
uint32_t scan_size_bits, scan_size_bytes, bits_last_scan;
uint32_t scans_max_payload, bytecount;
uint8_t *tdi_buffer_start = NULL, *tdi_buffer = NULL;
uint8_t *tdo_buffer_start = NULL, *tdo_buffer = NULL;
uint8_t first_tms_count, first_tms_sequence;
uint8_t last_tms_count, last_tms_sequence;
uint8_t tms_count_pause, tms_sequence_pause;
uint8_t tms_count_resume, tms_sequence_resume;
uint8_t tms_count_start, tms_sequence_start;
uint8_t tms_count_end, tms_sequence_end;
enum scan_type type;
int ret;
scan_size_bits = jtag_scan_size(cmd->cmd.scan);
scan_size_bytes = DIV_ROUND_UP(scan_size_bits, 8);
type = jtag_scan_type(cmd->cmd.scan);
scans_max_payload = scan_size_bytes / 58;
bits_last_scan = scan_size_bits - (scans_max_payload * 58 * 8);
if ((type == SCAN_IN) || (type == SCAN_IO)) {
tdo_buffer_start = calloc(scan_size_bytes, sizeof(uint8_t));
if (!tdo_buffer_start)
return ERROR_FAIL;
tdo_buffer = tdo_buffer_start;
}if ((type == SCAN_IN) || (type == SCAN_IO)) { ... }
if ((type == SCAN_OUT) || (type == SCAN_IO)) {
jtag_build_buffer(cmd->cmd.scan, &tdi_buffer_start);
tdi_buffer = tdi_buffer_start;
}if ((type == SCAN_OUT) || (type == SCAN_IO)) { ... }
if (cmd->cmd.scan->ir_scan) {
ulink_set_end_state(TAP_IRSHIFT);
first_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
first_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
tap_set_state(TAP_IRSHIFT);
tap_set_end_state(cmd->cmd.scan->end_state);
last_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
last_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
tms_count_pause = tap_get_tms_path_len(TAP_IRSHIFT, TAP_IRPAUSE);
tms_sequence_pause = tap_get_tms_path(TAP_IRSHIFT, TAP_IRPAUSE);
tms_count_resume = tap_get_tms_path_len(TAP_IRPAUSE, TAP_IRSHIFT);
tms_sequence_resume = tap_get_tms_path(TAP_IRPAUSE, TAP_IRSHIFT);
}if (cmd->cmd.scan->ir_scan) { ... } else {
ulink_set_end_state(TAP_DRSHIFT);
first_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
first_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
tap_set_state(TAP_DRSHIFT);
tap_set_end_state(cmd->cmd.scan->end_state);
last_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
last_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
tms_count_pause = tap_get_tms_path_len(TAP_DRSHIFT, TAP_DRPAUSE);
tms_sequence_pause = tap_get_tms_path(TAP_DRSHIFT, TAP_DRPAUSE);
tms_count_resume = tap_get_tms_path_len(TAP_DRPAUSE, TAP_DRSHIFT);
tms_sequence_resume = tap_get_tms_path(TAP_DRPAUSE, TAP_DRSHIFT);
}else { ... }
bytecount = scan_size_bytes;
while (bytecount > 0) {
if (bytecount == scan_size_bytes) {
tms_count_start = first_tms_count;
tms_sequence_start = first_tms_sequence;
}if (bytecount == scan_size_bytes) { ... } else {
tms_count_start = tms_count_resume;
tms_sequence_start = tms_sequence_resume;
}else { ... }
if (bytecount > 58) {
tms_count_end = tms_count_pause;
tms_sequence_end = tms_sequence_pause;
ret = ulink_append_scan_cmd(device,
type,
58 * 8,
tdi_buffer,
tdo_buffer_start,
tdo_buffer,
tms_count_start,
tms_sequence_start,
tms_count_end,
tms_sequence_end,
cmd,
false);
bytecount -= 58;
if (tdi_buffer_start)
tdi_buffer += 58;
if (tdo_buffer_start)
tdo_buffer += 58;
}if (bytecount > 58) { ... } else if (bytecount == 58) {
tms_count_end = last_tms_count;
tms_sequence_end = last_tms_sequence;
ret = ulink_append_scan_cmd(device,
type,
58 * 8,
tdi_buffer,
tdo_buffer_start,
tdo_buffer,
tms_count_start,
tms_sequence_start,
tms_count_end,
tms_sequence_end,
cmd,
true);
bytecount = 0;
}else if (bytecount == 58) { ... } else {
tms_count_end = last_tms_count;
tms_sequence_end = last_tms_sequence;
ret = ulink_append_scan_cmd(device,
type,
bits_last_scan,
tdi_buffer,
tdo_buffer_start,
tdo_buffer,
tms_count_start,
tms_sequence_start,
tms_count_end,
tms_sequence_end,
cmd,
true);
bytecount = 0;
}else { ... }
if (ret != ERROR_OK) {
free(tdi_buffer_start);
free(tdo_buffer_start);
return ret;
}if (ret != ERROR_OK) { ... }
}while (bytecount > 0) { ... }
free(tdi_buffer_start);
tap_set_state(cmd->cmd.scan->end_state);
return ERROR_OK;
}{ ... }
/* ... */
static int ulink_queue_tlr_reset(struct ulink *device, struct jtag_command *cmd)
{
int ret;
ret = ulink_append_clock_tms_cmd(device, 5, 0xff);
if (ret == ERROR_OK)
tap_set_state(TAP_RESET);
return ret;
}{ ... }
/* ... */
static int ulink_queue_runtest(struct ulink *device, struct jtag_command *cmd)
{
int ret;
if (tap_get_state() != TAP_IDLE) {
ulink_set_end_state(TAP_IDLE);
ulink_queue_statemove(device);
}if (tap_get_state() != TAP_IDLE) { ... }
ret = ulink_append_clock_tck_cmd(device, cmd->cmd.runtest->num_cycles);
if (ret != ERROR_OK)
return ret;
if (cmd->cmd.runtest->end_state != tap_get_state()) {
tap_set_end_state(cmd->cmd.runtest->end_state);
ulink_queue_statemove(device);
}if (cmd->cmd.runtest->end_state != tap_get_state()) { ... }
return ERROR_OK;
}{ ... }
/* ... */
static int ulink_queue_reset(struct ulink *device, struct jtag_command *cmd)
{
uint8_t low = 0, high = 0;
if (cmd->cmd.reset->trst) {
tap_set_state(TAP_RESET);
high |= SIGNAL_TRST;
}if (cmd->cmd.reset->trst) { ... } else
low |= SIGNAL_TRST;
if (cmd->cmd.reset->srst)
high |= SIGNAL_RESET;
else
low |= SIGNAL_RESET;
return ulink_append_set_signals_cmd(device, low, high);
}{ ... }
/* ... */
static int ulink_queue_pathmove(struct ulink *device, struct jtag_command *cmd)
{
int ret, state_count;
tap_state_t *path;
uint8_t tms_sequence;
unsigned int num_states = cmd->cmd.pathmove->num_states;
path = cmd->cmd.pathmove->path;
state_count = 0;
while (num_states > 0) {
unsigned int batch_size;
tms_sequence = 0;
if (num_states >= 8)
batch_size = 8;
else
batch_size = num_states;
for (unsigned int i = 0; i < batch_size; i++) {
if (tap_state_transition(tap_get_state(), false) == path[state_count]) {
buf_set_u32(&tms_sequence, i, 1, 0x0);
}if (tap_state_transition(tap_get_state(), false) == path[state_count]) { ... } else if (tap_state_transition(tap_get_state(), true)
== path[state_count]) {
buf_set_u32(&tms_sequence, i, 1, 0x1);
}else if (tap_state_transition(tap_get_state(), true) == path[state_count]) { ... } else {
LOG_ERROR("BUG: %s -> %s isn't a valid TAP state transition",
tap_state_name(tap_get_state()),
tap_state_name(path[state_count]));
return ERROR_FAIL;
}else { ... }
tap_set_state(path[state_count]);
state_count++;
num_states--;
}for (unsigned int i = 0; i < batch_size; i++) { ... }
LOG_INFO(
"pathmove batch: count = %i, sequence = 0x%x", batch_size, tms_sequence);
ret = ulink_append_clock_tms_cmd(ulink_handle, batch_size, tms_sequence);
if (ret != ERROR_OK)
return ret;
}while (num_states > 0) { ... }
return ERROR_OK;
}{ ... }
/* ... */
static int ulink_queue_sleep(struct ulink *device, struct jtag_command *cmd)
{
/* ... */
return ulink_append_sleep_cmd(device, cmd->cmd.sleep->us);
}{ ... }
/* ... */
static int ulink_queue_stableclocks(struct ulink *device, struct jtag_command *cmd)
{
int ret;
if (!tap_is_state_stable(tap_get_state())) {
LOG_ERROR("JTAG_STABLECLOCKS: state not stable");
return ERROR_FAIL;
}if (!tap_is_state_stable(tap_get_state())) { ... }
unsigned int num_cycles = cmd->cmd.stableclocks->num_cycles;
if (tap_get_state() == TAP_RESET)
ret = ulink_append_set_signals_cmd(device, 0, SIGNAL_TMS);
else
ret = ulink_append_set_signals_cmd(device, SIGNAL_TMS, 0);
if (ret != ERROR_OK)
return ret;
while (num_cycles > 0) {
if (num_cycles > 0xFFFF) {
ret = ulink_append_clock_tck_cmd(device, 0xFFFF);
num_cycles -= 0xFFFF;
}if (num_cycles > 0xFFFF) { ... } else {
ret = ulink_append_clock_tck_cmd(device, num_cycles);
num_cycles = 0;
}else { ... }
if (ret != ERROR_OK)
return ret;
}while (num_cycles > 0) { ... }
return ERROR_OK;
}{ ... }
/* ... */
static int ulink_post_process_scan(struct ulink_cmd *ulink_cmd)
{
struct jtag_command *cmd = ulink_cmd->cmd_origin;
int ret;
switch (jtag_scan_type(cmd->cmd.scan)) {
case SCAN_IN:
case SCAN_IO:
ret = jtag_read_buffer(ulink_cmd->payload_in_start, cmd->cmd.scan);
break;case SCAN_IO:
case SCAN_OUT:
ret = ERROR_OK;
break;case SCAN_OUT:
default:
LOG_ERROR("BUG: ulink_post_process_scan() encountered an unknown"
" JTAG scan type");
ret = ERROR_FAIL;
break;default
}switch (jtag_scan_type(cmd->cmd.scan)) { ... }
return ret;
}{ ... }
/* ... */
static int ulink_post_process_queue(struct ulink *device)
{
struct ulink_cmd *current;
struct jtag_command *openocd_cmd;
int ret;
current = device->queue_start;
while (current) {
openocd_cmd = current->cmd_origin;
/* ... */
if ((current->needs_postprocessing == true) && (openocd_cmd)) {
switch (openocd_cmd->type) {
case JTAG_SCAN:
ret = ulink_post_process_scan(current);
break;case JTAG_SCAN:
case JTAG_TLR_RESET:
case JTAG_RUNTEST:
case JTAG_RESET:
case JTAG_PATHMOVE:
case JTAG_SLEEP:
case JTAG_STABLECLOCKS:
ret = ERROR_OK;
break;case JTAG_STABLECLOCKS:
default:
ret = ERROR_FAIL;
LOG_ERROR("BUG: ulink_post_process_queue() encountered unknown JTAG "
"command type");
break;default
}switch (openocd_cmd->type) { ... }
if (ret != ERROR_OK)
return ret;
}if ((current->needs_postprocessing == true) && (openocd_cmd)) { ... }
current = current->next;
}while (current) { ... }
return ERROR_OK;
}{ ... }
Interface between OpenULINK and OpenOCD
/* ... */
static int ulink_execute_queue(struct jtag_command *cmd_queue)
{
struct jtag_command *cmd = cmd_queue;
int ret;
while (cmd) {
switch (cmd->type) {
case JTAG_SCAN:
ret = ulink_queue_scan(ulink_handle, cmd);
break;case JTAG_SCAN:
case JTAG_TLR_RESET:
ret = ulink_queue_tlr_reset(ulink_handle, cmd);
break;case JTAG_TLR_RESET:
case JTAG_RUNTEST:
ret = ulink_queue_runtest(ulink_handle, cmd);
break;case JTAG_RUNTEST:
case JTAG_RESET:
ret = ulink_queue_reset(ulink_handle, cmd);
break;case JTAG_RESET:
case JTAG_PATHMOVE:
ret = ulink_queue_pathmove(ulink_handle, cmd);
break;case JTAG_PATHMOVE:
case JTAG_SLEEP:
ret = ulink_queue_sleep(ulink_handle, cmd);
break;case JTAG_SLEEP:
case JTAG_STABLECLOCKS:
ret = ulink_queue_stableclocks(ulink_handle, cmd);
break;case JTAG_STABLECLOCKS:
default:
ret = ERROR_FAIL;
LOG_ERROR("BUG: encountered unknown JTAG command type");
break;default
}switch (cmd->type) { ... }
if (ret != ERROR_OK)
return ret;
cmd = cmd->next;
}while (cmd) { ... }
if (ulink_handle->commands_in_queue > 0) {
ret = ulink_execute_queued_commands(ulink_handle, LIBUSB_TIMEOUT_MS);
if (ret != ERROR_OK)
return ret;
ret = ulink_post_process_queue(ulink_handle);
if (ret != ERROR_OK)
return ret;
ulink_clear_queue(ulink_handle);
}if (ulink_handle->commands_in_queue > 0) { ... }
return ERROR_OK;
}{ ... }
/* ... */
static int ulink_khz(int khz, int *jtag_speed)
{
int ret;
if (khz == 0) {
LOG_ERROR("RCLK not supported");
return ERROR_FAIL;
}if (khz == 0) { ... }
/* ... */
if (khz >= 375)
ulink_handle->delay_clock_tck = -1;
else {
ret = ulink_calculate_delay(DELAY_CLOCK_TCK, khz * 1000,
&ulink_handle->delay_clock_tck);
if (ret != ERROR_OK)
return ret;
}else { ... }
/* ... */
if (khz >= 176) {
ulink_handle->delay_clock_tms = -1;
ulink_handle->delay_scan_in = -1;
ulink_handle->delay_scan_out = -1;
ulink_handle->delay_scan_io = -1;
}if (khz >= 176) { ... } else {
ret = ulink_calculate_delay(DELAY_CLOCK_TMS, khz * 1000,
&ulink_handle->delay_clock_tms);
if (ret != ERROR_OK)
return ret;
ret = ulink_calculate_delay(DELAY_SCAN_IN, khz * 1000,
&ulink_handle->delay_scan_in);
if (ret != ERROR_OK)
return ret;
ret = ulink_calculate_delay(DELAY_SCAN_OUT, khz * 1000,
&ulink_handle->delay_scan_out);
if (ret != ERROR_OK)
return ret;
ret = ulink_calculate_delay(DELAY_SCAN_IO, khz * 1000,
&ulink_handle->delay_scan_io);
if (ret != ERROR_OK)
return ret;
}else { ... }
LOG_DEBUG_IO("ULINK TCK setup: delay_tck = %i (%li Hz),",
ulink_handle->delay_clock_tck,
ulink_calculate_frequency(DELAY_CLOCK_TCK, ulink_handle->delay_clock_tck));
LOG_DEBUG_IO(" delay_tms = %i (%li Hz),",
ulink_handle->delay_clock_tms,
ulink_calculate_frequency(DELAY_CLOCK_TMS, ulink_handle->delay_clock_tms));
LOG_DEBUG_IO(" delay_scan_in = %i (%li Hz),",
ulink_handle->delay_scan_in,
ulink_calculate_frequency(DELAY_SCAN_IN, ulink_handle->delay_scan_in));
LOG_DEBUG_IO(" delay_scan_out = %i (%li Hz),",
ulink_handle->delay_scan_out,
ulink_calculate_frequency(DELAY_SCAN_OUT, ulink_handle->delay_scan_out));
LOG_DEBUG_IO(" delay_scan_io = %i (%li Hz),",
ulink_handle->delay_scan_io,
ulink_calculate_frequency(DELAY_SCAN_IO, ulink_handle->delay_scan_io));
ret = ulink_append_configure_tck_cmd(ulink_handle,
ulink_handle->delay_scan_in,
ulink_handle->delay_scan_out,
ulink_handle->delay_scan_io,
ulink_handle->delay_clock_tck,
ulink_handle->delay_clock_tms);
if (ret != ERROR_OK)
return ret;
*jtag_speed = khz;
return ERROR_OK;
}{ ... }
/* ... */
static int ulink_speed(int speed)
{
int dummy;
return ulink_khz(speed, &dummy);
}{ ... }
/* ... */
static int ulink_speed_div(int speed, int *khz)
{
*khz = speed;
return ERROR_OK;
}{ ... }
/* ... */
static int ulink_init(void)
{
int ret, transferred;
char str_manufacturer[20];
bool download_firmware = false;
unsigned char *dummy;
uint8_t input_signals, output_signals;
ulink_handle = calloc(1, sizeof(struct ulink));
if (!ulink_handle)
return ERROR_FAIL;
libusb_init(&ulink_handle->libusb_ctx);
ret = ulink_usb_open(&ulink_handle);
if (ret != ERROR_OK) {
LOG_ERROR("Could not open ULINK device");
free(ulink_handle);
ulink_handle = NULL;
return ret;
}if (ret != ERROR_OK) { ... }
ret = libusb_get_string_descriptor_ascii(ulink_handle->usb_device_handle, 1, (unsigned char *)str_manufacturer, 20);
if (ret < 0) {
download_firmware = true;
}if (ret < 0) { ... } else {
if (strncmp(str_manufacturer, "OpenULINK", 9) != 0)
download_firmware = true;
}else { ... }
if (download_firmware == true) {
LOG_INFO("Loading OpenULINK firmware. This is reversible by power-cycling"
" ULINK device.");
ret = ulink_load_firmware_and_renumerate(&ulink_handle,
ULINK_FIRMWARE_FILE, ULINK_RENUMERATION_DELAY);
if (ret != ERROR_OK) {
LOG_ERROR("Could not download firmware and re-numerate ULINK");
free(ulink_handle);
ulink_handle = NULL;
return ret;
}if (ret != ERROR_OK) { ... }
}if (download_firmware == true) { ... } else
LOG_INFO("ULINK device is already running OpenULINK firmware");
ret = jtag_libusb_choose_interface(ulink_handle->usb_device_handle,
&ulink_handle->ep_in, &ulink_handle->ep_out, -1, -1, -1, -1);
if (ret != ERROR_OK)
return ret;
ulink_clear_queue(ulink_handle);
ret = ulink_append_test_cmd(ulink_handle);
if (ret != ERROR_OK)
return ret;
ret = ulink_execute_queued_commands(ulink_handle, 200);
if (ret != ERROR_OK) {
/* ... */
dummy = calloc(64, sizeof(uint8_t));
ret = libusb_bulk_transfer(ulink_handle->usb_device_handle, ulink_handle->ep_in,
dummy, 64, &transferred, 200);
free(dummy);
if (ret != 0 || transferred == 0) {
LOG_ERROR("Cannot communicate with ULINK device. Disconnect ULINK from "
"the USB port and re-connect, then re-run OpenOCD");
free(ulink_handle);
ulink_handle = NULL;
return ERROR_FAIL;
}if (ret != 0 || transferred == 0) { ... }
#ifdef _DEBUG_USB_COMMS_
else {
LOG_INFO("Recovered from lost Bulk IN packet");
}else { ... }
/* ... */#endif
}if (ret != ERROR_OK) { ... }
ulink_clear_queue(ulink_handle);
ret = ulink_append_get_signals_cmd(ulink_handle);
if (ret == ERROR_OK)
ret = ulink_execute_queued_commands(ulink_handle, 200);
if (ret == ERROR_OK) {
input_signals = ulink_handle->queue_start->payload_in[0];
output_signals = ulink_handle->queue_start->payload_in[1];
ulink_print_signal_states(input_signals, output_signals);
}if (ret == ERROR_OK) { ... }
ulink_clear_queue(ulink_handle);
return ERROR_OK;
}{ ... }
/* ... */
static int ulink_quit(void)
{
int ret;
ret = ulink_usb_close(&ulink_handle);
free(ulink_handle);
return ret;
}{ ... }
/* ... */
COMMAND_HANDLER(ulink_download_firmware_handler)
{
int ret;
if (CMD_ARGC != 1)
return ERROR_COMMAND_SYNTAX_ERROR;
LOG_INFO("Downloading ULINK firmware image %s", CMD_ARGV[0]);
ret = ulink_load_firmware_and_renumerate(&ulink_handle, CMD_ARGV[0],
ULINK_RENUMERATION_DELAY);
return ret;
}{ ... }
JTAG driver functions
static const struct command_registration ulink_subcommand_handlers[] = {
{
.name = "download_firmware",
.handler = &ulink_download_firmware_handler,
.mode = COMMAND_EXEC,
.help = "download firmware image to ULINK device",
.usage = "path/to/ulink_firmware.hex",
...},
COMMAND_REGISTRATION_DONE,
...};
static const struct command_registration ulink_command_handlers[] = {
{
.name = "ulink",
.mode = COMMAND_ANY,
.help = "perform ulink management",
.chain = ulink_subcommand_handlers,
.usage = "",
...},
COMMAND_REGISTRATION_DONE
...};
static struct jtag_interface ulink_interface = {
.execute_queue = ulink_execute_queue,
...};
struct adapter_driver ulink_adapter_driver = {
.name = "ulink",
.transports = jtag_only,
.commands = ulink_command_handlers,
.init = ulink_init,
.quit = ulink_quit,
.speed = ulink_speed,
.khz = ulink_khz,
.speed_div = ulink_speed_div,
.jtag_ops = &ulink_interface,
...};