]>
cvs.zerfleddert.de Git - usb-driver/blob - jtagkey.c
3 #include "usb-driver.h"
8 static struct ftdi_context ftdic
;
10 int jtagkey_init(unsigned short vid
, unsigned short pid
) {
14 if ((ret
= ftdi_init(&ftdic
)) != 0) {
15 fprintf(stderr
, "unable to initialise libftdi: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
19 if ((ret
= ftdi_usb_open(&ftdic
, vid
, pid
)) != 0) {
20 fprintf(stderr
, "unable to open ftdi device: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
24 if ((ret
= ftdi_usb_reset(&ftdic
)) != 0) {
25 fprintf(stderr
, "unable reset device: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
29 if ((ret
= ftdi_set_interface(&ftdic
, INTERFACE_A
)) != 0) {
30 fprintf(stderr
, "unable to set interface: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
35 if ((ret
= ftdi_write_data_set_chunksize(&ftdic
, 1)) != 0) {
36 fprintf(stderr
, "unable to set write chunksize: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
41 if ((ret
= ftdi_set_latency_timer(&ftdic
, 1)) != 0) {
42 fprintf(stderr
, "unable to set latency timer: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
46 if ((ret
= ftdi_set_baudrate(&ftdic
, 500000)) != 0) {
47 fprintf(stderr
, "unable to set baudrate: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
52 ftdi_write_data(&ftdic
, &c
, 1);
54 if ((ret
= ftdi_set_bitmode(&ftdic
, JTAGKEY_TCK
|JTAGKEY_TDI
|JTAGKEY_TMS
|JTAGKEY_OEn
, BITMODE_SYNCBB
)) != 0) {
55 fprintf(stderr
, "unable to enable bitbang mode: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
59 if ((ret
= ftdi_usb_purge_buffers(&ftdic
)) != 0) {
60 fprintf(stderr
, "unable to purge buffers: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
67 void jtagkey_close() {
68 ftdi_disable_bitbang(&ftdic
);
69 ftdi_usb_close(&ftdic
);
73 void jtagkey_state(unsigned char data
) {
74 fprintf(stderr
,"Pins high: ");
76 if (data
& JTAGKEY_TCK
)
77 fprintf(stderr
,"TCK ");
79 if (data
& JTAGKEY_TDI
)
80 fprintf(stderr
,"TDI ");
82 if (data
& JTAGKEY_TDO
)
83 fprintf(stderr
,"TDO ");
85 if (data
& JTAGKEY_TMS
)
86 fprintf(stderr
,"TMS ");
88 if (data
& JTAGKEY_VREF
)
89 fprintf(stderr
,"VREF ");
94 int jtagkey_transfer(WD_TRANSFER
*tr
, int fd
, unsigned int request
, int ppbase
, int ecpbase
, int num
) {
100 static unsigned char last_data
= 0;
101 static unsigned char writebuf
[USBBUFSIZE
], *writepos
= writebuf
;
102 static unsigned char readbuf
[USBBUFSIZE
], *readpos
;
104 static unsigned char last_write
= 0x00;
107 for (i
= 0; i
< num
; i
++)
108 if (tr
[i
].cmdTrans
== 10)
111 /* Write combining */
112 if ((writepos
-writebuf
> sizeof(writebuf
)-num
) || (nread
&& writepos
-writebuf
)) {
113 DPRINTF("writing %d bytes due to %d following reads in %d chunks or full buffer\n", writepos
-writebuf
, nread
, num
);
115 ftdi_write_data(&ftdic
, writebuf
, writepos
-writebuf
);
118 while (i
< writepos
-writebuf
) {
119 i
+= ftdi_read_data(&ftdic
, readbuf
, sizeof(readbuf
));
121 DPRINTF("read %d/%d bytes\n", i
, writepos
-writebuf
);
125 for (i
= 0; i
< num
; i
++) {
126 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
127 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
128 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
130 port
= (unsigned long)tr
[i
].dwPort
;
131 val
= tr
[i
].Data
.Byte
;
134 if (tr
[i
].cmdTrans
== 13)
135 DPRINTF("write byte: %d\n", val
);
138 /* Pad writebuf for read-commands in stream */
139 *writepos
= last_data
;
141 if (port
== ppbase
+ PP_DATA
) {
142 DPRINTF("data port\n");
145 switch(tr
[i
].cmdTrans
) {
147 ret
= 0; /* We don't support reading of the data port */
189 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
[i
].cmdTrans
);
199 DPRINTF("writing %d bytes\n", writepos
-writebuf
);
200 ftdi_write_data(&ftdic
, writebuf
, writepos
-writebuf
);
201 DPRINTF("reading %d bytes\n", writepos
-writebuf
);
204 while (i
< writepos
-writebuf
) {
205 i
+= ftdi_read_data(&ftdic
, readbuf
, sizeof(readbuf
));
207 DPRINTF("read %d bytes\n", i
);
211 hexdump(writebuf
, writepos
-writebuf
);
224 for (i
= 0; i
< num
; i
++) {
225 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
226 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
227 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
229 port
= (unsigned long)tr
[i
].dwPort
;
230 val
= tr
[i
].Data
.Byte
;
233 if (port
== ppbase
+ PP_DATA
) {
234 if (tr
[i
].cmdTrans
== PP_WRITE
) {
237 } else if (port
== ppbase
+ PP_STATUS
) {
238 DPRINTF("status port (last write: 0x%x)\n", last_write
);
239 switch(tr
[i
].cmdTrans
) {
243 DPRINTF("READ: 0x%x\n", data
);
248 if ((data
& JTAGKEY_TDO
) && (last_write
& PP_PROG
))
251 if (!(last_write
& PP_PROG
))
254 if (last_write
& 0x40)
261 ret
= 0; /* Status Port is readonly */
265 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
[i
].cmdTrans
);
273 tr
[i
].Data
.Byte
= val
;
275 DPRINTF("dwPortReturn: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
276 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
277 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
279 if (tr
[i
].cmdTrans
== 10)
280 DPRINTF("read byte: %d\n", tr
[i
].Data
.Byte
);