The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Device::BusPirate::Mode::SPI - use Device::BusPirate in SPI mode

SYNOPSIS

Simple output (e.g. driving LEDs on a shift register)

   use Device::BusPirate;

   my $pirate = Device::BusPirate->new;
   my $spi = $pirate->enter_mode( "SPI" )->get;

   $spi->configure( open_drain => 0 )->get;

   my $count = 0;
   while(1) {
      $spi->writeread_cs( chr $count )->get;
      $count++; $count %= 255;
   }

Simple input (e.g. reading buttons on a shift register)

   while(1) {
      my $in = ord $spi->writeread_cs( "\x00" )->get;
      printf "Read %02x\n", $in;
   }

DESCRIPTION

This object is returned by a Device::BusPirate instance when switching it into SPI mode. It provides methods to configure the hardware, and interact with an SPI-attached chip.

METHODS

The following methods documented with await expressions Future instances.

configure

   await $spi->configure( %args );

Change configuration options. The following options exist; all of which are simple true/false booleans.

open_drain

If enabled (default), a "high" output pin will be set as an input; i.e. hi-Z. When disabled, a "high" output pin will be driven by 3.3V. A "low" output will be driven to GND in either case.

sample

Whether to sample input in the middle of the clock phase or at the end.

cs_high

Whether "active" Chip Select should be at high level. Defaults false to be active-low. This only affects the writeread_cs method; not the chip_select method.

The SPI clock parameters can be specified in any of three forms:

ckp
cke

The SPI Clock Polarity and Clock Edge settings, in PIC style.

cpol
cpha

The SPI Clock Polarity and Clock Phase settings, in AVR style.

mode

The SPI mode number, 0 to 3.

The following non-boolean options exist:

speed

A string giving the clock speed to use for SPI. Must be one of the values:

   30k 125k 250k 1M 2M 2.6M 4M 8M

By default the speed is 30kHz.

chip_select

   await $spi->chip_select( $cs );

Set the CS output pin level. A false value will pull it to ground. A true value will either pull it up to 3.3V or will leave it in a hi-Z state, depending on the setting of the open_drain configuration.

writeread

   $miso_bytes = await $spi->writeread( $mosi_bytes );

Performs an actual SPI data transfer. Writes bytes of data from $mosi_bytes out of the MOSI pin, while capturing bytes of input from the MISO pin, which will be returned as $miso_bytes when the Future completes. This method does not toggle the CS pin, so is safe to call multiple times to effect a larger transaction.

This is performed atomically using the enter_mutex method.

writeread_cs

   $miso_bytes = await $spi->writeread_cs( $mosi_bytes );

A convenience wrapper around writeread which toggles the CS pin before and afterwards. It uses the cs_high configuration setting to determine the active sense of the chip select pin.

This is performed atomically using the enter_mutex method.

AUTHOR

Paul Evans <leonerd@leonerd.org.uk>