OpenCores
URL https://opencores.org/ocsvn/an-fpga-implementation-of-low-latency-noc-based-mpsoc/an-fpga-implementation-of-low-latency-noc-based-mpsoc/trunk

Subversion Repositories an-fpga-implementation-of-low-latency-noc-based-mpsoc

[/] [an-fpga-implementation-of-low-latency-noc-based-mpsoc/] [trunk/] [mpsoc/] [src_c/] [jtag/] [jtag_libusb/] [jtag.c] - Blame information for rev 38

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 38 alirezamon
/* Copyright 2012 Brian Swetland <swetland@frotz.net>
2
 *
3
 * Licensed under the Apache License, Version 2.0 (the "License");
4
 * you may not use this file except in compliance with the License.
5
 * You may obtain a copy of the License at
6
 *
7
 *     http://www.apache.org/licenses/LICENSE-2.0
8
 *
9
 * Unless required by applicable law or agreed to in writing, software
10
 * distributed under the License is distributed on an "AS IS" BASIS,
11
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
 * See the License for the specific language governing permissions and
13
 * limitations under the License.
14
 */
15
 
16
#include <stdio.h>
17
#include <stdlib.h>
18
#include <unistd.h>
19
#include <string.h>
20
#include <ctype.h>
21
 
22
#include <libusb-1.0/libusb.h>
23
 
24
#define TRACE_USB       0
25
#define TRACE_JTAG      0
26
 
27
#define TIMOUT 1000
28
 
29
 
30
 
31
 
32
 
33
static struct libusb_device_handle *udev;
34
static int usb_open(unsigned vid, unsigned pid) {
35
        if (libusb_init(NULL) < 0)
36
                return -1;
37
 
38
        if (!(udev = libusb_open_device_with_vid_pid(NULL, vid, pid))) {
39
                fprintf(stderr,"cannot find device\n");
40
                return -1;
41
        }
42
 
43
        if (libusb_claim_interface(udev, 0) < 0) {
44
                fprintf(stderr,"cannot claim interface\n");
45
                return -1;
46
        }
47
        return 0;
48
}
49
static void usb_close(void) {
50
        libusb_exit(NULL);
51
}
52
#if TRACE_USB
53
static void dump(char *prefix, void *data, int len) {
54
        unsigned char *x = data;
55
        fprintf(stderr,"%s: (%d)", prefix, len);
56
        while (len > 0) {
57
                fprintf(stderr," %02x", *x++);
58
                len--;
59
        }
60
        fprintf(stderr,"\n");
61
}
62
#endif
63
static int usb_bulk(unsigned char ep, void *data, int len, unsigned timeout) {
64
        int r, xfer;
65
#if TRACE_USB
66
        if (!(ep & 0x80))
67
                dump("xmit", data, len);
68
#endif
69
        r = libusb_bulk_transfer(udev, ep, data, len, &xfer, timeout);
70
        if (r < 0) {
71
                fprintf(stderr,"bulk: error: %d\n", r);
72
                return r;
73
        }
74
#if TRACE_USB
75
        if (ep & 0x80)
76
                dump("recv", data, xfer);
77
#endif
78
        return xfer;
79
}
80
 
81
#define EP1_IN  0x81
82
#define EP2_OUT 0x02
83
 
84
#define UB_BYTEMODE     0x80
85
#define UB_BITMODE      0x00
86
#define UB_READBACK     0x40
87
 
88
/* bits in bit mode */
89
#define UB_OE           0x20
90
#define UB_TDI          0x10
91
#define UB_nCS          0x08
92
#define UB_nCE          0x04
93
#define UB_TMS          0x02
94
#define UB_TCK          0x01
95
#define BUFF_SZ         512
96
/* bytecount for data bytes that follow in byte mode */
97
#define UB_COUNT(n)     ((n) & 0x3F)
98
 
