i2c.php 15.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
<?php
/*!***************************************************************************
*! FILE NAME  : i2c.php
*! DESCRIPTION: Provides single byte/short r/w from/to the i2c
*!              Setting per-slave protection mask,
*!              Synchronizing system clock with a "CMOS" one
*!              Reading from/writing to EEPROM memory
*! Copyright (C) 2008 Elphel, Inc
*! -----------------------------------------------------------------------------**
*!
*!  This program is free software: you can redistribute it and/or modify
*!  it under the terms of the GNU General Public License as published by
*!  the Free Software Foundation, either version 3 of the License, or
*!  (at your option) any later version.
*!
*!  This program is distributed in the hope that it will be useful,
*!  but WITHOUT ANY WARRANTY; without even the implied warranty of
*!  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*!  GNU General Public License for more details.
*!
*!  You should have received a copy of the GNU General Public License
*!  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*! -----------------------------------------------------------------------------**
*!  $Log: i2c.php,v $
*!  Revision 1.7  2012/04/08 04:08:58  elphel
*!  bug fix in temperature measurement
*!
*!  Revision 1.6  2012/01/16 01:42:24  elphel
*!  comments
*!
*!  Revision 1.5  2011/12/22 05:35:11  elphel
*!  Added "slow" option (for slow i2c devices) and reading the sensor board temperature
*!
*!  Revision 1.4  2011/08/13 00:51:21  elphel
*!  support for "granddaughter" board ID eeprom
*!
*!  Revision 1.3  2011/01/18 17:38:34  dzhimiev
*!  1. added <len> record,  passed through 'length'
*!
*!  Revision 1.2  2010/08/10 21:11:40  elphel
*!  added EEPROM support of the 10359 and 10338D boards.
*!
*!  Revision 1.1.1.1  2008/11/27 20:04:03  elphel
*!
*!
*!  Revision 1.8  2008/04/24 20:32:44  elphel
*!  reverted, wrong edit
*!
*!  Revision 1.7  2008/04/24 18:19:50  elphel
*!  changed to absolute path to i2c.inc
*!
*!  Revision 1.6  2008/03/16 01:24:09  elphel
*!  Added i2c speed control interface
*!
*!  Revision 1.5  2008/03/15 23:05:18  elphel
*!  split i2c.php into i2c.php and i2c.inc (to be used from other scripts)
*!
*!  Revision 1.4  2008/03/15 18:38:21  elphel
*!  Implemented add-on boards EEPROM support
*!
*!  Revision 1.3  2008/02/20 07:41:42  elphel
*!  report error if the date is wrong
*!
*!  Revision 1.2  2008/02/13 00:30:01  elphel
*!  Added system time synchronization  with "CMOS" clock
*!
*!  Revision 1.1  2008/02/12 21:53:20  elphel
*!  Modified I2c to support multiple buses, added raw access (no address registers) and per-slave protection bitmasks
*!
*!
*/

73
// In NC393: buses 0..3 - sensors(old bus=0), 4 - aux (old bus=1), 5 - system i2c (TODO)
74 75 76
set_include_path(get_include_path() . PATH_SEPARATOR . '/www/pages/include');
require 'i2c_include.php';

