Setting and Using I2C


To use or set I2C, pid_ioctl function is required. Available commands are as follows:

Command Sub Command Description
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

Setting I2C

You can set a communication mode and a device address by using a "set" command.

Set Data Rate

PHPoC provides standard mode and fast mode. The standard mode is the default value.

Communication Mode Syntax
standard pid_ioctl($pid, "set mode sm");
fast pid_ioctl($pid, "set mode fm");

Set Device Address

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:

Type Syntax
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.

Addresses (Binary) Example(Hexa) Comment
7 6 5 4 3 2 1 0
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

Getting I2C Status

You can get status of I2C by using "get" command.

Get the Number of Pending Bytes in Send and Receive Buffer

How to get the number of pending bytes in send and receive buffer is as follows:

Division Syntax
Send buffer pid_ioctl($pid, "get txlen");
Receive buffer pid_ioctl($pid, "get rxlen");

Get the Remaining Size of Send Buffer

How to get the remaining size of send buffer in byte-unit is as follows:

Division Syntax
remaining size of send buffer pid_ioctl($pid, "get txfree");

Get Amount of Errors

You can check the amount of bus errors and NACK in the last transaction.

Division Syntax
NACK pid_ioctl($pid, "get error nack");
bus Error pid_ioctl($pid, "get error bus");

Get Status

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.

Division Syntax
State pid_ioctl($pid, "get state");

Using I2C

Request to Read Data

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.

Division Syntax
Request to Read Data pid_ioctl($pid, "req read [N]");

Request to Write Data

An I2C master requests to write data to an I2C slave with this command. There are two ways and those are as follows:

Division Syntax
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.

Request to Reset Bus

You can reset an I2C bus with this command when communication is bad.

Command Syntax
Request to Request Bus pid_ioctl($pid, "req reset");