Description
It seems that there is an "unlucky" period during and shortly after the initialization of this driver. If the device resets during this period the touch chip seems to get "locked up" into a state where it will always cause this error:
Traceback (most recent call last):
File "code.py", line 32, in <module>
File "adafruit_stmpe610.py", line 281, in __init__
RuntimeError: Failed to find STMPE610! Chip Version 0x8
I am using:
Adafruit CircuitPython 6.2.0-beta.4 on 2021-03-18; Adafruit Feather RP2040 with rp2040
With a 3.5" FeatherWing. and the following test code:
import board
import time
import digitalio
import displayio
from adafruit_stmpe610 import Adafruit_STMPE610_SPI
from adafruit_hx8357 import HX8357
COUNTDOWN = 5
for i in range(COUNTDOWN):
print("waiting {}".format(COUNTDOWN-i))
time.sleep(0.7)
displayio.release_displays()
spi = board.SPI()
cs = board.D9
dc = board.D10
ts_cs = board.D6
sd_cs = board.D5
ts_cs = digitalio.DigitalInOut(ts_cs)
_display_bus = displayio.FourWire(spi, command=dc, chip_select=cs)
display = HX8357(_display_bus, width=480, height=320)
st = Adafruit_STMPE610_SPI(spi, ts_cs)
print("Go Ahead - Touch the Screen - Make My Day!")
while True:
if not st.buffer_empty:
print(st.read_data())
I am able to enter the state reliably with this set up. Here are the series of events I see:
- Plug in device to PC
- Device immediately boots and begins running code.py
- If I am quick I can see "detected new device" in Mu and open the serial console
- I can see the last few numbers from the countdown get printed
- The screen gets initialized and I see "Go Ahead - Touch the Screen - Make My Day!" printed briefly.
- CIRCUITPY drive appears to my OS
- The device resets again. I suspect the OS may have written something automatically to cause it. But I am not certain.
- Device reconnects. I see "detected new device" in Mu again and can open the serial console.
- Countdown finishes normally. Display turns on and shows the "Failed to find" Runtime error.
- at this point I can CTRL-C then CTRL-D over and over and reliably get the same error every time. To get back to a working state I can use the reset button on the device, or unplug / replug. But if nothing else changes it will end up right back in this state again.
I could record a video of this sequence if it would be helpful.
The Chip Version {}
number that it that it prints is not always the same.
If the time of the countdown is increased it makes it more likely that the device wont hit his "unlucky" period during the initialization. With a high enough countdown it will reliably boot and work correctly every time because it's never having a chance to try setting up the touch screen before the "extra" reset happens.
If my hunch about the OS writing something to trigger the "extra" reset is correct then I suppose that is really at fault here. However I am left wondering if there is something we could within the driver to try to be more resilient to badly timed resets. Or if there would be some way to more safely catch this error and recover back to a working state without needing the reset or unplug/replug.
Another way I see it get triggered sometimes is with scripts that don't have a wait time built in, when pasting multiple files onto the device (i.e. a module or several image assets) the device resets several times as the different files get finished writing.
Adding enough wait time before doing anything seems to be a reliable solution, but means that you have to wait several seconds before being able to do anything each time it runs.