adafruit / Adafruit_CircuitPython_STMPE610

Adafruit CircuitPython driver for the STMPE610 resistive touchscreen controller
MIT License
1 stars 10 forks source link

OSError: [Errno 22] Invalid argument - on Buster #18

Closed briankaemingk closed 3 years ago

briankaemingk commented 3 years ago

Hello!

I have set up the STMPE610 on a pi zero WH running the latest version or Raspbian (buster release 10). I have installed the latest pip dependencies (Adafruit CircuitPython, Bus Device, Register). I have enabled SPI in the boot config.

Running the simpletest with sudo python3 connected to a beautiful, new Adafruit touchscreen overlay gives me the following error:

(env) pi@raspberrypi:~/Adafruit_CircuitPython_STMPE610 $ sudo python3 examples/stmpe610_simpletest.py 
Traceback (most recent call last):
  File "examples/stmpe610_simpletest.py", line 12, in <module>
    st = Adafruit_STMPE610_SPI(spi, cs)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_stmpe610.py", line 278, in __init__
    version = self.get_version
  File "/usr/local/lib/python3.7/dist-packages/adafruit_stmpe610.py", line 193, in get_version
    v_1 = self._read_byte(0)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_stmpe610.py", line 164, in _read_byte
    return self._read_register(register, 1)[0]
  File "/usr/local/lib/python3.7/dist-packages/adafruit_stmpe610.py", line 291, in _read_register
    spi.write(bytearray([register]))
  File "/usr/local/lib/python3.7/dist-packages/busio.py", line 325, in write
    return self._spi.write(buf, start, end)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_blinka/microcontroller/generic_linux/spi.py", line 81, in write
    self._spi.mode = self.mode
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_PureIO/spi.py", line 355, in mode
    self._ioctl(SPI._IOC_WR_MODE, mode)
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_PureIO/spi.py", line 224, in _ioctl
    ioctl(self.handle, ioctl_bytes, arg)
OSError: [Errno 22] Invalid argument

Things I have tried:

Can't wait to get my touchscreen to make my day and get my project rolling!! Any other ideas for troubleshooting this error? Happy to provide more info. Much appreciated.

ladyada commented 3 years ago

if you have the kernel overlay you cannot connect to it over SPI 'user space' - you can only do one or the other.

briankaemingk commented 3 years ago

Thanks for the quick response, @ladyada. Trying to interpret and get to a next step... Not sure if I have the kernel overlay, just installed Buster as directed.. Should I try this all as root or...?

ladyada commented 3 years ago

oh you just mean overlay, not kernel overlay. not sure what it is then, we'll leave this open for now!

briankaemingk commented 3 years ago

ah, yes, the touch overlay is what i mean, which i have used in the past to great success with this microcontroller. the example "make my day" script works like a charm, but now, alas, no dice. not sure what, if anything, changed. also, not really sure how to debug it....

any logs i can provide? any tips from users is much appreciated.

briankaemingk commented 3 years ago

thanks for assigning this/helping me out, @ladyada and @makermelissa . i did a little more debugging.. this particular error is caused after the first get version/initialization step fails.

in other words, it tries to find the stmpe610 version first on this line. That version check fails (the version returned is 0x0).

next, it executes the following code to switch the polarity, related to this issue

            # if it fails try SPI MODE 1  -- that is what Arduino does
            self._spi = spidev.SPIDevice(spi, cs, baudrate=baudrate, polarity=0, phase=1)

it then tries to find the stmpe610 version on this line. that is when i get the below error:

Traceback (most recent call last):
  File "examples/stmpe610_simpletest.py", line 12, in <module>
    st = Adafruit_STMPE610_SPI(spi, cs)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_stmpe610.py", line 278, in __init__
    version = self.get_version
  File "/usr/local/lib/python3.7/dist-packages/adafruit_stmpe610.py", line 193, in get_version
    v_1 = self._read_byte(0)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_stmpe610.py", line 164, in _read_byte
    return self._read_register(register, 1)[0]
  File "/usr/local/lib/python3.7/dist-packages/adafruit_stmpe610.py", line 291, in _read_register
    spi.write(bytearray([register]))
  File "/usr/local/lib/python3.7/dist-packages/busio.py", line 325, in write
    return self._spi.write(buf, start, end)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_blinka/microcontroller/generic_linux/spi.py", line 81, in write
    self._spi.mode = self.mode
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_PureIO/spi.py", line 355, in mode
    self._ioctl(SPI._IOC_WR_MODE, mode)
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_PureIO/spi.py", line 224, in _ioctl
    ioctl(self.handle, ioctl_bytes, arg)
OSError: [Errno 22] Invalid argument
briankaemingk commented 3 years ago

Hi everyone,

Just another quick update here. We just got the chip working by pulling "up" the MODE pin on the breakout board (aka setting it to 1). We did this physically on the board by connecting the MODE pin to an open 3.3v pin on the raspberry pi and cycling power to the controller. There is also probably a way to do it via python, which we will try next.

So, it seems like our board was in I2C mode, which initially cause it to fail the version check and not find the board. Then it switched the polarity to try to initialize with...

# if it fails try SPI MODE 1  -- that is what Arduino does
            self._spi = spidev.SPIDevice(spi, cs, baudrate=baudrate, polarity=0, phase=1)

After it ran that, for some reason it couldn't read the output it got back about the version, and it errored out with the below. So maybe there is a way for a more descriptive error message or something, or a warning about setting the controller to SPI mode...? Hope that helps, let me know if you need anything more to help debug. Thanks for the great stuff, Adafruit!! Love playing with it.

  File "examples/stmpe610_simpletest.py", line 12, in <module>
    st = Adafruit_STMPE610_SPI(spi, cs)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_stmpe610.py", line 278, in __init__
    version = self.get_version
  File "/usr/local/lib/python3.7/dist-packages/adafruit_stmpe610.py", line 193, in get_version
    v_1 = self._read_byte(0)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_stmpe610.py", line 164, in _read_byte
    return self._read_register(register, 1)[0]
  File "/usr/local/lib/python3.7/dist-packages/adafruit_stmpe610.py", line 291, in _read_register
    spi.write(bytearray([register]))
  File "/usr/local/lib/python3.7/dist-packages/busio.py", line 325, in write
    return self._spi.write(buf, start, end)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_blinka/microcontroller/generic_linux/spi.py", line 81, in write
    self._spi.mode = self.mode
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_PureIO/spi.py", line 355, in mode
    self._ioctl(SPI._IOC_WR_MODE, mode)
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_PureIO/spi.py", line 224, in _ioctl
    ioctl(self.handle, ioctl_bytes, arg)
OSError: [Errno 22] Invalid argument
makermelissa commented 3 years ago

I've been trying various strategies to try and reproduce this error without much luck. I also found it defaulted to I2C mode and tying the mode to 3.3v and then fully shutting down the pi and restarting got it working, though I suspect removing power from the breakout for a minute or so would have also worked due to capacitors.

I have tried reproducing the error with both a Raspberry Pi 4 and a Raspberry Pi Zero WH running the latest Raspberry Pi OS. I even tried forcing it to Mode 1 (which didn't work, but didn't produce the OSError). The only states I was able to get are working or I2C mode.

I like your suggestion about making the error message a bit better, so I think I will go with that for the fix.

briankaemingk commented 3 years ago

Thanks for your efforts, @makermelissa. Hoping this will make things better for users down the road.