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 a trailing call to ->get return Future instances.

$i2c->start_bit->get

Sends an I2C START bit transition

$i2c->stop_bit->get

Sends an I2C STOP bit transition

$i2c->write( $bytes )->get

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.

$bytes = $i2c->read( $length )->get

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.

$i2c->send( $address, $bytes )->get

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.

$bytes = $i2c->recv( $address, $length )->get

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.

$bytes_in = $ic->send_then_recv( $address, $bytes_out, $read_len )->get

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>