0% found this document useful (0 votes)
125 views

Python Library - Spidev

The Python spidev library allows interfacing with SPI devices from Python code via the Linux spidev driver. It can be installed from source by downloading the tarball from PyPI, extracting it, and running setup.py install. The library provides an SpiDev class to open connections to SPI devices and methods like xfer() to transfer data over SPI with options to set bus speed, mode, and other settings.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views

Python Library - Spidev

The Python spidev library allows interfacing with SPI devices from Python code via the Linux spidev driver. It can be installed from source by downloading the tarball from PyPI, extracting it, and running setup.py install. The library provides an SpiDev class to open connections to SPI devices and methods like xfer() to transfer data over SPI with options to set bus speed, mode, and other settings.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Python Library - spidev http://domoticx.

com/python-library-spidev/

Het gebruik van software, downloads, scripts en uitleg op deze website is geheel op eigen risico, X
DomoticX is niet aansprakelijk voor de schade die, rechtstreeks of onrechtstreeks het gevolg is van
gebruik van deze website!
OK

Zoeken
Zoek hier …

Domoticx.nl | Laat techniek leven! 


The closer you look, the less you see!

Python Library – spidev

Python spidev library

Informatie (ENG):

Python bindings for Linux SPI access through spidev

Python Spidev
=============
This project contains a python module for interfacing with SPI devices from user space via the

1 of 6 11/10/2022, 1:10 PM
Python Library - spidev http://domoticx.com/python-library-spidev/

spidev linux kernel driver.


This is a modified version of the code originally found [here](http://elk.informatik.fh-
augsburg.de/da/da-49/trees/pyap7k/lang/py-spi)
All code is GPLv2 licensed unless explicitly stated otherwise.

Usage

1 ```python
2 import spidev
3 spi = spidev.SpiDev()
4 spi.open(bus, device)
5 to_send = [0x01, 0x02, 0x03]
6 spi.xfer(to_send)
7 ```

Settings

1 ```python
2 import spidev
3 spi = spidev.SpiDev()
4 spi.open(bus, device)
5  
6 # Settings (for example)
7 spi.max_speed_hz = 5000
8 spi.mode = 0b01
9 ...
10 ```

* bits_per_word
* cshigh
* loop
* lsbfirst
* max_speed_hz
* mode – SPI mode as two bit pattern of clock polarity and phase [CPOL|CPHA], min: 0b00 =
0, max: 0b11 = 3
* threewire – SI/SO signals shared

Methods
——-
open(bus, device) / Connects to the specified SPI device, opening /dev/spidev-bus.device

2 of 6 11/10/2022, 1:10 PM
Python Library - spidev http://domoticx.com/python-library-spidev/

readbytes(n) / Read n bytes from SPI device.


writebytes(list of values) / Writes a list of values to SPI device.

xfer(list of values[, speed_hz, delay_usec, bits_per_word])


Performs an SPI transaction. Chip-select should be released and reactivated between blocks.
Delay specifies the delay in usec between blocks.

xfer2(list of values[, speed_hz, delay_usec, bits_per_word])


Performs an SPI transaction. Chip-select should be held active between blocks.

close()
Disconnects from the SPI device.

Installatie via Source (TAR.GZ)

Eerst heb je de python DEV dependancies nodig, installeer deze met het commando:

sudo apt-get install python-dev

1 Reading package lists... Done


2 Building dependency tree
3 Reading state information... Done
4 The following extra packages will be installed:
5   libexpat1-dev libpython-dev libpython2.7-dev python2.7-dev
6 The following NEW packages will be installed:
7   libexpat1-dev libpython-dev libpython2.7-dev python-dev python2.7-dev
8 0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
9 Need to get 18.3 MB of archives.
10 After this operation, 26.3 MB of additional disk space will be used.
11 Do you want to continue? [Y/n] Y
12 Get:1 http://mirrordirector.raspbian.org/raspbian/ jessie/main libexpat1-dev arm                    
13 Get:2 http://mirrordirector.raspbian.org/raspbian/ jessie/main libpython2.7-dev                     
14 Get:3 http://mirrordirector.raspbian.org/raspbian/ jessie/main python-dev armhf                     
15 Get:4 http://mirrordirector.raspbian.org/raspbian/ jessie/main libpython-dev armhf 2.7.9-1 [19.6 kB]
16 Get:5 http://mirrordirector.raspbian.org/raspbian/ jessie/main python2.7-dev armhf 2.7.9-2 [281 kB]
17 Fetched 18.3 MB in 3s (5,672 kB/s)
18 Selecting previously unselected package libexpat1-dev:armhf.
19 (Reading database ... 30300 files and directories currently installed.)
20 Preparing to unpack .../libexpat1-dev_2.1.0-6+deb8u1_armhf.deb ...
21 Unpacking libexpat1-dev:armhf (2.1.0-6+deb8u1) ...
22 Selecting previously unselected package libpython2.7-dev:armhf.
23 Preparing to unpack .../libpython2.7-dev_2.7.9-2_armhf.deb ...
24 Unpacking libpython2.7-dev:armhf (2.7.9-2) ...
25 Selecting previously unselected package libpython-dev:armhf.
26 Preparing to unpack .../libpython-dev_2.7.9-1_armhf.deb ...
27 Unpacking libpython-dev:armhf (2.7.9-1) ...

