gadgetoid
Posts: 152
Joined: Wed Mar 07, 2012 9:58 pm

Read this if you use Python Spidev

Thu Jan 29, 2015 10:53 pm

This message is important for people using the SpiDev package from PyPi.

https://pypi.python.org/pypi/spidev

I've seen a few tutorials advocating it's use and I use it in dot3k because it's fast and reliable so I know it's out there.

I noticed the package was both hideously out of date and, more recently, incompatible with kernels >= 3.15+. It most definitely breaks with the most recent bleeding-edge firmware, resulting in "invalid argument" errors when using ".xfer". To pre-empt everything breaking with the next kernel release I've requested and received ownership of this package ( under the trustworthy banner of the good ship Pimoroni! ) and plan to update it very soon.

I plan to review and hopefully include the useful pull requests contributed against this package on GitHub, too, and ensure Python 2/3 support with the help of the current GitHub maintainer.

The GitHub repo is here: https://github.com/doceme/py-spidev

If anyone is out there using it, and has a moment to test the latest code base, it would be very helpful!

Thanks!

User avatar
DougieLawson
Posts: 40254
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Read this if you use Python Spidev

Fri Jan 30, 2015 12:29 am

https://github.com/piface/libmcp23s17/issues/8 might interest you.

Change your code at line 218

Code: Select all

struct spi_ioc_transfer xfer;
to

Code: Select all

struct spi_ioc_transfer xfer;
memset(&xfer, 0, sizeof(xfer));
and the same wherever you've got a spi_ioc_transfer structure.

I'll take a closer look tomorrow.
Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

gadgetoid
Posts: 152
Joined: Wed Mar 07, 2012 9:58 pm

Re: Read this if you use Python Spidev

Fri Jan 30, 2015 9:59 am

Thank, you. This is definitely interesting- there are a few such instances in the code which look like they could use some attention.

I'm currently working in my fork to create a stable version of spidev which I will test against current and bleeding-edge Raspbian kernels, so I'll work this change in for testing.

The memset line, in these instances, is ensuring that these buffers are initialized to a zero'd and known value? I see a malloc call in the code which looks like it might need the same treatment:

Code: Select all

xferptr = (struct spi_ioc_transfer*) malloc(sizeof(struct spi_ioc_transfer) * len);

gadgetoid
Posts: 152
Joined: Wed Mar 07, 2012 9:58 pm

Re: Read this if you use Python Spidev

Fri Jan 30, 2015 2:52 pm

I have started compiling an updated and patched version of py-spidev in my own fork here:

https://github.com/Gadgetoid/py-spidev

I'd love feedback from users of this library, whether or not you're using the new kernel, particularly those with read/write SPI devices since I've not tested any of the read functionality yet.

You should also be able to use this version in Python 3.

seanspins211
Posts: 2
Joined: Wed Dec 10, 2014 5:22 am

Re: Read this if you use Python Spidev

Fri Jan 30, 2015 8:21 pm

I am new to py-spidev. I have been trying to use it to communicate between RPi and Arduino. I have started by following this tutorial: http://blog.riyas.org/2014/08/raspberry ... s.html?m=1.

I am currently getting the "Invalid Argument" error. I am will to try any experimental version of spidev. I am new to all of this, so I will need a little coaching :)

Thanks!

Sean

gadgetoid
Posts: 152
Joined: Wed Mar 07, 2012 9:58 pm

Re: Read this if you use Python Spidev

Fri Jan 30, 2015 8:49 pm

In a terminal try:

Code: Select all

git clone https://github.com/Gadgetoid/py-spidev
cd py-spidev
sudo make install

User avatar
DougieLawson
Posts: 40254
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Read this if you use Python Spidev

Fri Jan 30, 2015 10:35 pm

I tried it and didn't get any obvious errors. But, my hastily crafted program didn't work (I'll have to revert to the old 3.12.33 kernel that I've kept and get that piece tested).
Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

