tinue / apa102-pi

Pure Python library to drive APA102 LED stripes; Use with Raspberry Pi.
GNU General Public License v2.0
201 stars 71 forks source link

lgpio.error: 'GPIO busy' in test script, R.Pi 5 #60

Open breaker-of-stone opened 3 weeks ago

breaker-of-stone commented 3 weeks ago

Previous work on an R.Pi.4 is throwing an error on the R.Pi.5.

I'm using apa102_pi and the demo script therein: https://github.com/tinue/apa102-pi/blob/main/runcolorcycle.py

and seeing a 'GPIO busy' exception. Configuration and exception dump follows. How to diagnose?

pinout

Description        : Raspberry Pi 5B rev 1.0
Revision           : d04170
SoC                : BCM2712
RAM                : 8GB

lsb_release -a

No LSB modules are available.
Distributor ID: Debian  # raspi-OS via R.Pi Imager
Description:    Debian GNU/Linux 12 (bookworm)
Release:    12
Codename:   bookworm

python --version Python 3.11.2 # distro venv is engaged

Prior to this situation I encountered:

  File "/noisebin/.venv/lib/python3.11/site-packages/adafruit_blinka/microcontroller/bcm283x/pin.py", line 40, in init
    GPIO.setup(self.id, GPIO.IN)
RuntimeError: Cannot determine SOC peripheral base address

So chose to:

sudo apt remove python3-rpi.gpio
pip3 install rpi-lgpio

/boot/firmware/config.txt has:

dtparam=i2c_arm=on
dtparam=spi=on

[all]
dtoverlay=spi1-3cs

Have enabled SPI in raspi-config for good measure.

pinctrl gives:

116: ip    pd | lo // GPIO16 = input
117: ip    pd | lo // GPIO17 = input
118: ip    pd | lo // GPIO18 = input

gpioinfo says:

line  16:     "GPIO16"   "spi1 CS2"  output   active-low [used]
line  17:     "GPIO17"   "spi1 CS1"  output   active-low [used]
line  18:     "GPIO18"   "spi1 CS0"  output   active-low [used]

Issue follows. Results are identical if running under sudo.

Traceback (most recent call last):
  File "/home/quiet-mode/examples/led-light-drivers/rcc.py", line 47, in <module>
    main()
  File "/home/quiet-mode/examples/led-light-drivers/rcc.py", line 14, in main
    my_cycle.start()
  File "/quiet-mode/.venv/lib/python3.11/site-packages/apa102_pi/driver/colorcycletemplate.py", line 71, in start
    strip = apa102.APA102(num_led=self.num_led, bus_method=self.bus_method, spi_bus=self.spi_bus,
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/quiet-mode/.venv/lib/python3.11/site-packages/apa102_pi/driver/apa102.py", line 130, in __init__
    ce = eval("digitalio.DigitalInOut(board.D" + str(ce) + ")")
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <module>
  File "/quiet-mode/.venv/lib/python3.11/site-packages/digitalio.py", line 193, in __init__
    self.direction = Direction.INPUT
    ^^^^^^^^^^^^^^
  File "/quiet-mode/.venv/lib/python3.11/site-packages/digitalio.py", line 223, in direction
    self._pin.init(mode=Pin.IN)
  File "/quiet-mode/.venv/lib/python3.11/site-packages/adafruit_blinka/microcontroller/bcm283x/pin.py", line 40, in init
    GPIO.setup(self.id, GPIO.IN)
  File "/quiet-mode/.venv/lib/python3.11/site-packages/RPi/GPIO/__init__.py", line 696, in setup
    _check(lgpio.gpio_claim_input(_chip, gpio, {
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/quiet-mode/.venv/lib/python3.11/site-packages/lgpio.py", line 755, in gpio_claim_input
    return _u2i(_lgpio._gpio_claim_input(handle&0xffff, lFlags, gpio))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/quiet-mode/.venv/lib/python3.11/site-packages/lgpio.py", line 458, in _u2i
    raise error(error_text(v))
lgpio.error: 'GPIO busy'

Counter-evidence: Another demo from the apa102_pi repo succeeds, has no errors: https://github.com/tinue/apa102-pi/blob/main/sample.py

tinue commented 3 weeks ago

Hello, thanks for the report. It's been a while since I last tried on a Raspberry Pi 5, but it worked at the time. I suspect a chance in Adafruit's Blinka library, which is imported by apa102. Unfortunately I can't test for at least a week, so a resolution might take a while.

breaker-of-stone commented 3 weeks ago

Thanks, would at least welcome verification.

tinue commented 2 weeks ago

Unfortunately I cannot reproduce the error. I have the same Pi 5 revision as you, and I have fully updated/upgraded the OS, and upgraded all Python packages. See the list below.

There is one difference in my setup: After installing apa102-pi, I uninstall it right away (I only want the required dependencies). Then, I use the cloned apa102-pi, and run e.g. ./runcolorcycle.py. You seem to run e.g. .venv/lib/python3.11/site-packages/apa102_pi/driver/colorcycletemplate.py, while I would run ./apa102_pi/driver/colorcycletemplate.py. Could you try this as well?

Edit: I have this package installed by default via apt:

python3-rpi.gpio:
  Installed: 0.7.1~a4-1+b4

lgpio was installed via Adafruit-Blinka, I suspect. It's in the list below. It is not installed via apt, on my system.

Package                                  Version
---------------------------------------- --------
Adafruit-Blinka                          8.47.0
adafruit-circuitpython-bitbangio         1.3.15
adafruit-circuitpython-busdevice         5.2.9
adafruit-circuitpython-connectionmanager 3.1.1
adafruit-circuitpython-requests          4.1.6
adafruit-circuitpython-typing            1.11.0
Adafruit-PlatformDetect                  3.73.0
Adafruit-PureIO                          1.1.11
binho-host-adapter                       0.1.6
certifi                                  2024.7.4
chardet                                  5.2.0
charset-normalizer                       3.3.2
colorzero                                2.0
distro                                   1.9.0
gpiozero                                 2.0.1
idna                                     3.8
lgpio                                    0.2.2.0
pigpio                                   1.78
pip                                      24.2
pycryptodomex                            3.20.0
pyftdi                                   0.55.4
pyserial                                 3.5
python-apt                               2.6.0
pyusb                                    1.2.1
requests                                 2.32.3
RPi.GPIO                                 0.7.1
rpi-ws281x                               5.0.0
setuptools                               74.0.0
six                                      1.16.0
smbus2                                   0.4.3
spidev                                   3.6
ssh-import-id                            5.11
sysv-ipc                                 1.1.0
toml                                     0.10.2
typing_extensions                        4.12.2
urllib3                                  2.2.2
wheel                                    0.44.0