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

Subversion Repositories xulalx25soc

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

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
                nr = pop_fifo(buf, left);
275
                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
        int r = libusb_bulk_transfer(m_xula_usb_device, XESS_ENDPOINT_OUT,
298
                (unsigned char *)m_txbuf, REQ_RX_LEN, &actual_length,
299
                timeout_ms);
300
        if ((r==0)&&(actual_length == REQ_RX_LEN)) {
301
        } else if (r == -7) {
302
                // Nothing to read in the timeout provided
303
                // We'll have to request this data again ... later
304
                return;
305
        } else {
306
                printf("READ(WRITE,READ-REQ) -- ERR\n");
307
                printf("r = %d, actual_length = %d (!= %d requested)\n", r,
308
                        actual_length, len+6);
309
                perror("O/S Err");
310
                exit(-2);
311
        }
312
 
313
        // Try to read back however many bytes we can
314
        r = libusb_bulk_transfer(m_xula_usb_device, XESS_ENDPOINT_IN,
315
                (unsigned char *)m_rxbuf, USB_PKTLEN, &actual_length, 0);
316
        if ((r==0)&&(actual_length > 0)) {
317
                /*
318
                printf("RAW-READ() -> %d Read\n", actual_length);
319
                for(int i=0; i<actual_length; i++)
320
                        printf("%02x ", m_rxbuf[i] & 0x0ff);
321
                printf("\n");
322
                */
323
                push_fifo(m_rxbuf, actual_length);
324
        } else if (r == -7) {
325
                // Nothing to read in the timeout provided
326
                // Return, adding nothing to our FIFO
327
        } else {
328
                printf("Some error took place in receiving\n");
329
                perror("O/S Err");
330
        }
331
 
332
        // fprintf(stderr, "\tUSBI::RAW-READ() -- COMPLETE (%d avail)\n",
333
                // (m_rbeg-m_rend)&(RCV_BUFMASK));
334
}
335
 
336
void    USBI::flush_read(void) {
337
        while(poll(4)) {
338
                m_rbeg = m_rend = 0;
339
        }
340
}
341
 
342
void    USBI::push_fifo(char *buf, int len) {
343
        char    last = 0;
344
        char    *sptr = buf;
345
 
346
        // printf("Pushing %d items onto FIFO (%d - %d)\n", len, m_rend, m_rbeg);
347
        if (m_rbeg != m_rend)
348
                last = m_rbuf[m_rend];
349
        for(int i=0; i<len; i++) {
350
                char v = *sptr++;
351
                if (((v & 0x80)||((unsigned char)v < 0x10))&&(v == last)) {
352
                        // printf("\tSkipping: %02x\n", v & 0x0ff);
353
                } else if ((unsigned char)v == 0x0ff) {
354
                } else {
355
                        m_rbuf[m_rbeg] = v;
356
                        m_rbeg = (m_rbeg+1)&(RCV_BUFMASK);
357
                        // printf("\tPushing: %02x\n", v & 0x0ff);
358
                } last = v;
359
        }
360
}
361
 
362
int     USBI::pop_fifo(char *buf, int len) {
363
        int     avail = (m_rbeg - m_rend)&(RCV_BUFMASK);
364
        int     left = len;
365
        int     nr = 0;
366
 
367
        // printf("Attempting to pop %d items from FIFO (%d - %d)\n",
368
                        // len, m_rend, m_rbeg);
369
        while((avail > 0)&&(left > 0)) {
370
                int ln = RCV_BUFLEN-m_rend;
371
                if (ln > left)
372
                        ln = left;
373
                if (ln > avail)
374
                        ln = avail;
375
                memcpy(&buf[len-left], &m_rbuf[m_rend], ln);
376
                left   -= ln;
377
                avail  -= ln;
378
                m_rend  = (m_rend + ln)&(RCV_BUFMASK);
379
                nr     += ln;
380
        }
381
 
382
        /*
383
        if (nr > 0)
384
                printf("\tPopped %d items, buf[0] = %02x (%d - %d)\n",
385
                        nr, buf[0], m_rend, m_rbeg);
386
        else
387
                printf("\tPopped nothing, %d - %d\n", m_rend, m_rbeg);
388
        */
389 13 dgisselq
 
390 5 dgisselq
        return nr;
391
}
392
 
393
bool    USBI::poll(unsigned ms) {
394
        int     avail = (m_rbeg-m_rend)&(RCV_BUFMASK);
395
        bool    r = true;
396
 
397
        // printf("POLL request\n");
398
 
399
        if ((avail < 2)&&((avail<1)||(m_rbuf[m_rend]&0x80)||(m_rbuf[m_rend]<0x10))) {
400
                raw_read(4,ms);
401
                avail = (m_rbeg-m_rend)&(RCV_BUFMASK);
402
                // printf("%d availabe\n", avail);
403
 
404
                char    v = (m_rbuf[(m_rbeg-1)&(RCV_BUFMASK)]);
405
                while(((v&0x80)==0)&&((unsigned)v>=0x10)&&(avail < RCV_BUFMASK-32)) {
406
                        raw_read(26,ms);
407
                        avail = (m_rbeg-m_rend)&(RCV_BUFMASK);
408
                }
409
                if (avail < 1)
410
                        r = false;
411
                else if ((avail==1)&&((m_rbuf[m_rend]&0x80)||(m_rbuf[m_rend]<0x10)))
412
                        r = false;
413
        }
414
 
415
        // printf("USBI::poll() -> %s (%d avail)\n", (r)?"true":"false", avail);
416
        return r;
417
}

powered by: WebSVN 2.1.0

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