OpenCores
URL https://opencores.org/ocsvn/xulalx25soc/xulalx25soc/trunk

Subversion Repositories xulalx25soc

[/] [xulalx25soc/] [trunk/] [sw/] [usbi.cpp] - Blame information for rev 73

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3 11 dgisselq
// Filename:    usbi.cpp
4 5 dgisselq
//
5
// Project:     XuLA2 board
6
//
7
// Purpose:     Creates a USB port, similar to a serial port, out of the
8
//              XuLA2 JTAG interface S/W.
9
//
10
//
11
// Creator:     Dan Gisselquist, Ph.D.
12
//              Gisselquist Technology, LLC
13
//
14
////////////////////////////////////////////////////////////////////////////////
15
//
16
// Copyright (C) 2015, Gisselquist Technology, LLC
17
//
18
// This program is free software (firmware): you can redistribute it and/or
19
// modify it under the terms of  the GNU General Public License as published
20
// by the Free Software Foundation, either version 3 of the License, or (at
21
// your option) any later version.
22
//
23
// This program is distributed in the hope that it will be useful, but WITHOUT
24
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
25
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
26
// for more details.
27
//
28
// License:     GPL, v3, as defined and found on www.gnu.org,
29
//              http://www.gnu.org/licenses/gpl.html
30
//
31
//
32
////////////////////////////////////////////////////////////////////////////////
33
//
34
//
35
#include <stdio.h>
36
#include <stdlib.h>
37
#include <unistd.h>
38
#include <string.h>
39
#include <ctype.h>
40
// #include <usb.h>
41
 
42
#include <libusb.h>
43
 
44
#include "llcomms.h"
45
#include "usbi.h"
46
 
47
// Walk us through the JTAG Chain:
48
//      5-1's to go to test/reset
49
//      0        to go to Run-Test/Idle
50
//      1       to go to select-dr-scan
51
//      1       to go to select-ir-scan
52
//      0        to go to capture-ir
53
//      0        to go to shift-ir
54
//      (6-bit code 0x02 through TDI to IR, while sending 0-bits to TMS)
55
//      1       to leave shift IR and go to exit1-ir
56
//      1       to go to update-ir
57
//      1       to go to select-dr-scan
58
//      0        to go to capture-dr
59
//      0        to go to shift-dr
60
#define RESET_JTAG_LEN  12
61
static const char       RESET_TO_USER_DR[RESET_JTAG_LEN] = {
62
        JTAG_CMD,
63
        21, // clocks
64
        0,0,0,     // Also clocks, higher order bits
65
        PUT_TMS_MASK | PUT_TDI_MASK, // flags
66
        (char)(0x0df),  // TMS: Five ones, then one zero, and two ones -- low bits first
67
        0x00,   // TDI: irrelevant here
68
        (char)0x80,     // TMS: two zeros, then six zeros
69
        0x08,   // TDI: user command #1, bit reversed
70
        0x03,   // TMS: three ones, then two zeros
71
        0x00    // TDI byte -- irrelevant here
72
        //
73
        // 0xc0, // TDI byte -- user command #2
74
        // 0x40,        // TDI: user command #1
75
        // 0x0c, // TDI byte -- user command #2, bit reversed
76
};
77
 
78
//
79
//      TMS: 
80
#define TX_DR_LEN       12
81
static const    char    TX_DR_BITS[TX_DR_LEN] = {
82
        JTAG_CMD,
83
        48, // clocks
84
        0,0,0,     // Also clocks, higher order bits
85
        PUT_TDI_MASK, // flags
86
        (char)0x0ff, 0, 0, 0, 0, 0   // Six data bytes
87
        // module_id = 255
88
        // 32'h(payload.length)
89
        // payload
90
        // module_id + payload.len + num_result_bits, length=32 + payload ???
91
};
92
 
93
//
94
//      TMS: 
95
//      
96
#define REQ_RX_LEN      6
97
static const    char    REQ_RX_BITS[REQ_RX_LEN] = {
98
        JTAG_CMD,
99
        (const char)((USB_PKTLEN-REQ_RX_LEN)*8), // bits-requested
100
        0,0,0,     // Also clocks, higher order bits
101
        GET_TDO_MASK|TDI_VAL_MASK, // flags:TDI is kept low here, so no TDI flag
102
        // No data given, since there's no info to send or receive
103
        // Leave the result in shift-DR mode
104
};
105
 
106
/*
107
#define RETURN_TO_RESET_LEN     7
108
static const char       RETURN_TO_RESET[RETURN_TO_RESET_LEN] = {
109
        JTAG_CMD,
110
        5, // clocks
111
        0,0,0,  // Also clocks, higher order bits
112
        PUT_TMS_MASK, // flags
113
        (char)0x0ff, // Five ones
114
};
115
*/
116
 
