## HiPi::Device::I2C

The HiPi::Device::I2C module provides access to the kernel driver for the I2C bus.

See : I2C Device Driver

The settings for the I2C device can be controlled in the HiPi Control GUI.

Currently the module only implements the SMBus protocol as this appears to be the most widely supported protocol used on the i2c bus. It is likely that your i2c devices support it.

The following interface modules use HiPi::Device::I2C as a backend and may contain code that helps with your own usage.

HiPi::Interface::HTADCI2C
HiPi::Interface::MCP23017
HiPi::Interface::HTBackpackV2 ( as an optional backend )

## Class Methods

##### HiPi::Device::I2C->load_modules( $forceunload );

        Loads the kernel device driver modules for i2c
        i2c_bcm2708
        i2c_dev

        When $forceunload is true, unloads the kernel modules
        first if they are loaded (effectively forces a reload)

        i2c_bcm2708 is loaded with the current baudrate

        Of course, the user the script is running as must have
        root permissions to load and unload kernel modules.

##### HiPi::Device::I2C->unload_modules();

        Unloads the kernel device driver modules for i2c
        i2c_bcm2708
        i2c_dev

        Of course, the user the script is running as must have
        root permissions to load and unload kernel modules.

##### HiPi::Device::I2C->get_baudrate();

        Returns the current baudrate of a loaded i2c_bcm2708
        module. If the module is not loaded or the process
        does not have root permissions, returns the current
        default module setting for baudrate.

##### HiPi::Device::I2C->set_baudrate( $baudrate );

        Sets the baudrate of the i2c_bcm2708 module. Must
        unload and load the kernel module to do this.

        Of course, the user the script is running as must have
        root permissions to load and unload kernel modules.

##### HiPi::Device::I2C->get_device_list();

        Returns an array containing the names of available
        devices.
        Will normally return ('/dev/i2c-1', '/dev/i2c-0')

## Object Constructor and Methods

##### my $dev = HiPi::Device::I2C->new( address => $devaddress );

        $devaddress    address of the device ( e.g. 0x20 ) on the
                       default bus. The default bus is determined
                       according to your Pi board revision.
                       revision 1 = /dev/i2c-0
                       revision 2 = /dev/i2c-1

        returns a new instance of the HiPi::Device::I2C class.

        You can specify which i2c device to use in the constructor if
        you wish using the key 'devicename'.

        my $dev = HiPi::Device::I2C->new(
            devicename => '/dev/i2c-1',
            address    => 0x28
        );

### SMBus Methods

##### $dev->smbus_write( @params );

        The module provides the method smbus_write as
        a generic call to the main smbus methods. I have
        found it is all I need for all i2c devices
        tested so far.

        If @params is a single value, the method calls

        $dev->smbus_write_byte($params[0]);

        If @params contains two values, the method calls

        $dev->smbus_write_byte_data( @params );

        With three or more values in @params, the method does

        my $command = shift @params;
        $dev->smbus_write_i2c_block_data($command, @bytes );

##### $dev->smbus_read( $cmdval, $numbytes );

        The module provides the method smbus_read as
        a generic call to the main smbus methods. I have
        found it is all I need for all i2c devices
        tested so far.

        If $cmdval is not defined, the method returns a
        scalar value from

        $dev->smbus_read_byte;

        If $cmdval is defined but $numbytes is undefined
        or zero, then the method returns a scalar value
        from

        $dev->smbus_read_byte_data( $cmdval );

        If $cmdval is defined and $numbytes is greater
        than zero then the method returns an array of
        values from

        $dev->smbus_read_i2c_block_data( $cmdval, $numbytes );

##### $dev->smbus_write_quick($value);

        writes $value to the device

##### $dev->smbus_read_byte();

        returns the first byte read from the device

##### $dev->smbus_write_byte($value);

        writes byte $value to the device

##### $dev->smbus_read_byte_data( $command );

        returns byte from the device specifying
        register address in $command;

##### $dev->smbus_write_byte_data( $command, $value );

        writes byte $value to the register address specified
        in $command

##### $dev->smbus_read_word_data($command);

        reads a word beginning at the device register
        specified in $command;

##### $dev->smbus_write_word_data($command, $word);

        writes a word to the device register
        specified in $command;

##### $dev->smbus_read_word_swapped($command);

        reads a word beginning at the device register
        specified in $command and swaps the high /low
        bytes in the return value

##### $dev->smbus_write_word_swapped($command, $word);

        writes a word to the device register
        specified in $command after swapping the
        high / low bytes in $word

##### $dev->smbus_read_block_data( $command );

        Returns an array of values read starting at register
        specified in $command.
        In all devices tested so far I have found it necessary
        to use $dev->smbus_read_i2c_block_data; instead.

##### $dev->smbus_read_i2c_block_data( $command, $numbytes );

        returns an array of bytes $numbytes in size reading from
        register specified in $command;

##### $dev->smbus_write_block_data( $command, $arrayref );

        Writes all the bytes from array reference $arrayref to
        device specifying the register in $command.
        In all devices tested so far I have found it necessary
        to use $dev->smbus_write_i2c_block_data instead.

##### $dev->smbus_write_i2c_block_data( $command, $arrayref );

        Writes all the bytes from array reference $arrayref to
        device specifying the register in $command.

HiPi Modules Copyright © 2013 Mark Dootson