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_libusb.c] - Blame information for rev 38

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

Line No. Rev Author Line
1 38 alirezamon
#include <stdio.h>
2
#include <stdlib.h>
3
#include <ctype.h>
4
#include <stdint.h>
5
#include <unistd.h> // getopt
6
#include <inttypes.h>
7
#include <string.h>
8
#include "jtag.h"
9
 
10
/* You may want to change the VENDOR_ID and PRODUCT_ID
11
 * depending on your device.
12
 */
13
#define VENDOR_ID      0x09fb   // Altera
14
#define PRODUCT_ID     0x6001   // usb blaster (DE2-115) 
15
// Altera usb blaster  product IDs "6001", "6002", "6003", MODE="0666"   
16
// dose not work for USB-Blaster II "6010", "6810"
17
// run ./list_usb_dev  to see the list of all usb devices' vid and pid
18
 
19
unsigned usb_blaster_id = PRODUCT_ID;
20
 
21
 
22
#define UPDATE_WB_ADDR  0x7
23
#define UPDATE_WB_WR_DATA  0x6
24
#define UPDATE_WB_RD_DATA  0x5
25
#define RD_WR_STATUS    0x4
26
 
27
#define BIT_NUM         (word_width<<3) 
28
#define BYTE_NUM         word_width     
29
/* Global vars */
30
unsigned int index_num=126;
31
unsigned int word_width=4; // 
32
unsigned int write_verify=0;
33
unsigned int memory_offset=0;
34
unsigned int memory_boundary=0xFFFFFFFF;
35
 
36
 
37
 
38
char * binary_file_name=0;
39
char enable_binary_send=0;
40
char enable_binary_read=0;
41
char * write_data=0;
42
 
43
 
44
 
45
 
46
/* functions */
47
int send_binary_file();
48
int read_mem();
49
void usage();
50
void processArgs (int , char** );
51
int send_data ();
52
int hexcut( char *  , unsigned *  , int  );
53
int vdr_large (unsigned , char * , char *);
54
void hexgen( char * , unsigned *, int );
55
 
56
int main(int argc, char **argv) {
57
        //unsigned bits;
58
        //unsigned int val;
59
 
60
        //unsigned bits;
61
        //unsigned val;
62
 
63
        processArgs (argc, argv );
64
        printf("index num=%u\n",index_num);
65
        if (jtag_open_virtual_device(index_num,VENDOR_ID,usb_blaster_id)){
66
                fprintf (stderr, "Error openning jtag IP with %d index num\n",index_num);
67
                return -1;
68
        }
69
        if (enable_binary_send) {
70
                if( send_binary_file() == -1) return -1;
71
        }
72
 
73
        if (write_data!=0){
74
                printf("send %s to jtag\n",write_data);
75
                send_data();
76
 
77
 
78
        }
79
 
80
        return 0;
81
}
82
 
83
 
84
 
85
void usage(){
86
 
87
        printf ("usage:./jtag_main [-n  index number] [-i file_name][-c][-s rd/wr offset address][-d string]\n");
88
        printf ("\t-a   product_id in hex: Altera usb blasterI  product IDs \"6001\", \"6002\", \"6003\" . The default value is 0x6001\n");
89
        printf ("\t-n   index number: the target jtag IP core index number. The default number is 126\n");
90
        printf ("\t-i   file_name:  input binary file name (.bin file)\n");
91
        printf ("\t-r   read memory content and display in terminal\n");
92
        printf ("\t-w   bin file word width in byte. default is 4 bytes (32 bits)\n");
93
        printf ("\t-c   verify after write\n");
94
        printf ("\t-s   memory wr/rd offset address in byte (hex format). The default value is 0x0000000\n");
95
        printf ("\t-e   memory  boundary address in byte (hex format).  The default value is 0xFFFFFFFF\n");
96
        printf ("\t-d   string: use for setting instruction or data value to jtag tap.  string format : \"instr1,instr2,...,instrn\"\n \tinstri = I:instruct_num: send instruct_num to instruction register \n \tD:data_size_in_bit:data : send data in hex to data register\n  \tR:data_size_in_bit:data : Read data register and show it on screan then write given data in hex to data register\n");
97
 
98
}
99
 
