apps/fpcf/fpcf.c

Go to the documentation of this file.
00001 /*!***************************************************************************
00002 *! FILE NAME  : fpcf.c
00003 *! DESCRIPTION: A big set of different FPGA-related commands
00004 *! Copyright (C) 2002-2007 Elphel, Inc.
00005 *! -----------------------------------------------------------------------------**
00006 *!  This program is free software: you can redistribute it and/or modify
00007 *!  it under the terms of the GNU General Public License as published by
00008 *!  the Free Software Foundation, either version 3 of the License, or
00009 *!  (at your option) any later version.
00010 *!
00011 *!  This program is distributed in the hope that it will be useful,
00012 *!  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *!  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 *!  GNU General Public License for more details.
00015 *!
00016 *!  You should have received a copy of the GNU General Public License
00017 *!  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 *! -----------------------------------------------------------------------------**
00019 *!  $Log: fpcf.c,v $
00020 *!  Revision 1.7  2008/08/01 23:52:44  spectr_rain
00021 *!  add result=OK string to output of SDRAM phase settings and SDRAM memory test
00022 *!
00023 *!  Revision 1.6  2008/04/29 06:24:06  elphel
00024 *!  used strtoll() instead of strtol to handle unsigned long >=0x7fffffff
00025 *!
00026 *!  Revision 1.5  2008/04/09 19:46:59  elphel
00027 *!  added fflush
00028 *!
00029 *!  Revision 1.4  2008/04/09 19:35:08  elphel
00030 *!  removed "test error"
00031 *!
00032 *!  Revision 1.3  2008/04/09 19:33:07  elphel
00033 *!  added system memory test, more wait state registers
00034 *!
00035 *!  Revision 1.2  2008/01/25 07:13:01  elphel
00036 *!  typo
00037 *!
00038 *!  Revision 1.1.1.1  2007/09/30 03:20:25  elphel
00039 *!  This is a fresh tree based on elphel353-2.10
00040 *!
00041 *!  Revision 1.5  2007/09/30 03:20:25  elphel
00042 *!  fixed comment typo
00043 *!
00044 *!  Revision 1.4  2007/09/16 06:15:42  elphel
00045 *!  Just debug info (disabled by #define) to troubleshoot sctl.cgi
00046 *!
00047 *!  Revision 1.3  2007/08/17 12:12:22  spectr_rain
00048 *!  switch to GPL3 license
00049 *!
00050 *!  Revision 1.2  2007/07/10 08:25:44  spectr_rain
00051 *!  *** empty log message ***
00052 *!
00053 *!  Revision 1.3  2007/04/17 21:37:16  elphel
00054 *!  added short output for "fpcf -c <bit_number>"
00055 *!
00056 *!  Revision 1.2  2007/04/04 03:58:57  elphel
00057 *!  support for new i2c features (hello.c - control i2c timing, fpcf - character device access to i2c)
00058 *!
00059 *!  Revision 1.1.1.1  2007/02/23 10:11:49  elphel
00060 *!  initial import into CVS
00061 *!
00062 *!  Revision 1.12  2006/04/06 07:46:33  elphel
00063 *!  control for canon lenses
00064 *!
00065 *!  Revision 1.11  2006/02/18 18:55:28  elphel
00066 *!  addet FPGA 6 i/o pins capture mode
00067 *!
00068 *!  Revision 1.10  2006/01/11 17:10:05  elphel
00069 *!  *** empty log message ***
00070 *!
00071 *!  Revision 1.9  2006/01/05 05:07:46  spectr_rain
00072 *!  load precalculated gamma table
00073 *!
00074 *!  Revision 1.8  2005/11/23 05:07:42  spectr_rain
00075 *!  up limit for sensor clock
00076 *!
00077 *!  Revision 1.7  2005/08/28 15:46:55  elphel
00078 *!  bug fix in "fpcf -?"
00079 *!
00080 *!  Revision 1.6  2005/08/27 00:46:08  elphel
00081 *!  continue on histograms
00082 *!
00083 *!  Revision 1.5  2005/08/26 02:59:06  elphel
00084 *!  working on histogram
00085 *!
00086 *!  Revision 1.4  2005/07/11 01:33:05  elphel
00087 *!  improved DDR SDRAM phase adjustment
00088 *!
00089 *!  Revision 1.3  2005/05/15 17:39:07  elphel
00090 *!  made DDR SDRAM clock phase adjustment - same as in Theora for model 333 branch
00091 *!
00092 *!  Revision 1.2  2005/05/10 21:08:46  elphel
00093 *!  *** empty log message ***
00094 *!
00095 *!  Revision 1.2  2004/06/18 21:34:13  elphel
00096 *!  added "JPEG_CMD_JUST_STOP" command to stop continuous acquisition mode (actually just a line in help)
00097 *!
00098 */
00099 
00100 /****************** INCLUDE FILES SECTION ***********************************/
00101 #include <stdio.h>
00102 
00103 #include <sys/socket.h>
00104 #include <netinet/in.h>
00105 #include <sys/types.h>
00106 #include <sys/stat.h>
00107 #include <fcntl.h>
00108 
00109 #include <sys/ioctl.h>
00110 #include <fcntl.h>
00111 #include <stdio.h>
00112 #include <stdlib.h>
00113 #include <ctype.h>
00114 #include <errno.h>
00115 #include <unistd.h>
00116 #include <string.h>
00117 //#include <math.h>
00118 
00119 //#include <asm/elphel/fpgaconfa.h>
00120 #include <asm/elphel/c313a.h>
00121 #include <asm/elphel/autoexp.h>
00122 
00123 #include <asm/elphel/fpgaclocks.h>
00124 #include <asm/elphel/fpgactrl.h>
00125 
00126 //#define X313_WA_SD_MANCMD 0x23 // from x3x3.h
00127 #define X313_WA_SD_MODE   0x27 // from x3x3.h
00128 #define X313__RA__STATUS  0x10 // read status register
00129 #define X313_WA_DCM          8  // SDRAM clock phase shift
00130 
00131 
00132   #define X313_SR__DCM_OVFL   22
00133   #define X313_SR__DCM_LOCKED 21
00134   #define X313_SR__DCM_RDY    20
00135   #define X313_SR__DCM_EARLY  19
00136   #define X313_SR__DCM_LATE   18
00137 
00138 
00139 #define IS_DCM_OVFL(x)   ( x & (1 << X313_SR__DCM_OVFL ))
00140 //#define IS_DCM_LOCK(x) ( x & (1 << X313_SR__DCM_LOCKED ))
00141 #define IS_DCM_LOCK(x)   ( x & (1 << X313_SR__DCM_LOCKED ))
00142 #define IS_DCM_RDY(x)    ( x & (1 << X313_SR__DCM_RDY ))
00143 #define DCM_ERR(x)       (( x >> X313_SR__DCM_LATE ) & 3)
00144 #define IS_DCM_GOOD(x)   (IS_DCM_LOCK(x) && IS_DCM_RDY(x) && !(IS_DCM_OVFL(x)))
00145 
00146 
00147 /*
00148 
00149 
00150 #define CY22393_PLLMIN 100.0
00151 #define CY22393_PLLMAX 400.0
00152 #define CY22393_XTAL 20.0
00153 #define CY22393_OUTMAX 166.0
00154 #define CY22393_PMIN 16
00155 #define CY22393_PMAX 1600
00156 
00157 
00158 static int pll_corr[]= {232,627,835,1044};
00159 
00160 */
00161 
00162 
00163 #define CLOCK_LOW       20
00164 #define CLOCK_HIGH      127
00165 #define CY22393_SA 0xd2
00166 
00167 // saving parameter string in /var/log/fpcf.log if defined
00168 //#define DEBUG_USAGE 1
00169 
00170 static char * usage = "Usage: fpcf \n" \
00171             "-lens D                - write byte D to lens (print to stdout byte read)\n" \
00172             "-capture T [mask]      - capture I/O pins for T usec (actually will be longer), return number of captured states, then data\n" \
00173             "                         if mask is specified - AND results with mask, output 2 hex digits if mask> 16, else - 1 digit.\n" \
00174             "-histogram             - dump histogram\n" \
00175             "-phase min max [corr]  - adjust optimal readout phase in the range [min,max], min can be negative. Will turn off refresh\n" \
00176             "                         Apply optional correction [corr] (signed decimal) to the calcualted result.\n" \
00177             "-dbg B [L [S]]         - read out debug data: B - bit position (decimal), L - register length, S - optional preceeding string\n" \
00178             "-mem                   - analyze all memory, expecting pattern of 4 16-bit words (will compare to the previous)\n" \
00179             "-mem N                 - analyze specified number of words (hex) - up to 1000000 expecting pattern of 4 16-bit words\n" \
00180             "                         N will be rounded to the nearest multiple of 4096\n" \
00181             "-mem N ptrn0 ptrn1 ptrn2 ptrn3    - write repetitive pattern of 4 16-bit words (hex), read and compare\n" \
00182             "-raw L   filename      - read hex byte data from filename and put it into frame buffer\n" \
00183             "                         L (decimal) - full length of the line, including margins. 132 for a single 128x64 supertile \n" \
00184             "-mcu     filename      - (not used in 333-theora) read hex data and write it to the first MCU(s)\n" \
00185             "-table A filename      - write quantization/huffman table(s) from file starting att address A(hex word)\n" \
00186             "-dma 0                 - stop DMA\n" \
00187             "-dma 1                 - start DMA (descriptors should be filled)\n" \
00188             "-dma 2                 - start DMA (raw mode). Descriptor array will be built according to current\n" \
00189             "                         acquisition parameters (image size, word size)\n" \
00190             "-jpeg 0                - 'reset' JPEG acquire/read pointers\n" \
00191 //            "-jpeg 1                - 'arm' JPEG (compress data with the next frame acquisition)\n"
00192             "-jpeg 2                - 'get' acquire JPEG data from current frame in memory\n" \
00193             "-jpeg 3                - 'forget' advance JPEG read pointer to the next frame\n" \
00194             "-jpeg 5                - acquire and compress one frame\n" \
00195             "-jpeg 6                - save read pointer\n" \
00196             "-jpeg 7                - restore read pointer\n" \
00197             "-jpeg a                - start constant compression to buffer\n" \
00198             "-jpeg b                - stop constant compression to buffer or acquire just one buffer if it wasn't running\n" \
00199             "-jpeg c                - rebuild the frames chain backwards from the last acquired. Return number of frames\n" \
00200             "-jpeg d                - stop constant compression to buffer, don't try to acquire! \n" \
00201             "-jpeg f                - print some debug data on the serial console\n" \
00202             "-jctl data             - write JPEG control word (+0x10000 - no header)\n" \
00203 
00204             "-jn data               - get specified number of frames (will add to already asked for if any)\n" \
00205             "-jl data               - get specified length in 32-bit long words (will stop after frame if got more)\n" \
00206 
00207             "-dr (-drb, -drw, -drl) - get current DMA received data length\n" \
00208             "-dr A (-drb A) -         read DMA recieved data (bytes) at location A\n" \
00209             "-dr A N (-drb A N) -     read DMA recieved data (bytes) at locations A through A+N-1\n" \
00210             "-drw A -                 read DMA recieved data (words) at location A\n" \
00211             "-drw A N -               read DMA recieved data (words) at locations A through A+N-1\n" \
00212             "-drl A -                 read DMA recieved data (long words) at location A\n" \
00213             "-drl A N -               read DMA recieved data (long words) at locations A through A+N-1\n" \
00214             "-sr A -                  read SDRAM location A\n" \
00215             "-sr A N -                read SDRAM locations A through A+N-1\n" \
00216             "-sw{,8,16,32} A D -      write SDRAM location A with data D\n" \
00217             "-sw{,8,16,32} A D N -    write SDRAM locations A through A+N-1 with data D\n" \
00218             "-sw{,8,16,32} A D N INC -write SDRAM locations A through A+N-1 with data D, D+INC, ... D+INC*(N-1)\n" \
00219             "-p FILENAME -            load FPGA with specified bitstream file\n" \
00220             "-button -                returns 1 if button is pressed, 0 - otherwise\n" \
00221             "-a -                     read port A input pins\n" \
00222             "-t TMS LEN D-            send/get JTAG data - TMS=0..1, LEN=0..7 (0==8), D - hex byte\n" \
00223             "-a D -                   write port A input data\n" \
00224             "-X N -                   read current cloclk frequency in MHz (shadow)\n" \
00225             "-X N F -                 program clock output 0,1,2,3 to frequency in MHz (N=3 - only 20MHz), decimal (invisible to driver) - floating pont value\n" \
00226             "-x N F -                 program clock output 0,1   to frequency in MHz (will be visible to driver)\n" \
00227             "-s -                     read fpga/clocks state\n" \
00228             "-r   REGISTER     -      read fpga register (0..ff) using CSP0 (fast access)\n" \
00229             "-r4  REGISTER     -      read fpga register (0..ff) using CSP4 (slow access)\n" \
00230 
00231 
00232             "-rl  REGISTER     -      read fpga register (0..ff) low word using CSP0 (fast access)\n" \
00233             "-rl4 REGISTER     -      read fpga register (0..ff) low word using CSP4 (slow access)\n" \
00234             "-rh  REGISTER     -      read fpga register (0..ff) high word using CSP0 (fast access)\n" \
00235             "-rh4 REGISTER     -      read fpga register (0..ff) high word using CSP4 (slow access)\n" \
00236             "-w   REGISTER DATA -     write fpga 32-bit register (0..ff) using CSP0 (fast access)\n" \
00237             "-w4  REGISTER DATA -     write fpga 32-bit register (0..ff) using CSP0 (slow access)\n" \
00238             "-rw [1..4]         -     read R_WAITSTATES\n" \
00239             "-ww [1..4] D       -     write R_WAITSTATES\n" \
00240             "-i2cr8 A           -     read i2c bus 0 at location A (slave address is made of A), 8-bit registers\n" \
00241             "-i2cr8 A N         -     read i2c bus 0 at location A through A+N-1\n" \
00242             "-i2cw8 A D         -     write i2c bus 0 at location A with 8-bit data D\n" \
00243             "-i2cr16 A          -     read i2c bus 0 at location A (slave address is made of A), 16-bit registers\n" \
00244             "-i2cr16 A N        -     read i2c bus 0 at location A through A+N-1\n" \
00245             "-i2cw16 A D        -     write i2c bus 0 at location A with 16-bit data D\n" \
00246             "-sr A N -                read SDRAM locations A through A+N-1\n" \
00247             "-sw{,8,16,32} A D -      write SDRAM location A with data D\n" \
00248             "-ir SA RA - read i2c register RA from slave SA (hex)\n"\
00249             "-iw SA RA DATA - write i2c register, slave address SA, register RA data DATA (hex)\n"\
00250             "-ir - read all relevant i2c registers from slave 0xd2(0x61)\n"\
00251             "-c -                     read FPGA command register shadow\n"\
00252             "-c bit(0..1f of CR) op(0 -nop, 1 - clr, 2 - set, 3 - toggle)\n"\
00253             "-gamma filename        - write precalculated gamma table\n"\
00254             "-sysmem N [SEED [SIZE]]- allocate N (decimal) of SIZE (hex, default=20000 (128KB)) chunks, fill them with incrementing data starting with SEED (hex), read back\n" ;
00255 static char * short_usage = "Use fpcf -help for options\n";
00256 
00257 /*
00258 int setCYField (int devfd, int addr, int mask, int value) {
00259     unsigned long d, ra;
00260     ra= addr & 0xff;
00261     d=ioctl(devfd, _IO(FPGACONF_IOCTYPE,I2C_READREG), I2C_READARG (0, CY22393_SA, ra)) ;
00262     if (d & 0xffffff00) return -1;  //bad data
00263     d^=(d ^ (unsigned long) value) & (unsigned long) mask;
00264     d=ioctl(devfd, _IO(FPGACONF_IOCTYPE, I2C_WRITEREG ),I2C_WRITEARG(0, CY22393_SA, ra, d));
00265     if (d & 0xffffff00) return -1;  //bad data
00266     return 0;  
00267 
00268 }
00269 */
00270 int readSDRAMdummy(void) { // reads SDRAM and ignores result
00271   int memfd;
00272   int dummy;
00273   int res;
00274   if ((memfd = open("/dev/fsdram", O_RDONLY))<0)  {printf("error opening /dev/fsdram\r\n"); return -1;}
00275   res=read(memfd,&dummy,4);
00276   close (memfd);
00277   if (res<4) {printf("readSDRAMdummy failed\n"); return -1; }
00278   return 0;
00279 }
00280 
00281 //        res=(res<<1) || ( (ioctl(devfd, _IO(FPGACONF_READREG4, 0x70 ), 0) & 2)>>1);
00282 //        ioctl(devfd, _IO(FPGACONF_WRITEREG, 0x70 ), 0x23 | ((n & 0x80)?0x10:0)); // 
00283   void usleepFPGA(int devfd, int dly) { // <0.5 sec, devfd should be opened
00284     int t0,t1,t;
00285     ioctl(devfd, _IO(FPGACONF_WRITEREG, 0x47 )); // latch timer
00286     t0=ioctl(devfd, _IO(FPGACONF_READREG, 0x44 ), 0);
00287     t1=t0+dly;
00288     if (t1>=1000000) t1-=1000000;
00289     t=t0;
00290     if (t1>t0) {
00291       while (t<t1) {
00292         ioctl(devfd, _IO(FPGACONF_WRITEREG, 0x47 )); // latch timer
00293         t=ioctl(devfd, _IO(FPGACONF_READREG, 0x44 ), 0);
00294       }
00295     } else {
00296       while ((t>t0) || (t<t1)) {
00297         ioctl(devfd, _IO(FPGACONF_WRITEREG, 0x47 )); // latch timer
00298         t=ioctl(devfd, _IO(FPGACONF_READREG, 0x44 ), 0);
00299       }
00300     }
00301   }  
00302 
00303 #define FPCF_SDBUFFER_SIZE 0x800
00304 int main (int argc, char *argv[]) {
00305   const unsigned char syncseq[]={0xff,0xff,0xff,0xff,0xaa,0x99,0x55,0x66};
00306 
00307   unsigned char charheadbuf[8];
00308   unsigned short usbuf[FPCF_SDBUFFER_SIZE];
00309   unsigned char  * usbuf8 =  (unsigned char *)  usbuf;
00310   unsigned short * usbuf16 = (unsigned short *) usbuf;
00311   unsigned long  * usbuf32 = (unsigned long *)  usbuf;
00312     int retry,corr;
00313     char * cp;
00314     int i,a;
00315     int j=0;
00316     int n,res;
00317     double f;
00318     int devfd;
00319 //    double dif, bdif,k;
00320 //    int divmn,divmx,divi,p,q,pllc;
00321     int bp=0;
00322     int bq=0;
00323 //    int bdiv=0;
00324     unsigned long d, sa, ra, rd,fs, ll, nl, nt;
00325     int ifd;
00326     int retr;
00327     unsigned char b,b0;
00328 //    unsigned short usd,usi;
00329     unsigned long uld,uli;
00330   int shft;
00331   int testcs=0;
00332   int testnum=0;
00333     int iorw_args=0;
00334     int ws;
00335   int tables[4096];
00336   unsigned int ptrn[2];
00337   unsigned int ands[2];
00338   unsigned int ors[2];
00339   unsigned short * ands16 = (unsigned short *) ands;
00340   unsigned short * ors16 =  (unsigned short *) ors;
00341   unsigned short * ptrn16 = (unsigned short *) ptrn;
00342   int nerr;
00343   int comp_mode;
00344   int seed;
00345   int ** memchunks= (int **) tables;
00346 
00347   unsigned char * btables= (unsigned char *) tables;
00348     const int bits_in_byte[]={0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,\
00349                               1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,\
00350                               1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,\
00351                               2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,\
00352                               1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,\
00353                               2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,\
00354                               2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,\
00355                               3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,\
00356                               1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,\
00357                               2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,\
00358                               2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,\
00359                               3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,\
00360                               2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,\
00361                               3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,\
00362                               3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,\
00363                               4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8};                              
00364 
00365  char fline[1024];
00366   FILE *  tfile;
00367   char *  ep;
00368   char    fvx[5]="%08x\0";
00369   int     mn,mx;
00370   int     dqmn=0;
00371   int     dqmx=0;
00372   int     state;
00373         int gamma_fd;
00374         int autoexp_fd;
00375    int capByteOut;
00376    int capByteIn;
00377    int capBitCntr;
00378    int capByteCntr;
00379    int capLastByte;
00380    int capByte;
00381    int capFormat=0;
00382 
00383 #ifdef DEBUG_USAGE
00384   FILE *  fpcflog;
00385   if ((fpcflog = fopen("/var/log/fpcf.log", "w"))==NULL) {printf("error opening file %s\n","/var/log/fpcf.log"); return -1;}
00386   
00387   for (i=0; i<argc;i++) fprintf (fpcflog, " %s", argv[i]);
00388   fclose(fpcflog);
00389 #endif
00390    if (argc<2) {printf(short_usage); return 0;}
00391    if ((strcasecmp(argv[1], "-help")==0) || (strcasecmp(argv[1], "-?")==0)) {
00392      printf(usage);
00393      return 0;
00394    }
00395 
00396    if(strcasecmp(argv[1], "-lens") == 0) {
00397       if(argc <3) { // read capture
00398          printf("need data to send to the lens\n");
00399          return -1;
00400       }
00401       
00402       if ((devfd = open("/dev/fpgaio", O_RDWR))<0)  {printf("error opening /dev/fpgaio\r\n"); return -1;}
00403       for (j=2; j< argc; j++) {
00404         res= ioctl(devfd, _IO(FPGACONF_CANON_IOBYTE, 0 ),(unsigned long) strtoll (argv[j],&cp,16));
00405         tables[j-2]=res;
00406       }  
00407       close (devfd);
00408       for (j=2; j< argc; j++) {
00409         printf ("%2x ",tables[j-2]);
00410       }
00411       printf("\n");  
00412       return 0;
00413    }
00414    
00415    
00416    if(strcasecmp(argv[1], "-capture") == 0) {
00417       j=0xff;
00418       if (argc >3) j=strtol (argv[3],&cp,10);
00419       j &= 0xff;
00420       if(argc <3) { // read capture
00421          printf("need capture duration (usec)\n");
00422          return -1;
00423 
00424       }
00425       if ((devfd = open("/dev/fpgaio", O_RDWR))<0)  {printf("error opening /dev/fpgaio\r\n"); return -1;}
00426       n= ioctl(devfd, _IO(FPGACONF_START_CAPTURE, j ), strtol (argv[2],&cp,10));
00427       printf ("%d\n",n);
00428       for (i=0;i<n;i++) {
00429         if (j<16) printf ("%1x",j & ioctl(devfd, _IO(FPGACONF_READ_CAPTURE, 0 ), 0));
00430         else      printf ("%2x",j & ioctl(devfd, _IO(FPGACONF_READ_CAPTURE, 0 ), 0));
00431       }
00432       printf ("\n");
00433       close (devfd);
00434       return 0;
00435    }
00436    if(strcasecmp(argv[1], "-capbytes") == 0) {
00437       if (argc >4) capFormat=strtol (argv[4],&cp,10);
00438       j=0xff;
00439       if (argc >3) j=strtol (argv[3],&cp,10);
00440       j &= 0xff;
00441       if(argc <3) { // read capture
00442          printf("need capture duration (usec)\n");
00443          return -1;
00444 
00445       }
00446       if ((devfd = open("/dev/fpgaio", O_RDWR))<0)  {printf("error opening /dev/fpgaio\r\n"); return -1;}
00447       n= ioctl(devfd, _IO(FPGACONF_START_CAPTURE, j ), strtol (argv[2],&cp,10));
00448       printf ("%d\n",n);
00449       capByteOut=0;
00450       capByteIn=0;
00451       capBitCntr=0;
00452       capByteCntr=0;
00453       capLastByte=7;
00454       for (i=0;i<n;i++) {
00455         capByte=ioctl(devfd, _IO(FPGACONF_READ_CAPTURE, 0 ), 0);
00456         if (!(capLastByte & 1) && (capByte & 1)) {
00457           capByteOut= (capByteOut << 1) | ((capByte & 4)? 1:0);
00458           capByteIn=  (capByteIn  << 1) | ((capByte & 2)? 1:0);
00459           capBitCntr++;
00460           if (capBitCntr==8) {
00461             if      (capFormat==0) printf (" %2x(%2x)",capByteOut,capByteIn);
00462             else if (capFormat==1)  printf ("%2x  %2x",capByteOut,capByteIn);
00463             else if (capFormat==2)  printf ("%2x  %2x\n",capByteOut,capByteIn);
00464             capByteOut=0;
00465             capByteIn=0;
00466           } else if (capBitCntr>8) {
00467             if      (capFormat==0) printf (" [%1x/%1x]",capByteOut,capByteIn);
00468             else if (capFormat==1)  printf ("  [%1x/%1x]\n",capByteOut,capByteIn);
00469             capByteOut=0;
00470             capByteIn=0;
00471             capBitCntr=0;
00472             capByteCntr++;
00473           }
00474         }
00475         capLastByte=capByte;
00476       }
00477       printf ("\n");
00478       printf ("Exchanged %d bytes, %d bits left\n", capByteCntr,capBitCntr);
00479       if (capBitCntr>0)  printf ("Left: %2x(%2x)\n",capByteOut,capByteIn);
00480       close (devfd);
00481       return 0;
00482    }
00483         if(strcasecmp(argv[1], "-gamma") == 0) {
00484                 if(argv[2] == NULL) {
00485                         printf("need gamma table filename\n");
00486                         return -1;
00487                 }
00488                 if((autoexp_fd = open(AUTOEXP_DEV_NAME, O_RDWR)) < 0) {
00489                         printf("failed to open %s\n", AUTOEXP_DEV_NAME);
00490                         return -1;
00491                 }
00492                 if((gamma_fd = open(argv[2], O_RDONLY)) < 0) {
00493                         printf("failed to open %s\n", AUTOEXP_DEV_NAME);
00494                         return -1;
00495                 }
00496                 ioctl(autoexp_fd, _IO(IOC_AUTOEXP_GAMMA_TABLE, 0));
00497                 while((i = read(gamma_fd, (void *)usbuf, FPCF_SDBUFFER_SIZE)) > 0)
00498                         if(write(autoexp_fd, (void *)usbuf, i) < 0) {
00499                                 printf("error write gamma\r\n");
00500                                 break;
00501                         }
00502                 close(gamma_fd);
00503                 close(autoexp_fd);
00504                 return 0;
00505         }
00506    if (strcasecmp(argv[1], "-histogram")==0) {
00507          if ((devfd = open("/dev/fpgaio", O_RDWR))<0)  {printf("error opening /dev/fpgaio\r\n"); return -1;}
00508          ioctl(devfd, _IO(FPGACONF_WRITEREG, 0x42 ), 0);
00509          for (i=0; i<4;i++) {
00510            res=0;
00511            for (j=0; j<256; j++) {
00512              n=ioctl(devfd, _IO(FPGACONF_READREG4, 0x43 ), 0);
00513              res+=n;
00514              printf ("%6x",n);
00515              if ((j & 0xf)==0xf) printf("\n");
00516              if ((j & 0xff)==0xff) printf("\n");
00517            }
00518            printf ("total=0x%x (%d)\n",res,res);
00519          }
00520          close (devfd);
00521          return 0;
00522    }
00523 #if 0
00524 #define X313_SR__DCM_OVFL    28
00525 #define X313_SR__DCM_LOCKED  25
00526 //#define X313_SR__DCM_STOPPED 24
00527 #define X313_SR__DCM_RDY    23
00528 #define X313_SR__DCM_EARLY  22
00529 #define X313_SR__DCM_LATE   21
00530 
00531 #define IS_DCM_OVFL(x)   ( x & (1 << X313_SR__DCM_OVFL ))
00532 //#define IS_DCM_LOCK(x) ( x & (1 << X313_SR__DCM_LOCKED ))
00533 #define IS_DCM_LOCK(x)   ( x & (1 << X313_SR__DCM_LOCKED ))
00534 #define IS_DCM_RDY(x)    ( x & (1 << X313_SR__DCM_RDY ))
00535 #define DCM_ERR(x)       (( x >> X313_SR__DCM_LATE ) & 3)
00536 #define IS_DCM_GOOD(x)   (IS_DCM_LOCK(x) && IS_DCM_RDY(x) && !(IS_DCM_OVFL(x)))
00537 #endif
00538 #define DCM_RETRY 100
00539 #define phase_noise_margin 5
00540    if (strcasecmp(argv[1], "-phase")==0) { // adjust phase
00541       if (argc<4) {printf("min and max phase shift(decimal) required\r\n");return -1;}
00542       mn= strtol (argv[2],&cp,10);
00543       mx= strtol (argv[3],&cp,10);
00544       corr=0;
00545       if (argc>4) corr= strtol (argv[4],&cp,10);
00546       if ((devfd = open("/dev/fpgaio", O_RDWR))<0)  {printf("error opening /dev/fpgaio\r\n"); return -1;}
00547 //reset phase
00548       ioctl(devfd, _IO(FPGACONF_WRITEREG, X313_WA_DCM ), 3); // reset phase
00549       for (retry=DCM_RETRY;retry>0;retry--) {
00550            a=ioctl(devfd, _IO(FPGACONF_READREG, X313__RA__STATUS ), 0);
00551            if (IS_DCM_LOCK(a)) break;
00552       }
00553       if (retry <=0) {
00554         printf ("DCM does not lock after bein reset (status=%x)\n",a);
00555         close (devfd);
00556         return -1;
00557       }
00558 // go to lower limit
00559       if (mn <0) {
00560         for (i=0; i>mn;i--) {
00561           for (retry=DCM_RETRY;retry>0;retry--) {
00562              a=ioctl(devfd, _IO(FPGACONF_READREG, X313__RA__STATUS ), 0);
00563              if (IS_DCM_GOOD (a)) break;
00564           }
00565           if (retry <=0) {
00566             printf ("DCM phase probably reached lower limit (status=%x). Try >%d (instead of %d)\n",a,i,mn);
00567             ioctl(devfd, _IO(FPGACONF_WRITEREG, X313_WA_DCM ), 3); // reset phase
00568             close (devfd);
00569             return -2;
00570           }
00571           ioctl(devfd, _IO(FPGACONF_WRITEREG, X313_WA_DCM ), 1); // increase phase
00572         }
00573       } else if (mn>0) {
00574         for (i=0; i<mn;i++) {
00575           for (retry=DCM_RETRY;retry>0;retry--) {
00576              a=ioctl(devfd, _IO(FPGACONF_READREG, X313__RA__STATUS ), 0);
00577              if (IS_DCM_GOOD (a)) break;
00578           }
00579           if (retry <=0) {
00580             printf ("DCM phase lower limit is probably too high (status=%x). Try <%d (instead of %d)\n",a,i,mn);
00581             ioctl(devfd, _IO(FPGACONF_WRITEREG, X313_WA_DCM ), 3); // reset phase
00582             close (devfd);
00583             return -3;
00584           }
00585           ioctl(devfd, _IO(FPGACONF_WRITEREG, X313_WA_DCM ), 2); // decrease phase
00586         }
00587       }
00588 #if 0 // What the hell was that???
00589 // reset and reprogram SDRAM (twice, to be sure phase was settled)
00590 //      ra=X313_WA_SD_MANCMD; // NOTE: WAS BUG - 0x20, needed 0x23
00591       ra=0x20;  // NOTE: WAS BUG - 0x20, needed 0x23
00592       rd=0x15555; // all channels reset, stop refresh (so SDRAM will be initialized when opened)
00593       for (i=0;i<2;i++) {
00594         ioctl(devfd, _IO(FPGACONF_WRITEREG, ra ), rd);
00595         if (readSDRAMdummy()<0) {close (devfd); return -1;}
00596       }
00597 #endif
00598 // scan all phases in [min,max] storing data in tables[];
00599       for (i=0; i < ((mx-mn)+1); i++) {
00600         if (i>0) { // decrease phase by 1 - all but first;
00601           for (retry=DCM_RETRY;retry>0;retry--) {
00602              a=ioctl(devfd, _IO(FPGACONF_READREG, X313__RA__STATUS ), 0);
00603              if (IS_DCM_GOOD (a)) break;
00604           }
00605           if (retry <=0) {
00606             i+=mn;
00607             if (i<0) {
00608               printf ("DCM phase probably reached lower limit (pass2, status=%x). Try >%d (instead of %d)\n",a,i,mn);
00609               ioctl(devfd, _IO(FPGACONF_WRITEREG, X313_WA_DCM ), 3); // reset phase
00610               close (devfd);
00611               return -3;
00612             } else {
00613               printf ("DCM phase high limit is probably too high (status=%x). Try <%d (instead of %d)\n",a,i,mx);
00614               ioctl(devfd, _IO(FPGACONF_WRITEREG, X313_WA_DCM ), 3); // reset phase
00615               close (devfd);
00616               return -4;
00617             }
00618           }
00619           ioctl(devfd, _IO(FPGACONF_WRITEREG, X313_WA_DCM ), 2); // decrease phase
00620         }
00621 // NEW: reset PLL in SDRAM after each phase change - is that really needed?
00622         ioctl(devfd, _IO(FPGACONF_WRITEREG, X313_WA_SD_MODE ), 0); // SDRAM, refresh - off - will reinit on attempt to read
00623 // read, reset errors and read again, so phase will be settled after change
00624         if (readSDRAMdummy()<0) {close (devfd); return -1;}
00625         ioctl(devfd, _IO(FPGACONF_WRITEREG, X313_WA_DCM ), 0);
00626         if (readSDRAMdummy()<0) {close (devfd); return -1;}
00627 //        tables[i]=(ioctl(devfd, _IO(FPGACONF_READREG, X313__RA__STATUS ), 0)>>21) & 3;
00628         tables[i]=DCM_ERR(ioctl(devfd, _IO(FPGACONF_READREG, X313__RA__STATUS ), 0));
00629       }
00630 // calculate and report results
00631       for (i=0; i<((mx-mn)+1); i++) {
00632         printf (" %d",tables[i]);
00633         if ((i & 0xf)==0xf) printf ("\r\n");
00634       }
00635       printf ("\r\n");
00636 
00637       state=0; // 0 - looking for "1", 1 - looking for "3", 2 - looking for 2, 3 - done
00638       for (i=0;(i<=(mx-mn)) && (state<3); i++) 
00639         switch (state) {
00640         case 0: if      (tables[i]==1)  state = 1; break;
00641         case 1: if      (tables[i]==3) {state = 2; dqmn=mn+i;dqmx=dqmn;}
00642                 else if (tables[i]==2)  state = 0;
00643                 break;
00644         case 2: if      (tables[i]==2)  state = 3;
00645                 else if (tables[i]==1)  state = 1;
00646                 else     dqmx=mn+i;
00647                 break;
00648         }
00649       if (state < 2) {close (devfd);printf("failed to find lower margin\n"); return -2;}
00650       if (state < 3) {close (devfd);printf("failed to find upper margin\n"); return -2;}
00651       printf ("phase uncertainty range = %d (minimal %d, maximal - %d)\r\n",dqmx-dqmn,dqmn,dqmx);
00652       printf ("optimal phase = %d\r\n",(dqmx+dqmn)>>1);
00653       if (corr) {
00654          printf ("manually corrected optimal phase = %d\r\n",((dqmx+dqmn)>>1)+corr);
00655       }
00656       corr+=((dqmx+dqmn)>>1);
00657 //set the calculated phase (it was now left at mx      
00658       if (corr <= mx) {
00659         for (i=mx; i > corr; i--) {
00660           for (retry=DCM_RETRY;retry>0;retry--) {
00661              a=ioctl(devfd, _IO(FPGACONF_READREG, X313__RA__STATUS ), 0);
00662              if (IS_DCM_GOOD (a)) break;
00663           }
00664           if (retry <=0) {
00665             if (i>(mx-5)) {
00666               printf ("DCM phase high limit is probably too high (pass3, status=%x). Try <%d (instead of %d)\n",a,i,mx);
00667               ioctl(devfd, _IO(FPGACONF_WRITEREG, X313_WA_DCM ), 3); // reset phase
00668               close (devfd);
00669               return -4;
00670             } else {
00671               printf ("DCM failed to set to the optimal phase ( should not be so - status=%x at phase %d)\n",a,i);
00672               ioctl(devfd, _IO(FPGACONF_WRITEREG, X313_WA_DCM ), 3); // reset phase
00673               close (devfd);
00674               return -5;
00675             }
00676           }
00677           ioctl(devfd, _IO(FPGACONF_WRITEREG, X313_WA_DCM ), 1); // increase phase
00678         }
00679       } else {
00680         for (i=mx; i < corr; i++) {
00681           for (retry=DCM_RETRY;retry>0;retry--) {
00682              a=ioctl(devfd, _IO(FPGACONF_READREG, X313__RA__STATUS ), 0);
00683              if (IS_DCM_GOOD (a)) break;
00684           }
00685           if (retry <=0) {
00686             printf ("DCM corrected phase (%d) is probably too high. Failed at %d (with status=%x)\n",corr,i,a);
00687             ioctl(devfd, _IO(FPGACONF_WRITEREG, X313_WA_DCM ), 3); // reset phase
00688             close (devfd);
00689             return -4;
00690           }
00691           ioctl(devfd, _IO(FPGACONF_WRITEREG, X313_WA_DCM ), 2); // decrease phase
00692         }
00693       }
00694 // now turn off refresh so SDRAM will be initialized next time
00695 #if 0 // What the hell was that???
00696       ra=0x20;
00697       rd=0x15555; // all channels reset, stop refresh (so SDRAM will be initialized when opened)
00698       ioctl(devfd, _IO(FPGACONF_WRITEREG, ra ), rd);      
00699 #endif
00700 
00701       ioctl(devfd, _IO(FPGACONF_WRITEREG, X313_WA_SD_MODE ), 0); // SDRAM, refresh - off - will reinit on attempt to read
00702       readSDRAMdummy();
00703                 // need to automatic check result
00704       printf("result=OK\n");
00705 
00706       close (devfd);
00707       return 0;
00708    }
00709    if (strcasecmp(argv[1], "-dbg")==0) { // read out internal debug data
00710      if (argc<3) {printf("Bit position and bit width are expected (optionally followed by preceeding text ('_' instead of spaces))\r\n");return -1;}
00711      bp= strtol (argv[2],&cp,10);
00712      if (argc >3) bq= strtol (argv[3],&cp,10);
00713      else bq=32;
00714      if (bq>32) bq=32;
00715      if ((devfd = open("/dev/fpgaio", O_RDWR))<0)  {printf("error opening /dev/fpgaio\r\n"); return -1;}
00716      res=ioctl(devfd, _IO(FPGACONF_WRITEREG, 0x10 ),0); // strobe all data
00717      for (i=0; i<bp;i++) res=ioctl(devfd, _IO(FPGACONF_READREG4, 0x10 ),0);
00718      d=0;
00719      for (i=0;i<32;i++)
00720        if ((i<bq) && (ioctl(devfd, _IO(FPGACONF_READREG4, 0x10 ),0) & 0x40000000))  d=(d>>1) | 0x80000000;
00721        else d=(d>>1) & 0x7fffffff;
00722      close (devfd);
00723      if (argc >4) {
00724       for (cp=argv[4];cp[0];cp++) if (cp[0]!='_') printf("%c",cp[0]);
00725                                   else printf(" ");
00726       printf(" ");
00727      }
00728      fvx[2]='1'+(bq>>2);
00729      printf(fvx,d);
00730      printf("\n");
00731      return 0;
00732    }
00733 
00734    if (strcasecmp(argv[1], "-tr")==0) { // test register A N
00735      if (argc<4) {printf("A nad N (hex) are expected\r\n");return -1;}
00736      a= strtol (argv[2],&cp,16);
00737      n= strtol (argv[3],&cp,16);
00738      ands[0]=0xffffffff;
00739      ors[0]=0;
00740      if ((devfd = open("/dev/fpgaio", O_RDWR))<0)  {printf("error opening /dev/fpgaio\r\n"); return -1;}
00741      for (i=0; i<n;i++) {
00742        j=ioctl(devfd, _IO(FPGACONF_READREG, a ),0);
00743        ands[0] &= j;
00744        ors[0] |= j;
00745      }
00746      printf ("and=0x%x, or=0x%x\n",ands[0],ors[0]);
00747      close(devfd);
00748      return 0;
00749    }
00750     
00751    if (strcasecmp(argv[1], "-mem")==0) { // line width (in pixels) and file name - starts at address 0
00752       comp_mode=0; // compare with previous
00753       n=0x2000000; // full memory size in 16-bit words
00754       if (argc>=3) {
00755         n= strtol (argv[2],&cp,16);
00756         if (argc >=7) {
00757           comp_mode=1; // compare with pattern
00758           ptrn16[0]= strtol (argv[3],&cp,16);
00759           ptrn16[1]= strtol (argv[4],&cp,16);
00760           ptrn16[2]= strtol (argv[5],&cp,16);
00761           ptrn16[3]= strtol (argv[6],&cp,16);
00762         }
00763       }
00764       if (n>0x2000000) n=0x2000000;
00765       n=n>>12; // 4096 (of short int) pages
00766       if (n<1) n=1;
00767 // fill memory with pattern if needed
00768       if (comp_mode) {
00769         for (i=0;i<2048;i+=2) {tables[i]=ptrn[0];tables[i+1]=ptrn[1];}
00770         if ((devfd = open("/dev/fsdram", O_RDWR))<0)  {printf("error opening /dev/fsdram\r\n"); return -1;}
00771         for (i=0;i<n;i++) if ((res=write(devfd,tables,8192))<8192) {
00772             printf("write failed, returned %d, expected - %d\n",res, 8192);
00773             close (devfd);
00774             return -1;
00775         }
00776         close (devfd);
00777       }  
00778 // now read and analyze      
00779       if ((devfd = open("/dev/fsdram", O_RDONLY))<0)  {printf("error opening /dev/fsdram\r\n"); return -1;}
00780       ands[0]=0xffffffff;
00781       ands[1]=0xffffffff;
00782       ors[0]=0;
00783       ors[1]=0;
00784       nerr=0;
00785       for (i=0;i<n;i++) {
00786         if ((res=read(devfd,tables,8192))<8192) {
00787           printf("read failed, returned %d, expected - %d\n",res, 8192);
00788           close (devfd);
00789           return -1;
00790         }
00791         for (j=0;j<2048;j+=2) {
00792           ands[0] &= tables[j];
00793           ands[1] &= tables[j+1];
00794           ors[0]  |= tables[j];
00795           ors[1]  |= tables[j+1];
00796         }
00797         if (comp_mode) {
00798           for (j=0;j<2048;j+=2) {
00799             tables[j]   ^= ptrn[0];
00800             tables[j+1] ^= ptrn[1];
00801           }
00802         } else {
00803           for (j=2;j<2048;j+=2) {
00804             tables[j-2] ^= tables[j];
00805             tables[j-1] ^= tables[j+1];
00806           }
00807           tables[2046]=0;
00808           tables[2047]=0;
00809         }
00810         for (j=0;j<8192;j++)  {
00811          nerr+=bits_in_byte[btables[j]];
00812 //         if (btables[j]) printf("btables[%d]=0x%x, bits_in_byte[btables[%d]]=%d, nerr=%d\n",j,(int) btables[j],j,bits_in_byte[btables[j]],nerr);
00813         } 
00814       }
00815                 // print test result for automatic tests
00816                 if(nerr == 0)
00817                         printf("result=OK\n");
00818                 else
00819                         printf("result=FAIL\n");
00820       printf                ("wrong bits - 0x%x (in 0x%x tested 16-bit words)\n",nerr,(n<<12));
00821       printf                ("and     %4x %4x %4x %4x\n",ands16[0],ands16[1],ands16[2],ands16[3]);
00822       printf                ("or      %4x %4x %4x %4x\n",ors16[0], ors16[1], ors16[2], ors16[3]);
00823       if (comp_mode) printf ("pattern %4x %4x %4x %4x\n",ptrn16[0],ptrn16[1],ptrn16[2],ptrn16[3]);
00824       
00825 
00826       close (devfd);
00827       return 0;
00828    }
00829 
00830    
00831    
00832          
00833    if (strcasecmp(argv[1], "-raw")==0) { // line width (in pixels) and file name - starts at address 0
00834       if (argc<3) {printf("Line length (decimal, pixels) and filename with pixel data (hex bytes)\r\n");return -1;}
00835       ll= strtol (argv[2],&cp,10);
00836 
00837       if ((tfile = fopen(argv[3], "r"))==NULL) {printf("error opening file %s\n",argv[3]); return -1;}
00838       if ((devfd = open("/dev/fsdram", O_RDWR))<0)  {printf("error opening /dev/fsdram\r\n"); fclose (tfile); return -1;}
00839       d=0;
00840       n=0;
00841       nl=0; // line number
00842       
00843       fline[1023]=0;
00844       while (((cp=fgets(fline,1023,tfile)) !=NULL) && (n<2048)) {
00845        while (((d=strtol (cp,&ep,16)), ((ep-fline)<strlen(fline))) && (n<2048) && (ep != cp)){
00846          btables[n++]=d;
00847          cp=ep;
00848          if (n>=ll) {
00849            for (i=0; i<ll; i+=256) {
00850              nt=(nl>>4)*(((ll-1)>>8)+1)+(i>>8); // number of block with the same RA
00851              sa=((nt >>  1) << 13) + //in channel 3 blocks with the same RA go in pairs - each 512*16 pixels/bytes
00852                 ((nt &   1) << 8)  + // odd blocks add 256 in channel3 pointer
00853                 ((nl & 0xf) << 9);  // "lines" in channel 3 are 512 bytes long
00854              lseek(devfd,sa,0);
00855              write(devfd,&btables[i],((ll-i) > 256)?256:(ll-i));
00856            }
00857            nl++;
00858            n=0;
00859          }
00860        }
00861       }
00862       fclose (tfile);
00863       close (devfd);
00864       printf("read %d lines, each %d pixels long\n", (int) nl, (int) ll);
00865       return 0;
00866    }
00867    
00868    
00869    
00870    if (strcasecmp(argv[1], "-mcu")==0) {
00871       sa=0x280000;   
00872       if (argc<2) {printf("Address and filename\r\n");return -1;}
00873 
00874       if ((tfile = fopen(argv[2], "r"))==NULL) {printf("error opening file %s\n",argv[2]); return -1;}
00875       d=0;
00876       n=0;
00877       fline[1023]=0;
00878 
00879                 
00880       while (((cp=fgets(fline,1023,tfile)) !=NULL) && (n<2048)) {
00881        while (((d=strtol (cp,&ep,16)), ((ep-fline)<strlen(fline))) && (n<2048) && (ep != cp)){
00882          btables[n++]=d;
00883          cp=ep;
00884        }
00885       }
00886       fclose (tfile);
00887       printf("read %d elements\n",n);
00888 // image format is expected to be not more than 256 pixels wide, 8 bit
00889       if ((devfd = open("/dev/fsdram", O_RDWR))<0)  {printf("error opening /dev/fsdram\r\n"); return -1;}
00890       for (i=0;i<n;i+=16){
00891         lseek(devfd,sa+ ((i & 0xf0) << 4)+((i & 0xf00)>>4),0);
00892         write(devfd,&btables[i],16);
00893       }
00894       close (devfd);
00895       return 0;
00896    }    
00897         if (argv[1][0] != '-'){printf(usage); return 0;}
00898    if (strcasecmp(argv[1], "-table")==0) {
00899       if (argc<3) {
00900 // no reading table for now      
00901 /*      
00902          if ((devfd = open("/dev/fpgaio", O_RDWR))<0)  {printf("error opening /dev/fpgaio\r\n"); return -1;}
00903          for (i=0; i<256; i++) {
00904            ioctl(devfd, _IO(FPGACONF_WRITEREG, 0x0d ), i);
00905            ioctl(devfd, _IO(FPGACONF_WRITEREG, 0x0d ), i);
00906            ioctl(devfd, _IO(FPGACONF_WRITEREG, 0x0d ), i);
00907            printf ("%6x",ioctl(devfd, _IO(FPGACONF_WRITEREG, 0x14 ), 0));
00908            if ((i & 0x7)==0x7) printf("\n");
00909            if ((i & 0x3f)==0x3f) printf("\n");
00910          }
00911          for (i=512; i<1024; i++) {
00912            ioctl(devfd, _IO(FPGACONF_WRITEREG, 0x0d ), i);
00913            ioctl(devfd, _IO(FPGACONF_WRITEREG, 0x0d ), i);
00914            ioctl(devfd, _IO(FPGACONF_WRITEREG, 0x0d ), i);
00915            printf ("%6x",ioctl(devfd, _IO(FPGACONF_WRITEREG, 0x14 ), 0));
00916            if ((i & 0xf)==0xf) printf("\n");
00917            if ((i & 0xff)==0xff) printf("\n");
00918 
00919          }
00920          close (devfd);
00921 */         
00922          return 0;
00923       }
00924       if (argc==3)  {
00925         sa=0;
00926         cp=argv[2];
00927       } else {
00928         sa= strtol (argv[2],&cp,16);
00929         cp=argv[3];
00930       }
00931 //
00932       // try reading table data to memory
00933       if ((tfile = fopen(cp, "r"))==NULL) {printf("error opening file %s\n",cp); return -1;}
00934       d=0;
00935       i=0;
00936       fline[1023]=0;
00937                 
00938       while (((cp=fgets(fline,1023,tfile)) !=NULL) && (i<1024)) {
00939        while (((d=strtol (cp,&ep,16)), ((ep-fline)<strlen(fline))) && (i<2048) && (ep != cp)){
00940          tables[i++]=d;
00941          cp=ep;
00942        }
00943       }
00944       fclose (tfile);
00945         
00946       printf("read %d elements\n",i);
00947 /*
00948       for (j=0;j<i;j++) {
00949         printf("%6x ",tables[j]);
00950         if ((j & 0xf)==0xf) printf("\n");
00951 
00952       }
00953 */
00954       if ((devfd = open("/dev/fpgaio", O_RDWR))<0)  {printf("error opening /dev/fpgaio\r\n"); return -1;}
00955 
00956 //      ioctl(devfd, _IO(FPGACONF_WRITEREG, 0x0d ), sa);
00957 //      for (j=0;j<i;j++) ioctl(devfd, _IO(FPGACONF_WRITEREG, 0x0e ), tables[j]);
00958       ioctl(devfd, _IO(FPGACONF_WRITEREG, 0x0e ), sa);
00959       for (j=0;j<i;j++) ioctl(devfd, _IO(FPGACONF_WRITEREG, 0x0f ), tables[j]);
00960       printf ("%x\n",(int) d);
00961       close (devfd);
00962       return 0;
00963     }
00964 
00965     if (strcasecmp(argv[1], "-jpeg")==0) {
00966       if (argc<3) {printf("Need JPEG command number\r\n");return -1;}
00967       i= strtol (argv[2],&cp,16);
00968 // had a problem turning off compression mode from the sctl.cgi as "/dev/ccam_dma.raw" was busy.
00969 // changed (only for "d") to "/dev/sensorpars" that does not check for busy
00970       if (i==0x0d)
00971         {
00972       if ((devfd = open("/dev/sensorpars", O_RDONLY))<=0) {
00973           printf("error opening /dev/sensorpars, errno=%d\n",errno);
00974 #ifdef DEBUG_USAGE
00975           fpcflog = fopen("/var/log/fpcf.log", "a");
00976           fprintf (fpcflog, " error opening /dev/sensorpars, errno=%d\n",errno);
00977 // still getting errno=9 "Bad file numer"???
00978           fclose(fpcflog);
00979 #endif
00980           return -1;}
00981       } else {
00982         if ((devfd = open("/dev/ccam_dma.raw", O_RDONLY))<=0) {
00983           printf("error opening /dev/ccam_dma.raw, errno=%d\n",errno);
00984 #ifdef DEBUG_USAGE
00985           fpcflog = fopen("/var/log/fpcf.log", "a");
00986           fprintf (fpcflog, " error opening /dev/ccam_dma.raw, errno=%d\n",errno);
00987           fclose(fpcflog);
00988 #endif
00989           return -1;}
00990       }
00991       res=ioctl(devfd, _IO(CMOSCAM_IOCTYPE, IO_CCAM_JPEG ), i);
00992       printf("0x%x\n",res);
00993 #ifdef DEBUG_USAGE
00994 //  FILE *  fpcflog;
00995   if ((fpcflog = fopen("/var/log/fpcf.log", "a"))==NULL) {printf("error opening file %s\n","/var/log/fpcf.log"); return -1;}
00996   
00997    fprintf (fpcflog, " 0x%x\n",res);
00998   fclose(fpcflog);
00999 #endif
01000       close (devfd);
01001       return 0;
01002     }
01003 
01004     if (strcasecmp(argv[1], "-jctl")==0) {
01005       if (argc<3) {printf("Need JPEG control word\r\n");return -1;}
01006       i= strtol (argv[2],&cp,16);
01007       if ((devfd = open("/dev/ccam_dma.raw", O_RDONLY))<=0) {printf("error opening /dev/ccam_dma.raw\n"); return -1;}
01008       res=ioctl(devfd, _IO(CMOSCAM_IOCTYPE, IO_CCAM_JPEG_CTRL ), i);
01009       close (devfd);
01010       return 0;
01011     }
01012     if (strcasecmp(argv[1], "-jn")==0) {
01013       if (argc<3) {printf("Need number of frames to acquire\r\n");return -1;}
01014       i= strtol (argv[2],&cp,16);
01015       if ((devfd = open("/dev/ccam_dma.raw", O_RDONLY))<=0) {printf("error opening /dev/ccam_dma.raw\n"); return -1;}
01016       res=ioctl(devfd, _IO(CMOSCAM_IOCTYPE, IO_CCAM_JPEG_GET_N ), i);
01017       close (devfd);
01018       return 0;
01019     }
01020     if (strcasecmp(argv[1], "-jl")==0) {
01021       if (argc<3) {printf("Need dadat length (in 32-bit words) to acquire\r\n");return -1;}
01022       i= strtol (argv[2],&cp,16);
01023       if ((devfd = open("/dev/ccam_dma.raw", O_RDONLY))<=0) {printf("error opening /dev/ccam_dma.raw\n"); return -1;}
01024       res=ioctl(devfd, _IO(CMOSCAM_IOCTYPE, IO_CCAM_JPEG_GET_L ), i);
01025       close (devfd);
01026       return 0;
01027     }
01028     if (strcasecmp(argv[1], "-dma")==0) {
01029       if (argc<3) {printf("Need DMA command number\r\n");return -1;}
01030           i= strtol (argv[2],&cp,16);
01031       if ((i<0) || (i>2)) {printf("Supported DMA commands are 0,1 and 2 only\r\n");return -1;}
01032       if ((devfd = open("/dev/ccam_dma.raw", O_RDONLY))<=0) {printf("error opening /dev/ccam_dma.raw\n"); return -1;}
01033       res=ioctl(devfd, _IO(CMOSCAM_IOCTYPE, IO_CCAM_DMA ), i);
01034       close (devfd);
01035       return 0;
01036     }
01037 
01038     if ((strcasecmp(argv[1], "-dr" )==0) || 
01039         (strcasecmp(argv[1], "-drb")==0) ||
01040         (strcasecmp(argv[1], "-drw")==0) ||
01041         (strcasecmp(argv[1], "-drl")==0)) {
01042       ws=0;
01043       if (strcasecmp(argv[1], "-drw")==0) ws=1;
01044       if (strcasecmp(argv[1], "-drl")==0) ws=2;
01045       if (argc<3) {
01046             if ((devfd = open("/dev/ccam_dma.raw", O_RDONLY))<=0) {printf("error opening /dev/ccam_dma.raw\n"); return -1;}
01047         fs=lseek(devfd,0,2);
01048         if (fs>=0) fs>>=ws;
01049         printf("DMA received 0x%x (%d) ", (int) fs, (int) fs);
01050         if      (ws==0) printf("bytes\n");
01051         else if (ws==1) printf("words\n");
01052         else            printf("long words\n");
01053         close (devfd);
01054         return 0;
01055       }
01056           sa= strtol (argv[2],&cp,16);
01057       n=1; if (argc >3) n= strtol (argv[3],&cp,16);
01058       if ((devfd = open("/dev/ccam_dma.raw", O_RDONLY))<=0) {printf("error opening /dev/ccam_dma.raw\n"); return -1;}
01059       while (n>0) {
01060         i=n; if (i>((FPCF_SDBUFFER_SIZE<<1)>>ws)) i=((FPCF_SDBUFFER_SIZE<<1)>>ws);
01061         if ((res=lseek(devfd,sa<<ws,0))<0) {
01062           printf("lseek failed, returned %d\n",res);
01063           close (devfd);
01064           return -1;
01065         }
01066 //    printf("lseek OK, returned %d\n",res);
01067         if ((res=read(devfd,usbuf,i<<ws))<(i<<ws)) {
01068           if (res!=0) printf("\nread failed, returned %d bytes, expected - %d\n",res, (i<<ws));
01069           if (res <= 0 ) {
01070             close (devfd);
01071             return -1;
01072           }
01073           i=(res>>ws);
01074         }
01075 // print buffer
01076         for (j=0;j<i;j++) {
01077           if ( ((sa+j) & 0x0f) == 0) printf("\n%06x:",(int) (sa+j));
01078           if      (ws==0) printf (" %2x", (int) usbuf8 [j]);
01079           else if (ws==1) printf (" %4x", (int) usbuf16[j]);
01080           else            printf (" %8x", (int) usbuf32[j]);
01081         }
01082         sa += i;
01083         n  -= i;
01084       }
01085       printf("\n");
01086       close (devfd);
01087       return 0;
01088 
01089 
01090     }
01091 
01092     shft=-1;
01093     res=0;
01094     if (strcasecmp(argv[1], "-sr")   ==0) shft=1;
01095     if (strcasecmp(argv[1], "-sr8")  ==0) shft=0;
01096     if (strcasecmp(argv[1], "-sr16") ==0) shft=1;
01097     if (strcasecmp(argv[1], "-sr32") ==0) shft=2;
01098     if (shft>=0) {
01099 //    if ((strcasecmp(argv[1], "-sr8")==0) { // read SDRAM through PIO
01100       if (argc<3) {printf("Need SDRAM address (hex) to read\r\n");return -1;}
01101            sa= strtol (argv[2],&cp,16);
01102       n=1; if (argc >3) n= strtol (argv[3],&cp,16);
01103       if ((devfd = open("/dev/fsdram", O_RDONLY))<0)  {printf("error opening /dev/fsdram\r\n"); return -1;}
01104 //      printf("devfd=%d, sa=%x, n=%x\n",devfd, (int) sa,n);
01105       while (n>0) {
01106         i=n; if (i>(sizeof(usbuf)>>shft)) i=sizeof(usbuf)>>shft;
01107         if ((res=lseek(devfd,sa<<shft,0))<0) {
01108           printf("lseek failed, returned %d\n",res);
01109           close (devfd);
01110           return -1;
01111         }
01112         if ((res=read(devfd,usbuf,i<<shft))<(i<<shft)) {
01113           printf("read failed, returned %d, expected - %d\n",res, (i<<shft));
01114           close (devfd);
01115           return -1;
01116         }
01117 // print buffer
01118         for (j=0;j<i;j++) {
01119 //          if (res && ( ((sa+j) & 0x0f) == 0)) printf("\n%06x:",(int) (sa+j));
01120           if (res && ( ((sa+j) &  ((0x20 >> shft)-1)) == 0)) printf("\n%06x:",(int) (sa+j));
01121 //          printf (" %4x", (int) usbuf[j]);
01122           if      (shft==0)  printf (" %2x", (int) usbuf8[j]);
01123           else if (shft==1)  printf (" %4x", (int) usbuf16[j]);
01124           else if (shft==2)  printf (" %8x", (int) usbuf32[j]);
01125           res=1;
01126         }
01127         sa += i;
01128         n  -= i;
01129       }
01130       printf("\n");
01131       close (devfd);
01132       return 0;
01133     }
01134     shft=-1;
01135     if (strcasecmp(argv[1], "-sw")   ==0) shft=1;
01136     if (strcasecmp(argv[1], "-sw8")  ==0) shft=0;
01137     if (strcasecmp(argv[1], "-sw16") ==0) shft=1;
01138     if (strcasecmp(argv[1], "-sw32") ==0) shft=2;
01139 //    if (strcasecmp(argv[1], "-sw")==0) { // write SDRAM through PIO
01140     if (shft>=0)
01141         { // write SDRAM through PIO
01142       if (argc<3) {printf("Need SDRAM address (hex) to write data\r\n");return -1;}
01143       if (argc<4) {printf("Need SDRAM data (hex) to write to SDRAM\r\n");return -1;}
01144            sa= strtol (argv[2],&cp,16);
01145       uld=(unsigned long) strtoll (argv[3],&cp,16);
01146       n=1;   if (argc >4) n=   (unsigned long) strtoll (argv[4],&cp,16);
01147       uli=0; if (argc >5) uli= (unsigned long) strtoll (argv[5],&cp,16);
01148 //  unsigned short usbuf[FPCF_SDBUFFER_SIZE];
01149 //  unsigned char  * usbuf8 =  (unsigned char *)  usbuf;
01150 //  unsigned short * usbuf16 = (unsigned short *) usbuf;
01151 //  unsigned long  * usbuf32 = (unsigned long *)  usbuf;
01152 
01153       if ((devfd = open("/dev/fsdram", O_RDWR))<0)  {printf("error opening /dev/fsdram\r\n"); return -1;}
01154       while (n>0) {
01155         i=n; if (i>(sizeof(usbuf)>>shft)) i=sizeof(usbuf)>>shft;
01156 //        for (j=0;j<i;j++) {usbuf[j]=uld; uld+=uli;}
01157         if      (shft==0) for (j=0;j<i;j++) {usbuf8[j] =uld; uld+=uli;}
01158         else if (shft==1) for (j=0;j<i;j++) {usbuf16[j]=uld; uld+=uli;}
01159         else if (shft==2) for (j=0;j<i;j++) {usbuf32[j]=uld; uld+=uli;}
01160         if ((res=lseek(devfd,sa<<shft,0))<0) {
01161           printf("lseek failed, returned %d\n",res);
01162           close (devfd);
01163           return -1;
01164         }
01165         if ((res=write(devfd,usbuf,i<<shft))<(i<<shft)) {
01166           printf("write failed, returned %d, expected - %d\n",res, (i<<shft));
01167           close (devfd);
01168           return -1;
01169         }
01170         sa += i;
01171         n  -= i;
01172       }
01173 
01174       close (devfd);
01175       return 0;
01176     }
01177 
01178     shft=-1;
01179     if (strcasecmp(argv[1], "-i2cr8")  ==0) shft=0;
01180     if (strcasecmp(argv[1], "-i2cr16") ==0) shft=1;
01181     if (shft>=0) { // read i2c bus0
01182       if (argc<3) {printf("Need I2C address (hex) to read: register address + 256* (slave address>>1)\r\n");return -1;}
01183       sa= strtol (argv[2],&cp,16);
01184       n=1; if (argc >3) n= strtol (argv[3],&cp,16);
01185       if (shft==1) {
01186          if ((devfd = open("/dev/xi2c16", O_RDONLY))<0)  {printf("error opening /dev/xi2c16\r\n"); return -1;}
01187       } else {
01188          if ((devfd = open("/dev/xi2c8",  O_RDONLY))<0)  {printf("error opening /dev/xi2c8\r\n"); return -1;}
01189       }
01190 //      printf("devfd=%d, sa=%x, n=%x\n",devfd, (int) sa,n);
01191       while (n>0) {
01192         i=n; if (i>(sizeof(usbuf)>>shft)) i=sizeof(usbuf)>>shft;
01193         if ((res=lseek(devfd,sa<<shft,0))<0) {
01194           printf("lseek failed, returned %d\n",res);
01195           close (devfd);
01196           return -1;
01197         }
01198         if ((res=read(devfd,usbuf,i<<shft))<(i<<shft)) {
01199           printf("read failed, returned %d, expected - %d\n",res, (i<<shft));
01200           close (devfd);
01201           return -1;
01202         }
01203 // print buffer
01204         for (j=0;j<i;j++) {
01205           if (res && ( ((sa+j) &  ((0x20 >> shft)-1)) == 0)) printf("\n%06x:",(int) (sa+j));
01206           if      (shft==0)  printf (" %2x", (int) usbuf8[j]);
01207 //          else if (shft==1)  printf (" %4x", (int) usbuf16[j]);
01208           else if (shft==1)  printf (" %4x", ((((int) usbuf16[j])>>8) & 0xff) | ((((int) usbuf16[j]) << 8) & 0xff00)  );
01209 //          else if (shft==2)  printf (" %8x", (int) usbuf32[j]);
01210           res=1;
01211         }
01212         sa += i;
01213         n  -= i;
01214       }
01215       printf("\n");
01216       close (devfd);
01217       return 0;
01218     }
01219 
01220     shft=-1;
01221     if (strcasecmp(argv[1], "-i2cw8")  ==0) shft=0;
01222     if (strcasecmp(argv[1], "-i2cw16") ==0) shft=1;
01223     if (shft>=0) { // write i2c bus0
01224       if (argc<3) {printf("Need I2C address (hex) to write: register address + 256* (slave address>>1)\r\n");return -1;}
01225       if (argc<4) {printf("Need data (hex) to write to i2c bus 0\r\n");return -1;}
01226            sa= strtol (argv[2],&cp,16);
01227       uld=strtol (argv[3],&cp,16);
01228       if (shft==1) {
01229          if ((devfd = open("/dev/xi2c16", O_RDWR))<0)  {printf("error opening /dev/xi2c16\r\n"); return -1;}
01230          uld= ((uld >>8) & 0xff) | ((uld <<8) & 0xff00);
01231       } else {
01232          if ((devfd = open("/dev/xi2c8",  O_RDWR))<0)  {printf("error opening /dev/xi2c8\r\n"); return -1;}
01233       }
01234       if ((res=lseek(devfd,sa<<shft,0))<0) {
01235         printf("lseek failed, returned %d\n",res);
01236         close (devfd);
01237         return -1;
01238       }
01239       if ((res=write(devfd,&uld,1<<shft))<(1<<shft)) {
01240         printf("write failed, returned %d, expected - %d\n",res, (1<<shft));
01241         close (devfd);
01242         return -1;
01243       }
01244       close (devfd);
01245       return 0;
01246     }
01247 // end of i2c{r,w}{8,16}
01248 
01249          if (strcasecmp(argv[1], "-s")==0) { // read FPGA state
01250       if ((devfd = open("/dev/fpgaio", O_RDONLY))<0)  {printf("error opening /dev/fpgaio\r\n"); return -1;}
01251             d=ioctl(devfd, _IO(FPGACONF_GETSTATE,0), 0) ;
01252 // add details...
01253         printf ("FPGA state is %x\r\n",(int) d);
01254         close (devfd);
01255       return 0;
01256     }
01257 
01258 // now parse direct r/w commands and arguments
01259     iorw_args=0;
01260         if (strcasecmp(argv[1], "-r")==0)    iorw_args=FPGACONF_READREG;
01261         if (strcasecmp(argv[1], "-rl")==0)   iorw_args=FPGACONF_READREG_L;
01262         if (strcasecmp(argv[1], "-rh")==0)   iorw_args=FPGACONF_READREG_H;
01263         if (strcasecmp(argv[1], "-r4")==0)   iorw_args=FPGACONF_READREG4;
01264         if (strcasecmp(argv[1], "-rl4")==0)  iorw_args=FPGACONF_READREG_L4;
01265         if (strcasecmp(argv[1], "-rh4")==0)  iorw_args=FPGACONF_READREG_H4;
01266         if (strcasecmp(argv[1], "-w")==0)    iorw_args=FPGACONF_WRITEREG;
01267         if (strcasecmp(argv[1], "-w4")==0)   iorw_args=FPGACONF_WRITEREG4;
01268         if (strcasecmp(argv[1], "-rw")==0)   iorw_args=FPGACONF_RD_WAITSTATES;
01269         if (strcasecmp(argv[1], "-ww")==0)   iorw_args=FPGACONF_WR_WAITSTATES;
01270     if (iorw_args) {
01271 // check args
01272        rd=0;
01273        ra=0;
01274        switch (iorw_args) {
01275        case FPGACONF_WRITEREG:
01276        case FPGACONF_WRITEREG4:
01277         {
01278              if (argc<4) {printf("Need 32-bit data (hex) to write to a register\r\n"); return -1;}
01279              rd= (unsigned long) strtoll (argv[3],&cp,16);
01280         }
01281       // fall to get ra
01282        case FPGACONF_READREG:
01283        case FPGACONF_READREG_L:
01284        case FPGACONF_READREG_H:
01285        case FPGACONF_READREG4:
01286        case FPGACONF_READREG_L4:
01287        case FPGACONF_READREG_H4:
01288         {
01289          if (argc<3) {printf("Need register address (hex) to read\r\n");return -1;}
01290              ra= (strtol (argv[2],&cp,16) & 0xff);
01291          break;         
01292         }
01293        case FPGACONF_WR_WAITSTATES:
01294          if (argc<3) {printf("Need value for rw_grp[i]_cfg (hex)\r\n");return -1;}
01295          if (argc<4) {
01296            ra=2;
01297            rd= (unsigned long) strtoll (argv[2],&cp,16);
01298          } else {
01299            ra= strtol (argv[2],&cp,16);
01300            rd= (unsigned long) strtoll (argv[3],&cp,16);
01301            if ((ra<1) || (ra>4)) { printf("address should be 1..4, you provided %ld\r\n",ra);return -1; }
01302          }
01303        case FPGACONF_RD_WAITSTATES:
01304          if (argc<3) {
01305            ra=2;
01306          } else {
01307            ra= strtol (argv[2],&cp,16);
01308            if ((ra<1) || (ra>4)) { printf("address should be 1..4, you provided %ld\r\n",ra);return -1; }
01309          }
01310        break;           
01311        default: {printf("unrecognised IORW command # %d\r\n",iorw_args);return -1;} 
01312       }
01313       if ((devfd = open("/dev/fpgaio", O_RDWR))<0)  {printf("error opening /dev/fpgaio\r\n"); return -1;}
01314       d=ioctl(devfd, _IO(iorw_args, ra ), rd); // works for both W and R
01315       printf ("%x\n",(int) d);
01316       close (devfd);
01317 //      return d; 
01318       return 0; // OK
01319     }
01320     if (strcasecmp(argv[1], "-button")==0) {
01321        if ((devfd = open("/dev/fpgaconfi2c", O_RDWR))<0)  {printf("error opening /dev/fpgaconfi2c\r\n"); return -1;}
01322        d=(ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_PA_RD ),0)>>6) & 1;
01323        printf ("%d\n",(int) d);
01324        close (devfd);
01325        return d;
01326     } 
01327  
01328 // read/write port A (JTAG to FPGA)
01329         if ((argv[1][1] == 'a') || (argv[1][1] == 'A')) { 
01330           if ((devfd = open("/dev/fpgaconfi2c", O_RDWR))<0)  {printf("error opening /dev/fpgaconfi2c\r\n"); return -1;}
01331           if (argc<3) { // read port A
01332             d=ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_PA_RD ),0);
01333             printf ("%x\n",(int) d);
01334           } else {     // write port A
01335             d=strtol (argv[2],&cp,16);    
01336             ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_PA_WR ),d);
01337           }
01338           close (devfd);
01339           return d;
01340         }
01341 //FPGA programming - not needed anymore as it is possible just to "cat filename >/dev/fpgaconfjtag
01342 //  #define FPGA_JTAG_ARG(tms, len, d) (((tms) << 11) | ((len) << 8) | ((d) & 0xff))
01343         if ((argv[1][1] == 't') || (argv[1][1] == 'T')) { // send/get JTAG command
01344           d=0; n= 0; i=0;
01345           if (argc>4) {
01346             d=(strtol (argv[4],&cp,16) & 0xff);
01347             n=(strtol (argv[3],&cp,16) & 0x7);
01348             i=(strtol (argv[2],&cp,16) & 0x1);
01349           } else if (argc>3) {
01350             d=(strtol (argv[3],&cp,16) & 0xff);
01351             n=(strtol (argv[2],&cp,16) & 0x7);
01352           } else if (argc>2) {
01353             d=(strtol (argv[2],&cp,16) & 0xff);
01354           }
01355           if ((devfd = open("/dev/fpgaconfi2c", O_RDWR))<0)  {printf("error opening /dev/fpgaconfi2c\r\n"); return -1;}
01356           j=ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_JTAG ),FPGA_JTAG_ARG(i, n, d ));
01357           printf ("JTAG returned %x\n", j);
01358           close (devfd);
01359           return 0;
01360         }
01361         if ((argv[1][1] == 'p') || (argv[1][1] == 'P')) { // program FPGA
01362 //        if (argc<3) {
01363 //            printf("Need FILENAME to load\r\n");
01364 //            return -1;
01365 //        }
01366           if ((devfd = open("/dev/fpgaconfi2c", O_RDWR))<0)  {printf("error opening /dev/fpgaconfi2c\r\n"); return -1;}
01367           if (argc>2) {
01368             if ((ifd=open(argv[2],O_RDONLY))<0) {printf("error opening bitstream file %s\r\n",argv[2]); close(devfd); return -1;}
01369           } else ifd=-1;  
01370           i=0;
01371 
01372           n=-1;
01373           if ( ifd>=0) {
01374             while ((i<1000) && (read(ifd,&charheadbuf[(i++) & 7],1)>0)) {
01375               j=0;
01376               while ((j<8) && (charheadbuf[(i+j) & 7]==syncseq[j])) j++;
01377 
01378               if (j==8) {n=i-8;break;}
01379 /* 
01380             if (i<80) {
01381              bp=charheadbuf[i & 7];
01382              printf ("i= %d,j=%d,
01383 charheadbuf[i & 7]=%d \n",i,j,bp);
01384             }
01385 */
01386             }
01387             if (n<0)  {printf("No <ff ff ff ff aa 99 55 66> syncseq found\r\n"); close(devfd); return -1;}
01388             printf ("Skipping header %d bytes long\n",n);
01389           }
01390 
01391 //  while ((nr=read(ifd,dbi,USEBUFFERSIZE<<1))>0)
01392           // pulse PROGRAM pin;
01393           ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_PGM ),1);     // set PROGRAM on  (logic low)
01394           ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_PGM ),0);     // set PROGRAM off (logic high)
01395           retr=0;
01396           while ((retr < 100000) && ((ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_STAT ),0) & 1) == 0)) retr ++;
01397           printf ("waited for INIT high %d cycles\r\n",retr);
01398           if ((ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_STAT ),0) & 1) == 0) {
01399             close (ifd);
01400             close (devfd);
01401             printf("FPGA not ready\r\n");
01402             return -2;
01403           }
01404 // actual programming
01405           ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_JTAG ),FPGA_JTAG_ARG(1, 5, 0   )); //step 1 - set Test-Logic-Reset state
01406           ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_JTAG ),FPGA_JTAG_ARG(0, 1, 0   )); //step 2 - set Run-Test-Idle state
01407           ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_JTAG ),FPGA_JTAG_ARG(1, 2, 0   )); //step 3 - set SELECT-IR state
01408           ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_JTAG ),FPGA_JTAG_ARG(0, 2, 0   )); //step 4 - set SHIFT-IR state
01409           ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_JTAG ),FPGA_JTAG_ARG(0, 5, 0xa0)); //step 5 - start of CFG_IN ***NOW 6 bits ***
01410           ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_JTAG ),FPGA_JTAG_ARG(1, 1, 0   )); //step 6 - finish CFG_IN
01411           ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_JTAG ),FPGA_JTAG_ARG(1, 2, 0   )); //step 7 - set SELECT-DR state
01412           ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_JTAG ),FPGA_JTAG_ARG(0, 2, 0   )); //step 8 - set SHIFT-DR state
01413 
01414 // send 8-byte syncseq
01415           for (i=0; i<8; i++) {
01416                      j=ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_JTAG ),FPGA_JTAG_ARG(0, 0, syncseq[i] )); //shift 8 data bits
01417                 printf("i=%d, j=%x, syncseq[i]=%x\n",i,j,(int) syncseq[i]);
01418           }
01419 
01420           testnum=7;
01421      retr=0;
01422           if (ifd>=0) {
01423             read(ifd,&b0,1);  b=b0;     //for (i=0;i<8;i++) {b=(b<<1) | (b0 & 1); b0>>=1;} b&=0xff;
01424             while (read(ifd,&b0,1)>0) {
01425 testcs+=b;
01426 testnum++;
01427              i= ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_JTAG ),FPGA_JTAG_ARG(0, 0, b ));  //shift 8 data bits
01428 //          if (testnum<90) printf("num=%5x, wr= %2x, rd=%3x\n",testnum,(int) b, i );
01429 
01430               if (i & 0x100) { //readback mismatch, but printed are wrong data
01431                   printf("readback mismatch at %x - written:%x, read: %x\n",testnum,(int) b, i & 0xff);
01432               }
01433 
01434               b=b0;
01435 //        for (i=0;i<8;i++) {b=(b<<1) | (b0 & 1); b0>>=1;} b&=0xff;
01436 
01437 //       if ((retr & 0xfff)==0) {printf("\r\nretr=%d\r\n",retr);getchar ();}
01438 //       if (retr <10) {printf("\r\nafter step9 (byte %d) - %d\r\n",retr,ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_STAT ),0));getchar ();}
01439        retr++;
01440             }
01441           } else {
01442             b0=0; b=b0;
01443             for (n=0;n<8;n++) {
01444              b0=0;
01445 testcs+=b;
01446 testnum++;
01447              i= ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_JTAG ),FPGA_JTAG_ARG(0, 0, b ));  //shift 8 data bits
01448              if (testnum<90) printf("num=%5x, wr= %2x, rd=%3x\n",testnum,(int) b, i );
01449 
01450               if (i & 0x100) { //readback mismatch, but printed are wrong data
01451                   printf("readback mismatch at %x - written:%x, read: %x\n",testnum,(int) b, i & 0xff);
01452               }
01453 
01454               b=b0;
01455 //        for (i=0;i<8;i++) {b=(b<<1) | (b0 & 1); b0>>=1;} b&=0xff;
01456 
01457 //       if ((retr & 0xfff)==0) {printf("\r\nretr=%d\r\n",retr);getchar ();}
01458 //       if (retr <10) {printf("\r\nafter step9 (byte %d) - %d\r\n",retr,ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_STAT ),0));getchar ();}
01459        retr++;
01460             }
01461             close (devfd);
01462             return 0;
01463           } // end of test mopde  
01464 testcs+=b;
01465 testnum++;
01466           ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_JTAG ),FPGA_JTAG_ARG(0, 7, b   )); //shift 7 data bits
01467           ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_JTAG ),FPGA_JTAG_ARG(1, 1, (b<<7))); //step 10 - shift last data bit
01468           ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_JTAG ),FPGA_JTAG_ARG(1, 1, 0   )); //step 11 - set UPDATE-DR state
01469           ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_JTAG ),FPGA_JTAG_ARG(1, 2, 0   )); //step 12 - set SELECT-IR state
01470           ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_JTAG ),FPGA_JTAG_ARG(0, 2, 0   )); //step 13 - set SHIFT-IR state
01471           ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_JTAG ),FPGA_JTAG_ARG(0, 5, 0x30)); //step 14 - start of JSTART  ***NOW 6 bits ***
01472           ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_JTAG ),FPGA_JTAG_ARG(1, 1, 0   )); //step 15 - finish JSTART
01473           ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_JTAG ),FPGA_JTAG_ARG(1, 2, 0   )); //step 16 - set SELECT-DR state
01474           ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_JTAG ),FPGA_JTAG_ARG(0, 0, 0   )); //step 17 - set SHIFT-DR , clock startup
01475           ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_JTAG ),FPGA_JTAG_ARG(0, 0, 0   )); //step 17a - (total >=12 clocks)
01476           ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_JTAG ),FPGA_JTAG_ARG(1, 2, 0   )); //step 18 - set UPDATE-DR state
01477           ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_JTAG ),FPGA_JTAG_ARG(0, 1, 0   )); //step 19 - set Run-Test-Idle state
01478           printf ("FPGA status = %x\r\n", ioctl(devfd, _IO(FPGACONF_IOCTYPE, FPGA_STAT ),0));
01479       printf ("testcs = %x, testnum= %x (%d)\r\n", testcs,testnum,testnum);
01480 
01481           if ( ifd>=0) close (ifd);
01482           close (devfd);
01483           return 0;
01484         } 
01485         if (argv[1][1] == 'X') { // frequencies settings
01486           if (argc<3) {printf("Needs more args...\r\n"); return 0;}
01487           n= strtol (argv[2],&cp,10);
01488      if ((n<0) || (n>3)) {
01489         printf("Invalid clock number - %d\r\n", n);
01490         return -1;
01491      }
01492      if (argc<4) { // read clock (shadow)
01493        
01494        if ((devfd = open("/dev/fpgaclocks", O_RDONLY))<0)  {printf("error opening /dev/fpgaclocks\r\n"); return -1;}
01495 //        f= ioctl(devfd, _IO(FPGA_CLOCK_IOCTYPE_RD, n ),0); // read current clock frequency
01496         i= ioctl(devfd, _IO(FPGA_CLOCK_IOCTYPE_RD, n ),0); // read current clock frequency
01497         close (devfd);
01498         if (i<0) {printf("Error reading clock %d, code=%d\r\n", n, -i); return -1;}
01499         f=i;
01500         f*=0.000001;
01501         if (i) printf("Clock %d is set to %eMHz\r\n",n,f);
01502         else   printf("Clock %d is OFF\r\n",n);
01503         return 0;
01504      }
01505           f= strtod (argv[3],&cp); // floating point now (1Hz resolution)
01506 
01507 //====================
01508       if ((devfd = open("/dev/fpgaclocks", O_RDWR))<0)  {printf("error opening /dev/fpgaclocks\r\n"); return -1;}
01509       i=1000000*f; // integer in Hz
01510       switch (n) {
01511          case 0: {
01512            j= ioctl(devfd, _IO(FPGA_CLOCK_IOCTYPE, 0 ),i);
01513            break;
01514          }
01515          case 1: {
01516            j= ioctl(devfd, _IO(FPGA_CLOCK_IOCTYPE, 1 ),i);
01517            break;
01518          }
01519          case 2: {
01520            j= ioctl(devfd, _IO(FPGA_CLOCK_IOCTYPE, 2 ),i);
01521            break;
01522          }
01523          case 3: {
01524            j= ioctl(devfd, _IO(FPGA_CLOCK_IOCTYPE, 3 ),i);
01525            break;
01526          }
01527 //         default:{
01528 //           printf("Invalid clock number - %d\r\n", n);
01529 //           return -1;
01530 //         }
01531        }
01532        close (devfd);
01533        if (j<0) { // TODO: change later - too low, to high
01534            printf("Error setting clock, code=%d\r\n", -j);
01535            return -1;
01536        } else if (j==0) {
01537            printf("Clock %d turned off\r\n",n);
01538        } else {
01539            f=0.000001*j;
01540            printf("Set clock %d to %eMHz\r\n",n,f);
01541        }
01542       return 0;
01543     }
01544 
01545 
01546   if (argv[1][1] == 'x') { // frequencies settings
01547           if (argc<4) {printf("Needs more args...\r\n"); return 0;}
01548           n= strtol (argv[2],&cp,10);
01549 //        i= strtol (argv[3],&cp,10);
01550           f= strtod (argv[3],&cp); // floating point now (1Hz resolution)
01551      i=1000000*f; // integer in Hz
01552 
01553 //    if ((i<0) || (i>127)) {
01554 //      printf ("frequency should be in the range of 0...127, not %d\n",i);
01555 
01556     if((f < CLOCK_LOW) || (f > CLOCK_HIGH)) {
01557       printf ("frequency should be in the range of %d...%d MHz, not %fMhz\n", CLOCK_LOW, CLOCK_HIGH, f);
01558       return -1;
01559     }
01560 
01561     if ((n<0) || (n>1)) {
01562       printf ("Only clocks 0 (fpga) and 1 (sensor) are supported, not %d\n",n);
01563       return -1;
01564     }
01565     devfd = open("/dev/circbuf", O_RDWR);  // this file will not init fpga and sensor
01566     if (devfd<0) { // check control OK
01567       printf ("Error opening %s\n","/dev/circbuf");
01568       return -1;
01569     }
01570     switch (n) {
01571       case 0: {j=ioctl(devfd, _CCCMD(CCAM_WPARS ,  P_CLK_FPGA  ), i);break;} // in Hz now!!
01572       case 1: {j=ioctl(devfd, _CCCMD(CCAM_WPARS ,  P_CLK_SENSOR), i);break;}
01573       default: j=-1;
01574     }
01575     if (j<0) printf ("Error setting clock %d to %dMHz\n",n,i);
01576     else  printf ("clock %d will be set to %dMHz when sensor will be programmed\n",n,i);
01577     close (devfd);
01578     return 0;
01579   }
01580 
01581 //++++++++++++++++++++++++++++++++++++++++++++++++++++++
01582 
01583     
01584         if ((argv[1][1] == 'i') || (argv[1][1] == 'I')) { // i2c operations
01585           if ((argv[1][2] == 'r') || (argv[1][2] == 'R')) {
01586             if (argc<3) { // dumping all i2c registers from Cypress CLock chip
01587         sa=CY22393_SA;
01588 //              bus=0;
01589                 devfd = open("/dev/fpgaclocksi2c", O_RDONLY);
01590 
01591                 printf ("open returned - %x\r\n",devfd);
01592         j= 0x58;
01593                 printf("dumping all i2c data from slave address %x\r\n", (int) sa);
01594                 for (i=0;i<j;i++) {
01595                   if ((i & 0x0f)==0) printf("\r\n%2x:",i);
01596                   d=ioctl(devfd, _IO(FPGA_CLOCK_IOCTYPE,FPGA_CLOCK_I2C_READREG), I2C_READARG ( 0,  sa, i));
01597                   printf(" %2lx", d);
01598                 }
01599                 printf ("\r\n");
01600                 close(devfd);
01601                 printf ("devfd closed\r\n");
01602                 return 0;
01603             }
01604             if (argc<4) {printf("Needs more args...\r\n"); return 0;}
01605 
01606             sa= (strtol (argv[2],&cp,16) & 0xff);
01607             ra= (strtol (argv[3],&cp,16) & 0xff);
01608             devfd = open("/dev/fpgaclocksi2c", O_RDONLY);
01609             printf ("open returned - %x\r\n",devfd);
01610 
01611             d=ioctl(devfd, _IO(FPGA_CLOCK_IOCTYPE,FPGA_CLOCK_I2C_READREG), I2C_READARG (0, sa, ra)) ;
01612             printf ("i2c register = %lx\r\n", d);
01613             close(devfd);
01614           printf ("devfd closed\r\n");
01615 
01616             return 0;
01617           } else if  ((argv[1][2] == 'w') || (argv[1][2] == 'W')) {
01618   
01619             if (argc<5) {printf("Needs more args...\r\n"); return 0;}
01620             sa= (strtol (argv[2],&cp,16) & 0xff);
01621             ra= (strtol (argv[3],&cp,16) & 0xff);
01622             rd= (strtol (argv[4],&cp,16) & 0xff);
01623             devfd = open("/dev/fpgaclocksi2c", O_WRONLY);
01624             printf ("open returned - %x\r\n",devfd);
01625 
01626             d=ioctl(devfd, _IO(FPGA_CLOCK_IOCTYPE, FPGA_CLOCK_I2C_WRITEREG ),I2C_WRITEARG(0, sa, ra, rd));
01627             printf ("i2c returned - %lx\r\n",d);//168
01628             close(devfd);  printf ("devfd closed\r\n");
01629             return 0;
01630           } // if  ((argv[1][2] == 'w') ...
01631    }
01632 
01633    if  ((argv[1][1] == 'c') || (argv[1][1] == 'C')){
01634 
01635 //        if (argc<3) {printf("Needs more args (bit #, [op])\r\n"); return -1;}
01636         if ((devfd = open("/dev/fpgaio", O_RDWR))<0)  {printf("error opening /dev/fpgaio\r\n"); return -1;}
01637           if (argc<3) { // print status register
01638           res= ioctl(devfd, _IO(FPGACONF_CONTROL_REG,FPGACONF_CR_SHADOW), 0);
01639           i=   ioctl(devfd, _IO(FPGACONF_CONTROL_REG,FPGACONF_CR_SHADOW1), 0); // MSB ioctl returns positive or error
01640           res |= ((i << 1) & 0x80000000);
01641           printf ("%x\n",res);
01642           close (devfd);
01643           return 0;
01644         }
01645        a=(strtol (argv[2],&cp,16) & 0x1f);
01646        res=0;
01647        if (argc>3) {
01648          res=(strtol (argv[3],&cp,16) & 0x3);
01649          i=ioctl(devfd, _IO(FPGACONF_CONTROL_REG, FPGACONF_CR_MODIFY), res | (a<<2));
01650          j=ioctl(devfd, _IO(FPGACONF_CONTROL_REG, FPGACONF_CR_MODIFY), 0   | (a<<2));
01651          printf ("CR[%x]=%x (was %x)\r\n",a,j,i);
01652        } else printf("%x\n", ioctl(devfd, _IO(FPGACONF_CONTROL_REG, FPGACONF_CR_MODIFY), 0   | (a<<2))); // quiet mode, for scripts analysing output
01653        close(devfd);
01654        return 0;
01655    }
01656 
01657    if (strcasecmp(argv[1], "-sysmem")==0) { // test system memory - now - just fro intereference, not a real memory test
01658      if (argc<3) {printf("Number of 128KB chunks (decimal)to test is required\r\n");return -1;}
01659      ll=0x20000;
01660      n= strtol (argv[2],&cp,10);
01661      if (argc >3) {
01662         seed = strtol (argv[3],&cp,16);
01663         if (argc >4) {
01664           ll= strtol (argv[4],&cp,16);
01665         }
01666      } else seed=0;
01667      if (n>4096) n=4096;
01668      ll=ll>>2; // 32-bit
01669 
01671      for (i=0; i<n; i++) {
01672        memchunks[i] = (int *) malloc(ll<<2);
01673        if (!memchunks[i]) {
01674         n=i+1;
01675         break;
01676        }
01677      }
01678      printf ("testing %d (0x%x) memory chunks of %d (0x%x) bytes each... ",n,n, (int) (ll<<2),(int) (ll<<2));
01679      fflush(NULL);
01680      res=seed;
01681      for (i=0; i<n; i++) for (j=0; j<ll; j++) {
01682        memchunks[i][j]= (res <<16) || res;
01683        res++;
01684      }
01685 //     i=n/2; j=ll/2;  memchunks[i][j]=0x12345678; // test error
01686      res=seed;
01687      mn=0;
01688      for (i=0; i<n; i++) for (j=0; j<ll; j++) {
01689        if (memchunks[i][j]!= ((res <<16) || res)) {
01690          if (mn<1000) printf ("\n chunk 0x%x, word 0x%x expected 0x%x read 0x%x",i,j,((res <<16) || res),memchunks[i][j]);
01691          fflush(NULL);
01692          mn++;
01693        }
01694        res++;
01695      }
01697      for (i=0; i<n; i++)  free(memchunks[i]);
01698      if (mn==0) printf ("done\n");
01699      else printf ("\nNumber of errors - %d (0x%x)\n",mn,mn);
01700      return 0;
01701    }
01702    printf(short_usage);
01703    return 0;    
01704 }

Generated on Thu Aug 7 16:18:59 2008 for elphel by  doxygen 1.5.1