Subversion Repositories f9daq

Rev

Rev 15 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
12 f9daq 1
// ./pts -a 0x1000000 -v 1 -w 2 turn LED ON
2
// ./pts -a 0x1000000 -v 1 -w 1 turn LED OFF
3
#include <stdlib.h>
4
#include <stdio.h>
5
#include <stdint.h>
6
#include <ctype.h>
7
#include <unistd.h>
8
#include <string.h>
9
#include "PtsModule.h"
15 f9daq 10
#include <getopt.h>
12 f9daq 11
 
12
void Delay(double t){
13
usleep(t*1e6);
14
}
15
 
16
 
17
uint32_t ptaddr=0;
18
 
19
int Pts_write(uint32_t addr, uint32_t data ){
81 f9daq 20
fprintf(stderr, "Pts_write ADDR 0x%04x  0x%04x ",addr,ptaddr+addr );
21
int nb=VME_A32D32_W(ptaddr+addr,&data);
22
fprintf(stderr, " nb=%d\n",nb);
23
return nb;
12 f9daq 24
}
25
 
81 f9daq 26
int Pts_Mwrite(uint32_t addr, uint32_t data ){
27
VME_A32D32_MW(ptaddr+addr,&data);
28
return 1;
29
}
30
 
12 f9daq 31
int Pts_read(uint32_t addr, uint32_t *data ){
81 f9daq 32
fprintf(stderr, "Pts_read ADDR  0x%04x  0x%04x  ",addr,ptaddr+addr );
33
int nb=VME_A32D32_R(ptaddr+addr,data);
34
fprintf(stderr, " nb=%d\n",nb);
35
return nb;
12 f9daq 36
}
37
 
38
 
