fpga/x359/test_scripts/sensors_init.php

Go to the documentation of this file.
00001 <?php
00002 /*!***************************************************************************
00003 *! FILE NAME  : sensors_init.php
00004 *! DESCRIPTION: reads parameters from the sensor 1 and writes to other sensors
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: sensors_init.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:32:29  dzhimiev
00026 *!  reads each sensor registers after configuration
00027 *!
00028 *!  Revision 1.1  2008/04/23 01:55:49  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 send_i2c_4($width,$bus,$a,$d,$raw=0) { //$a<0 - use raw read/write
00048    $w=2;
00049    $i2c_fn='/dev/xi2c'.($raw?'raw':(($w==2)?'16':'8')).(($bus==0)?'':'_aux');
00050    $i2c  = fopen($i2c_fn, 'w');
00051    fseek ($i2c, $w*$a) ;
00052    if ($w==1)    $res=fwrite($i2c, chr ($d));
00053    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))) );
00054 
00055          //printf("Sending passage ");
00056          //printf("%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))));     
00057          //printf("\n");
00058 
00059    fclose($i2c);
00060    return $res;
00061 } // end of send_i2c()
00062 
00063 function receive_i2c($width,$bus,$a,$raw=0) {
00064    $w=($width==16)?2:1;
00065    $i2c_fn='/dev/xi2c'.($raw?'raw':(($w==2)?'16':'8')).(($bus==0)?'':'_aux');
00066    $i2c  = fopen($i2c_fn, 'r');
00067    fseek ($i2c, $w*$a);
00068    $data = fread($i2c, $w);
00069    fclose($i2c);
00070    if (strlen($data)<$w) return -1;
00071    $v=unpack(($w==1)?'C':'n1',$data);
00072    return $v[1];
00073 } // end of receive_i2c()
00074 
00075 function receive_i2c_4($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, 4, SEEK_END);
00080    fseek ($i2c, $w*$a);
00081          
00082    $data = fread($i2c, 2*$w);
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 $v[1];
00089 } // end of receive_i2c()
00090 
00091 function dcm_reset($width,$bus,$raw=0){
00092 
00093         printf("sent 0x%04x ",0x0810); send_i2c_4($width,$bus,0x0810,0xffffffff,$raw=0);
00094         printf("\nsent 0x%04x ",0x0810); send_i2c_4($width,$bus,0x0810,0x00000000,$raw=0);
00095         
00096 }
00097 
00098 function sensor_init($width,$bus,$channel,$init_pars,$raw=0){
00099 
00100 
00101         for ($i=0;$i<256;$i++){
00102                 send_i2c($width,$bus,0x4800+$i,$init_pars[$i],$raw=0);
00103 
00104                 //printf("%04x ",$init_pars[$i]);       
00105 
00106                 $readout=receive_i2c($width,$bus,0x4800+$i,$raw=0);
00107                 printf("%04x ",$readout);
00108 
00109                 if($j==15){
00110                         $j=0; printf("\n");
00111                 }else{
00112                         $j++;
00113                 }
00114         }
00115 }
00116 
00117 $width=16;
00118 $bus=0;
00119 $sphase=0x0000007;
00120 
00121 printf("<pre>\n");
00122 
00123 dcm_reset($width,$bus,$raw=0);
00124 
00125 send_i2c_4($width,$bus,0x0835,0x00000001,$raw=0); printf("\nSet to sensor <font size=\"6\">1</font>\n");
00126 
00127 // read sens 1 pars
00128 for ($i=0;$i<256;$i++){
00129         $init_pars[$i]=receive_i2c($width,$bus,0x4800+$i,$raw=0);
00130 
00131         printf("%04x ",$init_pars[$i]); 
00132 
00133         if($j==15){
00134                 $j=0; printf("\n");
00135         }else{
00136                 $j++;
00137         }
00138         
00139 }
00140 
00141 // write sens1 pars to sens2
00142 send_i2c_4($width,$bus,0x0835,0x00000002,$raw=0); printf("\nInitializing sensor <font size=\"6\">2</font>\n");
00143 sensor_init(16,0,0x00000004,$init_pars,$raw=0);
00144 // write sens1 pars to sens3
00145 send_i2c_4($width,$bus,0x0835,0x00000004,$raw=0); printf("\nInitializing sensor <font size=\"6\">3</font>\n");
00146 sensor_init(16,0,0x00000002,$init_pars,$raw=0);
00147 
00148 send_i2c_4($width,$bus,0x0835,0x00000001,$raw=0); printf("\nSet back to sensor <font size=\"6\">1</font>\n");
00149 
00150 //phase adjustment
00151 for ($i=0;$i<20;$i++){
00152         send_i2c_4($width,$bus,0x0807,0x00000001,$raw=0);
00153 }
00154 
00155 printf("</pre>\n");
00156 
00157 ?> 

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