使用SPI例


通过Slave发送数据

下面是PHPoC通过SPI Slave发送数据的一般例。

发送数据例

$wbuf = 0xA2;                       // Data to be sent
$rbuf = "";

$pid = pid_open("/mmap/spi0");      // open SPI0
pid_ioctl($pid, "set mode 3");      // set SPI mode to 3
pid_ioctl($pid, "set lsb 0");       // set bit transmission order: MSB first
pid_write($pid, $wbuf, 1);          // write 1 byte to buffer: 0xA2
pid_ioctl($pid, "req start");       // request to write data
while(pid_ioctl($pid, "get txlen")) // check the size of transmitted data
    ;
pid_read($pid, $rbuf, 1);           // read 1 byte
pid_close($pid);

在上面例子中的最后一行,通过pid_read函数读1字节的理由是SPI的数据发送与接收一向同时进行的特点。

自Slave读取数据

下面是PHPoC自SPI接收数据的一般例。

接收数据 例

$wbuf = 0x00;                       // Data to be sent
$rbuf = "";

$pid = pid_open("/mmap/spi0");      // open SPI0
pid_ioctl($pid, "set mode 3");      // set SPI mode to 3
pid_ioctl($pid, "set lsb 0");       // set bit transmission order: MSB first
pid_write($pid, $wbuf, 1);          // write 1 byte to buffer: 0x00
pid_ioctl($pid, "req start");       // request to write data
while(pid_ioctl($pid, "get txlen")) // check the size of transmitted data
    ;
pid_read($pid, $rbuf, 1);           // read 1 byte
pid_close($pid);