]>
git.zerfleddert.de Git - usb-driver/blob - jtagkey.c
41f0fd60723df3d2e472ada46b1a2cd8da1d0690
4 #include "usb-driver.h"
8 #define USBBUFSIZE 4096
9 #define SLOW_AND_SAFE 1
11 static struct ftdi_context ftdic
;
12 static unsigned int usb_maxlen
= 0;
13 static unsigned char bitbang_mode
;
15 static int jtagkey_init(unsigned short vid
, unsigned short pid
) {
19 if ((ret
= ftdi_init(&ftdic
)) != 0) {
20 fprintf(stderr
, "unable to initialise libftdi: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
24 if ((ret
= ftdi_usb_open(&ftdic
, vid
, pid
)) != 0) {
25 fprintf(stderr
, "unable to open ftdi device: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
29 if ((ret
= ftdi_usb_reset(&ftdic
)) != 0) {
30 fprintf(stderr
, "unable reset device: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
34 if ((ret
= ftdi_set_interface(&ftdic
, INTERFACE_A
)) != 0) {
35 fprintf(stderr
, "unable to set interface: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
43 if ((ret
= ftdi_write_data_get_chunksize(&ftdic
, &usb_maxlen
)) != 0) {
44 fprintf(stderr
, "unable to get write chunksize: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
49 if ((ret
= ftdi_set_latency_timer(&ftdic
, 1)) != 0) {
50 fprintf(stderr
, "unable to set latency timer: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
54 if ((ret
= ftdi_set_baudrate(&ftdic
, 500000)) != 0) {
55 fprintf(stderr
, "unable to set baudrate: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
60 ftdi_write_data(&ftdic
, &c
, 1);
62 if ((ret
= ftdi_set_bitmode(&ftdic
, JTAGKEY_TCK
|JTAGKEY_TDI
|JTAGKEY_TMS
|JTAGKEY_OEn
, BITMODE_SYNCBB
)) != 0) {
63 fprintf(stderr
, "unable to enable bitbang mode: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
67 bitbang_mode
= BITMODE_SYNCBB
;
69 if ((ret
= ftdi_usb_purge_buffers(&ftdic
)) != 0) {
70 fprintf(stderr
, "unable to purge buffers: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
77 int jtagkey_open(int num
) {
80 ret
= jtagkey_init(config_usb_vid(num
), config_usb_pid(num
));
88 void jtagkey_close(int handle
) {
90 ftdi_disable_bitbang(&ftdic
);
91 ftdi_usb_close(&ftdic
);
96 static int jtagkey_set_bbmode(unsigned char mode
) {
99 if (bitbang_mode
!= mode
) {
100 DPRINTF("switching bitbang-mode!\n");
102 /* Wait for the latency-timer to kick in */
104 if ((ret
= ftdi_set_bitmode(&ftdic
, JTAGKEY_TCK
|JTAGKEY_TDI
|JTAGKEY_TMS
|JTAGKEY_OEn
, mode
)) != 0) {
105 fprintf(stderr
, "unable to enable bitbang mode: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
108 if ((ret
= ftdi_usb_purge_buffers(&ftdic
)) != 0) {
109 fprintf(stderr
, "unable to purge buffers: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
112 /* Wait for the FTDI2232 to settle */
121 void jtagkey_state(unsigned char data
) {
122 fprintf(stderr
,"Pins high: ");
124 if (data
& JTAGKEY_TCK
)
125 fprintf(stderr
,"TCK ");
127 if (data
& JTAGKEY_TDI
)
128 fprintf(stderr
,"TDI ");
130 if (data
& JTAGKEY_TDO
)
131 fprintf(stderr
,"TDO ");
133 if (data
& JTAGKEY_TMS
)
134 fprintf(stderr
,"TMS ");
136 if (data
& JTAGKEY_VREF
)
137 fprintf(stderr
,"VREF ");
139 fprintf(stderr
,"\n");
142 int jtagkey_transfer(WD_TRANSFER
*tr
, int fd
, unsigned int request
, int ppbase
, int ecpbase
, int num
) {
148 static unsigned char last_data
= 0;
149 static unsigned char last_write
= 0x00;
150 static unsigned char writebuf
[USBBUFSIZE
], *writepos
= writebuf
;
151 static unsigned char readbuf
[USBBUFSIZE
], *readpos
;
152 unsigned char data
, prev_data
;
155 for (i
= 0; i
< num
; i
++)
156 if (tr
[i
].cmdTrans
== PP_READ
)
159 /* Write combining */
160 if ((writepos
-writebuf
> sizeof(writebuf
)-num
) || (nread
&& writepos
-writebuf
)) {
161 unsigned char *pos
= writebuf
;
163 DPRINTF("writing %d bytes due to %d following reads in %d chunks or full buffer\n", writepos
-writebuf
, nread
, num
);
165 #ifndef SLOW_AND_SAFE
166 jtagkey_set_bbmode(BITMODE_BITBANG
);
168 while (pos
< writepos
) {
171 if (len
> usb_maxlen
)
174 DPRINTF("combined write of %d/%d\n",len
,writepos
-pos
);
175 ftdi_write_data(&ftdic
, pos
, len
);
179 i
+= ftdi_read_data(&ftdic
, readbuf
, sizeof(readbuf
));
188 for (i
= 0; i
< num
; i
++) {
189 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
190 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
191 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
193 port
= (unsigned long)tr
[i
].dwPort
;
194 val
= tr
[i
].Data
.Byte
;
197 if (tr
[i
].cmdTrans
== 13)
198 DPRINTF("write byte: %d\n", val
);
201 /* Pad writebuf for read-commands in stream */
202 *writepos
= last_data
;
203 prev_data
= last_data
;
205 if (port
== ppbase
+ PP_DATA
) {
206 DPRINTF("data port\n");
209 switch(tr
[i
].cmdTrans
) {
211 ret
= 0; /* We don't support reading of the data port */
253 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
[i
].cmdTrans
);
259 if (nread
|| (*writepos
!= prev_data
))
265 DPRINTF("writing %d bytes\n", writepos
-writebuf
);
267 *writepos
= last_data
;
270 jtagkey_set_bbmode(BITMODE_SYNCBB
);
271 ftdi_write_data(&ftdic
, writebuf
, writepos
-writebuf
);
274 while (i
< writepos
-writebuf
) {
275 i
+= ftdi_read_data(&ftdic
, readbuf
, sizeof(readbuf
));
280 hexdump(writebuf
, writepos
-writebuf
);
292 for (i
= 0; i
< num
; i
++) {
293 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
294 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
295 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
297 port
= (unsigned long)tr
[i
].dwPort
;
298 val
= tr
[i
].Data
.Byte
;
301 if (port
== ppbase
+ PP_DATA
) {
302 if (tr
[i
].cmdTrans
== PP_WRITE
) {
305 } else if (port
== ppbase
+ PP_STATUS
) {
306 DPRINTF("status port (last write: 0x%x)\n", last_write
);
307 switch(tr
[i
].cmdTrans
) {
312 DPRINTF("READ: 0x%x\n", data
);
317 if ((data
& JTAGKEY_TDO
) && (last_write
& PP_PROG
))
320 if (!(last_write
& PP_PROG
))
323 if (last_write
& 0x40)
330 ret
= 0; /* Status Port is readonly */
334 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
[i
].cmdTrans
);
342 tr
[i
].Data
.Byte
= val
;
344 DPRINTF("dwPortReturn: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
345 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
346 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
348 if (tr
[i
].cmdTrans
== 10)
349 DPRINTF("read byte: %d\n", tr
[i
].Data
.Byte
);