-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First pass at I2C bit bang implementation
- Loading branch information
Showing
11 changed files
with
352 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module Denko | ||
class Board | ||
# CMD = 31 | ||
def i2c_bb_write(scl, sda, address, bytes, repeated_start=false) | ||
bytes = [bytes] unless bytes.class == Array | ||
|
||
# Use top bit of address to select stop condition (1), or repated start (0). | ||
send_stop = repeated_start ? 0 : 1 | ||
|
||
write Message.encode command: 31, | ||
pin: scl, | ||
value: sda, | ||
aux_message: pack(:uint8, 0x00) + | ||
pack(:uint8, address | (send_stop << 7)) + | ||
pack(:uint8, bytes.length) + | ||
pack(:uint8, bytes) | ||
end | ||
|
||
# CMD = 35 | ||
def i2c_bb_read(scl, sda, address, register, read_length, repeated_start=false) | ||
# Use top bit of address to select stop condition (1), or repated start (0). | ||
send_stop = repeated_start ? 0 : 1 | ||
|
||
# A register address starting register address can be given (up to 4 bytes) | ||
if register | ||
register = [register].flatten | ||
raise ArgumentError, 'maximum 4 byte register address for I2C read' if register.length > 4 | ||
register_packed = pack(:uint8, [register.length] + register) | ||
else | ||
register_packed = pack(:uint8, [0]) | ||
end | ||
|
||
write Message.encode command: 32, | ||
pin: scl, | ||
value: sda, | ||
aux_message: pack(:uint8, 0x00) + | ||
pack(:uint8, address | (send_stop << 7)) + | ||
pack(:uint8, read_length) + | ||
register_packed | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
module Denko | ||
module I2C | ||
autoload :Bus, "#{__dir__}/i2c/bus" | ||
autoload :BitBang, "#{__dir__}/i2c/bit_bang" | ||
autoload :Peripheral, "#{__dir__}/i2c/peripheral" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
module Denko | ||
module I2C | ||
class BitBang | ||
include Behaviors::MultiPin | ||
include Behaviors::BusControllerAddressed | ||
include Behaviors::Reader | ||
|
||
def initialize_pins(options={}) | ||
require_pins :scl, :sda | ||
end | ||
|
||
attr_reader :found_devices | ||
|
||
def after_initialize(options={}) | ||
super(options) | ||
@found_devices = [] | ||
|
||
# Board will see we respond to #pin with pins[:sda], and provide updates | ||
unregister | ||
register | ||
bubble_callbacks | ||
end | ||
|
||
# Receive data coming from the SDA pin. | ||
def pin | ||
pins[:sda] | ||
end | ||
|
||
# def search | ||
# addresses = read_using -> { board.i2c_search } | ||
# @found_devices = addresses.split(":").map(&:to_i) if addresses | ||
# end | ||
|
||
def write(address, bytes, frequency=nil, repeated_start=false) | ||
board.i2c_bb_write(pins[:scl], pins[:sda], address, bytes, repeated_start) | ||
end | ||
|
||
def _read(address, register, num_bytes, frequency=nil, repeated_start=false) | ||
board.i2c_bb_read(pins[:scl], pins[:sda], address, register, num_bytes, repeated_start) | ||
end | ||
|
||
def bubble_callbacks | ||
add_callback(:bus_controller) do |str| | ||
if str && str.match(/\A\d+-/) | ||
address, data = str.split("-", 2) | ||
address = address.to_i | ||
|
||
data = data.split(",").map(&:to_i) | ||
data = nil if data.empty? | ||
|
||
components.each do |component| | ||
component.update(data) if component.address == address | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.