Complete Communications Engineering

When developing software for embedded devices that use I2C, it is often useful to try out different register values on peripheral devices to find out what combinations work.  The Linux kernel manages the I2C bus.  Each bus is available to user-space applications through a /dev node.  The Linux ‘I2C-Tools’ package contains some useful programs that can be installed on a target device to provide command line access to the I2C busses.  In many cases, this can be an easier way to get started than writing custom software.  The method for installing I2C-Tools depends on the Linux distribution being used.  Once installed, the I2C-Tools commands should be available on the command prompt.  Some of the useful commands are: i2cdetect, i2cget and i2cset.

The i2cdetect command can be used to detect available I2C busses, and search for chips on those busses.  Using the -l option will display some information about each available I2C bus, including the /dev node name (listed first).  If the first argument is a bus index, the command will attempt to probe that bus for chips.  It then prints a table of results.  In the table, it will print ‘–’ for any address that was probed but no chip was found.  If a chip was found, it will either print the address, or ‘UU’ if the chip is in use by a Linux device driver.  Example:

> i2cdetect -l

i2c-1   i2c             Generic I2C adapter                     I2C adapter

i2c-0   i2c             Generic I2C adapter                     I2C adapter

> 

> i2cdetect 1

Warning: Can’t use SMBus Quick Write command, will skip some addresses

WARNING! This program can confuse your I2C bus, cause data loss and worse!

I will probe file /dev/i2c-1.

I will probe address range 0x08-0x77.

Continue? [Y/n] Y

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f

00:                         — — — — — — — —

10: — — — — — — — — UU UU UU UU UU UU UU UU

20: — — — — — — — — — — — — — — — —

30: — — — — — — — — — — — — — — — —

40: UU UU UU UU — — — — — — — — — — — —

50: — — — — — — — — — — — — — — — —

60: — — — — — — — — — — — — — — — —

70: — — — — — — — —

>

The i2cget command can read register values from I2C peripheral devices, and the i2cset command can write register values to I2C peripheral devices.  These operations are usually prevented if a Linux driver is using the chip, but the command can be sent anyways by using the -f option.  Both commands take an I2C bus index, a chip address and a register index.  The set command also takes a value to set, and the get command returns (prints) the value that was read.  Example:

> i2cset -y -f 1 0x58 2 0xbb

> i2cget -y -f 1 0x58 2

0xbb

> i2cset -y -f 1 0x58 2 0xcc

> i2cget -y -f 1 0x58 2

0xcc

>