class UChip::MCP2221
Attributes
handle[R]
Public Class Methods
each() { |new dev| ... }
click to toggle source
# File lib/uchip/mcp2221.rb, line 13 def self.each MyHIDAPI.enumerate(0x04d8, 0x00dd).each { |dev| yield new dev } end
new(dev)
click to toggle source
# File lib/uchip/mcp2221.rb, line 17 def initialize dev @dev = dev @handle = dev.open end
Public Instance Methods
chip_settings()
click to toggle source
# File lib/uchip/mcp2221.rb, line 106 def chip_settings buf = read_flash(FlashData::CHIP_SETTINGS).bytes .drop(2) # response header .drop(2) # not care (according to data sheet) .first(10) ChipSettings.new buf end
chip_settings=(settings)
click to toggle source
# File lib/uchip/mcp2221.rb, line 114 def chip_settings= settings write_flash FlashData::CHIP_SETTINGS, settings.bytes end
configure_gpio(pin, mode, default = 0)
click to toggle source
# File lib/uchip/mcp2221.rb, line 291 def configure_gpio pin, mode, default = 0 settings = default << 4 case mode when :input then mode = (1 << 3) when :output then mode = 0 end settings |= mode # Read the current pin settings bytes = sram.gp_settings.bytes bytes[pin] = settings cmd = 0x60 buf = pad ([cmd, 0x0, 0x0, # clock output divider 0x0, # DAC voltage reference 0x0, # DAC output value 0x0, # ADC voltage reference 0x0, # interrupt detection (1 << 7), # Alter GPIO config ] + bytes).pack('C*') write_request buf check_response read_response, cmd end
factory_serial_number()
click to toggle source
# File lib/uchip/mcp2221.rb, line 202 def factory_serial_number byte_count, _, *rest = read_flash(FlashData::FACTORY_SERIAL_NUMBER).bytes .drop(2) # response header rest[0, byte_count - 2].pack('U*') end
gp_settings()
click to toggle source
# File lib/uchip/mcp2221.rb, line 176 def gp_settings buf = read_flash(FlashData::GP_SETTINGS).bytes .drop(2) # response header .drop(2) # structure length, don't care .first(4) GPSettings.new buf end
gp_settings=(settings)
click to toggle source
# File lib/uchip/mcp2221.rb, line 118 def gp_settings= settings write_flash FlashData::GP_SETTINGS, settings.bytes end
gpio_value(pin)
click to toggle source
# File lib/uchip/mcp2221.rb, line 284 def gpio_value pin cmd = 0x51 write_request pad cmd.chr buf = check_response read_response, cmd buf[2 + (pin * 2), 1].ord end
i2c_cancel()
click to toggle source
# File lib/uchip/mcp2221.rb, line 237 def i2c_cancel buf = pad [0x10, 0x0, 0x10].pack('C*') write_request buf check_response read_response, 0x10 end
i2c_on(address)
click to toggle source
# File lib/uchip/mcp2221.rb, line 262 def i2c_on address I2CProxy.new address, self end
i2c_read()
click to toggle source
# File lib/uchip/mcp2221.rb, line 229 def i2c_read buf = pad 0x40.chr write_request buf buf = check_response read_response, 0x40 len = buf[3].ord buf[4, len] end
i2c_read_repeated_start(address, length)
click to toggle source
# File lib/uchip/mcp2221.rb, line 225 def i2c_read_repeated_start address, length send_i2c_command 0x93, address, length, "".b end
i2c_read_start(address, length)
click to toggle source
# File lib/uchip/mcp2221.rb, line 221 def i2c_read_start address, length send_i2c_command 0x91, address, length, "".b end
i2c_write(address, bytes)
click to toggle source
# File lib/uchip/mcp2221.rb, line 213 def i2c_write address, bytes send_i2c_command 0x90, address, bytes.bytesize, bytes end
i2c_write_no_stop(address, bytes)
click to toggle source
# File lib/uchip/mcp2221.rb, line 217 def i2c_write_no_stop address, bytes send_i2c_command 0x94, address, bytes.bytesize, bytes end
manufacturer()
click to toggle source
# File lib/uchip/mcp2221.rb, line 184 def manufacturer byte_count, _, *rest = read_flash(FlashData::MANUFACTURER).bytes .drop(2) # response header rest[0, byte_count - 2].pack('U*') end
pin(number)
click to toggle source
# File lib/uchip/mcp2221.rb, line 280 def pin number Pin.new self, number end
product()
click to toggle source
# File lib/uchip/mcp2221.rb, line 190 def product byte_count, _, *rest = read_flash(FlashData::PRODUCT).bytes .drop(2) # response header rest[0, byte_count - 2].pack('U*') end
read_flash(section)
click to toggle source
# File lib/uchip/mcp2221.rb, line 30 def read_flash section buf = pad [0xB0, section].pack('C*') write_request buf check_response read_response, 0xB0 end
reset()
click to toggle source
# File lib/uchip/mcp2221.rb, line 208 def reset buf = pad ([0x70, 0xAB, 0xCD, 0xEF]).pack('C*') write_request buf end
serial_number()
click to toggle source
# File lib/uchip/mcp2221.rb, line 196 def serial_number byte_count, _, *rest = read_flash(FlashData::SERIAL_NUMBER).bytes .drop(2) # response header rest[0, byte_count - 2].pack('U*') end
set_gpio_value(pin, value)
click to toggle source
# File lib/uchip/mcp2221.rb, line 316 def set_gpio_value pin, value cmd = 0x50 pin_values = [ 0x0, # Alter output 0x0, # Output Value 0x0, # Alter direction 0x0 # Direction value ] * 4 pin_values[(pin * 4)] = 0x1 pin_values[(pin * 4) + 1] = (value & 0x1) buf = pad ([cmd, 0x0] + pin_values).pack('C*') write_request buf val = check_response(read_response, cmd)[3 + (pin * 2)].ord if val == 0xEE raise GPIOConfigurationError, "Pin #{pin} not configured as GPIO" else val end end
sram()
click to toggle source
# File lib/uchip/mcp2221.rb, line 346 def sram cmd = 0x61 buf = pad cmd.chr write_request buf buf = check_response read_response, cmd cs = ChipSettings.new buf[4, 10].bytes pw = buf[14, 8] gp = GPSettings.new buf[22, 4].bytes SRAM.new cs, pw, gp end
usb_manufacturer()
click to toggle source
# File lib/uchip/mcp2221.rb, line 22 def usb_manufacturer handle.manufacturer end
usb_product()
click to toggle source
# File lib/uchip/mcp2221.rb, line 26 def usb_product handle.product end
write_flash(section, bytes)
click to toggle source
# File lib/uchip/mcp2221.rb, line 36 def write_flash section, bytes buf = pad ([0xB1, section] + bytes).pack('C*') write_request buf check_response read_response, 0xB1 end
Private Instance Methods
check_response(buf, type)
click to toggle source
# File lib/uchip/mcp2221.rb, line 369 def check_response buf, type raise Error, buf unless buf[0].ord == type raise Busy, buf unless buf[1].ord == 0 buf end
pad(buf)
click to toggle source
# File lib/uchip/mcp2221.rb, line 365 def pad buf buf << ("\x0".b * (64 - buf.bytesize)) end
read_response()
click to toggle source
# File lib/uchip/mcp2221.rb, line 375 def read_response # 300 ms timeout #return @handle.read 64 @handle.read_timeout(64, 30) || raise(EmptyResponse) end
send_i2c_command(cmd, address, length, bytes)
click to toggle source
# File lib/uchip/mcp2221.rb, line 359 def send_i2c_command cmd, address, length, bytes buf = pad [cmd, length & 0xFF, (length >> 16) & 0xFF, address].pack('C*') + bytes write_request buf check_response read_response, cmd end
write_request(buf)
click to toggle source
# File lib/uchip/mcp2221.rb, line 381 def write_request buf retries = 0 loop do break if handle.write buf retries += 1 raise "Too many retries" if retries > 3 end end