]> cvs.zerfleddert.de Git - usb-driver/blame - jtagkey.c
much faster programming via FTDI2232 now.
[usb-driver] / jtagkey.c
CommitLineData
8121bc50 1#include <stdio.h>
2#include <ftdi.h>
3#include "usb-driver.h"
4#include "jtagkey.h"
5
d0e2002d 6#define USBBUFSIZE 384
7
8121bc50 8static struct ftdi_context ftdic;
9
10int jtagkey_init(unsigned short vid, unsigned short pid) {
11 int ret = 0;
d0e2002d 12 unsigned char c;
8121bc50 13
14 if ((ret = ftdi_init(&ftdic)) != 0) {
15 fprintf(stderr, "unable to initialise libftdi: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
16 return ret;
17 }
18
19 if ((ret = ftdi_usb_open(&ftdic, vid, pid)) != 0) {
20 fprintf(stderr, "unable to open ftdi device: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
21 return ret;
22 }
23
24 if ((ret = ftdi_usb_reset(&ftdic)) != 0) {
25 fprintf(stderr, "unable reset device: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
26 return ret;
27 }
28
29 if ((ret = ftdi_set_interface(&ftdic, INTERFACE_A)) != 0) {
30 fprintf(stderr, "unable to set interface: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
31 return ret;
32 }
33
b122ac4b 34#if 0
d0e2002d 35 if ((ret = ftdi_write_data_set_chunksize(&ftdic, 1)) != 0) {
8121bc50 36 fprintf(stderr, "unable to set write chunksize: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
37 return ret;
38 }
b122ac4b 39#endif
8121bc50 40
41 if ((ret = ftdi_set_latency_timer(&ftdic, 1)) != 0) {
42 fprintf(stderr, "unable to set latency timer: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
43 return ret;
44 }
45
d0e2002d 46 if ((ret = ftdi_set_baudrate(&ftdic, 500000)) != 0) {
8121bc50 47 fprintf(stderr, "unable to set baudrate: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
48 return ret;
49 }
50
d0e2002d 51 c = 0x00;
52 ftdi_write_data(&ftdic, &c, 1);
53
b122ac4b 54 if ((ret = ftdi_set_bitmode(&ftdic, JTAGKEY_TCK|JTAGKEY_TDI|JTAGKEY_TMS|JTAGKEY_OEn, BITMODE_SYNCBB)) != 0) {
8121bc50 55 fprintf(stderr, "unable to enable bitbang mode: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
56 return ret;
57 }
58
59 if ((ret = ftdi_usb_purge_buffers(&ftdic)) != 0) {
60 fprintf(stderr, "unable to purge buffers: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
61 return ret;
62 }
63
64 return ret;
65}
66
67void jtagkey_close() {
68 ftdi_disable_bitbang(&ftdic);
69 ftdi_usb_close(&ftdic);
70 ftdi_deinit(&ftdic);
71}
72
73void jtagkey_state(unsigned char data) {
74 fprintf(stderr,"Pins high: ");
75
76 if (data & JTAGKEY_TCK)
77 fprintf(stderr,"TCK ");
78
79 if (data & JTAGKEY_TDI)
80 fprintf(stderr,"TDI ");
81
82 if (data & JTAGKEY_TDO)
83 fprintf(stderr,"TDO ");
84
85 if (data & JTAGKEY_TMS)
86 fprintf(stderr,"TMS ");
87
88 if (data & JTAGKEY_VREF)
89 fprintf(stderr,"VREF ");
90
91 fprintf(stderr,"\n");
92}
93
94int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase, int ecpbase, int num) {
95 int ret = 0;
96 int i;
d0e2002d 97 int nread = 0;
8121bc50 98 unsigned long port;
99 unsigned char val;
d0e2002d 100 static unsigned char last_data = 0;
101 static unsigned char writebuf[USBBUFSIZE], *writepos = writebuf;
102 static unsigned char readbuf[USBBUFSIZE], *readpos;
8121bc50 103 unsigned char data;
d0e2002d 104 static unsigned char last_write = 0x00;
105
106 /* Count writes */
107 for (i = 0; i < num; i++)
108 if (tr[i].cmdTrans == 10)
109 nread++;
110
111 /* Write combining */
112 if ((writepos-writebuf > sizeof(writebuf)-num) || (nread && writepos-writebuf)) {
113 DPRINTF("writing %d bytes due to %d following reads in %d chunks or full buffer\n", writepos-writebuf, nread, num);
114
115 ftdi_write_data(&ftdic, writebuf, writepos-writebuf);
116
117 i = 0;
118 while (i < writepos-writebuf) {
119 i += ftdi_read_data(&ftdic, readbuf, sizeof(readbuf));
120 };
121 DPRINTF("read %d/%d bytes\n", i, writepos-writebuf);
122 writepos = writebuf;
123 }
8121bc50 124
125 for (i = 0; i < num; i++) {
126 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
127 (unsigned long)tr[i].dwPort, tr[i].cmdTrans, tr[i].dwBytes,
128 tr[i].fAutoinc, tr[i].dwOptions);
129
130 port = (unsigned long)tr[i].dwPort;
131 val = tr[i].Data.Byte;
132
133#ifdef DEBUG
134 if (tr[i].cmdTrans == 13)
135 DPRINTF("write byte: %d\n", val);
136#endif
137
b122ac4b 138 /* Pad writebuf for read-commands in stream */
d0e2002d 139 *writepos = last_data;
b122ac4b 140
8121bc50 141 if (port == ppbase + PP_DATA) {
142 DPRINTF("data port\n");
143
144 data = 0x00;
145 switch(tr[i].cmdTrans) {
146 case PP_READ:
147 ret = 0; /* We don't support reading of the data port */
148 break;
149
150 case PP_WRITE:
151 if (val & PP_TDI) {
152 data |= JTAGKEY_TDI;
153 DPRINTF("TDI\n");
154 } else {
155 DPRINTF("!TDI\n");
156 }
157 if (val & PP_TCK) {
158 data |= JTAGKEY_TCK;
159 DPRINTF("TCK\n");
160 } else {
161 DPRINTF("!TCK\n");
162 }
163 if (val & PP_TMS) {
164 data |= JTAGKEY_TMS;
165 DPRINTF("TMS\n");
166 } else {
167 DPRINTF("!TMS\n");
168 }
169 if (val & PP_CTRL) {
d0e2002d 170 data = JTAGKEY_OEn;
8121bc50 171 DPRINTF("CTRL\n");
172 } else {
173 DPRINTF("!CTRL\n");
174 }
8121bc50 175
d0e2002d 176 if (val & PP_PROG) {
177 DPRINTF("PROG\n");
178 } else {
179 DPRINTF("!PROG\n");
180 }
181
182 *writepos = data;
183
b122ac4b 184 last_data = data;
d0e2002d 185 last_write = val;
8121bc50 186 break;
187
188 default:
189 fprintf(stderr,"!!!Unsupported TRANSFER command: %lu!!!\n", tr[i].cmdTrans);
190 ret = -1;
191 break;
192 }
b122ac4b 193 }
d0e2002d 194 writepos++;
b122ac4b 195 }
196
d0e2002d 197 if (nread)
198 {
199 DPRINTF("writing %d bytes\n", writepos-writebuf);
200 ftdi_write_data(&ftdic, writebuf, writepos-writebuf);
201 DPRINTF("reading %d bytes\n", writepos-writebuf);
b122ac4b 202
d0e2002d 203 i = 0;
204 while (i < writepos-writebuf) {
205 i += ftdi_read_data(&ftdic, readbuf, sizeof(readbuf));
206 }
207 DPRINTF("read %d bytes\n", i);
b122ac4b 208
209#ifdef DEBUG
d0e2002d 210 DPRINTF("write: ");
211 hexdump(writebuf, writepos-writebuf);
212 DPRINTF("read: ");
213 hexdump(readbuf, i);
b122ac4b 214#endif
215
d0e2002d 216 writepos = writebuf;
217 } else {
218 return ret;
219 }
220
221 readpos = readbuf;
222 readpos += 0;
b122ac4b 223
224 for (i = 0; i < num; i++) {
225 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
226 (unsigned long)tr[i].dwPort, tr[i].cmdTrans, tr[i].dwBytes,
227 tr[i].fAutoinc, tr[i].dwOptions);
228
229 port = (unsigned long)tr[i].dwPort;
230 val = tr[i].Data.Byte;
d0e2002d 231 readpos++;
b122ac4b 232
d0e2002d 233 if (port == ppbase + PP_DATA) {
234 if (tr[i].cmdTrans == PP_WRITE) {
235 last_write = val;
236 }
237 } else if (port == ppbase + PP_STATUS) {
238 DPRINTF("status port (last write: 0x%x)\n", last_write);
8121bc50 239 switch(tr[i].cmdTrans) {
240 case PP_READ:
d0e2002d 241 data = *readpos;
8121bc50 242#ifdef DEBUG
243 DPRINTF("READ: 0x%x\n", data);
244 jtagkey_state(data);
245#endif
246
247 val = 0x00;
d0e2002d 248 if ((data & JTAGKEY_TDO) && (last_write & PP_PROG))
8121bc50 249 val |= PP_TDO;
250
d0e2002d 251 if (!(last_write & PP_PROG))
252 val |= 0x08;
253
8121bc50 254 if (last_write & 0x40)
255 val |= 0x20;
256 else
257 val |= 0x80;
258 break;
259
260 case PP_WRITE:
261 ret = 0; /* Status Port is readonly */
262 break;
263
264 default:
265 fprintf(stderr,"!!!Unsupported TRANSFER command: %lu!!!\n", tr[i].cmdTrans);
266 ret = -1;
267 break;
268 }
8121bc50 269 } else {
8121bc50 270 ret = 0;
271 }
272
273 tr[i].Data.Byte = val;
274
275 DPRINTF("dwPortReturn: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
276 (unsigned long)tr[i].dwPort, tr[i].cmdTrans, tr[i].dwBytes,
277 tr[i].fAutoinc, tr[i].dwOptions);
278#ifdef DEBUG
279 if (tr[i].cmdTrans == 10)
280 DPRINTF("read byte: %d\n", tr[i].Data.Byte);
281#endif
282 }
283
284 return ret;
285}
Impressum, Datenschutz