SMBus
NAME
RPi::Device::SMBus - SMBus/i²c interface for Raspberry Pi
SYNOPSIS
use RPi::Device::SMBus;
# Obviously you will need to actually read the data sheet of your device.
my RPi::Device::SMBus $smbus = RPi::Device::SMBus.new(device => '/dev/i2c-1', address => 0x54);
$smbus.write-byte(0x10);
....
DESCRIPTION
This is an SMBus/i²c interface that has been written and tested for the Raspberry Pi, however it uses a fairly generic POSIX interface so if your platform exposes the i²c interface as a character special device it may work.
In order to use this you will need to install and configure the i2c-dev kernel module and tools. On a default Debian image you should be able to just do:
sudo apt-get install libi2c-dev i2c-tools
And then edit the /etc/modules
to add the modules by adding:
i2c-dev
i2c-bcm2708
And then rebooting.
Typicaly the i2c device will be /dev/i2c-1
on a Raspberry Pi rev B. or v2 or /dev/i2c-0
on older versions.
You can determine the bus address of your device by doing:
sudo i2cdetect -y 1 # replace the 1 with a 0 for older versions
(Obviously the device should be connected, consult the manual for your device about this.)
Which should give you the hexadecimal address of your device. Some
devices may not respond, so you may want to either check the data sheet
of your device or read the i2cdetect
manual page to get other options.
METHODS
Not all devices will necessarily support all of the methods, here.
You should consult the data sheet for your device to determine which
commands (passed as the Command $command
- an unsigned 8 bit integer)
should be used as they will differ for all devices.
method new
method new(:$device!, :$address!) returns RPi::Device::SMBus
This is the constructor of the class. The named parameters are both
required: device
is an the full path (e.g /dev/i2c-1
) of the
character special device that represents your i2c bus, and address
should be the 7 bit numeric address (i.e. an integer less that 128)
of the device on the bus.
An exception may be thrown if the device cannot be opened, or it does not support the required ioctls that an i2c device should.
method write-quick
method write-quick(Byte $value) returns Int
This sends a single bit to the device, at the place of the Read/Write bit returning an integer to indicate the result
method read-byte
method read-byte() returns Int
This reads a single byte from the device, without the need to specify
a register Command
. This may only be implemented by the simplest
devices, but it can be used as a short hand for read-byte-data
but
re-using the previous Command.
method write-byte
method write-byte(Byte $value) returns Int
This is the reverse of read-byte, it will send the single byte to the device.
method read-byte-data
method read-byte-data(Command $command) returns Int
This reads a single byte from the device register specified by $command
.
method write-byte-data
method write-byte-data(Command $command, Byte $value) returns Int
This writes the single byte $value
to the device register $command
,
returning an int which will be -1 on failure.
read-word-data
method read-word-data(Command $command) returns Int
This reads the 16 bit word from the register specified by<$command>.
method write-word-data
method write-word-data(Command $command, Word $value) returns Int
This writes the 16 bit word $value
to the register specified by $command
returning an int (which will be -1 on failure.)
method process-call
method process-call(Command $command, Word $value) returns Int
This writes the 16 bit word $value
to the command register $command
and then reads it back (presumably the device will have changed it.)
method read-block-data
method read-block-data(Command $command) returns Block
This reads the command register $value
and returns the Block
(an array containing up to 32 unsigned 8 bit integers.)
method write-block-data
method write-block-data(Command $command, Block $block) returns Int
This writes the Block
$block
(An array of unsigned bytes,) the
the command register $command
and returns an integer to indicate the
success of the call.
method read-i2c-block-data
method read-i2c-block-data(Command $command, Int $length) returns Block
This is similar to read-block-data
but the $length
of the number of
bytes to be read can be provided, some systems may not support anything
other than 32.
method write-i2c-block-data
method write-i2c-block-data(Command $command, Block $block ) returns Int
This is similiar to write-block-data
to the extent that the Raku interface is identical.
method block-process-call
method block-process-call(Command $command, Block $block) returns Block
This sends the specified Block
(Array of unsigned bytes) to the
Command register $command
and reads up to 32 bytes back which are
returned as a Block