Why is python3 stopping without an error message durring a spidev xfer2 call?

2018-04-15 00:19:03

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 s

  • 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

    2018-04-15 00:52:19
  • 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!!

    2018-04-15 01:24:19
  • can you post your entire code?

    2018-04-15 01:57:38