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

Subversion Repositories xulalx25soc

[/] [xulalx25soc/] [trunk/] [sw/] [usbtst.cpp] - Blame information for rev 116

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

Line No. Rev Author Line
1 5 dgisselq
#include <stdio.h>
2
#include <stdlib.h>
3
#include <unistd.h>
4
#include <string.h>
5
#include <ctype.h>
6
// #include <usb.h>
7
 
8
#include <libusb.h>
9
 
10
// /sys/bus/usb/devices/1-4/ is our device, as currently plugged in
11
//
12
// It supports
13
//      1 configuration
14
//      1 interface
15
// and has a product string of...
16
//      "XuLA - XESS Micro Logic Array"
17
// 
18
#define VENDOR_ID       0x04d8
19
#define PRODUCT_ID      0x0ff8c
20
#define XESS_ENDPOINT_OUT       0x01
21
#define XESS_ENDPOINT_IN        0x81
22
//
23
#define JTAG_CMD        0x4f
24
#define GET_TDO_MASK    0x01
25
#define PUT_TMS_MASK    0x02
26
#define TMS_VAL_MASK    0x04
27
#define PUT_TDI_MASK    0x08
28
#define TDI_VAL_MASK    0x10
29
//
30
#define USER1_INSTR     0x02    // a SIX bit two
31
 
32
 
33
bool    gbl_transfer_received = false;
34
 
35
// libusbtransfer_cb_fn callback;
36
extern "C" {
37
void    my_callback(libusb_transfer *tfr) {
38
        gbl_transfer_received = true;
39
 
40
        printf("Callback received!\n");
41
}
42
}
43
 
44
// All messages must be 32 bytes or less
45
//
46
 
47
 
48
// Walk us through the JTAG Chain:
49
//      5-1's to go to test/reset
50
//      0        to go to Run-Test/Idle
51
//      1       to go to select-dr-scan
52
//      1       to go to select-ir-scan
53
//      0        to go to capture-ir
54
//      0        to go to shift-ir
55
//      (6-bit code 0x02 through TDI to IR, while sending 0-bits to TMS)
56
//      1       to leave shift IR and go to exit1-ir
57
//      1       to go to update-ir
58
//      1       to go to select-dr-scan
59
//      0        to go to capture-dr
60
//      0        to go to shift-dr
61
#define RESET_JTAG_LEN  12
62
const char      RESET_TO_USER_DR[RESET_JTAG_LEN] = {
63
        JTAG_CMD,
64
        21, // clocks
65
        0,0,0,     // Also clocks, higher order bits
66
        PUT_TMS_MASK | PUT_TDI_MASK, // flags
67
        (char)(0x0df),  // TMS: Five ones, then one zero, and two ones -- low bits first
68
        0x00,   // TDI: irrelevant here
69
        (char)(0x80),   // TMS: two zeros, then six zeros
70
        0x08,   // TDI: user command #1, bit reversed
71
        0x03,   // TMS: three ones, then two zeros
72
        0x00    // TDI byte -- irrelevant here
73
        //
74
        // 0xc0, // TDI byte -- user command #2
75
        // 0x40,        // TDI: user command #1
76
        // 0x0c, // TDI byte -- user command #2, bit reversed
77
};
78
 
79
//
80
//      TMS: 
81
/*
82
#define TX_DR_LEN       12
83
const   char    TX_DR_BITS[TX_DR_LEN] = {
84
        JTAG_CMD,
85
        48, // clocks
86
        0,0,0,  // Also clocks, higher order bits
87
        PUT_TDI_MASK, // flags
88
        (char)0x0ff, 0, 0, 0, 0, 0      // Six data bytes
89
        // module_id = 255
90
        // 32'h(payload.length)
91
        // payload
92
        // module_id + payload.len + num_result_bits, length=32 + payload ???
93
};
94
*/
95
 
96
//
97
//      TMS: 
98
//      
99
#define REQ_RX_LEN      6
100
const   char    REQ_RX_BITS[REQ_RX_LEN] = {
101
        JTAG_CMD,
102
        (char)((32-6)*8), // bits-requested
103
        0,0,0,     // Also clocks, higher order bits
104
        GET_TDO_MASK|TDI_VAL_MASK, // flags:TDI is kept low here, so no TDI flag
105
        // No data given, since there's no info to send or receive
106
        // Leave the result in shift-DR mode
107
};
108
 
109
#define RETURN_TO_RESET_LEN     7
110
const char      RETURN_TO_RESET[RETURN_TO_RESET_LEN] = {
111
        JTAG_CMD,
112
        5, // clocks
113
        0,0,0,     // Also clocks, higher order bits
114
        PUT_TMS_MASK, // flags
115
        (char)(0x0ff), // Five ones
116
};
117
 
