Commit 53fbcbd6 authored by Andrey Filippov's avatar Andrey Filippov

added [to|from]_EEPROM5 and i2c device access on a system i2c bus

parent 9d392245
...@@ -136,6 +136,8 @@ if (count ( $_GET ) == 0) { ...@@ -136,6 +136,8 @@ if (count ( $_GET ) == 0) {
case "toEEPROM3" : case "toEEPROM3" :
case "fromEEPROM4" : case "fromEEPROM4" :
case "toEEPROM4" : case "toEEPROM4" :
case "fromEEPROM5" :
case "toEEPROM5" :
case "ctl" : case "ctl" :
$cmd = $value; $cmd = $value;
break; break;
...@@ -227,9 +229,10 @@ switch ($cmd) { ...@@ -227,9 +229,10 @@ switch ($cmd) {
case "fromEEPROM2" : case "fromEEPROM2" :
case "fromEEPROM3" : case "fromEEPROM3" :
case "fromEEPROM4" : case "fromEEPROM4" :
case "fromEEPROM5" :
$EEPROM_bus0 = intval ( substr ( $cmd, 10 ) ); // and fall below $EEPROM_bus0 = intval ( substr ( $cmd, 10 ) ); // and fall below
case "fromEEPROM" : case "fromEEPROM" :
if ($EEPROM_bus0 == 4) { // using FPGA PIO bus (bus=1 in NC353) for IMU, GPS and such if (($EEPROM_bus0 == 4) || ($EEPROM_bus0 == 5)) { // using FPGA PIO bus (bus=1 in NC353) for IMU, GPS and such, bus = 5 - 10389 eeprom
$rslt=i2c_read256b(0xa0+($EEPROM_chn * 2), $EEPROM_bus0); $rslt=i2c_read256b(0xa0+($EEPROM_chn * 2), $EEPROM_bus0);
$zero=strpos($rslt,chr(0)); $zero=strpos($rslt,chr(0));
if ($zero!==false) $rslt=substr($rslt,0, $zero); if ($zero!==false) $rslt=substr($rslt,0, $zero);
...@@ -290,9 +293,11 @@ switch ($cmd) { ...@@ -290,9 +293,11 @@ switch ($cmd) {
case "toEEPROM2" : case "toEEPROM2" :
case "toEEPROM3" : case "toEEPROM3" :
case "toEEPROM4" : case "toEEPROM4" :
case "toEEPROM5" :
$EEPROM_bus0 = intval ( substr ( $cmd, 8 ) ); // and fall below $EEPROM_bus0 = intval ( substr ( $cmd, 8 ) ); // and fall below
case "toEEPROM" : case "toEEPROM" :
if ($EEPROM_bus0 == 4) { // using FPGA PIO bus (bus=1 in NC353) for IMU, GPS and such // 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
if ($wprot>=0) { if ($wprot>=0) {
i2c_setprot ($EEPROM_bus0, 0xa0+($EEPROM_chn*2),1,(1-$wprot)); i2c_setprot ($EEPROM_bus0, 0xa0+($EEPROM_chn*2),1,(1-$wprot));
} }
...@@ -406,6 +411,10 @@ switch ($cmd) { ...@@ -406,6 +411,10 @@ switch ($cmd) {
exit ( 0 ); exit ( 0 );
} }
if ($rslt == "") { if ($rslt == "") {
// $msg .= "<width1>" . $width . "</width1>\n";
// $msg .= "<bus1>" . $bus . "</bus1>\n";
// $msg .= "<slave1>" . (sprintf ( "0x%x", ($adr >> (($width == 8) ? 7 : 8)) & 0xfe )) . "</slave1>\n";
if ($bus > 3) { if ($bus > 3) {
if (($adr >= 0) && (($width == 8) || ($width == 16))) { if (($adr >= 0) && (($width == 8) || ($width == 16))) {
$slave = ($adr >> (($width == 16) ? 8 : 7)) & 0xfe; $slave = ($adr >> (($width == 16) ? 8 : 7)) & 0xfe;
......
...@@ -124,6 +124,16 @@ el10359: 0x8 1 2 500 kHz ...@@ -124,6 +124,16 @@ el10359: 0x8 1 2 500 kHz
mt9p006: 0x48 1 2 500 kHz mt9p006: 0x48 1 2 500 kHz
mt9f002: 0x10 2 2 500 kHz mt9f002: 0x10 2 2 500 kHz
*/ */
/** Convert nc353 addres/width to sa7 and register address */
function aw_to_sa7r($adr, ///< composite address, nc353 style (256 bytes for each byte-wide device, 512 bytes for each 16-bit one)
$width ///< Data with 16 or 8
) ///< @return: array(sa7,ra)
{
$sa7 = ($adr >> (($width == 16) ? 9 : 8)) & 0x7f;
$ra = ($adr >> (($width == 16) ? 1 : 0)) & 0xff;
return array(sa7,ra);
}
function getSlowArray($usec=0){ function getSlowArray($usec=0){
if ($usec<0) $usec=0; if ($usec<0) $usec=0;
...@@ -191,7 +201,19 @@ function i2c_send($width, $bus, $a, $d, $raw = 0) { // $a<0 - use raw read/write ...@@ -191,7 +201,19 @@ function i2c_send($width, $bus, $a, $d, $raw = 0) { // $a<0 - use raw read/write
return i2c_send_sensor ( $width, $bus, $a, $d, $raw ); return i2c_send_sensor ( $width, $bus, $a, $d, $raw );
else else
$bus = i2c_bus353 ( $bus ); $bus = i2c_bus353 ( $bus );
$bus353 = i2c_bus353 ( $bus ); if ($bus == 2) { // System i2c in nc393 (was 5)
$return = -1;
$w = ($width == 16) ?'w' : 'b';
$sa7r = aw_to_sa7r($a,$width); // works for raw also, $width is 8 for raw
if ($raw){
exec ( 'i2cset -y 0 '.$sa7r[0].' '.$d, $i2c_data, $return );
} else {
exec ( 'i2cget -y 0 '.$sa7r[0].' '.$sa7r[1].' '.$d.' '.$w, $i2c_data, $return );
}
if ($return != 0) return -1;
return ($width == 16)?2:1;
}
$w = ($width == 16) ? 2 : 1; $w = ($width == 16) ? 2 : 1;
$i2c_fn = '/dev/xi2c' . ($raw ? 'raw' : (($w == 2) ? '16' : '8')) . (($bus == 0) ? '' : '_aux'); $i2c_fn = '/dev/xi2c' . ($raw ? 'raw' : (($w == 2) ? '16' : '8')) . (($bus == 0) ? '' : '_aux');
$i2c = fopen ( $i2c_fn, 'w' ); $i2c = fopen ( $i2c_fn, 'w' );
...@@ -204,6 +226,7 @@ function i2c_send($width, $bus, $a, $d, $raw = 0) { // $a<0 - use raw read/write ...@@ -204,6 +226,7 @@ function i2c_send($width, $bus, $a, $d, $raw = 0) { // $a<0 - use raw read/write
return $res; return $res;
} // end of i2c_send() } // end of i2c_send()
// Seems no difference from i2c_send for nc393
function i2c_send_slow($width, $bus, $a, $d, $raw = 0, $extrausec = -1) { // $a<0 - use raw read/write function i2c_send_slow($width, $bus, $a, $d, $raw = 0, $extrausec = -1) { // $a<0 - use raw read/write
if ($bus < 4) if ($bus < 4)
return i2c_send_sensor ( $width, $bus, $a, $d, $raw ); return i2c_send_sensor ( $width, $bus, $a, $d, $raw );
...@@ -213,6 +236,19 @@ function i2c_send_slow($width, $bus, $a, $d, $raw = 0, $extrausec = -1) { // $a< ...@@ -213,6 +236,19 @@ function i2c_send_slow($width, $bus, $a, $d, $raw = 0, $extrausec = -1) { // $a<
$i2c_old_ctrl = i2c_ctl_arr ( 0 ); $i2c_old_ctrl = i2c_ctl_arr ( 0 );
i2c_ctl_arr ( 0, getSlowArray ( $extrausec ) ); i2c_ctl_arr ( 0, getSlowArray ( $extrausec ) );
} }
if ($bus == 2) { // System i2c in nc393 (was 5)
$return = -1;
$w = ($width == 16) ?'w' : 'b';
$sa7r = aw_to_sa7r($a,$width); // works for raw also, $width is 8 for raw
if ($raw){
exec ( 'i2cset -y 0 '.$sa7r[0].' '.$d, $i2c_data, $return );
} else {
exec ( 'i2cget -y 0 '.$sa7r[0].' '.$sa7r[1].' '.$d.' '.$w, $i2c_data, $return );
}
if ($return != 0) return -1;
return ($width == 16)?2:1;
}
$w = ($width == 16) ? 2 : 1; $w = ($width == 16) ? 2 : 1;
$i2c_fn = '/dev/xi2c' . ($raw ? 'raw' : (($w == 2) ? '16' : '8')) . (($bus == 0) ? '' : '_aux'); $i2c_fn = '/dev/xi2c' . ($raw ? 'raw' : (($w == 2) ? '16' : '8')) . (($bus == 0) ? '' : '_aux');
$i2c = fopen ( $i2c_fn, 'w' ); $i2c = fopen ( $i2c_fn, 'w' );
...@@ -241,11 +277,23 @@ function smbus_send($a, $d) { // d - array ...@@ -241,11 +277,23 @@ function smbus_send($a, $d) { // d - array
return $res; return $res;
} // end of i2c_send() } // end of i2c_send()
function i2c_receive($width, $bus, $a, $raw = 0) { function i2c_receive($width, $bus, $a, $raw = 0) {
if ($bus < 4) if ($bus < 4)
return i2c_receive_sensor ( $width, $bus, $a, $raw ); return i2c_receive_sensor ( $width, $bus, $a, $raw );
else else
$bus = i2c_bus353 ( $bus ); $bus = i2c_bus353 ( $bus );
if ($bus == 2) { // System i2c in nc393 (was 5)
$w = ($width == 16) ?'w' : 'b';
$sa7r = aw_to_sa7r($a,$width); // works for raw also, $width is 8 for raw
if ($raw){
exec ( 'i2cget -y 0 '.$sa7r[0], $i2c_data, $return );
} else {
exec ( 'i2cget -y 0 '.$sa7r[0].' '.$sa7r[1].' '.$w, $i2c_data, $return );
}
if ($return != 0) return -1;
return $i2c_data[0];
}
$w = ($width == 16) ? 2 : 1; $w = ($width == 16) ? 2 : 1;
$i2c_fn = '/dev/xi2c' . ($raw ? 'raw' : (($w == 2) ? '16' : '8')) . (($bus == 0) ? '' : '_aux'); $i2c_fn = '/dev/xi2c' . ($raw ? 'raw' : (($w == 2) ? '16' : '8')) . (($bus == 0) ? '' : '_aux');
$i2c = fopen ( $i2c_fn, 'r' ); $i2c = fopen ( $i2c_fn, 'r' );
...@@ -257,6 +305,8 @@ function i2c_receive($width, $bus, $a, $raw = 0) { ...@@ -257,6 +305,8 @@ function i2c_receive($width, $bus, $a, $raw = 0) {
$v = unpack ( ($w == 1) ? 'C' : 'n1', $data ); $v = unpack ( ($w == 1) ? 'C' : 'n1', $data );
return $v [1]; return $v [1];
} // end of i2c_receive() } // end of i2c_receive()
// i2c_receive_slow is the same as i2c_receive for nc393
function i2c_receive_slow($width, $bus, $a, $raw = 0, $extrausec = -1) { function i2c_receive_slow($width, $bus, $a, $raw = 0, $extrausec = -1) {
if ($bus < 4) if ($bus < 4)
return i2c_receive_sensor ( $width, $bus, $a, $raw ); return i2c_receive_sensor ( $width, $bus, $a, $raw );
...@@ -266,6 +316,17 @@ function i2c_receive_slow($width, $bus, $a, $raw = 0, $extrausec = -1) { ...@@ -266,6 +316,17 @@ function i2c_receive_slow($width, $bus, $a, $raw = 0, $extrausec = -1) {
$i2c_old_ctrl = i2c_ctl_arr ( 0 ); $i2c_old_ctrl = i2c_ctl_arr ( 0 );
i2c_ctl_arr ( 0, getSlowArray ( $extrausec ) ); i2c_ctl_arr ( 0, getSlowArray ( $extrausec ) );
} }
if ($bus == 2) { // System i2c in nc393 (was 5)
$w = ($width == 16) ?'w' : 'b';
$sa7r = aw_to_sa7r($a,$width); // works for raw also, $width is 8 for raw
if ($raw){
exec ( 'i2cget -y 0 '.$sa7r[0], $i2c_data, $return );
} else {
exec ( 'i2cget -y 0 '.$sa7r[0].' '.$sa7r[1].' '.$w, $i2c_data, $return );
}
if ($return != 0) return -1;
return $i2c_data[0];
}
$w = ($width == 16) ? 2 : 1; $w = ($width == 16) ? 2 : 1;
$i2c_fn = '/dev/xi2c' . ($raw ? 'raw' : (($w == 2) ? '16' : '8')) . (($bus == 0) ? '' : '_aux'); $i2c_fn = '/dev/xi2c' . ($raw ? 'raw' : (($w == 2) ? '16' : '8')) . (($bus == 0) ? '' : '_aux');
$i2c = fopen ( $i2c_fn, 'r' ); $i2c = fopen ( $i2c_fn, 'r' );
...@@ -282,8 +343,10 @@ function i2c_receive_slow($width, $bus, $a, $raw = 0, $extrausec = -1) { ...@@ -282,8 +343,10 @@ function i2c_receive_slow($width, $bus, $a, $raw = 0, $extrausec = -1) {
} // end of i2c_receive() } // end of i2c_receive()
function i2c_setprot($bus, $slave, $bit, $value) { // !slave is MSB aligned, LSB==0) function i2c_setprot($bus, $slave, $bit, $value) { // !slave is MSB aligned, LSB==0)
if ($bus != 4)
return -1; // applicable only to grand-daughter i2c (like imu, gps)
$bus = i2c_bus353 ( $bus ); $bus = i2c_bus353 ( $bus );
$i2cprot = fopen ( "/dev/xi2cenable", 'r+' ); $i2cprot = fopen ("/dev/xi2cenable", 'r+' );
fseek ( $i2cprot, ($bus * 128) + ($slave >> 1) ); fseek ( $i2cprot, ($bus * 128) + ($slave >> 1) );
$data = ord ( fread ( $i2cprot, 1 ) ); $data = ord ( fread ( $i2cprot, 1 ) );
...@@ -344,57 +407,86 @@ function i2c_setCMOSClock(){ ...@@ -344,57 +407,86 @@ function i2c_setCMOSClock(){
return "OK"; return "OK";
} // end of i2c_setCMOSClock() } // end of i2c_setCMOSClock()
function i2c_read256b($slave=0xa0,$bus=1,$extrausec=0) { //will read 256 bytes from slave (address is 8-bit, includes r/~w) function i2c_read256b($slave = 0xa0, $bus = 4, $extrausec = 0) { // will read 256 bytes from slave (address is 8-bit, includes r/~w)
if ($bus < 4) if ($bus < 4)
return i2c_read256b_sensor ( $slave, $bus, $extrausec); // ($name, $sensor_port, $sa7_offset) return i2c_read256b_sensor ( $slave, $bus, $extrausec ); // ($name, $sensor_port, $sa7_offset)
else else
$bus = i2c_bus353 ( $bus ); $bus = i2c_bus353 ( $bus );
if (($bus==0) && ($extrausec>=0)){ if (($bus == 0) && ($extrausec >= 0)) {
$i2c_old_ctrl= i2c_ctl_arr(0); $i2c_old_ctrl = i2c_ctl_arr ( 0 );
i2c_ctl_arr(0,getSlowArray($extrausec)); i2c_ctl_arr ( 0, getSlowArray ( $extrausec ) );
} }
$i2c_fn='/dev/xi2c8'.(($bus==0)?'':'_aux'); if ($bus == 2) { // System i2c in nc393 (was 5)
$i2c = fopen($i2c_fn, 'r'); $sa7 = $slave >> 1;
fseek ($i2c, $slave*128) ; //256 per slave, but slave are only even for($i = 0; $i < 256; $i ++)
$data = fread($i2c, 256); // full 256 bytes exec ( 'i2cget -y 0 ' . $sa7 . ' ' . $i . ' b', $i2c_data, $return );
fclose($i2c); if ($return != 0)
if (($bus==0) && ($extrausec>=0)){ return - 1;
i2c_ctl_arr(0,$i2c_old_ctrl); /// restore old speed (not thread-safe) $data == "";
foreach ($i2c_data as $c) $data.=chr($c);
return $data;
}
$i2c_fn = '/dev/xi2c8' . (($bus == 0) ? '' : '_aux');
$i2c = fopen ( $i2c_fn, 'r' );
fseek ( $i2c, $slave * 128 ); // 256 per slave, but slave are only even
$data = fread ( $i2c, 256 ); // full 256 bytes
fclose ( $i2c );
if (($bus == 0) && ($extrausec >= 0)) {
i2c_ctl_arr ( 0, $i2c_old_ctrl ); // / restore old speed (not thread-safe)
} }
return $data; return $data;
} // end of i2c_read256b () } // end of i2c_read256b ()
//this EEPROM writes only 4 bytes sequentionally (only 2 LSBs are incremented) //this EEPROM writes only 4 bytes sequentionally (only 2 LSBs are incremented)
function i2c_write256b($data, $slave=0xa0,$bus=1,$extrausec=0) { //will write up to 256 bytes $data to slave (address is 8-bit, includes r/~w). EEPROM should be un-protected
function i2c_write256b($data, $slave = 0xa0, $bus = 4, $extrausec = 0) { // will write up to 256 bytes $data to slave (address is 8-bit, includes r/~w). EEPROM should be un-protected
if ($bus < 4) if ($bus < 4)
return i2c_write256b_sensor ($data, $slave, $bus, $extrausec); // ($data, $name, $sensor_port, $sa7_offset) return i2c_write256b_sensor ( $data, $slave, $bus, $extrausec ); // ($data, $name, $sensor_port, $sa7_offset)
else else
$bus = i2c_bus353 ( $bus ); $bus = i2c_bus353 ( $bus );
if (($bus==0) && ($extrausec>=0)){ if (($bus == 0) && ($extrausec >= 0)) {
$i2c_old_ctrl= i2c_ctl_arr(0); $i2c_old_ctrl = i2c_ctl_arr ( 0 );
i2c_ctl_arr(0,getSlowArray($extrausec)); i2c_ctl_arr ( 0, getSlowArray ( $extrausec ) );
} }
$maxretries=200; //measured - 19 $maxretries = 200; // measured - 19
if (!is_string($data)) return -1; $len = 0;
if (strlen($data)>256) return -2; if (! is_string ( $data ))
if (strlen($data)<256) $data.=chr(0); return - 1;
$i2c_fn='/dev/xi2c8'.(($bus==0)?'':'_aux'); if (strlen ( $data ) > 256)
$i2c = fopen($i2c_fn, 'w'); return - 2;
$len=0; if (strlen ( $data ) < 256)
for ($i=0;$i<strlen($data); $i+=4) { $data .= chr ( 0 );
for ($retry=0; $retry< $maxretries; $retry++) {
fseek ($i2c, $slave*128+$i) ; //256 per slave, but slave are only even if ($bus == 2) { // System i2c in nc393 (was 5)
$rslt=fwrite($i2c, substr($data,$i,4)); $sa7 = $slave >> 1;
if ($rslt>0) break; foreach (str_split($data) as $d) {
exec ( 'i2cset -y 0 ' . $sa7 . ' ' .$len . ' ' . ord($d) . ' b', $i2c_data, $return );
if ($return != 0)
return - 1;
usleep ( 10000 );
$len ++;
}
return $len;
} }
if ($rslt<=0) { //foreach (str_split($data) as $d) echo ord($d);
$len=$rslt; $i2c_fn = '/dev/xi2c8' . (($bus == 0) ? '' : '_aux');
$i2c = fopen ( $i2c_fn, 'w' );
for($i = 0; $i < strlen ( $data ); $i += 4) {
for($retry = 0; $retry < $maxretries; $retry ++) {
fseek ( $i2c, $slave * 128 + $i ); // 256 per slave, but slave are only even
$rslt = fwrite ( $i2c, substr ( $data, $i, 4 ) );
if ($rslt > 0)
break; break;
} }
$len+=$rslt; if ($rslt <= 0) {
$len = $rslt;
break;
} }
fclose($i2c); $len += $rslt;
if (($bus==0) && ($extrausec>=0)){ }
i2c_ctl_arr(0,$i2c_old_ctrl); /// restore old speed (not thread-safe) fclose ( $i2c );
if (($bus == 0) && ($extrausec >= 0)) {
i2c_ctl_arr ( 0, $i2c_old_ctrl ); // / restore old speed (not thread-safe)
} }
return $len; return $len;
} // end of i2c_write256b () } // end of i2c_write256b ()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment