bucaneer 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Josh Bassett
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # Bucaneer
2
+
3
+ ## Introduction
4
+
5
+ Bucaneer is a Ruby library which allows you to control your BusPirate bitbang modes (I2C, SPI, etc).
@@ -0,0 +1,14 @@
1
+ rc = "#{ENV['HOME']}/.rubyrc"
2
+ load(rc) if File.exist?(rc)
3
+
4
+ require 'rubygems'
5
+ require 'bundler'
6
+
7
+ envs = [:default]
8
+ envs << ENV["BUCANEER_ENV"].downcase.to_sym if ENV["BUCANEER_ENV"]
9
+ Bundler.setup(*envs)
10
+
11
+ path = File.join(File.expand_path(File.dirname(__FILE__)), '..')
12
+ $LOAD_PATH.unshift(path)
13
+
14
+ require 'bucaneer'
@@ -0,0 +1,115 @@
1
+ require 'serialport'
2
+
3
+ module Bucaneer
4
+ class BusPirate
5
+ DEFAULT_BAUD = 115200
6
+
7
+ TIMEOUT = 0.0005
8
+ MAX_TRIES = 40
9
+
10
+ BITBANG_MODE = 0x00
11
+ RESET = 0x0f
12
+
13
+ SET_PERIPHERALS = 0x40
14
+ POWER_ON = 0x08
15
+ PULLUPS_ON = 0x04
16
+ AUX_ON = 0x02
17
+ CS_ON = 0x01
18
+
19
+ FAILURE = 0x00
20
+ SUCCESS = 0x01
21
+
22
+ attr_reader :serial_port, :protocol
23
+
24
+ def self.connect(options = {})
25
+ dev = options.delete(:dev)
26
+ mode = options.delete(:mode)
27
+ baud = options.delete(:baud) || DEFAULT_BAUD
28
+
29
+ raise "no device specified" unless dev
30
+ raise "no mode specified" unless mode
31
+
32
+ serial_port = SerialPort.new(dev, baud)
33
+
34
+ begin
35
+ bus_pirate = Bucaneer::BusPirate.new(serial_port, mode, options)
36
+ yield bus_pirate.protocol if block_given?
37
+ ensure
38
+ begin
39
+ if bus_pirate
40
+ bus_pirate.exit_bitbang_mode
41
+ bus_pirate.reset
42
+ end
43
+
44
+ serial_port.close
45
+ rescue
46
+ # Do nothing.
47
+ end
48
+ end
49
+ end
50
+
51
+ def initialize(serial_port, mode, options)
52
+ @serial_port = serial_port
53
+ set_mode(mode.to_sym, options)
54
+ end
55
+
56
+ def tx(byte, expected = SUCCESS)
57
+ @serial_port.putc byte
58
+ sleep TIMEOUT
59
+ response = @serial_port.getc
60
+ raise "failed to send data" unless response == expected
61
+ response
62
+ end
63
+
64
+ def enter_bitbang_mode
65
+ tries = 0
66
+ begin
67
+ raise "failed to enter bitbang mode" if tries >= MAX_TRIES
68
+ @serial_port.puts BITBANG_MODE.chr
69
+ sleep TIMEOUT
70
+ response = @serial_port.read(5)
71
+ tries += 1
72
+ end until response == "BBIO1"
73
+ end
74
+
75
+ def exit_bitbang_mode
76
+ tx(BITBANG_MODE)
77
+ end
78
+
79
+ def reset
80
+ tx(RESET)
81
+ end
82
+
83
+ def set_peripherals(options)
84
+ mask = SET_PERIPHERALS
85
+
86
+ mask |= POWER_ON if options[:power]
87
+ mask |= PULLUPS_ON if options[:pullups]
88
+ mask |= AUX_ON if options[:aux]
89
+ mask |= CS_ON if options[:cs]
90
+
91
+ tx(mask)
92
+ end
93
+
94
+ private
95
+ # Set the BusPirate to the given mode.
96
+ def set_mode(mode, options)
97
+ enter_bitbang_mode
98
+
99
+ @protocol =
100
+ case mode
101
+ when :i2c
102
+ Bucaneer::Protocol::I2C.new(self, options)
103
+ when :spi
104
+ Bucaneer::Protocol::SPI.new(self, options)
105
+ else
106
+ raise "unknown mode '#{mode}'"
107
+ end
108
+
109
+ set_peripherals(options)
110
+
111
+ # Allow things to settle down.
112
+ sleep 1
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,55 @@
1
+ module Bucaneer::Protocol
2
+ # I2C bitbang on the BusPirate:
3
+ # http://dangerousprototypes.com/docs/I2C_(binary)
4
+ #
5
+ # I2C protocol:
6
+ # http://en.wikipedia.org/wiki/I²C
7
+ class I2C
8
+ I2C_MODE = 0x02
9
+
10
+ SET_SPEED = 0x62
11
+ START = 0x02
12
+ STOP = 0x03
13
+ BULK_WRITE = 0x10
14
+
15
+ I2C_WRITE_BIT = 0x00
16
+ I2C_READ_BIT = 0x01
17
+
18
+ ACK = 0x00
19
+ NACK = 0x01
20
+
21
+ CHUNK_SIZE = 16
22
+
23
+ def initialize(controller, options = {})
24
+ @controller = controller
25
+ enter_i2c_mode
26
+ @controller.tx(SET_SPEED)
27
+ end
28
+
29
+ def tx(address, *bytes)
30
+ bytes.flatten!
31
+
32
+ @controller.tx(START)
33
+
34
+ bytes.to_enum.each_slice(CHUNK_SIZE) do |chunk|
35
+ start_bulk_write(chunk.length + 1)
36
+ @controller.tx((address << 1) | I2C_WRITE_BIT, ACK)
37
+ chunk.each {|byte| @controller.tx(byte, ACK) }
38
+ end
39
+
40
+ @controller.tx(STOP)
41
+ end
42
+
43
+ private
44
+ def start_bulk_write(length)
45
+ @controller.tx(BULK_WRITE | (length - 1))
46
+ end
47
+
48
+ def enter_i2c_mode
49
+ @controller.serial_port.puts I2C_MODE.chr
50
+ sleep Bucaneer::BusPirate::TIMEOUT
51
+ response = @controller.serial_port.read(4)
52
+ raise "failed to enter I2C mode" unless response == "I2C1"
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,61 @@
1
+ module Bucaneer::Protocol
2
+ # SPI bitbang on the BusPirate:
3
+ # http://dangerousprototypes.com/docs/SPI_(binary)
4
+ #
5
+ # SPI protocol:
6
+ # http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus
7
+ class SPI
8
+ SPI_MODE = 0x01
9
+
10
+ SET_CS = 0x02
11
+ SET_SPEED = 0x61
12
+ BULK_WRITE = 0x10
13
+
14
+ ENABLE = 0x01
15
+
16
+ CHUNK_SIZE = 16
17
+
18
+ def initialize(controller, options = {})
19
+ @controller = controller
20
+ enter_spi_mode
21
+ @controller.tx(SET_SPEED)
22
+ @controller.tx(0x82)
23
+ end
24
+
25
+ def tx(bytes)
26
+ set_cs(false)
27
+
28
+ sleep 0.0005
29
+
30
+ bytes.to_enum.each_slice(CHUNK_SIZE) do |chunk|
31
+ start_bulk_write(chunk.length)
32
+ chunk.each do |byte|
33
+ @controller.serial_port.putc byte
34
+ end
35
+ @controller.serial_port.read(chunk.length)
36
+ end
37
+
38
+ sleep 0.0005
39
+
40
+ set_cs(true)
41
+ end
42
+
43
+ private
44
+ def set_cs(enable)
45
+ mask = SET_CS
46
+ mask |= ENABLE if enable
47
+ @controller.tx(mask)
48
+ end
49
+
50
+ def start_bulk_write(length)
51
+ @controller.tx(BULK_WRITE | (length - 1))
52
+ end
53
+
54
+ def enter_spi_mode
55
+ @controller.serial_port.puts SPI_MODE.chr
56
+ sleep Bucaneer::BusPirate::TIMEOUT
57
+ response = @controller.serial_port.read(4)
58
+ raise "failed to enter SPI mode" unless response == "SPI1"
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,4 @@
1
+ module Bucaneer::Protocol
2
+ autoload :I2C, 'bucaneer/protocol/i2c'
3
+ autoload :SPI, 'bucaneer/protocol/spi'
4
+ end
@@ -0,0 +1,3 @@
1
+ module Bucaneer
2
+ VERSION = "0.1.0" unless defined?(Bucaneer::VERSION)
3
+ end
data/lib/bucaneer.rb ADDED
@@ -0,0 +1,6 @@
1
+ module Bucaneer
2
+ end
3
+
4
+ require 'bucaneer/bus_pirate'
5
+ require 'bucaneer/protocol'
6
+ require 'bucaneer/version'
metadata ADDED
@@ -0,0 +1,202 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bucaneer
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Josh Bassett
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-22 00:00:00 +10:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: ruby-serialport
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: bundler
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 23
44
+ segments:
45
+ - 1
46
+ - 0
47
+ - 0
48
+ version: 1.0.0
49
+ type: :development
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: hirb
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ hash: 23
60
+ segments:
61
+ - 0
62
+ - 3
63
+ - 2
64
+ version: 0.3.2
65
+ type: :development
66
+ version_requirements: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ name: rake
69
+ prerelease: false
70
+ requirement: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ hash: 49
76
+ segments:
77
+ - 0
78
+ - 8
79
+ - 7
80
+ version: 0.8.7
81
+ type: :development
82
+ version_requirements: *id004
83
+ - !ruby/object:Gem::Dependency
84
+ name: rcov
85
+ prerelease: false
86
+ requirement: &id005 !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ~>
90
+ - !ruby/object:Gem::Version
91
+ hash: 43
92
+ segments:
93
+ - 0
94
+ - 9
95
+ - 8
96
+ version: 0.9.8
97
+ type: :development
98
+ version_requirements: *id005
99
+ - !ruby/object:Gem::Dependency
100
+ name: rspec
101
+ prerelease: false
102
+ requirement: &id006 !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ~>
106
+ - !ruby/object:Gem::Version
107
+ hash: 27
108
+ segments:
109
+ - 1
110
+ - 3
111
+ - 0
112
+ version: 1.3.0
113
+ type: :development
114
+ version_requirements: *id006
115
+ - !ruby/object:Gem::Dependency
116
+ name: rr
117
+ prerelease: false
118
+ requirement: &id007 !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ~>
122
+ - !ruby/object:Gem::Version
123
+ hash: 23
124
+ segments:
125
+ - 1
126
+ - 0
127
+ - 0
128
+ version: 1.0.0
129
+ type: :development
130
+ version_requirements: *id007
131
+ - !ruby/object:Gem::Dependency
132
+ name: wirble
133
+ prerelease: false
134
+ requirement: &id008 !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - ~>
138
+ - !ruby/object:Gem::Version
139
+ hash: 29
140
+ segments:
141
+ - 0
142
+ - 1
143
+ - 3
144
+ version: 0.1.3
145
+ type: :development
146
+ version_requirements: *id008
147
+ description: Bucaneer is a Ruby library which allows you to control your BusPirate bitbang modes (I2C, SPI, etc).
148
+ email: josh.bassett@gmail.com
149
+ executables: []
150
+
151
+ extensions: []
152
+
153
+ extra_rdoc_files: []
154
+
155
+ files:
156
+ - lib/bucaneer/alone.rb
157
+ - lib/bucaneer/bus_pirate.rb
158
+ - lib/bucaneer/protocol/i2c.rb
159
+ - lib/bucaneer/protocol/spi.rb
160
+ - lib/bucaneer/protocol.rb
161
+ - lib/bucaneer/version.rb
162
+ - lib/bucaneer.rb
163
+ - LICENSE
164
+ - README.md
165
+ has_rdoc: true
166
+ homepage: http://github.com/nullobject/bucaneer
167
+ licenses: []
168
+
169
+ post_install_message:
170
+ rdoc_options: []
171
+
172
+ require_paths:
173
+ - lib
174
+ required_ruby_version: !ruby/object:Gem::Requirement
175
+ none: false
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ hash: 3
180
+ segments:
181
+ - 0
182
+ version: "0"
183
+ required_rubygems_version: !ruby/object:Gem::Requirement
184
+ none: false
185
+ requirements:
186
+ - - ">="
187
+ - !ruby/object:Gem::Version
188
+ hash: 23
189
+ segments:
190
+ - 1
191
+ - 3
192
+ - 6
193
+ version: 1.3.6
194
+ requirements: []
195
+
196
+ rubyforge_project: bucaneer
197
+ rubygems_version: 1.3.7
198
+ signing_key:
199
+ specification_version: 3
200
+ summary: BusPirate library for Ruby.
201
+ test_files: []
202
+