]>
cvs.zerfleddert.de Git - usb-driver/blob - jtagkey.c
4 #include "usb-driver.h"
7 #define USBBUFSIZE 4096
9 static struct ftdi_context ftdic
;
10 static unsigned int usb_maxlen
= 0;
11 static unsigned char bitbang_mode
;
13 int jtagkey_init(unsigned short vid
, unsigned short pid
) {
17 if ((ret
= ftdi_init(&ftdic
)) != 0) {
18 fprintf(stderr
, "unable to initialise libftdi: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
22 if ((ret
= ftdi_usb_open(&ftdic
, vid
, pid
)) != 0) {
23 fprintf(stderr
, "unable to open ftdi device: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
27 if ((ret
= ftdi_usb_reset(&ftdic
)) != 0) {
28 fprintf(stderr
, "unable reset device: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
32 if ((ret
= ftdi_set_interface(&ftdic
, INTERFACE_A
)) != 0) {
33 fprintf(stderr
, "unable to set interface: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
38 if ((ret
= ftdi_write_data_get_chunksize(&ftdic
, &usb_maxlen
)) != 0) {
39 fprintf(stderr
, "unable to get write chunksize: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
43 if ((ret
= ftdi_set_latency_timer(&ftdic
, 1)) != 0) {
44 fprintf(stderr
, "unable to set latency timer: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
48 if ((ret
= ftdi_set_baudrate(&ftdic
, 500000)) != 0) {
49 fprintf(stderr
, "unable to set baudrate: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
54 ftdi_write_data(&ftdic
, &c
, 1);
56 if ((ret
= ftdi_set_bitmode(&ftdic
, JTAGKEY_TCK
|JTAGKEY_TDI
|JTAGKEY_TMS
|JTAGKEY_OEn
, BITMODE_BITBANG
)) != 0) {
57 fprintf(stderr
, "unable to enable bitbang mode: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
61 bitbang_mode
= BITMODE_BITBANG
;
63 if ((ret
= ftdi_usb_purge_buffers(&ftdic
)) != 0) {
64 fprintf(stderr
, "unable to purge buffers: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
71 void jtagkey_close() {
72 ftdi_disable_bitbang(&ftdic
);
73 ftdi_usb_close(&ftdic
);
77 static int jtagkey_set_bbmode(unsigned char mode
) {
80 if (bitbang_mode
!= mode
) {
81 DPRINTF("switching bitbang-mode!\n");
83 /* Wait for the latency-timer to kick in */
85 if ((ret
= ftdi_set_bitmode(&ftdic
, JTAGKEY_TCK
|JTAGKEY_TDI
|JTAGKEY_TMS
|JTAGKEY_OEn
, mode
)) != 0) {
86 fprintf(stderr
, "unable to enable bitbang mode: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
89 if ((ret
= ftdi_usb_purge_buffers(&ftdic
)) != 0) {
90 fprintf(stderr
, "unable to purge buffers: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
93 /* Wait for the FTDI2232 to settle */
102 void jtagkey_state(unsigned char data
) {
103 fprintf(stderr
,"Pins high: ");
105 if (data
& JTAGKEY_TCK
)
106 fprintf(stderr
,"TCK ");
108 if (data
& JTAGKEY_TDI
)
109 fprintf(stderr
,"TDI ");
111 if (data
& JTAGKEY_TDO
)
112 fprintf(stderr
,"TDO ");
114 if (data
& JTAGKEY_TMS
)
115 fprintf(stderr
,"TMS ");
117 if (data
& JTAGKEY_VREF
)
118 fprintf(stderr
,"VREF ");
120 fprintf(stderr
,"\n");
123 int jtagkey_transfer(WD_TRANSFER
*tr
, int fd
, unsigned int request
, int ppbase
, int ecpbase
, int num
) {
129 static unsigned char last_data
= 0;
130 static unsigned char last_write
= 0x00;
131 static unsigned char writebuf
[USBBUFSIZE
], *writepos
= writebuf
;
132 static unsigned char readbuf
[USBBUFSIZE
], *readpos
;
133 unsigned char data
, prev_data
;
136 for (i
= 0; i
< num
; i
++)
137 if (tr
[i
].cmdTrans
== PP_READ
)
140 /* Write combining */
141 if ((writepos
-writebuf
> sizeof(writebuf
)-num
) || (nread
&& writepos
-writebuf
)) {
142 unsigned char *pos
= writebuf
;
144 DPRINTF("writing %d bytes due to %d following reads in %d chunks or full buffer\n", writepos
-writebuf
, nread
, num
);
146 jtagkey_set_bbmode(BITMODE_BITBANG
);
147 while (pos
< writepos
) {
150 if (len
> usb_maxlen
)
153 DPRINTF("combined write of %d/%d\n",len
,writepos
-pos
);
154 ftdi_write_data(&ftdic
, pos
, len
);
158 DPRINTF("read %d/%d bytes\n", i
, writepos
-writebuf
);
162 for (i
= 0; i
< num
; i
++) {
163 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
164 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
165 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
167 port
= (unsigned long)tr
[i
].dwPort
;
168 val
= tr
[i
].Data
.Byte
;
171 if (tr
[i
].cmdTrans
== 13)
172 DPRINTF("write byte: %d\n", val
);
175 /* Pad writebuf for read-commands in stream */
176 *writepos
= last_data
;
177 prev_data
= last_data
;
179 if (port
== ppbase
+ PP_DATA
) {
180 DPRINTF("data port\n");
183 switch(tr
[i
].cmdTrans
) {
185 ret
= 0; /* We don't support reading of the data port */
227 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
[i
].cmdTrans
);
233 if (nread
|| (*writepos
!= prev_data
))
239 DPRINTF("writing %d bytes\n", writepos
-writebuf
);
241 *writepos
= last_data
;
244 jtagkey_set_bbmode(BITMODE_SYNCBB
);
245 ftdi_write_data(&ftdic
, writebuf
, writepos
-writebuf
);
248 while (i
< writepos
-writebuf
) {
249 i
+= ftdi_read_data(&ftdic
, readbuf
, sizeof(readbuf
));
254 hexdump(writebuf
, writepos
-writebuf
);
266 for (i
= 0; i
< num
; i
++) {
267 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
268 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
269 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
271 port
= (unsigned long)tr
[i
].dwPort
;
272 val
= tr
[i
].Data
.Byte
;
275 if (port
== ppbase
+ PP_DATA
) {
276 if (tr
[i
].cmdTrans
== PP_WRITE
) {
279 } else if (port
== ppbase
+ PP_STATUS
) {
280 DPRINTF("status port (last write: 0x%x)\n", last_write
);
281 switch(tr
[i
].cmdTrans
) {
286 DPRINTF("READ: 0x%x\n", data
);
291 if ((data
& JTAGKEY_TDO
) && (last_write
& PP_PROG
))
294 if (!(last_write
& PP_PROG
))
297 if (last_write
& 0x40)
304 ret
= 0; /* Status Port is readonly */
308 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
[i
].cmdTrans
);
316 tr
[i
].Data
.Byte
= val
;
318 DPRINTF("dwPortReturn: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
319 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
320 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
322 if (tr
[i
].cmdTrans
== 10)
323 DPRINTF("read byte: %d\n", tr
[i
].Data
.Byte
);