77 78 79 80 81 82 83 84 85 86 87 88
$width = 0;
$bus = 0;
$adr = - 1;
$class = "";
$sa7offs = 0;
$data = "";
$nopars = false;
$raw = 0;
$wprot = - 1;
$rprot = - 1;
$rslt = "";
$cmd = "";
89
//
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
$model = "";
$rev = "";
$brand = "Elphel";
$serial = "";
$time = "";
$port = "";
$part = "";
$baud = "";
$sync = "";
$slowbus0 = - 1; // extra microseconds for SCL low and SCL high
$EEPROM_bus0 = - 1;
$EEPROM_chn = 0; // / 0 - 10359/single 10338D, 1..3 - 10338D attached to the 10359
$application = '';
$applicationMode = '';
$sensor = '';
$temperatureSA = 0x30;
$temperarureDelay = 1.0; // extra microseconds for SCL low/high (nominal 4, but works with 1)
$msg = "<?xml version=\"1.0\"?>\n<i2c>\n";
if (count ( $_GET ) == 0) {
	$nopars = true;
} else {
	$pars = array ();
	foreach ( $_GET as $key => $value ) {
		switch ($key) {
			case "slow" :
			case "slowbus0" :
				$slowbus0 = $value + 0;
				break;
			case "cmd" :
				switch ($value) {
					case "fromCMOS" :
						// TODO: NC393: implement
						// $rslt=i2c_getCMOSClock()?"Set system time error (probably CMOS clock is not set)":("System clock is set to ".exec("date"));
						break;
					case "toCMOS" :
						$rslt = i2c_setCMOSClock ();
						break;
					case "fromEEPROM" :
					case "toEEPROM" :
					case "fromEEPROM0" :
					case "toEEPROM0" :
					case "fromEEPROM1" :
					case "toEEPROM1" :
					case "fromEEPROM2" :
					case "toEEPROM2" :
					case "fromEEPROM3" :
					case "toEEPROM3" :
137 138
					case "fromEEPROM4" :
					case "toEEPROM4" :
139 140
					case "fromEEPROM5" :
					case "toEEPROM5" :
141
					case "ctl" :
142 143
					$cmd = $value;
					break;
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
				}
				break;
			case "EEPROM_chn" :
				$EEPROM_chn = $value;
				break;
			case "app" :
			case "application" :
				$application = $value;
				break;
			case "mode" :
			case "application_mode" :
				$application_mode = $value;
				break;
			case "sensor" :
				$sensor = $value;
				break;
			case "port" :
				$port = $value;
				break;
			case "part" :
				$part = $value;
				break;
			case "baud" :
				$baud = $value;
				break;
			case "sync" :
				$sync = $value;
				break;
			case "model" :
				$model = $value;
				break;
			case "rev" :
				$rev = $value;
				break;
			case "serial" :
				$serial = $value;
				break;
			case "time" : // seconds from 1970
				$time = $value + 0;
				break;
			case "raw" :
				$adr = ($value + 0) & ~ 0x7f;
				$raw = 1;
				$width = 8;
				break;
			case "width" :
				$width = $value + 0;
				break;
			case "bus" :
				$bus = $value + 0;
				break;
			case "adr" :
				$adr = $value + 0;
				break;
			case "wp" :
				if ($value == 0)
					$wprot = 0;
				else if ($value == 1)
					$wprot = 1;
				break;
			case "rp" :
				if ($value == 0)
					$rprot = 0;
				else if ($value == 1)
					$rprot = 1;
				break;
			case "data" :
				$data = $value;
				break;
			case "length" :
				$length = $value + 0;
				break;
			case "class" :
				$class = $value;
				break;
			case "sa7offs" :
				$sa7offs = $value + 0;
				break;
		}
	}
}
225

