Class: UChip::MCP2221

Inherits:
Object
  • Object
show all
Extended by:
Enumerable
Defined in:
lib/uchip/mcp2221.rb

Defined Under Namespace

Modules: FlashData Classes: Busy, ChipSettings, CommandNotSupported, EmptyResponse, Error, GPIOConfigurationError, GPSettings, I2CProxy, Pin, SRAM

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dev) ⇒ MCP2221

Returns a new instance of MCP2221.



17
18
19
20
# File 'lib/uchip/mcp2221.rb', line 17

def initialize dev
  @dev = dev
  @handle = dev.open
end

Class Method Details

.eachObject



13
14
15
# File 'lib/uchip/mcp2221.rb', line 13

def self.each
  MyHIDAPI.enumerate(0x04d8, 0x00dd).each { |dev| yield new dev }
end

Instance Method Details

#chip_settingsObject



106
107
108
109
110
111
112
# 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) ⇒ Object



114
115
116
# 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) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# 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_numberObject



202
203
204
205
206
# 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_settingsObject



176
177
178
179
180
181
182
# 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) ⇒ Object



118
119
120
# File 'lib/uchip/mcp2221.rb', line 118

def gp_settings= settings
  write_flash FlashData::GP_SETTINGS, settings.bytes
end

#gpio_value(pin) ⇒ Object



284
285
286
287
288
289
# 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_cancelObject



237
238
239
240
241
# 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) ⇒ Object



262
263
264
# File 'lib/uchip/mcp2221.rb', line 262

def i2c_on address
  I2CProxy.new address, self
end

#i2c_readObject



229
230
231
232
233
234
235
# 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) ⇒ Object



225
226
227
# 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) ⇒ Object



221
222
223
# 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) ⇒ Object



213
214
215
# 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) ⇒ Object



217
218
219
# File 'lib/uchip/mcp2221.rb', line 217

def i2c_write_no_stop address, bytes
  send_i2c_command 0x94, address, bytes.bytesize, bytes
end

#manufacturerObject



184
185
186
187
188
# 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) ⇒ Object



280
281
282
# File 'lib/uchip/mcp2221.rb', line 280

def pin number
  Pin.new self, number
end

#productObject



190
191
192
193
194
# 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) ⇒ Object



30
31
32
33
34
# 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

#resetObject



208
209
210
211
# File 'lib/uchip/mcp2221.rb', line 208

def reset
  buf = pad ([0x70, 0xAB, 0xCD, 0xEF]).pack('C*')
  write_request buf
end

#serial_numberObject



196
197
198
199
200
# 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) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# 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

#sramObject



346
347
348
349
350
351
352
353
354
355
# 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_manufacturerObject



22
23
24
# File 'lib/uchip/mcp2221.rb', line 22

def usb_manufacturer
  handle.manufacturer
end

#usb_productObject



26
27
28
# File 'lib/uchip/mcp2221.rb', line 26

def usb_product
  handle.product
end

#write_flash(section, bytes) ⇒ Object



36
37
38
39
40
# 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