Interactive Device Design / Mbed 2 deprecated readMifare

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Adafruit_PN532.h Source File

Adafruit_PN532.h

Go to the documentation of this file.
00001 /**************************************************************************/
00002 /*! 
00003     @file     Adafruit_PN532.h
00004     @author   Adafruit Industries
00005     @license  BSD (see license.txt)
00006     
00007 
00008     This is a library for the Adafruit PN532 NFC/RFID breakout boards
00009     This library works with the Adafruit NFC breakout 
00010     ----> https://www.adafruit.com/products/364
00011     
00012     Check out the links above for our tutorials and wiring diagrams 
00013     These chips use SPI to communicate, 4 required to interface
00014     
00015     Adafruit invests time and resources providing this open source code, 
00016     please support Adafruit and open-source hardware by purchasing 
00017     products from Adafruit!
00018 
00019     @section  HISTORY
00020 
00021     v1.1  - Added full command list
00022           - Added 'verbose' mode flag to constructor to toggle debug output
00023           - Changed readPassiveTargetID() to return variable length values
00024     
00025 */
00026 /**************************************************************************/
00027 
00028 #ifndef _PN532_ADAFRUIT_PN532_H_
00029 #define _PN532_ADAFRUIT_PN532_H_
00030 
00031 #include <stdint.h>
00032 
00033 #include "mbed.h"
00034 
00035 #define PN532_PREAMBLE                      (0x00)
00036 #define PN532_STARTCODE1                    (0x00)
00037 #define PN532_STARTCODE2                    (0xFF)
00038 #define PN532_POSTAMBLE                     (0x00)
00039 
00040 #define PN532_HOSTTOPN532                   (0xD4)
00041 
00042 // PN532 Commands
00043 #define PN532_COMMAND_DIAGNOSE              (0x00)
00044 #define PN532_COMMAND_GETFIRMWAREVERSION    (0x02)
00045 #define PN532_COMMAND_GETGENERALSTATUS      (0x04)
00046 #define PN532_COMMAND_READREGISTER          (0x06)
00047 #define PN532_COMMAND_WRITEREGISTER         (0x08)
00048 #define PN532_COMMAND_READGPIO              (0x0C)
00049 #define PN532_COMMAND_WRITEGPIO             (0x0E)
00050 #define PN532_COMMAND_SETSERIALBAUDRATE     (0x10)
00051 #define PN532_COMMAND_SETPARAMETERS         (0x12)
00052 #define PN532_COMMAND_SAMCONFIGURATION      (0x14)
00053 #define PN532_COMMAND_POWERDOWN             (0x16)
00054 #define PN532_COMMAND_RFCONFIGURATION       (0x32)
00055 #define PN532_COMMAND_RFREGULATIONTEST      (0x58)
00056 #define PN532_COMMAND_INJUMPFORDEP          (0x56)
00057 #define PN532_COMMAND_INJUMPFORPSL          (0x46)
00058 #define PN532_COMMAND_INLISTPASSIVETARGET   (0x4A)
00059 #define PN532_COMMAND_INATR                 (0x50)
00060 #define PN532_COMMAND_INPSL                 (0x4E)
00061 #define PN532_COMMAND_INDATAEXCHANGE        (0x40)
00062 #define PN532_COMMAND_INCOMMUNICATETHRU     (0x42)
00063 #define PN532_COMMAND_INDESELECT            (0x44)
00064 #define PN532_COMMAND_INRELEASE             (0x52)
00065 #define PN532_COMMAND_INSELECT              (0x54)
00066 #define PN532_COMMAND_INAUTOPOLL            (0x60)
00067 #define PN532_COMMAND_TGINITASTARGET        (0x8C)
00068 #define PN532_COMMAND_TGSETGENERALBYTES     (0x92)
00069 #define PN532_COMMAND_TGGETDATA             (0x86)
00070 #define PN532_COMMAND_TGSETDATA             (0x8E)
00071 #define PN532_COMMAND_TGSETMETADATA         (0x94)
00072 #define PN532_COMMAND_TGGETINITIATORCOMMAND (0x88)
00073 #define PN532_COMMAND_TGRESPONSETOINITIATOR (0x90)
00074 #define PN532_COMMAND_TGGETTARGETSTATUS     (0x8A)
00075 
00076 #define PN532_WAKEUP                        (0x55)
00077 
00078 #define PN532_SPI_STATREAD                  (0x02)
00079 #define PN532_SPI_DATAWRITE                 (0x01)
00080 #define PN532_SPI_DATAREAD                  (0x03)
00081 #define PN532_SPI_READY                     (0x01)
00082 
00083 #define PN532_MIFARE_ISO14443A              (0x00)
00084 
00085 // Mifare Commands
00086 #define MIFARE_CMD_AUTH_A                   (0x60)
00087 #define MIFARE_CMD_AUTH_B                   (0x61)
00088 #define MIFARE_CMD_READ                     (0x30)
00089 #define MIFARE_CMD_WRITE                    (0xA0)
00090 #define MIFARE_CMD_TRANSFER                 (0xB0)
00091 #define MIFARE_CMD_DECREMENT                (0xC0)
00092 #define MIFARE_CMD_INCREMENT                (0xC1)
00093 #define MIFARE_CMD_STORE                    (0xC2)
00094 
00095 // Prefixes for NDEF Records (to identify record type)
00096 #define NDEF_URIPREFIX_NONE                 (0x00)
00097 #define NDEF_URIPREFIX_HTTP_WWWDOT          (0x01)
00098 #define NDEF_URIPREFIX_HTTPS_WWWDOT         (0x02)
00099 #define NDEF_URIPREFIX_HTTP                 (0x03)
00100 #define NDEF_URIPREFIX_HTTPS                (0x04)
00101 #define NDEF_URIPREFIX_TEL                  (0x05)
00102 #define NDEF_URIPREFIX_MAILTO               (0x06)
00103 #define NDEF_URIPREFIX_FTP_ANONAT           (0x07)
00104 #define NDEF_URIPREFIX_FTP_FTPDOT           (0x08)
00105 #define NDEF_URIPREFIX_FTPS                 (0x09)
00106 #define NDEF_URIPREFIX_SFTP                 (0x0A)
00107 #define NDEF_URIPREFIX_SMB                  (0x0B)
00108 #define NDEF_URIPREFIX_NFS                  (0x0C)
00109 #define NDEF_URIPREFIX_FTP                  (0x0D)
00110 #define NDEF_URIPREFIX_DAV                  (0x0E)
00111 #define NDEF_URIPREFIX_NEWS                 (0x0F)
00112 #define NDEF_URIPREFIX_TELNET               (0x10)
00113 #define NDEF_URIPREFIX_IMAP                 (0x11)
00114 #define NDEF_URIPREFIX_RTSP                 (0x12)
00115 #define NDEF_URIPREFIX_URN                  (0x13)
00116 #define NDEF_URIPREFIX_POP                  (0x14)
00117 #define NDEF_URIPREFIX_SIP                  (0x15)
00118 #define NDEF_URIPREFIX_SIPS                 (0x16)
00119 #define NDEF_URIPREFIX_TFTP                 (0x17)
00120 #define NDEF_URIPREFIX_BTSPP                (0x18)
00121 #define NDEF_URIPREFIX_BTL2CAP              (0x19)
00122 #define NDEF_URIPREFIX_BTGOEP               (0x1A)
00123 #define NDEF_URIPREFIX_TCPOBEX              (0x1B)
00124 #define NDEF_URIPREFIX_IRDAOBEX             (0x1C)
00125 #define NDEF_URIPREFIX_FILE                 (0x1D)
00126 #define NDEF_URIPREFIX_URN_EPC_ID           (0x1E)
00127 #define NDEF_URIPREFIX_URN_EPC_TAG          (0x1F)
00128 #define NDEF_URIPREFIX_URN_EPC_PAT          (0x20)
00129 #define NDEF_URIPREFIX_URN_EPC_RAW          (0x21)
00130 #define NDEF_URIPREFIX_URN_EPC              (0x22)
00131 #define NDEF_URIPREFIX_URN_NFC              (0x23)
00132 
00133 #define PN532_GPIO_VALIDATIONBIT            (0x80)
00134 #define PN532_GPIO_P30                      (0)
00135 #define PN532_GPIO_P31                      (1)
00136 #define PN532_GPIO_P32                      (2)
00137 #define PN532_GPIO_P33                      (3)
00138 #define PN532_GPIO_P34                      (4)
00139 #define PN532_GPIO_P35                      (5)
00140 
00141 class Adafruit_PN532{
00142  public:
00143   Adafruit_PN532(DigitalOut clk, DigitalIn miso, DigitalOut mosi, DigitalOut ss);
00144   Adafruit_PN532(PinName clk_pin, PinName miso_pin, PinName mosi_pin, PinName ss_pin);
00145   
00146   void begin(void);
00147   
00148   // Generic PN532 functions
00149   bool SAMConfig(void);
00150   uint32_t getFirmwareVersion(void);
00151   bool sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, uint16_t timeout = 1000);  
00152   bool writeGPIO(uint8_t pinstate);
00153   uint8_t readGPIO(void);
00154   bool setPassiveActivationRetries(uint8_t maxRetries);
00155   
00156   // ISO14443A functions
00157   bool readPassiveTargetID(uint8_t cardbaudrate, uint8_t * uid, uint8_t * uidLength);
00158   
00159   // Mifare Classic functions
00160   bool mifareclassic_IsFirstBlock (uint32_t uiBlock);
00161   bool mifareclassic_IsTrailerBlock (uint32_t uiBlock);
00162   uint8_t mifareclassic_AuthenticateBlock (uint8_t * uid, uint8_t uidLen, uint32_t blockNumber, uint8_t keyNumber, uint8_t * keyData);
00163   uint8_t mifareclassic_ReadDataBlock (uint8_t blockNumber, uint8_t * data);
00164   uint8_t mifareclassic_WriteDataBlock (uint8_t blockNumber, uint8_t * data);
00165   uint8_t mifareclassic_FormatNDEF (void);
00166   uint8_t mifareclassic_WriteNDEFURI (uint8_t sectorNumber, uint8_t uriIdentifier, const char * url);
00167   
00168   // Mifare Ultralight functions
00169   uint8_t mifareultralight_ReadPage (uint8_t page, uint8_t * buffer);
00170   
00171   // Help functions to display formatted text
00172   static void PrintHex(const uint8_t * data, const uint32_t numBytes);
00173   static void PrintHexChar(const uint8_t * pbtData, const uint32_t numBytes);
00174 
00175  private:
00176   DigitalOut _clk;
00177   DigitalIn _miso;
00178   DigitalOut _mosi, _ss;
00179   uint8_t _uid[7];  // ISO14443A uid
00180   uint8_t _uidLen;  // uid len
00181   uint8_t _key[6];  // Mifare Classic key
00182 
00183   bool spi_readack();
00184   uint8_t readspistatus(void);
00185   void readspidata(uint8_t* buff, uint8_t n);
00186   void spiwritecommand(uint8_t* cmd, uint8_t cmdlen);
00187   void spiwrite(uint8_t c);
00188   uint8_t spiread(void);
00189 };
00190 
00191 #endif  // _PN532_ADAFRUIT_PN532_H_