Do a full USB reset, for compatibility with newer NEXYS2 boards.
[s3load.git] / libnexys.parport.c
CommitLineData
5ee943f9
JW
1#ifdef WIN32
2#include "libnexys.win32.c"
3#else
4#define LIBNEXYS_INTERNAL
5#ifndef PARPORT
6# define PARPORT
7#endif
8#include "libnexys.h"
9#include <stdio.h>
10#include <fcntl.h>
11#include <linux/ioctl.h>
12#include <linux/ppdev.h>
13#include <string.h>
14
15#define TDI 0
16#define TCK 1
17#define TMS 2
18#define TDO 4
19
20
21nexys2_t nexys2_init(void) {
22 int pfd = open("/dev/parport0", O_RDWR);
23 if (pfd < 0)
24 {
25 perror("open /dev/parport0");
26 return -1;
27 }
28
29 if ((ioctl(pfd, PPEXCL) < 0) || (ioctl(pfd, PPCLAIM) < 0))
30 {
31 perror("lock /dev/parport0");
32 return -1;
33 }
34
35 return pfd;
36}
37
38int nexys2_jtag_enable(int pfd)
39{
40 return 0;
41}
42
43int nexys2_jtag_disable(int pfd)
44{
45 return 0;
46}
47
48int nexys2_jtag_setbits(int pfd, int tms, int tdi, int tck)
49{
50 unsigned char data;
51 int i;
52
53 data = ((!!tms) << TMS) | ((!!tdi) << TDI) | ((!!tck) << TCK);
54
55 if (ioctl(pfd, PPWDATA, &data) < 0)
56 {
57 perror("nexys2_jtag_setbits: ioctl");
58 return -1;
59 }
60
61 return 0;
62}
63
64int _gettdo(int pfd)
65{
66 unsigned char data;
67
68 if (ioctl(pfd, PPRSTATUS, &data) < 0)
69 {
70 perror("_gettdi: PPRSTATUS");
71 return -1;
72 }
73
74 data ^= 0x80;
75 data >>= TDO;
76 data &= 1;
77
78 return data;
79}
80
81int nexys2_jtag_puttdi(int pfd, int tms, int nbits, unsigned char *bits, unsigned char *obits)
82{
83 int i;
84
85 if (obits)
86 memset(obits, 0, (nbits / 8) + ((nbits % 8) != 0));
87
88 for (i = 0; i < nbits; i++)
89 {
90 int bit;
91
92 bit = bits[i / 8] & (1 << (i % 8));
93 nexys2_jtag_setbits(pfd, tms, bit, 1);
94 nexys2_jtag_setbits(pfd, tms, bit, 0);
95 if (obits)
96 obits[i/8] |= _gettdo(pfd) << (i % 8);
97 }
98 return 0;
99}
100
101int nexys2_jtag_puttmstdi(int pfd, int nbits, unsigned char *bits, unsigned char *obits)
102{
103 int i;
104
105 if (obits)
106 memset(obits, 0, (nbits / 8) + ((nbits % 8) != 0));
107
108 for (i = 0; i < nbits; i++)
109 {
110 int tdi, tms;
111
112 tdi = bits[i / 4] & (1 << ((i % 4) * 2));
113 tms = bits[i / 4] & (2 << ((i % 4) * 2));
114 nexys2_jtag_setbits(pfd, tms, tdi, 0);
115 nexys2_jtag_setbits(pfd, tms, tdi, 1);
116 nexys2_jtag_setbits(pfd, tms, tdi, 0);
117 if (obits)
118 obits[i/8] |= _gettdo(pfd) << (i % 8);
119 }
120 return 0;
121}
122
123int nexys2_jtag_gettdo(int pfd, int tdi, int tms, int nbits, unsigned char *obits)
124{
125 int i;
126
127 if (obits)
128 memset(obits, 0, (nbits / 8) + ((nbits % 8) != 0));
129
130 for (i = 0; i < nbits; i++)
131 {
132 nexys2_jtag_setbits(pfd, tms, tdi, 0);
133 nexys2_jtag_setbits(pfd, tms, tdi, 1);
134 nexys2_jtag_setbits(pfd, tms, tdi, 0);
135 if (obits)
136 obits[i/8] |= _gettdo(pfd) << (i % 8);
137 }
138 return 0;
139}
140
141#endif
142
143#ifdef TEST_DRIVER
144
145void poke_idcode(nexys2_t nexys)
146{
147 unsigned char seq1[] = { 0xaa, 0x22};
148 unsigned char seq2[] = { 0xaa, 0x02};
149 unsigned int idcode = 0;
150 unsigned int woop = 0x12345678;
151
152// while(1)
153 {
154 while (nexys2_jtag_enable(nexys) < 0)
155 ;
156 if (nexys2_jtag_puttmstdi(nexys, 8, seq1, NULL) < 0)
157 return;
158 do
159 {
160 if (nexys2_jtag_gettdo(nexys, 0, 0, 32, &idcode) < 0)
161 return;
162 printf("IDCODE: %08x\n", idcode);
163 } while(idcode && (idcode != 0xFFFFFFFF));
164 if (nexys2_jtag_puttmstdi(nexys, 8, seq2, NULL) < 0)
165 return;
166 if (nexys2_jtag_disable(nexys) < 0)
167 return;
168 }
169}
170
171void main()
172{
173 nexys2_t nexys;
174 nexys = nexys2_init();
175 if (!nexys)
176 return;
177 poke_idcode(nexys);
178}
179
180#endif
181
This page took 0.032804 seconds and 4 git commands to generate.