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.1.1.1  2008/11/27 20:04:03  elphel
00026 *!
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 "toCMOS":
00086           $rslt=i2c_setCMOSClock();
00087           break;
00088          case "fromEEPROM":
00089          case "toEEPROM":
00090          case "ctl":
00091           $cmd=$value;
00092           break;
00093          }
00094        break;
00095        case "model":
00096           $model=$value;
00097           break;
00098        case "rev":
00099           $rev=$value;
00100           break;
00101        case "serial":
00102           $serial=$value;
00103           break;
00104        case "time": // seconds from 1970
00105           $time=$value+0;
00106           break;
00107        case "raw":
00108           $adr=($value+0) & ~0x7f;
00109           $raw=1;
00110           $width=8;
00111           break;
00112        case "width":
00113           $width=$value+0;
00114           break;
00115        case "bus":
00116           $bus=$value+0;
00117           break;
00118        case "adr":
00119           $adr=$value+0;
00120           break;
00121        case "wp":
00122           if      ($value==0) $wprot=0;
00123           else if ($value==1) $wprot=1;
00124           break;
00125        case "rp":
00126           if      ($value==0) $rprot=0;
00127           else if ($value==1) $rprot=1;
00128           break;
00129        case "data":
00130           $data=$value;
00131           break;
00132        }
00133      }
00134    }
00135 
00136    switch($cmd) {
00137       case "fromEEPROM":
00138         $rslt=i2c_read256b();
00139         $zero=strpos($rslt,chr(0));
00140         if ($zero!==false) $rslt=substr($rslt,0, $zero);
00141         if (substr($rslt,0,5)=="<?xml") {
00142           header("Content-Type: text/xml");
00143           header("Content-Length: ".strlen($rslt)."\n");
00144           header("Pragma: no-cache\n");
00145           printf($rslt);
00146           exit (0);
00147         }
00148 
00149         break;
00150       case "toEEPROM":
00151         if ($wprot>=0) {
00152           i2c_setprot (1,0xa0,1,(1-$wprot));
00153         }
00154         if ($data=="") {
00155           if ($model  === "") {$rslt="model  not specified"; break;}
00156           if ($rev    === "") {$rslt="rev    not specified"; break;}
00157           if ($serial === "") {$rslt="serial not specified"; break;}
00158           if ($time   === "") {$rslt="time   not specified"; break;}
00159            $xml = new SimpleXMLElement("<?xml version='1.0' standalone='yes'?><board/>");
00160            $xml->addChild ('brand',$brand);
00161            $xml->addChild ('model',$model);
00162            $xml->addChild ('rev',  $rev);
00163            $xml->addChild ('serial',$serial);
00164            $xml->addChild ('time',$time);
00165            $data=$xml->asXML();
00166         }
00167         if (strlen($data)>256) {
00168            $rslt="data too long - ".strlen($data)." bytes, only 256 are permitted";
00169            break;
00170         }
00171         $rslt="written ".i2c_write256b($data);
00172         i2c_setprot (1,0xa0,1,0);
00173         break;
00174       case "ctl":
00175         if ($bus=="") {
00176          $rslt="i2c bus number is not specified";
00177          break;
00178         }
00179         $rslt=i2c_ctl($bus,$data)->asXML();
00180         header("Content-Type: text/xml");
00181         header("Content-Length: ".strlen($rslt)."\n");
00182         header("Pragma: no-cache\n");
00183         printf($rslt);
00184         exit (0);
00185     }
00186    if ($rslt=="") {
00187     if (($adr>=0) && (($width==8) || ($width==16))) {
00188       $slave=($adr >> (($width==16)?8:7)) & 0xfe;
00189       if ($wprot>=0) {
00190        i2c_setprot ($bus,$slave,1,(1-$wprot));
00191       }
00192       if ($rprot>=0) {
00193        i2c_setprot ($bus,$slave,0,(1-$rprot));
00194       }
00195       $msg.="<width>".$width."</width>\n";
00196       $msg.="<bus>".$bus."</bus>\n";
00197       $msg.="<slave>".(sprintf("0x%x",($adr>>(($width==8)?7:8))  & 0xfe ))."</slave>\n";
00198 
00199       if (!$raw) {
00200         $msg.="<adr>".$adr."</adr>\n";
00201         $msg.="<hex_adr>".sprintf("0x%x",$adr)."</hex_adr>\n";
00202       }
00203      if ($data!="") { 
00204       $data+=0;
00205       $msg.="<data>".$data."</data>\n";
00206       $msg.="<wdata>".$data."</wdata>\n";
00207       $msg.="<hex_data>".sprintf("0x%x",$data)."</hex_data>\n";
00208       $rslt=i2c_send($width,$bus,$adr,$data,$raw);
00209       if (!($rslt>0)) $msg.="<error> \"i2c write error (".$rslt.")\"</error>\n";
00210      } else { 
00211       $data=i2c_receive($width,$bus,$adr,$raw);
00212       $msg.="<data>".$data."</data>\n";
00213       $msg.="<rdata>".$data."</rdata>\n";
00214       $msg.="<hex_data>".sprintf("0x%x",$data)."</hex_data>\n";
00215       if ($data==-1) $msg.="<error> \"i2c read error\"</error>\n";
00216      }
00217     } else {
00218      if (!$nopars) $msg.="<error>\"Address (adr or raw) or width are not specified or are not positive.\"</error>\n";
00219      $msg.="<usage>\"open URL: i2c.php?width&#061;www&amp;bus&#061;bbb&amp;adr&#061;aaa&amp;data&#061;ddd\"</usage>\n";
00220     }
00221    } else {
00222       $msg.="<result>\"".$rslt."\"</result>\n";
00223    }
00224 
00225    $msg.="</i2c>\n";
00226    header("Content-Type: text/xml");
00227    header("Content-Length: ".strlen($msg)."\n");
00228    header("Pragma: no-cache\n");
00229    printf($msg);
00230 ?>

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