seanspins211
Posts: 2
Joined: Wed Dec 10, 2014 5:22 am

Re: Read this if you use Python Spidev

Sat Jan 31, 2015 12:07 am

I no longer receive the invalid argument error. Everything seems to work as expected. Thank you for your help!

paulv
Posts: 564
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: Read this if you use Python Spidev

Sat Jan 31, 2015 4:00 pm

Hi gadgetoid,

I'm getting the following warnings when I run the sudo make install.
BTW, it is the same with the 2.0 Version.
Maybe you can have a look at what is going on?

Code: Select all

pi@TestRig ~/py-spidev $ sudo make install
python setup.py install
running install
running build
running build_ext
building 'spidev' extension
creating build
creating build/temp.linux-armv6l-2.7
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/src/linux/include -I/usr/include/python2.7 -c spidev_module.c -o build/temp.linux-armv6l-2.7/spidev_module.o
spidev_module.c:35:0: warning: "PyLong_Check" redefined [enabled by default]
/usr/include/python2.7/longobject.h:14:0: note: this is the location of the previous definition
spidev_module.c:888:1: warning: return type defaults to âintâ [-Wreturn-type]
spidev_module.c: In function âinitspidevâ:
spidev_module.c:897:3: warning: âreturnâ with no value, in function returning non-void [-Wreturn-type]
creating build/lib.linux-armv6l-2.7
gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro build/temp.linux-armv6l-2.7/spidev_module.o -o build/lib.linux-armv6l-2.7/spidev.so
running install_lib
copying build/lib.linux-armv6l-2.7/spidev.so -> /usr/local/lib/python2.7/dist-packages
running install_egg_info
Writing /usr/local/lib/python2.7/dist-packages/spidev-3.0.egg-info
I'm using this on:

Code: Select all

pi@TestRig ~/py-spidev $ uname -a
Linux TestRig 3.12.33+ #724 PREEMPT Wed Nov 26 17:55:23 GMT 2014 armv6l GNU/Linux
I did use today's update/upgrade/dist-upgrade, and I'm using Python 2.7.

