Class: Bucaneer::BusPirate
- Inherits:
-
Object
- Object
- Bucaneer::BusPirate
- 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
-
#protocol ⇒ Object
readonly
Returns the value of attribute protocol.
-
#serial_port ⇒ Object
readonly
Returns the value of attribute serial_port.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
- #enter_bitbang_mode ⇒ Object
-
#initialize(serial_port, mode, options) ⇒ BusPirate
constructor
A new instance of BusPirate.
- #reset ⇒ Object
- #set_peripherals(options) ⇒ Object
- #tx(byte, expected = SUCCESS) ⇒ Object
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, ) @serial_port = serial_port set_mode(mode.to_sym, ) end |
Instance Attribute Details
#protocol ⇒ Object (readonly)
Returns the value of attribute protocol.
22 23 24 |
# File 'lib/bucaneer/bus_pirate.rb', line 22 def protocol @protocol end |
#serial_port ⇒ Object (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( = {}) dev = .delete(:dev) mode = .delete(:mode) baud = .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, ) 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
#close ⇒ Object
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_mode ⇒ Object
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 |
#reset ⇒ Object
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() mask = SET_PERIPHERALS mask |= POWER_ON if [:power] mask |= PULLUPS_ON if [:pullups] mask |= AUX_ON if [:aux] mask |= CS_ON if [:cs] tx(mask) end |