upm  0.4.1
Sensor/Actuator repository for libmraa (v0.8.0)
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/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 
74  class PN532 {
75  public:
76 
80  typedef enum {
81  CMD_DIAGNOSE = 0x00,
82  CMD_GETFIRMWAREVERSION = 0x02,
83  CMD_GETGENERALSTATUS = 0x04,
84  CMD_READREGISTER = 0x06,
85  CMD_WRITEREGISTER = 0x08,
86  CMD_READGPIO = 0x0C,
87  CMD_WRITEGPIO = 0x0E,
88  CMD_SETSERIALBAUDRATE = 0x10,
89  CMD_SETPARAMETERS = 0x12,
90  CMD_SAMCONFIGURATION = 0x14,
91  CMD_POWERDOWN = 0x16,
92  CMD_RFCONFIGURATION = 0x32,
93  CMD_RFREGULATIONTEST = 0x58,
94  CMD_INJUMPFORDEP = 0x56,
95  CMD_INJUMPFORPSL = 0x46,
96  CMD_INLISTPASSIVETARGET = 0x4A,
97  CMD_INATR = 0x50,
98  CMD_INPSL = 0x4E,
99  CMD_INDATAEXCHANGE = 0x40,
100  CMD_INCOMMUNICATETHRU = 0x42,
101  CMD_INDESELECT = 0x44,
102  CMD_INRELEASE = 0x52,
103  CMD_INSELECT = 0x54,
104  CMD_INAUTOPOLL = 0x60,
105  CMD_TGINITASTARGET = 0x8C,
106  CMD_TGSETGENERALBYTES = 0x92,
107  CMD_TGGETDATA = 0x86,
108  CMD_TGSETDATA = 0x8E,
109  CMD_TGSETMETADATA = 0x94,
110  CMD_TGGETINITIATORCOMMAND = 0x88,
111  CMD_TGRESPONSETOINITIATOR = 0x90,
112  CMD_TGGETTARGETSTATUS = 0x8A
113  } PN532_CMD_T;
114 
118  typedef enum {
119  RSP_INDATAEXCHANGE = 0x41,
120  RSP_INLISTPASSIVETARGET = 0x4B
121  } PN532_RSP_T;
122 
126  typedef enum {
127  MIFARE_CMD_AUTH_A = 0x60,
128  MIFARE_CMD_AUTH_B = 0x61,
129  MIFARE_CMD_READ = 0x30,
130  MIFARE_CMD_WRITE = 0xA0,
131  MIFARE_CMD_TRANSFER = 0xB0,
132  MIFARE_CMD_DECREMENT = 0xC0,
133  MIFARE_CMD_INCREMENT = 0xC1,
134  MIFARE_CMD_STORE = 0xC2,
135  MIFARE_ULTRALIGHT_CMD_WRITE = 0xA2
136  } MIFARE_CMD_T;
137 
141  typedef enum {
142  NDEF_URIPREFIX_NONE = 0x00,
143  NDEF_URIPREFIX_HTTP_WWWDOT = 0x01,
144  NDEF_URIPREFIX_HTTPS_WWWDOT = 0x02,
145  NDEF_URIPREFIX_HTTP = 0x03,
146  NDEF_URIPREFIX_HTTPS = 0x04,
147  NDEF_URIPREFIX_TEL = 0x05,
148  NDEF_URIPREFIX_MAILTO = 0x06,
149  NDEF_URIPREFIX_FTP_ANONAT = 0x07,
150  NDEF_URIPREFIX_FTP_FTPDOT = 0x08,
151  NDEF_URIPREFIX_FTPS = 0x09,
152  NDEF_URIPREFIX_SFTP = 0x0A,
153  NDEF_URIPREFIX_SMB = 0x0B,
154  NDEF_URIPREFIX_NFS = 0x0C,
155  NDEF_URIPREFIX_FTP = 0x0D,
156  NDEF_URIPREFIX_DAV = 0x0E,
157  NDEF_URIPREFIX_NEWS = 0x0F,
158  NDEF_URIPREFIX_TELNET = 0x10,
159  NDEF_URIPREFIX_IMAP = 0x11,
160  NDEF_URIPREFIX_RTSP = 0x12,
161  NDEF_URIPREFIX_URN = 0x13,
162  NDEF_URIPREFIX_POP = 0x14,
163  NDEF_URIPREFIX_SIP = 0x15,
164  NDEF_URIPREFIX_SIPS = 0x16,
165  NDEF_URIPREFIX_TFTP = 0x17,
166  NDEF_URIPREFIX_BTSPP = 0x18,
167  NDEF_URIPREFIX_BTL2CAP = 0x19,
168  NDEF_URIPREFIX_BTGOEP = 0x1A,
169  NDEF_URIPREFIX_TCPOBEX = 0x1B,
170  NDEF_URIPREFIX_IRDAOBEX = 0x1C,
171  NDEF_URIPREFIX_FILE = 0x1D,
172  NDEF_URIPREFIX_URN_EPC_ID = 0x1E,
173  NDEF_URIPREFIX_URN_EPC_TAG = 0x1F,
174  NDEF_URIPREFIX_URN_EPC_PAT = 0x20,
175  NDEF_URIPREFIX_URN_EPC_RAW = 0x21,
176  NDEF_URIPREFIX_URN_EPC = 0x22,
177  NDEF_URIPREFIX_URN_NFC = 0x23
178  } NDEF_URI_T;
179 
183  typedef enum {
184  BAUD_MIFARE_ISO14443A = 0x00 // 106 Kbit/s
185  } BAUD_T;
186 
187 
191  typedef enum {
192  TAG_TYPE_UNKNOWN = 0,
193  TAG_TYPE_MIFARE_CLASSIC = 1,
194  TAG_TYPE_NFC2 = 2 /* ultralight or NTAG2XX */
195  } TAG_TYPE_T;
196 
205  PN532(int irq, int reset, int bus=PN532_I2C_BUS,
206  uint8_t address=PN532_DEFAULT_I2C_ADDR);
207 
211  ~PN532();
212 
218  bool init();
219 
225  uint32_t getFirmwareVersion();
226 
237  bool sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen,
238  uint16_t timeout=1000);
239 
245  bool SAMConfig(void);
246 
256  bool setPassiveActivationRetries(uint8_t maxRetries);
257 
270  bool readPassiveTargetID(BAUD_T cardbaudrate, uint8_t * uid,
271  uint8_t * uidLength, uint16_t timeout);
272 
284  bool inDataExchange(uint8_t * send, uint8_t sendLength,
285  uint8_t * response, uint8_t * responseLength);
286 
293  bool inListPassiveTarget();
294 
301  bool mifareclassic_IsFirstBlock (uint32_t uiBlock);
302 
308  bool mifareclassic_IsTrailerBlock (uint32_t uiBlock);
309 
327  bool mifareclassic_AuthenticateBlock (uint8_t * uid, uint8_t uidLen,
328  uint32_t blockNumber,
329  uint8_t keyNumber,
330  uint8_t * keyData);
331 
343  bool mifareclassic_ReadDataBlock (uint8_t blockNumber, uint8_t * data);
344 
355  bool mifareclassic_WriteDataBlock (uint8_t blockNumber, uint8_t * data);
356 
362  bool mifareclassic_FormatNDEF (void);
363 
380  bool mifareclassic_WriteNDEFURI (uint8_t sectorNumber,
381  NDEF_URI_T uriIdentifier,
382  const char * url);
383 
393  bool ntag2xx_ReadPage (uint8_t page, uint8_t * buffer);
394 
404  bool ntag2xx_WritePage (uint8_t page, uint8_t * data);
405 
419  bool ntag2xx_WriteNDEFURI (NDEF_URI_T uriIdentifier, char * url,
420  uint8_t dataLen);
421 
429  uint16_t getATQA() { return m_ATQA; };
430 
438  uint8_t getSAK() { return m_SAK; };
439 
446  mraa::I2c& i2cContext() { return m_i2c; };
447 
453  void pn532Debug(bool enable) { m_pn532Debug = enable; };
454 
461  void mifareDebug(bool enable) { m_mifareDebug = enable; };
462 
469 
470  protected:
471  mraa::I2c m_i2c;
472  mraa::Gpio m_gpioIRQ;
473  mraa::Gpio m_gpioReset;
474 
475  bool readAck();
476  bool isReady();
477  bool waitForReady(uint16_t timeout);
478  void readData(uint8_t* buff, uint8_t n);
479  void writeCommand(uint8_t* cmd, uint8_t cmdlen);
480 
481  private:
482  static void dataReadyISR(void *ctx);
483  bool m_isrInstalled;
484  volatile bool m_irqRcvd;
485 
486  uint8_t m_addr;
487 
488  uint8_t m_uid[7]; // ISO14443A uid
489  uint8_t m_uidLen; // uid len
490  uint8_t m_key[6]; // Mifare Classic key
491  uint8_t m_inListedTag; // Tg number of inlisted tag.
492 
493  uint16_t m_ATQA; // ATQA (Answer to Request Acknowlege - ISO14443)
494  // for currently inlisted card
495  uint8_t m_SAK; // SAK (Select Acknowlege)
496  // for currently inlisted card
497 
498  // debugables
499  bool m_pn532Debug;
500  bool m_mifareDebug;
501  };
502 }
503 
504 
uint16_t getATQA()
Definition: pn532.h:429
bool readAck()
Tries to read/verify the ACK packet.
Definition: pn532.cxx:1399
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:823
bool readPassiveTargetID(BAUD_T cardbaudrate, uint8_t *uid, uint8_t *uidLength, uint16_t timeout)
Definition: pn532.cxx:292
void readData(uint8_t *buff, uint8_t n)
Reads n bytes of data from the PN532 via SPI or I2C.
Definition: pn532.cxx:1459
bool isReady()
Return true if the PN532 is ready with a response.
Definition: pn532.cxx:1414
API for the PN532 based NFC/RFID reader/writer.
Definition: pn532.h:74
PN532_CMD_T
Definition: pn532.h:80
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:1504
bool SAMConfig(void)
Configures the SAM (Secure Access Module)
Definition: pn532.cxx:232
bool setPassiveActivationRetries(uint8_t maxRetries)
Definition: pn532.cxx:259
bool mifareclassic_WriteNDEFURI(uint8_t sectorNumber, NDEF_URI_T uriIdentifier, const char *url)
Definition: pn532.cxx:970
bool init()
Definition: pn532.cxx:83
bool mifareclassic_WriteDataBlock(uint8_t blockNumber, uint8_t *data)
Definition: pn532.cxx:888
TAG_TYPE_T tagType()
Definition: pn532.cxx:1576
bool ntag2xx_ReadPage(uint8_t page, uint8_t *buffer)
Definition: pn532.cxx:1148
Definition: a110x.h:33
MIFARE_CMD_T
Definition: pn532.h:126
void mifareDebug(bool enable)
Definition: pn532.h:461
bool mifareclassic_IsTrailerBlock(uint32_t uiBlock)
Definition: pn532.cxx:713
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:1299
bool mifareclassic_IsFirstBlock(uint32_t uiBlock)
Definition: pn532.cxx:699
~PN532()
Definition: pn532.cxx:77
mraa::I2c & i2cContext()
Definition: pn532.h:446
bool waitForReady(uint16_t timeout)
Waits until the PN532 is ready.
Definition: pn532.cxx:1433
bool mifareclassic_FormatNDEF(void)
Definition: pn532.cxx:927
uint8_t getSAK()
Definition: pn532.h:438
bool ntag2xx_WritePage(uint8_t page, uint8_t *data)
Definition: pn532.cxx:1236
bool inListPassiveTarget()
'InLists' a passive target. PN532 acting as reader/initiator, peer acting as card/responder.
Definition: pn532.cxx:484
BAUD_T
Definition: pn532.h:183
bool mifareclassic_AuthenticateBlock(uint8_t *uid, uint8_t uidLen, uint32_t blockNumber, uint8_t keyNumber, uint8_t *keyData)
Definition: pn532.cxx:741
NDEF_URI_T
Definition: pn532.h:141
PN532_RSP_T
Definition: pn532.h:118
TAG_TYPE_T
Definition: pn532.h:191
void pn532Debug(bool enable)
Definition: pn532.h:453
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:394