upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
pn532.hpp
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/common.hpp>
35 #include <mraa/i2c.hpp>
36 
37 #include <mraa/gpio.hpp>
38 
39 #define PN532_I2C_BUS 0
40 #define PN532_DEFAULT_I2C_ADDR (0x48 >> 1)
41 
42 #define PN532_PREAMBLE (0x00)
43 #define PN532_STARTCODE1 (0x00)
44 #define PN532_STARTCODE2 (0xFF)
45 #define PN532_POSTAMBLE (0x00)
46 
47 #define PN532_HOSTTOPN532 (0xD4)
48 #define PN532_PN532TOHOST (0xD5)
49 
50 namespace upm {
51 
75  class PN532 {
76  public:
77 
81  typedef enum {
82  CMD_DIAGNOSE = 0x00,
83  CMD_GETFIRMWAREVERSION = 0x02,
84  CMD_GETGENERALSTATUS = 0x04,
85  CMD_READREGISTER = 0x06,
86  CMD_WRITEREGISTER = 0x08,
87  CMD_READGPIO = 0x0C,
88  CMD_WRITEGPIO = 0x0E,
89  CMD_SETSERIALBAUDRATE = 0x10,
90  CMD_SETPARAMETERS = 0x12,
91  CMD_SAMCONFIGURATION = 0x14,
92  CMD_POWERDOWN = 0x16,
93  CMD_RFCONFIGURATION = 0x32,
94  CMD_RFREGULATIONTEST = 0x58,
95  CMD_INJUMPFORDEP = 0x56,
96  CMD_INJUMPFORPSL = 0x46,
97  CMD_INLISTPASSIVETARGET = 0x4A,
98  CMD_INATR = 0x50,
99  CMD_INPSL = 0x4E,
100  CMD_INDATAEXCHANGE = 0x40,
101  CMD_INCOMMUNICATETHRU = 0x42,
102  CMD_INDESELECT = 0x44,
103  CMD_INRELEASE = 0x52,
104  CMD_INSELECT = 0x54,
105  CMD_INAUTOPOLL = 0x60,
106  CMD_TGINITASTARGET = 0x8C,
107  CMD_TGSETGENERALBYTES = 0x92,
108  CMD_TGGETDATA = 0x86,
109  CMD_TGSETDATA = 0x8E,
110  CMD_TGSETMETADATA = 0x94,
111  CMD_TGGETINITIATORCOMMAND = 0x88,
112  CMD_TGRESPONSETOINITIATOR = 0x90,
113  CMD_TGGETTARGETSTATUS = 0x8A
114  } PN532_CMD_T;
115 
119  typedef enum {
120  RSP_INDATAEXCHANGE = 0x41,
121  RSP_INLISTPASSIVETARGET = 0x4B
122  } PN532_RSP_T;
123 
127  typedef enum {
128  MIFARE_CMD_AUTH_A = 0x60,
129  MIFARE_CMD_AUTH_B = 0x61,
130  MIFARE_CMD_READ = 0x30,
131  MIFARE_CMD_WRITE = 0xA0,
132  MIFARE_CMD_TRANSFER = 0xB0,
133  MIFARE_CMD_DECREMENT = 0xC0,
134  MIFARE_CMD_INCREMENT = 0xC1,
135  MIFARE_CMD_STORE = 0xC2,
136  MIFARE_ULTRALIGHT_CMD_WRITE = 0xA2
137  } MIFARE_CMD_T;
138 
142  typedef enum {
143  NDEF_URIPREFIX_NONE = 0x00,
144  NDEF_URIPREFIX_HTTP_WWWDOT = 0x01,
145  NDEF_URIPREFIX_HTTPS_WWWDOT = 0x02,
146  NDEF_URIPREFIX_HTTP = 0x03,
147  NDEF_URIPREFIX_HTTPS = 0x04,
148  NDEF_URIPREFIX_TEL = 0x05,
149  NDEF_URIPREFIX_MAILTO = 0x06,
150  NDEF_URIPREFIX_FTP_ANONAT = 0x07,
151  NDEF_URIPREFIX_FTP_FTPDOT = 0x08,
152  NDEF_URIPREFIX_FTPS = 0x09,
153  NDEF_URIPREFIX_SFTP = 0x0A,
154  NDEF_URIPREFIX_SMB = 0x0B,
155  NDEF_URIPREFIX_NFS = 0x0C,
156  NDEF_URIPREFIX_FTP = 0x0D,
157  NDEF_URIPREFIX_DAV = 0x0E,
158  NDEF_URIPREFIX_NEWS = 0x0F,
159  NDEF_URIPREFIX_TELNET = 0x10,
160  NDEF_URIPREFIX_IMAP = 0x11,
161  NDEF_URIPREFIX_RTSP = 0x12,
162  NDEF_URIPREFIX_URN = 0x13,
163  NDEF_URIPREFIX_POP = 0x14,
164  NDEF_URIPREFIX_SIP = 0x15,
165  NDEF_URIPREFIX_SIPS = 0x16,
166  NDEF_URIPREFIX_TFTP = 0x17,
167  NDEF_URIPREFIX_BTSPP = 0x18,
168  NDEF_URIPREFIX_BTL2CAP = 0x19,
169  NDEF_URIPREFIX_BTGOEP = 0x1A,
170  NDEF_URIPREFIX_TCPOBEX = 0x1B,
171  NDEF_URIPREFIX_IRDAOBEX = 0x1C,
172  NDEF_URIPREFIX_FILE = 0x1D,
173  NDEF_URIPREFIX_URN_EPC_ID = 0x1E,
174  NDEF_URIPREFIX_URN_EPC_TAG = 0x1F,
175  NDEF_URIPREFIX_URN_EPC_PAT = 0x20,
176  NDEF_URIPREFIX_URN_EPC_RAW = 0x21,
177  NDEF_URIPREFIX_URN_EPC = 0x22,
178  NDEF_URIPREFIX_URN_NFC = 0x23
179  } NDEF_URI_T;
180 
184  typedef enum {
185  BAUD_MIFARE_ISO14443A = 0x00 // 106 Kbit/s
186  } BAUD_T;
187 
188 
192  typedef enum {
193  TAG_TYPE_UNKNOWN = 0,
194  TAG_TYPE_MIFARE_CLASSIC = 1,
195  TAG_TYPE_NFC2 = 2 /* ultralight or NTAG2XX */
196  } TAG_TYPE_T;
197 
206  PN532(int irq, int reset, int bus=PN532_I2C_BUS,
207  uint8_t address=PN532_DEFAULT_I2C_ADDR);
208 
212  ~PN532();
213 
219  bool init();
220 
226  uint32_t getFirmwareVersion();
227 
238  bool sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen,
239  uint16_t timeout=1000);
240 
246  bool SAMConfig(void);
247 
257  bool setPassiveActivationRetries(uint8_t maxRetries);
258 
271  bool readPassiveTargetID(BAUD_T cardbaudrate, uint8_t * uid,
272  uint8_t * uidLength, uint16_t timeout);
273 
285  bool inDataExchange(uint8_t * send, uint8_t sendLength,
286  uint8_t * response, uint8_t * responseLength);
287 
294  bool inListPassiveTarget();
295 
302  bool mifareclassic_IsFirstBlock (uint32_t uiBlock);
303 
309  bool mifareclassic_IsTrailerBlock (uint32_t uiBlock);
310 
328  bool mifareclassic_AuthenticateBlock (uint8_t * uid, uint8_t uidLen,
329  uint32_t blockNumber,
330  uint8_t keyNumber,
331  uint8_t * keyData);
332 
344  bool mifareclassic_ReadDataBlock (uint8_t blockNumber, uint8_t * data);
345 
356  bool mifareclassic_WriteDataBlock (uint8_t blockNumber, uint8_t * data);
357 
363  bool mifareclassic_FormatNDEF (void);
364 
381  bool mifareclassic_WriteNDEFURI (uint8_t sectorNumber,
382  NDEF_URI_T uriIdentifier,
383  const char * url);
384 
394  bool ntag2xx_ReadPage (uint8_t page, uint8_t * buffer);
395 
405  bool ntag2xx_WritePage (uint8_t page, uint8_t * data);
406 
420  bool ntag2xx_WriteNDEFURI (NDEF_URI_T uriIdentifier, char * url,
421  uint8_t dataLen);
422 
430  uint16_t getATQA() { return m_ATQA; };
431 
439  uint8_t getSAK() { return m_SAK; };
440 
447  mraa::I2c& i2cContext() { return m_i2c; };
448 
454  void pn532Debug(bool enable) { m_pn532Debug = enable; };
455 
462  void mifareDebug(bool enable) { m_mifareDebug = enable; };
463 
470 
471  protected:
472  mraa::Gpio m_gpioIRQ;
473  mraa::Gpio m_gpioReset;
474  mraa::I2c m_i2c;
475 
476  bool readAck();
477  bool isReady();
478  bool waitForReady(uint16_t timeout);
479  void readData(uint8_t* buff, uint8_t n);
480  void writeCommand(uint8_t* cmd, uint8_t cmdlen);
481 
482  private:
483  static void dataReadyISR(void *ctx);
484  bool m_isrInstalled;
485  volatile bool m_irqRcvd;
486 
487  uint8_t m_addr;
488 
489  uint8_t m_uid[7]; // ISO14443A uid
490  uint8_t m_uidLen; // uid len
491  uint8_t m_key[6]; // Mifare Classic key
492  uint8_t m_inListedTag; // Tg number of inlisted tag.
493 
494  uint16_t m_ATQA; // ATQA (Answer to Request Acknowledge - ISO14443)
495  // for currently inlisted card
496  uint8_t m_SAK; // SAK (Select Acknowledge)
497  // for currently inlisted card
498 
499  // debugables
500  bool m_pn532Debug;
501  bool m_mifareDebug;
502  };
503 }
504 
505 
uint16_t getATQA()
Definition: pn532.hpp:430
NDEF_URI_T
Definition: pn532.hpp:142
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:153
bool mifareclassic_ReadDataBlock(uint8_t blockNumber, uint8_t *data)
Definition: pn532.cxx:820
bool readPassiveTargetID(BAUD_T cardbaudrate, uint8_t *uid, uint8_t *uidLength, uint16_t timeout)
Definition: pn532.cxx:290
BAUD_T
Definition: pn532.hpp:184
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.hpp:75
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:230
bool setPassiveActivationRetries(uint8_t maxRetries)
Definition: pn532.cxx:257
bool mifareclassic_WriteNDEFURI(uint8_t sectorNumber, NDEF_URI_T uriIdentifier, const char *url)
Definition: pn532.cxx:967
bool init()
Definition: pn532.cxx:83
bool mifareclassic_WriteDataBlock(uint8_t blockNumber, uint8_t *data)
Definition: pn532.cxx:885
TAG_TYPE_T tagType()
Definition: pn532.cxx:1562
bool ntag2xx_ReadPage(uint8_t page, uint8_t *buffer)
Definition: pn532.cxx:1147
C++ API wrapper for the bh1749 driver.
Definition: a110x.hpp:29
void mifareDebug(bool enable)
Definition: pn532.hpp:462
bool mifareclassic_IsTrailerBlock(uint32_t uiBlock)
Definition: pn532.cxx:711
PN532(int irq, int reset, int bus=PN532_I2C_BUS, uint8_t address=PN532_DEFAULT_I2C_ADDR)
Definition: pn532.cxx:47
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:697
PN532_RSP_T
Definition: pn532.hpp:119
~PN532()
Definition: pn532.cxx:77
mraa::I2c & i2cContext()
Definition: pn532.hpp:447
bool waitForReady(uint16_t timeout)
Waits until the PN532 is ready.
Definition: pn532.cxx:1432
MIFARE_CMD_T
Definition: pn532.hpp:127
bool mifareclassic_FormatNDEF(void)
Definition: pn532.cxx:924
uint8_t getSAK()
Definition: pn532.hpp:439
PN532_CMD_T
Definition: pn532.hpp:81
bool ntag2xx_WritePage(uint8_t page, uint8_t *data)
Definition: pn532.cxx:1235
bool inListPassiveTarget()
&#39;InLists&#39; a passive target. PN532 acting as reader/initiator, peer acting as card/responder.
Definition: pn532.cxx:482
bool mifareclassic_AuthenticateBlock(uint8_t *uid, uint8_t uidLen, uint32_t blockNumber, uint8_t keyNumber, uint8_t *keyData)
Definition: pn532.cxx:739
TAG_TYPE_T
Definition: pn532.hpp:192
void pn532Debug(bool enable)
Definition: pn532.hpp:454
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:196
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:392