fpga/x359/test_scripts/switch_to_ch2.php

Go to the documentation of this file.
00001 <?php
00002 /*!***************************************************************************
00003 *! FILE NAME  : switch_to_ch2.php
00004 *! DESCRIPTION: switches between channels
00005 *! Copyright (C) 2008 Elphel, Inc
00006 *! -----------------------------------------------------------------------------**
00007 *!
00008 *!  This program is free software: you can redistribute it and/or modify
00009 *!  it under the terms of the GNU General Public License as published by
00010 *!  the Free Software Foundation, either version 3 of the License, or
00011 *!  (at your option) any later version.
00012 *!
00013 *!  This program is distributed in the hope that it will be useful,
00014 *!  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 *!  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 *!  GNU General Public License for more details.
00017 *!
00018 *!  You should have received a copy of the GNU General Public License
00019 *!  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020 *! -----------------------------------------------------------------------------**
00021 *!  $Log: switch_to_ch2.php,v $
00022 *!  Revision 1.1.1.1  2008/11/27 20:04:02  elphel
00023 *!
00024 *!
00025 *!  Revision 1.2  2008/05/01 20:27:08  dzhimiev
00026 *!  reformatted a little
00027 *!
00028 *!  Revision 1.1  2008/04/23 01:55:50  dzhimiev
00029 *!  1. added x359 files to src lists
00030 *!  2. x359 read/write DDR
00031 *!  3. x359 3 channels mux directly to out
00032 *!  4. x359 one channel through DDR and another directly frames switching at out
00033 *!
00034 */
00035 
00036 function send_i2c($width,$bus,$a,$d,$raw=0) { //$a<0 - use raw read/write
00037    $w=($width==16)?2:1;
00038    $i2c_fn='/dev/xi2c'.($raw?'raw':(($w==2)?'16':'8')).(($bus==0)?'':'_aux');
00039    $i2c  = fopen($i2c_fn, 'w');
00040    fseek ($i2c, $w*$a) ;
00041    if ($w==1)    $res=fwrite($i2c, chr ($d));
00042    else          $res=fwrite($i2c, chr (floor($d/256)).chr ($d-256*floor($d/256)));
00043    fclose($i2c);
00044    return $res;
00045 } // end of send_i2c()
00046 
00047 function receive_i2c($width,$bus,$a,$raw=0) {
00048    $w=($width==16)?2:1;
00049    $i2c_fn='/dev/xi2c'.($raw?'raw':(($w==2)?'16':'8')).(($bus==0)?'':'_aux');
00050    $i2c  = fopen($i2c_fn, 'r');
00051    fseek ($i2c, $w*$a);
00052    $data = fread($i2c, $w);
00053    fclose($i2c);
00054    if (strlen($data)<$w) return -1;
00055    $v=unpack(($w==1)?'C':'n1',$data);
00056    return $v[1];
00057 } // end of receive_i2c()
00058 
00059 function receive_i2c_4($width,$bus,$a,$raw=0) {
00060    $w=($width==16)?2:1;
00061    $i2c_fn='/dev/xi2c'.($raw?'raw':(($w==2)?'16':'8')).(($bus==0)?'':'_aux');
00062    $i2c  = fopen($i2c_fn, 'r');
00063          fseek ($i2c, 4, SEEK_END);
00064    fseek ($i2c, $w*$a);
00065          
00066    $data = fread($i2c, 2*$w);
00067    fclose($i2c);
00068    if (strlen($data)<2*$w) return -1;
00069    $v=unpack(($w==1)?'C*':'N*',$data);
00070          //print_r($v);
00071          //printf("0x%x\n",$v[1]);
00072          return $v[1];
00073 } // end of receive_i2c()
00074 
00075 function receive_i2c_n($width,$bus,$a,$raw=0) {
00076    $w=($width==16)?2:1;
00077    $i2c_fn='/dev/xi2c'.($raw?'raw':(($w==2)?'16':'8')).(($bus==0)?'':'_aux');
00078    $i2c  = fopen($i2c_fn, 'r');
00079          fseek ($i2c, 16*4, SEEK_END);
00080    fseek ($i2c, $w*$a);
00081          
00082    $data = fread($i2c, 2*$w*4*4);
00083    fclose($i2c);
00084    if (strlen($data)<2*$w) return -1;
00085    $v=unpack(($w==1)?'C*':'N*',$data);
00086          //print_r($v);
00087          //printf("0x%x\n",$v[1]);
00088          return array($v[1],$v[2],$v[3],$v[4],$v[5],$v[6],$v[7],$v[8],$v[9],$v[10],$v[11],$v[12],$v[13],$v[14],$v[15],$v[16]);
00089 } // end of receive_i2c()
00090 
00091 function send_i2c_4($width,$bus,$a,$d,$raw=0) { //$a<0 - use raw read/write
00092    $w=($width==16)?2:1;
00093    $i2c_fn='/dev/xi2c'.($raw?'raw':(($w==2)?'16':'8')).(($bus==0)?'':'_aux');
00094    $i2c  = fopen($i2c_fn, 'w');
00095    fseek ($i2c, $w*$a) ;
00096    if ($w==1)    $res=fwrite($i2c, chr ($d));
00097    else          $res=fwrite($i2c, chr (floor($d/(256*256*256))).chr (($d - 256*256*256*floor($d/(256*256*256)))/(256*256)).chr (($d - 256*256*floor($d/(256*256)))/256).chr ($d - 256*floor($d/(256))) );
00098 
00099          //printf("Sending passage ");
00100          printf("0x%02x%02x%02x%02x ",(floor($d/(256*256*256))),(($d - 256*256*256*floor($d/(256*256*256)))/(256*256)),(($d - 256*256*floor($d/(256*256)))/256),($d - 256*floor($d/(256))));     
00101          //printf("\n");
00102 
00103    fclose($i2c);
00104    return $res;
00105 } // end of send_i2c()
00106 
00107 function dcm_reset($width,$bus,$raw=0){
00108 
00109         printf("sent 0x%04x ",0x0810); send_i2c_4($width,$bus,0x0810,0xffffffff,$raw=0);
00110         printf("\nsent 0x%04x ",0x0810); send_i2c_4($width,$bus,0x0810,0x00000000,$raw=0);
00111         
00112 }
00113 
00114 function ramb_check($width,$bus,$raw=0){
00115         printf("Data sent:     ");
00116 
00117         send_i2c_4($width,0,0x0801,0x01020304,$raw=0);
00118         send_i2c_4($width,0,0x0801,0x05060708,$raw=0);
00119         send_i2c_4($width,0,0x0801,0x090a0b0c,$raw=0);
00120         send_i2c_4($width,0,0x0801,0x0d0e0f10,$raw=0);
00121 
00122         send_i2c_4($width,$bus,0x0801,0x11121314,$raw=0);
00123         send_i2c_4($width,$bus,0x0801,0x15161718,$raw=0);
00124         send_i2c_4($width,$bus,0x0801,0x191a1b1c,$raw=0);
00125         send_i2c_4($width,$bus,0x0801,0x1d1e1f20,$raw=0);
00126 
00127         send_i2c_4($width,$bus,0x0801,0x21222324,$raw=0);
00128         send_i2c_4($width,$bus,0x0801,0x25262728,$raw=0);
00129         send_i2c_4($width,$bus,0x0801,0x292a2b2c,$raw=0);
00130         send_i2c_4($width,$bus,0x0801,0x2d2e2f30,$raw=0);
00131 
00132         send_i2c_4($width,$bus,0x0801,0x31323334,$raw=0);
00133         send_i2c_4($width,$bus,0x0801,0x35363738,$raw=0);
00134         send_i2c_4($width,$bus,0x0801,0x393a3b3c,$raw=0);
00135         send_i2c_4($width,$bus,0x0801,0x3d3e3f40,$raw=0);
00136 
00137         printf("\nData received: ");
00138         for ($i=0;$i<16;$i++)
00139         {
00140                 $data=receive_i2c_4($width,$bus,0x0868,$raw=0); // first try to read 4
00141                 printf("0x%08x ",$data);// 0x%x",$data[2]);
00142         }
00143 }
00144 
00145 function ddr_init($width,$bus,$raw=0){
00146 
00147         printf("sent 0x%04x ",0x0827); send_i2c_4($width,$bus,0x0827,0x00000000,$raw=0); printf("\n");
00148 
00149         printf("sent 0x%04x ",0x0823); send_i2c_4($width,$bus,0x0823,0x00017fff,$raw=0); 
00150 
00151         $data=receive_i2c_4($width,$bus,0x0841,$raw=0); 
00152         printf("read 0x%04x 0x%08x\n",0x0841,$data);
00153 
00154         printf("sent 0x%04x ",0x0823); send_i2c_4($width,$bus,0x0823,0x00002002,$raw=0); 
00155 
00156         $data=receive_i2c_4($width,$bus,0x0841,$raw=0); 
00157         printf("read 0x%04x 0x%08x\n",0x0841,$data);
00158 
00159         printf("sent 0x%04x ",0x0823); send_i2c_4($width,$bus,0x0823,0x00000163,$raw=0); 
00160 
00161         $data=receive_i2c_4($width,$bus,0x0841,$raw=0); 
00162         printf("read 0x%04x 0x%08x\n",0x0841,$data);
00163 
00164         printf("sent 0x%04x ",0x0823); send_i2c_4($width,$bus,0x0823,0x00008000,$raw=0);
00165 
00166         $data=receive_i2c_4($width,$bus,0x0841,$raw=0); 
00167         printf("read 0x%04x 0x%08x\n",0x0841,$data);
00168 
00169         printf("sent 0x%04x ",0x0823); send_i2c_4($width,$bus,0x0823,0x00008000,$raw=0);
00170 
00171         $data=receive_i2c_4($width,$bus,0x0841,$raw=0); 
00172         printf("read 0x%04x 0x%08x\n",0x0841,$data);
00173 
00174         printf("sent 0x%04x ",0x0823); send_i2c_4($width,$bus,0x0823,0x00017fff,$raw=0); 
00175 
00176         $data=receive_i2c_4($width,$bus,0x0841,$raw=0);
00177         printf("read 0x%04x 0x%08x\n",0x0841,$data);
00178 
00179 }
00180 
00181 function ddr_check($width,$bus,$raw=0){
00182 
00183         printf("sent 0x%04x ",0x0827); send_i2c_4($width,$bus,0x0827,0x00000000,$raw=0); printf("\n");
00184         printf("sent 0x%04x ",0x0827); send_i2c_4($width,$bus,0x0827,0x00000001,$raw=0); printf("\n");
00185 
00186         printf("sent 0x%04x ",0x082c); send_i2c_4($width,$bus,0x082c,0x00004001,$raw=0); printf("\n");
00187         printf("sent 0x%04x ",0x082d); send_i2c_4($width,$bus,0x082d,0x000001f0,$raw=0); printf("\n");
00188         printf("sent 0x%04x ",0x082e); send_i2c_4($width,$bus,0x082e,0x00000005,$raw=0); printf("\n");
00189 
00190         printf("sent 0x%04x ",0x0827); send_i2c_4($width,$bus,0x0827,0x00000021,$raw=0); printf("\n");
00191 
00192         printf("\nSent array:\n");
00193 
00194         printf("<font color=\"#0000FF\">");
00195 
00196 
00197         for($aha=0;$aha<128;$aha++){
00198                 send_i2c_4($width,$bus,0x0831,4*$aha,$raw=0);
00199                 send_i2c_4($width,$bus,0x0831,4*$aha+1,$raw=0);
00200                 send_i2c_4($width,$bus,0x0831,4*$aha+2,$raw=0);
00201                 send_i2c_4($width,$bus,0x0831,4*$aha+3,$raw=0); printf("\n"); 
00202         }
00203 
00204         printf("\n");
00205         printf("</font>");
00206 
00207         printf("sent 0x%04x ",0x0834);send_i2c_4($width,$bus,0x0834,0x11223344,$raw=0); printf("\n");
00208 
00209         printf("sent 0x%04x ",0x082c); send_i2c_4($width,$bus,0x082c,0x00000001,$raw=0);printf("\n");
00210         printf("sent 0x%04x ",0x082d); send_i2c_4($width,$bus,0x082d,0x000001f0,$raw=0);printf("\n");
00211         printf("sent 0x%04x ",0x082e); send_i2c_4($width,$bus,0x082e,0x00000005,$raw=0);printf("\n");
00212 
00213         $data=receive_i2c_4($width,$bus,0x0860,$raw=0); // first try to read 4
00214         printf("i2c_4 read slave 0x%04x ",0x0860);
00215         printf("acquired data is 0x%08x\n",$data);// 0x%x",$data[2]);
00216 
00217         $data=receive_i2c_4($width,$bus,0x0860,$raw=0); // first try to read 4
00218         printf("i2c_4 read slave 0x%04x ",0x0860);
00219         printf("acquired data is 0x%08x\n",$data);// 0x%x",$data[2]);
00220 
00221         $data_array=receive_i2c_n($width,$bus,0x0833,$raw=0); // first try to read 4
00222         
00223         $j=0;   
00224         printf("\nAcquired data:\n");
00225         printf("<font color=\"#0000FF\">");
00226 
00227         for ($i=0,$index=count($data_array); $i<$index;$i++) 
00228         {
00229                 printf("0x%08x ",$data_array[$i]);
00230 
00231                 if ($j==3) 
00232                         {
00233                                 $j=0; printf("\n");
00234                         }else{
00235                                 $j++;
00236                         };
00237         }
00238 
00239         printf("</font>\n");
00240 
00241 }
00242 
00243 function ddr_read($width,$bus,$raw=0){
00244 
00245         printf("sent 0x%04x ",0x0834);send_i2c_4($width,$bus,0x0834,0x11223344,$raw=0); printf("\n");
00246 
00247         printf("sent 0x%04x ",0x082d); send_i2c_4($width,$bus,0x082d,0x000001f0,$raw=0);printf("\n");
00248         printf("sent 0x%04x ",0x082e); send_i2c_4($width,$bus,0x082e,0x00000000,$raw=0);printf("\n");
00249         
00250 
00251         printf("\nAcquired data:\n");
00252 
00253 for($k=0;$k<2;$k++){
00254 
00255         printf("sent 0x%04x ",0x082c); send_i2c_4($width,$bus,0x082c,4*$k,$raw=0);printf("\n");
00256 
00257 
00258         for($g=0;$g<32;$g++){
00259                 $data_array=receive_i2c_n($width,$bus,0x0833,$raw=0); // first try to read 4
00260                 
00261                 $j=0;
00262 
00263                 printf("<font color=\"#0000FF\">");
00264                 for ($i=0,$index=count($data_array); $i<$index;$i++) 
00265                 {
00266                         printf("%08x ",$data_array[$i]);
00267 
00268                         if ($j==15) 
00269                                 {
00270                                         $j=0; printf("\n");
00271                                 }else{
00272                                         $j++;
00273                                 };
00274                 }
00275                 printf("</font>");
00276         }
00277 
00278         printf("\n");
00279 }
00280 
00281         printf("</font>\n");
00282 
00283 }
00284 
00285 function channels_init($width,$bus,$raw=0){
00286         send_i2c_4($width,$bus,0x0827,0x00000001,$raw=0); printf("sent %d\n",$cnt); $cnt++;
00287 // initialize
00288         send_i2c_4($width,$bus,0x0827,0x00000003,$raw=0); printf("sent %d\n",$cnt); $cnt++;
00289 
00290         send_i2c_4($width,$bus,0x0821,0x00000ff0,$raw=0); printf("sent %d\n",$cnt); $cnt++;
00291         send_i2c_4($width,$bus,0x0822,0x00000fff,$raw=0); printf("sent %d\n",$cnt); $cnt++;
00292         send_i2c_4($width,$bus,0x0820,0x00004000,$raw=0); printf("sent %d\n",$cnt); $cnt++;
00293 
00294         //send_i2c_4($width,$bus,0x0827,0x00000007,$raw=0); printf("sent %d\n",$cnt); $cnt++;
00295 
00296         send_i2c_4($width,$bus,0x0829,0x00000ff0,$raw=0); printf("sent %d\n",$cnt); $cnt++;
00297         send_i2c_4($width,$bus,0x082a,0x00000fff,$raw=0); printf("sent %d\n",$cnt); $cnt++;
00298         send_i2c_4($width,$bus,0x0828,0x00000000,$raw=0); printf("sent %d\n",$cnt); $cnt++;
00299 
00300         send_i2c_4($width,$bus,0x0827,0x00000017,$raw=0); printf("sent %d\n",$cnt); $cnt++;
00301 }
00302 
00303 
00304 
00305   $width=16;
00306   $bus=0;
00307         $adr=0x0860;
00308         $datax=5;
00309   $nopars=false;
00310   $raw=0;
00311 
00312         $phase_shift=0;
00313         $channel=9;
00314         $lines=4;
00315         $pixes=1619;
00316         $delay=0x000fffff;
00317         $change_channel=0;
00318         $sphase=0x00000000;
00319         $ch1_ch2_delay=2;
00320 
00321         printf("<pre>");
00322 
00323         foreach($_GET as $key=>$value) {
00324                 switch($key) {
00325                         case "phase_shift"    : $phase_shift=$value+0;    break;
00326                         case "sphase"         : $sphase=$value+0;         break;
00327                         case "delay"          : $delay=$value+0;          break;
00328                         case "ch1_ch2_delay"  : $c1_ch2_delay=$value+0;   break;
00329                         case "cc"             : $cc=$value+0;             break;
00330                         case "dr"             : $dr=$value+0;             break;
00331                         case "lines"          : $lines=$value+0;          break;
00332                         case "channel"        : $channel=$value+0;        break;
00333                         case "pixes"          : $pixes=$value+0;          break;
00334                 }
00335         }
00336 
00337 if ($dr==1) {
00338 dcm_reset($width,$bus,$raw=0);
00339 }
00340 
00341 if ($cc==1) {
00342 send_i2c_4($width,$bus,0x083a,$sphase,$raw=0);
00343 send_i2c_4($width,$bus,0x083b,$ch1_ch2_delay,$raw=0);
00344 send_i2c_4($width,$bus,0x0838,$delay,$raw=0); printf("\nSet delay to <font size=\"6\">".$delay."0 ns</font>\n");
00345 send_i2c_4($width,$bus,0x0835,$channel,$raw=0); printf("\nSet channel to <font size=\"6\">$channel</font>\n");
00346 }
00347 
00348         $data=receive_i2c_4($width,$bus,0x0860,$raw=0); // first try to read 4
00349         printf("i2c_4 read slave 0x%04x ",0x0860);
00350         printf("acquired data is 0x%08x\n",$data);// 0x%x",$data[2]);
00351 
00352         $data=receive_i2c_4($width,$bus,0x0840,$raw=0); // first try to read 4
00353         printf("i2c_4 read slave 0x%04x ",0x0840);
00354         printf("acquired data is 0x%08x\n",$data);// 0x%x",$data[2]);
00355 
00356         $data=receive_i2c_4($width,$bus,0x0870,$raw=0); // first try to read 4
00357         printf("i2c_4 read slave 0x%04x \n",0x0870);
00358         printf("\nThe delay between frames <font size=\"6\">0x%08x</font>\n",$data);
00359 
00360         $data=receive_i2c_4($width,$bus,0x0871,$raw=0); // first try to read 4
00361         printf("i2c_4 read slave 0x%04x \n",0x0871);
00362         printf("\nThe delay between frames <font size=\"6\">0x%08x</font>\n",$data);
00363 
00364 
00365 /*
00366         send_i2c_4($width,$bus,0x0827,0x00000000,$raw=0);
00367 
00368         send_i2c_4($width,$bus,0x0837,(0x10000*$lines)+$pixes,$raw=0); printf("\nSet normal & fake frame resolution to <font size=\"6\">$pixes"."x$lines</font>\n");
00369 
00370 if ($change_channel==1) {
00371         send_i2c_4($width,$bus,0x0827,0x00000000,$raw=0);
00372         sleep(1);
00373         send_i2c_4($width,$bus,0x0835,$channel,$raw=0); printf("\nSet channel to <font size=\"6\">$channel</font>\n");
00374         printf("<h3>Initialize channels 0 & 2</h3>");
00375         ddr_init($width,$bus,$raw=0);
00376         channels_init($width,$bus,$raw=0);
00377 }else{
00378         printf("<h3>Channel has not changed</h3>");
00379 }
00380 */
00381 
00382 //      if ($phase_shift==0) {
00383 //              printf("<h3>Reset DCM</h3>");
00384 //              dcm_reset($width,$bus,$raw=0);
00385 //      }else{
00386 //              printf("<h3>Phase Shift DCM</h3>");
00387 //              send_i2c_4($width,$bus,0x0808,$phase_shift,$raw=0);
00388 //      }
00389 
00390 //      printf("<h3>RAMB & I2C test</h3>");
00391 //      ramb_check($width,$bus,$raw=0);
00392 
00393         //printf("<h3>DDR Initialization sequence</h3>");
00394         //ddr_init($width,$bus,$raw=0);
00395 
00396         //printf("<h3>Write data array to DDR and read it back</h3>");
00397         //ddr_check($width,$bus,$raw=0);
00398 
00399         //send_i2c_4($width,$bus,0x0835,$channel,$raw=0); printf("\nSet channel to <font size=\"6\">$channel</font>\n");
00400 
00401 /*      $data=receive_i2c_4($width,$bus,0x0846,$raw=0); // first try to read 4
00402         printf("i2c_4 read slave 0x%04x ",0x0846);
00403         printf("acquired data is 0x%08x\n",$data);// 0x%x",$data[2]);
00404 
00405         $data=receive_i2c_4($width,$bus,0x0845,$raw=0); // first try to read 4
00406         printf("i2c_4 read slave 0x%04x ",0x0845);
00407         printf("acquired data is 0x%08x\n",$data);// 0x%x",$data[2]);
00408 
00409         $data=receive_i2c_4($width,$bus,0x0844,$raw=0); // first try to read 4
00410         printf("i2c_4 read slave 0x%04x ",0x0844);
00411         printf("acquired data is 0x%08x\n",$data);// 0x%x",$data[2]);
00412 
00413         send_i2c_4($width,$bus,0x0839,0x01234567,$raw=0); printf("\n");
00414         sleep(1);
00415 
00416         $data=receive_i2c_4($width,$bus,0x0846,$raw=0); // first try to read 4
00417         printf("i2c_4 read slave 0x%04x ",0x0846);
00418         printf("acquired data is 0x%08x\n",$data);// 0x%x",$data[2]);
00419 
00420         $data=receive_i2c_4($width,$bus,0x0845,$raw=0); // first try to read 4
00421         printf("i2c_4 read slave 0x%04x ",0x0845);
00422         printf("acquired data is 0x%08x\n",$data);// 0x%x",$data[2]);
00423 
00424         $data=receive_i2c_4($width,$bus,0x0844,$raw=0); // first try to read 4
00425         printf("i2c_4 read slave 0x%04x ",0x0844);
00426         printf("acquired data is 0x%08x\n",$data);// 0x%x",$data[2]);
00427         //send_i2c_4($width,$bus,0x0837,(0x10000*$lines)+$pixes,$raw=0); printf("\nSet normal & fake frame resolution to <font size=\"6\">$pixes"."x$lines</font>\n");
00428 
00429         sleep(1);
00430 
00431         $data=receive_i2c_4($width,$bus,0x0846,$raw=0); // first try to read 4
00432         printf("i2c_4 read slave 0x%04x ",0x0846);
00433         printf("acquired data is 0x%08x\n",$data);// 0x%x",$data[2]);
00434 
00435         $data=receive_i2c_4($width,$bus,0x0845,$raw=0); // first try to read 4
00436         printf("i2c_4 read slave 0x%04x ",0x0845);
00437         printf("acquired data is 0x%08x\n",$data);// 0x%x",$data[2]);
00438 
00439         $data=receive_i2c_4($width,$bus,0x0844,$raw=0); // first try to read 4
00440         printf("i2c_4 read slave 0x%04x ",0x0844);
00441         printf("acquired data is 0x%08x\n",$data);// 0x%x",$data[2]);*/
00442         //printf("<h3>Initialize channels 0 & 2</h3>");
00443         //channels_init($width,$bus,$raw=0);
00444 
00445 
00446         //printf("<h3>Read DDR</h3>");
00447         //ddr_read($width,$bus,$raw=0);
00448 
00449 
00450 
00451         printf("</pre>");
00452 
00453 ?>

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