/ adafruit_pn532 / i2c.py
i2c.py
1 # Adafruit PN532 NFC/RFID control library. 2 # Author: Tony DiCola 3 # 4 # The MIT License (MIT) 5 # 6 # Copyright (c) 2015-2018 Adafruit Industries 7 # 8 # Permission is hereby granted, free of charge, to any person obtaining a copy 9 # of this software and associated documentation files (the "Software"), to deal 10 # in the Software without restriction, including without limitation the rights 11 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 # copies of the Software, and to permit persons to whom the Software is 13 # furnished to do so, subject to the following conditions: 14 # 15 # The above copyright notice and this permission notice shall be included in 16 # all copies or substantial portions of the Software. 17 # 18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 # THE SOFTWARE. 25 """ 26 ``adafruit_pn532.i2c`` 27 ==================================================== 28 29 This module will let you communicate with a PN532 RFID/NFC shield or breakout 30 using I2C. 31 32 * Author(s): Original Raspberry Pi code by Tony DiCola, CircuitPython by ladyada, 33 refactor by Carter Nelson 34 35 """ 36 37 __version__ = "0.0.0-auto.0" 38 __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PN532.git" 39 40 import time 41 import adafruit_bus_device.i2c_device as i2c_device 42 from digitalio import Direction 43 from micropython import const 44 from adafruit_pn532.adafruit_pn532 import PN532, BusyError, _reset 45 46 # pylint: disable=bad-whitespace 47 _I2C_ADDRESS = const(0x24) 48 49 50 class PN532_I2C(PN532): 51 """Driver for the PN532 connected over I2C.""" 52 53 def __init__(self, i2c, *, irq=None, reset=None, req=None, debug=False): 54 """Create an instance of the PN532 class using I2C. Note that PN532 55 uses clock stretching. Optional IRQ pin (not used), 56 reset pin and debugging output. 57 """ 58 self.debug = debug 59 self._irq = irq 60 self._req = req 61 if reset: 62 _reset(reset) 63 self._i2c = i2c_device.I2CDevice(i2c, _I2C_ADDRESS) 64 super().__init__(debug=debug, reset=reset) 65 66 def _wakeup(self): # pylint: disable=no-self-use 67 """Send any special commands/data to wake up PN532""" 68 if self._req: 69 self._req.direction = Direction.OUTPUT 70 self._req.value = True 71 time.sleep(0.1) 72 self._req.value = False 73 time.sleep(0.1) 74 self._req.value = True 75 time.sleep(0.5) 76 77 def _wait_ready(self, timeout=1): 78 """Poll PN532 if status byte is ready, up to `timeout` seconds""" 79 status = bytearray(1) 80 timestamp = time.monotonic() 81 while (time.monotonic() - timestamp) < timeout: 82 try: 83 with self._i2c: 84 self._i2c.readinto(status) 85 except OSError: 86 self._wakeup() 87 continue 88 if status == b"\x01": 89 return True # No longer busy 90 time.sleep(0.05) # lets ask again soon! 91 # Timed out! 92 return False 93 94 def _read_data(self, count): 95 """Read a specified count of bytes from the PN532.""" 96 # Build a read request frame. 97 frame = bytearray(count + 1) 98 with self._i2c as i2c: 99 i2c.readinto(frame, end=1) # read status byte! 100 if frame[0] != 0x01: # not ready 101 raise BusyError 102 i2c.readinto(frame) # ok get the data, plus statusbyte 103 if self.debug: 104 print("Reading: ", [hex(i) for i in frame[1:]]) 105 else: 106 time.sleep(0.1) 107 return frame[1:] # don't return the status byte 108 109 def _write_data(self, framebytes): 110 """Write a specified count of bytes to the PN532""" 111 with self._i2c as i2c: 112 i2c.write(framebytes)