]>
git.zerfleddert.de Git - usb-driver/blob - jtagkey.c
dec0b8127e59d817768893034c84f9ff568b0f3c
4 #include "usb-driver.h"
8 #define USBBUFSIZE 4096
10 static struct ftdi_context ftdic
;
11 static unsigned int usb_maxlen
= 0;
12 static unsigned char bitbang_mode
;
14 static int jtagkey_init(unsigned short vid
, unsigned short pid
) {
18 if ((ret
= ftdi_init(&ftdic
)) != 0) {
19 fprintf(stderr
, "unable to initialise libftdi: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
23 if ((ret
= ftdi_usb_open(&ftdic
, vid
, pid
)) != 0) {
24 fprintf(stderr
, "unable to open ftdi device: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
28 if ((ret
= ftdi_usb_reset(&ftdic
)) != 0) {
29 fprintf(stderr
, "unable reset device: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
33 if ((ret
= ftdi_set_interface(&ftdic
, INTERFACE_A
)) != 0) {
34 fprintf(stderr
, "unable to set interface: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
39 if ((ret
= ftdi_write_data_get_chunksize(&ftdic
, &usb_maxlen
)) != 0) {
40 fprintf(stderr
, "unable to get write chunksize: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
44 if ((ret
= ftdi_set_latency_timer(&ftdic
, 1)) != 0) {
45 fprintf(stderr
, "unable to set latency timer: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
49 if ((ret
= ftdi_set_baudrate(&ftdic
, 500000)) != 0) {
50 fprintf(stderr
, "unable to set baudrate: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
55 ftdi_write_data(&ftdic
, &c
, 1);
57 if ((ret
= ftdi_set_bitmode(&ftdic
, JTAGKEY_TCK
|JTAGKEY_TDI
|JTAGKEY_TMS
|JTAGKEY_OEn
, BITMODE_BITBANG
)) != 0) {
58 fprintf(stderr
, "unable to enable bitbang mode: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
62 bitbang_mode
= BITMODE_BITBANG
;
64 if ((ret
= ftdi_usb_purge_buffers(&ftdic
)) != 0) {
65 fprintf(stderr
, "unable to purge buffers: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
72 int jtagkey_open(int num
) {
75 ret
= jtagkey_init(config_usb_vid(num
), config_usb_pid(num
));
83 void jtagkey_close(int handle
) {
85 ftdi_disable_bitbang(&ftdic
);
86 ftdi_usb_close(&ftdic
);
91 static int jtagkey_set_bbmode(unsigned char mode
) {
94 if (bitbang_mode
!= mode
) {
95 DPRINTF("switching bitbang-mode!\n");
97 /* Wait for the latency-timer to kick in */
99 if ((ret
= ftdi_set_bitmode(&ftdic
, JTAGKEY_TCK
|JTAGKEY_TDI
|JTAGKEY_TMS
|JTAGKEY_OEn
, mode
)) != 0) {
100 fprintf(stderr
, "unable to enable bitbang mode: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
103 if ((ret
= ftdi_usb_purge_buffers(&ftdic
)) != 0) {
104 fprintf(stderr
, "unable to purge buffers: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
107 /* Wait for the FTDI2232 to settle */
116 void jtagkey_state(unsigned char data
) {
117 fprintf(stderr
,"Pins high: ");
119 if (data
& JTAGKEY_TCK
)
120 fprintf(stderr
,"TCK ");
122 if (data
& JTAGKEY_TDI
)
123 fprintf(stderr
,"TDI ");
125 if (data
& JTAGKEY_TDO
)
126 fprintf(stderr
,"TDO ");
128 if (data
& JTAGKEY_TMS
)
129 fprintf(stderr
,"TMS ");
131 if (data
& JTAGKEY_VREF
)
132 fprintf(stderr
,"VREF ");
134 fprintf(stderr
,"\n");
137 int jtagkey_transfer(WD_TRANSFER
*tr
, int fd
, unsigned int request
, int ppbase
, int ecpbase
, int num
) {
143 static unsigned char last_data
= 0;
144 static unsigned char last_write
= 0x00;
145 static unsigned char writebuf
[USBBUFSIZE
], *writepos
= writebuf
;
146 static unsigned char readbuf
[USBBUFSIZE
], *readpos
;
147 unsigned char data
, prev_data
;
150 for (i
= 0; i
< num
; i
++)
151 if (tr
[i
].cmdTrans
== PP_READ
)
154 /* Write combining */
155 if ((writepos
-writebuf
> sizeof(writebuf
)-num
) || (nread
&& writepos
-writebuf
)) {
156 unsigned char *pos
= writebuf
;
158 DPRINTF("writing %d bytes due to %d following reads in %d chunks or full buffer\n", writepos
-writebuf
, nread
, num
);
160 jtagkey_set_bbmode(BITMODE_BITBANG
);
161 while (pos
< writepos
) {
164 if (len
> usb_maxlen
)
167 DPRINTF("combined write of %d/%d\n",len
,writepos
-pos
);
168 ftdi_write_data(&ftdic
, pos
, len
);
175 for (i
= 0; i
< num
; i
++) {
176 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
177 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
178 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
180 port
= (unsigned long)tr
[i
].dwPort
;
181 val
= tr
[i
].Data
.Byte
;
184 if (tr
[i
].cmdTrans
== 13)
185 DPRINTF("write byte: %d\n", val
);
188 /* Pad writebuf for read-commands in stream */
189 *writepos
= last_data
;
190 prev_data
= last_data
;
192 if (port
== ppbase
+ PP_DATA
) {
193 DPRINTF("data port\n");
196 switch(tr
[i
].cmdTrans
) {
198 ret
= 0; /* We don't support reading of the data port */
240 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
[i
].cmdTrans
);
246 if (nread
|| (*writepos
!= prev_data
))
252 DPRINTF("writing %d bytes\n", writepos
-writebuf
);
254 *writepos
= last_data
;
257 jtagkey_set_bbmode(BITMODE_SYNCBB
);
258 ftdi_write_data(&ftdic
, writebuf
, writepos
-writebuf
);
261 while (i
< writepos
-writebuf
) {
262 i
+= ftdi_read_data(&ftdic
, readbuf
, sizeof(readbuf
));
267 hexdump(writebuf
, writepos
-writebuf
);
279 for (i
= 0; i
< num
; i
++) {
280 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
281 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
282 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
284 port
= (unsigned long)tr
[i
].dwPort
;
285 val
= tr
[i
].Data
.Byte
;
288 if (port
== ppbase
+ PP_DATA
) {
289 if (tr
[i
].cmdTrans
== PP_WRITE
) {
292 } else if (port
== ppbase
+ PP_STATUS
) {
293 DPRINTF("status port (last write: 0x%x)\n", last_write
);
294 switch(tr
[i
].cmdTrans
) {
299 DPRINTF("READ: 0x%x\n", data
);
304 if ((data
& JTAGKEY_TDO
) && (last_write
& PP_PROG
))
307 if (!(last_write
& PP_PROG
))
310 if (last_write
& 0x40)
317 ret
= 0; /* Status Port is readonly */
321 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
[i
].cmdTrans
);
329 tr
[i
].Data
.Byte
= val
;
331 DPRINTF("dwPortReturn: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
332 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
333 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
335 if (tr
[i
].cmdTrans
== 10)
336 DPRINTF("read byte: %d\n", tr
[i
].Data
.Byte
);