AimSkyward
Posts: 20
Joined: Sun Jun 23, 2013 10:01 am

Python, SPI and spidev

Fri Sep 27, 2013 9:12 am

I am trying to put together the SPI interface on the Rpi and am having a little trouble getting it all to work. So far I have followed this procedure as suggested on various posts:

Code: Select all

/etc/modprobe.d/raspi-blacklist.conf
I have commented out the blacklisted spi-bcm2708 and rebooted. I checked that spi_bcm2708 is present using ismod and all is good. To ensure the Rpi is up to date I did:

Code: Select all

sudo apt-get update
and then installed python-dev:

Code: Select all

sudo apt-get install python-dev
Finally I installed the python SPI-wrapper to enable access to the port:

Code: Select all

mkdir python-spi
cd python-spi
wget https://raw.github.com/doceme/py-spidev/master/setup.py
wget https://raw.github.com/doceme/py-spidev/master/spidev_module.c
sudo python setup.py install
I have checked that all is where it should be and everything is there. I then put together a piece of test code to check the connection by handshaking the MISO and MOSI pins on the Rpi.

Code: Select all

import spidev
import time
spi = spidev.SpiDev()
spi.open(0,0)
while True:
   resp = spi.xfer2([0x00])
   print resp[0]
   time.sleep(1)
When I run this code I get the following error:
Traceback (most recent call last):
File "/var/www/python-spi/Tester1.py", line 5, in <module>
spi.open(0,0)
IOError: [Errno 13] Permission Denied
I have looked at the permissions on the spidev0.0 and it is for the 'root' crw. As as user would I need to give myself permission on this to get it working when running the code such as:

Code: Select all

sudo chmod 666 /dev/spidev0.0
or is there something else I am missing? Any help would be appreciated.

User avatar
TonyD
Posts: 445
Joined: Thu Sep 08, 2011 10:58 am
Location: Newcastle, UK
Contact: Website

Re: Python, SPI and spidev

Fri Sep 27, 2013 1:31 pm

Silly question but when you run your program did you do a

sudo chmod +x progname.py

then

sudo ./progname.py
Tony

AimSkyward
Posts: 20
Joined: Sun Jun 23, 2013 10:01 am

Re: Python, SPI and spidev

Fri Sep 27, 2013 3:16 pm

Hi TonyD,

Not a silly question at all. I am currently trying to run it from IDLE, however, ultimately I want to make this into an executable which I guess would mean I run it from the command line? As you can probably guess I am new to Python and Unix.
I tried what you suggested and got the following response:
./Tester1.py: 1: ./Tester1.py: import: not found
./Tester1.py: 2: ./Tester1.py: import: not found
./Tester1.py: 4: ./Tester1.py: Syntax error: "(" unexpected

Not quite sure what to make of this as I have checked and double checked the syntax and have used the same imported modules in other code and have not had the error messages.
Cheers

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Python, SPI and spidev

Sat Sep 28, 2013 10:29 pm

try this

python Tester1.py

If that works make sure you have

#!/usr/bin/python

in the first line of your scripts.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

AimSkyward
Posts: 20
Joined: Sun Jun 23, 2013 10:01 am

Re: Python, SPI and spidev

Sun Sep 29, 2013 12:31 pm

Hi Richard-TX,

Thanks for your help. I ended up being able to get it running through terminal as an executable (as suggested by TonyD) so I will stick with that I think as in the long term it's what I will need to do anyway. Thanks for your advice though.

User avatar
TonyD
Posts: 445
Joined: Thu Sep 08, 2011 10:58 am
Location: Newcastle, UK
Contact: Website

Re: Python, SPI and spidev

Mon Sep 30, 2013 12:31 pm

from a command line/terminal, try:

sudo idle

once Idle is running you should be able to run your program and not get the error messages
Tony

junkert
Posts: 1
Joined: Mon Jun 02, 2014 10:56 am

Re: Python, SPI and spidev

Mon Jun 02, 2014 11:04 am

You may be interested in my latest blog post. I explain how to use SPIDEV with the Raspberry Pi by example:
http://levi.etn.fm/blog/2014/06/02/how- ... pberry-pi/

Not sure if this answer all of your questions, but it may be a good start.

mikeh32217
Posts: 3
Joined: Fri Oct 09, 2015 4:36 pm

Re: Python, SPI and spidev

Fri Oct 09, 2015 4:41 pm

I am trying to do the; sudo python setup.py install
and am receiving an error that the compiler cannot find the Python.h file.
I looked at the /usr/include directories and all the necessary .h files are in the python3 directory but not in the python directory.

Can this be built for Python2.7?

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Python, SPI and spidev

Tue Dec 01, 2015 1:52 pm

A bit late but just in case...
mikeh32217 wrote:I am trying to do the; sudo python setup.py install
and am receiving an error that the compiler cannot find the Python.h file.
I looked at the /usr/include directories and all the necessary .h files are in the python3 directory but not in the python directory.

Can this be built for Python2.7?
Should be possible - you don't seem to have the Python development library installed for Python 2 - something like sudo apt-get install python-dev should help?
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

Raspnew
Posts: 4
Joined: Wed Dec 09, 2015 2:08 am

Re: Python, SPI and spidev

Wed Dec 09, 2015 2:52 am

