Class: Bucaneer::Protocol::SPI

Inherits:
Object
  • Object
show all
Defined in:
lib/bucaneer/protocol/spi.rb

Overview

SPI bitbang on the BusPirate:

http://dangerousprototypes.com/docs/SPI_(binary)

SPI protocol:

http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus

Constant Summary collapse

SPI_MODE =
0x01
SET_CS =
0x02
SET_SPEED =
0x61
BULK_WRITE =
0x10
ENABLE =
0x01
CHUNK_SIZE =
16

Instance Method Summary collapse

Constructor Details

#initialize(controller, options = {}) ⇒ SPI

Returns a new instance of SPI.



18
19
20
21
22
23
# File 'lib/bucaneer/protocol/spi.rb', line 18

def initialize(controller, options = {})
  @controller = controller
  enter_spi_mode
  @controller.tx(SET_SPEED)
  @controller.tx(0x82)
end

Instance Method Details

#tx(bytes) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bucaneer/protocol/spi.rb', line 25

def tx(bytes)
  set_cs(false)

  sleep 0.0005

  bytes.to_enum.each_slice(CHUNK_SIZE) do |chunk|
    start_bulk_write(chunk.length)
    chunk.each do |byte|
      @controller.serial_port.putc byte
    end
    @controller.serial_port.read(chunk.length)
  end

  sleep 0.0005

  set_cs(true)
end