Giter Club home page Giter Club logo

spi-py's Introduction

SPI-Py: Hardware SPI as a C Extension for Python

COPYRIGHT (C) 2012 Louis Thiery. All rights reserved. Further work by Connor Wolf.

Forked in 2019 by Nathan Leefer to fix memory handling in the C extension.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License V2 as published by the Free Software Foundation.

LIABILITY
This program is distributed for educational purposes only and is no way suitable for any particular application, especially commercial. There is no implied suitability so use at your own risk!

Instructions

  1. Clone or download this repository, navigate to the SPI-Py directory, and install the library using the following command. Use python3 if installing for Python 3.
> sudo python setup.py install
  1. Make sure the SPI interface is enabled for your Raspberry Pi. This can be done using the raspi-config utility.
> sudo raspi-config
  1. This module provides three functions for communicating with SPI devices:
dev_dictionary = spi.openSPI(kwargs)
data_in = spi.transfer(dev_dictionary, data_out)
spi.closeSPI(dev_dictionary)

The next section covers these in detail.

Example usage

The below commands can be found in the test_script.py file.

After installing the library, import the spi module to your Python code via :

import spi

Open the file descriptor to the SPI device with one of two chip selects:

device_0 = spi.openSPI(device="/dev/spidev0.0",mode=0,speed=500000,bits=8,delay=0)

The device keyword can be either "/dev/spidev0.0" or "/dev/spidev0.1". The difference refers to which chip select pin is used by the SPI device driver. The mode keyword can be 0,1,2, or 3, and many SPI devices can operate up to 8000000 Hz speed, however it is recommended to check your data sheet. See the Raspberry Pi docs for a detailed explanation of these and other parameters.

Use the returned device handle to conduct an SPI transaction

data_out = (0xFF,0x00,0xFF)
data_in = (0x00, 0x00, 0x00)
data_in = spi.transfer(device_0, data_out)

The above would write the 3 bytes contained in data_out and copy the received data to data_in. Note that data_in will always be a tuple the same length as data_out, and will simply reflect the state of the MISO pin throughout the transaction. It is up to the user to understand the device behavior connected to the SPI pins.

To verify that this works connect GPIO 10 (MOSI, physical pin 19) to GPIO 9 (MISO, physical pin 21) in a loop back. You should see that data_out now equals data_in.

Close the file descriptor for your SPI device

spi.closeSPI(device_0)

Memory leak

The memory_leak.py script continuously executes a simple transaction on /dev/spidev0.0. There does not appear to be a memory leak in this use case.

spi-py's People

Contributors

blark avatar ericpobot avatar fake-name avatar lthiery avatar msedv avatar naleefer avatar pelwell avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

spi-py's Issues

Accessing two SPI devices

As long as I'm only accessing ONE SPI device everything is fine, but when accessing TWO (in my case: MFRC522.py for two RFID-Readers) only one of them works. Looking at your code I think the problem is that

int fd;

is declared globally?!

Sending 256 bytes or greater hangs SPI-Py

I am using the plugin to play with a SPI SRAM (512kbit). For now I am simply filling it up and then reading back to make sure that the memory (and SPI) works properly.

The SRAM has sequential write mode so I can write many bytes at once, like so:

DATA = (0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xAD, 0xF0, 0x0D) * 16

spi.transfer((0x02, 0,0) + (DATA))

This method works great, as you can see this is transmitting 128bytes. As soon as I double it the program just hangs. Anything below 16 bytes works fine. I've read that that SPI chip is capable of sending up to 4096 bytes at once, it would be great if I could take advantage of that so I can read/write faster.

Any ideas of what might be causing this? If you need any debug information please let me know.

Please tag your versions

First of all: thank you for your great work - I really appreciate it :-)

Just one thing: it would help, if you tag your versions (my App "broke" with your last changes - no big thing but tags would help).

Warning on Run sudo python setup.py install

I Follow this instruction http://geraintw.blogspot.com/2014/01/rfid-and-raspberry-pi.html.

i run this script on Raspberry Pi B+ (raspbian). when i run 👍
sudo python setup.py install
running install
running build
running build_ext
building 'spi' 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/include/python2.7 -c spi.c -o build/temp.linux-armv6l-2.7/spi.o
spi.c:197:2: warning: initialization from incompatible pointer type [enabled by default]
spi.c:197:2: warning: (near initialization for âSpiMethods[0].ml_methâ) [enabled by default]
creating build/lib.linux-armv6l-2.7
gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro build/temp.linux-armv6l-2.7/spi.o -o build/lib.linux-armv6l-2.7/spi.so
running install_lib
copying build/lib.linux-armv6l-2.7/spi.so -> /usr/local/lib/python2.7/dist-packages
running install_egg_info
Removing /usr/local/lib/python2.7/dist-packages/SPI_Py-1.0.egg-info
Writing /usr/local/lib/python2.7/dist-packages/SPI_Py-1.0.egg-info

but when i run sudo python setup.py install again :
running install
running build
running build_ext
running install_lib
copying build/lib.linux-armv6l-2.7/spi.so -> /usr/local/lib/python2.7/dist-packages
running install_egg_info
Removing /usr/local/lib/python2.7/dist-packages/SPI_Py-1.0.egg-info
Writing /usr/local/lib/python2.7/dist-packages/SPI_Py-1.0.egg-info

