Contents Up Previous Next

HiPi I2C Utility

The HiPi::BCM2835::I2C module provides access to i2c devices including those that require repeated start signals before a read can take place. It also allows you to enable or disable individual i2c buses so that for Model B Rev 2 and Model A boards you can enable a second i2c bus on the P5 header. ( see :Second I2C Bus).

To allow access to this facility from outside your Perl scripts HiPi modules installs a utility application hipi-i2c ( /usr/local/bin/hipi-i2c ).

The utility is installed with setuid permission so allways runs as root allowing the necessary access to the i2c hardware peripherals on the Pi.

The calling process user must be a member of the group i2c to execute the utility.

You can manage group membership with the HiPi Control Gui.


Command Line Help

    MODE     = w[rite] | r[read] | h[elp] | b[aud] | e[nable]
    I2C      = 0 | 1   The i2c perphipheral to use
    ADDRESS  = The device address on the i2c bus when mode r|w
    REGISTER = The register on the device you wish
               to write to / read from when mode r|w
    BAUDRATE = Baudrate for I2C ( only root may change baudrate);
    ARG1,2,n = additional arguments
We have a device at address 0x3A on the I2C-1 peripheral. (default for 
Model B Revision 2.0 and Model A boards.)
write one byte (0xFF) to register 0x02

        hipi-i2c w 1 0x3A 0x02 0xFF
write just the register address (0x02) to the device

        hipi-i2c w 1 0x3A 0x02
write 3 bytes (0xFF, 0xFE, 0x11) to register 0x02

        hipi-i2c w 1 0x3A 0x02 0xFF 0xFE 0x11
read 1 byte starting at register 0x09

        hipi-i2c r 1 0x3A 0x09 1
read 24 bytes starting at register 0x09

        hipi-i2c r 1 0x3A 0x09 24
set baudrate to 400000 on I2C peripheral 1

        sudo hipi-i2c b 1 400000

get current baudrate on I2C peripheral 1

        hipi-i2c b 1

enable i2c bus 1
        hipi-i2c e 1 1
enable i2c bus 0
        hipi-i2c e 0 1
disable i2c bus 0
        hipi-i2c e 0 0

Calls to read return a list of space separated byte values on

Calls to write return the number of bytes written on stdout. 
The register address passed to the write command counts as 
one byte (it is possible to write only the register address)
so the command 

    hipi-i2c w 1 0x3A 0x02 0xFF

will return '2' on success.

Calls to baud, whether set or get, always return the baudrate 
from the hardware peripheral on stdout. Because the baudrate 
is converted to a divider for the system clock on set, and then
converted back to a baudrate on get, the number you get back 
will not always be exactly the number that you set.

Allowed baudrate settings are in the range 500 to 1600000 ( near
the maximum the Broadcom peripheral supports). You may see 
problems at speeds over 1MHz ( 1000000 ) even if your device
claims to support it.

Common values for baudrate are:

 100000  - the i2c standard
 400000  - fast i2c standard
1000000  - seems to be the fastest reliable speed that this
           implementation supports. Of course, all the 
           devices on your bus must support this too.
  32000  - the Broadcom I2C peripherals don't support
           clock stretching so if you have a device that
           employs this, a baudrate of 32000 ( or perhaps
           even lower ) will often allow the device to work. 

Calls to enable allow you to disable and enable the available 
I2C buses. Parameters are
    e(command) bus(0 or 1) enable(0 or 1)
So to disable bus 1

    hipi-i2c e 1 0

to enable bus 0

    hipi-i2c e 0 1

See also : Second I2C Bus

On error, the utility will exit with a none zero value.

Contents Up Previous Next

HiPi Modules Copyright © 2013 Mark Dootson