Skip to content

Commit 5542a15

Browse files
committed
SPI working
1 parent f1f0b7a commit 5542a15

File tree

2 files changed

+63
-22
lines changed

2 files changed

+63
-22
lines changed

adafruit_pn532.py

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,16 @@
144144
_ACK = b'\x00\x00\xFF\x00\xFF\x00'
145145
_FRAME_START = b'\x00\x00\xFF'
146146

147+
def reverse_bit(num):
148+
result = 0
149+
for _ in range(8):
150+
result <<= 1
151+
result += (num & 1)
152+
num >>= 1
153+
return result
154+
155+
156+
147157
class BusyError(Exception):
148158
"""Base class for exceptions in this module."""
149159
pass
@@ -156,7 +166,6 @@ def __init__(self, *, debug=False, reset=None):
156166
"""
157167
self.debug = debug
158168
if reset:
159-
print("resetting")
160169
reset.direction = Direction.OUTPUT
161170
reset.value = True
162171
time.sleep(0.1)
@@ -408,7 +417,7 @@ def _wait_ready(self, timeout=1):
408417
return True
409418
else:
410419
time.sleep(0.1)
411-
return True
420+
return False
412421

413422
def _read_data(self, count):
414423
"""Read a specified count of bytes from the PN532."""
@@ -426,3 +435,53 @@ def _read_data(self, count):
426435
def _write_data(self, framebytes):
427436
with self._i2c:
428437
self._i2c.write(framebytes)
438+
439+
class PN532_SPI(PN532):
440+
"""Driver for the PN532 connected over I2C."""
441+
def __init__(self, spi, cs_pin, *, irq=None, reset=None, debug=False):
442+
"""Create an instance of the PN532 class using SPI
443+
"""
444+
self.debug = debug
445+
self._irq = irq
446+
self._spi = spi_device.SPIDevice(spi, cs_pin)
447+
super().__init__(debug=debug, reset=reset)
448+
449+
def _wait_ready(self, timeout=1):
450+
if self._irq:
451+
print("TODO IRQ")
452+
else:
453+
status = bytearray([reverse_bit(_SPI_STATREAD), 0])
454+
455+
t = time.monotonic()
456+
while (time.monotonic() - t) < timeout:
457+
with self._spi as spi:
458+
spi.write_readinto(status, status)
459+
if reverse_bit(status[1]) == 0x01: # LSB data is read in MSB
460+
return True
461+
else:
462+
time.sleep(0.1)
463+
return False
464+
465+
def _read_data(self, count):
466+
"""Read a specified count of bytes from the PN532."""
467+
# Build a read request frame.
468+
frame = bytearray(count+1)
469+
# Add the SPI data read signal byte, but LSB'ify it
470+
frame[0] = reverse_bit(_SPI_DATAREAD)
471+
472+
with self._spi as spi:
473+
time.sleep(0.01)
474+
spi.write_readinto(frame, frame)
475+
for i in range(len(frame)):
476+
frame[i] = reverse_bit(frame[i]) # turn LSB data to MSB
477+
if self.debug:
478+
print("Reading: ", [hex(i) for i in frame[1:]])
479+
return frame[1:]
480+
481+
def _write_data(self, framebytes):
482+
with self._spi as spi:
483+
reversed = [reverse_bit(x) for x in bytes([_SPI_DATAWRITE]) + framebytes]
484+
time.sleep(0.01)
485+
if self.debug:
486+
print("writing: ", [hex(i) for i in reversed])
487+
spi.write(bytes(reversed))

examples/main.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,5 @@
1-
from Adafruit_Circuitpython_PN532 import adafruit_pn532
2-
from digitalio import DigitalInOut, Direction, Pull
3-
import board
4-
import time
5-
import busio
6-
# I2C connection:
7-
i2c = busio.I2C(board.SCL, board.SDA)
8-
pn532 = adafruit_pn532.PN532_I2C(i2c)
9-
10-
ic, ver, rev, support = pn532.get_firmware_version()
11-
print('Found PN532 with firmware version: {0}.{1}'.format(ver, rev))
12-
13-
# Configure PN532 to communicate with MiFare cards.
14-
pn532.SAM_configuration()
15-
16-
print('Waiting for MiFare card...')
17-
while True:
18-
# Check if a card is available to read
19-
uid = pn532.read_passive_target(timeout=0.25)
1+
ush=True)
202
# Try again if no card is available.
213
if uid is None:
224
continue
23-
print('Found card with UID:', [hex(i) for i in uid])
5+
print('Found card with UID:', [hex(i) for i in uid])

0 commit comments

Comments
 (0)