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/] [uart_xsct_terminal/] [main.c] - Blame information for rev 48

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 48 alirezamon
#include <curses.h>
2
#include <unistd.h>
3
#include <stdlib.h>
4
#include <string.h>
5
#include <ctype.h>
6
#include <sys/types.h>
7
#include <sys/stat.h>
8
 
9
#include "../jtag_xilinx_xsct/jtag.h"
10
 
11
 
12
 
13
#define WIN_PER_COL 2
14
#define BUFF_WIDTH 100
15
 
16
#define ESCAPE 27
17
 
18
int win_col_size;
19
int win_line_size;
20
 
21
 
22
int uart_num=0;
23
int * index_nums;
24
WINDOW **win;
25
WINDOW * info;
26
int * win_x;
27
int * win_y;
28
char ** buffer;
29
char * outfile;
30
char write_log_file=0;
31
int  * buff_ptr;
32
 
33
 
34
 
35
void update_out_file(int n);
36
int get_jtag_indexs( char * index_str);
37
 
38
void usage(){
39
        printf ("usage:./uart  [-o output log file name] [-c jtag_chain_num] -n uart_index_number_string -a jtag_target_number -b jtag_shift_reg_size\n");
40
        printf ("\t-a   the order number of target device in jtag chain. Run jtag targets after \"connect\" command in xsct terminal to list all availble targets\n");
41
        printf ("\t-b   Jtag shiftreg data width. It should be the target device Data width + 4\n");
42
        printf ("\t-t   Jtag_chain number: the BSCANE2 tab number :1,2,3 or 4. The default is 3\n");
43
        printf ("\t-o   optinal output log file name. If file name is given the output from serial ports are wriiten to a file\n");
44
        printf ("\t-n   UART index numbers seprated by \",\": e.g: -n 126,125\n");
45
        exit(1);
46
}
47
 
48
void processArgs (int argc, char **argv )
49
{
50
        char c;
51
        int p;
52
 
53
   /* don't want getopt to moan - I can do that just fine thanks! */
54
   opterr = 0;
55
   if (argc < 2)  {
56
        usage();
57
 
58
   }
59
   while ((c = getopt (argc, argv, "n:o:a:b:t:")) != -1)
60
      {
61
         switch (c)
62
            {
63
            case 'n':   /* indexs */
64
               get_jtag_indexs(optarg);
65
               break;
66
            case 'o':   /* output file name */
67
                outfile=optarg;
68
                write_log_file=1;
69
                break;
70
             case 'a':  /* hardware_name */
71
               jtag_target_number = atoi(optarg);
72
               break;
73
            case 'b':   /* device number in chain */
74
               jtag_shift_reg_size = atoi(optarg);
75
               break;
76
             case 't':  /* Jtag_chain_num */
77
               chain_num = atoi(optarg);
78
               if (chain_num<1 || chain_num>4 ) {
79
                        fprintf (stderr, "Wrong jtag_chain_num the given %u value is out of valid range 1,2,3 or 4.\n\n", chain_num);
80
                        usage();
81
               }
82
               break;
83
 
84
 
85
            case '?':
86
               if (isprint (optopt))
87
                  fprintf (stderr, "Unknown option `-%c'.\n", optopt);
88
               else
89
                  fprintf (stderr,
90
                           "Unknown option character `\\x%x'.\n",
91
                           optopt);
92
            default:
93
               usage();
94
            }
95
      }
96
}
97
 
98
int get_jtag_indexs( char * index_str){
99
        printf ("%s\n",index_str);
100
        char delim[] = ",";
101
        char *ptr;
102
        int i;
103
        //count number of indexes
104
        ptr=index_str;
105
        uart_num=1;
106
        for (i=0;i<strlen(index_str);i++ ){
107
                if(ptr[0]==',') uart_num++;
108
                ptr++;
109
        }
110
        index_nums = (int*)malloc((uart_num+1)* sizeof(int));
111
        if (index_nums == NULL) {
112
                printf("Error index_nums[%u] could not be allocated.\n",uart_num);
113
                exit(1);
114
        }
115
 
116
        ptr = strtok(index_str, delim);
117
        i=0;
118
        while(ptr != NULL)
119
        {
120
 
121
                index_nums[i] = atoi(ptr);
122
                printf("index_nums[%u]=%u\n",i,index_nums[i]);
123
                ptr = strtok(NULL,delim);
124
                i++;
125
        }
126
 
127
        printf ("Uart_num =%u\n",uart_num);
128
        return 1;
129
 
130
}
131
 