99
int jtag_move(int count, unsigned bits){
100
        unsigned char buf[BUFF_SZ];
101
        int n = 0;
102
#if TRACE_JTAG
103
        fprintf(stderr,"move: %08x (%d)\n", bits, count);
104
#endif
105
        while (count-- > 0) {
106
                if (bits & 1) {
107
                        buf[n++] = UB_TMS;
108
                        buf[n++] = UB_TMS | UB_TCK;
109
                } else {
110
                        buf[n++] = 0;
111
                        buf[n++] = UB_TCK;
112
                }
113
                bits >>= 1;
114
        }
115
        return usb_bulk(EP2_OUT, buf, n, TIMOUT);
116
}
117
 
118
int jtag_shift(int count, unsigned bits, unsigned *out) {
119
        unsigned char buf[BUFF_SZ];
120
        unsigned RB = out ? UB_READBACK : 0;
121
        int n = 0;
122
        int readcount = count;
123
        int r,bit;
124
#if TRACE_JTAG
125
        fprintf(stderr,"xfer: %08x (%d)\n", bits, count);
126
#endif
127
        while (count-- > 0) {
128
                if (bits & 1) {
129
                        buf[n++] = UB_TDI;
130
                        buf[n++] = UB_TDI | UB_TCK | RB;
131
                } else {
132
                        buf[n++] = 0;
133
                        buf[n++] = UB_TCK | RB;
134
                }
135
                bits >>= 1;
136
        }
137
        buf[n-1] |= UB_TMS;
138
        buf[n-2] |= UB_TMS;
139
        r = usb_bulk(EP2_OUT, buf, n, TIMOUT);
140
        if (r < 0)
141
                return r;
142
        if (!out)
143
                return 0;
144
        bits = 0;
145
        bit = 1;
146
        while (readcount > 0) {
147
                r = usb_bulk(EP1_IN, buf, BUFF_SZ, TIMOUT);
148
                if (r < 0)
149
                        return r;
150
                if (r < 3)
151
                        continue;
152
                for (n = 2; n < r; n++) {
153
                        if (buf[n] & 1)
154
                                bits |= bit;
155
                        bit <<= 1;
156
                        readcount--;
157
                        if (readcount == 0) {
158
#if TRACE_JTAG
159
                                fprintf(stderr,"    : %08x\n", bits);
160
#endif
161
                                *out = bits;
162
                                return 0;
163
                        }
164
                }
165
        }
166
        return -1;
167
}
168
 
169
 
170
int jtag_shift_long(int count, unsigned * bits, unsigned *out) {
171
        unsigned char buf[BUFF_SZ];
172
        unsigned RB = out ? UB_READBACK : 0;
173
        int n = 0;
174
        int readcount = count;
175
        int r,bit;
176
        unsigned int p=0;
177
 
178
#if TRACE_JTAG
179
        fprintf(stderr,"xfer: %08x (%d)\n", bits[count>>5], count);
180
#endif
181
        while (count-- > 0) {
182
                p=((readcount-count)-1)>>5;
183
                if (bits[p] & 1) {
184
                        buf[n++] = UB_TDI;
185
                        buf[n++] = UB_TDI | UB_TCK | RB;
186
                } else {
187
                        buf[n++] = 0;
188
                        buf[n++] = UB_TCK | RB;
189
                }
190
                bits[p] = bits[p] >> 1;
191
        }
192
        buf[n-1] |= UB_TMS;
193
        buf[n-2] |= UB_TMS;
194
        r = usb_bulk(EP2_OUT, buf, n, TIMOUT);
195
        if (r < 0)
196
                return r;
197
        if (!out)
198
                return 0;
199
 
200
        unsigned B = 0;
201
        bit = 1;
202
 
203
        count=readcount;
204
        int shift=0;
205
        while (readcount > 0) {
206
 
207
                r = usb_bulk(EP1_IN, buf, BUFF_SZ, TIMOUT);
208
                //int j;
209
                //for(j=0;j<r;j++)printf("%u",buf[j]&1);
210
                //printf("r=%u\n",r);           
211
                if (r < 0)
212
                        return r;
213
                if (r < 3)
214
                        continue;
215
                for (n = 2; n < r; n++) {
216
                        if(n%64==0 ) continue;
217
                        if(n%64==1 ) continue;
218
                        p=((count-readcount))>>5;
219
                        //printf("%u",buf[n]&1);
220
                        if (buf[n] & 1)
221
                                B |= bit;
222
                                bit <<= 1;
223
                                shift++;
224
                                if(shift%32==0){
225
                                        bit=1;
226
                                        out[p]= B;
227
                                        //printf("out[%u]=%x\n",p, out[p]);
228
                                        B=0;
229
 
230
 
231
                                }
232
                                readcount--;
233
                        if (readcount == 0 ) {
234
#if TRACE_JTAG
235
                                fprintf(stderr,"    : %08x\n", bits[p]);
236
#endif
237
                                if (shift%32!=0) out[p]= B;
238
                                //printf("out[%u]=%x\n",p, out[p]);
239
                                return 0;
240
                        }
241
                }
242
        }
243
        return -1;
244
}
245
 
