#ST7735R example, object has no attribute 'SPI'

1 messages · Page 1 of 1 (latest)

proper token
#

I'm trying to run some sample code provided by Adafruit, but it refuses to run stating that the 'module' object has no attribute 'SPI'.

#

Example code is attached. Copied straight from the ST7735R library provided by Adafruit.

#

It seems there's something wrong with how it's defining SPI in the board file.

#

I cannot get this to run in Thonny, PyCharm, or VS Code.

proper token
#

I've downloaded the latest Thonny 4.0.2, which now seems to install plugins and packages properly. I manually copied over the Adafruit_display_text and st7735r drivers to the Pico clone. It keeps throwing this error:

Traceback (most recent call last):
File "<stdin>", line 18, in <module>
AttributeError: 'module' object has no attribute 'SPI'

#

It happens right at line 16:
spi = board.SPI()

wet jackal
#

Some boards have built in SPI interface. Others need you to define it like:

import busio

spi = busio.SPI(board.SCK, MISO=board.MISO)
#

What board are you using?

proper token
#

It's a Waveshare RP2040-LCD-0.96 board, which is a Pico clone.

wet jackal
#
import board
import busio

spi = busio.SPI(clock=board.GP2, MOSI=board.GP3, MISO=board.GP4)
proper token
#

Okay, that seems to be exactly what I'm looking for! I have dozens of tabs open in my browser hunting for info on this stuff, and you're the first one to mention this. It seems to be exactly what I'm running into.

#

Thanks! I'll give this a try.

wet jackal
#

That page explains that the Pico does not have preset interfaces for I2C, SPI and UART. Because everything is GPIO.

proper token
#

I noticed that. Very interesting. It also makes sense, as the Pico has multiple pins that can act as SPI, I2C, or UART pins.

#

So you have to choose which ones you need.

wet jackal
#

Right

#

Very flexible. But...

proper token
#

A little odd that the example code doesn't follow the one on the page you listed...

wet jackal
#

With great power, comes great responsibility.

#

Most Adafruit boards have a built-in SPI and I2C

proper token
#

I wonder if the spi = board.SPI() could accept the variables you mentioned and pass them to the SPI code in the board.py...

#

board.py mentions it checks for local variables.

#

if "SCLK" in locals() and "MOSI" in locals() and "MISO" in locals():

def SPI():
    """The singleton SPI interface"""
    import busio

    return busio.SPI(SCLK, MOSI, MISO)
wet jackal
#

But those aren't set either.

#

Because there are multiple options.

proper token
#

But if I supply the pins in the main code to the board.SPI() function, does it pass it down into board.py's SPI function to check and assign the pins?

#

(If you can't tell, I'm VERY green at this...)

wet jackal
#

Are you defining your own board?

proper token
#

No, I'm not.

wet jackal
#

board is built in to CP for each different type

proper token
#

I see board.py checks for the board type automatically, so it can grab pin assignments and such.

#

I'm just confused because the SPI code in the provided example with the ST7735R library doesn't match the SPI example on the Adafruit page you shared.

wet jackal
#

Because most boards have a predefined SPI. If you do:

import board
dir(board)
``` in REPL, you will see the predefined pins.
proper token
#

The example uses board.SPI() and the example you shared uses busio.SPI(XYZ).

#

(Sub XYZ for the pin assignments.)

wet jackal
#

You can replace the line in the example with the line I mentioned.

#

Everything else should work.

proper token
#

Great, I'll give that a shot. Thanks for being patient while I wrap my head around this.

wet jackal
#

But I recall that you have a board with a built in TFT. So, you may need to double check the data sheet to make sure what pins that is connected to.

proper token
#

Yep, the pins used are different.

#

I think they also use one pin that switches for MOSI/MISO, rather than two separate pins... so, we'll see if it still works.

wet jackal
#

The ones with the red dotted line

proper token
#

CLK is straightforward.

#

DC is the data/command pin, I believe.

wet jackal
#

Sorry. I have to go.

#

You are on the right track.

proper token
#

Thanks again 🙂

#

Holy CRAP, it works!

#

For reference, this board does NOT use a MISO/SDI pin, it just uses CLK, MOSI/SDO and DC to communicate.