Device::BusPirate::Mode::I2C - use Device::BusPirate in I2C mode
Device::BusPirate::Mode::I2C
Device::BusPirate
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; }
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.
I2C
The following methods documented with await expressions Future instances.
await
await $i2c->configure( %args );
Change configuration options. The following options exist:
A string giving the clock speed to use for I2C. Must be one of the values:
5k 50k 100k 400k
await $i2c->start_bit;
Sends an I2C START bit transition
await $i2c->stop_bit;
Sends an I2C STOP bit transition
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.
send
recv
$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.
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.
start_bit
write
stop_bit
enter_mutex
$address should be an integer, in the range 0 to 0x7f.
$address
$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.
read
$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.
Paul Evans <leonerd@leonerd.org.uk>
To install Device::BusPirate, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Device::BusPirate
CPAN shell
perl -MCPAN -e shell install Device::BusPirate
For more information on module installation, please visit the detailed CPAN module installation guide.