1.3" SPI Colour LCD (240x240) Breakout Driver/example issue

I wrote my own code based upon the scrolling-text.py code.

I left my code running for about 20 mins and it failed with :-

free(): invalid pointer
Aborted

No stacktrace or anything. So naturally I assumed I was doing something wrong. I’m not python expert for sure, so thought that was most likely. Btw, I was using python3.

In the scrolling-text.py example, an image and draw are created. Then the draw is updated and displayed over and over again. Which is what I was doing as I had a static part to my image and a smaller part I could update quickly. As this caused the error, I changed the code to create a new draw object every time around and that still failed.

So I removed 1 line. The disp.display(). So not updating the display. This time, it didn’t fail. So, I thought the issue is somewhere in the driver (or the demo code).

So I went back to the scrolling-text.py code and added a simple counter, so I know if it failed, how many times around the loop it was going. For my program it always failed after 37278 updates.

Amazingly, so did scrolling-text.py :-

pi@pi4-2:~/st7789-python/examples $ python3 scrolling-text.py
count 37278free(): invalid pointer
Aborted

It takes it about 25 minutes before it failed on my pi 4 with the default spi speed of 80mhz. Slightly quicker if you increase it 100mhz. Either way its always at 37278.

So I decided to run it using python 2. It still failed, but with a different error :-

pi@pi4-2:~/st7789-python/examples $ python scrolling-text.py
37279
Traceback (most recent call last):
  File "scrolling-text.py", line 50, in <module>
    draw.text((int(text_x - x), text_y), MESSAGE, font=font, fill=(255, 255, 255))
  File "/usr/lib/python2.7/dist-packages/PIL/ImageDraw.py", line 276, in text
    xy = xy[0] + offset[0], xy[1] + offset[1]
IndexError: tuple index out of range
Segmentation fault

I decided to see if the gif.py had the same issue. Sadly it does, so its not specific to the calls to draw.text() that’s the issue as gif.py doesn’t do any of those :-

pi@pi4-2:~/st7789-python/examples $ python gif.py deployrainbows.gif

gif.py - Display a gif on the LCD.

If you're using Breakout Garden, plug the 1.3" LCD (SPI)
breakout into the front slot.

Loading gif: deployrainbows.gif...
Drawing gif, press Ctrl+C to exit!
37277Segmentation fault

I did adapt it to display a loop counter again, but no other changes. I even ran scrolling-text.py with the unmodified code and it still failed :-

 python scrolling-text.py #
 Segmentation fault

Any ideas what’s wrong? Is it the code or the driver?

I did spot one typing mistake in all the example code. Its in a comment however, but someone could following it and get misled by it :-

# Create ST7789 LCD display class.
disp = ST7789.ST7789(
    port=0,
    cs=ST7789.BG_SPI_CS_FRONT,  # BG_SPI_CSB_BACK or BG_SPI_CS_FRONT
    dc=9,
    backlight=19,               # 18 for back BG slot, 19 for front BG slot.
    spi_speed_hz=80 * 1000 * 1000
)

The comment ‘BG_SPI_CSB_BACK or BG_SPI_CS_FRONT’ should be ‘BG_SPI_CS_BACK or BG_SPI_CS_FRONT’.

It’s not BG_SPI_CSB_BACK, but BG_SPI_CS_BACK. But obviously that’s not the cause of the problems.

I thought I’d get the code as simple as I could :-

from PIL import Image
import ST7789
disp = ST7789.ST7789(
    port=0,
    cs=ST7789.BG_SPI_CS_FRONT,
    dc=9,
    backlight=19,
    spi_speed_hz=100 * 1000 * 1000
)
disp.begin()
image = Image.new('RGB', (disp.width, disp.height), color=(0,0,0))
while True:
        disp.display(image)

This also fails with :-

pi@pi4-2:~/st7789-python/examples $ python test2.py
Segmentation fault

Hi, sorry for resurrecting an old issue but has anyone resolved this?

I have tried to use reset function or even deleting and recreating disp object after some time but it still crashes after 37278 updates

In the end I used another driver that was available from somewhere else. It was even a good deal quicker than the pimoroni one as well. Works perfectly without crashing after 24+ hours of updating at 15+fps. I’ll have a look to see where I got the driver from, or post a link to it later. I’m not sure if I tweaked it on not as well. I think I did, but it was many months ago now.

I would highly appreciated! I need to update the screen only every second so it’s not a big load but it should last for months without a crash. Worst of this is that “try” doesn’t catch that :/

I’ve put a tar gz file at https://www.dropbox.com/s/bfa3jte53shcj5o/ST7789.gz?dl=0

It contains ST7789.py and a python script that I think displays some stuff called display.py.

Sadly the project is in bits currently so I’m unable to test. But it is the good version.

Thank you so much! You are a saviour! I’ll see what I can do with my limited programming skills! :D

For my version just put the ST7789.py file in the same directory as your code. I’m not that great at python either, so wasn’t sure how/where to install it.

Oh and with a small change to the display.py script it works with a chinese version of the display (not the same pin arrangement).

diff spiral/ST7789.py spiral_chinese/ST7789.py
23a24,26
>         # Set SPI mode to 3 as we're using the Chinese LCD
>         self._spi.mode = 0b11
>
27c30
<         #GPIO.setup(self._rst,GPIO.OUT)
---
>         GPIO.setup(self._rst,GPIO.OUT)

The chinese display uses 3 pin spi and has reset defined.

I don’t know what I did but I actually don’t have that part of code.

However, your class works! The only problem I had was with “Clear” and “ShowImage” which crashed the program, but changing value from 65535 to 4096 (as in Pimoroni library) solved the problem. Thank you very much again!

Ah forgot I changed the buffer size. You can do that by changing /boot/cmdline.txt and adding :-

spidev.bufsiz=65535

You get a bit more performance out of it if you do that. Assuming its important to you.

Hi
I too had this problem and solved it by updating the spidev package, there was a reference leak or somethingg like that in the spidev with version < 3.5.
pip3 install --upgrade spidev

It was quite a search to find the reason for this so for completeness I add my findings here