Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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