2021-01-14 16:45:11 +00:00
|
|
|
# SPDX-FileCopyrightText: 2015-2018 Tony DiCola for Adafruit Industries
|
2018-08-28 19:36:54 +00:00
|
|
|
#
|
2021-01-14 16:45:11 +00:00
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2018-08-28 19:36:54 +00:00
|
|
|
"""
|
|
|
|
``adafruit_pn532.spi``
|
|
|
|
====================================================
|
|
|
|
|
|
|
|
This module will let you communicate with a PN532 RFID/NFC shield or breakout
|
|
|
|
using SPI.
|
|
|
|
|
|
|
|
* Author(s): Original Raspberry Pi code by Tony DiCola, CircuitPython by ladyada,
|
|
|
|
refactor by Carter Nelson
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
__version__ = "0.0.0-auto.0"
|
|
|
|
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PN532.git"
|
|
|
|
|
|
|
|
import time
|
|
|
|
import adafruit_bus_device.spi_device as spi_device
|
|
|
|
from micropython import const
|
2018-08-28 20:26:02 +00:00
|
|
|
from adafruit_pn532.adafruit_pn532 import PN532
|
2018-08-28 19:36:54 +00:00
|
|
|
|
2020-03-16 20:00:49 +00:00
|
|
|
_SPI_STATREAD = const(0x02)
|
|
|
|
_SPI_DATAWRITE = const(0x01)
|
|
|
|
_SPI_DATAREAD = const(0x03)
|
|
|
|
_SPI_READY = const(0x01)
|
|
|
|
|
2018-08-28 19:36:54 +00:00
|
|
|
|
|
|
|
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!"""
|
2019-07-03 19:12:18 +00:00
|
|
|
result = 0
|
|
|
|
for _ in range(8):
|
|
|
|
result <<= 1
|
2020-03-16 20:00:49 +00:00
|
|
|
result += num & 1
|
2019-07-03 19:12:18 +00:00
|
|
|
num >>= 1
|
|
|
|
return result
|
2018-08-28 19:36:54 +00:00
|
|
|
|
2020-03-16 20:00:49 +00:00
|
|
|
|
2018-08-28 19:36:54 +00:00
|
|
|
class PN532_SPI(PN532):
|
|
|
|
"""Driver for the PN532 connected over SPI. Pass in a hardware or bitbang
|
|
|
|
SPI device & chip select digitalInOut pin. Optional IRQ pin (not used),
|
|
|
|
reset pin and debugging output."""
|
2020-03-16 20:00:49 +00:00
|
|
|
|
2018-08-28 19:36:54 +00:00
|
|
|
def __init__(self, spi, cs_pin, *, irq=None, reset=None, debug=False):
|
|
|
|
"""Create an instance of the PN532 class using SPI"""
|
|
|
|
self.debug = debug
|
|
|
|
self._spi = spi_device.SPIDevice(spi, cs_pin)
|
2020-08-31 01:36:06 +00:00
|
|
|
super().__init__(debug=debug, irq=irq, reset=reset)
|
2018-08-28 19:36:54 +00:00
|
|
|
|
|
|
|
def _wakeup(self):
|
|
|
|
"""Send any special commands/data to wake up PN532"""
|
2020-08-31 01:36:06 +00:00
|
|
|
if self._reset_pin:
|
|
|
|
self._reset_pin.value = True
|
|
|
|
time.sleep(0.01)
|
2018-08-28 19:36:54 +00:00
|
|
|
with self._spi as spi:
|
2020-03-16 20:00:49 +00:00
|
|
|
spi.write(bytearray([0x00])) # pylint: disable=no-member
|
2020-08-31 01:36:06 +00:00
|
|
|
time.sleep(0.01)
|
|
|
|
self.low_power = False
|
2020-09-10 19:42:03 +00:00
|
|
|
self.SAM_configuration() # Put the PN532 back in normal mode
|
2018-08-28 19:36:54 +00:00
|
|
|
|
|
|
|
def _wait_ready(self, timeout=1):
|
|
|
|
"""Poll PN532 if status byte is ready, up to `timeout` seconds"""
|
2020-01-05 20:58:13 +00:00
|
|
|
status_cmd = bytearray([reverse_bit(_SPI_STATREAD), 0x00])
|
|
|
|
status_response = bytearray([0x00, 0x00])
|
2018-08-28 19:36:54 +00:00
|
|
|
timestamp = time.monotonic()
|
2019-07-01 18:48:24 +00:00
|
|
|
with self._spi as spi:
|
|
|
|
while (time.monotonic() - timestamp) < timeout:
|
2020-03-16 20:00:49 +00:00
|
|
|
spi.write_readinto(
|
|
|
|
status_cmd, status_response
|
|
|
|
) # pylint: disable=no-member
|
2020-01-05 20:58:13 +00:00
|
|
|
if reverse_bit(status_response[1]) == 0x01: # LSB data is read in MSB
|
2020-03-16 20:00:49 +00:00
|
|
|
return True # Not busy anymore!
|
|
|
|
time.sleep(0.01) # pause a bit till we ask again
|
2018-08-28 19:36:54 +00:00
|
|
|
# We timed out!
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _read_data(self, count):
|
|
|
|
"""Read a specified count of bytes from the PN532."""
|
|
|
|
# Build a read request frame.
|
2020-03-16 20:00:49 +00:00
|
|
|
frame = bytearray(count + 1)
|
2018-08-28 19:36:54 +00:00
|
|
|
# Add the SPI data read signal byte, but LSB'ify it
|
|
|
|
frame[0] = reverse_bit(_SPI_DATAREAD)
|
|
|
|
|
|
|
|
with self._spi as spi:
|
2020-03-16 20:00:49 +00:00
|
|
|
spi.write_readinto(frame, frame) # pylint: disable=no-member
|
2018-08-28 19:36:54 +00:00
|
|
|
for i, val in enumerate(frame):
|
2020-03-16 20:00:49 +00:00
|
|
|
frame[i] = reverse_bit(val) # turn LSB data to MSB
|
2019-07-01 18:48:24 +00:00
|
|
|
if self.debug:
|
|
|
|
print("Reading: ", [hex(i) for i in frame[1:]])
|
2018-08-28 19:36:54 +00:00
|
|
|
return frame[1:]
|
|
|
|
|
|
|
|
def _write_data(self, framebytes):
|
|
|
|
"""Write a specified count of bytes to the PN532"""
|
|
|
|
# start by making a frame with data write in front,
|
|
|
|
# then rest of bytes, and LSBify it
|
|
|
|
rev_frame = [reverse_bit(x) for x in bytes([_SPI_DATAWRITE]) + framebytes]
|
2019-07-01 18:48:24 +00:00
|
|
|
if self.debug:
|
|
|
|
print("Writing: ", [hex(i) for i in rev_frame])
|
2018-08-28 19:36:54 +00:00
|
|
|
with self._spi as spi:
|
2020-03-16 20:00:49 +00:00
|
|
|
spi.write(bytes(rev_frame)) # pylint: disable=no-member
|