MicroPython_PN532/pn532/i2c.py

131 lines
4.5 KiB
Python
Raw Normal View History

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.i2c``
====================================================
This module will let you communicate with a PN532 RFID/NFC shield or breakout
using I2C.
* Author(s): Original Raspberry Pi code by Tony DiCola, CircuitPython by ladyada,
refactor by Carter Nelson
"""
2022-08-16 22:09:14 +00:00
__version__ = "0.0.0+auto.0"
2018-08-28 19:36:54 +00:00
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PN532.git"
import time
from machine import I2C, Pin
2018-08-28 19:36:54 +00:00
from micropython import const
from pn532.pn532 import PN532, BusyError
2018-08-28 19:36:54 +00:00
2023-02-09 20:12:59 +00:00
try:
from typing import Optional, Union
2023-02-09 20:12:59 +00:00
except ImportError:
pass
2020-03-16 20:00:49 +00:00
_I2C_ADDRESS = const(0x24)
2023-02-27 01:34:00 +00:00
2018-08-28 19:36:54 +00:00
class PN532_I2C(PN532):
"""Driver for the PN532 connected over I2C."""
2020-03-16 20:00:49 +00:00
2023-02-09 20:12:59 +00:00
def __init__(
self,
i2c: I2C,
address: int = _I2C_ADDRESS,
2023-02-09 20:12:59 +00:00
*,
irq: Optional[DigitalInOut] = None,
reset: Optional[DigitalInOut] = None,
req: Optional[DigitalInOut] = None,
debug: bool = False
) -> None:
2018-08-28 19:36:54 +00:00
"""Create an instance of the PN532 class using I2C. Note that PN532
uses clock stretching. Optional IRQ pin (not used),
2022-01-04 23:19:13 +00:00
resetp pin and debugging output.
2023-03-20 01:08:14 +00:00
2023-03-21 18:49:32 +00:00
:param ~busio.I2C i2c: The I2C bus the PN532 is connected to.
2023-03-20 01:08:14 +00:00
:param int address: The I2C device address. Defaults to :const:`0x24`
:param digitalio.DigitalInOut irq: board pin the PN532 IRQ is connected to
:param digitalio.DigitalInOut reset: board pin the PN532 RSTOUT_N is connected to
2023-03-21 18:49:32 +00:00
:param digitalio.DigitalInOut req: board pin the PN532 P32 is connected to
:param bool debug: if True print additional debug statements. Defaults to False
2023-03-20 01:08:14 +00:00
**Quickstart: Importing and using the device**
2023-03-21 18:49:32 +00:00
Here is an example of using the :class:`PN532_I2C` class.
First you will need to import the libraries to use the sensor
2023-03-20 01:08:14 +00:00
2023-03-21 18:49:32 +00:00
Once this is done you can define your `board.I2C` object and define your object
2023-03-20 01:08:14 +00:00
2023-03-21 18:49:32 +00:00
.. code-block:: python
2023-03-20 01:08:14 +00:00
i2c = machine.I2C(0, scl=SCL_PIN, sda=SDA_PIN)
pn532 = PN532_I2C(i2c, debug=False, reset=RESET_PIN, req=REQ_PIN)
2023-03-21 18:49:32 +00:00
# Configure PN532 to communicate with MiFare cards
pn532.SAM_configuration()
2023-03-20 01:08:14 +00:00
2023-03-21 18:49:32 +00:00
Now you have access to the attributes and functions of the PN532 RFID/NFC
shield or breakout
.. code-block:: python
uid = pn532.read_passive_target(timeout=0.5)
2023-03-20 01:08:14 +00:00
2018-08-28 19:36:54 +00:00
"""
self.debug = debug
self._req = req
self._i2c = i2c
super().__init__(debug=debug, irq=irq, reset=reset)
2018-08-28 19:36:54 +00:00
2023-02-09 20:12:59 +00:00
def _wakeup(self) -> None:
2018-08-28 19:36:54 +00:00
"""Send any special commands/data to wake up PN532"""
if self._reset_pin:
self._reset_pin.value = True
time.sleep(0.01)
2018-08-28 19:36:54 +00:00
if self._req:
self._req.direction = Direction.OUTPUT
self._req.value(False)
time.sleep(0.01)
self._req.value(True)
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
2023-02-09 20:12:59 +00:00
def _wait_ready(self, timeout: float = 1) -> bool:
2018-08-28 19:36:54 +00:00
"""Poll PN532 if status byte is ready, up to `timeout` seconds"""
status = bytearray(1)
timestamp = time.monotonic()
while (time.monotonic() - timestamp) < timeout:
try:
with self._i2c:
self._i2c.readfrom_into(address, status)
2018-08-28 19:36:54 +00:00
except OSError:
continue
2020-03-16 20:00:49 +00:00
if status == b"\x01":
2018-08-28 19:36:54 +00:00
return True # No longer busy
2023-02-11 16:09:29 +00:00
time.sleep(0.01) # let's ask again soon!
2018-08-28 19:36:54 +00:00
# Timed out!
return False
2023-02-09 20:12:59 +00:00
def _read_data(self, count: int) -> bytearray:
2018-08-28 19:36:54 +00:00
"""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
with self._i2c as i2c:
i2c.readfrom_into(address, frame, stop=1) # read status byte!
2020-03-16 20:00:49 +00:00
if frame[0] != 0x01: # not ready
2018-08-28 20:21:44 +00:00
raise BusyError
i2c.readfrom_into(address, frame) # ok get the data, plus statusbyte
2018-08-28 19:36:54 +00:00
if self.debug:
print("Reading: ", [hex(i) for i in frame[1:]])
2020-03-16 20:00:49 +00:00
return frame[1:] # don't return the status byte
2018-08-28 19:36:54 +00:00
2023-02-11 16:09:29 +00:00
def _write_data(self, framebytes: bytes) -> None:
2018-08-28 19:36:54 +00:00
"""Write a specified count of bytes to the PN532"""
with self._i2c as i2c:
i2c.writeto(address, framebytes)