but when i test with python MFRC522-python/Read.py, cannot detect tag

Update to kernel 2.4/device tree

Starting with the firmware based on the 2.4 kernels it isn't possible to deactivate device_tree anymore which makes the library stop working - anyone any idea what has to be done?

Thank you, Markus

Warnings when installing

Hi,

I get 2 warnings when I try to install SPI-PY. I've got a "Raspberry PI B+" board and "Raspbian GNU/Linux 7 (wheezy)".

gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c spi.c -o build/temp.linux-armv6l-2.7/spi.o

spi.c:197:2: warning: initialization from incompatible pointer type [enabled by default]
spi.c:197:2: warning: (near initialization for ‘SpiMethods[0].ml_meth’) [enabled by default]

What can I do to prevent these warning, are the warnings a big problem?

Thank you in advance!

python packagename in setup.py

It would be recommendable to change the package name in the setup.py file, so that one can actually know what the package is once it is installed.

right now the setup.py file only has the demo defaults in there.

Setting cs_change breaks MFRC522 RFID reader

I've been investigating a problem using the Mifare RFID reader module on Raspberry Pi. The problem started when we made the switch to the upstream SPI driver, spi-bcm2835.

One of the features of the upstream SPI driver is that it forces software CS due to a deficiency in the hardware. I noticed that with that driver the CS line was being held active between messages, but the MFRC522 spec says that it must become inactive between messages. Hacking the downstream driver to allow hardware CS to be used if requested, and requesting it, fixed the problem. I put this down to some incompatibility in the SW CS implementation in the driver.

I now think the driver is behaving correctly, and the bug is in this library. The kernel documentation says:

  - An spi_message is a sequence of protocol operations, executed
    as one atomic sequence.  SPI driver controls include:
[...]
      + whether the chipselect becomes inactive after a transfer and
        any delay ... by using the spi_transfer.cs_change flag;

      + hinting whether the next message is likely to go to this same
        device ... using the spi_transfer.cs_change flag on the last
        transfer in that atomic group, and potentially saving costs
        for chip deselect and select operations.

I think this is a bit cryptic, but another doc I found online spells it out:

All SPI transfers start with the relevant chipselect active. Normally it stays selected until after the last transfer in a message. Drivers can affect the chipselect signal using cs_change.

(i) If the transfer isn't the last one in the message, this flag is used to make the chipselect briefly go inactive in the middle of the message. Toggling chipselect in this way may be needed to terminate a chip command, letting a single spi_message perform all of group of chip transactions together.

(ii) When the transfer is the last one in the message, the chip may stay selected until the next transfer. On multi-device SPI busses with nothing blocking messages going to other devices, this is just a performance hint; starting a message to another device deselects this one. But in other cases, this can be used to ensure correctness. Some devices need protocol transactions to be built from a series of spi_message submissions, where the content of one message is determined by the results of previous messages and where the whole transaction ends when the chipselect goes intactive.

When the transfer() function sets cs_change to 1 on the one-and-only message it sends, it is asking the SPI subsystem to try to keep CS active between it and the next message. I don't know if this was your intention, but it breaks the RFC reader. I suspect it only used to work on the old SPI driver because the hardware was incapable of keep CS active between transfers.

You can read the original bug report and subsequent discussion here.

SPi has no attribute openSpi

I am have a lot of problems with it on my Raspberry Pi. I am trying to use the MFRC552 module and it gives me the error

Spi has no attribute 'openSpi'. I would really appreciate it if you fixed it, or you showed me how to fix it. Thanks!

Having problems running Sudo Python read.py

I have been getting an error "can't open device: No such file or directory" which I have nailed down to the line

pi@raspberrypi:~/gr_programs/RFID/SPI-Py/MFRC522-python $ sudo python read.py
Opening file.
importing MFRC533.
Opening signal
Signal open
MIFAREREADER =
can't open device: No such file or directory

This is my modified .py

print "Opening file."
print "importing MFRC533."
import MFRC522
print "Opening signal"
import signal
print "Signal open"
continue_reading = True
print "MIFAREREADER ="
MIFAREReader = MFRC522.MFRC522()

print "Def card AtoC ="
cardA = [5,74,28,185,234]
cardB = [83,164,247,164,164]
cardC = [20,38,121,207,132]

print "Def end read ="
def end_read(signal, frame):
global continue_reading
continue_reading = False
print "Ctrl+C captured, ending read."
MIFAREReader.GPIO_CLEEN()

signal.signal(signal.SIGINT, end_read)

Any idea on what I doing wrong?

Returning Error and Failing on Install

SPI-Py fails to install, this is from my logs:

arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/include/python2.7 -c spi.c -o build/temp.linux-armv6l-2.7/spi.o
arm-linux-gnueabihf-gcc: error: spi.c: No such file or directory
arm-linux-gnueabihf-gcc: fatal error: no input files
compilation terminated.
error: command 'arm-linux-gnueabihf-gcc' failed with exit status 4

spi.transfert type error

the command spi.transfer((2,15)) return the error
TypeError: function takes exactly 2 arguments (1 given)

This command used to work before recent commit.

how i can send to spi same data

i need analog function like android spi.transfer(data)
in your code function is (adr, data)
i dont understand it( help me

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.