100
void processArgs (int argc, char **argv )
101
{
102
   char c;
103
int p;
104
 
105
   /* don't want getopt to moan - I can do that just fine thanks! */
106
   opterr = 0;
107
   if (argc < 2)  usage();
108
   while ((c = getopt (argc, argv, "s:a:e:d:n:i:w:cr")) != -1)
109
      {
110
         switch (c)
111
            {
112
            case 'a':
113
                p=sscanf(optarg,"%x",&usb_blaster_id);
114
                if( p==0){
115
                         fprintf (stderr, "invalid usb_blaster_id format `%s'.\n", optarg);
116
                         usage();
117
                         exit(1);
118
                }else if(usb_blaster_id == 0x6010 ||  usb_blaster_id ==0x6810 ){
119
                         fprintf (stderr, "%x is a usbblasterII id which is not supported. Please use jtag_quartus_stp for usbblaster II\n", usb_blaster_id);
120
                         usage();
121
                         exit(1);
122
                }
123
 
124
            case 'n':   /* index number */
125
               index_num = atoi(optarg);
126
               break;
127
            case 'i':   /* input binary file name */
128
                binary_file_name = optarg;
129
                enable_binary_send=1;
130
                break;
131
             case 'r':  /* read memory */
132
                enable_binary_read=1;
133
                break;
134
            case 'w':   /* word width in byte */
135
                word_width= atoi(optarg);
136
                break;
137
            case 'c':   /* enable write verify */
138
                write_verify= 1;
139
                break;
140
            case 'd':   /* send string */
141
                write_data= optarg;
142
                break;
143
            case 's':   /* set offset address*/
144
 
145
                p=sscanf(optarg,"%x",&memory_offset);
146
                if( p==0){
147
                         fprintf (stderr, "invalid memory offset adress format `%s'.\n", optarg);
148
                         usage();
149
                         exit(1);
150
                }
151
                //printf("p=%d,memory_offset=%x\n",p,memory_offset);            
152
                break;
153
            case 'e':   /* word width in byte */
154
                p=sscanf(optarg,"%x",&memory_boundary);
155
                if( p==0){
156
                         fprintf (stderr, "invalid memory boundary adress format `%s'.\n", optarg);
157
                         usage();
158
                         exit(1);
159
                }
160
                break;
161
 
162
            case '?':
163
               if (isprint (optopt))
164
                  fprintf (stderr, "Unknown option `-%c'.\n", optopt);
165
               else
166
                  fprintf (stderr,
167
                           "Unknown option character `\\x%x'.\n",
168
                           optopt);
169
            default:
170
               usage();
171
               exit(1);
172
            }
173
      }
174
}
175
 
176
unsigned * read_file (FILE * fp, unsigned int  * n ){
177
 
178
        unsigned * buffer;
179
        unsigned val;
180
        unsigned char ch;
181
        unsigned int i=0;
182
        char cnt=0;
183
        unsigned int num=0;
184
        unsigned int width= (BYTE_NUM < sizeof(unsigned )) ? BYTE_NUM :  sizeof(unsigned ); //max is 4 then
185
        fseek(fp, 0, SEEK_END); // seek to end of file
186
        num = ftell(fp); // get current file pointer
187
        *n=num;// number of bytes from the beginning of the file        
188
 
189
 
190
        num=(num/width)+2;
191
        fseek(fp, 0, SEEK_SET);
192
        //printf ("num=%u\n",num);      
193
        buffer = (unsigned *) malloc(num * sizeof(unsigned ) );  //memory allocated using malloc
194
        if(buffer == NULL)
195
        {
196
                printf("Error! memory not allocated.");
197
                exit(0);
198
        }
199
        ch=fgetc(fp);
200
 
201
        while(!feof(fp)){
202
                val<<=8;
203
                val|=ch;
204
                cnt++;
205
                //printf("ch=%x\t",ch);
206
                if(cnt==width){
207
                        //printf("%d:%x\n",i,val);
208
                        buffer[i] = val;
209
                        val=0;
210
                        cnt=0;
211
                        i++;
212
                }
213
                ch=fgetc(fp);
214
        }
215
        if( cnt>0){
216
                val<<=(8 *(width-cnt));
217
                printf("%d:%x\n",i,val);
218
                buffer[i] = val;
219
 
220
        }
221
 
222
return buffer;
223
 
224
}
225
 