3 of 6 11/10/2022, 1:10 PM
Python Library - spidev http://domoticx.com/python-library-spidev/

28 Selecting previously unselected package python2.7-dev.


29 Preparing to unpack .../python2.7-dev_2.7.9-2_armhf.deb ...
30 Unpacking python2.7-dev (2.7.9-2) ...
31 Selecting previously unselected package python-dev.
32 Preparing to unpack .../python-dev_2.7.9-1_armhf.deb ...
33 Unpacking python-dev (2.7.9-1) ...
34 Processing triggers for man-db (2.7.0.2-5) ...
35 Setting up libexpat1-dev:armhf (2.1.0-6+deb8u1) ...
36 Setting up libpython2.7-dev:armhf (2.7.9-2) ...
37 Setting up libpython-dev:armhf (2.7.9-1) ...
38 Setting up python2.7-dev (2.7.9-2) ...
39 Setting up python-dev (2.7.9-1) ...

Installeer de module met de volgende commando’s:

wget https://pypi.python.org/packages/source/s/spidev/spidev-3.1.tar.gz

1 --2016-03-06 17:42:15--  https://pypi.python.org/packages/source/s/spidev/spidev-3.1.tar.gz
2 Resolving pypi.python.org (pypi.python.org)... 23.235.43.223
3 Connecting to pypi.python.org (pypi.python.org)|23.235.43.223|:443... connected.
4 HTTP request sent, awaiting response... 200 OK
5 Length: 12583 (12K) [application/octet-stream]
6 Saving to: ‘spidev-3.1.tar.gz’
7  
8 spidev-3.1.tar.gz               100%[========================================================>]  12.
9  
10 2016-03-06 17:42:15 (61.7 MB/s) - ‘spidev-3.1.tar.gz’ saved [12583/12583]

tar -zxvf spidev-3.1.tar.gz

1 spidev-3.1/
2 spidev-3.1/README.md
3 spidev-3.1/CHANGELOG.md
4 spidev-3.1/setup.py
5 spidev-3.1/spidev_module.c
6 spidev-3.1/PKG-INFO
7 spidev-3.1/LICENSE.md

cd spidev-3.1

sudo python setup.py install

1 running install
2 running build
3 running build_ext
4 building 'spidev' extension
5 creating build
6 creating build/temp.linux-armv7l-2.7

4 of 6 11/10/2022, 1:10 PM
Python Library - spidev http://domoticx.com/python-library-spidev/

7 arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-alias


8 creating build/lib.linux-armv7l-2.7
9 arm-linux-gnueabihf-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-a
10 running install_lib
11 copying build/lib.linux-armv7l-2.7/spidev.so -> /usr/local/lib/python2.7/dist-packages
12 running install_egg_info
13 Writing /usr/local/lib/python2.7/dist-packages/spidev-3.1.egg-info

Changelog

Download spidev @ pypi.python.org

5 of 6 11/10/2022, 1:10 PM
Python Library - spidev http://domoticx.com/python-library-spidev/

Disclaimer: Het gebruik van software, downloads, scripts en uitleg op deze website is geheel op eigen risico en is
bestemd voor educatief gebruik, DomoticX is niet aansprakelijk voor de schade die, rechtstreeks of onrechtstreeks het
gevolg is van gebruik van deze website!

Domotica forum EU Tweakers.net Arduino forum NL Instructables Tindie Thingiverse (3D modellen)
DXF Projects (2D modellen) Arduino Project HUB

  

6 of 6 11/10/2022, 1:10 PM

You might also like