Skip to content

Commit c44abc2

Browse files
authored
Sped up for the RaspberryPi SPI
Tested writing 437 bytes of data to an NTAG215. Original script: 18.9 seconds write, 19.2 seconds read. Modified script: 7.6 seconds write, 7.8 seconds read.
1 parent 949a5c6 commit c44abc2

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

adafruit_pn532/spi.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,7 @@
5151
def reverse_bit(num):
5252
"""Turn an LSB byte to an MSB byte, and vice versa. Used for SPI as
5353
it is LSB for the PN532, but 99% of SPI implementations are MSB only!"""
54-
result = 0
55-
for _ in range(8):
56-
result <<= 1
57-
result += (num & 1)
58-
num >>= 1
59-
return result
54+
return int('{:08b}'.format(num)[::-1], 2)
6055

6156
class PN532_SPI(PN532):
6257
"""Driver for the PN532 connected over SPI. Pass in a hardware or bitbang
@@ -83,12 +78,12 @@ def _wait_ready(self, timeout=1):
8378
timestamp = time.monotonic()
8479
while (time.monotonic() - timestamp) < timeout:
8580
with self._spi as spi:
86-
time.sleep(0.02) # required
81+
#time.sleep(0.02) # required
8782
spi.write_readinto(status, status) #pylint: disable=no-member
8883
if reverse_bit(status[1]) == 0x01: # LSB data is read in MSB
8984
return True # Not busy anymore!
90-
else:
91-
time.sleep(0.01) # pause a bit till we ask again
85+
#else:
86+
# time.sleep(0.01) # pause a bit till we ask again
9287
# We timed out!
9388
return False
9489

@@ -100,21 +95,21 @@ def _read_data(self, count):
10095
frame[0] = reverse_bit(_SPI_DATAREAD)
10196

10297
with self._spi as spi:
103-
time.sleep(0.02) # required
98+
#time.sleep(0.02) # required
10499
spi.write_readinto(frame, frame) #pylint: disable=no-member
105100
for i, val in enumerate(frame):
106101
frame[i] = reverse_bit(val) # turn LSB data to MSB
107-
if self.debug:
108-
print("Reading: ", [hex(i) for i in frame[1:]])
102+
#if self.debug:
103+
# print("Reading: ", [hex(i) for i in frame[1:]])
109104
return frame[1:]
110105

111106
def _write_data(self, framebytes):
112107
"""Write a specified count of bytes to the PN532"""
113108
# start by making a frame with data write in front,
114109
# then rest of bytes, and LSBify it
115110
rev_frame = [reverse_bit(x) for x in bytes([_SPI_DATAWRITE]) + framebytes]
116-
if self.debug:
117-
print("Writing: ", [hex(i) for i in rev_frame])
111+
#if self.debug:
112+
# print("Writing: ", [hex(i) for i in rev_frame])
118113
with self._spi as spi:
119-
time.sleep(0.02) # required
114+
#time.sleep(0.02) # required
120115
spi.write(bytes(rev_frame)) #pylint: disable=no-member

0 commit comments

Comments
 (0)