uart support

This commit is contained in:
ladyada 2018-08-25 01:32:00 -04:00
parent 72fa49206d
commit caa34fadff
2 changed files with 73 additions and 1 deletions

View file

@ -174,6 +174,7 @@ class PN532:
reset.value = True reset.value = True
time.sleep(1) time.sleep(1)
try: try:
self._wakeup()
self.get_firmware_version() # first time often fails, try 2ce self.get_firmware_version() # first time often fails, try 2ce
return return
except: except:
@ -389,6 +390,38 @@ class PN532:
return response[0] == 0x0 return response[0] == 0x0
class PN532_UART(PN532):
"""Driver for the PN532 connected over Serial UART"""
def __init__(self, uart, *, irq=None, reset=None, debug=False):
"""Create an instance of the PN532 class using Serial connection
"""
self.debug = debug
self._irq = irq
self._uart = uart
super().__init__(debug=debug, reset=reset)
def _wakeup(self):
#self._write_frame([_HOSTTOPN532, _COMMAND_SAMCONFIGURATION, 0x01])
self.SAM_configuration()
def _wait_ready(self, timeout=1):
time.sleep(timeout)
return True
def _read_data(self, count):
"""Read a specified count of bytes from the PN532."""
frame = self._uart.read(count)
if not frame:
raise BusyError("No data read from PN532")
if self.debug:
print("Reading: ", [hex(i) for i in frame])
return frame
def _write_data(self, framebytes):
while self._uart.read(1): # this would be a lot nicer if we could query the # of bytes
pass
self._uart.write('\x55\x55\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') # wake up!
self._uart.write(framebytes)
class PN532_I2C(PN532): class PN532_I2C(PN532):
"""Driver for the PN532 connected over I2C.""" """Driver for the PN532 connected over I2C."""
@ -404,6 +437,9 @@ class PN532_I2C(PN532):
self._i2c = i2c_device.I2CDevice(i2c, _I2C_ADDRESS) self._i2c = i2c_device.I2CDevice(i2c, _I2C_ADDRESS)
super().__init__(debug=debug, reset=reset) super().__init__(debug=debug, reset=reset)
def _wakeup(self):
time.sleep(0.5)
def _wait_ready(self, timeout=1): def _wait_ready(self, timeout=1):
status = bytearray(1) status = bytearray(1)
t = time.monotonic() t = time.monotonic()
@ -444,6 +480,10 @@ class PN532_SPI(PN532):
self._spi = spi_device.SPIDevice(spi, cs_pin) self._spi = spi_device.SPIDevice(spi, cs_pin)
super().__init__(debug=debug, reset=reset) super().__init__(debug=debug, reset=reset)
def _wakeup(self):
with self._spi as spi:
time.sleep(1)
def _wait_ready(self, timeout=1): def _wait_ready(self, timeout=1):
status = bytearray([reverse_bit(_SPI_STATREAD), 0]) status = bytearray([reverse_bit(_SPI_STATREAD), 0])

View file

@ -1,4 +1,36 @@
ush=True) from Adafruit_Circuitpython_PN532 import adafruit_pn532
from digitalio import DigitalInOut, Direction, Pull
import board
import time
import busio
reset_pin = DigitalInOut(board.D3)
# I2C connection:
#i2c = busio.I2C(board.SCL, board.SDA)
#pn532 = adafruit_pn532.PN532_I2C(i2c, debug=False, reset=reset_pin)
# SPI connection:
#spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
#cs_pin = DigitalInOut(board.D2)
#pn532 = adafruit_pn532.PN532_SPI(spi, cs_pin, debug=False, reset=reset_pin)
# UART connection
#uart = busio.UART(board.TX, board.RX, baudrate=115200, timeout=100)
#pn532 = adafruit_pn532.PN532_UART(uart, debug=False, reset=reset_pin)
ic, ver, rev, support = pn532.get_firmware_version()
print('Found PN532 with firmware version: {0}.{1}'.format(ver, rev))
# Configure PN532 to communicate with MiFare cards
pn532.SAM_configuration()
print('Waiting for MiFare card...')
while True:
# Check if a card is available to read
uid = pn532.read_passive_target(timeout=0.25)
print('.', end="", flush=True)
# Try again if no card is available. # Try again if no card is available.
if uid is None: if uid is None:
continue continue