Skip to content

Commit caa34fa

Browse files
committed
uart support
1 parent 72fa492 commit caa34fa

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

adafruit_pn532.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ def __init__(self, *, debug=False, reset=None):
174174
reset.value = True
175175
time.sleep(1)
176176
try:
177+
self._wakeup()
177178
self.get_firmware_version() # first time often fails, try 2ce
178179
return
179180
except:
@@ -389,6 +390,38 @@ def mifare_classic_write_block(self, block_number, data):
389390
return response[0] == 0x0
390391

391392

393+
class PN532_UART(PN532):
394+
"""Driver for the PN532 connected over Serial UART"""
395+
def __init__(self, uart, *, irq=None, reset=None, debug=False):
396+
"""Create an instance of the PN532 class using Serial connection
397+
"""
398+
self.debug = debug
399+
self._irq = irq
400+
self._uart = uart
401+
super().__init__(debug=debug, reset=reset)
402+
403+
def _wakeup(self):
404+
#self._write_frame([_HOSTTOPN532, _COMMAND_SAMCONFIGURATION, 0x01])
405+
self.SAM_configuration()
406+
407+
def _wait_ready(self, timeout=1):
408+
time.sleep(timeout)
409+
return True
410+
411+
def _read_data(self, count):
412+
"""Read a specified count of bytes from the PN532."""
413+
frame = self._uart.read(count)
414+
if not frame:
415+
raise BusyError("No data read from PN532")
416+
if self.debug:
417+
print("Reading: ", [hex(i) for i in frame])
418+
return frame
419+
420+
def _write_data(self, framebytes):
421+
while self._uart.read(1): # this would be a lot nicer if we could query the # of bytes
422+
pass
423+
self._uart.write('\x55\x55\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') # wake up!
424+
self._uart.write(framebytes)
392425

393426
class PN532_I2C(PN532):
394427
"""Driver for the PN532 connected over I2C."""
@@ -404,6 +437,9 @@ def __init__(self, i2c, *, irq=None, reset=None, debug=False):
404437
self._i2c = i2c_device.I2CDevice(i2c, _I2C_ADDRESS)
405438
super().__init__(debug=debug, reset=reset)
406439

440+
def _wakeup(self):
441+
time.sleep(0.5)
442+
407443
def _wait_ready(self, timeout=1):
408444
status = bytearray(1)
409445
t = time.monotonic()
@@ -444,6 +480,10 @@ def __init__(self, spi, cs_pin, *, irq=None, reset=None, debug=False):
444480
self._spi = spi_device.SPIDevice(spi, cs_pin)
445481
super().__init__(debug=debug, reset=reset)
446482

483+
def _wakeup(self):
484+
with self._spi as spi:
485+
time.sleep(1)
486+
447487
def _wait_ready(self, timeout=1):
448488
status = bytearray([reverse_bit(_SPI_STATREAD), 0])
449489

examples/main.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,36 @@
1-
ush=True)
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+
7+
reset_pin = DigitalInOut(board.D3)
8+
9+
10+
# I2C connection:
11+
#i2c = busio.I2C(board.SCL, board.SDA)
12+
#pn532 = adafruit_pn532.PN532_I2C(i2c, debug=False, reset=reset_pin)
13+
14+
# SPI connection:
15+
#spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
16+
#cs_pin = DigitalInOut(board.D2)
17+
#pn532 = adafruit_pn532.PN532_SPI(spi, cs_pin, debug=False, reset=reset_pin)
18+
19+
# UART connection
20+
#uart = busio.UART(board.TX, board.RX, baudrate=115200, timeout=100)
21+
#pn532 = adafruit_pn532.PN532_UART(uart, debug=False, reset=reset_pin)
22+
23+
ic, ver, rev, support = pn532.get_firmware_version()
24+
print('Found PN532 with firmware version: {0}.{1}'.format(ver, rev))
25+
26+
# Configure PN532 to communicate with MiFare cards
27+
pn532.SAM_configuration()
28+
29+
print('Waiting for MiFare card...')
30+
while True:
31+
# Check if a card is available to read
32+
uid = pn532.read_passive_target(timeout=0.25)
33+
print('.', end="", flush=True)
234
# Try again if no card is available.
335
if uid is None:
436
continue

0 commit comments

Comments
 (0)