226 227 228 229 230
switch ($cmd) {
	case "fromEEPROM0" :
	case "fromEEPROM1" :
	case "fromEEPROM2" :
	case "fromEEPROM3" :
231
	case "fromEEPROM4" :
232
	case "fromEEPROM5" :
233 234
		$EEPROM_bus0 = intval ( substr ( $cmd, 10 ) ); // and fall below
	case "fromEEPROM" :
235
		if (($EEPROM_bus0 == 4) || ($EEPROM_bus0 == 5)) { // using FPGA PIO bus (bus=1 in NC353) for IMU, GPS and such, bus = 5 - 10389 eeprom
236
			$rslt=i2c_read256b(0xa0+($EEPROM_chn * 2), $EEPROM_bus0);
237 238 239
			if (!is_string($rslt)){
				break; // will report error 
			}
240 241 242 243 244 245 246 247 248 249 250 251 252
			$zero=strpos($rslt,chr(0));
			if ($zero!==false) $rslt=substr($rslt,0, $zero);
			if (substr($rslt,0,5)=="<?xml") {
				$xml=simplexml_load_string($rslt);
				$xml_string=$xml->asXML();
				header("Content-Type: text/xml");
				header("Content-Length: ".strlen($xml_string)."\n");
				header("Pragma: no-cache\n");
				printf($xml_string);
				exit (0);
			}
			break;
		}
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
		if (($EEPROM_bus0 < 0) || ($EEPROM_bus0 > 3)) {
			// Not supported in NC393 - TODO: implement with bus=5 for 10389 board serial number
			break;
		}
		if (($EEPROM_chn < 0) || ($EEPROM_chn > 4)) {
			// Not supported in NC393
			break;
		}
		
		// print(i2c_read256b_sensor('sensor_eeprom',3,0));
		// $rslt=i2c_read256b(0xa0+($EEPROM_chn*($EEPROM_bus0?4:2)), $EEPROM_bus0?0:1);
		$rslt = i2c_read256b ( 'sensor_eeprom', $EEPROM_bus0, 2 * $EEPROM_chn );
		$zero = strpos ( $rslt, chr ( 0 ) );
		if ($zero !== false)
			$rslt = substr ( $rslt, 0, $zero );
		if (substr ( $rslt, 0, 5 ) == "<?xml") {
			$xml = simplexml_load_string ( $rslt );
			// $temperatureSA=0x30;
			// $temperarureDelay=1.0; // extra microseconds for SCL low/high (nominal 4, but works with 1)
			// try reading sensor temperature (if available)
			if ($EEPROM_bus0 >= 0) {
				$data = i2c_receive_sensor ( 'sensor_temp', $EEPROM_bus0, 5, 2 * $EEPROM_chn );
				$bus = 0;
				// $sa=$temperatureSA+($EEPROM_chn*($EEPROM_bus0?4:2));
				// $data=i2c_receive_slow(16,$bus,$sa*128+5,0,$temperarureDelay);
				if (($data >= 0) && ($data!=0xffff)) { // <0 - i2c error - not implemented
					$data = (($data & 0x1000) ? - 1 : 1) * ($data & 0xfff) / 16.0;
					$xml->addChild ( 'sensorTemperature', $data );
					// i2c_send_slow(8,$bus,$sa*128+8,3,0,$temperarureDelay); // set slow conversion, 1/16C resolution - for the next time
					i2c_send_sensor ( 'sensor_temp', $EEPROM_bus0, 8, 3, 2 * $EEPROM_chn );
				}
			}
			$xml_string = $xml->asXML ();
			header ( "Content-Type: text/xml" );
			header ( "Content-Length: " . strlen ( $xml_string ) . "\n" );
			header ( "Pragma: no-cache\n" );
			printf ( $xml_string );
			exit ( 0 );
		}
		
		break;
	case "toEEPROM0" :
	case "toEEPROM1" :
	case "toEEPROM2" :
	case "toEEPROM3" :
298
	case "toEEPROM4" :
299
	case "toEEPROM5" :
300 301
		$EEPROM_bus0 = intval ( substr ( $cmd, 8 ) ); // and fall below
	case "toEEPROM" :
302 303
//		echo "<!--  EEPROM_bus0=". $EEPROM_bus0." EEPROM_chn=".$EEPROM_chn."-->";
		if (($EEPROM_bus0 == 4) || ($EEPROM_bus0 == 5)) { // using FPGA PIO bus (bus=1 in NC353) for IMU, GPS and such
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
			if ($wprot>=0) {
				i2c_setprot ($EEPROM_bus0, 0xa0+($EEPROM_chn*2),1,(1-$wprot));
			}
			if ($data=="") {
				if ($model  === "") {$rslt="model  not specified"; break;}
				if ($rev    === "") {$rslt="rev    not specified"; break;}
				if ($serial === "") {$rslt="serial not specified"; break;}
				if ($time   === "") {$rslt="time   not specified"; break;}
				$xml = new SimpleXMLElement("<?xml version='1.0' standalone='yes'?><board/>");
				if ($brand!='') $xml->addChild ('brand',$brand);
				if ($model!='') $xml->addChild ('model',$model);
				if ($rev!='') $xml->addChild ('rev',  $rev);
				if ($serial!='') $xml->addChild ('serial',$serial);
				if ($time!='') $xml->addChild ('time',$time);
				if ($application!="") $xml->addChild ('app',$application);
				if ($application_mode!="") $xml->addChild ('mode',$application_mode);
				if ($sensor!='') $xml->addChild ('sensor',$sensor);
				if ($length!='') $xml->addChild ('len',$length);
				if ($port!='') $xml->addChild ('port',$port);
				if ($part!='') $xml->addChild ('part',$part);
				if ($baud!='') $xml->addChild ('baud',$baud);
				if ($sync!='') $xml->addChild ('sync',$sync);
				$data=$xml->asXML();
			}
			if (strlen($data)>256) {
				$rslt="data too long - ".strlen($data)." bytes, only 256 are permitted";
				break;
			}
			$rslt="written ".i2c_write256b($data,0xa0+($EEPROM_chn*2), $EEPROM_bus0);
			i2c_setprot ($EEPROM_bus0,0xa0+($EEPROM_chn*2),1,0);
			break;
		}
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
		if (($EEPROM_bus0 < 0) || ($EEPROM_bus0 > 3)) {
			// Not supported in NC393 - TODO: implement with bus=5 for 10389 board serial number
			break;
		}
		if (($EEPROM_chn < 0) || ($EEPROM_chn > 4)) {
			// Not supported in NC393
			break;
		}
		
		if ($wprot >= 0) {
			// not implemented in NC393?
			// i2c_setprot ($EEPROM_bus0?0:1,0xa0+($EEPROM_chn*($EEPROM_bus0?4:2)),1,(1-$wprot));
		}
		if ($data == "") {
			if ($model === "") {
				$rslt = "model  not specified";
				break;
			}
			if ($rev === "") {
				$rslt = "rev    not specified";
				break;
			}
			if ($serial === "") {
				$rslt = "serial not specified";
				break;
			}
			if ($time === "") {
				$rslt = "time   not specified";
				break;
			}
			$xml = new SimpleXMLElement ( "<?xml version='1.0' standalone='yes'?><board/>" );
			if ($brand != '')
				$xml->addChild ( 'brand', $brand );
			if ($model != '')
				$xml->addChild ( 'model', $model );
			if ($rev != '')
				$xml->addChild ( 'rev', $rev );
			if ($serial != '')
				$xml->addChild ( 'serial', $serial );
			if ($time != '')
				$xml->addChild ( 'time', $time );
			if ($application != "")
				$xml->addChild ( 'app', $application );
			if ($application_mode != "")
				$xml->addChild ( 'mode', $application_mode );
			if ($sensor != '')
				$xml->addChild ( 'sensor', $sensor );
			if ($length != '')
				$xml->addChild ( 'len', $length );
			if ($port != '')
				$xml->addChild ( 'port', $port );
			if ($part != '')
				$xml->addChild ( 'part', $part );
			if ($baud != '')
				$xml->addChild ( 'baud', $baud );
			if ($sync != '')
				$xml->addChild ( 'sync', $sync );
			$data = $xml->asXML ();
		}
		if (strlen ( $data ) > 256) {
			$rslt = "data too long - " . strlen ( $data ) . " bytes, only 256 are permitted";
			break;
		}
		// $rslt="written ".i2c_write256b($data,0xa0+($EEPROM_chn*($EEPROM_bus0?4:2)),$EEPROM_bus0?0:1);
		$rslt = "written " . i2c_write256b_sensor ( $data, 'sensor_eeprom', $EEPROM_bus0, 2 * $EEPROM_chn );
		
		// i2c_setprot ($EEPROM_bus0?0:1,0xa0+($EEPROM_chn*($EEPROM_bus0?4:2)),1,0);
		break;
	case "ctl" :
		if ($bus === "") {
			$rslt = "i2c bus number is not specified";
			break;
		}
		$rslt = i2c_ctl ( $bus, $data )->asXML ();
		header ( "Content-Type: text/xml" );
		header ( "Content-Length: " . strlen ( $rslt ) . "\n" );
		header ( "Pragma: no-cache\n" );
		printf ( $rslt );
		exit ( 0 );
}
if ($rslt == "") {
417 418 419 420
//	$msg .= "<width1>" . $width . "</width1>\n";
//	$msg .= "<bus1>" . $bus . "</bus1>\n";
//	$msg .= "<slave1>" . (sprintf ( "0x%x", ($adr >> (($width == 8) ? 7 : 8)) & 0xfe )) . "</slave1>\n";
	
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
	if ($bus > 3) {
		if (($adr >= 0) && (($width == 8) || ($width == 16))) {
			$slave = ($adr >> (($width == 16) ? 8 : 7)) & 0xfe;
			if ($wprot >= 0) {
				// i2c_setprot ($bus,$slave,1,(1-$wprot));
			}
			if ($rprot >= 0) {
				// i2c_setprot ($bus,$slave,0,(1-$rprot));
			}
			$msg .= "<width>" . $width . "</width>\n";
			$msg .= "<bus>" . $bus . "</bus>\n";
			$msg .= "<slave>" . (sprintf ( "0x%x", ($adr >> (($width == 8) ? 7 : 8)) & 0xfe )) . "</slave>\n";
			
			if (! $raw) {
				$msg .= "<adr>" . $adr . "</adr>\n";
				$msg .= "<hex_adr>" . sprintf ( "0x%x", $adr ) . "</hex_adr>\n";
			}
			if ($data != "") { // ! i2c write
				$data += 0;
				$msg .= "<data>" . $data . "</data>\n";
				$msg .= "<wdata>" . $data . "</wdata>\n";
				$msg .= "<hex_data>" . sprintf ( "0x%x", $data ) . "</hex_data>\n";
				$rslt = ($slowbus0 >= 0) ? i2c_send_slow ( $width, $bus, $adr, $data, $raw, $slowbus0 ) : i2c_send ( $width, $bus, $adr, $data, $raw );
				;
				if (! ($rslt > 0))
					$msg .= "<error> \"i2c write error (" . $rslt . ")\"</error>\n";
			} else { // !i2c read
				$data = ($slowbus0 >= 0) ? i2c_receive_slow ( $width, $bus, $adr, $raw, $slowbus0 ) : i2c_receive ( $width, $bus, $adr, $raw );
				$msg .= "<data>" . $data . "</data>\n";
				$msg .= "<rdata>" . $data . "</rdata>\n";
				$msg .= "<hex_data>" . sprintf ( "0x%x", $data ) . "</hex_data>\n";
				if ($data == - 1)
					$msg .= "<error> \"i2c read error\"</error>\n";
			}
		} else {
			if (! $nopars)
				$msg .= "<error>\"Address (adr or raw) or width are not specified or are not positive.\"</error>\n";
			$msg .= "<usage>\"open URL: i2c.php?width&#061;www&amp;bus&#061;bbb&amp;adr&#061;aaa&amp;data&#061;ddd\"</usage>\n";
		}
	} else {
		if ((strlen($class)>0) || ($adr >= 0)) {
			$msg .= "<class>" . $class . "</class>\n";
			$msg .= "<bus>" . $bus . "</bus>\n";
			$msg .= "<sa7offs>" . sprintf ( "0x%x", $sa7offs) . "</sa7offs>\n";
			$msg .= "<adr>" . $adr . "</adr>\n";
			$msg .= "<hex_adr>" . sprintf ( "0x%x", $adr ) . "</hex_adr>\n";
			if ($data != "") { // ! i2c write
				$data += 0;
				$msg .= "<data>" . $data . "</data>\n";
				$msg .= "<wdata>" . $data . "</wdata>\n";
				$msg .= "<hex_data>" . sprintf ( "0x%x", $data ) . "</hex_data>\n";
				$rslt = i2c_send_sensor($class, $bus, $adr, $data, $sa7offs);
				if (! ($rslt > 0))
					$msg .= "<error> \"i2c write error (" . $rslt . ")\"</error>\n";
			} else { // !i2c read
			$data = i2c_receive_sensor($class, $bus, $adr, $sa7offs);
				$msg .= "<data>" . $data . "</data>\n";
				$msg .= "<rdata>" . $data . "</rdata>\n";
				$msg .= "<hex_data>" . sprintf ( "0x%x", $data ) . "</hex_data>\n";
				if ($data == - 1)
					$msg .= "<error> \"i2c read error\"</error>\n";
			}
		} else {
			if (! $nopars)
				$msg .= "<error>\"Device class name and/or register address are not specified address is not positive.\"</error>\n";
				$msg .= "<usage>\"open URL: i2c.php?class&#061;device_class_name&amp;bus&#061;sensor_port&amp;adr&#061;register_address&amp;data&#061;data_to_write&amp;sa7offs&#061;add_to_slave_address\"</usage>\n";
		}
	}
} else {
	$msg .= "<result>\"" . $rslt . "\"</result>\n";
}
492

493 494 495 496 497
$msg .= "</i2c>\n";
header ( "Content-Type: text/xml" );
header ( "Content-Length: " . strlen ( $msg ) . "\n" );
header ( "Pragma: no-cache\n" );
printf ( $msg );
498
?>