2 * Main libusb driver for libnexys
3 * libnexys, a free driver for Digilent NEXYS2 boards
5 * Copyright (c) 2007-2008 Joshua Wise
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License.
11 * Commercial licenses are available by request.
15 #include "libnexys.win32.c"
17 #define LIBNEXYS_INTERNAL
22 #define NEXYS2_VENDOR 0x1443
23 #define NEXYS2_DEVICE 0x0005
25 nexys2_t nexys2_init(void) {
27 struct usb_device *dev;
30 unsigned char buf[15];
38 for (bus = usb_get_busses(); bus && !udev; bus = bus->next)
39 for (dev = bus->devices; dev && !udev; dev = dev->next)
40 if ((dev->descriptor.idVendor == NEXYS2_VENDOR) && (dev->descriptor.idProduct == NEXYS2_DEVICE))
45 fprintf(stderr, "nexys2_init: no device found or device open failed\n");
49 if (usb_set_configuration(udev, 1) < 0)
51 fprintf(stderr, "nexys2_init: failed to set configuration 1 -- check your permissions\n");
55 if (usb_claim_interface(udev, 0) < 0) /* Needed for libusb on win32. This code somehow crashes win32 libusb/kernel anyway, but... */
57 fprintf(stderr, "nexys2_init: failed to claim interface 0 -- check your permissions\n");
61 if (usb_reset(udev) < 0) /* Sometimes the board locks up when you close it, so send this. */
63 fprintf(stderr, "nexys2_init: failed to reset the USB (%s)\n", usb_strerror());
67 ret = usb_control_msg(udev, 0x00, 0x09, 1, 0, NULL, 0, 1000);
70 fprintf(stderr, "nexys2_init: failed to send setup control message\n");
74 ret = usb_control_msg(udev, 0xC0, 0xE4, 0, 0, buf, 14, 1000);
77 fprintf(stderr, "nexys2_init: failed to send serial number control message\n");
82 fprintf(stderr, "nexys2_init: got serial number \"%s\" (%d bytes)\n", buf, ret);
84 ret = usb_control_msg(udev, 0xC0, 0xE6, 0, 0, buf, 4, 1000);
87 fprintf(stderr, "nexys2_init: failed to send magic goo message\n");
90 if (memcmp(buf, "\x01\x00\x02\x00", 4))
92 fprintf(stderr, "nexys2_init: magic goo did not return what we expected (%d, %02x %02x %02x %02x)\n", ret, buf[0], buf[1], buf[2], buf[3]);
99 static unsigned char common_response[64] =
100 { 0x00, 0x03, 0xf3, 0x77, 0x9f, 0xf5, 0xdf, 0xdd,
101 0xdf, 0xfe, 0x5f, 0xac, 0xff, 0xad, 0xf3, 0x34,
102 0xaf, 0xf5, 0xac, 0xf7, 0xca, 0xb7, 0xf7, 0x6f,
103 0xff, 0x5e, 0x5e, 0xf7, 0xfb, 0xfd, 0xfb, 0x37,
104 0x81, 0x53, 0xbf, 0x64, 0x59, 0x47, 0x59, 0x2d,
105 0xe8, 0x30, 0x77, 0xda, 0x4f, 0xaf, 0x7f, 0xd7,
106 0x5f, 0xee, 0xc7, 0x3b, 0xdf, 0x3e, 0xbf, 0x35,
107 0xd1, 0xdf, 0xef, 0x3f, 0x9f, 0xde, 0xfa, 0xe2
110 int nexys2_jtag_enable(usb_dev_handle *udev)
113 unsigned char message[64] =
114 { 0x07, 0x01, 0x00, 0x00, 0xdc, 0xff, 0xd3, 0x00,
115 0xf3, 0x99, 0x83, 0x7c, 0x08, 0x26, 0x80, 0x7c,
116 0xff, 0xff, 0xff, 0xff, 0x00, 0x26, 0x80, 0x7c,
117 0xf1, 0xe2, 0x90, 0x7c, 0xb7, 0x24, 0x80, 0x7c,
118 0x14, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
119 0x2a, 0x95, 0x80, 0x7c, 0x5b, 0x3d, 0x00, 0x11,
120 0x14, 0x07, 0x00, 0x00, 0xe8, 0x22, 0xa0, 0x00,
121 0x02, 0x00, 0x00, 0x00, 0xb3, 0x9e, 0x01, 0x11 /* XXX does this need to be sequenced? */
123 unsigned char resp[64];
125 if ((ret = usb_bulk_write(udev, 0x01, message, 64, 1000)) < 64)
127 fprintf(stderr, "nexys2_jtag_enable: write failed: %d (%s)\n", ret, usb_strerror());
131 if ((ret = usb_bulk_read(udev, 0x81, resp, 64, 1000)) < 64)
133 fprintf(stderr, "nexys2_jtag_enable: short read on response: %d\n", ret);
137 if (memcmp(resp, common_response, 64))
139 fprintf(stderr, "nexys2_jtag_enable: incorrect response?\n");
146 int nexys2_jtag_disable(usb_dev_handle *udev)
149 unsigned char message[64] =
150 { 0x07, 0x00, 0xf3, 0x76, 0xdc, 0xff, 0xd3, 0x00,
151 0xf3, 0x99, 0x83, 0x7c, 0x08, 0x26, 0x80, 0x7c,
152 0xff, 0xff, 0xff, 0xff, 0x00, 0x26, 0x80, 0x7c,
153 0xf1, 0xe2, 0x90, 0x7c, 0xb7, 0x24, 0x80, 0x7c,
154 0x14, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
155 0x2a, 0x95, 0x80, 0x7c, 0x5b, 0x3d, 0x00, 0x11,
156 0x14, 0x07, 0x00, 0x00, 0xe8, 0x22, 0xa0, 0x00,
157 0x05, 0x00, 0x00, 0x00, 0xb3, 0x9e, 0x01, 0x11 /* XXX does this need to be sequenced? */
159 unsigned char resp[64];
161 if ((ret = usb_bulk_write(udev, 0x01, message, 64, 1000)) < 64)
163 fprintf(stderr, "nexys2_jtag_disable: write failed: %d (%s)\n", ret, usb_strerror());
167 if ((ret = usb_bulk_read(udev, 0x81, resp, 64, 1000)) < 64)
169 fprintf(stderr, "nexys2_jtag_disable: short read on response: %d\n", ret);
173 if (memcmp(resp, common_response, 64))
175 fprintf(stderr, "nexys2_jtag_disable: incorrect response?\n");
182 int nexys2_jtag_setbits(usb_dev_handle *udev, int tms, int tdi, int tck)
185 unsigned char message[64] =
186 { 0x08, !!tms, !!tdi, !!tck, 0xdc, 0xff, 0xd3, 0x00,
187 0xf3, 0x99, 0x83, 0x7c, 0x08, 0x26, 0x80, 0x7c,
188 0xff, 0xff, 0xff, 0xff, 0x00, 0x26, 0x80, 0x7c,
189 0xf1, 0xe2, 0x90, 0x7c, 0xb7, 0x24, 0x80, 0x7c,
190 0x14, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
191 0x2a, 0x95, 0x80, 0x7c, 0x5b, 0x3d, 0x00, 0x11,
192 0x14, 0x07, 0x00, 0x00, 0xe8, 0x22, 0xa0, 0x00,
193 0x03, 0x00, 0x00, 0x00, 0xb3, 0x9e, 0x01, 0x11 /* XXX does this need to be sequenced? */
195 unsigned char resp[64];
197 if ((ret = usb_bulk_write(udev, 0x01, message, 64, 1000)) < 64)
199 fprintf(stderr, "nexys2_jtag_setbits: write failed: %d (%s)\n", ret, usb_strerror());
203 /* if ((ret = usb_bulk_read(udev, 0x81, resp, 64, 1000)) < 64)
205 fprintf(stderr, "nexys2_jtag_setbits: short read on response: %d\n", ret);
209 if (memcmp(resp, common_response, 64))
211 fprintf(stderr, "nexys2_jtag_setbits: incorrect response?\n");
218 int nexys2_jtag_puttdi(usb_dev_handle *udev, int tms, int nbits, unsigned char *bits, unsigned char *obits)
220 int wanttdo = obits ? 1 : 0;
224 unsigned char message[64] =
225 { 0x09, wanttdo, !!tms, 0x00, 0x00, (nbits >> 8) & 0xFF, nbits & 0xFF, 0x7C,
226 0xeb, 0x06, 0x91, 0x7c, 0x28, 0xfa, 0xd3, 0x00,
227 0xec, 0xfc, 0xd3, 0x00, 0x00, 0x00, 0x00, 0x00,
228 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
229 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
230 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
231 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
232 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
237 fprintf(stderr, "nexys2_jtag_puttdi: too many bits!\n");
241 if ((ret = usb_bulk_write(udev, 0x01, message, 64, 1000)) < 64)
243 fprintf(stderr, "nexys2_jtag_puttdi: write failed: %d (%s)\n", ret, usb_strerror());
247 bytesrem = nbits / 8 + ((nbits % 8) != 0);
250 int nbytes = (bytesrem > 512) ? 512 : bytesrem;
251 if ((ret = usb_bulk_write(udev, 0x02, bits, nbytes, 1000)) < nbytes)
253 fprintf(stderr, "nexys2_jtag_puttdi: data write failed: %d (%s)\n", ret, usb_strerror());
262 bytesrem = nbits / 8 + ((nbits % 8) != 0);
265 int nbytes = (bytesrem > 512) ? 512 : bytesrem;
266 if ((ret = usb_bulk_read(udev, 0x86, obits, nbytes, 1000)) < nbytes)
268 fprintf(stderr, "nexys2_jtag_puttdi: data read failed: %d (%s)\n", ret, usb_strerror());
279 int nexys2_jtag_puttmstdi(usb_dev_handle *udev, int nbits, unsigned char *bits, unsigned char *obits)
281 int wanttdo = obits ? 1 : 0;
285 unsigned char message[64] =
286 { 0x0a, wanttdo, 0x00, 0x00, (nbits >> 8) & 0xFF, nbits & 0xFF, 0x91, 0x7C,
287 0xeb, 0x06, 0x91, 0x7c, 0x28, 0xfa, 0xd3, 0x00,
288 0xec, 0xfc, 0xd3, 0x00, 0x00, 0x00, 0x00, 0x00,
289 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
290 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
291 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
292 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
293 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
298 fprintf(stderr, "nexys2_jtag_puttmstdi: too many bits!\n");
302 if ((ret = usb_bulk_write(udev, 0x01, message, 64, 1000)) < 64)
304 fprintf(stderr, "nexys2_jtag_puttmstdi: write failed: %d (%s)\n", ret, usb_strerror());
308 bytesrem = nbits / 4 + ((nbits % 4) != 0);
311 int nbytes = (bytesrem > 512) ? 512 : bytesrem;
312 if ((ret = usb_bulk_write(udev, 0x02, bits, nbytes, 1000)) < nbytes)
314 fprintf(stderr, "nexys2_jtag_puttmstdi: data write failed: %d (%s)\n", ret, usb_strerror());
319 int nrbytes = nbytes/2 + (nbytes % 2);
320 if ((ret = usb_bulk_read(udev, 0x86, obits, nrbytes, 1000)) < nrbytes)
322 fprintf(stderr, "nexys2_jtag_puttmstdi: data read failed: %d (%s)\n", ret, usb_strerror());
334 int nexys2_jtag_gettdo(usb_dev_handle *udev, int tdi, int tms, int nbits, unsigned char *obits)
336 int wanttdo = obits ? 1 : 0;
340 unsigned char message[64] =
341 { 0x0B, !!tdi, !!tms, 0x00, 0x00, (nbits >> 8) & 0xFF, nbits & 0xFF, 0x7C,
342 0xeb, 0x06, 0x91, 0x7c, 0x28, 0xfa, 0xd3, 0x00,
343 0xec, 0xfc, 0xd3, 0x00, 0x00, 0x00, 0x00, 0x00,
344 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
345 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
346 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
347 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
348 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
353 fprintf(stderr, "nexys2_jtag_puttdo: too many bits!\n");
357 if ((ret = usb_bulk_write(udev, 0x01, message, 64, 1000)) < 64)
359 fprintf(stderr, "nexys2_jtag_gettdo: write failed: %d (%s)\n", ret, usb_strerror());
363 bytesrem = nbits / 8 + ((nbits % 8) != 0);
366 int nbytes = (bytesrem > 512) ? 512 : bytesrem;
367 if ((ret = usb_bulk_read(udev, 0x86, obits, nbytes, 1000)) < nbytes)
369 fprintf(stderr, "nexys2_jtag_gettdo: data read failed: %d (%s)\n", ret, usb_strerror());
383 void poke_idcode(nexys2_t nexys)
385 unsigned char seq1[] = { 0xaa, 0x22};
386 unsigned char seq2[] = { 0xaa, 0x02};
387 unsigned int idcode = 0;
391 while (nexys2_jtag_enable(nexys) < 0)
393 if (nexys2_jtag_puttmstdi(nexys, 8, seq1, NULL) < 0)
397 if (nexys2_jtag_gettdo(nexys, 0, 0, 32, &idcode) < 0)
399 printf("IDCODE: %08x\n", idcode);
400 } while(idcode && (idcode != 0xFFFFFFFF));
401 if (nexys2_jtag_puttmstdi(nexys, 8, seq2, NULL) < 0)
403 if (nexys2_jtag_disable(nexys) < 0)
411 nexys = nexys2_init();