PN532 NFC library for Seeed Studio's NFC Shield

Fork of PN532 by Yihui Xiong

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PN532.h Source File

PN532.h

Go to the documentation of this file.
00001 /**************************************************************************/
00002 /*!
00003     @file     PN532.h
00004     @author   Adafruit Industries & Seeed Studio
00005     @license  BSD
00006 */
00007 /**************************************************************************/
00008 
00009 #ifndef __PN532_H__
00010 #define __PN532_H__
00011 
00012 #include <stdint.h>
00013 #include "PN532Interface.h"
00014 
00015 // PN532 Commands
00016 #define PN532_COMMAND_DIAGNOSE              (0x00)
00017 #define PN532_COMMAND_GETFIRMWAREVERSION    (0x02)
00018 #define PN532_COMMAND_GETGENERALSTATUS      (0x04)
00019 #define PN532_COMMAND_READREGISTER          (0x06)
00020 #define PN532_COMMAND_WRITEREGISTER         (0x08)
00021 #define PN532_COMMAND_READGPIO              (0x0C)
00022 #define PN532_COMMAND_WRITEGPIO             (0x0E)
00023 #define PN532_COMMAND_SETSERIALBAUDRATE     (0x10)
00024 #define PN532_COMMAND_SETPARAMETERS         (0x12)
00025 #define PN532_COMMAND_SAMCONFIGURATION      (0x14)
00026 #define PN532_COMMAND_POWERDOWN             (0x16)
00027 #define PN532_COMMAND_RFCONFIGURATION       (0x32)
00028 #define PN532_COMMAND_RFREGULATIONTEST      (0x58)
00029 #define PN532_COMMAND_INJUMPFORDEP          (0x56)
00030 #define PN532_COMMAND_INJUMPFORPSL          (0x46)
00031 #define PN532_COMMAND_INLISTPASSIVETARGET   (0x4A)
00032 #define PN532_COMMAND_INATR                 (0x50)
00033 #define PN532_COMMAND_INPSL                 (0x4E)
00034 #define PN532_COMMAND_INDATAEXCHANGE        (0x40)
00035 #define PN532_COMMAND_INCOMMUNICATETHRU     (0x42)
00036 #define PN532_COMMAND_INDESELECT            (0x44)
00037 #define PN532_COMMAND_INRELEASE             (0x52)
00038 #define PN532_COMMAND_INSELECT              (0x54)
00039 #define PN532_COMMAND_INAUTOPOLL            (0x60)
00040 #define PN532_COMMAND_TGINITASTARGET        (0x8C)
00041 #define PN532_COMMAND_TGSETGENERALBYTES     (0x92)
00042 #define PN532_COMMAND_TGGETDATA             (0x86)
00043 #define PN532_COMMAND_TGSETDATA             (0x8E)
00044 #define PN532_COMMAND_TGSETMETADATA         (0x94)
00045 #define PN532_COMMAND_TGGETINITIATORCOMMAND (0x88)
00046 #define PN532_COMMAND_TGRESPONSETOINITIATOR (0x90)
00047 #define PN532_COMMAND_TGGETTARGETSTATUS     (0x8A)
00048 
00049 #define PN532_RESPONSE_INDATAEXCHANGE       (0x41)
00050 #define PN532_RESPONSE_INLISTPASSIVETARGET  (0x4B)
00051 
00052 
00053 #define PN532_MIFARE_ISO14443A              (0x00)
00054 
00055 // Mifare Commands
00056 #define MIFARE_CMD_AUTH_A                   (0x60)
00057 #define MIFARE_CMD_AUTH_B                   (0x61)
00058 #define MIFARE_CMD_READ                     (0x30)
00059 #define MIFARE_CMD_WRITE                    (0xA0)
00060 #define MIFARE_CMD_TRANSFER                 (0xB0)
00061 #define MIFARE_CMD_DECREMENT                (0xC0)
00062 #define MIFARE_CMD_INCREMENT                (0xC1)
00063 #define MIFARE_CMD_STORE                    (0xC2)
00064 #define MIFARE_UL_CMD_WRITE_PAGE            (0xA2)
00065 // Prefixes for NDEF Records (to identify record type)
00066 #define NDEF_URIPREFIX_NONE                 (0x00)
00067 #define NDEF_URIPREFIX_HTTP_WWWDOT          (0x01)
00068 #define NDEF_URIPREFIX_HTTPS_WWWDOT         (0x02)
00069 #define NDEF_URIPREFIX_HTTP                 (0x03)
00070 #define NDEF_URIPREFIX_HTTPS                (0x04)
00071 #define NDEF_URIPREFIX_TEL                  (0x05)
00072 #define NDEF_URIPREFIX_MAILTO               (0x06)
00073 #define NDEF_URIPREFIX_FTP_ANONAT           (0x07)
00074 #define NDEF_URIPREFIX_FTP_FTPDOT           (0x08)
00075 #define NDEF_URIPREFIX_FTPS                 (0x09)
00076 #define NDEF_URIPREFIX_SFTP                 (0x0A)
00077 #define NDEF_URIPREFIX_SMB                  (0x0B)
00078 #define NDEF_URIPREFIX_NFS                  (0x0C)
00079 #define NDEF_URIPREFIX_FTP                  (0x0D)
00080 #define NDEF_URIPREFIX_DAV                  (0x0E)
00081 #define NDEF_URIPREFIX_NEWS                 (0x0F)
00082 #define NDEF_URIPREFIX_TELNET               (0x10)
00083 #define NDEF_URIPREFIX_IMAP                 (0x11)
00084 #define NDEF_URIPREFIX_RTSP                 (0x12)
00085 #define NDEF_URIPREFIX_URN                  (0x13)
00086 #define NDEF_URIPREFIX_POP                  (0x14)
00087 #define NDEF_URIPREFIX_SIP                  (0x15)
00088 #define NDEF_URIPREFIX_SIPS                 (0x16)
00089 #define NDEF_URIPREFIX_TFTP                 (0x17)
00090 #define NDEF_URIPREFIX_BTSPP                (0x18)
00091 #define NDEF_URIPREFIX_BTL2CAP              (0x19)
00092 #define NDEF_URIPREFIX_BTGOEP               (0x1A)
00093 #define NDEF_URIPREFIX_TCPOBEX              (0x1B)
00094 #define NDEF_URIPREFIX_IRDAOBEX             (0x1C)
00095 #define NDEF_URIPREFIX_FILE                 (0x1D)
00096 #define NDEF_URIPREFIX_URN_EPC_ID           (0x1E)
00097 #define NDEF_URIPREFIX_URN_EPC_TAG          (0x1F)
00098 #define NDEF_URIPREFIX_URN_EPC_PAT          (0x20)
00099 #define NDEF_URIPREFIX_URN_EPC_RAW          (0x21)
00100 #define NDEF_URIPREFIX_URN_EPC              (0x22)
00101 #define NDEF_URIPREFIX_URN_NFC              (0x23)
00102 
00103 #define PN532_GPIO_VALIDATIONBIT            (0x80)
00104 #define PN532_GPIO_P30                      (0)
00105 #define PN532_GPIO_P31                      (1)
00106 #define PN532_GPIO_P32                      (2)
00107 #define PN532_GPIO_P33                      (3)
00108 #define PN532_GPIO_P34                      (4)
00109 #define PN532_GPIO_P35                      (5)
00110 
00111 /**
00112  * The PN532 class
00113  */
00114 class PN532
00115 {
00116 public:
00117     PN532(PN532Interface &interface);
00118 
00119     void begin(void);
00120 
00121     // Generic PN532 functions
00122     bool SAMConfig(void);
00123     uint32_t getFirmwareVersion(void);
00124     bool writeGPIO (uint8_t pinstate);
00125     uint8_t readGPIO (void);
00126     bool setPassiveActivationRetries (uint8_t maxRetries);
00127 
00128     /**
00129      * @brief    Init PN532 as a target
00130      * @param    timeout max time to wait, 0 means no timeout
00131      * @return   > 0     success
00132      *           = 0     timeout
00133      *           < 0     failed
00134      */
00135     int8_t tgInitAsTarget(uint16_t timeout = 0);
00136     int8_t tgInitAsTarget(const uint8_t* command, const uint8_t len, const uint16_t timeout = 0);
00137 
00138     int16_t tgGetData(uint8_t *buf, uint8_t len);
00139     bool tgSetData(const uint8_t *header, uint8_t hlen, const uint8_t *body = 0, uint8_t blen = 0);
00140 
00141     int16_t inRelease(const uint8_t relevantTarget = 0);
00142 
00143     // ISO14443A functions
00144     bool inListPassiveTarget();
00145     bool readPassiveTargetID (uint8_t cardbaudrate, uint8_t *uid, uint8_t *uidLength, uint16_t *atqa, uint8_t *sak, uint16_t timeout = 1000);
00146     bool inDataExchange(uint8_t *send, uint8_t sendLength, uint8_t *response, uint8_t *responseLength);
00147 
00148     // Mifare Classic functions
00149     bool mifareclassic_IsFirstBlock  (uint32_t uiBlock);
00150     bool mifareclassic_IsTrailerBlock  (uint32_t uiBlock);
00151     uint8_t mifareclassic_AuthenticateBlock  (uint8_t *uid, uint8_t uidLen, uint32_t blockNumber, uint8_t keyNumber, uint8_t *keyData);
00152     uint8_t mifareclassic_ReadDataBlock  (uint8_t blockNumber, uint8_t *data);
00153     uint8_t mifareclassic_WriteDataBlock  (uint8_t blockNumber, uint8_t *data);
00154     uint8_t mifareclassic_FormatNDEF  (void);
00155     uint8_t mifareclassic_WriteNDEFURI  (uint8_t sectorNumber, uint8_t uriIdentifier, const char *url);
00156 
00157     // Mifare Ultralight functions
00158     uint8_t mifareultralight_ReadPage  (uint8_t page, uint8_t *buffer);
00159     uint8_t mifareultralight_WritePage  (uint8_t page, uint8_t *buffer);
00160     // Help functions to display formatted text
00161     static void PrintHex(const uint8_t *data, const uint32_t numBytes);
00162     static void PrintHexChar(const uint8_t *pbtData, const uint32_t numBytes);
00163 
00164     uint8_t *getBuffer(uint8_t *len) {
00165         *len = sizeof(pn532_packetbuffer) - 4;
00166         return pn532_packetbuffer;
00167     };
00168 
00169 private:
00170     uint8_t _uid[7];  // ISO14443A uid
00171     uint8_t _uidLen;  // uid len
00172     uint8_t _key[6];  // Mifare Classic key
00173     uint8_t inListedTag; // Tg number of inlisted tag.
00174 
00175     uint8_t pn532_packetbuffer[64];
00176 
00177     PN532Interface *_interface;
00178 };
00179 
00180 #endif