upm  0.3.2
Sensor/Actuator repository for libmraa (v0.7.2)
pn532.h
1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2015 Intel Corporation.
4  *
5  * This code is heavily based on the Adafruit-PN532 library at
6  * https://github.com/adafruit/Adafruit-PN532, which is licensed under
7  * the BSD license. See upm/src/pn532/license.txt
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  */
28 
29 
30 #pragma once
31 
32 #include <string.h>
33 #include <string>
34 #include <mraa/i2c.hpp>
35 #include <mraa/gpio.hpp>
36 
37 #define PN532_I2C_BUS 0
38 #define PN532_DEFAULT_I2C_ADDR (0x48 >> 1)
39 
40 #define PN532_PREAMBLE (0x00)
41 #define PN532_STARTCODE1 (0x00)
42 #define PN532_STARTCODE2 (0xFF)
43 #define PN532_POSTAMBLE (0x00)
44 
45 #define PN532_HOSTTOPN532 (0xD4)
46 #define PN532_PN532TOHOST (0xD5)
47 
48 namespace upm {
49 
72  class PN532 {
73  public:
74 
78  typedef enum {
79  CMD_DIAGNOSE = 0x00,
80  CMD_GETFIRMWAREVERSION = 0x02,
81  CMD_GETGENERALSTATUS = 0x04,
82  CMD_READREGISTER = 0x06,
83  CMD_WRITEREGISTER = 0x08,
84  CMD_READGPIO = 0x0C,
85  CMD_WRITEGPIO = 0x0E,
86  CMD_SETSERIALBAUDRATE = 0x10,
87  CMD_SETPARAMETERS = 0x12,
88  CMD_SAMCONFIGURATION = 0x14,
89  CMD_POWERDOWN = 0x16,
90  CMD_RFCONFIGURATION = 0x32,
91  CMD_RFREGULATIONTEST = 0x58,
92  CMD_INJUMPFORDEP = 0x56,
93  CMD_INJUMPFORPSL = 0x46,
94  CMD_INLISTPASSIVETARGET = 0x4A,
95  CMD_INATR = 0x50,
96  CMD_INPSL = 0x4E,
97  CMD_INDATAEXCHANGE = 0x40,
98  CMD_INCOMMUNICATETHRU = 0x42,
99  CMD_INDESELECT = 0x44,
100  CMD_INRELEASE = 0x52,
101  CMD_INSELECT = 0x54,
102  CMD_INAUTOPOLL = 0x60,
103  CMD_TGINITASTARGET = 0x8C,
104  CMD_TGSETGENERALBYTES = 0x92,
105  CMD_TGGETDATA = 0x86,
106  CMD_TGSETDATA = 0x8E,
107  CMD_TGSETMETADATA = 0x94,
108  CMD_TGGETINITIATORCOMMAND = 0x88,
109  CMD_TGRESPONSETOINITIATOR = 0x90,
110  CMD_TGGETTARGETSTATUS = 0x8A
111  } PN532_CMD_T;
112 
116  typedef enum {
117  RSP_INDATAEXCHANGE = 0x41,
118  RSP_INLISTPASSIVETARGET = 0x4B
119  } PN532_RSP_T;
120 
124  typedef enum {
125  MIFARE_CMD_AUTH_A = 0x60,
126  MIFARE_CMD_AUTH_B = 0x61,
127  MIFARE_CMD_READ = 0x30,
128  MIFARE_CMD_WRITE = 0xA0,
129  MIFARE_CMD_TRANSFER = 0xB0,
130  MIFARE_CMD_DECREMENT = 0xC0,
131  MIFARE_CMD_INCREMENT = 0xC1,
132  MIFARE_CMD_STORE = 0xC2,
133  MIFARE_ULTRALIGHT_CMD_WRITE = 0xA2
134  } MIFARE_CMD_T;
135 
139  typedef enum {
140  NDEF_URIPREFIX_NONE = 0x00,
141  NDEF_URIPREFIX_HTTP_WWWDOT = 0x01,
142  NDEF_URIPREFIX_HTTPS_WWWDOT = 0x02,
143  NDEF_URIPREFIX_HTTP = 0x03,
144  NDEF_URIPREFIX_HTTPS = 0x04,
145  NDEF_URIPREFIX_TEL = 0x05,
146  NDEF_URIPREFIX_MAILTO = 0x06,
147  NDEF_URIPREFIX_FTP_ANONAT = 0x07,
148  NDEF_URIPREFIX_FTP_FTPDOT = 0x08,
149  NDEF_URIPREFIX_FTPS = 0x09,
150  NDEF_URIPREFIX_SFTP = 0x0A,
151  NDEF_URIPREFIX_SMB = 0x0B,
152  NDEF_URIPREFIX_NFS = 0x0C,
153  NDEF_URIPREFIX_FTP = 0x0D,
154  NDEF_URIPREFIX_DAV = 0x0E,
155  NDEF_URIPREFIX_NEWS = 0x0F,
156  NDEF_URIPREFIX_TELNET = 0x10,
157  NDEF_URIPREFIX_IMAP = 0x11,
158  NDEF_URIPREFIX_RTSP = 0x12,
159  NDEF_URIPREFIX_URN = 0x13,
160  NDEF_URIPREFIX_POP = 0x14,
161  NDEF_URIPREFIX_SIP = 0x15,
162  NDEF_URIPREFIX_SIPS = 0x16,
163  NDEF_URIPREFIX_TFTP = 0x17,
164  NDEF_URIPREFIX_BTSPP = 0x18,
165  NDEF_URIPREFIX_BTL2CAP = 0x19,
166  NDEF_URIPREFIX_BTGOEP = 0x1A,
167  NDEF_URIPREFIX_TCPOBEX = 0x1B,
168  NDEF_URIPREFIX_IRDAOBEX = 0x1C,
169  NDEF_URIPREFIX_FILE = 0x1D,
170  NDEF_URIPREFIX_URN_EPC_ID = 0x1E,
171  NDEF_URIPREFIX_URN_EPC_TAG = 0x1F,
172  NDEF_URIPREFIX_URN_EPC_PAT = 0x20,
173  NDEF_URIPREFIX_URN_EPC_RAW = 0x21,
174  NDEF_URIPREFIX_URN_EPC = 0x22,
175  NDEF_URIPREFIX_URN_NFC = 0x23
176  } NDEF_URI_T;
177 
181  typedef enum {
182  BAUD_MIFARE_ISO14443A = 0x00 // 106 Kbit/s
183  } BAUD_T;
184 
185 
189  typedef enum {
190  TAG_TYPE_UNKNOWN = 0,
191  TAG_TYPE_MIFARE_CLASSIC = 1,
192  TAG_TYPE_NFC2 = 2 /* ultralight or NTAG2XX */
193  } TAG_TYPE_T;
194 
203  PN532(int irq, int reset, int bus=PN532_I2C_BUS,
204  uint8_t address=PN532_DEFAULT_I2C_ADDR);
205 
209  ~PN532();
210 
216  bool init();
217 
223  uint32_t getFirmwareVersion();
224 
235  bool sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen,
236  uint16_t timeout=1000);
237 
243  bool SAMConfig(void);
244 
254  bool setPassiveActivationRetries(uint8_t maxRetries);
255 
268  bool readPassiveTargetID(BAUD_T cardbaudrate, uint8_t * uid,
269  uint8_t * uidLength, uint16_t timeout);
270 
282  bool inDataExchange(uint8_t * send, uint8_t sendLength,
283  uint8_t * response, uint8_t * responseLength);
284 
291  bool inListPassiveTarget();
292 
299  bool mifareclassic_IsFirstBlock (uint32_t uiBlock);
300 
306  bool mifareclassic_IsTrailerBlock (uint32_t uiBlock);
307 
325  bool mifareclassic_AuthenticateBlock (uint8_t * uid, uint8_t uidLen,
326  uint32_t blockNumber,
327  uint8_t keyNumber,
328  uint8_t * keyData);
329 
341  bool mifareclassic_ReadDataBlock (uint8_t blockNumber, uint8_t * data);
342 
353  bool mifareclassic_WriteDataBlock (uint8_t blockNumber, uint8_t * data);
354 
360  bool mifareclassic_FormatNDEF (void);
361 
378  bool mifareclassic_WriteNDEFURI (uint8_t sectorNumber,
379  NDEF_URI_T uriIdentifier,
380  const char * url);
381 
391  bool ntag2xx_ReadPage (uint8_t page, uint8_t * buffer);
392 
402  bool ntag2xx_WritePage (uint8_t page, uint8_t * data);
403 
417  bool ntag2xx_WriteNDEFURI (NDEF_URI_T uriIdentifier, char * url,
418  uint8_t dataLen);
419 
427  uint16_t getATQA() { return m_ATQA; };
428 
436  uint8_t getSAK() { return m_SAK; };
437 
444  mraa::I2c& i2cContext() { return m_i2c; };
445 
451  void pn532Debug(bool enable) { m_pn532Debug = enable; };
452 
459  void mifareDebug(bool enable) { m_mifareDebug = enable; };
460 
467 
468  protected:
469  mraa::I2c m_i2c;
470  mraa::Gpio m_gpioIRQ;
471  mraa::Gpio m_gpioReset;
472 
473  bool readAck();
474  bool isReady();
475  bool waitForReady(uint16_t timeout);
476  void readData(uint8_t* buff, uint8_t n);
477  void writeCommand(uint8_t* cmd, uint8_t cmdlen);
478 
479  private:
480  static void dataReadyISR(void *ctx);
481  bool m_isrInstalled;
482  volatile bool m_irqRcvd;
483 
484  uint8_t m_addr;
485 
486  uint8_t m_uid[7]; // ISO14443A uid
487  uint8_t m_uidLen; // uid len
488  uint8_t m_key[6]; // Mifare Classic key
489  uint8_t m_inListedTag; // Tg number of inlisted tag.
490 
491  uint16_t m_ATQA; // ATQA (Answer to Request Acknowlege - ISO14443)
492  // for currently inlisted card
493  uint8_t m_SAK; // SAK (Select Acknowlege)
494  // for currently inlisted card
495 
496  // debugables
497  bool m_pn532Debug;
498  bool m_mifareDebug;
499  };
500 }
501 
502 
uint16_t getATQA()
Definition: pn532.h:427
bool readAck()
Tries to read/verify the ACK packet.
Definition: pn532.cxx:1398
uint32_t getFirmwareVersion()
Checks the firmware version of the PN5xx chip.
Definition: pn532.cxx:152
bool mifareclassic_ReadDataBlock(uint8_t blockNumber, uint8_t *data)
Definition: pn532.cxx:822
bool readPassiveTargetID(BAUD_T cardbaudrate, uint8_t *uid, uint8_t *uidLength, uint16_t timeout)
Definition: pn532.cxx:291
void readData(uint8_t *buff, uint8_t n)
Reads n bytes of data from the PN532 via SPI or I2C.
Definition: pn532.cxx:1458
bool isReady()
Return true if the PN532 is ready with a response.
Definition: pn532.cxx:1413
API for the PN532 based NFC/RFID reader/writer.
Definition: pn532.h:72
PN532_CMD_T
Definition: pn532.h:78
void writeCommand(uint8_t *cmd, uint8_t cmdlen)
Writes a command to the PN532, automatically inserting the preamble and required frame details (check...
Definition: pn532.cxx:1497
bool SAMConfig(void)
Configures the SAM (Secure Access Module)
Definition: pn532.cxx:231
bool setPassiveActivationRetries(uint8_t maxRetries)
Definition: pn532.cxx:258
bool mifareclassic_WriteNDEFURI(uint8_t sectorNumber, NDEF_URI_T uriIdentifier, const char *url)
Definition: pn532.cxx:969
bool init()
Definition: pn532.cxx:82
bool mifareclassic_WriteDataBlock(uint8_t blockNumber, uint8_t *data)
Definition: pn532.cxx:887
TAG_TYPE_T tagType()
Definition: pn532.cxx:1558
bool ntag2xx_ReadPage(uint8_t page, uint8_t *buffer)
Definition: pn532.cxx:1147
Definition: a110x.h:29
MIFARE_CMD_T
Definition: pn532.h:124
void mifareDebug(bool enable)
Definition: pn532.h:459
bool mifareclassic_IsTrailerBlock(uint32_t uiBlock)
Definition: pn532.cxx:712
PN532(int irq, int reset, int bus=PN532_I2C_BUS, uint8_t address=PN532_DEFAULT_I2C_ADDR)
Definition: pn532.cxx:46
bool ntag2xx_WriteNDEFURI(NDEF_URI_T uriIdentifier, char *url, uint8_t dataLen)
Definition: pn532.cxx:1298
bool mifareclassic_IsFirstBlock(uint32_t uiBlock)
Definition: pn532.cxx:698
~PN532()
Definition: pn532.cxx:76
mraa::I2c & i2cContext()
Definition: pn532.h:444
bool waitForReady(uint16_t timeout)
Waits until the PN532 is ready.
Definition: pn532.cxx:1432
bool mifareclassic_FormatNDEF(void)
Definition: pn532.cxx:926
uint8_t getSAK()
Definition: pn532.h:436
bool ntag2xx_WritePage(uint8_t page, uint8_t *data)
Definition: pn532.cxx:1235
bool inListPassiveTarget()
'InLists' a passive target. PN532 acting as reader/initiator, peer acting as card/responder.
Definition: pn532.cxx:483
BAUD_T
Definition: pn532.h:181
bool mifareclassic_AuthenticateBlock(uint8_t *uid, uint8_t uidLen, uint32_t blockNumber, uint8_t keyNumber, uint8_t *keyData)
Definition: pn532.cxx:740
NDEF_URI_T
Definition: pn532.h:139
PN532_RSP_T
Definition: pn532.h:116
TAG_TYPE_T
Definition: pn532.h:189
void pn532Debug(bool enable)
Definition: pn532.h:451
bool sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, uint16_t timeout=1000)
Sends a command and waits a specified period for the ACK.
Definition: pn532.cxx:195
bool inDataExchange(uint8_t *send, uint8_t sendLength, uint8_t *response, uint8_t *responseLength)
Exchanges an APDU with the currently inlisted peer.
Definition: pn532.cxx:393