Permalink
Browse files

Merge pull request #2 from nofxx/master

Some fixes and a bin/pirate!
  • Loading branch information...
2 parents c176504 + bf66b32 commit fce9014b07a7045fa34913ca8ee591a4b6d4014d @profmaad committed Apr 13, 2012
Showing with 183 additions and 89 deletions.
  1. +41 −4 README.md
  2. +109 −0 bin/pirate
  3. +1 −0 buspirate.gemspec
  4. +32 −85 lib/buspirate.rb
View
@@ -1,8 +1,9 @@
-# ruby-buspirate - Ruby interface to Bus Pirate (via binary mode)
+# BusPirate - Ruby interface to Bus Pirate (via binary mode)
## Description
-Ruby class (and gem in the future) to access [The Bus Pirate](http://dangerousprototypes.com/docs/Bus_Pirate) in binary mode
+Ruby class (and gem in the future) to access [The Bus Pirate](http://dangerousprototypes.com/docs/Bus_Pirate) in binary mode.
+
## Features
@@ -11,12 +12,48 @@ Ruby class (and gem in the future) to access [The Bus Pirate](http://dangerouspr
* SPI mode
* all other modes are missing (but will be implemented soon'ish)
+Tested on Linux and OSX (should on windoze if seriaport does).
+
+
+## Use
+
+Create a BusPirate instance:
+
+ require 'buspirate'
+
+ pirate = BusPirate.new('/dev/port', bauds, bits, stopbit, parity)
+
+
+Linux example (115200b 8-N-1):
+
+ pirate = BusPirate.new('/dev/ttyACM0')
+
+
+Select Mode:
+
+ Mode::SPI
+ Mode::I2C
+ Mode::UART
+ Mode::ONEWIRE
+ Mode::RAWWIRE
+
+ pirate.switch_mode(BusPirate::Mode::XXX)
+
+
+Have fun!
+
+
+## Examples
+
+Check out the examples/ folder for some action.
+
+
## Dependencies
- * rubygems
- * ruby-serialport
+ * serialport
* a Bus Pirate ^^
+
## License
Copyright (c) 2011, *Prof. MAAD* aka Max Wolter
View
@@ -0,0 +1,109 @@
+#!/usr/bin/env ruby
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+require 'buspirate'
+
+
+DEFAULT_DEVICE = Dir.glob('/dev/ttyACM*').first
+
+
+def buspirate
+ @pirate ||= BusPirate.new(DEFAULT_DEVICE)
+end
+
+def warm_up
+ buspirate.reset_console
+ puts "Start on #{DEFAULT_DEVICE}"
+
+ print "entering bitbang mode..\t\t"
+ if buspirate.enter_bitbang
+ puts "done"
+ else
+ puts "failed"
+ exit
+ end
+end
+
+def volt_sniffer
+ warm_up
+ buspirate.read_adc_continuous
+end
+
+def uart_sniffer
+ warm_up
+
+ print "entering binary UART mode...\t"
+ if buspirate.switch_mode(BusPirate::Mode::UART)
+ puts "done"
+ else
+ puts "failed"
+ exit
+ end
+
+ print "setting baud rate...\t\t"
+ if buspirate.uart_set_baudrate(19600)
+ puts "done"
+ else
+ puts "failed"
+ exit
+ end
+
+ print "setting configuration...\t"
+ if buspirate.uart_set_config(BusPirate::UART::PIN_OUTPUT_HIZ, BusPirate::UART::FORMAT_8N, BusPirate::UART::STOPBITS_1, BusPirate::UART::IDLE_POLARITY_1)
+ puts "done"
+ else
+ puts "failed"
+ exit
+ end
+
+ print "configuring peripherals...\t"
+ if buspirate.config_peripherals(true, true, false, false)
+ puts "done"
+ else
+ puts "failed"
+ exit
+ end
+
+ print "starting RX echo...\t\t"
+ if buspirate.uart_rx_echo(true)
+ puts "done"
+ else
+ puts "failed"
+ end
+
+ while !buspirate.port.eof?
+ puts buspirate.port.readline
+ end
+
+ print "press enter to exit"
+ STDIN.getc
+
+rescue => e
+ puts "Error: #{e}"
+ensure
+ buspirate.close unless buspirate.nil?
+end
+
+
+if ARGV.empty?
+
+ puts <<TEXT
+
+ BusPirate!
+------------
+
+What should we do captain?"
+
+ uart - sniffs UART"
+ volt - monitor VOLTAGE probe"
+
+TEXT
+
+else
+ case comm = ARGV.join
+ when /uart/ then uart_sniffer
+ when /volt/ then volt_sniffer
+ else
+ puts "Dunno how to #{comm}"
+ end
+end
+
View
@@ -21,5 +21,6 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.add_dependency("serialport", ["~> 1.0.4"])
s.add_development_dependency("rspec", ["~> 2.8.0"])
end
View
@@ -10,7 +10,7 @@ class BusPirate
attr_reader :port
- def initialize(device, baudrate, databits, stopbits, parity)
+ def initialize(device, baudrate = 115200, databits = 8, stopbits = 1, parity = SerialPort::NONE)
@port = SerialPort.new(device, :baud => baudrate, :data_bits => databits, :stop_bits => stopbits, :parity => parity)
throw "failed to initialize Bus Pirate on port #{device}" if @port.nil?
end
@@ -22,14 +22,10 @@ def close
end
def reset_console
- (0..10).each do
- @port.putc 0x0d # send enter
- end
+ (0..10).each { @port.putc 0x0d } # send enter
@port.putc '#'
end
-
-
def exit_bitbang
@port.putc 0x0f
end
@@ -77,45 +73,30 @@ def enter_bitbang
return false
end
+ def do_switch_mode(bitvalue, ack)
+ @port.putc bitvalue
+ return check_for_ack(ack)
+ end
+
def switch_mode(mode)
case mode
- when Mode::RESET
- bitvalue = 0b00000000
- ack = "BBIO1"
- when Mode::SPI
- bitvalue = 0b00000001
- ack = "SPI1"
- when Mode::I2C
- bitvalue = 0b00000010
- ack = "I2C1"
- when Mode::UART
- bitvalue = 0b00000011
- ack = "ART1"
- when Mode::ONEWIRE
- bitvalue = 0b00000100
- ack = "1W01"
- when Mode::RAWWIRE
- bitvalue = 0b00000101
- ack = "RAW1"
+ when Mode::RESET then do_switch_mode(0b00000000, "BBIO1")
+ when Mode::SPI then do_switch_mode(0b00000001, "SPI1")
+ when Mode::I2C then do_switch_mode(0b00000010, "I2C1")
+ when Mode::UART then do_switch_mode(0b00000011, "ART1")
+ when Mode::ONEWIRE then do_switch_mode(0b00000100, "1W01")
+ when Mode::RAWWIRE then do_switch_mode(0b00000101, "RAW1")
else
return false
end
-
- @port.putc bitvalue
- return check_for_ack(ack)
end
def run_selftest(long)
- if long
- @port.putc 0b00010001
- else
- @port.putc 0b00010000
- end
+ @port.putc(long ? 0b00010001 : 0b00010000)
errors = @port.readbyte
@port.putc 0xFF # leave selftest mode
return -1 unless check_for_ack(0x01)
-
return errors
end
@@ -133,12 +114,7 @@ def calculate_pwm_bytes(prescaler, pwm_period, duty_cycle_in_percent)
bytes = Array.new
- bytes.push case prescaler
- when 1 then 0
- when 8 then 1
- when 64 then 2
- when 256 then 3
- end
+ bytes.push({1 => 0, 8 => 1, 64 => 2, 256 => 3}[prescaler])
bytes.push((ocr.to_i >> 8) & 0xFF)
bytes.push ocr.to_i & 0xFF
@@ -175,35 +151,27 @@ def read_adc
return get_adc_voltage
end
+
def read_adc_continuous
@port.putc 0b00010101
loop do
- yield get_adc_voltage
+ p get_adc_voltage
end
end
def configure_pins(aux, mosi, clk, miso, cs)
bitvalue = 0b01000000
- if aux == PinMode::INPUT
- bitvalue += 0b00010000
- end
- if mosi == PinMode::INPUT
- bitvalue += 0b00001000
- end
- if clk == PinMode::INPUT
- bitvalue += 0b00000100
- end
- if miso == PinMode::INPUT
- bitvalue += 0b00000010
- end
- if cs == PinMode::INPUT
- bitvalue += 0b00000001
- end
+ bitvalue += 0b00010000 if aux == PinMode::INPUT
+ bitvalue += 0b00001000 if mosi == PinMode::INPUT
+ bitvalue += 0b00000100 if clk == PinMode::INPUT
+ bitvalue += 0b00000010 if miso == PinMode::INPUT
+ bitvalue += 0b00000001 if cs == PinMode::INPUT
@port.putc bitvalue
return @port.readbyte
end
+
def set_pins(power, pullup, aux, mosi, clk, miso, cs)
bitvalue = 0b10000000
bitvalue += 0b01000000 if power
@@ -220,18 +188,10 @@ def set_pins(power, pullup, aux, mosi, clk, miso, cs)
def config_peripherals(power, pullups, aux, cs)
bitvalue = 0b01000000
- if power
- bitvalue += 0b00001000
- end
- if pullups
- bitvalue += 0b00000100
- end
- if aux
- bitvalue += 0b00000010
- end
- if cs
- bitvalue += 0b00000001
- end
+ bitvalue += 0b00001000 if power
+ bitvalue += 0b00000100 if pullups
+ bitvalue += 0b00000010 if aux
+ bitvalue += 0b00000001 if cs
@port.putc bitvalue
return check_for_ack(0x01)
@@ -253,6 +213,7 @@ def uart_set_baudrate(baudrate)
return true
end
+
def uart_set_config(pin_output, format, stopbits, idle_polarity)
bitvalue = 0b10000000
bitvalue += pin_output
@@ -265,11 +226,7 @@ def uart_set_config(pin_output, format, stopbits, idle_polarity)
end
def uart_rx_echo(echo)
- if echo
- @port.putc 0b00000010
- else
- @port.putc 0b00000011
- end
+ @port.putc(echo ? 0b00000010 : 0b00000011)
return check_for_ack(0x01)
end
@@ -311,11 +268,7 @@ def uart_activate_bridge_mode
end
def spi_set_cs(cs)
- if cs
- @port.putc 0b00000011
- else
- @port.putc 0b00000010
- end
+ @port.putc(cs ? 0b00000011 : 0b00000010)
return check_for_ack(0x01)
end
@@ -427,18 +380,12 @@ def spi_cs_block(cs_idle_high)
end
def spi_sniffer(sniff_all)
- if sniff_all
- command = 0b00001101
- else
- command = 0b00001110
- end
+ command = (sniff_all ? 0b00001101 : 0b00001110)
@port.putc command
return false unless check_for_ack(0x01)
- loop do
- yield @port.readbyte
- end
+ loop { yield @port.readbyte }
end
def split_int(value)

0 comments on commit fce9014

Please sign in to comment.