117
USBI::USBI(void) {
118
        int     config;
119
 
120
        m_total_nread = 0;
121
 
122
        if (0 != libusb_init(&m_usb_context)) {
123
                fprintf(stderr, "Error initializing the USB library\n");
124
                perror("O/S Err:");
125
                exit(-1);
126
        }
127
 
128
        m_xula_usb_device = libusb_open_device_with_vid_pid(m_usb_context,
129
                VENDOR_ID, PRODUCT_ID);
130
        if (!m_xula_usb_device) {
131
                fprintf(stderr, "Could not open XuLA device\n");
132
                perror("O/S Err:");
133
 
134
                libusb_exit(m_usb_context);
135
                exit(-1);
136
        }
137
 
138
        if (0 != libusb_get_configuration(m_xula_usb_device, &config)) {
139
                fprintf(stderr, "Could not get configuration\n");
140
                perror("O/S Err:");
141
 
142
                libusb_close(m_xula_usb_device);
143
                libusb_exit(m_usb_context);
144
                exit(-1);
145
        }
146
 
147
        if (0 != libusb_claim_interface(m_xula_usb_device, XESS_INTERFACE)) {
148
                fprintf(stderr, "Could not claim interface\n");
149
                perror("O/S Err:");
150
 
151
                libusb_close(m_xula_usb_device);
152
                libusb_exit(m_usb_context);
153
                exit(-1);
154
        }
155
 
156
        unsigned char   abuf[RESET_JTAG_LEN];
157
        int     actual_length = RESET_JTAG_LEN, r;
158
 
159
        memcpy(abuf, RESET_TO_USER_DR, RESET_JTAG_LEN);
160
        r = libusb_bulk_transfer(m_xula_usb_device, XESS_ENDPOINT_OUT,
161
                abuf, RESET_JTAG_LEN, &actual_length, 4);
162
        if ((r!=0)||(actual_length != RESET_JTAG_LEN)) {
163
                // Try clearing the queue twice, then doing this
164
                do {
165
                        r = libusb_bulk_transfer(m_xula_usb_device, XESS_ENDPOINT_IN,
166
                                (unsigned char *)m_rxbuf, USB_PKTLEN, &actual_length, 20);
167
                } while((r==0)&&(actual_length > 0));
168
 
169
                r = libusb_bulk_transfer(m_xula_usb_device, XESS_ENDPOINT_OUT,
170
                        abuf, RESET_JTAG_LEN, &actual_length, 4);
171
                if ((r != 0)||(actual_length != RESET_JTAG_LEN)) {
172
                        printf("Some error took place requesting RESET_TO_USER_DR\n");
173
                        printf("r = %d, actual_length = %d (vs %d requested)\n",
174
                                r, actual_length, RESET_JTAG_LEN);
175
                        perror("O/S Err");
176
                        exit(-2);
177
                }
178
        }
179
 
180
        // Initialize our read FIFO
181
        m_rbeg = m_rend = 0;
182
 
183
        flush_read();
184
}
185
 
186
void    USBI::close(void) {
187
        // Release our interface
188
        if (0 != libusb_release_interface(m_xula_usb_device, XESS_INTERFACE)) {
189
                fprintf(stderr, "Could not release interface\n");
190
                perror("O/S Err:");
191
 
192
                libusb_close(m_xula_usb_device);
193
                libusb_exit(m_usb_context);
194
                exit(-1);
195
        }
196
 
197
        // And then close our device with
198
        libusb_close(m_xula_usb_device);
199
 
200
        // And just before exiting, we free our USB context
201
        libusb_exit(m_usb_context);
202
}
203
 