226
 
227
 
228
int send_data ()
229
{
230
        char * pch;
231
        char string[100];
232
        int bit=0,  inst=0, d=0;
233
        char out[100];
234
        pch = strtok (write_data,",");
235
        printf("%s\n",pch);
236
        while (pch != NULL)
237
        {
238
                while(1){
239
                         d=1;
240
                        if(sscanf( pch, "D:%d:%s", &bit, string )) break;
241
                        if(sscanf( pch, "d:%d:%s", &bit, string )) break;
242
                        //if(sscanf( pch, "D:%d:" PRIx64  , &bit, &data )) break;
243
                        //if(sscanf( pch, "d:%d:%016x", &bit, &data )) break;
244
                         d=2;
245
                        if(sscanf( pch, "R:%d:%s",&bit, string)) break;
246
                        if(sscanf( pch, "r:%d:%s",&bit, string)) break;
247
                         d=0;
248
                        if(sscanf( pch, "I:%d", &inst)) break;
249
                        if(sscanf( pch, "i:%d", &inst)) break;
250
                        printf("invalid format : %s\n",pch);
251
                        return -1;
252
 
253
                }
254
                if(d==1){
255
                        //printf ("(bit=%d, data=%s)",bit, string);
256
                        //jtag_vdr(bit, data, 0);
257
                        vdr_large(bit,string,0);
258
                }if(d==2){
259
 
260
                        vdr_large(bit,string,out);
261
                        printf("###read data#%s###read data#\n",out);
262
                }else{
263
 
264
                        jtag_vir(inst);
265
                        //printf("%d\n",inst);
266
                }
267
 
268
                pch = strtok (NULL, ",");
269
 
270
        }
271
  return 0;
272
}
273
 
274
int compare_values( unsigned * val1, unsigned * val2, int words, unsigned int address){
275
 
276
        int i,error=0;
277
        for(i=0;i<words;i++){
278
                if (val1[i] != val2[i]) error=1;
279
        }
280
        if(error){
281
                 printf ("Error: missmatched at location %d. Expected 0X",address);
282
                 for(i=0;i<words;i++) printf("%08X",val1[i] );
283
                 printf (" but read 0X");
284
                 for(i=0;i<words;i++) printf("%08X",val2[i] );
285
                 printf ("\n");
286
 
287
        }
288
        return error;
289
 
290
 
291
}
292
 
293
void reorder_buffer(unsigned * buff, unsigned int words){
294
        unsigned tmp;
295
        unsigned int i;
296
        for(i=0;i<words/2;i++){
297
                tmp= buff[i];
298
                buff[i]=buff[i+words-1];
299
                buff[i+words-1]=tmp;
300
        }
301
}
302
 
303
 
304
 
305
 