I'm new on the Raspberry Pi and I'm finding it fascinating - have some LEDs blinking etc, however all the controls are done through the GPIO ports by toggling the pins ON/OFF.
To be able to do more I decided to get spidev and try to use it, however I'm stuck!
Went to the 100RandomTasks blog, on the "Simple SPI on Raspberry PI" page and on the first instruction - modify /etc/modprobe.d/raspi-blacklsit.conf - I had problem because I can not find raspi-blacklist.conf under /etc/modprobe.d.
The only files I find in this folder are: alsa-base-blacklist.conf and fbdev-blacklist.conf, and none of them have spi-bcm2708 blacklisted.
By issuing the lsmod command on the terminal I do not see the SPI as installed device (no reference to spi-bcm2708).
I had no problem following the rest of the instructions and copied the example, which give the error "IOError: [Errno 2] no such file or directory" when trying to open the SPI 0,0.

As you can see, I'm lost on the first step. Any suggestion on what to do?

User avatar
DougieLawson
Posts: 35156
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Python, SPI and spidev

Wed Dec 09, 2015 8:56 am

Have you added

Code: Select all

dtparam=i2c=on,spi=on
or

Code: Select all

dtparam=spi=on
to /boot/config.txt?

There's an option in sudo raspi-config (or the GUI preferences dialog) to do that.
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a hippy & doctor free zone.

Raspnew
Posts: 4
Joined: Wed Dec 09, 2015 2:08 am

Re: Python, SPI and spidev

Thu Dec 10, 2015 1:15 am

DougieLawson wrote:Have you added

Code: Select all

dtparam=i2c=on,spi=on
or

Code: Select all

dtparam=spi=on
to /boot/config.txt?

There's an option in sudo raspi-config (or the GUI preferences dialog) to do that.
Thank you Dougie

Everything is working now! :D

pcopa
Posts: 7
Joined: Tue Dec 08, 2015 10:02 pm

Re: Python, SPI and spidev

Tue Dec 15, 2015 2:18 am

I'm also moving on and having issues loading spidev.

I've gone through the steps as described at the beginning of this thread and the package "python-dev" did load, because trying a second time it did say "already installed". Added the "dtparam=spi=on" to the "conig.txt" file, but still I'm getting the error: "Import spidev ... ImportError: No module named spidev" .

Is the spidev module inside the python-dev module?
Do I also have to install the python SPI-wrapper as described above?

I'm using the VNC to access the RPI 2 and using the gksudo .... command to make the changes.

Raspnew
Posts: 4
Joined: Wed Dec 09, 2015 2:08 am

Re: Python, SPI and spidev

Fri Jan 01, 2016 9:08 pm

Here I'm again...

I got everything working well, using an old Xilinx 3AN FPGA Starter Kit - analog capture with dual programmable amps LTC6912-1 and dual A/D LTC1407A-1 - I have the readings accurate to 2mV (mostly wiring noise). SPI is working like a charm and it is very robust to the rats nest wiring between the Raspberry Pi and the Xilinx board!

The only thing is that I have to set "spi.mode = 1" to have it working. Honestly I do not understand why it does not work with "spi.mode = 0" since Rasp Pi signal is clocking the data on the falling edge as it should be.

What is the difference between spi.modes 0, 1, 2 and 3? Where can I get more detailed documentation on the spidev package?

charles1017
Posts: 4
Joined: Tue Feb 02, 2016 5:21 am

Re: Python, SPI and spidev

Tue Feb 02, 2016 5:31 am

Raspnew wrote:Here I'm again...

I got everything working well, using an old Xilinx 3AN FPGA Starter Kit - analog capture with dual programmable amps LTC6912-1 and dual A/D LTC1407A-1 - I have the readings accurate to 2mV (mostly wiring noise). SPI is working like a charm and it is very robust to the rats nest wiring between the Raspberry Pi and the Xilinx board!

The only thing is that I have to set "spi.mode = 1" to have it working. Honestly I do not understand why it does not work with "spi.mode = 0" since Rasp Pi signal is clocking the data on the falling edge as it should be.

What is the difference between spi.modes 0, 1, 2 and 3? Where can I get more detailed documentation on the spidev package?
https://en.wikipedia.org/wiki/Serial_Pe ... _and_phase
From wiki, each spi.mode actually correspond to different timing mode.

While they code the mode number (0,1,2,3) is pretty much arbitrary
The "correct" way to refer the "mode" is by CPOL CPHA

vikram_shadow
Posts: 7
Joined: Mon Dec 21, 2015 9:05 am

Re: Python, SPI and spidev

Wed Feb 03, 2016 5:21 am

hi guys i need help
when i tried this command 'sudo python setup.py install'
I'm getting error
File "setup.py", line 19, in <module>
long_description= open('README.md').read() + "\n" + open('CHANGELOG.md').read(),
IOError: [Errno 2] No such file or directory: 'README.md'

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Python, SPI and spidev

Wed Feb 03, 2016 9:33 am

Apparently the library has changed since the steps in the original post - you should probably clone the full git project and try again or at least do this before setup.py:

Code: Select all

wget https://raw.github.com/doceme/py-spidev/master/README.md
(and maybe Makefile too)

Or use pip instead:

Code: Select all

sudo pip install spidev
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

Return to “Interfacing (DSI, CSI, I2C, etc.)”