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