00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
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":
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
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=www&bus=bbb&adr=aaa&data=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 ?>