0

I need to use SPI for a project I'm working on with a Raspberry PI, but the little bit of test code I wrote doesn't seem to work.

#test program for spi

import spidev
import RPi.GPIO as GPIO
from time import sleep

bus = 0                     #always 0, apparently
device = 0                  #we will use our own CS pin

mode = 3                    #see SPI modes
maxSpeed = 100000           #maximum clock speed in Hz
noCs = True                 #hardware CS
lsbFirst = False            #MSB first is default

##############################################################
CS_PIN = 24                 #CS pin number
WRITE_DATA = [0x4D, 0x4D, 0x4D]
##############################################################


if __name__ == '__main__':
    #config GPIO
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(CS_PIN, GPIO.OUT)
    GPIO.setwarnings(False)
    
    GPIO.output(CS_PIN, True)
    
    #config SPI
    spi = spidev.SpiDev()
    spi.open(bus, device)
    spi.mode = mode
    spi.no_cs = noCs
    spi.lsbfirst = lsbFirst
    spi.max_speed_hz = maxSpeed

    #run SPI
    GPIO.output(CS_PIN, False)
    spi.xfer(WRITE_DATA)
    GPIO.output(CS_PIN, True)
    sleep(0.1)
    #print data
    '''
    listLen = len(readbytes)
    print("Lenght: ", listLen)

    i = 0
    while i < listLen:
        print("Byte ", i, ": ", readbytes[i])
        i += 1
    '''
    GPIO.cleanup()

I'm using an external CS because I need to connect multiple devices to this SPI bus. I probed the SPI signals with an oscilloscope. I see the CS go low and then high, but there is no switching activity on SCK, MOSI, or MISO, they just stay high.

I am using the Adafruit T-Cobbler Plus for RPi, and I am probing the correct pins (they shouldn't be high if they weren't the correct pins.

Can anyone help me with this?

2 Answers 2

0

The python code you listed will run then immediately stop and restore GPIO to initial state.

2
  • The code itself is not particularly useful. I wrote it simply to test SPI. It should also write three bytes of data. I am not seeing any clock or write.
    – AMacDonald
    Commented Jun 23, 2023 at 13:56
  • To clarify, I used an oscilloscope and set a trigger in order to capture SCK/MOSI.
    – AMacDonald
    Commented Jun 23, 2023 at 15:00
0

You need to enable SPI before you can use it. You can enable SPI using the raspi-config utility (sudo raspi-config from the command line). You may also need to add some configuration steps in /boot/config.txt depending on exactly what you want to do.

1
  • I'm aware of this, and I enabled it prior to testing.
    – AMacDonald
    Commented Jun 28, 2023 at 2:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.