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