X-Git-Url: https://git.ayoreis.com/Adafruit_Blinka-hackapet.git/blobdiff_plain/069a670a4c6c364e5e13fc0b8febc27a13980450..a902c58e079de67dba3bb3ead2cad5c6f783894c:/src/adafruit_blinka/microcontroller/mcp2221/mcp2221.py diff --git a/src/adafruit_blinka/microcontroller/mcp2221/mcp2221.py b/src/adafruit_blinka/microcontroller/mcp2221/mcp2221.py index bd44474..0cddf42 100644 --- a/src/adafruit_blinka/microcontroller/mcp2221/mcp2221.py +++ b/src/adafruit_blinka/microcontroller/mcp2221/mcp2221.py @@ -1,6 +1,12 @@ +import os import time import hid +# Here if you need it +MCP2221_HID_DELAY = float(os.environ.get('BLINKA_MCP2221_HID_DELAY', 0)) +# Use to set delay between reset and device reopen +MCP2221_RESET_DELAY = float(os.environ.get('BLINKA_MCP2221_RESET_DELAY', 0.5)) + # from the C driver # http://ww1.microchip.com/downloads/en/DeviceDoc/mcp2221_0_1.tar.gz # others (???) determined during driver developement @@ -44,13 +50,13 @@ class MCP2221: self._hid = hid.device() self._hid.open(MCP2221.VID, MCP2221.PID) self._reset() - time.sleep(0.25) def _hid_xfer(self, report, response=True): # first byte is report ID, which =0 for MCP2221 # remaing bytes = 64 byte report data # https://github.com/libusb/hidapi/blob/083223e77952e1ef57e6b77796536a3359c1b2a3/hidapi/hidapi.h#L185 self._hid.write(b'\0' + report + b'\0'*(64-len(report))) + time.sleep(MCP2221_HID_DELAY) if response: # return is 64 byte response report return self._hid.read(64) @@ -100,6 +106,7 @@ class MCP2221: def _reset(self): self._hid_xfer(b'\x70\xAB\xCD\xEF', response=False) + time.sleep(MCP2221_RESET_DELAY) start = time.monotonic() while time.monotonic() - start < 5: try: @@ -279,12 +286,12 @@ class MCP2221: found = [] for addr in range(start, end+1): # try a write - self.i2c_writeto(addr, b'\x00') + try: + self.i2c_writeto(addr, b'\x00') + except RuntimeError: # no reply! + continue # store if success - if self._i2c_status() == 0x00: - found.append(addr) - # cancel and continue - self._i2c_cancel() + found.append(addr) return found #----------------------------------------------------------------