adafruit / Adafruit_Blinka

Add CircuitPython hardware API and libraries to MicroPython & CPython devices
https://learn.adafruit.com/circuitpython-on-raspberrypi-linux
MIT License
453 stars 340 forks source link

Missing Pin imports for the MCP2221 #541

Closed andrejacobs closed 2 years ago

andrejacobs commented 2 years ago

Hi,

I am using the Adafruit MCP2221 breakout board to connect my Mac to I2C devices.

However when I try to import the library adafruit_ads1x15 it would raise the following error: NotImplementedError: ('Microcontroller not supported:', 'MCP2221')

I noticed that the import in the init.py for the MCP2221 was missing compared to what is in pin.py. After adding the missing import, I was able to use the ADS1015 with the MCP2221 as expected.

I checked if there were any other missing imports, and there were none. The only thing was that init.py prints a warning for GENERIC_X86 whereas pin.py doesn't.

Not really sure what the process is for testings etc. on this repository. If there are specific guidelines on raising a PR, then please let me know.

Kind regards, Andre

caternuson commented 2 years ago

This seems like something else. That file generally shouldn't come into play when using Blinka/MCP2221 on a Mac.

Are you passing the Blinka post install checks? https://learn.adafruit.com/circuitpython-libraries-on-any-computer-with-mcp2221/post-install-checks

Are you seeing the issue running the ADS example from the library? https://github.com/adafruit/Adafruit_CircuitPython_ADS1x15/blob/main/examples/ads1x15_simpletest.py

andrejacobs commented 2 years ago

Hi,

I will go through an example of my process.

Note: I did do the Blinka post install checks and trying to run the ADS1015 single port example results in the same error.

First some info about my setup:

System info

MacBook Pro (Retina, 15-inch, Mid 2014) 2.2 GHz Quad-Core Intel Core i7 macOS Big Sur 11.4

I use brew (homebrew.sh) for package management and Python installation

$ which python3
/usr/local/bin/python3

$ ls -la /usr/local/bin/python3
lrwxr-xr-x  1 andre  admin    38B 15 Dec 09:19 /usr/local/bin/python3@ -> ../Cellar/python@3.9/3.9.9/bin/python3

$ python3 --version
Python 3.9.9

Starting a new project

$ mkdir -p ~/temp/example
$ cd ~/temp/example

# Setup virtual environment
$ python3 -m venv ./venv
$ source ./venv/bin/activate
(venv)$ python3 -m pip install --upgrade pip
Requirement already satisfied: pip in ./venv/lib/python3.9/site-packages (21.3.1)

My requirements.txt for Blinka

Adafruit-Blinka==6.15.0
Adafruit-PlatformDetect==3.16.1
Adafruit-PureIO==1.1.9
hidapi==0.11.0.post2
pyftdi==0.53.3
pyserial==3.5
pyusb==1.2.1

Install dependencies

(venv)$ pip3 install -r requirements.txt

Collecting Adafruit-Blinka==6.15.0
  Using cached Adafruit-Blinka-6.15.0.tar.gz (151 kB)
  Preparing metadata (setup.py) ... done
Collecting Adafruit-PlatformDetect==3.16.1
  Using cached Adafruit-PlatformDetect-3.16.1.tar.gz (31 kB)
  Preparing metadata (setup.py) ... done
Collecting Adafruit-PureIO==1.1.9
  Using cached Adafruit_PureIO-1.1.9-py3-none-any.whl
Collecting hidapi==0.11.0.post2
  Using cached hidapi-0.11.0.post2-cp39-cp39-macosx_10_9_x86_64.whl (48 kB)
Collecting pyftdi==0.53.3
  Using cached pyftdi-0.53.3-py3-none-any.whl (141 kB)
Collecting pyserial==3.5
  Using cached pyserial-3.5-py2.py3-none-any.whl (90 kB)
Collecting pyusb==1.2.1
  Using cached pyusb-1.2.1-py3-none-any.whl (58 kB)
Requirement already satisfied: setuptools>=19.0 in ./venv/lib/python3.9/site-packages (from hidapi==0.11.0.post2->-r requirements.txt (line 4)) (59.0.1)
Using legacy 'setup.py install' for Adafruit-Blinka, since package 'wheel' is not installed.
Using legacy 'setup.py install' for Adafruit-PlatformDetect, since package 'wheel' is not installed.
Installing collected packages: pyusb, pyserial, pyftdi, Adafruit-PureIO, Adafruit-PlatformDetect, hidapi, Adafruit-Blinka
    Running setup.py install for Adafruit-PlatformDetect ... done
    Running setup.py install for Adafruit-Blinka ... done
