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.
This commit is contained in:
tgikal 2019-06-28 15:12:41 -04:00 committed by GitHub
parent 949a5c6213
commit c44abc207e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -51,12 +51,7 @@ _SPI_READY = const(0x01)
def reverse_bit(num): def reverse_bit(num):
"""Turn an LSB byte to an MSB byte, and vice versa. Used for SPI as """Turn an LSB byte to an MSB byte, and vice versa. Used for SPI as
it is LSB for the PN532, but 99% of SPI implementations are MSB only!""" it is LSB for the PN532, but 99% of SPI implementations are MSB only!"""
result = 0 return int('{:08b}'.format(num)[::-1], 2)
for _ in range(8):
result <<= 1
result += (num & 1)
num >>= 1
return result
class PN532_SPI(PN532): class PN532_SPI(PN532):
"""Driver for the PN532 connected over SPI. Pass in a hardware or bitbang """Driver for the PN532 connected over SPI. Pass in a hardware or bitbang
@ -83,12 +78,12 @@ class PN532_SPI(PN532):
timestamp = time.monotonic() timestamp = time.monotonic()
while (time.monotonic() - timestamp) < timeout: while (time.monotonic() - timestamp) < timeout:
with self._spi as spi: with self._spi as spi:
time.sleep(0.02) # required #time.sleep(0.02) # required
spi.write_readinto(status, status) #pylint: disable=no-member spi.write_readinto(status, status) #pylint: disable=no-member
if reverse_bit(status[1]) == 0x01: # LSB data is read in MSB if reverse_bit(status[1]) == 0x01: # LSB data is read in MSB
return True # Not busy anymore! return True # Not busy anymore!
else: #else:
time.sleep(0.01) # pause a bit till we ask again # time.sleep(0.01) # pause a bit till we ask again
# We timed out! # We timed out!
return False return False
@ -100,12 +95,12 @@ class PN532_SPI(PN532):
frame[0] = reverse_bit(_SPI_DATAREAD) frame[0] = reverse_bit(_SPI_DATAREAD)
with self._spi as spi: with self._spi as spi:
time.sleep(0.02) # required #time.sleep(0.02) # required
spi.write_readinto(frame, frame) #pylint: disable=no-member spi.write_readinto(frame, frame) #pylint: disable=no-member
for i, val in enumerate(frame): for i, val in enumerate(frame):
frame[i] = reverse_bit(val) # turn LSB data to MSB frame[i] = reverse_bit(val) # turn LSB data to MSB
if self.debug: #if self.debug:
print("Reading: ", [hex(i) for i in frame[1:]]) # print("Reading: ", [hex(i) for i in frame[1:]])
return frame[1:] return frame[1:]
def _write_data(self, framebytes): def _write_data(self, framebytes):
@ -113,8 +108,8 @@ class PN532_SPI(PN532):
# start by making a frame with data write in front, # start by making a frame with data write in front,
# then rest of bytes, and LSBify it # then rest of bytes, and LSBify it
rev_frame = [reverse_bit(x) for x in bytes([_SPI_DATAWRITE]) + framebytes] rev_frame = [reverse_bit(x) for x in bytes([_SPI_DATAWRITE]) + framebytes]
if self.debug: #if self.debug:
print("Writing: ", [hex(i) for i in rev_frame]) # print("Writing: ", [hex(i) for i in rev_frame])
with self._spi as spi: with self._spi as spi:
time.sleep(0.02) # required #time.sleep(0.02) # required
spi.write(bytes(rev_frame)) #pylint: disable=no-member spi.write(bytes(rev_frame)) #pylint: disable=no-member