]>
git.zerfleddert.de Git - usb-driver/blob - jtagkey.c
e17afc8895a71aa55da0bd7bb6bcd5c091a848f0
5 #include "usb-driver.h"
9 #define USBBUFSIZE 1048576
10 #define SLOW_AND_SAFE 1
12 static struct ftdi_context ftdic
;
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
));
39 if ((ret
= ftdi_write_data_set_chunksize(&ftdic
, USBBUFSIZE
)) != 0) {
40 fprintf(stderr
, "unable to set write chunksize: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
44 if ((ret
= ftdi_read_data_set_chunksize(&ftdic
, USBBUFSIZE
)) != 0) {
45 fprintf(stderr
, "unable to set read 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
);
97 static int jtagkey_set_bbmode(unsigned char mode
) {
100 if (bitbang_mode
!= mode
) {
101 DPRINTF("switching bitbang-mode!\n");
103 /* Wait for the latency-timer to kick in */
105 if ((ret
= ftdi_set_bitmode(&ftdic
, JTAGKEY_TCK
|JTAGKEY_TDI
|JTAGKEY_TMS
|JTAGKEY_OEn
, mode
)) != 0) {
106 fprintf(stderr
, "unable to enable bitbang mode: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
109 if ((ret
= ftdi_usb_purge_buffers(&ftdic
)) != 0) {
110 fprintf(stderr
, "unable to purge buffers: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
113 /* Wait for the FTDI2232 to settle */
123 void jtagkey_state(unsigned char data
) {
124 fprintf(stderr
,"Pins high: ");
126 if (data
& JTAGKEY_TCK
)
127 fprintf(stderr
,"TCK ");
129 if (data
& JTAGKEY_TDI
)
130 fprintf(stderr
,"TDI ");
132 if (data
& JTAGKEY_TDO
)
133 fprintf(stderr
,"TDO ");
135 if (data
& JTAGKEY_TMS
)
136 fprintf(stderr
,"TMS ");
138 if (data
& JTAGKEY_VREF
)
139 fprintf(stderr
,"VREF ");
141 fprintf(stderr
,"\n");
144 struct jtagkey_reader_arg
{
149 static void *jtagkey_reader(void *thread_arg
) {
150 struct jtagkey_reader_arg
*arg
= (struct jtagkey_reader_arg
*)thread_arg
;
155 DPRINTF("reader for %d bytes\n", arg
->num
);
156 while (i
< arg
->num
) {
157 i
+= ftdi_read_data(&ftdic
, arg
->buf
+ i
, arg
->num
- i
);
163 int jtagkey_transfer(WD_TRANSFER
*tr
, int fd
, unsigned int request
, int ppbase
, int ecpbase
, int num
) {
169 static unsigned char last_data
= 0;
170 static unsigned char last_write
= 0x00;
171 static unsigned char writebuf
[USBBUFSIZE
], *writepos
= writebuf
;
172 static unsigned char readbuf
[USBBUFSIZE
], *readpos
;
173 unsigned char data
, prev_data
;
174 struct jtagkey_reader_arg targ
;
175 pthread_t reader_thread
;
178 for (i
= 0; i
< num
; i
++)
179 if (tr
[i
].cmdTrans
== PP_READ
)
182 /* Write combining */
183 if ((writepos
-writebuf
> sizeof(writebuf
)-num
) || (nread
&& writepos
-writebuf
)) {
184 unsigned char *pos
= writebuf
;
187 DPRINTF("writing %d bytes due to %d following reads in %d chunks or full buffer\n", writepos
-writebuf
, nread
, num
);
189 #ifndef SLOW_AND_SAFE
190 jtagkey_set_bbmode(BITMODE_BITBANG
);
193 targ
.num
= writepos
-pos
;
195 pthread_create(&reader_thread
, NULL
, &jtagkey_reader
, &targ
);
197 while (pos
< writepos
) {
200 if (len
> USBBUFSIZE
)
203 DPRINTF("combined write of %d/%d\n",len
,writepos
-pos
);
204 ftdi_write_data(&ftdic
, pos
, len
);
208 pthread_join(reader_thread
, NULL
);
214 for (i
= 0; i
< num
; i
++) {
215 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
216 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
217 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
219 port
= (unsigned long)tr
[i
].dwPort
;
220 val
= tr
[i
].Data
.Byte
;
223 if (tr
[i
].cmdTrans
== 13)
224 DPRINTF("write byte: %d\n", val
);
227 /* Pad writebuf for read-commands in stream */
228 *writepos
= last_data
;
229 prev_data
= last_data
;
231 if (port
== ppbase
+ PP_DATA
) {
232 DPRINTF("data port\n");
235 switch(tr
[i
].cmdTrans
) {
237 ret
= 0; /* We don't support reading of the data port */
279 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
[i
].cmdTrans
);
285 if (nread
|| (*writepos
!= prev_data
) || (i
== num
-1))
291 DPRINTF("writing %d bytes\n", writepos
-writebuf
);
293 *writepos
= last_data
;
296 #ifndef SLOW_AND_SAFE
297 jtagkey_set_bbmode(BITMODE_SYNCBB
);
299 targ
.num
= writepos
-writebuf
;
301 pthread_create(&reader_thread
, NULL
, &jtagkey_reader
, &targ
);
302 ftdi_write_data(&ftdic
, writebuf
, writepos
-writebuf
);
303 pthread_join(reader_thread
, NULL
);
307 hexdump(writebuf
, writepos
-writebuf
);
319 for (i
= 0; i
< num
; i
++) {
320 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
321 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
322 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
324 port
= (unsigned long)tr
[i
].dwPort
;
325 val
= tr
[i
].Data
.Byte
;
328 if (port
== ppbase
+ PP_DATA
) {
329 if (tr
[i
].cmdTrans
== PP_WRITE
) {
332 } else if (port
== ppbase
+ PP_STATUS
) {
333 DPRINTF("status port (last write: 0x%x)\n", last_write
);
334 switch(tr
[i
].cmdTrans
) {
339 DPRINTF("READ: 0x%x\n", data
);
344 if ((data
& JTAGKEY_TDO
) && (last_write
& PP_PROG
))
347 if (!(last_write
& PP_PROG
))
350 if (last_write
& 0x40)
357 ret
= 0; /* Status Port is readonly */
361 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
[i
].cmdTrans
);
369 tr
[i
].Data
.Byte
= val
;
371 DPRINTF("dwPortReturn: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
372 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
373 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
375 if (tr
[i
].cmdTrans
== 10)
376 DPRINTF("read byte: %d\n", tr
[i
].Data
.Byte
);