]>
cvs.zerfleddert.de Git - usb-driver/blob - jtagkey.c
3 #include "usb-driver.h"
6 static struct ftdi_context ftdic
;
8 int jtagkey_init(unsigned short vid
, unsigned short pid
) {
11 if ((ret
= ftdi_init(&ftdic
)) != 0) {
12 fprintf(stderr
, "unable to initialise libftdi: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
16 if ((ret
= ftdi_usb_open(&ftdic
, vid
, pid
)) != 0) {
17 fprintf(stderr
, "unable to open ftdi device: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
21 if ((ret
= ftdi_usb_reset(&ftdic
)) != 0) {
22 fprintf(stderr
, "unable reset device: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
26 if ((ret
= ftdi_set_interface(&ftdic
, INTERFACE_A
)) != 0) {
27 fprintf(stderr
, "unable to set interface: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
32 if ((ret
= ftdi_write_data_set_chunksize(&ftdic
, 3)) != 0) {
33 fprintf(stderr
, "unable to set write chunksize: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
38 if ((ret
= ftdi_set_latency_timer(&ftdic
, 1)) != 0) {
39 fprintf(stderr
, "unable to set latency timer: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
43 if ((ret
= ftdi_set_baudrate(&ftdic
, 1000000)) != 0) {
44 fprintf(stderr
, "unable to set baudrate: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
48 if ((ret
= ftdi_set_bitmode(&ftdic
, JTAGKEY_TCK
|JTAGKEY_TDI
|JTAGKEY_TMS
|JTAGKEY_OEn
, BITMODE_SYNCBB
)) != 0) {
49 fprintf(stderr
, "unable to enable bitbang mode: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
53 if ((ret
= ftdi_usb_purge_buffers(&ftdic
)) != 0) {
54 fprintf(stderr
, "unable to purge buffers: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
61 void jtagkey_close() {
62 ftdi_disable_bitbang(&ftdic
);
63 ftdi_usb_close(&ftdic
);
67 void jtagkey_state(unsigned char data
) {
68 fprintf(stderr
,"Pins high: ");
70 if (data
& JTAGKEY_TCK
)
71 fprintf(stderr
,"TCK ");
73 if (data
& JTAGKEY_TDI
)
74 fprintf(stderr
,"TDI ");
76 if (data
& JTAGKEY_TDO
)
77 fprintf(stderr
,"TDO ");
79 if (data
& JTAGKEY_TMS
)
80 fprintf(stderr
,"TMS ");
82 if (data
& JTAGKEY_VREF
)
83 fprintf(stderr
,"VREF ");
88 int jtagkey_transfer(WD_TRANSFER
*tr
, int fd
, unsigned int request
, int ppbase
, int ecpbase
, int num
) {
93 static unsigned char last_write
= 0, last_data
= 0;
95 unsigned char writebuf
[4096], readbuf
[4096], *pos
;
98 for (i
= 0; i
< num
; i
++) {
99 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
100 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
101 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
103 port
= (unsigned long)tr
[i
].dwPort
;
104 val
= tr
[i
].Data
.Byte
;
107 if (tr
[i
].cmdTrans
== 13)
108 DPRINTF("write byte: %d\n", val
);
111 /* Pad writebuf for read-commands in stream */
112 if (tr
[i
].cmdTrans
== 10) {
117 if (port
== ppbase
+ PP_DATA
) {
118 DPRINTF("data port\n");
121 switch(tr
[i
].cmdTrans
) {
123 ret
= 0; /* We don't support reading of the data port */
159 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
[i
].cmdTrans
);
166 ftdi_usb_purge_buffers(&ftdic
);
167 ftdi_write_data(&ftdic
, writebuf
, pos
-writebuf
);
172 ftdi_write_data(&ftdic
, &last_data
, 1);
174 i
+= ftdi_read_data(&ftdic
, readbuf
, sizeof(readbuf
));
175 } while (i
< pos
-writebuf
);
179 hexdump(writebuf
, pos
-writebuf
);
186 for (i
= 0; i
< num
; i
++) {
187 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
188 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
189 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
191 port
= (unsigned long)tr
[i
].dwPort
;
192 val
= tr
[i
].Data
.Byte
;
195 if (port
== ppbase
+ PP_STATUS
) {
196 DPRINTF("status port (last write: %d)\n", last_write
);
197 switch(tr
[i
].cmdTrans
) {
201 DPRINTF("READ: 0x%x\n", data
);
206 if (data
& JTAGKEY_TDO
)
209 if (last_write
& 0x40)
216 ret
= 0; /* Status Port is readonly */
220 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
[i
].cmdTrans
);
228 tr
[i
].Data
.Byte
= val
;
230 DPRINTF("dwPortReturn: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
231 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
232 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
234 if (tr
[i
].cmdTrans
== 10)
235 DPRINTF("read byte: %d\n", tr
[i
].Data
.Byte
);