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

Subversion Repositories xulalx25soc

[/] [xulalx25soc/] [trunk/] [sw/] [txtest.cpp] - Blame information for rev 30

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
        const char hello_world[] = "Hello, World!\n";
202
        abuf[0] = JTAG_CMD;
203
        abuf[1] = strlen(hello_world) * 8 + 8;
204
        abuf[2] = abuf[3] = abuf[4] = 0;
205
        abuf[5] = PUT_TDI_MASK | GET_TDO_MASK;
206
        strcpy((char *)&abuf[6], hello_world);
207
        // abuf[6] = 0xff;
208
        // abuf[7] = 0xff;
209
        // abuf[8] = 0x00;
210
        // abuf[9] = 0xff;
211
        // abuf[10] = 0x01;
212
        // abuf[11] = 0x02;
213
        // abuf[12] = 0x04;
214
        // abuf[13] = 0x08;
215
        r = libusb_bulk_transfer(xula_usb_device, XESS_ENDPOINT_OUT,
216
                abuf, strlen(hello_world)+6+1, &actual_length, 20);
217
        if ((r==0)&&(actual_length == strlen(hello_world)+6+1)) {
218
                printf("Successfully sent request for TDO bits!\n");
219
        } else {
220
                printf("Some error took place in requesting TDO bits\n");
221
                printf("r = %d, actual_length = %d (!= %d)\n", r,
222
                        actual_length, (int)strlen(hello_world)+6);
223
                perror("O/S Err");
224
        }
225
 
226
        r = libusb_bulk_transfer(xula_usb_device, XESS_ENDPOINT_IN,
227
                abuf, strlen(hello_world)+1, &actual_length, 20);
228
        if ((r==0)&&(actual_length > 0)) {
229
                printf("Successfully read %d bytes from port!\n", actual_length);
230
                for(int i=0; i<(actual_length); i+=4)
231
                        printf("%2d: %02x %02x %02x %02x -- %c%c%c%c\n", i,
232
                                abuf[i+0], abuf[i+1], abuf[i+2], abuf[i+3],
233
                                dec(abuf[i+0]),dec(abuf[i+1]), dec(abuf[i+2]), dec(abuf[i+3]));
234
        } else {
235
                printf("Some error took place in receiving\n");
236
                perror("O/S Err");
237
        }
238
 
239
 
240
        // Release our interface
241
        if (0 != libusb_release_interface(xula_usb_device, interface)) {
242
                fprintf(stderr, "Could not release interface\n");
243
                perror("O/S Err:");
244
 
245
                libusb_close(xula_usb_device);
246
                libusb_exit(usb_context);
247
                exit(-1);
248
        }
249
 
250
        // And then close our device with
251
        libusb_close(xula_usb_device);
252
 
253
        // And just before exiting, we free our USB context
254
        libusb_exit(usb_context);
255
}
256
 

powered by: WebSVN 2.1.0

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