246
 
247
 
248
/* JTAG notes
249
 *
250
 * TMS is sampled on +TCK
251
 * Capture-XR state loads shift register on +TCK as state is exited
252
 * Shift-XR state TDO goes active (containing shiftr[0]) on the first -TCK
253
 *          after entry, shifts occur on each +TCK, *including* the +TCK
254
 *          that will exist Shift-XR when TMS=1 again
255
 * Update-XR update occurs on the -TCK after entry to state
256
 *
257
 * Any -> Reset: 11111
258
 * Any -> Reset -> RTI: 111110
259
 * RTI -> ShiftDR: 100
260
 * ShiftDR shifting: 0 x N
261
 * ShiftDR -> UpdateDR -> RTI: 110
262
 * ShiftDR -> UpdateDR -> ShiftDR: 11100
263
 * RTI -> ShiftIR: 1100
264
 * ShiftIR shifting: 0 x N
265
 * ShiftIR -> UpdateIR -> RTI: 110
266
 */
267
 
268
#define RESET   8,0b01111111
269
#define SHIFTDR 3,0b001
270
#define SHIFTIR 4,0b0011
271
#define DONE    2,0b01
272
#define AGAIN   4,0b0011
273
 
274
int jtag_ir(unsigned sz, unsigned bits) {
275
        int r;
276
        if ((r = jtag_move(SHIFTIR)) < 0) return r;
277
        if ((r = jtag_shift(sz, bits, 0)) < 0) return r;
278
        if ((r = jtag_move(DONE)) < 0) return r;
279
        return 0;
280
}
281
 
282
int jtag_dr(unsigned sz, unsigned bits, unsigned *out) {
283
        int r;
284
        if ((r = jtag_move(SHIFTDR)) < 0) return r;
285
        if ((r = jtag_shift(sz, bits, out)) < 0) return r;
286
        if ((r = jtag_move(DONE)) < 0) return r;
287
        return 0;
288
}
289
 
290
int jtag_dr_long(unsigned sz, unsigned * bits, unsigned *out, int words) {
291
        int r;
292
        //unsigned s=32;
293
        if ((r = jtag_move(SHIFTDR)) < 0) return r;
294
        //for(i=0;i<words;i++){
295
        //      if(i==words-1) s= sz-(i*32);
296
        //      printf("shift(%u, %x, out)\n",s,bits[i]);
297
                if ((r = jtag_shift_long(sz, bits, out)) < 0) return r;
298
        //}
299
        if ((r = jtag_move(DONE)) < 0) return r;
300
        return 0;
301
}
302
 
303
 
304
 
305
static int jtag_is_open = 0;
306
 
307
int jtag_open(unsigned vid, unsigned pid) {
308
        int r;
309
        if (!jtag_is_open) {
310
                r = usb_open(vid, pid);
311
                if (r < 0)
312
                        return r;
313
                jtag_is_open = 1;
314
        }
315
        return 0;
316
}
317
int jtag_close(void) {
318
        if (jtag_is_open) {
319
                usb_close();
320
                jtag_is_open = 0;
321
        }
322
        return 0;
323
}
324
int jtag_reset(void) {
325
        return jtag_move(RESET);
326
}
327
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.