39
int Pts_erase( int verbose ) {
81 f9daq 40
  uint32_t dum = 0;
12 f9daq 41
  Pts_write( ADR_CSR1, 0 );
42
  Delay(0.1);
43
  Pts_read( ADR_CSR1, &dum);
81 f9daq 44
  if( verbose ) fprintf( stderr, "CSR1(0x%02x) = 0x%04x <<<<\n", ADR_CSR1,
12 f9daq 45
                        dum & 0xffff );
46
  Pts_write( ADR_CSR1, CSR1_PROGRAM_ );
47
  Delay(0.1);
48
  Pts_read( ADR_CSR1, &dum);
81 f9daq 49
  if( verbose ) fprintf(stderr, "CSR1(0x%02x) = 0x%04x >>>>>\n", ADR_CSR1,
12 f9daq 50
                        dum & 0xffff );
51
  return 1;
52
}
53
/*
54
int Pts_configure( const char *filename, int mode, int verbose ) {
55
  int len = strlen( filename );
56
  if( len > 4 ) {
57
    const char *p = filename+len-4;
58
    if( strcmp(p,".bit")==0 ) return Pts_configure_bit( filename, mode, verbose );
59
    if( strcmp(p,".rbt")==0 ) return Pts_configure_rbt( filename, mode, verbose );
60
  }
61
  if( verbose ) fprintf( stderr, "Bad filename: \"%s\" .\n" );
62
  return -1;
63
}
64
 
65
int PtsModule::configure_rbt( const char *filename, int mode, int verbose ) {
66
  long n, pos;
67
  char cs[1024];
68
  FILE *fp;
69
  if( ( fp = fopen( filename, "r" ) ) == NULL ) {
70
    if( verbose ) fprintf( stderr, "cannot open \"%s\"\n", filename );
71
    return -1;
72
  }
73
  if( verbose ) printf( "file \"%s\" opened.\n", filename );
74
 
75
  do{
76
    pos = ftell(fp);
77
    fgets( cs, sizeof(cs), fp );
78
    puts(cs);
79
    n=strlen(cs);
80
  } while( n==0 || strspn( cs, " \t\n01" )!=n );
81
  fseek( fp, pos, SEEK_SET );
82
 
83
  long nchar = 0;
84
  if( mode == SLAVESERIAL_MODE ) {
85
    write( ADR_MODE, SLAVESERIAL_MODE );
86
    erase( verbose );
87
    int c;
88
    const long bit_per_dot = BYTE_PER_DOT*8;
89
    while( (c=getc(fp))!=EOF ){
90
      if( c=='0' || c=='1' ) {
91
        write( ADR_CFG, c=='0' ? 0 : 1 );
92
        nchar++;
93
        if( verbose && ++nchar%bit_per_dot==0 ) putchar('.');
94
      }
95
      else if( isspace(c) ) continue;
96
      else {
97
        fprintf( stderr, "(%ld) illegal character : %c\n", nchar, c );
98
        fclose(fp);
99
        return -1;
100
      }
101
    }
102
  } else if(mode == SELECTMAP_MODE) {
103
    if(verbose) fprintf(stderr, "\nselectmap mode (rbt file) not supported\n");
104
    return -1;
105
  } else {
106
    if(verbose) fprintf(stderr, "\nIllegal mode\n");
107
    return -1;
108
  }
109
  if(verbose) printf("\ntotal %ld bits\n", nchar);
110
  fclose(fp);
111
  return check_configure( verbose );
112
}
113
*/
114
int Pts_configure_bit( const char *filename, int mode, int verbose ) {
115
  int c,j;
116
  int dummyword;
117
  FILE *fp;
118
  const long byte_per_dot = BYTE_PER_DOT;
119
  unsigned long nchar = 0;
120
 
121
  if( ( fp = fopen( filename, "rb" ) ) == NULL ) {
122
    if( verbose ) fprintf( stderr, "cannot open \"%s\"\n", filename );
123
    return -1;
81 f9daq 124
  }
125
  if(verbose) printf( "Verbose= %d File \"%s\" opened.\n", verbose ,filename );
12 f9daq 126
 
127
  /* ------------------------------------------------------------ *\
128
    The data for the configuration start from 0xffff_ffff_aa99_aa66
129
    ( cf. xapp138; we don't know the definition of the BIT file )
130
  \* ------------------------------------------------------------ */
131
  dummyword = 0;
132
  do{
133
    if( (c = getc( fp )) == EOF ) {
134
      if(verbose) fprintf(stderr, "EOF detected. Exit.\n");
135
      return -1;
136
    }
137
    (c == 0xff) ? dummyword++ : (dummyword=0);
138
  } while( dummyword < 4 );
81 f9daq 139
 
12 f9daq 140
//  const long byte_per_dot = BYTE_PER_DOT;
141
//  unsigned long nchar = 0;
81 f9daq 142
 
12 f9daq 143
  if( mode == SLAVESERIAL_MODE ) {
81 f9daq 144
    if(verbose)  fprintf(stderr, "slave serial mode");
12 f9daq 145
    Pts_write( ADR_MODE, mode );
146
    Pts_erase( verbose );
147
    for( j=0; j<32; j++ ) Pts_write( ADR_CFG, 0x1 );
81 f9daq 148
    while(!feof(fp))
149
     {
150
      int nb = fread(&c,1,1,fp);
151
      if (nb<=0) continue;
152
    //while( (c=getc(fp))!=EOF ){
12 f9daq 153
      for( j=0; j<8; j++ ) Pts_write( ADR_CFG, (c>>(7-j))&0x1 );
154
      nchar++;
155
      if( verbose && nchar%byte_per_dot==0 ) putchar('.');
156
    }
157
  } else if( mode == SELECTMAP_MODE ) {
81 f9daq 158
    if( verbose )  fprintf(stderr, "select map mode ....");
159
 
12 f9daq 160
    Pts_write( ADR_MODE, SELECTMAP_MODE );
161
    Pts_erase( verbose );
81 f9daq 162
 
163
    fprintf(stderr, "-------------\n");
164
 
165
    //if( verbose )  fprintf(stderr, "program ....\n");
166
 
167
    for( j=0; j<4; j++ ) {
168
      fprintf(stderr, "%d ------------\n", j);
169
      Pts_write( ADR_CFG, 0xff );
170
    }
171
    fprintf(stderr, "at line %d -------------\n", __LINE__);
172
    VME_MWRST();
173
    while(!feof(fp))
174
     {
175
      int nb = fread(&c,1,1,fp);
176
      if (nb<=0) continue;
177
   // while ( (c=getc(fp))!=EOF ){
178
      if (feof(fp)) {
179
        if(verbose) fprintf(stderr, "feof detected. Exit.\n");
180
        return -1;
181
      }
12 f9daq 182
      int cc = 0;
183
      for(j=0; j<8; j++) cc |= ((c&(1<<j))>>j)<<(7-j);
81 f9daq 184
      //fprintf(stderr, "s");
185
      Pts_Mwrite( ADR_CFG, cc );
12 f9daq 186
      nchar++;
81 f9daq 187
      if( verbose && nchar%byte_per_dot==0 ) {
188
        fprintf(stderr, "#");
189
        VME_MWEXEC();
190
        VME_MWRST();
191
        fprintf(stderr, ".");
192
      }
12 f9daq 193
    }
194
  } else {
195
    if(verbose) fprintf(stderr, "\nIllegal mode\n");
196
    return -1;
197
  }
81 f9daq 198
  if(verbose) fprintf(stderr, "\ntotal %ld bits\n", nchar);
199
  if (fp) fclose(fp);
12 f9daq 200
  return Pts_check_configure( verbose );
201
}
202
 
203
int Pts_check_configure( int verbose ) {
204
  uint32_t csr1_value;
205
  Pts_read(ADR_CSR1,&csr1_value);
206
  if(verbose) printf("CSR1(0x%02x)=0x%04x\n",ADR_CSR1,csr1_value&0xffff);
207
  if(csr1_value&CSR1_DONE) {
81 f9daq 208
    if(verbose)  fprintf(stderr, "configure complete.\n");
12 f9daq 209
    return 1;
210
  } else {
81 f9daq 211
    if(verbose)  fprintf(stderr, "configure not complete.\n");
12 f9daq 212
    return -1;
213
  }
214
}
215
 
