Class: Bucaneer::BusPirate

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

Constant Summary collapse

DEFAULT_BAUD =
115200
TIMEOUT =
0.0005
MAX_TRIES =
40
BITBANG_MODE =
0x00
RESET =
0x0f
SET_PERIPHERALS =
0x40
POWER_ON =
0x08
PULLUPS_ON =
0x04
AUX_ON =
0x02
CS_ON =
0x01
FAILURE =
0x00
SUCCESS =
0x01

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(serial_port, mode, options) ⇒ BusPirate

Returns a new instance of BusPirate.



50
51
52
53
# File 'lib/bucaneer/bus_pirate.rb', line 50

def initialize(serial_port, mode, options)
  @serial_port = serial_port
  set_mode(mode.to_sym, options)
end

Instance Attribute Details

#protocolObject (readonly)

Returns the value of attribute protocol.



22
23
24
# File 'lib/bucaneer/bus_pirate.rb', line 22

def protocol
  @protocol
end

#serial_portObject (readonly)

Returns the value of attribute serial_port.



22
23
24
# File 'lib/bucaneer/bus_pirate.rb', line 22

def serial_port
  @serial_port
end

Class Method Details

.connect(options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bucaneer/bus_pirate.rb', line 24

def self.connect(options = {})
  dev  = options.delete(:dev)
  mode = options.delete(:mode)
  baud = options.delete(:baud) || DEFAULT_BAUD

  raise "no device specified" unless dev
  raise "no mode specified"   unless mode

  serial_port = SerialPort.new(dev, baud)
  bus_pirate  = Bucaneer::BusPirate.new(serial_port, mode, options)

  if block_given?
    begin
      yield bus_pirate.protocol
    ensure
      begin
        bus_pirate.close
      rescue
        # Do nothing.
      end
    end
  end

  bus_pirate
end

Instance Method Details

#closeObject



55
56
57
58
59
# File 'lib/bucaneer/bus_pirate.rb', line 55

def close
  enter_bitbang_mode
  reset
  @serial_port.close
end

#enter_bitbang_modeObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/bucaneer/bus_pirate.rb', line 69

def enter_bitbang_mode
  tries = 0
  begin
    raise "failed to enter bitbang mode" if tries >= MAX_TRIES
    @serial_port.puts BITBANG_MODE.chr
    sleep TIMEOUT
    response = @serial_port.read(5)
    tries += 1
  end until response == "BBIO1"
end

#resetObject



80
81
82
# File 'lib/bucaneer/bus_pirate.rb', line 80

def reset
  tx(RESET)
end

#set_peripherals(options) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/bucaneer/bus_pirate.rb', line 84

def set_peripherals(options)
  mask = SET_PERIPHERALS

  mask |= POWER_ON   if options[:power]
  mask |= PULLUPS_ON if options[:pullups]
  mask |= AUX_ON     if options[:aux]
  mask |= CS_ON      if options[:cs]

  tx(mask)
end

#tx(byte, expected = SUCCESS) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/bucaneer/bus_pirate.rb', line 61

def tx(byte, expected = SUCCESS)
  @serial_port.putc byte
  sleep TIMEOUT
  response = @serial_port.getc
  raise "failed to send data" unless response == expected
  response
end