204
void    USBI::write(char *buf, int len) {
205
        int     r, actual_length;
206
 
207
        if (len >= USB_PKTLEN) {
208
                const int       nv = USB_PKTLEN/2-1;
209
                for(int pos=0; pos<len; pos+=nv)
210
                        write(&buf[pos], (len-pos>nv)?(nv):len-pos);
211
        } else {
212
                memset(m_txbuf, 0, len+6);
213
                m_txbuf[0] = JTAG_CMD;
214
                m_txbuf[1] = len * 8;
215
                m_txbuf[2] = 0;
216
                m_txbuf[3] = 0;
217
                m_txbuf[4] = 0;
218
                m_txbuf[5] = PUT_TDI_MASK | GET_TDO_MASK;
219
 
220
                for(int i=0; i<len; i++)
221
                        m_txbuf[6+i] = buf[i];
222 13 dgisselq
                // printf("WRITE::(buf=%*s, %d)\n", len, buf, len);
223 5 dgisselq
                r = libusb_bulk_transfer(m_xula_usb_device, XESS_ENDPOINT_OUT,
224
                        (unsigned char*)m_txbuf, len+6, &actual_length, 0);
225
                if ((r!=0)||(actual_length != len+6)) {
226
                        printf("WRITE::(buf, %d) -- ERR\n", len+6);
227
                        printf("r = %d, actual_length = %d (!= %d requested)\n", r,
228
                                actual_length, len+6);
229
 
230
                        if (r == -7) {
231
                                r = libusb_bulk_transfer(m_xula_usb_device,
232
                                        XESS_ENDPOINT_OUT,
233
                                        (unsigned char*)m_txbuf, len+6,
234
                                        &actual_length, 2);
235
                                if ((r!=0)||(actual_length != len+6)) {
236
                                        printf("WRITE::(buf, %d) -- ERR\n", len+6);
237
                                        printf("r = %d, actual_length = %d (!= %d requested)\n", r,
238
                                                actual_length, len+6);
239
                                        perror("O/S Err");
240
 
241
                                        exit(-2);
242
                                }
243
                        } else {
244
                                perror("O/S Err");
245
                                exit(-2);
246
                        }
247
                }
248
 
249
                // Try to read back however many bytes we can
250
                r = libusb_bulk_transfer(m_xula_usb_device, XESS_ENDPOINT_IN,
251
                        (unsigned char*)m_rxbuf, USB_PKTLEN, &actual_length, 20);
252
                if ((r==0)&&(actual_length > 0)) {
253
                        push_fifo(m_rxbuf, actual_length);
254
                } else {
255
                        printf("Some error took place in receiving\n");
256
                        perror("O/S Err");
257
                }
258
        }
259
}
260
 
261
int     USBI::read(char *buf, int len) {
262
        return read(buf, len, 4);
263
}
264
 
265
int     USBI::read(char *buf, int len, int timeout_ms) {
266
        int     left = len, nr=0;
267
 
268
        // printf("USBI::read(%d) (FIFO is %d-%d)\n", len, m_rend, m_rbeg);
269
        nr = pop_fifo(buf, left);
270
        left -= nr;
271
 
272
        while(left > 0) {
273
                raw_read(left, timeout_ms);
274 41 dgisselq
                nr = pop_fifo(&buf[len-left], left);
275 5 dgisselq
                left -= nr;
276
 
277
                // printf("\tWHILE (nr = %d, LEFT = %d, len=%d)\n", nr, left, len);
278
                if (nr == 0)
279
                        break;
280
        }
281
 
282
        // printf("READ %d characters (%d req, %d left)\n", len-left, len, left);
283
        return len-left;
284
}
285
 
286
void    USBI::raw_read(const int clen, int timeout_ms) {
287
        int     avail = (m_rbeg - m_rend)&(RCV_BUFMASK), actual_length;
288
        int     len = clen;
289
        if (len > RCV_BUFMASK-avail)
290
                len = RCV_BUFMASK-avail;
291
        if (len > 26)
292
                len = 26;
293
 
294
        // printf("USBI::RAW-READ(%d, was %d)\n", len, clen);
295
        memcpy(m_txbuf, REQ_RX_BITS, REQ_RX_LEN);
296
 
297 46 dgisselq
        // I have chased process hangs to this line, but have no more
298
        // information than that somewhere within libusb_bulk_transfer,
299
        // libusb_handle_events_completed,
300
        // libusb_handle_events_timeout_completed, ?, poll(), there seems
301
        // to be a bug.
302
        //
303
        // I'm not certain if the bug is in the Linux kernel, or in the 
304
        // Xess tools.  I will note that, when a followup attempt is made
305
        // to read from the device, previously unread data may still get dumped
306
        // to it.  Therefore, be careful to clear the device upon starting
307
        // any process.
308
        //
309
        // fprintf(stderr, "[");
310 5 dgisselq
        int r = libusb_bulk_transfer(m_xula_usb_device, XESS_ENDPOINT_OUT,
311
                (unsigned char *)m_txbuf, REQ_RX_LEN, &actual_length,
312
                timeout_ms);
313 46 dgisselq
        // fprintf(stderr, "]");
314
 
315 5 dgisselq
        if ((r==0)&&(actual_length == REQ_RX_LEN)) {
316
        } else if (r == -7) {
317
                // Nothing to read in the timeout provided
318
                // We'll have to request this data again ... later
319
                return;
320
        } else {
321
                printf("READ(WRITE,READ-REQ) -- ERR\n");
322
                printf("r = %d, actual_length = %d (!= %d requested)\n", r,
323
                        actual_length, len+6);
324
                perror("O/S Err");
325
                exit(-2);
326
        }
327
 
328
        // Try to read back however many bytes we can
329
        r = libusb_bulk_transfer(m_xula_usb_device, XESS_ENDPOINT_IN,
330
                (unsigned char *)m_rxbuf, USB_PKTLEN, &actual_length, 0);
331
        if ((r==0)&&(actual_length > 0)) {
332
                /*
333
                printf("RAW-READ() -> %d Read\n", actual_length);
334
                for(int i=0; i<actual_length; i++)
335
                        printf("%02x ", m_rxbuf[i] & 0x0ff);
336
                printf("\n");
337
                */
338
                push_fifo(m_rxbuf, actual_length);
339
        } else if (r == -7) {
340
                // Nothing to read in the timeout provided
341
                // Return, adding nothing to our FIFO
342
        } else {
343
                printf("Some error took place in receiving\n");
344
                perror("O/S Err");
345
        }
346
 
347
        // fprintf(stderr, "\tUSBI::RAW-READ() -- COMPLETE (%d avail)\n",
348
                // (m_rbeg-m_rend)&(RCV_BUFMASK));
349
}
350
 
