-
Notifications
You must be signed in to change notification settings - Fork 15
/
ftdiJTAG.c
1181 lines (1110 loc) · 35.8 KB
/
ftdiJTAG.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* XVC FTDI JTAG Copyright (c) 2021, The Regents of the University of
* California, through Lawrence Berkeley National Laboratory (subject to
* receipt of any required approvals from the U.S. Dept. of Energy). All
* rights reserved.
*
* If you have questions about your rights to use or distribute this software,
* please contact Berkeley Lab's Intellectual Property Office at
*
* NOTICE. This Software was developed under funding from the U.S. Department
* of Energy and the U.S. Government consequently retains certain rights. As
* such, the U.S. Government has been granted for itself and others acting on
* its behalf a paid-up, nonexclusive, irrevocable, worldwide license in the
* Software to reproduce, distribute copies to the public, prepare derivative
* works, and perform publicly and display publicly, and to permit others to
* do so.
*/
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <getopt.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <libusb-1.0/libusb.h>
#if (!defined(LIBUSBX_API_VERSION) || (LIBUSBX_API_VERSION < 0x01000102))
# error "You need to get a newer version of libusb-1.0 (16 at the very least)"
#endif
#define XVC_BUFSIZE 1024
#define FTDI_CLOCK_RATE 60000000
#define IDSTRING_CAPACITY 100
#define USB_BUFSIZE 512
/* libusb bmRequestType */
#define BMREQTYPE_OUT (LIBUSB_REQUEST_TYPE_VENDOR | \
LIBUSB_RECIPIENT_DEVICE | \
LIBUSB_ENDPOINT_OUT)
/* libusb bRequest */
#define BREQ_RESET 0x00
#define BREQ_SET_LATENCY 0x09
#define BREQ_SET_BITMODE 0x0B
/* libusb wValue for assorted bRequest values */
#define WVAL_RESET_RESET 0x00
#define WVAL_RESET_PURGE_RX 0x01
#define WVAL_RESET_PURGE_TX 0x02
#define WVAL_SET_BITMODE_MPSSE (0x0200 | \
FTDI_PIN_TCK | \
FTDI_PIN_TDI | \
FTDI_PIN_TMS)
/* FTDI commands (first byte of bulk write transfer) */
#define FTDI_MPSSE_BIT_WRITE_TMS 0x40
#define FTDI_MPSSE_BIT_READ_DATA 0x20
#define FTDI_MPSSE_BIT_WRITE_DATA 0x10
#define FTDI_MPSSE_BIT_LSB_FIRST 0x08
#define FTDI_MPSSE_BIT_READ_ON_FALLING_EDGE 0x04
#define FTDI_MPSSE_BIT_BIT_MODE 0x02
#define FTDI_MPSSE_BIT_WRITE_ON_FALLING_EDGE 0x01
#define FTDI_MPSSE_XFER_TDI_BYTES (FTDI_MPSSE_BIT_WRITE_DATA | \
FTDI_MPSSE_BIT_READ_DATA | \
FTDI_MPSSE_BIT_LSB_FIRST | \
FTDI_MPSSE_BIT_WRITE_ON_FALLING_EDGE)
#define FTDI_MPSSE_XFER_TDI_BITS (FTDI_MPSSE_BIT_WRITE_DATA | \
FTDI_MPSSE_BIT_READ_DATA | \
FTDI_MPSSE_BIT_LSB_FIRST | \
FTDI_MPSSE_BIT_BIT_MODE | \
FTDI_MPSSE_BIT_WRITE_ON_FALLING_EDGE)
#define FTDI_MPSSE_XFER_TMS_BITS (FTDI_MPSSE_BIT_WRITE_TMS | \
FTDI_MPSSE_BIT_READ_DATA | \
FTDI_MPSSE_BIT_LSB_FIRST | \
FTDI_MPSSE_BIT_BIT_MODE | \
FTDI_MPSSE_BIT_WRITE_ON_FALLING_EDGE)
#define FTDI_SET_LOW_BYTE 0x80
#define FTDI_ENABLE_LOOPBACK 0x84
#define FTDI_DISABLE_LOOPBACK 0x85
#define FTDI_SET_TCK_DIVISOR 0x86
#define FTDI_DISABLE_TCK_PRESCALER 0x8A
#define FTDI_DISABLE_3_PHASE_CLOCK 0x8D
#define FTDI_ACK_BAD_COMMAND 0xFA
/* FTDI I/O pin bits */
#define FTDI_PIN_TCK 0x1
#define FTDI_PIN_TDI 0x2
#define FTDI_PIN_TDO 0x4
#define FTDI_PIN_TMS 0x8
typedef struct usbInfo {
/*
* Diagnostics
*/
int quietFlag;
int runtFlag;
int loopback;
int showUSB;
int showXVC;
unsigned int lockedSpeed;
/*
* Statistics
*/
int statisticsFlag;
int largestShiftRequest;
int largestWriteRequest;
int largestWriteSent;
int largestReadRequest;
uint64_t shiftCount;
uint64_t chunkCount;
uint64_t bitCount;
/*
* Used to find matching device
*/
int vendorId;
int productId;
const char *serialNumber;
/*
* Matched device
*/
int deviceVendorId;
int deviceProductId;
char deviceVendorString[IDSTRING_CAPACITY];
char deviceProductString[IDSTRING_CAPACITY];
char deviceSerialString[IDSTRING_CAPACITY];
/*
* Libusb hooks
*/
libusb_context *usb;
libusb_device_handle *handle;
int bInterfaceNumber;
int isConnected;
int termChar;
unsigned char bTag;
int bulkOutEndpointAddress;
int bulkOutRequestSize;
int bulkInEndpointAddress;
int bulkInRequestSize;
/*
* FTDI info
*/
int ftdiJTAGindex;
const char *gpioArgument;
/*
* I/O buffers
*/
unsigned char tmsBuf[XVC_BUFSIZE];
unsigned char tdiBuf[XVC_BUFSIZE];
unsigned char tdoBuf[XVC_BUFSIZE];
int txCount;
unsigned char ioBuf[USB_BUFSIZE];
unsigned char rxBuf[USB_BUFSIZE];
unsigned char cmdBuf[USB_BUFSIZE];
} usbInfo;
/************************************* MISC ***************************/
static void
showBuf(const char *name, const unsigned char *buf, int numBytes)
{
int i;
printf("%s%4d:", name, numBytes);
if (numBytes > 40) numBytes = 40;
for (i = 0 ; i < numBytes ; i++) printf(" %02X", buf[i]);
printf("\n");
}
static void
badEOF(void)
{
fprintf(stderr, "Unexpected EOF!\n");
}
static void
badChar(void)
{
fprintf(stderr, "Unexpected character!\n");
}
/*
* Fetch 32 bit value from client
*/
static int
fetch32(FILE *fp, uint32_t *value)
{
int i;
uint32_t v;
for (i = 0, v = 0 ; i < 32 ; i += 8) {
int c = fgetc(fp);
if (c == EOF) {
badEOF();
return 0;
}
v |= c << i;
}
*value = v;
return 1;
}
/************************************* USB ***************************/
static void
getDeviceString(usbInfo *usb, int i, char *dest)
{
ssize_t n;
n = libusb_get_string_descriptor_ascii(usb->handle, i,
usb->ioBuf, sizeof usb->ioBuf);
if (n < 0) {
*dest = '\0';
return;
}
if (n >= IDSTRING_CAPACITY)
n = IDSTRING_CAPACITY - 1;
memcpy(dest, (char *)usb->ioBuf, n);
*(dest + n) = '\0';
}
static void
getDeviceStrings(usbInfo *usb, struct libusb_device_descriptor *desc)
{
getDeviceString(usb, desc->iManufacturer, usb->deviceVendorString);
getDeviceString(usb, desc->iProduct, usb->deviceProductString);
getDeviceString(usb, desc->iSerialNumber, usb->deviceSerialString);
}
/*
* Get endpoints
*/
static void
getEndpoints(usbInfo *usb, const struct libusb_interface_descriptor *iface_desc)
{
int e;
usb->bulkInEndpointAddress = 0;
usb->bulkOutEndpointAddress = 0;
for (e = 0 ; e < iface_desc->bNumEndpoints ; e++) {
const struct libusb_endpoint_descriptor *ep = &iface_desc->endpoint[e];
if ((ep->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) ==
LIBUSB_TRANSFER_TYPE_BULK) {
if ((ep->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) ==
LIBUSB_ENDPOINT_IN) {
if (usb->bulkInEndpointAddress != 0) {
fprintf(stderr, "Too many input endpoints!\n");
exit(10);
}
usb->bulkInEndpointAddress = ep->bEndpointAddress;
usb->bulkInRequestSize = ep->wMaxPacketSize;
if ((size_t)usb->bulkInRequestSize > sizeof usb->ioBuf) {
usb->bulkInRequestSize = sizeof usb->ioBuf;
}
}
else {
if (usb->bulkOutEndpointAddress != 0) {
fprintf(stderr, "Too many output endpoints!\n");
exit(10);
}
usb->bulkOutEndpointAddress = ep->bEndpointAddress;
usb->bulkOutRequestSize = ep->wMaxPacketSize;
if ((size_t)usb->bulkOutRequestSize > sizeof usb->cmdBuf) {
usb->bulkOutRequestSize = sizeof usb->cmdBuf;
}
}
}
}
if (usb->bulkInEndpointAddress == 0) {
fprintf(stderr, "No input endpoint!\n");
exit(10);
}
if (usb->bulkOutEndpointAddress == 0) {
fprintf(stderr, "No output endpoint!\n");
exit(10);
}
}
/*
* Search the bus for a matching device
*/
static int
findDevice(usbInfo *usb, libusb_device **list, int n)
{
int i;
for (i = 0 ; i < n ; i++) {
libusb_device *dev = list[i];
struct libusb_device_descriptor desc;
struct libusb_config_descriptor *config;
int productMatch = 0;
int s = libusb_get_device_descriptor(dev, &desc);
if (s != 0) {
fprintf(stderr, "libusb_get_device_descriptor failed: %s",
libusb_strerror(s));
return 0;
}
if (desc.bDeviceClass != LIBUSB_CLASS_PER_INTERFACE)
continue;
if (usb->productId < 0) {
static const uint16_t validCodes[] = { 0x6010, /* FT2232H */
0x6011, /* FT4232H */
0x6014 /* FT232H */
};
int nCodes = sizeof validCodes / sizeof validCodes[0];
int p;
for (p = 0 ; p < nCodes ; p++) {
if (desc.idProduct == validCodes[p]) {
productMatch = 1;
break;
}
}
}
else if (usb->productId == desc.idProduct) {
productMatch = 1;
}
if ((usb->vendorId != desc.idVendor) || !productMatch) {
continue;
}
if ((libusb_get_active_config_descriptor(dev, &config) < 0)
&& (libusb_get_config_descriptor(dev, 0, &config) < 0)) {
fprintf(stderr,
"Can't get vendor %04X product %04X configuration.\n",
desc.idVendor, desc.idProduct);
continue;
}
if (config == NULL) {
continue;
}
if (config->bNumInterfaces >= usb->ftdiJTAGindex) {
s = libusb_open(dev, &usb->handle);
if (s == 0) {
const struct libusb_interface *iface =
&config->interface[usb->ftdiJTAGindex-1];
const struct libusb_interface_descriptor *iface_desc =
&iface->altsetting[0];
usb->bInterfaceNumber = iface_desc->bInterfaceNumber;
usb->deviceVendorId = desc.idVendor;
usb->deviceProductId = desc.idProduct;
getDeviceStrings(usb, &desc);
if ((usb->serialNumber == NULL)
|| (strcmp(usb->serialNumber,
usb->deviceSerialString) == 0)) {
getEndpoints(usb, iface_desc);
libusb_free_config_descriptor(config);
usb->productId = desc.idProduct;
return 1;
}
libusb_close(usb->handle);
}
else {
fprintf(stderr, "libusb_open failed: %s\n",
libusb_strerror(s));
exit(1);
}
}
libusb_free_config_descriptor(config);
}
return 0;
}
static int
usbControl(usbInfo *usb, int bmRequestType, int bRequest, int wValue)
{
int c;
if (usb->showUSB) {
printf("usbControl bmRequestType:%02X bRequest:%02X wValue:%04X\n",
bmRequestType, bRequest, wValue);
}
c = libusb_control_transfer(usb->handle, bmRequestType, bRequest, wValue,
usb->ftdiJTAGindex, NULL, 0, 1000);
if (c != 0) {
fprintf(stderr, "usb_control_transfer failed: %s\n",libusb_strerror(c));
exit(1);
}
return 1;
}
static int
usbWriteData(usbInfo *usb, unsigned char *buf, int nSend)
{
int nSent, s;
if (usb->showUSB) {
showBuf("Tx", buf, nSend);
}
if (nSend > usb->largestWriteRequest) {
usb->largestWriteRequest = nSend;
}
while (nSend) {
s = libusb_bulk_transfer(usb->handle, usb->bulkOutEndpointAddress, buf,
nSend, &nSent, 10000);
if (s) {
fprintf(stderr, "Bulk write (%d) failed: %s\n", nSend,
libusb_strerror(s));
exit(1);
}
nSend -= nSent;
buf += nSent;
if (nSent > usb->largestWriteSent) {
usb->largestWriteSent = nSent;
}
}
return 1;
}
static int
usbReadData(usbInfo *usb, unsigned char *buf, int nWant)
{
int nWanted = nWant;
const unsigned char *base = buf;
if (nWant > usb->largestReadRequest) {
usb->largestReadRequest = nWant;
if ((nWant+2) > usb->bulkInRequestSize) {
fprintf(stderr, "usbReadData requested %d, limit is %d.\n",
nWant+2, usb->bulkInRequestSize);
exit(1);
}
}
while (nWant) {
int nRecv, s;
const unsigned char *src = usb->ioBuf;
s = libusb_bulk_transfer(usb->handle, usb->bulkInEndpointAddress,
usb->ioBuf, nWant+2, &nRecv, 5000);
if (s) {
fprintf(stderr, "Bulk read failed: %s\n", libusb_strerror(s));
exit(1);
}
if (nRecv <= 2) {
if (usb->runtFlag) {
fprintf(stderr, "wanted:%d want:%d got:%d",nWanted,nWant,nRecv);
if (nRecv >= 1) {
fprintf(stderr, " [%02X", src[0]);
if (nRecv >= 2) {
fprintf(stderr, " %02X", src[1]);
}
fprintf(stderr, "]");
}
fprintf(stderr, "\n");
}
continue;
}
else {
/* Skip FTDI status bytes */
nRecv -= 2;
src += 2;
}
if (nRecv > nWant) nRecv = nWant;
memcpy(buf, src, nRecv);
nWant -= nRecv;
buf += nRecv;
}
if (usb->showUSB) {
showBuf("Rx", base, nWanted);
}
return 1;
}
/************************************* FTDI/JTAG ***************************/
static int
divisorForFrequency(unsigned int frequency)
{
unsigned int divisor;
unsigned int actual;
double r;
static unsigned int warned = ~0;
if (frequency <= 0) frequency = 1;
divisor = ((FTDI_CLOCK_RATE / 2) + (frequency - 1)) / frequency;
if (divisor >= 0x10000) {
divisor = 0x10000;
}
if (divisor < 1) {
divisor = 1;
}
actual = FTDI_CLOCK_RATE / (2 * divisor);
r = (double)frequency / actual;
if (warned != actual) {
warned = actual;
if ((r < 0.999) || (r > 1.001)) {
fprintf(stderr, "Warning -- %d Hz clock requested, %d Hz actual\n",
frequency, actual);
}
if (actual < 500000) {
fprintf(stderr, "Warning -- %d Hz clock is a slow choice.\n",
actual);
}
}
return divisor;
}
static int
ftdiSetClockSpeed(usbInfo *usb, unsigned int frequency)
{
unsigned int count;
if (usb->lockedSpeed) {
frequency = usb->lockedSpeed;
}
count = divisorForFrequency(frequency) - 1;
usb->ioBuf[0] = FTDI_DISABLE_TCK_PRESCALER;
usb->ioBuf[1] = FTDI_SET_TCK_DIVISOR;
usb->ioBuf[2] = count;
usb->ioBuf[3] = count >> 8;
return usbWriteData(usb, usb->ioBuf, 4);
}
static int
ftdiGPIO(usbInfo *usb)
{
unsigned long value;
unsigned int direction;
const char *str = usb->gpioArgument;
char *endp;
static const struct timespec ms100 = { .tv_sec = 0, .tv_nsec = 100000000 };
usb->ioBuf[0] = FTDI_SET_LOW_BYTE;
for (;;) {
value = strtol(str, &endp, 16);
if ((endp == str) || ((*endp != '\0') && (*endp != ':'))) {
break;
}
str = endp + 1;
if (value > 0xFF) {
break;
}
direction = value >> 4;
value &= 0xF;
usb->ioBuf[1] = (value << 4) | FTDI_PIN_TMS;
usb->ioBuf[2] = (direction << 4) |
FTDI_PIN_TMS | FTDI_PIN_TDI | FTDI_PIN_TCK;
if (!usbWriteData(usb, usb->ioBuf, 3)) {
break;
}
if (*endp == '\0') {
return 1;
}
nanosleep(&ms100, NULL);
}
return 0;
}
static int
ftdiInit(usbInfo *usb)
{
static unsigned char startup[] = {
FTDI_DISABLE_LOOPBACK,
FTDI_DISABLE_3_PHASE_CLOCK,
FTDI_SET_LOW_BYTE,
FTDI_PIN_TMS,
FTDI_PIN_TMS | FTDI_PIN_TDI | FTDI_PIN_TCK
};
if (!usbControl(usb, BMREQTYPE_OUT, BREQ_RESET, WVAL_RESET_RESET)
|| !usbControl(usb, BMREQTYPE_OUT, BREQ_SET_BITMODE,WVAL_SET_BITMODE_MPSSE)
|| !usbControl(usb, BMREQTYPE_OUT, BREQ_SET_LATENCY, 2)
|| !usbControl(usb, BMREQTYPE_OUT, BREQ_RESET, WVAL_RESET_PURGE_TX)
|| !usbControl(usb, BMREQTYPE_OUT, BREQ_RESET, WVAL_RESET_PURGE_RX)
|| !ftdiSetClockSpeed(usb, 10000000)
|| !usbWriteData(usb, startup, sizeof startup)) {
return 0;
}
if (usb->gpioArgument && !ftdiGPIO(usb)) {
fprintf(stderr, "Bad -g direction:value[:value...]\n");
return 0;
}
return 1;
}
/************************************* XVC ***************************/
static void
cmdByte(usbInfo *usb, int byte)
{
if (usb->txCount == USB_BUFSIZE) {
fprintf(stderr, "USB TX OVERFLOW!\n");
exit(4);
}
usb->ioBuf[usb->txCount++] = byte;
}
/*
* The USB/JTAG chip can't shift data to TMS and TDI simultaneously
* so switch between TMS and TDI shift commands as necessary.
* Break into chunks small enough to fit in single packet.
*/
static int
shiftChunks(usbInfo *usb, int nBits)
{
int iBit = 0x01, iIndex = 0;
int cmdBit, cmdIndex, cmdBitcount;
int tmsBit, tmsBits, tmsState;
int rxBit, rxIndex;
int tdoBit = 0x01, tdoIndex = 0;
unsigned short rxBitcounts[USB_BUFSIZE/3];
if (usb->loopback) {
cmdByte(usb, FTDI_ENABLE_LOOPBACK);
}
while (nBits) {
int rxBytesWanted = 0;
int rxBitcountIndex = 0;
usb->txCount = 0;
usb->chunkCount++;
do {
/*
* Stash TMS bits until bit limit reached or TDI would change state
*/
int tdiFirstState = ((usb->tdiBuf[iIndex] & iBit) != 0);
cmdBitcount = 0;
cmdBit = 0x01;
tmsBits = 0;
do {
tmsBit = (usb->tmsBuf[iIndex] & iBit) ? cmdBit : 0;
tmsBits |= tmsBit;
if (iBit == 0x80) {
iBit = 0x01;
iIndex++;
}
else {
iBit <<= 1;
}
cmdBitcount++;
cmdBit <<= 1;
} while ((cmdBitcount < 6) && (cmdBitcount < nBits)
&& (((usb->tdiBuf[iIndex] & iBit) != 0) == tdiFirstState));
/*
* Duplicate the final TMS bit so the TMS pin holds
* its value for subsequent TDI shift commands.
* This is why the bit limit above is 6 and not 7 since
* we need space to hold the copy of the final bit.
*/
tmsBits |= (tmsBit << 1);
tmsState = (tmsBit != 0);
/*
* Send the TMS bits and TDI value.
*/
cmdByte(usb, FTDI_MPSSE_XFER_TMS_BITS);
cmdByte(usb, cmdBitcount - 1);
cmdByte(usb, (tdiFirstState << 7) | tmsBits);
rxBitcounts[rxBitcountIndex++] = cmdBitcount;
rxBytesWanted++;
nBits -= cmdBitcount;
/*
* Stash TDI bits until bit limit reached
* or TMS change of state
* or transmitter buffer capacity reached.
*/
cmdBitcount = 0;
cmdIndex = 0;
cmdBit = 0x01;
usb->cmdBuf[0] = 0;
while ((nBits != 0)
&& (((usb->tmsBuf[iIndex] & iBit) != 0) == tmsState)
&& ((usb->txCount+(cmdBitcount/8))<(usb->bulkOutRequestSize-5))){
if (usb->tdiBuf[iIndex] & iBit) {
usb->cmdBuf[cmdIndex] |= cmdBit;
}
if (cmdBit == 0x80) {
cmdBit = 0x01;
cmdIndex++;
usb->cmdBuf[cmdIndex] = 0;
}
else {
cmdBit <<= 1;
}
if (iBit == 0x80) {
iBit = 0x01;
iIndex++;
}
else {
iBit <<= 1;
}
cmdBitcount++;
nBits--;
}
/*
* Send stashed TDI bits
*/
if (cmdBitcount > 0) {
int cmdBytes = cmdBitcount / 8;
rxBitcounts[rxBitcountIndex++] = cmdBitcount;
if (cmdBitcount >= 8) {
int i;
rxBytesWanted += cmdBytes;
cmdBitcount -= cmdBytes * 8;
i = cmdBytes - 1;
cmdByte(usb, FTDI_MPSSE_XFER_TDI_BYTES);
cmdByte(usb, i);
cmdByte(usb, i >> 8);
for (i = 0 ; i < cmdBytes ; i++) {
cmdByte(usb, usb->cmdBuf[i]);
}
}
if (cmdBitcount) {
rxBytesWanted++;
cmdByte(usb, FTDI_MPSSE_XFER_TDI_BITS);
cmdByte(usb, cmdBitcount - 1);
cmdByte(usb, usb->cmdBuf[cmdBytes]);
}
}
} while ((nBits != 0)
&& ((usb->txCount+(cmdBitcount/8))<(usb->bulkOutRequestSize-6)));
/*
* Shift
*/
if (!usbWriteData(usb, usb->ioBuf, usb->txCount)
|| !usbReadData(usb, usb->rxBuf, rxBytesWanted)) {
return 0;
}
/*
* Process received data
*/
rxIndex = 0;
for (int i = 0 ; i < rxBitcountIndex ; i++) {
int rxBitcount = rxBitcounts[i];
if (rxBitcount < 8) {
rxBit = 0x1 << (8 - rxBitcount);
}
else {
rxBit = 0x01;
}
while (rxBitcount--) {
if (tdoBit == 0x1) {
usb->tdoBuf[tdoIndex] = 0;
}
if (usb->rxBuf[rxIndex] & rxBit) {
usb->tdoBuf[tdoIndex] |= tdoBit;
}
if (rxBit == 0x80) {
if (rxBitcount < 8) {
rxBit = 0x1 << (8 - rxBitcount);
}
else {
rxBit = 0x01;
}
rxIndex++;
}
else {
rxBit <<= 1;
}
if (tdoBit == 0x80) {
tdoBit = 0x01;
tdoIndex++;
}
else {
tdoBit <<= 1;
}
}
}
if (rxIndex != rxBytesWanted) {
printf("Warning -- consumed %d but supplied %d\n", rxIndex,
rxBytesWanted);
}
}
return 1;
}
/*
* Shift a client packet set of bits
*/
static int
shift(usbInfo *usb, FILE *fp)
{
uint32_t nBits, nBytes;
if (!fetch32(fp, &nBits)) {
return 0;
}
if (nBits > (unsigned int)usb->largestShiftRequest) {
usb->largestShiftRequest = nBits;
}
usb->bitCount += nBits;
usb->shiftCount++;
nBytes = (nBits + 7) / 8;
if (usb->showXVC) {
printf("shift:%d\n", (int)nBits);
}
if (nBytes > XVC_BUFSIZE) {
fprintf(stderr, "Client requested %u, max is %u\n", nBytes,XVC_BUFSIZE);
exit(1);
}
if ((fread(usb->tmsBuf, 1, nBytes, fp) != nBytes)
|| (fread(usb->tdiBuf, 1, nBytes, fp) != nBytes)) {
return 0;
}
if (usb->showXVC) {
showBuf("TMS", usb->tmsBuf, nBytes);
showBuf("TDI", usb->tdiBuf, nBytes);
}
if (!shiftChunks(usb, nBits)) {
return 0;
}
if (usb->showXVC) {
showBuf("TDO", usb->tdoBuf, nBytes);
}
if (usb->loopback) {
if (memcmp(usb->tdiBuf, usb->tdoBuf, nBytes)) {
printf("Loopback failed.\n");
}
}
return nBytes;
}
/*
* Fetch a known string
*/
static int
matchInput(FILE *fp, const char *str)
{
while (*str) {
int c = fgetc(fp);
if (c == EOF) {
badEOF();
return 0;
}
if (c != *str) {
fprintf(stderr, "Expected 0x%2x, got 0x%2x\n", *str, c);
return 0;
}
str++;
}
return 1;
}
static int
reply(int fd, const unsigned char *buf, int len)
{
if (write(fd, buf, len) != len) {
fprintf(stderr, "reply failed: %s\n", strerror(errno));
return 0;
}
return 1;
}
/*
* Return a 32 bit value
*/
static int
reply32(int fd, uint32_t value)
{
int i;
unsigned char cbuf[4];
for (i = 0 ; i < 4 ; i++) {
cbuf[i] = value;
value >>= 8;
}
return reply(fd, cbuf, 4);
}
/*
* Read and process commands
*/
static void
processCommands(FILE *fp, int fd, usbInfo *usb)
{
int c;
for (;;) {
switch(c = fgetc(fp)) {
case 's':
switch(c = fgetc(fp)) {
case 'e':
{
uint32_t num;
int frequency;
if (!matchInput(fp, "ttck:")) return;
if (!fetch32(fp, &num)) return;
frequency = 1000000000 / num;
if (usb->showXVC) {
printf("settck:%d (%d Hz)\n", (int)num, frequency);
}
if (!ftdiSetClockSpeed(usb, frequency)) return;
if (!reply32(fd, num)) return;
}
break;
case 'h':
{
int nBytes;
if (!matchInput(fp, "ift:")) return;
nBytes = shift(usb, fp);
if ((nBytes <= 0) || !reply(fd, usb->tdoBuf, nBytes)) {
return;
}
}
break;
default:
if (usb->showXVC) {
printf("Bad second char 0x%02x\n", c);
}
badChar();
return;
}
break;
case 'g':
if (matchInput(fp, "etinfo:")) {
char cBuf[40];
int len;;
if (usb->showXVC) {
printf("getinfo:\n");
}
len = sprintf(cBuf, "xvcServer_v1.0:%u\n", XVC_BUFSIZE);
if (reply(fd, (unsigned char *)cBuf, len)) {
break;
}
}
return;
case EOF:
return;
default:
if (usb->showXVC) {
printf("Bad initial char 0x%02x\n", c);
}
badChar();
return;
}
}
}
static int
createSocket(const char *interface, int port)
{
int s, o;
struct sockaddr_in myAddr;
s = socket (AF_INET, SOCK_STREAM, 0);
if (s < 0) {
return -1;
}
o = 1;
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &o, sizeof o) < 0) {
return -1;
}
memset (&myAddr, '\0', sizeof myAddr);
myAddr.sin_family = AF_INET;
if (inet_pton(AF_INET, interface, &myAddr.sin_addr) != 1) {
fprintf(stderr, "Bad address \"%s\"\n", interface);
return -1;
}
myAddr.sin_port = htons(port);
if (bind (s, (struct sockaddr *)&myAddr, sizeof myAddr) < 0) {
fprintf(stderr, "Bind() failed: %s\n", strerror (errno));
return -1;
}
if (listen (s, 1) < 0) {
fprintf(stderr, "Listen() failed: %s\n", strerror (errno));
return -1;
}
return s;
}
/************************************* Application ***************************/
static int
connectUSB(usbInfo *usb)
{
libusb_device **list;
ssize_t n;
int s;
n = libusb_get_device_list(usb->usb, &list);
if (n < 0) {
fprintf(stderr, "libusb_get_device_list failed: %s", libusb_strerror((int)n));
return 0;
}
s = findDevice(usb, list, n);
libusb_free_device_list(list, 1);
if (s) {
s = libusb_kernel_driver_active(usb->handle, usb->bInterfaceNumber);
if (s < 0) {
fprintf(stderr, "libusb_kernel_driver_active() failed: %s\n", libusb_strerror(s));
}
else if (s) {
s = libusb_detach_kernel_driver(usb->handle, usb->bInterfaceNumber);
if (s) {
fprintf(stderr, "libusb_detach_kernel_driver() failed: %s\n", libusb_strerror(s));
}
}
s = libusb_claim_interface(usb->handle, usb->bInterfaceNumber);
if (s) {
libusb_close(usb->handle);
fprintf(stderr, "libusb_claim_interface failed: %s\n", libusb_strerror(s));
return 0;