muezza29

Adafruit_PN532.h

Jun 29th, 2019
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.35 KB | None | 0 0
  1. /**************************************************************************/
  2. /*!
  3.     @file     Adafruit_PN532.h
  4.     @author   Adafruit Industries
  5.     @license  BSD (see license.txt)
  6.    
  7.  
  8.     This is a library for the Adafruit PN532 NFC/RFID breakout boards
  9.     This library works with the Adafruit NFC breakout
  10.     ----> https://www.adafruit.com/products/364
  11.    
  12.     Check out the links above for our tutorials and wiring diagrams
  13.   These chips use SPI or I2C to communicate.
  14.    
  15.     Adafruit invests time and resources providing this open source code,
  16.     please support Adafruit and open-source hardware by purchasing
  17.     products from Adafruit!
  18.  
  19.     @section  HISTORY
  20.  
  21.   v2.0  - Refactored to add I2C support from Adafruit_NFCShield_I2C library.
  22.  
  23.     v1.1  - Added full command list
  24.           - Added 'verbose' mode flag to constructor to toggle debug output
  25.           - Changed readPassiveTargetID() to return variable length values
  26.    
  27. */
  28. /**************************************************************************/
  29.  
  30. #ifndef ADAFRUIT_PN532_H
  31. #define ADAFRUIT_PN532_H
  32.  
  33. #if ARDUINO >= 100
  34.  #include "Arduino.h"
  35. #else
  36.  #include "WProgram.h"
  37. #endif
  38.  
  39. #define PN532_PREAMBLE                      (0x00)
  40. #define PN532_STARTCODE1                    (0x00)
  41. #define PN532_STARTCODE2                    (0xFF)
  42. #define PN532_POSTAMBLE                     (0x00)
  43.  
  44. #define PN532_HOSTTOPN532                   (0xD4)
  45. #define PN532_PN532TOHOST                   (0xD5)
  46.  
  47. // PN532 Commands
  48. #define PN532_COMMAND_DIAGNOSE              (0x00)
  49. #define PN532_COMMAND_GETFIRMWAREVERSION    (0x02)
  50. #define PN532_COMMAND_GETGENERALSTATUS      (0x04)
  51. #define PN532_COMMAND_READREGISTER          (0x06)
  52. #define PN532_COMMAND_WRITEREGISTER         (0x08)
  53. #define PN532_COMMAND_READGPIO              (0x0C)
  54. #define PN532_COMMAND_WRITEGPIO             (0x0E)
  55. #define PN532_COMMAND_SETSERIALBAUDRATE     (0x10)
  56. #define PN532_COMMAND_SETPARAMETERS         (0x12)
  57. #define PN532_COMMAND_SAMCONFIGURATION      (0x14)
  58. #define PN532_COMMAND_POWERDOWN             (0x16)
  59. #define PN532_COMMAND_RFCONFIGURATION       (0x32)
  60. #define PN532_COMMAND_RFREGULATIONTEST      (0x58)
  61. #define PN532_COMMAND_INJUMPFORDEP          (0x56)
  62. #define PN532_COMMAND_INJUMPFORPSL          (0x46)
  63. #define PN532_COMMAND_INLISTPASSIVETARGET   (0x4A)
  64. #define PN532_COMMAND_INATR                 (0x50)
  65. #define PN532_COMMAND_INPSL                 (0x4E)
  66. #define PN532_COMMAND_INDATAEXCHANGE        (0x40)
  67. #define PN532_COMMAND_INCOMMUNICATETHRU     (0x42)
  68. #define PN532_COMMAND_INDESELECT            (0x44)
  69. #define PN532_COMMAND_INRELEASE             (0x52)
  70. #define PN532_COMMAND_INSELECT              (0x54)
  71. #define PN532_COMMAND_INAUTOPOLL            (0x60)
  72. #define PN532_COMMAND_TGINITASTARGET        (0x8C)
  73. #define PN532_COMMAND_TGSETGENERALBYTES     (0x92)
  74. #define PN532_COMMAND_TGGETDATA             (0x86)
  75. #define PN532_COMMAND_TGSETDATA             (0x8E)
  76. #define PN532_COMMAND_TGSETMETADATA         (0x94)
  77. #define PN532_COMMAND_TGGETINITIATORCOMMAND (0x88)
  78. #define PN532_COMMAND_TGRESPONSETOINITIATOR (0x90)
  79. #define PN532_COMMAND_TGGETTARGETSTATUS     (0x8A)
  80.  
  81. #define PN532_RESPONSE_INDATAEXCHANGE       (0x41)
  82. #define PN532_RESPONSE_INLISTPASSIVETARGET  (0x4B)
  83.  
  84. #define PN532_WAKEUP                        (0x55)
  85.  
  86. #define PN532_SPI_STATREAD                  (0x02)
  87. #define PN532_SPI_DATAWRITE                 (0x01)
  88. #define PN532_SPI_DATAREAD                  (0x03)
  89. #define PN532_SPI_READY                     (0x01)
  90.  
  91. #define PN532_I2C_ADDRESS                   (0x48 >> 1)
  92. #define PN532_I2C_READBIT                   (0x01)
  93. #define PN532_I2C_BUSY                      (0x00)
  94. #define PN532_I2C_READY                     (0x01)
  95. #define PN532_I2C_READYTIMEOUT              (20)
  96.  
  97. #define PN532_MIFARE_ISO14443A              (0x00)
  98.  
  99. // Mifare Commands
  100. #define MIFARE_CMD_AUTH_A                   (0x60)
  101. #define MIFARE_CMD_AUTH_B                   (0x61)
  102. #define MIFARE_CMD_READ                     (0x30)
  103. #define MIFARE_CMD_WRITE                    (0xA0)
  104. #define MIFARE_CMD_TRANSFER                 (0xB0)
  105. #define MIFARE_CMD_DECREMENT                (0xC0)
  106. #define MIFARE_CMD_INCREMENT                (0xC1)
  107. #define MIFARE_CMD_STORE                    (0xC2)
  108. #define MIFARE_ULTRALIGHT_CMD_WRITE         (0xA2)
  109.  
  110. // Prefixes for NDEF Records (to identify record type)
  111. #define NDEF_URIPREFIX_NONE                 (0x00)
  112. #define NDEF_URIPREFIX_HTTP_WWWDOT          (0x01)
  113. #define NDEF_URIPREFIX_HTTPS_WWWDOT         (0x02)
  114. #define NDEF_URIPREFIX_HTTP                 (0x03)
  115. #define NDEF_URIPREFIX_HTTPS                (0x04)
  116. #define NDEF_URIPREFIX_TEL                  (0x05)
  117. #define NDEF_URIPREFIX_MAILTO               (0x06)
  118. #define NDEF_URIPREFIX_FTP_ANONAT           (0x07)
  119. #define NDEF_URIPREFIX_FTP_FTPDOT           (0x08)
  120. #define NDEF_URIPREFIX_FTPS                 (0x09)
  121. #define NDEF_URIPREFIX_SFTP                 (0x0A)
  122. #define NDEF_URIPREFIX_SMB                  (0x0B)
  123. #define NDEF_URIPREFIX_NFS                  (0x0C)
  124. #define NDEF_URIPREFIX_FTP                  (0x0D)
  125. #define NDEF_URIPREFIX_DAV                  (0x0E)
  126. #define NDEF_URIPREFIX_NEWS                 (0x0F)
  127. #define NDEF_URIPREFIX_TELNET               (0x10)
  128. #define NDEF_URIPREFIX_IMAP                 (0x11)
  129. #define NDEF_URIPREFIX_RTSP                 (0x12)
  130. #define NDEF_URIPREFIX_URN                  (0x13)
  131. #define NDEF_URIPREFIX_POP                  (0x14)
  132. #define NDEF_URIPREFIX_SIP                  (0x15)
  133. #define NDEF_URIPREFIX_SIPS                 (0x16)
  134. #define NDEF_URIPREFIX_TFTP                 (0x17)
  135. #define NDEF_URIPREFIX_BTSPP                (0x18)
  136. #define NDEF_URIPREFIX_BTL2CAP              (0x19)
  137. #define NDEF_URIPREFIX_BTGOEP               (0x1A)
  138. #define NDEF_URIPREFIX_TCPOBEX              (0x1B)
  139. #define NDEF_URIPREFIX_IRDAOBEX             (0x1C)
  140. #define NDEF_URIPREFIX_FILE                 (0x1D)
  141. #define NDEF_URIPREFIX_URN_EPC_ID           (0x1E)
  142. #define NDEF_URIPREFIX_URN_EPC_TAG          (0x1F)
  143. #define NDEF_URIPREFIX_URN_EPC_PAT          (0x20)
  144. #define NDEF_URIPREFIX_URN_EPC_RAW          (0x21)
  145. #define NDEF_URIPREFIX_URN_EPC              (0x22)
  146. #define NDEF_URIPREFIX_URN_NFC              (0x23)
  147.  
  148. #define PN532_GPIO_VALIDATIONBIT            (0x80)
  149. #define PN532_GPIO_P30                      (0)
  150. #define PN532_GPIO_P31                      (1)
  151. #define PN532_GPIO_P32                      (2)
  152. #define PN532_GPIO_P33                      (3)
  153. #define PN532_GPIO_P34                      (4)
  154. #define PN532_GPIO_P35                      (5)
  155.  
  156. class Adafruit_PN532{
  157.  public:
  158.   Adafruit_PN532(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t ss);  // Software SPI
  159.   Adafruit_PN532(uint8_t irq, uint8_t reset);  // Hardware I2C
  160.   Adafruit_PN532(uint8_t ss);  // Hardware SPI
  161.   void begin(void);
  162.  
  163.   // Generic PN532 functions
  164.   bool     SAMConfig(void);
  165.   uint32_t getFirmwareVersion(void);
  166.   bool     sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, uint16_t timeout = 1000);  
  167.   bool     writeGPIO(uint8_t pinstate);
  168.   uint8_t  readGPIO(void);
  169.   bool     setPassiveActivationRetries(uint8_t maxRetries);
  170.  
  171.   // ISO14443A functions
  172.   bool readPassiveTargetID(uint8_t cardbaudrate, uint8_t * uid, uint8_t * uidLength, uint16_t timeout = 0); //timeout 0 means no timeout - will block forever.
  173.   bool inDataExchange(uint8_t * send, uint8_t sendLength, uint8_t * response, uint8_t * responseLength);
  174.   bool inListPassiveTarget();
  175.  
  176.   // Mifare Classic functions
  177.   bool    mifareclassic_IsFirstBlock (uint32_t uiBlock);
  178.   bool    mifareclassic_IsTrailerBlock (uint32_t uiBlock);
  179.   uint8_t mifareclassic_AuthenticateBlock (uint8_t * uid, uint8_t uidLen, uint32_t blockNumber, uint8_t keyNumber, uint8_t * keyData);
  180.   uint8_t mifareclassic_ReadDataBlock (uint8_t blockNumber, uint8_t * data);
  181.   uint8_t mifareclassic_WriteDataBlock (uint8_t blockNumber, uint8_t * data);
  182.   uint8_t mifareclassic_FormatNDEF (void);
  183.   uint8_t mifareclassic_WriteNDEFURI (uint8_t sectorNumber, uint8_t uriIdentifier, const char * url);
  184.  
  185.   // Mifare Ultralight functions
  186.   uint8_t mifareultralight_ReadPage (uint8_t page, uint8_t * buffer);
  187.   uint8_t mifareultralight_WritePage (uint8_t page, uint8_t * data);
  188.  
  189.   // NTAG2xx functions
  190.   uint8_t ntag2xx_ReadPage (uint8_t page, uint8_t * buffer);
  191.   uint8_t ntag2xx_WritePage (uint8_t page, uint8_t * data);
  192.   uint8_t ntag2xx_WriteNDEFURI (uint8_t uriIdentifier, char * url, uint8_t dataLen);
  193.  
  194.   // Help functions to display formatted text
  195.   static void PrintHex(const byte * data, const uint32_t numBytes);
  196.   static void PrintHexChar(const byte * pbtData, const uint32_t numBytes);
  197.  
  198.  private:
  199.   uint8_t _ss, _clk, _mosi, _miso;
  200.   uint8_t _irq, _reset;
  201.   uint8_t _uid[7];       // ISO14443A uid
  202.   uint8_t _uidLen;       // uid len
  203.   uint8_t _key[6];       // Mifare Classic key
  204.   uint8_t _inListedTag;  // Tg number of inlisted tag.
  205.   bool    _usingSPI;     // True if using SPI, false if using I2C.
  206.   bool    _hardwareSPI;  // True is using hardware SPI, false if using software SPI.
  207.  
  208.   // Low level communication functions that handle both SPI and I2C.
  209.   void readdata(uint8_t* buff, uint8_t n);
  210.   void writecommand(uint8_t* cmd, uint8_t cmdlen);
  211.   bool isready();
  212.   bool waitready(uint16_t timeout);
  213.   bool readack();
  214.  
  215.   // SPI-specific functions.
  216.   void    spi_write(uint8_t c);
  217.   uint8_t spi_read(void);
  218.  
  219.   // Note there are i2c_read and i2c_write inline functions defined in the .cpp file.
  220. };
  221.  
  222. #endif
Add Comment
Please, Sign In to add comment