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.4  2008/06/23 08:11:58  dzhimiev
00023 *!  1. added more scripts for 10359 board
00024 *!
00025 *!  Revision 1.2  2008/05/22 22:57:54  dzhimiev
00026 *!  + 0x0800 - current channel number register
00027 *!  + DCMs independent phase adjustment
00028 *!  useful scripts
00029 *!
00030 *!  Revision 1.2  2008/05/01 20:32:29  dzhimiev
00031 *!  reads each sensor registers after configuration
00032 *!
00033 *!  Revision 1.1  2008/04/23 01:55:49  dzhimiev
00034 *!  1. added x359 files to src lists
00035 *!  2. x359 read/write DDR
00036 *!  3. x359 3 channels mux directly to out
00037 *!  4. x359 one channel through DDR and another directly frames switching at out
00038 *!
00039 */
00040 
00041 function send_i2c($width,$bus,$a,$d,$raw=0) { //$a<0 - use raw read/write
00042    $w=($width==16)?2:1;
00043    $i2c_fn='/dev/xi2c'.($raw?'raw':(($w==2)?'16':'8')).(($bus==0)?'':'_aux');
00044    $i2c  = fopen($i2c_fn, 'w');
00045    fseek ($i2c, $w*$a) ;
00046    if ($w==1)    $res=fwrite($i2c, chr ($d));
00047    else          $res=fwrite($i2c, chr (floor($d/256)).chr ($d-256*floor($d/256)));
00048    fclose($i2c);
00049    return $res;
00050 } // end of send_i2c()
00051 
00052 function send_i2c_4($width,$bus,$a,$d,$raw=0) { //$a<0 - use raw read/write
00053    $w=2;
00054    $i2c_fn='/dev/xi2c'.($raw?'raw':(($w==2)?'16':'8')).(($bus==0)?'':'_aux');
00055    $i2c  = fopen($i2c_fn, 'w');
00056    fseek ($i2c, $w*$a) ;
00057    if ($w==1)    $res=fwrite($i2c, chr ($d));
00058    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))) );
00059 
00060          //printf("Sending passage ");
00061          //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))));     
00062          //printf("\n");
00063 
00064    fclose($i2c);
00065    return $res;
00066 } // end of send_i2c()
00067 
00068 function receive_i2c($width,$bus,$a,$raw=0) {
00069    $w=($width==16)?2:1;
00070    $i2c_fn='/dev/xi2c'.($raw?'raw':(($w==2)?'16':'8')).(($bus==0)?'':'_aux');
00071    $i2c  = fopen($i2c_fn, 'r');
00072    fseek ($i2c, $w*$a);
00073    $data = fread($i2c, $w);
00074    fclose($i2c);
00075    if (strlen($data)<$w) return -1;
00076    $v=unpack(($w==1)?'C':'n1',$data);
00077    return $v[1];
00078 } // end of receive_i2c()
00079 
00080 function receive_i2c_4($width,$bus,$a,$raw=0) {
00081    $w=($width==16)?2:1;
00082    $i2c_fn='/dev/xi2c'.($raw?'raw':(($w==2)?'16':'8')).(($bus==0)?'':'_aux');
00083    $i2c  = fopen($i2c_fn, 'r');
00084          fseek ($i2c, 4, SEEK_END);
00085    fseek ($i2c, $w*$a);
00086          
00087    $data = fread($i2c, 2*$w);
00088    fclose($i2c);
00089    if (strlen($data)<2*$w) return -1;
00090    $v=unpack(($w==1)?'C*':'N*',$data);
00091          //print_r($v);
00092          //printf("0x%x\n",$v[1]);
00093          return $v[1];
00094 } // end of receive_i2c()
00095 
00096 function dcm_reset($width,$bus,$raw=0){
00097 
00098         printf("sent 0x%04x ",0x0810); send_i2c_4($width,$bus,0x0810,0xffffffff,$raw=0);
00099         printf("\nsent 0x%04x ",0x0810); send_i2c_4($width,$bus,0x0810,0x00000000,$raw=0);
00100         
00101 }
00102 
00103 function sensor_init($width,$bus,$channel,$init_pars,$raw=0){
00104 
00105 
00106         for ($i=0;$i<256;$i++){
00107                 send_i2c($width,$bus,0x4800+$i,$init_pars[$i],$raw=0);
00108 
00109                 //printf("%04x ",$init_pars[$i]);       
00110 
00111                 $readout=receive_i2c($width,$bus,0x4800+$i,$raw=0);
00112                 printf("%04x ",$readout);
00113 
00114                 if($j==15){
00115                         $j=0; printf("\n");
00116                 }else{
00117                         $j++;
00118                 }
00119         }
00120 }
00121 
00122 $width=16;
00123 $bus=0;
00124 $sphase=0x0000007;
00125 
00126 printf("<pre>\n");
00127 
00128 dcm_reset($width,$bus,$raw=0);
00129 
00130 send_i2c_4($width,$bus,0x0835,0x00000001,$raw=0); printf("\nSet to sensor <font size=\"6\">1</font>\n");
00131 
00132 // read sens 1 pars
00133 for ($i=0;$i<256;$i++){
00134         $init_pars[$i]=receive_i2c($width,$bus,0x4800+$i,$raw=0);
00135 
00136         printf("%04x ",$init_pars[$i]); 
00137 
00138         if($j==15){
00139                 $j=0; printf("\n");
00140         }else{
00141                 $j++;
00142         }
00143         
00144 }
00145 
00146 // write sens1 pars to sens2
00147 send_i2c_4($width,$bus,0x0835,0x00000002,$raw=0); printf("\nInitializing sensor <font size=\"6\">2</font>\n");
00148 sensor_init(16,0,0x00000004,$init_pars,$raw=0);
00149 // write sens1 pars to sens3
00150 send_i2c_4($width,$bus,0x0835,0x00000004,$raw=0); printf("\nInitializing sensor <font size=\"6\">3</font>\n");
00151 sensor_init(16,0,0x00000002,$init_pars,$raw=0);
00152 
00153 send_i2c_4($width,$bus,0x0835,0x00000001,$raw=0); printf("\nSet back to sensor <font size=\"6\">1</font>\n");
00154 
00155 //phase adjustment
00156 for ($i=0;$i<20;$i++){
00157         send_i2c_4($width,$bus,0x0807,0x00000001,$raw=0);
00158 }
00159 
00160 printf("</pre>\n");
00161 
00162 ?> 

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