The following code is a common example that an I2C master writes data to a slave.
<?php
$wbuf = 0x7A;
$pid = pid_open("/mmap/i2c0"); // open I2C 0
pid_ioctl($pid, "set mode fm"); // set data rate: fast mode
pid_ioctl($pid, "set saddr ee"); // set slave device address: 0xEE
pid_write($pid, $wbuf, 1); // input a byte to buffer: 0x7A
pid_ioctl($pid, "req write"); // request to write
while(pid_ioctl($pid, "get txlen")) // check received data
;
pid_close($pid);
?>
<?php
$pid = pid_open("/mmap/i2c0"); // open I2C 0
pid_ioctl($pid, "set mode fm"); // set data rate: fast mode
pid_ioctl($pid, "set saddr ee"); // set slave device address: 0xEE
pid_ioctl($pid, "req write wait"); // request to write and wait
pid_write($pid, 0x7A, 1); // write 1 byte: 0x7A
pid_write($pid, 0x8A, 1); // write 1 byte: 0x8A
pid_write($pid, 0x9A, 1); // write 1 byte: 0x9A
while(pid_ioctl($pid, "get txlen")) // check received data
;
pid_ioctl($pid, "req stop"); // stop writing data
pid_close($pid);
?>
The following code is a common example that an I2C master receives data from a slave.
<?php
$rbuf = "";
$pid = pid_open("/mmap/i2c0"); // open I2C
pid_ioctl($pid, "set mode fm"); // set data rate: fast mode
pid_ioctl($pid, "set saddr ee"); // set slave device address: 0xEE
pid_ioctl($pid, "req read 2"); // request to read 2 bytes
while(pid_ioctl($pid, "get rxlen") < 2) // check received data
;
pid_read($pid, $rbuf); // read buffer
pid_close($pid);
?>