351
void    USBI::flush_read(void) {
352
        while(poll(4)) {
353
                m_rbeg = m_rend = 0;
354
        }
355
}
356
 
357
void    USBI::push_fifo(char *buf, int len) {
358
        char    last = 0;
359
        char    *sptr = buf;
360
 
361
        // printf("Pushing %d items onto FIFO (%d - %d)\n", len, m_rend, m_rbeg);
362
        if (m_rbeg != m_rend)
363
                last = m_rbuf[m_rend];
364
        for(int i=0; i<len; i++) {
365
                char v = *sptr++;
366
                if (((v & 0x80)||((unsigned char)v < 0x10))&&(v == last)) {
367
                        // printf("\tSkipping: %02x\n", v & 0x0ff);
368
                } else if ((unsigned char)v == 0x0ff) {
369
                } else {
370
                        m_rbuf[m_rbeg] = v;
371
                        m_rbeg = (m_rbeg+1)&(RCV_BUFMASK);
372
                        // printf("\tPushing: %02x\n", v & 0x0ff);
373
                } last = v;
374
        }
375
}
376
 
377
int     USBI::pop_fifo(char *buf, int len) {
378
        int     avail = (m_rbeg - m_rend)&(RCV_BUFMASK);
379
        int     left = len;
380
        int     nr = 0;
381
 
382
        // printf("Attempting to pop %d items from FIFO (%d - %d)\n",
383 41 dgisselq
        //              len, m_rend, m_rbeg);
384 5 dgisselq
        while((avail > 0)&&(left > 0)) {
385
                int ln = RCV_BUFLEN-m_rend;
386
                if (ln > left)
387
                        ln = left;
388
                if (ln > avail)
389
                        ln = avail;
390
                memcpy(&buf[len-left], &m_rbuf[m_rend], ln);
391
                left   -= ln;
392
                avail  -= ln;
393
                m_rend  = (m_rend + ln)&(RCV_BUFMASK);
394
                nr     += ln;
395
 
396 41 dgisselq
        //      printf("P:");
397
        //      for(int i=0; i<ln; i++)
398
        //              printf("%02x ", buf[len-left-ln+i]);
399
        } // printf("\n");
400
 
401 5 dgisselq
        /*
402
        if (nr > 0)
403
                printf("\tPopped %d items, buf[0] = %02x (%d - %d)\n",
404
                        nr, buf[0], m_rend, m_rbeg);
405
        else
406
                printf("\tPopped nothing, %d - %d\n", m_rend, m_rbeg);
407
        */
408 13 dgisselq
 
409 5 dgisselq
        return nr;
410
}
411
 
412
bool    USBI::poll(unsigned ms) {
413
        int     avail = (m_rbeg-m_rend)&(RCV_BUFMASK);
414
        bool    r = true;
415
 
416
        // printf("POLL request\n");
417
 
418
        if ((avail < 2)&&((avail<1)||(m_rbuf[m_rend]&0x80)||(m_rbuf[m_rend]<0x10))) {
419
                raw_read(4,ms);
420
                avail = (m_rbeg-m_rend)&(RCV_BUFMASK);
421
                // printf("%d availabe\n", avail);
422
 
423
                char    v = (m_rbuf[(m_rbeg-1)&(RCV_BUFMASK)]);
424
                while(((v&0x80)==0)&&((unsigned)v>=0x10)&&(avail < RCV_BUFMASK-32)) {
425
                        raw_read(26,ms);
426
                        avail = (m_rbeg-m_rend)&(RCV_BUFMASK);
427
                }
428
                if (avail < 1)
429
                        r = false;
430
                else if ((avail==1)&&((m_rbuf[m_rend]&0x80)||(m_rbuf[m_rend]<0x10)))
431
                        r = false;
432
        }
433
 
434
        // printf("USBI::poll() -> %s (%d avail)\n", (r)?"true":"false", avail);
435
        return r;
436
}

powered by: WebSVN 2.1.0

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