132
 
133
void initial_windows (){
134
        FILE * fout;
135
        char tmp_str [30];
136
        win = (WINDOW**)malloc((uart_num+1)* sizeof(WINDOW *));
137
        win_x = (int *)malloc((uart_num+1)* sizeof(int));
138
        win_y = (int *)malloc((uart_num+1)* sizeof(int));
139
        if (win == NULL) {
140
                printf("Error *win[%u] could not be allocated.\n",uart_num);
141
                exit(1);
142
        }
143
 
144
        if(write_log_file){
145
                buffer = malloc((uart_num+1) * sizeof(char *));
146
                buff_ptr = (int *) malloc((uart_num+1) * sizeof(int ));
147
                for(int i = 0; i < uart_num+1; i++){
148
                        buffer[i] = malloc(BUFF_WIDTH * sizeof(char));
149
                        if(buffer[i]==NULL){
150
                                printf("Warning buffer could not be allocated. Write on output file is diabled\n");
151
                                write_log_file=0;
152
                        }
153
                }
154
 
155
                mkdir("TEMP_OUT", 0777);
156
                for(int i = 0; i < uart_num; i++){
157
                        sprintf(tmp_str,"TEMP_OUT/temp%u",i);
158
                        fout= fopen(tmp_str,"w");
159
                        if(fout ==NULL){
160
                                printf("Warning: could not creat %s file. Write on output file is diabled\n",tmp_str);
161
                                write_log_file=0;
162
                                return;
163
                        }
164
                        fprintf(fout,"UART%u: ",i);
165
                        fclose(fout);
166
                }
167
 
168
        }
169
 
170
 
171
 
172
 
173
        initscr();
174
        noecho();
175
        cbreak();
176
        nodelay(stdscr, TRUE);
177
        keypad(stdscr,TRUE);
178
        getch();
179
 
180
 
181
 
182
        int i;
183
        int tmp;
184
        int dev = (WIN_PER_COL<uart_num)? WIN_PER_COL :uart_num;
185
 
186
 
187
        win_col_size=(( COLS-1)/dev)-1;
188
        tmp = (uart_num%dev)?  (uart_num/dev)+1 : (uart_num/dev);
189
        win_line_size=((LINES-2)/tmp);
190
 
191
        int sline=0;
192
        int scol=0;
193
 
194
        for (i=0;i<uart_num;i++){
195
                win[i] = newwin(win_line_size, win_col_size, sline, scol);
196
                //scrollok(win[i], TRUE);
197
                scol=scol + win_col_size + 1;
198
                if(scol+ win_col_size >COLS){
199
                        scol=0;
200
                        sline=sline+win_line_size;
201
                }
202
 
203
                werase(win[i]);
204
                box( win[i], ACS_VLINE, ACS_HLINE );
205
                wmove(win[i],0, 0);
206
                wprintw(win[i],"UART%u(%u):",i,index_nums[i]);
207
                wmove(win[i],1, 1);
208
                win_x[i]=1;
209
                win_y[i]=1;
210
                wrefresh(win[i]);
211
        }
212
 
213
        info = newwin(1, (COLS-1), LINES-2, 0);
214
        werase(info);
215
        wprintw(info,"Press ESC to quit.");
216
        wrefresh(info);
217
}
218
 
219
 
220
 
221
 