306
int send_binary_file(){
307
        FILE *fp;
308
        int i=0;
309
        unsigned out;
310
        unsigned int file_size=0;
311
        unsigned int num=0;
312
        unsigned int mem_size;
313
        unsigned int memory_offset_in_word;
314
        unsigned * small_buff;
315
        int words= (BYTE_NUM % sizeof(unsigned )) ? (BYTE_NUM / sizeof(unsigned ) )+1 : (BYTE_NUM / sizeof(unsigned ));
316
 
317
        small_buff = (unsigned *) malloc(words * sizeof(unsigned ) );
318
        unsigned *  read_buff;
319
        read_buff  = (unsigned *) calloc(words , sizeof(unsigned ) );
320
 
321
 
322
        printf("send %s to the wishbone bus\n",binary_file_name);
323
        fp = fopen(binary_file_name,"rb");
324
        if (!fp) {
325
                fprintf (stderr,"Error: can not open %s file in read mode\n",binary_file_name);
326
                return -1;
327
        }
328
        unsigned * buffer;
329
        buffer=read_file (fp, &file_size);
330
        mem_size=memory_boundary-memory_offset;
331
        if(file_size>mem_size){
332
                printf("\n\n Warning:  %s file size (%x) is larger than the given memory size (%x). I will stop writing on end of memory address\n\n",binary_file_name,file_size,mem_size);
333
                file_size=mem_size;
334
        }
335
        fclose(fp);
336
        //disable the cpu
337
        jtag_vir(RD_WR_STATUS);
338
        jtag_vdr(BIT_NUM, 0x1, &out);
339
        //getchar();
340
        jtag_vir(UPDATE_WB_ADDR);
341
        // change memory sizes from byte to word        
342
        memory_offset_in_word=memory_offset /BYTE_NUM;
343
        //size of buffer
344
        num= (BYTE_NUM < sizeof(unsigned )) ? file_size /BYTE_NUM : file_size /sizeof(unsigned );
345
 
346
        jtag_vdr(BIT_NUM, memory_offset_in_word, 0);
347
        jtag_vir(UPDATE_WB_WR_DATA);
348
 
349
        printf ("start programing\n");
350
        //printf ("num=%d\n",num);
351
        for(i=0;i<num;i++){
352
                //printf("%d:%x\n",i,*buffer +i);
353
                if(BYTE_NUM <= sizeof(unsigned )){
354
                        jtag_vdr(BIT_NUM, buffer[i], 0);
355
                }else {
356
                        reorder_buffer(&buffer[i],words);
357
                        jtag_vdr_long(BIT_NUM, &buffer[i], 0, words);
358
                        i+= (words-1);
359
 
360
                }
361
 
362
 
363
        }
364
 
365
        //printf ("done programing\n");
366
        if(write_verify){
367
                if(!(fp = fopen(binary_file_name,"rb"))){
368
                        fprintf (stderr,"Error: can not open %s file in read mode\n",binary_file_name);
369
                        return -1;
370
                }
371
                buffer=read_file (fp, &file_size);
372
 
373
 
374
 
375
                //fclose(fp);
376
                jtag_vir(UPDATE_WB_RD_DATA);
377
                jtag_vdr(BIT_NUM, memory_offset_in_word+0, &out);
378
 
379
 
380
 
381
                if(BYTE_NUM <= sizeof(unsigned )){
382
                        for(i=1;i<=num; i++){
383
                                jtag_vdr(BIT_NUM, memory_offset_in_word+i, &out);
384
                                if(out!=buffer[i-1]) printf ("Error: missmatched at location %d. Expected %x but read %x\n",i-1,buffer[i-1], out);
385
                        }
386
                }
387
                else{
388
                        for(i=words;i<=num; i+=words){
389
                                read_buff[0]= memory_offset_in_word+i/words;
390
                                jtag_vdr_long(BIT_NUM, read_buff, small_buff, words);
391
                                reorder_buffer(&buffer[i-words],words);
392
                                compare_values(&buffer[i-words],small_buff,words,i/words-1);
393
 
394
                        }
395
 
396
                }
397
                printf ("write is verified\n");
398
 
399
        }
400
        //enable the cpu
401
        jtag_vir(RD_WR_STATUS);
402
        jtag_vdr(BIT_NUM, 0, &out);
403
        //printf ("status=%x\n",out);
404
        free(buffer);
405
        return 0;
406
}
407
 
408
void print_values( unsigned * val2, int words){
409
                 int i;
410
                 for(i=0;i<words;i++) printf("%08X",val2[words-i-1] );
411
                 printf ("\n");
412
}
413
 
414
 
415
 
