![]() ![]() ![]() ![]() |
|
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 )
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.
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.
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.
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.
Returns an array containing the names of available devices. Will normally return ('/dev/i2c-1', '/dev/i2c-0')
$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 );
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 );
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 );
writes $value to the device
returns the first byte read from the device
writes byte $value to the device
returns byte from the device specifying register address in $command;
writes byte $value to the register address specified in $command
reads a word beginning at the device register specified in $command;
writes a word to the device register specified in $command;
reads a word beginning at the device register specified in $command and swaps the high /low bytes in the return value
writes a word to the device register specified in $command after swapping the high / low bytes in $word
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.
returns an array of bytes $numbytes in size reading from register specified in $command;
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.
Writes all the bytes from array reference $arrayref to device specifying the register in $command.