vpaeder / pymcp2221

A python driver for the Microchip MCP2221/MCP2221A USB 2.0 to I2C/UART protocol converters
https://pypi.org/project/pymcp2221
MIT License
5 stars 2 forks source link

Question how to set GPIO funktion #2

Closed crbyxwpzfl closed 1 year ago

crbyxwpzfl commented 1 year ago

Hi, my mcp2221 breakout by adafruit seems to be recognized ok via find_device

>>> print(find_devices(vendor_id=1240, product_id=221))
[{'path': b'DevSrvsID:4294972456', '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': 65280, 'usage': 1, 'interface_number': 2}]
>>> 

But I am not able to set the GPIO 0 to GPIO0Function.GPIO

>>> print(mcp.gpio0_function)
GPIO0Function.UartRxLed
>>> 
>>> mcp.gpio0_function = GPIO0Function.GPIO
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mini/Library/Python/3.9/lib/python/site-packages/mcp2221/__init__.py", line 1278, in gpio0_write_function
    self._gpio_write_function(0, value, mem)
  File "/Users/mini/Library/Python/3.9/lib/python/site-packages/mcp2221/__init__.py", line 1255, in _gpio_write_function
    self._write_sram(SramDataSubcode.GPSettings, gpio_num, value)
  File "/Users/mini/Library/Python/3.9/lib/python/site-packages/mcp2221/__init__.py", line 352, in _write_sram
    cmd[8:12] = [(gp_set[2*n] << 4) + (gp_set[2*n+1] << 3) for n in range(4)]
ValueError: byte must be in range(0, 256)
>>> 
>>> 

mac os Ventura python 3.9.6

I really appreciate any help or tips on how to use your python driver. I am not too good at python and the mcp2221 is new to me as well. Anyway feel free to close this issue at any time if this is just me not reading docs correctly.

vpaeder commented 1 year ago

Hi, thanks for telling. I updated the code. The issue was that when not in GPIO mode the pin value is 0xEE, so 0xEE << 4 is > 255, hence the error. I guess the version of python I used when writing this was trimming instead.

crbyxwpzfl commented 1 year ago

Wow thank you for your quick and helpful response!! Everything is working now. your driver is great especially the ability to define powerup states and the really nice comments in your code! Thanks again