Skip to content

Commit 3769277

Browse files
authored
Merge pull request ondryaso#25 from LudwigKnuepfer/pr/irq
Add support for interrupt driven tag detection
2 parents 3763e23 + be3687b commit 3769277

File tree

5 files changed

+49
-7
lines changed

5 files changed

+49
-7
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ Connecting RC522 module to SPI is pretty easy. You can use [this neat website](h
3030
| SCK | 2 | 23 | GPIO11, SCKL |
3131
| MOSI | 3 | 19 | GPIO10, MOSI |
3232
| MISO | 4 | 21 | GPIO9, MISO |
33+
| IRQ | 5 | 18 | GPIO24 |
3334
| GND | 6 | 6, 9, 20, 25 | Ground |
3435
| RST | 7 | 22 | GPIO25 |
35-
| 3.3V | 8 | 1 | 3V3 |
36+
| 3.3V | 8 | 1,17 | 3V3 |
3637

3738
You can also connect the SDA pin to CE1 (GPIO7, pin #26) and call the RFID constructor with *bus=0, device=1*
3839
and you can connect RST pin to any other free GPIO pin and call the constructor with *pin_rst=__BOARD numbering pin__*.
40+
Furthermore, the IRQ pin is configurable by passing *pin_irq=__BOARD numbering pin__*.
3941

4042
__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.
4143

@@ -51,6 +53,7 @@ from pirc522 import RFID
5153
rdr = RFID()
5254

5355
while True:
56+
rdr.wait_for_tag()
5457
(error, tag_type) = rdr.request()
5558
if not error:
5659
print("Tag detected")
@@ -84,6 +87,9 @@ util = rdr.util()
8487
util.debug = True
8588

8689
while True:
90+
# Wait for tag
91+
rdr.wait_for_tag()
92+
8793
# Request tag
8894
(error, data) = rdr.request()
8995
if not error:
@@ -121,6 +127,4 @@ while True:
121127
util.dump()
122128
# We must stop crypto
123129
util.deauth()
124-
125-
time.sleep(1)
126130
```

examples/KeyChange.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ def end_read(signal,frame):
2020

2121
print("Starting")
2222
while run:
23+
rdr.wait_for_tag()
24+
2325
(error, data) = rdr.request()
2426
if not error:
2527
print("\nDetected: " + format(data, "02x"))

examples/Read.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import signal
44
import time
5+
import sys
56

67
from pirc522 import RFID
78

@@ -15,11 +16,14 @@ def end_read(signal,frame):
1516
print("\nCtrl+C captured, ending read.")
1617
run = False
1718
rdr.cleanup()
19+
sys.exit()
1820

1921
signal.signal(signal.SIGINT, end_read)
2022

2123
print("Starting")
2224
while run:
25+
rdr.wait_for_tag()
26+
2327
(error, data) = rdr.request()
2428
if not error:
2529
print("\nDetected: " + format(data, "02x"))

examples/UtilExample.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
util.debug = True
1212

1313
while True:
14+
# Wait for tag
15+
rdr.wait_for_tag()
16+
1417
# Request tag
1518
(error, data) = rdr.request()
1619
if not error:

pirc522/__init__.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import time
2-
import signal
1+
import threading
32

43
import spidev
54
import RPi.GPIO as GPIO
65

76

8-
__version__ = "2.0.0"
7+
__version__ = "2.1.0"
98

109

1110
class RFID(object):
1211
pin_rst = 22
1312
pin_ce = 0
13+
pin_irq = 18
1414

1515
mode_idle = 0x00
1616
mode_auth = 0x0E
@@ -40,21 +40,30 @@ class RFID(object):
4040
length = 16
4141

4242
authed = False
43+
irq = threading.Event()
4344

44-
def __init__(self, bus=0, device=0, speed=1000000, pin_rst=22, pin_ce=0):
45+
def __init__(self, bus=0, device=0, speed=1000000, pin_rst=22,
46+
pin_ce=0, pin_irq=18):
4547
self.pin_rst = pin_rst
4648
self.pin_ce = pin_ce
49+
self.pin_irq = pin_irq
4750

4851
self.spi = spidev.SpiDev()
4952
self.spi.open(bus, device)
5053
self.spi.max_speed_hz = speed
5154

5255
GPIO.setmode(GPIO.BOARD)
5356
GPIO.setup(pin_rst, GPIO.OUT)
57+
GPIO.setup(pin_irq, GPIO.IN, pull_up_down=GPIO.PUD_UP)
58+
GPIO.add_event_detect(pin_irq, GPIO.FALLING,
59+
callback=self.irq_callback)
5460
GPIO.output(pin_rst, 1)
5561
if pin_ce != 0:
5662
GPIO.setup(pin_ce, GPIO.OUT)
5763
GPIO.output(pin_ce, 1)
64+
self.init()
65+
66+
def init(self):
5867
self.reset()
5968
self.dev_write(0x2A, 0x8D)
6069
self.dev_write(0x2B, 0x3E)
@@ -344,7 +353,27 @@ def write(self, block_address, data):
344353

345354
return error
346355

356+
def irq_callback(self, pin):
357+
self.irq.set()
358+
359+
def wait_for_tag(self):
360+
# enable IRQ on detect
361+
self.init()
362+
self.irq.clear()
363+
self.dev_write(0x04, 0x00)
364+
self.dev_write(0x02, 0xA0)
365+
# wait for it
366+
waiting = True
367+
while waiting:
368+
self.dev_write(0x09, 0x26)
369+
self.dev_write(0x01, 0x0C)
370+
self.dev_write(0x0D, 0x87)
371+
waiting = not self.irq.wait(0.1)
372+
self.irq.clear()
373+
self.init()
374+
347375
def reset(self):
376+
authed = False
348377
self.dev_write(0x01, self.mode_reset)
349378

350379
def cleanup(self):

0 commit comments

Comments
 (0)