222
void win_add_char(int n, char c){
223
        int j;
224
        wprintw(win[n],"%c",c);
225
        if(write_log_file){
226
                buffer[n][buff_ptr[n]]=c;
227
                buff_ptr[n]++;
228
                if(buff_ptr[n]==BUFF_WIDTH-1){
229
                        update_out_file(n);
230
                        buff_ptr[n]=0;
231
                }
232
        }
233
        if(c ==10){
234
                win_x[n]=win_col_size-1;
235
                box( win[n], ACS_VLINE, ACS_HLINE );
236
                wmove(win[n],0, 0);
237
                wprintw(win[n],"UART%u(%u):",n,index_nums[n]);
238
        }else{
239
                win_x[n]++;
240
 
241
        }
242
 
243
 
244
        if(win_x[n] == win_col_size-1){
245
                win_x[n]=1;
246
                win_y[n]++;
247
 
248
                if(win_y[n]==win_line_size-1){
249
                // we reached at the end of the win. strat from the begining
250
 
251
                        win_x[n]=1;
252
                        win_y[n]=1;
253
                        wmove(win[n],1,1);
254
 
255
                }
256
                // clear the next two lines
257
                if(win_y[n]<win_line_size-2){
258
                        wmove(win[n],win_y[n],1);
259
                        for (j=1;j<win_col_size-1;j++) wprintw(win[n]," ");
260
                        wmove(win[n],win_y[n]+1,1);
261
                        for (j=1;j<win_col_size-1;j++) wprintw(win[n]," ");
262
                }
263
 
264
                wmove(win[n],win_y[n],win_x[n]);
265
        }
266
 
267
        wrefresh(win[n]);
268
}
269
 
270
 
271
void update_out_file(int n){
272
        char tmp_str [30];
273
        FILE * fout;
274
        sprintf(tmp_str,"TEMP_OUT/temp%u",n);
275
        fout= fopen(tmp_str,"a");
276
        if (fout==NULL)
277
        {
278
                printf("Warning: could not creat %s file. Write on output file is diabled\n",tmp_str);
279
                write_log_file=0;
280
                return;
281
        }
282
 
283
        buffer[n][buff_ptr[n]]=0;
284
        fprintf(fout,"%s",buffer[n]);
285
        fclose(fout);
286
}
287
 
288
void merge_output_files (){
289
        int i;
290
        FILE * fout;
291
        FILE * fin;
292
        char tmp_str[30];
293
        char ch;
294
        fout= fopen(outfile,"w");
295
 
296
        if(fout ==NULL){
297
                printf("Warning: could not creat %s file. Write on output file is diabled\n",outfile);
298
                write_log_file=0;
299
                return;
300
        }
301
        for (i=0;i<uart_num;i++){
302
                sprintf(tmp_str,"TEMP_OUT/temp%u",i);
303
                fin= fopen(tmp_str,"r");
304
                while ((ch = fgetc(fin)) != EOF)  fputc(ch,fout);
305
                fclose(fin);
306
                fprintf(fout,"\n-------------------------------------------------------------------------------------------------\n");
307
        }
308
        fclose(fout);
309
}
310
 
311
 
312
 
313
void run_jtag_scaner(){
314
        int i;
315
        int index;
316
        char send_char=0; // not added yet. needed to be taken from the user     
317
        unsigned out;
318
 
319
        for (i=0;i<uart_num;i++){
320
                index= index_nums[i];
321
                send_char=0;
322
                //select index          
323
                jtag_vindex(index);
324
                //select instruction
325
                jtag_vir (UPDATE_WB_RD_DATA);
326
                //read uart reg 0 
327
                jtag_vdr(32, 0, &out);
328
                out&=0xFF;
329
                if(out != 0){
330
                        win_add_char(i,out);
331
                }
332
        }
333
}
334
 
335
 
336
 
337
int main(int argc, char** argv) {
338
 
339
        int i,key;
340
        int j;
341
        chain_num = 3;//default chain tap num for UART. Howver the user can change it by giving argc
342
        processArgs (argc, argv );
343
        WORDS_NUM= (BYTE_NUM % sizeof(unsigned )) ? (BYTE_NUM / sizeof(unsigned ) )+1 : (BYTE_NUM / sizeof(unsigned ));
344
        printf("Initial Vjtag for index num=%u target num=%u and shift-reg size %u.\n",index_num,jtag_target_number,jtag_shift_reg_size);
345
 
346
        if (jtag_init()){
347
                fprintf (stderr, "Error opening jtag IP with %d index num\n",index_num);
348
                return -1;
349
        }
350
 
351
 
352
        //devide the screen equaly between all UARTs
353
        initial_windows();
354
 
355
 
356
        do {
357
                run_jtag_scaner();
358
                key=getch();
359
        } while (key != ESCAPE);
360
 
361
 
362
 
363
        if(write_log_file){
364
                for (i=0;i<uart_num;i++) update_out_file(i);
365
                merge_output_files();
366
        }
367
        endwin();
368
 
369
        return 0;
370
}

powered by: WebSVN 2.1.0

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