]>
git.zerfleddert.de Git - usb-driver/blob - jtagkey.c
0078e474e064234ee3561f5a8b6ca6c5a17aa2a4
3 #include "usb-driver.h"
6 static struct ftdi_context ftdic
;
8 int jtagkey_init(unsigned short vid
, unsigned short pid
) {
11 if ((ret
= ftdi_init(&ftdic
)) != 0) {
12 fprintf(stderr
, "unable to initialise libftdi: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
16 if ((ret
= ftdi_usb_open(&ftdic
, vid
, pid
)) != 0) {
17 fprintf(stderr
, "unable to open ftdi device: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
21 if ((ret
= ftdi_usb_reset(&ftdic
)) != 0) {
22 fprintf(stderr
, "unable reset device: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
26 if ((ret
= ftdi_set_interface(&ftdic
, INTERFACE_A
)) != 0) {
27 fprintf(stderr
, "unable to set interface: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
31 if ((ret
= ftdi_write_data_set_chunksize(&ftdic
, 1)) != 0) {
32 fprintf(stderr
, "unable to set write chunksize: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
36 if ((ret
= ftdi_set_latency_timer(&ftdic
, 1)) != 0) {
37 fprintf(stderr
, "unable to set latency timer: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
41 if ((ret
= ftdi_set_baudrate(&ftdic
, 230400)) != 0) {
42 fprintf(stderr
, "unable to set baudrate: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
46 if ((ret
= ftdi_set_bitmode(&ftdic
, JTAGKEY_TCK
|JTAGKEY_TDI
|JTAGKEY_TMS
|JTAGKEY_OEn
, 1)) != 0) {
47 fprintf(stderr
, "unable to enable bitbang mode: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
51 if ((ret
= ftdi_usb_purge_buffers(&ftdic
)) != 0) {
52 fprintf(stderr
, "unable to purge buffers: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
59 void jtagkey_close() {
60 ftdi_disable_bitbang(&ftdic
);
61 ftdi_usb_close(&ftdic
);
65 void jtagkey_state(unsigned char data
) {
66 fprintf(stderr
,"Pins high: ");
68 if (data
& JTAGKEY_TCK
)
69 fprintf(stderr
,"TCK ");
71 if (data
& JTAGKEY_TDI
)
72 fprintf(stderr
,"TDI ");
74 if (data
& JTAGKEY_TDO
)
75 fprintf(stderr
,"TDO ");
77 if (data
& JTAGKEY_TMS
)
78 fprintf(stderr
,"TMS ");
80 if (data
& JTAGKEY_VREF
)
81 fprintf(stderr
,"VREF ");
86 int jtagkey_transfer(WD_TRANSFER
*tr
, int fd
, unsigned int request
, int ppbase
, int ecpbase
, int num
) {
91 static unsigned char last_write
= 0;
94 for (i
= 0; i
< num
; i
++) {
95 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
96 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
97 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
99 port
= (unsigned long)tr
[i
].dwPort
;
100 val
= tr
[i
].Data
.Byte
;
103 if (tr
[i
].cmdTrans
== 13)
104 DPRINTF("write byte: %d\n", val
);
107 if (port
== ppbase
+ PP_DATA
) {
108 DPRINTF("data port\n");
111 switch(tr
[i
].cmdTrans
) {
113 ret
= 0; /* We don't support reading of the data port */
141 ftdi_write_data(&ftdic
, &data
, 1);
145 ftdi_read_pins(&ftdic
, &tmp
);
146 } while ((tmp
& (JTAGKEY_TDI
|JTAGKEY_TMS
|JTAGKEY_TCK
|JTAGKEY_TDI
)) != data
);
153 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
[i
].cmdTrans
);
157 } else if (port
== ppbase
+ PP_STATUS
) {
158 DPRINTF("status port (last write: %d)\n", last_write
);
159 switch(tr
[i
].cmdTrans
) {
161 ftdi_read_pins(&ftdic
, &data
);
163 DPRINTF("READ: 0x%x\n", data
);
168 if (data
& JTAGKEY_TDO
)
171 if (last_write
& 0x40)
178 ret
= 0; /* Status Port is readonly */
182 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
[i
].cmdTrans
);
186 } else if (port
== ppbase
+ PP_CONTROL
) {
187 DPRINTF("control port\n");
188 } else if ((port
== ecpbase
+ PP_ECP_CFGA
) && ecpbase
) {
189 DPRINTF("ECP_CFGA port\n");
190 } else if ((port
== ecpbase
+ PP_ECP_CFGB
) && ecpbase
) {
191 DPRINTF("ECP_CFGB port\n");
192 } else if ((port
== ecpbase
+ PP_ECP_ECR
) && ecpbase
) {
193 DPRINTF("ECP_ECR port\n");
195 DPRINTF("access to unsupported address range!\n");
199 tr
[i
].Data
.Byte
= val
;
201 DPRINTF("dwPortReturn: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
202 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
203 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
205 if (tr
[i
].cmdTrans
== 10)
206 DPRINTF("read byte: %d\n", tr
[i
].Data
.Byte
);