]>
git.zerfleddert.de Git - usb-driver/blob - jtagkey.c
fc762d368d211604cb0cb53ec6cdd2fca5ac775a
5 #include "usb-driver.h"
9 #define USBBUFSIZE 1048576
10 #define JTAG_SPEED 100000
11 #define SLOW_AND_SAFE 1
13 static struct ftdi_context ftdic
;
14 static unsigned char bitbang_mode
;
16 static int jtagkey_init(unsigned short vid
, unsigned short pid
) {
20 if ((ret
= ftdi_init(&ftdic
)) != 0) {
21 fprintf(stderr
, "unable to initialise libftdi: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
25 if ((ret
= ftdi_usb_open(&ftdic
, vid
, pid
)) != 0) {
26 fprintf(stderr
, "unable to open ftdi device: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
30 if ((ret
= ftdi_usb_reset(&ftdic
)) != 0) {
31 fprintf(stderr
, "unable reset device: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
35 if ((ret
= ftdi_set_interface(&ftdic
, INTERFACE_A
)) != 0) {
36 fprintf(stderr
, "unable to set interface: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
40 if ((ret
= ftdi_write_data_set_chunksize(&ftdic
, USBBUFSIZE
)) != 0) {
41 fprintf(stderr
, "unable to set write chunksize: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
45 if ((ret
= ftdi_read_data_set_chunksize(&ftdic
, USBBUFSIZE
)) != 0) {
46 fprintf(stderr
, "unable to set read chunksize: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
50 if ((ret
= ftdi_set_latency_timer(&ftdic
, 1)) != 0) {
51 fprintf(stderr
, "unable to set latency timer: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
56 ftdi_write_data(&ftdic
, &c
, 1);
58 if ((ret
= ftdi_set_bitmode(&ftdic
, JTAGKEY_TCK
|JTAGKEY_TDI
|JTAGKEY_TMS
|JTAGKEY_OEn
, BITMODE_SYNCBB
)) != 0) {
59 fprintf(stderr
, "unable to enable bitbang mode: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
63 if ((ret
= ftdi_set_baudrate(&ftdic
, JTAG_SPEED
)) != 0) {
64 fprintf(stderr
, "unable to set baudrate: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
68 bitbang_mode
= BITMODE_SYNCBB
;
70 if ((ret
= ftdi_usb_purge_buffers(&ftdic
)) != 0) {
71 fprintf(stderr
, "unable to purge buffers: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
78 int jtagkey_open(int num
) {
81 ret
= jtagkey_init(config_usb_vid(num
), config_usb_pid(num
));
89 void jtagkey_close(int handle
) {
91 ftdi_disable_bitbang(&ftdic
);
92 ftdi_usb_close(&ftdic
);
98 static int jtagkey_set_bbmode(unsigned char mode
) {
101 if (bitbang_mode
!= mode
) {
102 DPRINTF("switching bitbang-mode!\n");
104 /* Wait for the latency-timer to kick in */
106 if ((ret
= ftdi_set_bitmode(&ftdic
, JTAGKEY_TCK
|JTAGKEY_TDI
|JTAGKEY_TMS
|JTAGKEY_OEn
, mode
)) != 0) {
107 fprintf(stderr
, "unable to enable bitbang mode: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
110 if ((ret
= ftdi_usb_purge_buffers(&ftdic
)) != 0) {
111 fprintf(stderr
, "unable to purge buffers: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
114 /* Wait for the FTDI2232 to settle */
124 void jtagkey_state(unsigned char data
) {
125 fprintf(stderr
,"Pins high: ");
127 if (data
& JTAGKEY_TCK
)
128 fprintf(stderr
,"TCK ");
130 if (data
& JTAGKEY_TDI
)
131 fprintf(stderr
,"TDI ");
133 if (data
& JTAGKEY_TDO
)
134 fprintf(stderr
,"TDO ");
136 if (data
& JTAGKEY_TMS
)
137 fprintf(stderr
,"TMS ");
139 if (data
& JTAGKEY_VREF
)
140 fprintf(stderr
,"VREF ");
142 fprintf(stderr
,"\n");
145 struct jtagkey_reader_arg
{
150 static void *jtagkey_reader(void *thread_arg
) {
151 struct jtagkey_reader_arg
*arg
= (struct jtagkey_reader_arg
*)thread_arg
;
156 DPRINTF("reader for %d bytes\n", arg
->num
);
157 while (i
< arg
->num
) {
158 i
+= ftdi_read_data(&ftdic
, arg
->buf
+ i
, arg
->num
- i
);
164 /* TODO: Interpret JTAG commands and transfer in MPSSE mode */
165 int jtagkey_transfer(WD_TRANSFER
*tr
, int fd
, unsigned int request
, int ppbase
, int ecpbase
, int num
) {
171 static unsigned char last_data
= 0;
172 static unsigned char last_write
= 0x00;
173 static unsigned char writebuf
[USBBUFSIZE
], *writepos
= writebuf
;
174 static unsigned char readbuf
[USBBUFSIZE
], *readpos
;
175 unsigned char data
, prev_data
, last_cyc_write
;
176 struct jtagkey_reader_arg targ
;
177 pthread_t reader_thread
;
180 for (i
= 0; i
< num
; i
++)
181 if (tr
[i
].cmdTrans
== PP_READ
)
184 /* Write combining */
185 if ((writepos
-writebuf
> sizeof(writebuf
)-num
) || (nread
&& writepos
-writebuf
)) {
186 unsigned char *pos
= writebuf
;
189 DPRINTF("writing %d bytes due to %d following reads in %d chunks or full buffer\n", writepos
-writebuf
, nread
, num
);
191 #ifndef SLOW_AND_SAFE
192 jtagkey_set_bbmode(BITMODE_BITBANG
);
195 targ
.num
= writepos
-pos
;
197 pthread_create(&reader_thread
, NULL
, &jtagkey_reader
, &targ
);
199 while (pos
< writepos
) {
202 if (len
> USBBUFSIZE
)
205 DPRINTF("combined write of %d/%d\n",len
,writepos
-pos
);
206 ftdi_write_data(&ftdic
, pos
, len
);
210 pthread_join(reader_thread
, NULL
);
216 last_cyc_write
= last_write
;
218 for (i
= 0; i
< num
; i
++) {
219 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
220 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
221 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
223 port
= (unsigned long)tr
[i
].dwPort
;
224 val
= tr
[i
].Data
.Byte
;
227 if (tr
[i
].cmdTrans
== 13)
228 DPRINTF("write byte: %d\n", val
);
231 /* Pad writebuf for read-commands in stream */
232 *writepos
= last_data
;
233 prev_data
= last_data
;
235 if (port
== ppbase
+ PP_DATA
) {
236 DPRINTF("data port\n");
239 switch(tr
[i
].cmdTrans
) {
241 ret
= 0; /* We don't support reading of the data port */
283 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
[i
].cmdTrans
);
289 if ((tr
[i
].cmdTrans
== PP_READ
) || (*writepos
!= prev_data
) || (i
== num
-1))
295 DPRINTF("writing %d bytes\n", writepos
-writebuf
);
297 *writepos
= last_data
;
300 #ifndef SLOW_AND_SAFE
301 jtagkey_set_bbmode(BITMODE_SYNCBB
);
303 targ
.num
= writepos
-writebuf
;
305 pthread_create(&reader_thread
, NULL
, &jtagkey_reader
, &targ
);
306 ftdi_write_data(&ftdic
, writebuf
, writepos
-writebuf
);
307 pthread_join(reader_thread
, NULL
);
311 hexdump(writebuf
, writepos
-writebuf
);
322 last_write
= last_cyc_write
;
324 for (i
= 0; i
< num
; i
++) {
325 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
326 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
327 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
329 port
= (unsigned long)tr
[i
].dwPort
;
330 val
= tr
[i
].Data
.Byte
;
332 if ((tr
[i
].cmdTrans
!= PP_READ
) && (val
== last_write
) && (i
!= num
-1))
337 if (port
== ppbase
+ PP_DATA
) {
338 if (tr
[i
].cmdTrans
== PP_WRITE
) {
341 } else if (port
== ppbase
+ PP_STATUS
) {
342 DPRINTF("status port (last write: 0x%x)\n", last_write
);
343 switch(tr
[i
].cmdTrans
) {
348 DPRINTF("READ: 0x%x\n", data
);
353 if ((data
& JTAGKEY_TDO
) && (last_write
& PP_PROG
))
356 if (~last_write
& PP_PROG
)
359 if (last_write
& 0x40)
366 ret
= 0; /* Status Port is readonly */
370 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
[i
].cmdTrans
);
378 tr
[i
].Data
.Byte
= val
;
380 DPRINTF("dwPortReturn: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
381 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
382 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
384 if (tr
[i
].cmdTrans
== 10)
385 DPRINTF("read byte: %d\n", tr
[i
].Data
.Byte
);