416
int read_mem(){
417
        int i=0;
418
        unsigned int num=0;
419
        unsigned int mem_size;
420
        unsigned int memory_offset_in_word;
421
        unsigned out;
422
        unsigned * small_buff;
423
        int words= (BYTE_NUM % sizeof(unsigned )) ? (BYTE_NUM / sizeof(unsigned ) )+1 : (BYTE_NUM / sizeof(unsigned ));
424
 
425
        small_buff = (unsigned *) malloc(words * sizeof(unsigned ) );
426
        unsigned *  read_buff;
427
        read_buff  = (unsigned *) calloc(words , sizeof(unsigned ) );
428
        memory_offset_in_word=memory_offset /BYTE_NUM;
429
        mem_size=memory_boundary-memory_offset;
430
        num= (BYTE_NUM < sizeof(unsigned )) ? mem_size /BYTE_NUM : mem_size /sizeof(unsigned );
431
 
432
        jtag_vir(UPDATE_WB_RD_DATA);
433
        jtag_vdr(BIT_NUM, memory_offset_in_word+0, &out);
434
 
435
        printf("\n###read data#\n");
436
 
437
        if(BYTE_NUM <= sizeof(unsigned )){
438
                for(i=1;i<=num; i++){
439
                        jtag_vdr(BIT_NUM, memory_offset_in_word+i, &out);
440
                        printf("%X\n",out);
441
                }
442
        }
443
        else{
444
                for(i=words;i<=num; i+=words){
445
                        read_buff[0]= memory_offset_in_word+i/words;
446
                        jtag_vdr_long(BIT_NUM, read_buff, small_buff, words);
447
                        print_values(small_buff, words);
448
 
449
                }
450
 
451
        }
452
 
453
 
454
 
455
        printf("\n###read data#\n");
456
 
457
        //enable the cpu
458
        jtag_vir(RD_WR_STATUS);
459
        jtag_vdr(BIT_NUM, 0, &out);
460
        //printf ("status=%x\n",out);
461
        free(read_buff);
462
        return 0;
463
}
464
 
465
 
466
 
467
int vdr_large (unsigned sz, char * string, char *out){
468
        int words= (sz%32)? (sz/32)+1 : sz/32;
469
        unsigned  val[64],val_o[64];
470
        //printf("data=%s\n",string);
471
        hexcut(string, val, words );
472
 
473
 
474
        if( out == 0) return  jtag_vdr_long(sz,val,0,words);
475
        int u=  jtag_vdr_long(sz,val,val_o,words);
476
 
477
        hexgen( out, val_o, words );
478
 
479
        return u;
480
 
481
}
482
 
483
 
484
 
485
int hexcut( char * hexstring, unsigned * val, int words ){
486
    size_t count = 0;
487
    int start;
488
 
489
    if (*(hexstring+1)=='x' || *(hexstring+1)=='X') hexstring+=2;
490
    int size=strlen(hexstring);
491
    int hexnum= (size%8)? (size/8)+1 : size/8;
492
    for(count = 0; count < words; count++) val[count]=0;
493
 
494
    for(count = 1; count <= hexnum; count++) {
495
        start=(count*8>size)? 0 : size-count*8;
496
 
497
        sscanf(hexstring+start, "%08x", &val[count-1]);
498
        *(hexstring+start)=0;
499
    }
500
 
501
  //  printf("size=%d, hexnum=%u\n",size,hexnum);
502
 
503
 
504
    return hexnum;
505
}
506
 
507
 
508
void hexgen( char * hexstring, unsigned * val, int words ){
509
    size_t count = 0;
510
    sprintf(hexstring,"0x");
511
    for(count = 0; count < words; count++) {
512
        if(count == 0)  sprintf((hexstring+2),"%x",val[words-count-1]);
513
        else            sprintf(hexstring,"%08x",val[words-count-1]);
514
         hexstring+=strlen(hexstring);
515
   }
516
 
517
 // return hexnum;
518
}
519
 
520
 

powered by: WebSVN 2.1.0

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