Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

RPi Zero CirccuitPython SPI problem
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

RPi Zero CirccuitPython SPI problem

by ronfischler on Sat Aug 08, 2020 2:49 pm

I attempted to use the primary SPI bus with phase=1. For a short time it worked. I had configured the SPI port as such using CircuitPython:
spi = board.SPI()
cs = digitalio.DigitalInOut(board.D17)
device = SPIDevice(spi, cs, baudrate=200000, phase=1)

As stated, it did come up working, but after a reboot, neither board.SPI() and busio.SPI() worked again to properly configure SPI, and the primary SPI port is now bricked, with mode=4, looking at it using the command line tools spi-config or spidev-test. Using the CicruitPython modules, everything appears to be running, but looking at the SCLK and MOSI lines on my oscilloscope, there is no output - both lines are silent.

I installed and tried to reset the SPI to mode to 0 using the default spidev module, I entered the following commands and got:
import spidev
spi=spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz = 200000
spi.mode = 0
spi.cshigh=False
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
SystemError: error return without exception set

I can set the max_speed_hz, but spi.cshigh is stuck returning True, and I cannot set it to False. I moved the system micro-SD card to another RPi Zero, and I get the same behavior, so I don't think any hardware was damaged, but something has been set in the system that I cannot undo.

I am at a loss; I hope somebody knows what happened here. I don't know how to reset the SPI port so that it will work again.

I think I can still use bitbangio, but that is NOT what I wanted to do.

ronfischler
 
Posts: 27
Joined: Thu Jul 23, 2020 3:15 pm

Re: RPi Zero CirccuitPython SPI problem

by adafruit_support_carter on Mon Aug 10, 2020 12:57 pm

Using the CicruitPython modules, everything appears to be running, but looking at the SCLK and MOSI lines on my oscilloscope,

What are you clocking out on the SPI bus to check activity here?

adafruit_support_carter
 
Posts: 16911
Joined: Tue Nov 29, 2016 2:45 pm

Re: RPi Zero CirccuitPython SPI problem

by ronfischler on Mon Aug 10, 2020 2:00 pm

adafruit_support_carter wrote:
Using the CicruitPython modules, everything appears to be running, but looking at the SCLK and MOSI lines on my oscilloscope,

What are you clocking out on the SPI bus to check activity here?


Thanks for your response.

Using my CircuitPython setup, I was testing using something like this:
size = 100
buf = bytearray(size)
data = bytearray(size)

buf = bytearray([0xAA for i in range(size)])

with device as dev:
while True:
dev.write_readinto(buf, data)
time.sleep(.1)

I also tried using the standard Python library spidev xfer() function, but no improvement.

ronfischler
 
Posts: 27
Joined: Thu Jul 23, 2020 3:15 pm

Re: RPi Zero CirccuitPython SPI problem

by adafruit_support_carter on Mon Aug 10, 2020 2:59 pm

Can you correlate the loss of functionality to anything?

adafruit_support_carter
 
Posts: 16911
Joined: Tue Nov 29, 2016 2:45 pm

Re: RPi Zero CirccuitPython SPI problem

by ronfischler on Mon Aug 10, 2020 4:12 pm

adafruit_support_carter wrote:Can you correlate the loss of functionality to anything?


The only thing I believe I can correlate to, is after I rebooted the RPi and tried to use the primary SPI port again, and it no longer worked, that "spi-config -d /dev/spidev0.0 -q" began reporting that the SPI mode was now set to 4! Not 0!

$ spi-config -d /dev/spidev0.0 -q
/dev/spidev0.0: mode=4, lsb=0, bits=8, speed=125000000


If I set mode = 1, spi-config reports that it is set to 5. If I set mode back to 0, spi-config reports it is back to 4.

ronfischler
 
Posts: 27
Joined: Thu Jul 23, 2020 3:15 pm

Re: RPi Zero CirccuitPython SPI problem

by ronfischler on Mon Aug 10, 2020 5:35 pm

ronfischler wrote:
adafruit_support_carter wrote:Can you correlate the loss of functionality to anything?


I note that I can still use the primary SPI pins if I use bitbangio.SPI(), phase=1 works too. I will confirm that board.SPI() and busio.SPI() are still dead, regardless of mode. If this is the case, I will start again with a fresh MicroSD card with Raspbian Buster, and see what happens with that.

ronfischler
 
Posts: 27
Joined: Thu Jul 23, 2020 3:15 pm

Re: RPi Zero CirccuitPython SPI problem

by adafruit_support_carter on Mon Aug 10, 2020 5:46 pm

Starting fresh seems like the easiest thing to try. It may be that some of these other utility programs are running interference with each other. Not sure.

Once you've reached the point of passing the basic Blinka test:
https://learn.adafruit.com/circuitpytho ... 3030038-17
try running something like this to test SPI output:
Code: Select all | TOGGLE FULL SIZE
import board
import digitalio

spi = board.SPI()

buf = bytearray([0xAA]*100)

spi.try_lock()
spi.write(buf)
spi.unlock()

adafruit_support_carter
 
Posts: 16911
Joined: Tue Nov 29, 2016 2:45 pm

Re: RPi Zero CirccuitPython SPI problem

by ronfischler on Fri Aug 14, 2020 2:05 pm

adafruit_support_carter wrote:Starting fresh seems like the easiest thing to try. It may be that some of these other utility programs are running interference with each other. Not sure.

Once you've reached the point of passing the basic Blinka test:
https://learn.adafruit.com/circuitpytho ... 3030038-17
try running something like this to test SPI output:
Code: Select all | TOGGLE FULL SIZE
import board
import digitalio

spi = board.SPI()

buf = bytearray([0xAA]*100)

spi.try_lock()
spi.write(buf)
spi.unlock()



I started fresh as suggested: fresh OS image, fresh CircuitPython libraries, but a minimal installation, and now the Primary SPI bus works, mode=1. I note that Blinka was updated since the last time I tried this, and this time I did NOT install the various utilities I had before, such as spi-config, spidev-test, and pigpio. I don't know what had killed the SPI port prior, it remains a mystery, but at least I know that it can be made to work, using mode 1! Thank you for you help.

My test code:

Code: Select all | TOGGLE FULL SIZE
#  SPI Test 5...
import board
import busio
import digitalio
from adafruit_bus_device.spi_device import SPIDevice

# spi = board.SPI()
spi = busio.SPI(clock=board.SCK, MOSI=board.MOSI, MISO=board.MISO)
cs = digitalio.DigitalInOut(board.D17)
device = SPIDevice(spi, cs)

buf = bytearray([0xAA]*100)

while not spi.try_lock(): pass
spi.configure(baudrate=200000, phase=1)
spi.unlock()

with device:
    for i in range(10000):
        spi.write(buf)

ronfischler
 
Posts: 27
Joined: Thu Jul 23, 2020 3:15 pm

Please be positive and constructive with your questions and comments.