118
int     dec(int v) {
119
        int br = 0;
120
 
121
        /*
122
        br = (br<<1)|(v&1); v>>=1;
123
        br = (br<<1)|(v&1); v>>=1;
124
        br = (br<<1)|(v&1); v>>=1;
125
        br = (br<<1)|(v&1); v>>=1;
126
 
127
        br = (br<<1)|(v&1); v>>=1;
128
        br = (br<<1)|(v&1); v>>=1;
129
        br = (br<<1)|(v&1); v>>=1;
130
        br = (br<<1)|(v&1); v>>=1;
131
        */
132
        br = v&0x07f;
133
 
134
        if (br == ' ')
135
                return br;
136
        else if (isgraph(br))
137
                return br;
138
        else
139
                return '.';
140
}
141
 
142
//
143
// If max packet length is 32, why do you waste 4 bytes on num_clocks?
144
// Why is the bit counter always from 8 to zero?
145
//
146
 
147
int main(int argc, char **argv) {
148
        libusb_context          *usb_context;
149
        libusb_device_handle    *xula_usb_device;
150
        int     config;
151
 
152
        if (0 != libusb_init(&usb_context)) {
153
                fprintf(stderr, "Error initializing the USB library\n");
154
                perror("O/S Err:");
155
                exit(-1);
156
        }
157
 
158
        xula_usb_device = libusb_open_device_with_vid_pid(usb_context,
159
                VENDOR_ID, PRODUCT_ID);
160
        if (!xula_usb_device) {
161
                fprintf(stderr, "Could not open XuLA device\n");
162
                perror("O/S Err:");
163
 
164
                libusb_exit(usb_context);
165
                exit(-1);
166
        }
167
 
168
        if (0 != libusb_get_configuration(xula_usb_device, &config)) {
169
                fprintf(stderr, "Could not get configuration\n");
170
                perror("O/S Err:");
171
 
172
                libusb_close(xula_usb_device);
173
                libusb_exit(usb_context);
174
                exit(-1);
175
        }
176
 
177
        printf("Current configuration is %d\n", config);
178
        int     interface = 0;
179
 
180
        if (0 != libusb_claim_interface(xula_usb_device, interface)) {
181
                fprintf(stderr, "Could not claim interface\n");
182
                perror("O/S Err:");
183
 
184
                libusb_close(xula_usb_device);
185
                libusb_exit(usb_context);
186
                exit(-1);
187
        }
188
 
189
        unsigned char   *abuf = new unsigned char[32];
190
        int     actual_length = 32;
191
        memcpy(abuf, RESET_TO_USER_DR, RESET_JTAG_LEN);
192
        int r = libusb_bulk_transfer(xula_usb_device, XESS_ENDPOINT_OUT,
193
                abuf, RESET_JTAG_LEN, &actual_length, 20);
194
        if ((r==0)&&(actual_length == RESET_JTAG_LEN)) {
195
                printf("Successfully sent RESET_TO_USER_DR!\n");
196
        } else {
197
                printf("Some error took place requesting RESET_TO_USER_DR\n");
198
                perror("O/S Err");
199
        }
200
 
201
        memset(abuf, 0, 32);
202
        memcpy(abuf, REQ_RX_BITS, REQ_RX_LEN);
203
        r = libusb_bulk_transfer(xula_usb_device, XESS_ENDPOINT_OUT,
204
                abuf, REQ_RX_LEN, &actual_length, 20);
205
        if ((r==0)&&(actual_length == REQ_RX_LEN)) {
206
                printf("Successfully sent request for TDO bits!\n");
207
        } else {
208
                printf("Some error took place in requesting TDO bits\n");
209
                printf("r = %d, actual_length = %d (!= %d)\n", r,
210
                        actual_length, REQ_RX_LEN);
211
                perror("O/S Err");
212
        }
213
 
214
        r = libusb_bulk_transfer(xula_usb_device, XESS_ENDPOINT_IN,
215
                abuf, 32, &actual_length, 20);
216
        if ((r==0)&&(actual_length > 0)) {
217
                printf("Successfully read %d bytes from port!\n", actual_length);
218
                for(int i=0; i<(actual_length); i+=4)
219
                        printf("%2d: %02x %02x %02x %02x -- %c%c%c%c\n", i,
220
                                abuf[i+0], abuf[i+1], abuf[i+2], abuf[i+3],
221
                                dec(abuf[i+0]),dec(abuf[i+1]), dec(abuf[i+2]), dec(abuf[i+3]));
222
        } else {
223
                printf("Some error took place in receiving\n");
224
                perror("O/S Err");
225
        }
226
 
227
 
228
        // Release our interface
229
        if (0 != libusb_release_interface(xula_usb_device, interface)) {
230
                fprintf(stderr, "Could not release interface\n");
231
                perror("O/S Err:");
232
 
233
                libusb_close(xula_usb_device);
234
                libusb_exit(usb_context);
235
                exit(-1);
236
        }
237
 
238
        // And then close our device with
239
        libusb_close(xula_usb_device);
240
 
241
        // And just before exiting, we free our USB context
242
        libusb_exit(usb_context);
243
}
244
 

powered by: WebSVN 2.1.0

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