Subversion Repositories f9daq

Rev

Rev 15 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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