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

Generated on Fri Nov 28 00:06:21 2008 for elphel by  doxygen 1.5.1