1

I am having a problem with a python3 script where xfer2() from spidev fails with no warning or error message.

Background:

  • I am using Raspbian 8.0
  • I am trying to use the spi bus to talk to an MCP3008
  • When I do an ls /dev I see the spidev0.0 and spidev0.1 drivers so spi is enabled.
  • I have tried several different versions of the python wrapper for spidev.

The following code is simplified/minimized to just show the problem:

import sys
import spidev
print('Starting test code')
print('initializing the spi bus instance')
spi_bus = spidev.SpiDev()
print('opening the spi bus')
spi_bus.open(0, 0)
print('getting the adc output ')
adc_output = spi_bus.xfer2(1, 240, 0 )

# The program never gets to the following print statement.
# Instead it just exits and returns to the prompt with no error statement

print('adc read complete')

This is what I get when I run the python script:

pi@raspberrypi: ~ software $sudo python3 spi_testcode.py
starting test code
initializing the spi bus instance
opening the spi bus
getting the adc output
pi@raspberrypi: ~ software $

Notice the final print statement is never executed. The script just stops with no error and no warning during the xfer2() call. Without some kind of an error message it is hard to debug this. Any suggestions are welcome.

  • As I suggested on raspberrypi.org - don't use xfer2. – joan Mar 4 '16 at 9:20
  • This code is following many examples on the web that explain how to use spi to communicate to the MXP3008. With this device, xfer2() is necessary in order for the mcp3008 see the 3 bytes as a start bit followed by a command. (xfer2() does not release the chip select between bytes). Having said that, I tried the code using xfer() and got the same result. – Paul Rygaard Mar 4 '16 at 16:13
  • I don't use spidev. I really don't believe xfer2 is correct - you are not using multiple single byte transfers, you are using a single multiple byte transfer (spot the subtle difference). – joan Mar 4 '16 at 16:49
  • Joan.... you spotted the subtle difference that was the problem. My code was passing 3 decimal bytes without brackets: xfer2(1,240,0) I needed to add brackets. The following works xfer2([1,240,0]) xfer2() is looking for a list but I was passing 3 separate variables. The embarrassing thing: The code examples on the web that I was following had the brackets!! I visually compared my code to the examples several times but never noticed that difference..... :-(. It is too bad the spidev code did not flag the error better Problem solved.... Thanks!! – Paul Rygaard Mar 4 '16 at 17:55
  • I'm afraid I did not spot that error! It still shouldn't crash though - as it does on my Pis. – joan Mar 4 '16 at 18:05
2

I have run your code on both a Pi2B and a Pi3B. On both I get a segmentation fault with spidev. I don't know why. I'm not sure where I sourced those versions from (they may be part of the raspberrypi.org image at the moment).

Just checked, part of the raspberrypi.org image.

apt-cache policy python-spidev
python-spidev:
  Installed: 2.0~git20150907
  Candidate: 2.0~git20150907
  Version table:
 *** 2.0~git20150907 0
        500 http://archive.raspberrypi.org/debian/ jessie/main armhf Packages
        100 /var/lib/dpkg/status

I suggest you raise an issue (not sure where).

My pigpio Python module has an alternative SPI implementation (spi_open, spi_xfer, spi_close).

From the command line

$ sudo pigpiod
$ pigs spio 1 1000000 256 # my MCP3008 is on channel 1 of auxiliary SPI
0                         # returns handle 0
$ pigs spix 0 1 240 0     # I don't have anything connected to channel 7
3 0 0 51
$ pigs spix 0 1 240 0
3 0 0 210
$ pigs spix 0 1 240 0
3 0 0 225
$ pigs spix 0 1 128 0 # channel 0
3 0 2 210
$ pigs spix 0 1 128 0
3 0 2 209
$ pigs spix 0 1 128 0
3 0 2 210
$ 
0

With some help of Clem over at Element14 and a hint from joan here I solved the problem.

My code was passing 3 decimal bytes without brackets:

 xfer2(1,240,0)

I discovered I needed to add brackets. The following seems to work:

 xfer2([1,240,0]) 

So.... xfer2() is looking for a list but I was passing 3 separate variables.

Now the embarrassing thing: The code examples on the web that I was following had the brackets!! I had visually compared my code to the examples several times but never noticed that difference..... :-(.

I caused the problem but it is too bad the spidev code did not flag the error better

Problem solved.... now I am able to move forward with the build.

Thanks!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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