jwatte
Posts: 203
Joined: Sat Aug 13, 2011 7:28 pm

Python spidev doesn't seem to send bytes?

Sun Aug 03, 2014 6:13 pm

I'm using the spidev from http://www.100randomtasks.com/simple-sp ... spberry-pi in Python.
The module is from http://github.com/doceme/py-spidev.

I can read bytes from the SPI slave just fine, but the Raspberry Pi seems to send only 0xFF (or, more likely, the MOSI is held high by some config somewhere.)

I have disconnected the slave, to make sure that it's not holding the line high -- this is the RPi not pulling it low.

And here is the output (as in, nothing,) supposed to be on the green channel in the Saleae Logic display. You can see the clock (purple line):

Image

I'm pretty sure the wiring is correct, because I can program the target device (which is an ATTiny85) using avrdude/gpio over the same connection, and when I measure directly on the RPi, I hook the green probe to the fourth-from-the-end pin (ground, clock, miso, mosi, counting backwards.)

Here is the simple test program:

Code: Select all

# module spivolt.py
import spidev
import os


class SpiVolt(object):

    def __init__(self):
        """ Open and configure the SPI device.
        """
        self.dev = spidev.SpiDev()
        self.dev.open(0, 1)
        self.dev.max_speed_hz = 500000
        self.dev.bits_per_word = 8
        self.dev.cshigh = False
        self.dev.lsbfirst = False
        self.dev.mode = 0

    def close(self):
        self.dev.close()

    def read(self):
        """ Read a voltage value from the power controller. 
            Returns a voltage value as a float, or None if not able to 
            properly communicate (re-trying may improve things).
            """
        l = self.dev.xfer([0])
        l.extend(self.dev.xfer([1]))
        l.extend(self.dev.xfer([2]))
        l.extend(self.dev.xfer([3]))
        if l[0] == 240 and l[2] == 240:
            return (l[1] + l[3]) * 0.5 * 14.19 / 256;
        if l[1] == 240 and l[3] == 240:
            return (l[0] + l[2]) * 0.5 * 14.19 / 256;
        return None

    def off(self):
        """ Send a signal to cut power (if on battery.)
            Will sync the file system first, for safety.
        """
        print "syncing to turn off..."
        os.system("sync")
        print "turning off"
        self.dev.xfer([0xAA])
        self.dev.xfer([0xAA])
        self.dev.xfer([0x55])
        self.dev.xfer([0x55])

Code: Select all

# program
import spivolt

volt = spivolt.SpiVolt()
volt.off()
A separate program that uses the "read" value works just fine, and the values that come back are correct (verified both through printing, and through looking at the pin bits.)

Why does the RPi not output anything over the MOSI line?

jwatte
Posts: 203
Joined: Sat Aug 13, 2011 7:28 pm

Re: Python spidev doesn't seem to send bytes?

Sun Aug 03, 2014 6:59 pm

This one's solved! Another utility that ran on boot had grabbed GPIO10, and when GPIO10 is exported, the SPI peripheral cannot "steal" it.
Unexporting this GPIO before opening the SPI device made it work.

Return to “Troubleshooting”