216
int Pts_reset( int verbose ) {
217
  Pts_write(ADR_CSR0,1);
218
  if( verbose ) printf( "CSR0(0x%02x) = 0x01\n", ADR_CSR0 );
219
  return 1;
220
}
221
 
222
int Pts_write_csr( int verbose, uint32_t value ) {
223
  Pts_write(ADR_CSR0,value);
224
  if( verbose ) printf( "Pts_write_csr 0x%08x\n", value  );
225
  return 1;
226
}
227
 
228
 
229
void help(char *argv){
230
 printf("Usage: %s -a <ptsaddr> -v <verbose> -c  .... Pts_check_configure\n", argv);
15 f9daq 231
 printf("Usage: %s -a <ptsaddr> -v <verbose> -i <filename> -b mode (2) ... Pts_configure_bit\n", argv);
12 f9daq 232
 printf("Usage: %s -a <ptsaddr> -v <verbose> -e  .... Pts_erase\n", argv);
15 f9daq 233
 printf("Usage: %s -a <ptsaddr> -v <verbose> -x  .... Pts_reset\n", argv);
234
 printf("Usage: %s -a <ptsaddr> -v <verbose> -c  .... Pts_check_configure\n", argv);
235
 printf("Usage: %s -a <ptsaddr> -v <verbose> -r  .... vme read\n", argv);
236
 printf("Usage: %s -a <ptsaddr> -v <verbose> -w <value> .... vme write\n", argv);
237
 printf("Usage: %s -a <ptsaddr> -v <verbose> -s <value> .... Pts_write_csr\n", argv);
238
 printf("Example: %s  --address  0x1000000 --verbose 1 --erase\n", argv);
239
 printf("Example: %s --address  0x1000000 --verbose 1 --input pts_scaler.bit --load-bit 2\n", argv);
240
 printf("Example: %s --address  0x1000004 --write-csr 0x7600\n", argv);
12 f9daq 241
}
242
 
15 f9daq 243
 
244
 
245
static const char *optString = "v:rxea:cs:w:i:b:h";
246
static const struct option longOpts[] = {
247
    { "verbose", required_argument, NULL, 'v' },
248
    { "reset"  , no_argument, NULL, 'x' },
249
    { "erase"  , no_argument, NULL, 'e' },
250
    { "address", required_argument, NULL, 'a' },
251
    { "check-configure", no_argument, NULL, 'c' },
252
    { "write-csr", required_argument , NULL, 's' },
253
    { "input", required_argument , NULL, 'i' },
254
    { "load-bit", required_argument , NULL, 'b' },
255
    { "read", no_argument , NULL, 'r' },
256
    { "write",required_argument , NULL, 'w' },
257
    { "help", no_argument, NULL, 'h' },
258
    { NULL, no_argument, NULL, 0 }
259
};
260
 
12 f9daq 261
int main (int argc, char ** argv){
262
 
15 f9daq 263
uint32_t value;
264
 
12 f9daq 265
 int verbose=0;
266
 char filename[256]="file.bit";
267
 int c;
268
 opterr = 0;
15 f9daq 269
int longIndex = 0;
12 f9daq 270
 
271
if (argc<2) {
272
  help(argv[0]);
273
  exit(-1);
274
}
275
 
276
VME_START(NULL);
15 f9daq 277
 
278
c = getopt_long( argc, argv, optString, longOpts, &longIndex );
279
        while( c != -1 )
280
 
281
// while ((c = getopt (argc, argv, "v:xrea:csw:i:b:h")) != -1)
282
{
12 f9daq 283
         switch (c)
284
           {
285
          case 'v':
286
            verbose = atoi(optarg);
287
            break;
15 f9daq 288
          case 'x':
12 f9daq 289
            Pts_reset(verbose);
290
            break;             // reset
291
          case 'e':
292
            Pts_erase(verbose);
293
            break;             // erase
294
          case 'a':
295
            ptaddr  =  strtoul (optarg,NULL,0);
296
            break;             // address
297
          case 'c':
298
            Pts_check_configure( verbose );
299
            break;
300
          case 'w':
301
            value = strtoul (optarg,NULL,0);
15 f9daq 302
            Pts_write( 0 , value );
303
            break;
304
          case 'r':
305
            value = 0;
306
            Pts_read( 0 , &value );
307
            break;
308
          case 's':
309
            value = strtoul (optarg,NULL,0);
12 f9daq 310
            Pts_write_csr( verbose, value );
311
            break;
312
          case 'i':
313
            sprintf(filename,"%s",optarg);
314
            break;             // data width
315
          case 'b':
316
//#define SELECTMAP_MODE 2
317
//#define SLAVESERIAL_MODE 3
318
             value = strtoul (optarg,NULL,0);
319
             Pts_configure_bit( filename, value,  verbose );
320
             break;          
321
          case 'h':
322
            help(argv[0]);
323
            break;
324
           default:
325
             printf("Unknown command %c\n",c);
326
           }
15 f9daq 327
    c = getopt_long( argc, argv, optString, longOpts, &longIndex );
328
}
12 f9daq 329
VME_STOP();
330
}
331