The bigger issue I have is that when I tried to run my DHT22 test program ( look at this post: http://www.raspberrypi.org/forums/viewt ... 6&p=640039, it did not return any data from the DHT.
I have no time to look into it further for a few days, but maybe you have an idea what the problem could be.

Tks,
Paul

dkruchala
Posts: 2
Joined: Sun Jan 25, 2015 5:00 pm

Re: Read this if you use Python Spidev

Wed Feb 04, 2015 11:14 pm

Hi, i try to install your package but when i start i get something like this

Code: Select all

running install
running build
running build_ext
building 'spidev' extension
creating build
creating build/temp.linux-armv6l-2.7
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/src/linux/include -I/usr/include/python2.7 -c spidev_module.c -o build/temp.linux-armv6l-2.7/spidev_module.o
In file included from spidev_module.c:25:0:
/usr/include/linux/spi/spidev.h:5:1: error: expected identifier or ‘(’ before ‘<’ token
In file included from spidev_module.c:25:0:
/usr/include/linux/spi/spidev.h:13:5: error: stray ‘\302’ in program
/usr/include/linux/spi/spidev.h:13:5: error: stray ‘\267’ in program
/usr/include/linux/spi/spidev.h:13:5: error: stray ‘\302’ in program
/usr/include/linux/spi/spidev.h:13:5: error: stray ‘\267’ in program
In file included from spidev_module.c:25:0:
/usr/include/linux/spi/spidev.h:287:46: error: invalid suffix "af8a2e1dd3049f54fc67f19d0ad1" on integer constant
In file included from spidev_module.c:25:0:
/usr/include/linux/spi/spidev.h:377:3: error: expected identifier or ‘(’ before ‘<’ token
/usr/include/linux/spi/spidev.h:500:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:504:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:513:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:513:134: error: expected identifier or ‘(’ before ‘<’ token
/usr/include/linux/spi/spidev.h:513:179: error: expected identifier or ‘(’ before ‘<’ token
/usr/include/linux/spi/spidev.h:539:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:543:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:552:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:556:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:560:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:564:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:573:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:577:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:581:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:585:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:589:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:593:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:620:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:620:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:620:177: error: expected identifier or ‘(’ before ‘<’ token
/usr/include/linux/spi/spidev.h:620:177: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:620:211: error: expected identifier or ‘(’ before ‘<’ token
/usr/include/linux/spi/spidev.h:840:57: error: expected identifier or ‘(’ before ‘<’ token
/usr/include/linux/spi/spidev.h:853:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:853:9: error: stray ‘\’ in program
/usr/include/linux/spi/spidev.h:857:158: error: expected identifier or ‘(’ before ‘<’ token
/usr/include/linux/spi/spidev.h:857:158: error: stray ‘\’ in program
/usr/include/linux/spi/spidev.h:865:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:883:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:887:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:900:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:904:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:917:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:921:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:934:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:938:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:957:9: error: stray ‘#’ in program
/usr/include/linux/spi/spidev.h:1001:18: error: expected identifier or ‘(’ before numeric constant
spidev_module.c:35:0: warning: "PyLong_Check" redefined [enabled by default]
/usr/include/python2.7/longobject.h:14:0: note: this is the location of the previous definition
spidev_module.c: In function ‘SpiDev_xfer’:
spidev_module.c:219:26: error: storage size of ‘xfer’ isn’t known
spidev_module.c:300:2: warning: implicit declaration of function ‘SPI_IOC_MESSAGE’ [-Wimplicit-function-declaration]
spidev_module.c:320:19: error: ‘SPI_CS_HIGH’ undeclared (first use in this function)
spidev_module.c:320:19: note: each undeclared identifier is reported only once for each function it appears in
spidev_module.c:219:26: warning: unused variable ‘xfer’ [-Wunused-variable]
spidev_module.c: In function ‘SpiDev_xfer2’:
spidev_module.c:345:26: error: storage size of ‘xfer’ isn’t known
spidev_module.c:401:19: error: ‘SPI_CS_HIGH’ undeclared (first use in this function)
spidev_module.c:345:26: warning: unused variable ‘xfer’ [-Wunused-variable]
spidev_module.c: In function ‘__spidev_set_mode’:
spidev_module.c:411:16: error: ‘SPI_IOC_WR_MODE’ undeclared (first use in this function)
spidev_module.c:415:16: error: ‘SPI_IOC_RD_MODE’ undeclared (first use in this function)
spidev_module.c: In function ‘SpiDev_get_mode’:
spidev_module.c:428:55: error: ‘SPI_CPHA’ undeclared (first use in this function)
spidev_module.c:428:66: error: ‘SPI_CPOL’ undeclared (first use in this function)
spidev_module.c: In function ‘SpiDev_get_cshigh’:
spidev_module.c:438:19: error: ‘SPI_CS_HIGH’ undeclared (first use in this function)
spidev_module.c: In function ‘SpiDev_get_lsbfirst’:
spidev_module.c:452:19: error: ‘SPI_LSB_FIRST’ undeclared (first use in this function)
spidev_module.c: In function ‘SpiDev_get_3wire’:
spidev_module.c:466:19: error: ‘SPI_3WIRE’ undeclared (first use in this function)
spidev_module.c: In function ‘SpiDev_get_loop’:
spidev_module.c:480:19: error: ‘SPI_LOOP’ undeclared (first use in this function)
spidev_module.c: In function ‘SpiDev_set_mode’:
spidev_module.c:516:25: error: ‘SPI_CPHA’ undeclared (first use in this function)
spidev_module.c:516:36: error: ‘SPI_CPOL’ undeclared (first use in this function)
spidev_module.c: In function ‘SpiDev_set_cshigh’:
spidev_module.c:541:22: error: ‘SPI_CS_HIGH’ undeclared (first use in this function)
spidev_module.c: In function ‘SpiDev_set_lsbfirst’:
spidev_module.c:568:22: error: ‘SPI_LSB_FIRST’ undeclared (first use in this function)
spidev_module.c: In function ‘SpiDev_set_3wire’:
spidev_module.c:595:22: error: ‘SPI_3WIRE’ undeclared (first use in this function)
spidev_module.c: In function ‘SpiDev_set_loop’:
spidev_module.c:622:22: error: ‘SPI_LOOP’ undeclared (first use in this function)
spidev_module.c: In function ‘SpiDev_set_bits_per_word’:
spidev_module.c:665:23: error: ‘SPI_IOC_WR_BITS_PER_WORD’ undeclared (first use in this function)
spidev_module.c: In function ‘SpiDev_set_max_speed_hz’:
spidev_module.c:701:23: error: ‘SPI_IOC_WR_MAX_SPEED_HZ’ undeclared (first use in this function)
spidev_module.c: In function ‘SpiDev_open’:
spidev_module.c:754:22: error: ‘SPI_IOC_RD_MODE’ undeclared (first use in this function)
spidev_module.c:759:22: error: ‘SPI_IOC_RD_BITS_PER_WORD’ undeclared (first use in this function)
spidev_module.c:764:22: error: ‘SPI_IOC_RD_MAX_SPEED_HZ’ undeclared (first use in this function)
spidev_module.c: At top level:
spidev_module.c:888:1: warning: return type defaults to ‘int’ [-Wreturn-type]
spidev_module.c: In function ‘initspidev’:
spidev_module.c:897:3: warning: ‘return’ with no value, in function returning non-void [-Wreturn-type]
error: command 'gcc' failed with exit status 1
Help PLZ

Ok.Problem solved. And your ver of py-spidev works well on my PI. THANKS!!

kzxc
Posts: 5
Joined: Sat Feb 21, 2015 3:08 pm

Re: Read this if you use Python Spidev

Thu Feb 26, 2015 1:41 am

Hello,

I would like to ask what is the current status about spidev.
I am asking this because I believe that this post is closely related to this one: http://www.raspberrypi.org/forums/viewt ... r2#p702200 and as far as I can understand, this is a problem on the spidev code when using the xfer and xfer2 functions.

Regards

tdicola
Posts: 9
Joined: Wed Sep 26, 2012 5:54 am

Re: Read this if you use Python Spidev

Tue Mar 17, 2015 3:12 am

I think I've found another issue with spidev, even using the latest 3.0 version with gadgetoid's fixes. It seems when I enable LSB first mode all other access to GPIO (through the RPi.GPIO library) fails with a Error 22 invalid argument error.

Here's a very simple test program that opens /dev/spidev0.0 and sets GPIO23 high and low for a second:

Code: Select all

import time

import RPi.GPIO as GPIO
import spidev

spi = spidev.SpiDev()
spi.open(0, 0)
spi.lsbfirst = True

# Initialize GPIO23 as an output.
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.OUT)

# Set GPIO23 high and then low.
print 'High'
GPIO.output(23, GPIO.HIGH)
time.sleep(1)

print 'Low'
GPIO.output(23, GPIO.LOW)
time.sleep(1)

print 'Done!'
Run that and it will fail as soon as it tries to setup the GPIO with this error:

Code: Select all

Traceback (most recent call last):
  File "test2.py", line 13, in <module>
    GPIO.setup(23, GPIO.OUT)
IOError: [Errno 22] Invalid argument
However, go in and comment the line 'spi.lsbfirst = True', run it again, and it works perfectly with this output:

Code: Select all

High
Low
Done!
Definitely seems like something is broken with LSB first mode. Very odd that it seems to break other access to GPIO.

If it helps too, I'm using python spidev version 3.0 (when I run pip freeze I see 'spidev==3.0') on a Pi 1 but it also repros on a Pi 2. Both are using the February 16th 2015 image which uses the 3.18 kernel. I don't remember seeing failures like this on the older pre-3.15 kernel but I haven't tried it.

Has anyone else run into this issue?

parikshitvarade
Posts: 8
Joined: Tue Apr 14, 2015 10:25 pm

Re: Read this if you use Python Spidev

Sun Apr 19, 2015 12:47 am

hi i m using spidev to interface MAX186 ADC with raspberry pi. but thing I sometimes get output from some channels of my ADC ic and sometimes no date from any channel. can anyone please help

User avatar
joan
Posts: 15124
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Read this if you use Python Spidev

Sun Apr 19, 2015 7:30 am

parikshitvarade wrote:hi i m using spidev to interface MAX186 ADC with raspberry pi. but thing I sometimes get output from some channels of my ADC ic and sometimes no date from any channel. can anyone please help
For a new question it's usually best to start a new post.


User avatar
DougieLawson
Posts: 40254
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Read this if you use Python Spidev

Tue May 05, 2015 9:25 pm

Doceme is newer code.
github wrote: This branch is 5 commits behind doceme:master
Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

ezeakeal
Posts: 2
Joined: Wed Jun 17, 2015 5:28 pm

Re: Read this if you use Python Spidev

Wed Jun 17, 2015 5:33 pm

tdicola wrote: Has anyone else run into this issue?
Yup. It's not just with GPIO.
It's with any command following the lsbfirst command.

Can anyone share their pin mapping and arduino code?

I am sending some characters over SPI.
I am printing the characters and monitoring this on the serial monitor through the arduino IDE.
Things seem fine for a while, but eventually something is triggered and it just receives corrupted characters.
This behavior eventually subsides, but there is no repeatable steps to achieve this. Sometimes I just wait a second or two.

ezeakeal
Posts: 2
Joined: Wed Jun 17, 2015 5:28 pm

Re: Read this if you use Python Spidev

Wed Jun 17, 2015 7:01 pm

Hope I am replying to my own reply now.. I am new here.

Anyway, I found the issue. It was the speed!
I was getting seemingly random responses, but I set the speed as follows and now I am making some progress:

Code: Select all

spi.max_speed_hz = 50000 

gadgetoid
Posts: 152
Joined: Wed Mar 07, 2012 9:58 pm

Re: Read this if you use Python Spidev

Mon Aug 22, 2016 3:27 pm

I hate to resurrect an old thread, but since it comes up on Google searches for "lsbfirst" and "SPI_LSB_FIRST" I figured I'd best post the answer here.

I've been working on a fix for the error leaking from lsbfirst, but the underlying cause is that this operation simply isn't supported on the Raspberry Pi- as far as I can tell.

Attempting to set the SPI_LSB_FIRST bit via an ioctl on the SPI device just results in an "Invalid Argument"

This is a Pi hardware (or kernel driver?) issue, and not a problem with the spidev library.

The error leaking out into other method calls, and appearing to break stuff like RPi.GPIO, is documented exhaustively here: https://github.com/doceme/py-spidev/issues/18

User avatar
DougieLawson
Posts: 40254
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Read this if you use Python Spidev

Tue Aug 23, 2016 5:17 am

gadgetoid wrote: Attempting to set the SPI_LSB_FIRST bit via an ioctl on the SPI device just results in an "Invalid Argument"
If there's a bug in the kernel SPI driver have you opened an issue for it at https://github.com/raspberrypi/linux/issues if it can't handle a function it should fail gracefully.
Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

maurik
Posts: 2
Joined: Fri Apr 14, 2017 6:35 pm

Re: Read this if you use Python Spidev

Fri Apr 14, 2017 6:38 pm

Is is possible to set the bit count to something other than 8-bits for a Raspberry Pi?

When I try I get:

Code: Select all

In [43]: dev0.bits_per_word=12
In [44]: dev0.writebytes([1,2,3])
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-44-17c60470c890> in <module>()
----> 1 dev0.writebytes([1,2,3])
IOError: [Errno 22] Invalid argument

Return to “Python”