NAME
Device::BusPirate::Mode::I2C
- use Device::BusPirate
in I2C mode
SYNOPSIS
use Device::BusPirate;
my $pirate = Device::BusPirate->new;
my $i2c = $pirate->enter_mode( "I2C" )->get;
my $addr = 0x20;
my $count = 0;
while(1) {
$i2c->send( $addr, chr $count )->get;
my $in = ord $i2c->recv( $addr, 1 )->get;
printf "Read %02x\n", $in;
$count++; $count %= 255;
}
DESCRIPTION
This object is returned by a Device::BusPirate instance when switching it into I2C
mode. It provides methods to configure the hardware, and interact with one or more I2C-attached chips.
METHODS
The following methods documented with await
expressions Future instances.
configure
await $i2c->configure( %args );
Change configuration options. The following options exist:
- speed
-
A string giving the clock speed to use for I2C. Must be one of the values:
5k 50k 100k 400k
start_bit
await $i2c->start_bit;
Sends an I2C START bit transition
stop_bit
await $i2c->stop_bit;
Sends an I2C STOP bit transition
write
await $i2c->write( $bytes );
Sends the given bytes over the I2C wire. This method does not send a preceding start or a following stop; you must do that yourself, or see the send
and recv
methods.
read
$bytes = await $i2c->read( $length );
Receives the given number of bytes over the I2C wire, sending an ACK bit after each one but the final, to which is sent a NACK.
send
await $i2c->send( $address, $bytes );
A convenient wrapper around start_bit
, write
and stop_bit
. This method sends a START bit, then an initial byte to address the slave in WRITE mode, then the remaining bytes, followed finally by a STOP bit. This is performed atomically by using the enter_mutex
method.
$address
should be an integer, in the range 0 to 0x7f.
recv
$bytes = await $i2c->recv( $address, $length );
A convenient wrapper around start_bit
, write
, read
and stop_bit
. This method sends a START bit, then an initial byte to address the slave in READ mode, then reads the given number of bytes, followed finally by a STOP bit. This is performed atomically by using the enter_mutex
method.
$address
should be an integer, in the range 0 to 0x7f.
send_then_recv
$bytes_in = await $ic->send_then_recv( $address, $bytes_out, $read_len );
A convenient wrapper around start_bit
, write
, read
and stop_bit
. This method combines a send
and recv
operation, with a repeated START condition inbetween (not a STOP). It is useful when reading values from I2C slaves that implement numbered registers; sending the register number as a write, before requesting the read.
$address
should be an integer, in the range 0 to 0x7f.
AUTHOR
Paul Evans <leonerd@leonerd.org.uk>