To use or set I2C, pid_ioctl function is required. Available commands are as follows:
|set||mode||sm||set data rate: standard - 100 Kbps|
|fm||set data rate: fast - 400 Kbps|
|daddr||[A]||set address: local device address|
|saddr||[A]||set address: slave device address|
|get||rxlen||get the number of pending bytes in receive buffer|
|txlen||get the number of pending bytes in send buffer|
|txfree||get the remaining size of send buffer|
|error||nack||get the number of NACK in the last transaction|
|bus||get the number of bus error in the last transaction|
|state||0 - idle, etc. - I2C internal operation|
|req||read||[N]||request to read|
|write||-||request to write|
|wait||request to write and wait ("req stop" is required)|
|stop||request to stop writing data ("req wait" is required)|
|reset||request to reset bus|
You can set a communication mode and a device address by using a "set" command.
PHPoC provides standard mode and fast mode. The standard mode is the default value.
|standard||pid_ioctl($pid, "set mode sm");|
|fast||pid_ioctl($pid, "set mode fm");|
An I2C master uses a device address to select a slave to send data and a local device address to set its own device address. How to set those addresses is as follows:
|slave device address||pid_ioctl($pid, "set saddr [A]");|
|local device address||pid_ioctl($pid, "set daddr [A]");|
Each device address should be entered in 2 digits hexadecimal number. LSB of this byte is always zero because PHPoC supports 7 bits addressing for I2C. Note that there are some reserved addresses. If you use those addresses, PHPoC will stop due to errors.
|X||X||X||X||X||X||X||1||E1, A3, 1B||LSB is 1|
|0||0||0||0||X||X||X||X||00 ~ 0F||All of 4 Upper bits are 0|
|1||1||1||1||X||X||X||X||F0 ~ FF||All of 4 Upper bits are 1|
You can get status of I2C by using "get" command.
How to get the number of pending bytes in send and receive buffer is as follows:
|Send buffer||pid_ioctl($pid, "get txlen");|
|Receive buffer||pid_ioctl($pid, "get rxlen");|
How to get the remaining size of send buffer in byte-unit is as follows:
|remaining size of send buffer||pid_ioctl($pid, "get txfree");|
You can check the amount of bus errors and NACK in the last transaction.
|NACK||pid_ioctl($pid, "get error nack");|
|bus Error||pid_ioctl($pid, "get error bus");|
How to get an I2C state is as follows: 0 will be returned in idle state and the other values will be returned if otherwise.
|State||pid_ioctl($pid, "get state");|
An I2C master requests to read data from an I2C slave with this command. After sending this data, you can read it by the slave by pid_read function.
|Request to Read Data||pid_ioctl($pid, "req read [N]");|
An I2C master requests to write data to an I2C slave with this command. There are two ways and those are as follows:
|Request to Write Data||pid_ioctl($pid, "req write");|
|Request to Write Data and Wait||pid_ioctl($pid, "req write wait");|
|Stop Writing Data||pid_ioctl($pid, "req stop");|
When "req write" command runs, a master immediately sends data. Thus, you have to input data to a buffer before running this command. On the other hand, "req wait" command changes bus state to start condition and does not change it to stop condition until "req stop" command runs. Thus, you can write data to a slave by using pid_write function before "req stop" command runs.
You can reset an I2C bus with this command when communication is bad.
|Request to Request Bus||pid_ioctl($pid, "req reset");|