Test example

Dependencies:   mbed

Fork of readMifare by Interactive Device Design

Committer:
mjovanov1
Date:
Thu Mar 17 10:37:47 2016 +0000
Revision:
11:5e52e43e2977
Parent:
0:54bf4b21c7fa
Chained read;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nebgnahz 0:54bf4b21c7fa 1 /**************************************************************************/
nebgnahz 0:54bf4b21c7fa 2 /*!
nebgnahz 0:54bf4b21c7fa 3 @file Adafruit_PN532.h
nebgnahz 0:54bf4b21c7fa 4 @author Adafruit Industries
nebgnahz 0:54bf4b21c7fa 5 @license BSD (see license.txt)
nebgnahz 0:54bf4b21c7fa 6
nebgnahz 0:54bf4b21c7fa 7
nebgnahz 0:54bf4b21c7fa 8 This is a library for the Adafruit PN532 NFC/RFID breakout boards
nebgnahz 0:54bf4b21c7fa 9 This library works with the Adafruit NFC breakout
nebgnahz 0:54bf4b21c7fa 10 ----> https://www.adafruit.com/products/364
nebgnahz 0:54bf4b21c7fa 11
nebgnahz 0:54bf4b21c7fa 12 Check out the links above for our tutorials and wiring diagrams
nebgnahz 0:54bf4b21c7fa 13 These chips use SPI to communicate, 4 required to interface
nebgnahz 0:54bf4b21c7fa 14
nebgnahz 0:54bf4b21c7fa 15 Adafruit invests time and resources providing this open source code,
nebgnahz 0:54bf4b21c7fa 16 please support Adafruit and open-source hardware by purchasing
nebgnahz 0:54bf4b21c7fa 17 products from Adafruit!
nebgnahz 0:54bf4b21c7fa 18
nebgnahz 0:54bf4b21c7fa 19 @section HISTORY
nebgnahz 0:54bf4b21c7fa 20
nebgnahz 0:54bf4b21c7fa 21 v1.1 - Added full command list
nebgnahz 0:54bf4b21c7fa 22 - Added 'verbose' mode flag to constructor to toggle debug output
nebgnahz 0:54bf4b21c7fa 23 - Changed readPassiveTargetID() to return variable length values
nebgnahz 0:54bf4b21c7fa 24
nebgnahz 0:54bf4b21c7fa 25 */
nebgnahz 0:54bf4b21c7fa 26 /**************************************************************************/
nebgnahz 0:54bf4b21c7fa 27
nebgnahz 0:54bf4b21c7fa 28 #ifndef _PN532_ADAFRUIT_PN532_H_
nebgnahz 0:54bf4b21c7fa 29 #define _PN532_ADAFRUIT_PN532_H_
nebgnahz 0:54bf4b21c7fa 30
nebgnahz 0:54bf4b21c7fa 31 #include <stdint.h>
nebgnahz 0:54bf4b21c7fa 32
nebgnahz 0:54bf4b21c7fa 33 #include "mbed.h"
nebgnahz 0:54bf4b21c7fa 34
nebgnahz 0:54bf4b21c7fa 35 #define PN532_PREAMBLE (0x00)
nebgnahz 0:54bf4b21c7fa 36 #define PN532_STARTCODE1 (0x00)
nebgnahz 0:54bf4b21c7fa 37 #define PN532_STARTCODE2 (0xFF)
nebgnahz 0:54bf4b21c7fa 38 #define PN532_POSTAMBLE (0x00)
nebgnahz 0:54bf4b21c7fa 39
nebgnahz 0:54bf4b21c7fa 40 #define PN532_HOSTTOPN532 (0xD4)
nebgnahz 0:54bf4b21c7fa 41
nebgnahz 0:54bf4b21c7fa 42 // PN532 Commands
nebgnahz 0:54bf4b21c7fa 43 #define PN532_COMMAND_DIAGNOSE (0x00)
nebgnahz 0:54bf4b21c7fa 44 #define PN532_COMMAND_GETFIRMWAREVERSION (0x02)
nebgnahz 0:54bf4b21c7fa 45 #define PN532_COMMAND_GETGENERALSTATUS (0x04)
nebgnahz 0:54bf4b21c7fa 46 #define PN532_COMMAND_READREGISTER (0x06)
nebgnahz 0:54bf4b21c7fa 47 #define PN532_COMMAND_WRITEREGISTER (0x08)
nebgnahz 0:54bf4b21c7fa 48 #define PN532_COMMAND_READGPIO (0x0C)
nebgnahz 0:54bf4b21c7fa 49 #define PN532_COMMAND_WRITEGPIO (0x0E)
nebgnahz 0:54bf4b21c7fa 50 #define PN532_COMMAND_SETSERIALBAUDRATE (0x10)
nebgnahz 0:54bf4b21c7fa 51 #define PN532_COMMAND_SETPARAMETERS (0x12)
nebgnahz 0:54bf4b21c7fa 52 #define PN532_COMMAND_SAMCONFIGURATION (0x14)
nebgnahz 0:54bf4b21c7fa 53 #define PN532_COMMAND_POWERDOWN (0x16)
nebgnahz 0:54bf4b21c7fa 54 #define PN532_COMMAND_RFCONFIGURATION (0x32)
nebgnahz 0:54bf4b21c7fa 55 #define PN532_COMMAND_RFREGULATIONTEST (0x58)
nebgnahz 0:54bf4b21c7fa 56 #define PN532_COMMAND_INJUMPFORDEP (0x56)
nebgnahz 0:54bf4b21c7fa 57 #define PN532_COMMAND_INJUMPFORPSL (0x46)
nebgnahz 0:54bf4b21c7fa 58 #define PN532_COMMAND_INLISTPASSIVETARGET (0x4A)
nebgnahz 0:54bf4b21c7fa 59 #define PN532_COMMAND_INATR (0x50)
nebgnahz 0:54bf4b21c7fa 60 #define PN532_COMMAND_INPSL (0x4E)
nebgnahz 0:54bf4b21c7fa 61 #define PN532_COMMAND_INDATAEXCHANGE (0x40)
nebgnahz 0:54bf4b21c7fa 62 #define PN532_COMMAND_INCOMMUNICATETHRU (0x42)
nebgnahz 0:54bf4b21c7fa 63 #define PN532_COMMAND_INDESELECT (0x44)
nebgnahz 0:54bf4b21c7fa 64 #define PN532_COMMAND_INRELEASE (0x52)
nebgnahz 0:54bf4b21c7fa 65 #define PN532_COMMAND_INSELECT (0x54)
nebgnahz 0:54bf4b21c7fa 66 #define PN532_COMMAND_INAUTOPOLL (0x60)
nebgnahz 0:54bf4b21c7fa 67 #define PN532_COMMAND_TGINITASTARGET (0x8C)
nebgnahz 0:54bf4b21c7fa 68 #define PN532_COMMAND_TGSETGENERALBYTES (0x92)
nebgnahz 0:54bf4b21c7fa 69 #define PN532_COMMAND_TGGETDATA (0x86)
nebgnahz 0:54bf4b21c7fa 70 #define PN532_COMMAND_TGSETDATA (0x8E)
nebgnahz 0:54bf4b21c7fa 71 #define PN532_COMMAND_TGSETMETADATA (0x94)
nebgnahz 0:54bf4b21c7fa 72 #define PN532_COMMAND_TGGETINITIATORCOMMAND (0x88)
nebgnahz 0:54bf4b21c7fa 73 #define PN532_COMMAND_TGRESPONSETOINITIATOR (0x90)
nebgnahz 0:54bf4b21c7fa 74 #define PN532_COMMAND_TGGETTARGETSTATUS (0x8A)
nebgnahz 0:54bf4b21c7fa 75
nebgnahz 0:54bf4b21c7fa 76 #define PN532_WAKEUP (0x55)
nebgnahz 0:54bf4b21c7fa 77
nebgnahz 0:54bf4b21c7fa 78 #define PN532_SPI_STATREAD (0x02)
nebgnahz 0:54bf4b21c7fa 79 #define PN532_SPI_DATAWRITE (0x01)
nebgnahz 0:54bf4b21c7fa 80 #define PN532_SPI_DATAREAD (0x03)
nebgnahz 0:54bf4b21c7fa 81 #define PN532_SPI_READY (0x01)
nebgnahz 0:54bf4b21c7fa 82
nebgnahz 0:54bf4b21c7fa 83 #define PN532_MIFARE_ISO14443A (0x00)
nebgnahz 0:54bf4b21c7fa 84
nebgnahz 0:54bf4b21c7fa 85 // Mifare Commands
nebgnahz 0:54bf4b21c7fa 86 #define MIFARE_CMD_AUTH_A (0x60)
nebgnahz 0:54bf4b21c7fa 87 #define MIFARE_CMD_AUTH_B (0x61)
nebgnahz 0:54bf4b21c7fa 88 #define MIFARE_CMD_READ (0x30)
nebgnahz 0:54bf4b21c7fa 89 #define MIFARE_CMD_WRITE (0xA0)
nebgnahz 0:54bf4b21c7fa 90 #define MIFARE_CMD_TRANSFER (0xB0)
nebgnahz 0:54bf4b21c7fa 91 #define MIFARE_CMD_DECREMENT (0xC0)
nebgnahz 0:54bf4b21c7fa 92 #define MIFARE_CMD_INCREMENT (0xC1)
nebgnahz 0:54bf4b21c7fa 93 #define MIFARE_CMD_STORE (0xC2)
nebgnahz 0:54bf4b21c7fa 94
nebgnahz 0:54bf4b21c7fa 95 // Prefixes for NDEF Records (to identify record type)
nebgnahz 0:54bf4b21c7fa 96 #define NDEF_URIPREFIX_NONE (0x00)
nebgnahz 0:54bf4b21c7fa 97 #define NDEF_URIPREFIX_HTTP_WWWDOT (0x01)
nebgnahz 0:54bf4b21c7fa 98 #define NDEF_URIPREFIX_HTTPS_WWWDOT (0x02)
nebgnahz 0:54bf4b21c7fa 99 #define NDEF_URIPREFIX_HTTP (0x03)
nebgnahz 0:54bf4b21c7fa 100 #define NDEF_URIPREFIX_HTTPS (0x04)
nebgnahz 0:54bf4b21c7fa 101 #define NDEF_URIPREFIX_TEL (0x05)
nebgnahz 0:54bf4b21c7fa 102 #define NDEF_URIPREFIX_MAILTO (0x06)
nebgnahz 0:54bf4b21c7fa 103 #define NDEF_URIPREFIX_FTP_ANONAT (0x07)
nebgnahz 0:54bf4b21c7fa 104 #define NDEF_URIPREFIX_FTP_FTPDOT (0x08)
nebgnahz 0:54bf4b21c7fa 105 #define NDEF_URIPREFIX_FTPS (0x09)
nebgnahz 0:54bf4b21c7fa 106 #define NDEF_URIPREFIX_SFTP (0x0A)
nebgnahz 0:54bf4b21c7fa 107 #define NDEF_URIPREFIX_SMB (0x0B)
nebgnahz 0:54bf4b21c7fa 108 #define NDEF_URIPREFIX_NFS (0x0C)
nebgnahz 0:54bf4b21c7fa 109 #define NDEF_URIPREFIX_FTP (0x0D)
nebgnahz 0:54bf4b21c7fa 110 #define NDEF_URIPREFIX_DAV (0x0E)
nebgnahz 0:54bf4b21c7fa 111 #define NDEF_URIPREFIX_NEWS (0x0F)
nebgnahz 0:54bf4b21c7fa 112 #define NDEF_URIPREFIX_TELNET (0x10)
nebgnahz 0:54bf4b21c7fa 113 #define NDEF_URIPREFIX_IMAP (0x11)
nebgnahz 0:54bf4b21c7fa 114 #define NDEF_URIPREFIX_RTSP (0x12)
nebgnahz 0:54bf4b21c7fa 115 #define NDEF_URIPREFIX_URN (0x13)
nebgnahz 0:54bf4b21c7fa 116 #define NDEF_URIPREFIX_POP (0x14)
nebgnahz 0:54bf4b21c7fa 117 #define NDEF_URIPREFIX_SIP (0x15)
nebgnahz 0:54bf4b21c7fa 118 #define NDEF_URIPREFIX_SIPS (0x16)
nebgnahz 0:54bf4b21c7fa 119 #define NDEF_URIPREFIX_TFTP (0x17)
nebgnahz 0:54bf4b21c7fa 120 #define NDEF_URIPREFIX_BTSPP (0x18)
nebgnahz 0:54bf4b21c7fa 121 #define NDEF_URIPREFIX_BTL2CAP (0x19)
nebgnahz 0:54bf4b21c7fa 122 #define NDEF_URIPREFIX_BTGOEP (0x1A)
nebgnahz 0:54bf4b21c7fa 123 #define NDEF_URIPREFIX_TCPOBEX (0x1B)
nebgnahz 0:54bf4b21c7fa 124 #define NDEF_URIPREFIX_IRDAOBEX (0x1C)
nebgnahz 0:54bf4b21c7fa 125 #define NDEF_URIPREFIX_FILE (0x1D)
nebgnahz 0:54bf4b21c7fa 126 #define NDEF_URIPREFIX_URN_EPC_ID (0x1E)
nebgnahz 0:54bf4b21c7fa 127 #define NDEF_URIPREFIX_URN_EPC_TAG (0x1F)
nebgnahz 0:54bf4b21c7fa 128 #define NDEF_URIPREFIX_URN_EPC_PAT (0x20)
nebgnahz 0:54bf4b21c7fa 129 #define NDEF_URIPREFIX_URN_EPC_RAW (0x21)
nebgnahz 0:54bf4b21c7fa 130 #define NDEF_URIPREFIX_URN_EPC (0x22)
nebgnahz 0:54bf4b21c7fa 131 #define NDEF_URIPREFIX_URN_NFC (0x23)
nebgnahz 0:54bf4b21c7fa 132
nebgnahz 0:54bf4b21c7fa 133 #define PN532_GPIO_VALIDATIONBIT (0x80)
nebgnahz 0:54bf4b21c7fa 134 #define PN532_GPIO_P30 (0)
nebgnahz 0:54bf4b21c7fa 135 #define PN532_GPIO_P31 (1)
nebgnahz 0:54bf4b21c7fa 136 #define PN532_GPIO_P32 (2)
nebgnahz 0:54bf4b21c7fa 137 #define PN532_GPIO_P33 (3)
nebgnahz 0:54bf4b21c7fa 138 #define PN532_GPIO_P34 (4)
nebgnahz 0:54bf4b21c7fa 139 #define PN532_GPIO_P35 (5)
nebgnahz 0:54bf4b21c7fa 140
nebgnahz 0:54bf4b21c7fa 141 class Adafruit_PN532{
nebgnahz 0:54bf4b21c7fa 142 public:
nebgnahz 0:54bf4b21c7fa 143 Adafruit_PN532(DigitalOut clk, DigitalIn miso, DigitalOut mosi, DigitalOut ss);
nebgnahz 0:54bf4b21c7fa 144 Adafruit_PN532(PinName clk_pin, PinName miso_pin, PinName mosi_pin, PinName ss_pin);
nebgnahz 0:54bf4b21c7fa 145
nebgnahz 0:54bf4b21c7fa 146 void begin(void);
nebgnahz 0:54bf4b21c7fa 147
nebgnahz 0:54bf4b21c7fa 148 // Generic PN532 functions
nebgnahz 0:54bf4b21c7fa 149 bool SAMConfig(void);
nebgnahz 0:54bf4b21c7fa 150 uint32_t getFirmwareVersion(void);
nebgnahz 0:54bf4b21c7fa 151 bool sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, uint16_t timeout = 1000);
nebgnahz 0:54bf4b21c7fa 152 bool writeGPIO(uint8_t pinstate);
nebgnahz 0:54bf4b21c7fa 153 uint8_t readGPIO(void);
nebgnahz 0:54bf4b21c7fa 154 bool setPassiveActivationRetries(uint8_t maxRetries);
nebgnahz 0:54bf4b21c7fa 155
nebgnahz 0:54bf4b21c7fa 156 // ISO14443A functions
nebgnahz 0:54bf4b21c7fa 157 bool readPassiveTargetID(uint8_t cardbaudrate, uint8_t * uid, uint8_t * uidLength);
nebgnahz 0:54bf4b21c7fa 158
nebgnahz 0:54bf4b21c7fa 159 // Mifare Classic functions
nebgnahz 0:54bf4b21c7fa 160 bool mifareclassic_IsFirstBlock (uint32_t uiBlock);
nebgnahz 0:54bf4b21c7fa 161 bool mifareclassic_IsTrailerBlock (uint32_t uiBlock);
nebgnahz 0:54bf4b21c7fa 162 uint8_t mifareclassic_AuthenticateBlock (uint8_t * uid, uint8_t uidLen, uint32_t blockNumber, uint8_t keyNumber, uint8_t * keyData);
nebgnahz 0:54bf4b21c7fa 163 uint8_t mifareclassic_ReadDataBlock (uint8_t blockNumber, uint8_t * data);
nebgnahz 0:54bf4b21c7fa 164 uint8_t mifareclassic_WriteDataBlock (uint8_t blockNumber, uint8_t * data);
nebgnahz 0:54bf4b21c7fa 165 uint8_t mifareclassic_FormatNDEF (void);
nebgnahz 0:54bf4b21c7fa 166 uint8_t mifareclassic_WriteNDEFURI (uint8_t sectorNumber, uint8_t uriIdentifier, const char * url);
nebgnahz 0:54bf4b21c7fa 167
nebgnahz 0:54bf4b21c7fa 168 // Mifare Ultralight functions
nebgnahz 0:54bf4b21c7fa 169 uint8_t mifareultralight_ReadPage (uint8_t page, uint8_t * buffer);
nebgnahz 0:54bf4b21c7fa 170
nebgnahz 0:54bf4b21c7fa 171 // Help functions to display formatted text
nebgnahz 0:54bf4b21c7fa 172 static void PrintHex(const uint8_t * data, const uint32_t numBytes);
nebgnahz 0:54bf4b21c7fa 173 static void PrintHexChar(const uint8_t * pbtData, const uint32_t numBytes);
nebgnahz 0:54bf4b21c7fa 174
nebgnahz 0:54bf4b21c7fa 175 private:
nebgnahz 0:54bf4b21c7fa 176 DigitalOut _clk;
nebgnahz 0:54bf4b21c7fa 177 DigitalIn _miso;
nebgnahz 0:54bf4b21c7fa 178 DigitalOut _mosi, _ss;
nebgnahz 0:54bf4b21c7fa 179 uint8_t _uid[7]; // ISO14443A uid
nebgnahz 0:54bf4b21c7fa 180 uint8_t _uidLen; // uid len
nebgnahz 0:54bf4b21c7fa 181 uint8_t _key[6]; // Mifare Classic key
nebgnahz 0:54bf4b21c7fa 182
nebgnahz 0:54bf4b21c7fa 183 bool spi_readack();
nebgnahz 0:54bf4b21c7fa 184 uint8_t readspistatus(void);
nebgnahz 0:54bf4b21c7fa 185 void readspidata(uint8_t* buff, uint8_t n);
nebgnahz 0:54bf4b21c7fa 186 void spiwritecommand(uint8_t* cmd, uint8_t cmdlen);
nebgnahz 0:54bf4b21c7fa 187 void spiwrite(uint8_t c);
nebgnahz 0:54bf4b21c7fa 188 uint8_t spiread(void);
nebgnahz 0:54bf4b21c7fa 189 };
nebgnahz 0:54bf4b21c7fa 190
nebgnahz 0:54bf4b21c7fa 191 #endif // _PN532_ADAFRUIT_PN532_H_