Skip to content

SPI read_passive_target timeout #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 3, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions adafruit_pn532/spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@
def reverse_bit(num):
"""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!"""
result = 0
for _ in range(8):
result <<= 1
result += (num & 1)
num >>= 1
return result
return int('{:08b}'.format(num)[::-1], 2)

class PN532_SPI(PN532):
"""Driver for the PN532 connected over SPI. Pass in a hardware or bitbang
Expand All @@ -81,14 +76,14 @@ def _wait_ready(self, timeout=1):
status = bytearray([reverse_bit(_SPI_STATREAD), 0])

timestamp = time.monotonic()
while (time.monotonic() - timestamp) < timeout:
with self._spi as spi:
time.sleep(0.02) # required
with self._spi as spi:
while (time.monotonic() - timestamp) < timeout:
time.sleep(0.02) # required (not needed when tested on rPi 3)
spi.write_readinto(status, status) #pylint: disable=no-member
if reverse_bit(status[1]) == 0x01: # LSB data is read in MSB
return True # Not busy anymore!
else:
time.sleep(0.01) # pause a bit till we ask again
if reverse_bit(status[1]) == 0x01: # LSB data is read in MSB
return True # Not busy anymore!
else: # (not needed when tested on rPi 3)
time.sleep(0.01) # pause a bit till we ask again
# We timed out!
return False

Expand All @@ -100,7 +95,7 @@ def _read_data(self, count):
frame[0] = reverse_bit(_SPI_DATAREAD)

with self._spi as spi:
time.sleep(0.02) # required
time.sleep(0.02) # required (not needed when tested on rPi 3)
spi.write_readinto(frame, frame) #pylint: disable=no-member
for i, val in enumerate(frame):
frame[i] = reverse_bit(val) # turn LSB data to MSB
Expand All @@ -116,5 +111,5 @@ def _write_data(self, framebytes):
if self.debug:
print("Writing: ", [hex(i) for i in rev_frame])
with self._spi as spi:
time.sleep(0.02) # required
time.sleep(0.02) # required (not needed when tested on rPi 3)
spi.write(bytes(rev_frame)) #pylint: disable=no-member