adafruit / Adafruit_Blinka

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

Error when importing board on Rock 4C+ #683

Closed Skuallpa closed 1 year ago

Skuallpa commented 1 year ago

I'm trying to use the BME280 sensor on a Rock 4C+ but face an issue while importing board in my python script:

import board

The first error I've encountered was this one:

ImportError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "bme280.py", line 3, in <module>
    import board
  File "/home/john/.local/share/virtualenvs/garden-sensors-BH19RWdn/lib/python3.8/site-packages/board.py", line 251, in <module>
    from adafruit_blinka.board.radxa.rockpi4 import *
  File "/home/john/.local/share/virtualenvs/garden-sensors-BH19RWdn/lib/python3.8/site-packages/adafruit_blinka/board/radxa/rockpi4.py", line 7, in <module>
    from adafruit_blinka.microcontroller.rockchip.rk3399 import pin
  File "/home/john/.local/share/virtualenvs/garden-sensors-BH19RWdn/lib/python3.8/site-packages/adafruit_blinka/microcontroller/rockchip/rk3399/pin.py", line 7, in <module>
    from adafruit_blinka.microcontroller.generic_linux.libgpiod_pin import Pin
  File "/home/john/.local/share/virtualenvs/garden-sensors-BH19RWdn/lib/python3.8/site-packages/adafruit_blinka/microcontroller/generic_linux/libgpiod_pin.py", line 8, in <module>
    raise ImportError(
ImportError: libgpiod Python bindings not found, please install and try again! See https://github.com/adafruit/Raspberry-Pi-Installer-Scripts/blob/master/libgpiod.sh

I've attempted first to install libgpiod following instructions in project Raspberry-Pi-Installer-Scripts, using:

cd ~
sudo pip3 install --upgrade adafruit-python-shell click
wget https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/libgpiod.py
sudo python3 libgpiod.py

.. but this fails with an error LIBGPIOD E: Unable to locate package raspberrypi-kernel-headers

I've then tried to add directly gpiod using pipenv install gpiod, then the import board triggers the following error:

Traceback (most recent call last):
  File "bme280.py", line 3, in <module>
    import board
  File "/home/john/.local/share/virtualenvs/garden-sensors-BH19RWdn/lib/python3.8/site-packages/board.py", line 251, in <module>
    from adafruit_blinka.board.radxa.rockpi4 import *
  File "/home/john/.local/share/virtualenvs/garden-sensors-BH19RWdn/lib/python3.8/site-packages/adafruit_blinka/board/radxa/rockpi4.py", line 7, in <module>
    from adafruit_blinka.microcontroller.rockchip.rk3399 import pin
  File "/home/john/.local/share/virtualenvs/garden-sensors-BH19RWdn/lib/python3.8/site-packages/adafruit_blinka/microcontroller/rockchip/rk3399/pin.py", line 12, in <module>
    GPIO0_A0 = Pin((0, 0))
  File "/home/john/.local/share/virtualenvs/garden-sensors-BH19RWdn/lib/python3.8/site-packages/adafruit_blinka/microcontroller/generic_linux/libgpiod_pin.py", line 37, in __init__
    self._chip = gpiod.chip(str(pin_id[0]), gpiod.chip.OPEN_BY_NUMBER)
  File "/home/john/.local/share/virtualenvs/garden-sensors-BH19RWdn/lib/python3.8/site-packages/gpiod/libgpiodcxx/__init__.py", line 105, in __init__
    self.open(device, how)
  File "/home/john/.local/share/virtualenvs/garden-sensors-BH19RWdn/lib/python3.8/site-packages/gpiod/libgpiodcxx/__init__.py", line 135, in open
    raise OSError(
OSError: [Errno 0] Success: 'cannot open GPIO device 0'

I've seen that some things were fixed in recent releases of Adafruit_Blinka (e.g. v8.16.1) regarding the Rock 4C+, so I've also attempted to install the latest version (v.8.19.0). This is my current dependencies (in Pipfile.lock):

   "default": {
        "adafruit-blinka": {
            "hashes": [
                "sha256:95c3a58fcf1e820b295a994b21eeafef707525a9b9d6287f9085a7cccc05547b",
                "sha256:be65e91dfdd9780cc39ed2beaf489aee5b24a28759aad20633282f946e5113fb"
            ],
            "markers": "python_full_version >= '3.7.0'",
            "version": "==8.19.0"
        },
        "adafruit-circuitpython-bme280": {
            "hashes": [
                "sha256:c67b051c4c7689a6172f682264e7d2386dfceca3909d183976fa3297314fb244",
                "sha256:e730a332e5dcfb0c678101aef09af6b5ea8be9ab4966c5d89d2e1d85854897a9"
            ],
            "index": "pypi",
            "version": "==2.6.21"
        },

What is the correct way to install libgpiod and have a working setup with the Rock 4C+ board?

Skuallpa commented 1 year ago

I've got some help by posting on the forum. The issue was caused by an incorrect definition of the pinout for my board version. My Rock 4C+ is running a Linux Kernel < 4.8 and thus I had to revert the definition of pin.py in adafruit_blinka/microcontroller/rockchip/rk3399/pin.py to a previous implementation: https://github.com/adafruit/Adafruit_Blinka/pull/677/files

Thanks a lot to @GoGerriko for his help.