nonNoise / PyMCP2221A

MCP2221 & MCP2221A work in Python.
MIT License
36 stars 21 forks source link

IndexError: list index out of range #17

Closed xsherlockpl closed 3 years ago

xsherlockpl commented 3 years ago

Dear, I'm trying to run the example but it fails.

sherlock@dt-minion-1:~$ sudo python3 MCP2221_i2cdetect.py
--------------------------------------------------
MCP2221(A) i2cdetect
--------------------------------------------------
Reset
Traceback (most recent call last):
  File "MCP2221_i2cdetect.py", line 13, in <module>
    mcp2221 = PyMCP2221A.PyMCP2221A()
  File "/usr/local/lib/python3.8/dist-packages/PyMCP2221A/PyMCP2221A.py", line 16, in __init__
    self.mcp2221a.open_path(hid.enumerate(VID, PID)[devnum]["path"])
IndexError: list index out of range

It gets stuck after printing RESET for quite a long time. and then crashes

I can see the device (adafruit board) is connected to the USB.

sherlock@dt-minion-1:~$ sudo lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 007: ID 8087:0aaa Intel Corp. Bluetooth 9460/9560 Jefferson Peak (JfP)
Bus 001 Device 006: ID 2c7c:0800 Quectel Wireless Solutions Co., Ltd. RM500Q-GL
Bus 001 Device 005: ID 2886:8027 Seeed Technology Co., Ltd. Seeeduino_Cortex_M0+
Bus 001 Device 020: ID 04d8:00dd Microchip Technology, Inc. MCP2221 USB-I2C/UART Combo
Bus 001 Device 004: ID 0bda:0129 Realtek Semiconductor Corp. RTS5129 Card Reader Controller
Bus 001 Device 003: ID 0bda:c811 Realtek Semiconductor Corp. 802.11ac NIC
Bus 001 Device 002: ID 16d0:063d MCS Master Brick
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

is that incompatibility with python3?

I found that post that might be related to the issue https://github.com/trezor/cython-hidapi/issues/86

My output from enumerate is the following:


>>> hid.enumerate(0x04d8,0x00dd)
[{'path': b'0001:0016:02', 'vendor_id': 1240, 'product_id': 221, 'serial_number': '', 'release_number': 256, 'manufacturer_string': 'Microchip Technology Inc.', 'product_string': 'MCP2221 USB-I2C/UART Combo', 'usage_page': 0, 'usage': 0, 'interface_number': 2}]
>>> d = hid.device()
>>> d.open_path( b'0001:0016:02')
>>>

So i accually can open path to device

and if I hard code device path into the PyMCP2221A library itself AND will commnet out mcp2221.Reset() It will run! but mcp2221.Reset() changes the usb path every time so that is not an option

sherlock@dt-minion-1:~$ sudo python3 mcptest.py
--------------------------------------------------
MCP2221(A) i2cdetect
--------------------------------------------------
Manufacturer: Microchip Technology Inc.
Product: MCP2221 USB-I2C/UART Combo
Serial No: Љ
   0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
  --  --  --  --  --  --  --  --  --  --  --  0B  --  --  --  --   0F
  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --   1F
  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --   2F
  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --   3F
  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --   4F
  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --   5F
  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --   6F
  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --

What can be causing that?

TIA

nonNoise commented 3 years ago

Thank you very much. I haven't had a similar issue, but mcp2221.Reset may have a problem. mcp2221.Reset is a "Hacks" to release by resetting MCP2221A when the connection to I2C fails and it stops at Read. It's not working properly as a HID driver, so you might want to comment it out. It will be removed in future improvements.

Also, please tell me the Python version in detail. I have certainly not tried the new version. I didn't notice that Python had reached 3.9. It's changing faster than it was in the Python2 era.

xsherlockpl commented 3 years ago

I slept with the problem and now it is obvious that the problem is a Reset() function on my setup. It just crashes the the board.

I use Ubuntu 20.10 Python 3.8.6 (default, Jan 27 2021, 15:42:20) And that little board from Adafruit MCP2221A Breakout connected over USB-C https://www.adafruit.com/product/4471

Without Reset() it works great and I have not seen a crash yet.