2 * Parallel port bit-banging 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.
16 #include "libnexys.win32.c"
18 #define LIBNEXYS_INTERNAL
25 #include <linux/ioctl.h>
26 #include <linux/ppdev.h>
35 nexys2_t nexys2_init(void) {
36 int pfd = open("/dev/parport0", O_RDWR);
39 perror("open /dev/parport0");
43 if ((ioctl(pfd, PPEXCL) < 0) || (ioctl(pfd, PPCLAIM) < 0))
45 perror("lock /dev/parport0");
52 int nexys2_jtag_enable(int pfd)
57 int nexys2_jtag_disable(int pfd)
62 int nexys2_jtag_setbits(int pfd, int tms, int tdi, int tck)
67 data = ((!!tms) << TMS) | ((!!tdi) << TDI) | ((!!tck) << TCK);
69 if (ioctl(pfd, PPWDATA, &data) < 0)
71 perror("nexys2_jtag_setbits: ioctl");
82 if (ioctl(pfd, PPRSTATUS, &data) < 0)
84 perror("_gettdi: PPRSTATUS");
95 int nexys2_jtag_puttdi(int pfd, int tms, int nbits, unsigned char *bits, unsigned char *obits)
100 memset(obits, 0, (nbits / 8) + ((nbits % 8) != 0));
102 for (i = 0; i < nbits; i++)
106 bit = bits[i / 8] & (1 << (i % 8));
107 nexys2_jtag_setbits(pfd, tms, bit, 1);
108 nexys2_jtag_setbits(pfd, tms, bit, 0);
110 obits[i/8] |= _gettdo(pfd) << (i % 8);
115 int nexys2_jtag_puttmstdi(int pfd, int nbits, unsigned char *bits, unsigned char *obits)
120 memset(obits, 0, (nbits / 8) + ((nbits % 8) != 0));
122 for (i = 0; i < nbits; i++)
126 tdi = bits[i / 4] & (1 << ((i % 4) * 2));
127 tms = bits[i / 4] & (2 << ((i % 4) * 2));
128 nexys2_jtag_setbits(pfd, tms, tdi, 0);
129 nexys2_jtag_setbits(pfd, tms, tdi, 1);
130 nexys2_jtag_setbits(pfd, tms, tdi, 0);
132 obits[i/8] |= _gettdo(pfd) << (i % 8);
137 int nexys2_jtag_gettdo(int pfd, int tdi, int tms, int nbits, unsigned char *obits)
142 memset(obits, 0, (nbits / 8) + ((nbits % 8) != 0));
144 for (i = 0; i < nbits; i++)
146 nexys2_jtag_setbits(pfd, tms, tdi, 0);
147 nexys2_jtag_setbits(pfd, tms, tdi, 1);
148 nexys2_jtag_setbits(pfd, tms, tdi, 0);
150 obits[i/8] |= _gettdo(pfd) << (i % 8);
159 void poke_idcode(nexys2_t nexys)
161 unsigned char seq1[] = { 0xaa, 0x22};
162 unsigned char seq2[] = { 0xaa, 0x02};
163 unsigned int idcode = 0;
164 unsigned int woop = 0x12345678;
168 while (nexys2_jtag_enable(nexys) < 0)
170 if (nexys2_jtag_puttmstdi(nexys, 8, seq1, NULL) < 0)
174 if (nexys2_jtag_gettdo(nexys, 0, 0, 32, &idcode) < 0)
176 printf("IDCODE: %08x\n", idcode);
177 } while(idcode && (idcode != 0xFFFFFFFF));
178 if (nexys2_jtag_puttmstdi(nexys, 8, seq2, NULL) < 0)
180 if (nexys2_jtag_disable(nexys) < 0)
188 nexys = nexys2_init();