packages/web/353/php_top/i2c.php

Go to the documentation of this file.
00001 <?php
00002 /*!***************************************************************************
00003 *! FILE NAME  : i2c.php
00004 *! DESCRIPTION: Provides single byte/short r/w from/to the i2c
00005 *!              Setting per-slave protection mask,
00006 *!              Synchronizing system clock with a "CMOS" one
00007 *!              Reading from/writing to EEPROM memory
00008 *! Copyright (C) 2008 Elphel, Inc
00009 *! -----------------------------------------------------------------------------**
00010 *!
00011 *!  This program is free software: you can redistribute it and/or modify
00012 *!  it under the terms of the GNU General Public License as published by
00013 *!  the Free Software Foundation, either version 3 of the License, or
00014 *!  (at your option) any later version.
00015 *!
00016 *!  This program is distributed in the hope that it will be useful,
00017 *!  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 *!  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 *!  GNU General Public License for more details.
00020 *!
00021 *!  You should have received a copy of the GNU General Public License
00022 *!  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00023 *! -----------------------------------------------------------------------------**
00024 *!  $Log: i2c.php,v $
00025 *!  Revision 1.10  2008/07/16 23:18:54  spectr_rain
00026 *!  option cmd=readCMOS - just read w/o modification of camera time
00027 *!
00028 *!  Revision 1.8  2008/04/24 20:32:44  elphel
00029 *!  reverted, wrong edit
00030 *!
00031 *!  Revision 1.7  2008/04/24 18:19:50  elphel
00032 *!  changed to absolute path to i2c.inc
00033 *!
00034 *!  Revision 1.6  2008/03/16 01:24:09  elphel
00035 *!  Added i2c speed control interface
00036 *!
00037 *!  Revision 1.5  2008/03/15 23:05:18  elphel
00038 *!  split i2c.php into i2c.php and i2c.inc (to be used from other scripts)
00039 *!
00040 *!  Revision 1.4  2008/03/15 18:38:21  elphel
00041 *!  Implemented add-on boards EEPROM support
00042 *!
00043 *!  Revision 1.3  2008/02/20 07:41:42  elphel
00044 *!  report error if the date is wrong
00045 *!
00046 *!  Revision 1.2  2008/02/13 00:30:01  elphel
00047 *!  Added system time synchronization  with "CMOS" clock
00048 *!
00049 *!  Revision 1.1  2008/02/12 21:53:20  elphel
00050 *!  Modified I2c to support multiple buses, added raw access (no address registers) and per-slave protection bitmasks
00051 *!
00052 *!
00053 */
00054 
00055 require 'i2c.inc';
00056 
00057   $width=0;
00058   $bus=0;
00059   $adr=-1;
00060   $data="";
00061   $nopars=false;
00062   $raw=0;
00063   $wprot=-1;
00064   $rprot=-1;
00065   $rslt="";
00066   $cmd="";
00067 //
00068   $model="";
00069   $rev="";
00070   $brand="Elphel";
00071   $serial="";
00072   $time="";
00073   $msg="<?xml version=\"1.0\"?>\n<i2c>\n";
00074   if (count($_GET) == 0 ) {
00075      $nopars=true;
00076    } else {
00077      $pars=array();
00078      foreach($_GET as $key=>$value) {
00079        switch($key) {
00080        case "cmd":
00081          switch($value) {
00082          case "fromCMOS":
00083           $rslt=i2c_getCMOSClock()?"Set system time error (probably CMOS clock is not set)":("System clock is set to ".exec("date"));
00084           break;
00085          case "readCMOS":
00086           $rslt=i2c_readCMOSClock()?"Read CMOS clock error (probably CMOS clock is not set)":("CMOS clock is ".exec("date"));
00087           break;
00088          case "toCMOS":
00089           $rslt=i2c_setCMOSClock();
00090           break;
00091          case "fromEEPROM":
00092          case "toEEPROM":
00093          case "ctl":
00094           $cmd=$value;
00095           break;
00096          }
00097        break;
00098        case "model":
00099           $model=$value;
00100           break;
00101        case "rev":
00102           $rev=$value;
00103           break;
00104        case "serial":
00105           $serial=$value;
00106           break;
00107        case "time": // seconds from 1970
00108           $time=$value+0;
00109           break;
00110        case "raw":
00111           $adr=($value+0) & ~0x7f;
00112           $raw=1;
00113           $width=8;
00114           break;
00115        case "width":
00116           $width=$value+0;
00117           break;
00118        case "bus":
00119           $bus=$value+0;
00120           break;
00121        case "adr":
00122           $adr=$value+0;
00123           break;
00124        case "wp":
00125           if      ($value==0) $wprot=0;
00126           else if ($value==1) $wprot=1;
00127           break;
00128        case "rp":
00129           if      ($value==0) $rprot=0;
00130           else if ($value==1) $rprot=1;
00131           break;
00132        case "data":
00133           $data=$value;
00134           break;
00135        }
00136      }
00137    }
00138 
00139    switch($cmd) {
00140       case "fromEEPROM":
00141         $rslt=i2c_read256b();
00142         $zero=strpos($rslt,chr(0));
00143         if ($zero!==false) $rslt=substr($rslt,0, $zero);
00144         if (substr($rslt,0,5)=="<?xml") {
00145                   // make encoding as URL-encoding (i.e. in "%decimal_value" format)
00146           header("Content-Type: text/xml");
00147           header("Content-Length: ".strlen($rslt)."\n");
00148           header("Pragma: no-cache\n");
00149           printf($rslt);
00150           exit (0);
00151         }
00152                 $rslt=bin2hex($rslt);
00153 
00154         break;
00155       case "toEEPROM":
00156         if ($wprot>=0) {
00157           i2c_setprot (1,0xa0,1,(1-$wprot));
00158         }
00159         if ($data=="") {
00160           if ($model  === "") {$rslt="model  not specified"; break;}
00161           if ($rev    === "") {$rslt="rev    not specified"; break;}
00162           if ($serial === "") {$rslt="serial not specified"; break;}
00163           if ($time   === "") {$rslt="time   not specified"; break;}
00164            $xml = new SimpleXMLElement("<?xml version='1.0' standalone='yes'?><board/>");
00165            $xml->addChild ('brand',$brand);
00166            $xml->addChild ('model',$model);
00167            $xml->addChild ('rev',  $rev);
00168            $xml->addChild ('serial',$serial);
00169            $xml->addChild ('time',$time);
00170            $data=$xml->asXML();
00171         }
00172         if (strlen($data)>256) {
00173            $rslt="data too long - ".strlen($data)." bytes, only 256 are permitted";
00174            break;
00175         }
00176         $rslt="written ".i2c_write256b($data);
00177         i2c_setprot (1,0xa0,1,0);
00178         break;
00179       case "ctl":
00180         if ($bus=="") {
00181          $rslt="i2c bus number is not specified";
00182          break;
00183         }
00184         $rslt=i2c_ctl($bus,$data)->asXML();
00185         header("Content-Type: text/xml");
00186         header("Content-Length: ".strlen($rslt)."\n");
00187         header("Pragma: no-cache\n");
00188         printf($rslt);
00189         exit (0);
00190     }
00191    if ($rslt=="") {
00192     if (($adr>=0) && (($width==8) || ($width==16))) {
00193       $slave=($adr >> (($width==16)?8:7)) & 0xfe;
00194       if ($wprot>=0) {
00195        i2c_setprot ($bus,$slave,1,(1-$wprot));
00196       }
00197       if ($rprot>=0) {
00198        i2c_setprot ($bus,$slave,0,(1-$rprot));
00199       }
00200       $msg.="<width>".$width."</width>\n";
00201       $msg.="<bus>".$bus."</bus>\n";
00202       $msg.="<slave>".(sprintf("0x%x",($adr>>(($width==8)?7:8))  & 0xfe ))."</slave>\n";
00203 
00204       if (!$raw) {
00205         $msg.="<adr>".$adr."</adr>\n";
00206         $msg.="<hex_adr>".sprintf("0x%x",$adr)."</hex_adr>\n";
00207       }
00208      if ($data!="") { 
00209       $data+=0;
00210       $msg.="<data>".$data."</data>\n";
00211       $msg.="<wdata>".$data."</wdata>\n";
00212       $msg.="<hex_data>".sprintf("0x%x",$data)."</hex_data>\n";
00213       $rslt=i2c_send($width,$bus,$adr,$data,$raw);
00214       if (!($rslt>0)) $msg.="<error> \"i2c write error (".$rslt.")\"</error>\n";
00215      } else { 
00216       $data=i2c_receive($width,$bus,$adr,$raw);
00217       $msg.="<data>".$data."</data>\n";
00218       $msg.="<rdata>".$data."</rdata>\n";
00219       $msg.="<hex_data>".sprintf("0x%x",$data)."</hex_data>\n";
00220       if ($data==-1) $msg.="<error> \"i2c read error\"</error>\n";
00221      }
00222     } else {
00223      if (!$nopars) $msg.="<error>\"Address (adr or raw) or width are not specified or are not positive.\"</error>\n";
00224      $msg.="<usage>\"open URL: i2c.php?width&#061;www&amp;bus&#061;bbb&amp;adr&#061;aaa&amp;data&#061;ddd\"</usage>\n";
00225     }
00226    } else {
00227       $msg.="<result>\"".$rslt."\"</result>\n";
00228    }
00229 
00230    $msg.="</i2c>\n";
00231    header("Content-Type: text/xml");
00232    header("Content-Length: ".strlen($msg)."\n");
00233    header("Pragma: no-cache\n");
00234    printf($msg);
00235 ?>

Generated on Thu Aug 7 16:19:01 2008 for elphel by  doxygen 1.5.1