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


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;


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.


The following methods documented with a trailing call to ->get return Future instances.


   $spi->configure( %args )->get

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


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.


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


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:


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


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


The SPI mode number, 0 to 3.

The following non-boolean options exist:


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.


   $spi->chip_select( $cs )->get

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.


   $miso_bytes = $spi->writeread( $mosi_bytes )->get

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.


   $miso_bytes = $spi->writeread_cs( $mosi_bytes )->get

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.


Paul Evans <>