I2C使用例


通过Slave写数据

下面是PHPoC通过I2C slave写数据的一般例。

读写邀请 例

$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);

等待读写邀请及执行 例

$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);

自Slave读取数据

下面是PHPoC读取I2C slave数据的一般例子。

邀请读取例

$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);