Successfully installed Adafruit-Blinka-6.15.0 Adafruit-PlatformDetect-3.16.1 Adafruit-PureIO-1.1.9 hidapi-0.11.0.post2 pyftdi-0.53.3 pyserial-3.5 pyusb-1.2.1

Do a test with the MCP2221 board connected to my Mac

(venv)$ export BLINKA_MCP2221="1"
(venv)$ python3
>>> import board
>>> i2c = board.I2C()
>>> while not i2c.try_lock():
...     pass
...
>>> i2c.scan()
[72]
>>> i2c.unlock()
>>> exit()

Going through the Blinka post install checks

(venv)$ python3
>>> import hid
>>> device = hid.device()
>>> device.open(0x04D8, 0x00DD)

>>> import os
>>> os.environ["BLINKA_MCP2221"]
'1'

Try to use the ADS1x15 library

(venv)$ pip3 install adafruit-circuitpython-ads1x15

Collecting adafruit-circuitpython-ads1x15
  Using cached adafruit-circuitpython-ads1x15-2.2.10.tar.gz (30 kB)
  Preparing metadata (setup.py) ... done
Requirement already satisfied: Adafruit-Blinka in ./venv/lib/python3.9/site-packages (from adafruit-circuitpython-ads1x15) (6.15.0)
Collecting adafruit-circuitpython-busdevice
  Using cached adafruit-circuitpython-busdevice-5.1.1.tar.gz (28 kB)
  Preparing metadata (setup.py) ... done
Requirement already satisfied: Adafruit-PlatformDetect>=3.13.0 in ./venv/lib/python3.9/site-packages (from Adafruit-Blinka->adafruit-circuitpython-ads1x15) (3.16.1)
Requirement already satisfied: Adafruit-PureIO>=1.1.7 in ./venv/lib/python3.9/site-packages (from Adafruit-Blinka->adafruit-circuitpython-ads1x15) (1.1.9)
Requirement already satisfied: pyftdi>=0.40.0 in ./venv/lib/python3.9/site-packages (from Adafruit-Blinka->adafruit-circuitpython-ads1x15) (0.53.3)
Requirement already satisfied: pyusb!=1.2.0,>=1.0.0 in ./venv/lib/python3.9/site-packages (from pyftdi>=0.40.0->Adafruit-Blinka->adafruit-circuitpython-ads1x15) (1.2.1)
Requirement already satisfied: pyserial>=3.0 in ./venv/lib/python3.9/site-packages (from pyftdi>=0.40.0->Adafruit-Blinka->adafruit-circuitpython-ads1x15) (3.5)
Using legacy 'setup.py install' for adafruit-circuitpython-ads1x15, since package 'wheel' is not installed.
Using legacy 'setup.py install' for adafruit-circuitpython-busdevice, since package 'wheel' is not installed.
Installing collected packages: adafruit-circuitpython-busdevice, adafruit-circuitpython-ads1x15
    Running setup.py install for adafruit-circuitpython-busdevice ... done
    Running setup.py install for adafruit-circuitpython-ads1x15 ... done
Successfully installed adafruit-circuitpython-ads1x15-2.2.10 adafruit-circuitpython-busdevice-5.1.1

Trying out in the REPL

(venv)$ python3
>>> import board
>>> import busio
>>> import adafruit_ads1x15.ads1015 as ADS
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/andre/temp/example/venv/lib/python3.9/site-packages/adafruit_ads1x15/ads1015.py", line 16, in <module>
    from .ads1x15 import ADS1x15, Mode
  File "/Users/andre/temp/example/venv/lib/python3.9/site-packages/adafruit_ads1x15/ads1x15.py", line 24, in <module>
    from microcontroller import Pin
  File "/Users/andre/temp/example/venv/lib/python3.9/site-packages/microcontroller/__init__.py", line 134, in <module>
    raise NotImplementedError("Microcontroller not supported:", chip_id)
NotImplementedError: ('Microcontroller not supported:', 'MCP2221')

Kind regards, Andre

caternuson commented 2 years ago

Wow! Thanks for all the additional info. Helps a ton.

@FoamyGuy Can you take a look at this. It seems related to lines that were added as part of the type checking stuff. Might need to be something different to support Blinka setups which won't trigger an exception?

stonehippo commented 2 years ago

@FoamyGuy @caternuson In addition to the issue @andrejacobs is running into, it would be helpful to have this merged in so I can get displayio working with the MCP2221, per #542.