Skip to content

Commit 3763e23

Browse files
authored
Merge pull request ondryaso#20 from LudwigKnuepfer/pr/spidev
switch to spidev
2 parents 0eda8df + f58ae37 commit 3763e23

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ git clone https://github.com/ondryaso/pi-rc522.git
1414
cd pi-rc522
1515
python setup.py install
1616
```
17-
You'll also need to install the [**SPI-Py**](https://github.com/lthiery/SPI-Py) and [**RPi.GPIO**](https://pypi.python.org/pypi/RPi.GPIO) libraries.
17+
You'll also need to install the [**spidev**](https://pypi.python.org/pypi/spidev) and [**RPi.GPIO**](https://pypi.python.org/pypi/RPi.GPIO) libraries.
1818

1919
[MIFARE datasheet](http://www.nxp.com/documents/data_sheet/MF1S503x.pdf) can be useful.
2020

@@ -34,7 +34,7 @@ Connecting RC522 module to SPI is pretty easy. You can use [this neat website](h
3434
| RST | 7 | 22 | GPIO25 |
3535
| 3.3V | 8 | 1 | 3V3 |
3636

37-
You can also connect the SDA pin to CE1 (GPIO7, pin #26) and call the RFID constructor with *dev='/dev/spidev0.1'*
37+
You can also connect the SDA pin to CE1 (GPIO7, pin #26) and call the RFID constructor with *bus=0, device=1*
3838
and you can connect RST pin to any other free GPIO pin and call the constructor with *pin_rst=__BOARD numbering pin__*.
3939

4040
__NOTE:__ For RPi A+/B+/2/3 with 40 pin connector, SPI1/2 is available on top of SPI0. Kernel 4.4.x or higher and *dtoverlay* configuration is required. For SPI1/2, *pin_ce=__BOARD numbering pin__* is required.

pirc522/__init__.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import time
22
import signal
33

4-
import spi as SPI
4+
import spidev
55
import RPi.GPIO as GPIO
66

77

8-
__version__ = "1.1.0"
8+
__version__ = "2.0.0"
99

1010

1111
class RFID(object):
@@ -41,11 +41,14 @@ class RFID(object):
4141

4242
authed = False
4343

44-
def __init__(self, dev='/dev/spidev0.0', speed=1000000, pin_rst=22, pin_ce=0):
44+
def __init__(self, bus=0, device=0, speed=1000000, pin_rst=22, pin_ce=0):
4545
self.pin_rst = pin_rst
4646
self.pin_ce = pin_ce
4747

48-
SPI.openSPI(device=dev, speed=speed)
48+
self.spi = spidev.SpiDev()
49+
self.spi.open(bus, device)
50+
self.spi.max_speed_hz = speed
51+
4952
GPIO.setmode(GPIO.BOARD)
5053
GPIO.setup(pin_rst, GPIO.OUT)
5154
GPIO.output(pin_rst, 1)
@@ -64,16 +67,16 @@ def __init__(self, dev='/dev/spidev0.0', speed=1000000, pin_rst=22, pin_ce=0):
6467
def spi_transfer(self, data):
6568
if self.pin_ce != 0:
6669
GPIO.output(self.pin_ce, 0)
67-
r = SPI.transfer(data)
70+
r = self.spi.xfer2(data)
6871
if self.pin_ce != 0:
6972
GPIO.output(self.pin_ce, 1)
7073
return r
7174

7275
def dev_write(self, address, value):
73-
self.spi_transfer(((address << 1) & 0x7E, value))
76+
self.spi_transfer([(address << 1) & 0x7E, value])
7477

7578
def dev_read(self, address):
76-
return self.spi_transfer((((address << 1) & 0x7E) | 0x80, 0))[1]
79+
return self.spi_transfer([((address << 1) & 0x7E) | 0x80, 0])[1]
7780

7881
def set_bitmask(self, address, mask):
7982
current = self.dev_read(address)

0 commit comments

Comments
 (0)