| 121 |
Kevin |
1 |
#ifndef NFC_H
|
|
|
2 |
#define NFC_H
|
|
|
3 |
|
|
|
4 |
/* PN532 NFC Reader from Adafruit */
|
|
|
5 |
|
|
|
6 |
#define PN532_PREAMBLE (0x00)
|
|
|
7 |
#define PN532_STARTCODE1 (0x00)
|
|
|
8 |
#define PN532_STARTCODE2 (0xFF)
|
|
|
9 |
#define PN532_POSTAMBLE (0x00)
|
|
|
10 |
|
|
|
11 |
#define PN532_HOSTTOPN532 (0xD4)
|
|
|
12 |
|
|
|
13 |
// PN532 Commands
|
|
|
14 |
#define PN532_COMMAND_DIAGNOSE (0x00)
|
|
|
15 |
#define PN532_COMMAND_GETFIRMWAREVERSION (0x02)
|
|
|
16 |
#define PN532_COMMAND_GETGENERALSTATUS (0x04)
|
|
|
17 |
#define PN532_COMMAND_READREGISTER (0x06)
|
|
|
18 |
#define PN532_COMMAND_WRITEREGISTER (0x08)
|
|
|
19 |
#define PN532_COMMAND_READGPIO (0x0C)
|
|
|
20 |
#define PN532_COMMAND_WRITEGPIO (0x0E)
|
|
|
21 |
#define PN532_COMMAND_SETSERIALBAUDRATE (0x10)
|
|
|
22 |
#define PN532_COMMAND_SETPARAMETERS (0x12)
|
|
|
23 |
#define PN532_COMMAND_SAMCONFIGURATION (0x14)
|
|
|
24 |
#define PN532_COMMAND_POWERDOWN (0x16)
|
|
|
25 |
#define PN532_COMMAND_RFCONFIGURATION (0x32)
|
|
|
26 |
#define PN532_COMMAND_RFREGULATIONTEST (0x58)
|
|
|
27 |
#define PN532_COMMAND_INJUMPFORDEP (0x56)
|
|
|
28 |
#define PN532_COMMAND_INJUMPFORPSL (0x46)
|
|
|
29 |
#define PN532_COMMAND_INLISTPASSIVETARGET (0x4A)
|
|
|
30 |
#define PN532_COMMAND_INATR (0x50)
|
|
|
31 |
#define PN532_COMMAND_INPSL (0x4E)
|
|
|
32 |
#define PN532_COMMAND_INDATAEXCHANGE (0x40)
|
|
|
33 |
#define PN532_COMMAND_INCOMMUNICATETHRU (0x42)
|
|
|
34 |
#define PN532_COMMAND_INDESELECT (0x44)
|
|
|
35 |
#define PN532_COMMAND_INRELEASE (0x52)
|
|
|
36 |
#define PN532_COMMAND_INSELECT (0x54)
|
|
|
37 |
#define PN532_COMMAND_INAUTOPOLL (0x60)
|
|
|
38 |
#define PN532_COMMAND_TGINITASTARGET (0x8C)
|
|
|
39 |
#define PN532_COMMAND_TGSETGENERALBYTES (0x92)
|
|
|
40 |
#define PN532_COMMAND_TGGETDATA (0x86)
|
|
|
41 |
#define PN532_COMMAND_TGSETDATA (0x8E)
|
|
|
42 |
#define PN532_COMMAND_TGSETMETADATA (0x94)
|
|
|
43 |
#define PN532_COMMAND_TGGETINITIATORCOMMAND (0x88)
|
|
|
44 |
#define PN532_COMMAND_TGRESPONSETOINITIATOR (0x90)
|
|
|
45 |
#define PN532_COMMAND_TGGETTARGETSTATUS (0x8A)
|
|
|
46 |
|
|
|
47 |
#define PN532_WAKEUP (0x55)
|
|
|
48 |
|
|
|
49 |
#define PN532_SPI_STATREAD (0x02)
|
|
|
50 |
#define PN532_SPI_DATAWRITE (0x01)
|
|
|
51 |
#define PN532_SPI_DATAREAD (0x03)
|
|
|
52 |
#define PN532_SPI_READY (0x01)
|
|
|
53 |
|
|
|
54 |
#define PN532_I2C_ADDRESS (0x48 >> 1)
|
|
|
55 |
#define PN532_I2C_READBIT (0x01)
|
|
|
56 |
#define PN532_I2C_BUSY (0x00)
|
|
|
57 |
#define PN532_I2C_READY (0x01)
|
|
|
58 |
#define PN532_I2C_READYTIMEOUT (20)
|
|
|
59 |
|
|
|
60 |
#define PN532_MIFARE_ISO14443A (0x00)
|
|
|
61 |
|
|
|
62 |
// Mifare Commands
|
|
|
63 |
#define MIFARE_CMD_AUTH_A (0x60)
|
|
|
64 |
#define MIFARE_CMD_AUTH_B (0x61)
|
|
|
65 |
#define MIFARE_CMD_READ (0x30)
|
|
|
66 |
#define MIFARE_CMD_WRITE (0xA0)
|
|
|
67 |
#define MIFARE_CMD_TRANSFER (0xB0)
|
|
|
68 |
#define MIFARE_CMD_DECREMENT (0xC0)
|
|
|
69 |
#define MIFARE_CMD_INCREMENT (0xC1)
|
|
|
70 |
#define MIFARE_CMD_STORE (0xC2)
|
|
|
71 |
|
|
|
72 |
// Prefixes for NDEF Records (to identify record type)
|
|
|
73 |
#define NDEF_URIPREFIX_NONE (0x00)
|
|
|
74 |
#define NDEF_URIPREFIX_HTTP_WWWDOT (0x01)
|
|
|
75 |
#define NDEF_URIPREFIX_HTTPS_WWWDOT (0x02)
|
|
|
76 |
#define NDEF_URIPREFIX_HTTP (0x03)
|
|
|
77 |
#define NDEF_URIPREFIX_HTTPS (0x04)
|
|
|
78 |
#define NDEF_URIPREFIX_TEL (0x05)
|
|
|
79 |
#define NDEF_URIPREFIX_MAILTO (0x06)
|
|
|
80 |
#define NDEF_URIPREFIX_FTP_ANONAT (0x07)
|
|
|
81 |
#define NDEF_URIPREFIX_FTP_FTPDOT (0x08)
|
|
|
82 |
#define NDEF_URIPREFIX_FTPS (0x09)
|
|
|
83 |
#define NDEF_URIPREFIX_SFTP (0x0A)
|
|
|
84 |
#define NDEF_URIPREFIX_SMB (0x0B)
|
|
|
85 |
#define NDEF_URIPREFIX_NFS (0x0C)
|
|
|
86 |
#define NDEF_URIPREFIX_FTP (0x0D)
|
|
|
87 |
#define NDEF_URIPREFIX_DAV (0x0E)
|
|
|
88 |
#define NDEF_URIPREFIX_NEWS (0x0F)
|
|
|
89 |
#define NDEF_URIPREFIX_TELNET (0x10)
|
|
|
90 |
#define NDEF_URIPREFIX_IMAP (0x11)
|
|
|
91 |
#define NDEF_URIPREFIX_RTSP (0x12)
|
|
|
92 |
#define NDEF_URIPREFIX_URN (0x13)
|
|
|
93 |
#define NDEF_URIPREFIX_POP (0x14)
|
|
|
94 |
#define NDEF_URIPREFIX_SIP (0x15)
|
|
|
95 |
#define NDEF_URIPREFIX_SIPS (0x16)
|
|
|
96 |
#define NDEF_URIPREFIX_TFTP (0x17)
|
|
|
97 |
#define NDEF_URIPREFIX_BTSPP (0x18)
|
|
|
98 |
#define NDEF_URIPREFIX_BTL2CAP (0x19)
|
|
|
99 |
#define NDEF_URIPREFIX_BTGOEP (0x1A)
|
|
|
100 |
#define NDEF_URIPREFIX_TCPOBEX (0x1B)
|
|
|
101 |
#define NDEF_URIPREFIX_IRDAOBEX (0x1C)
|
|
|
102 |
#define NDEF_URIPREFIX_FILE (0x1D)
|
|
|
103 |
#define NDEF_URIPREFIX_URN_EPC_ID (0x1E)
|
|
|
104 |
#define NDEF_URIPREFIX_URN_EPC_TAG (0x1F)
|
|
|
105 |
#define NDEF_URIPREFIX_URN_EPC_PAT (0x20)
|
|
|
106 |
#define NDEF_URIPREFIX_URN_EPC_RAW (0x21)
|
|
|
107 |
#define NDEF_URIPREFIX_URN_EPC (0x22)
|
|
|
108 |
#define NDEF_URIPREFIX_URN_NFC (0x23)
|
|
|
109 |
|
|
|
110 |
#define PN532_GPIO_VALIDATIONBIT (0x80)
|
|
|
111 |
#define PN532_GPIO_P30 (0)
|
|
|
112 |
#define PN532_GPIO_P31 (1)
|
|
|
113 |
#define PN532_GPIO_P32 (2)
|
|
|
114 |
#define PN532_GPIO_P33 (3)
|
|
|
115 |
#define PN532_GPIO_P34 (4)
|
|
|
116 |
#define PN532_GPIO_P35 (5)
|
|
|
117 |
|
|
|
118 |
#define PN532_PACKBUFFSIZ 64
|
|
|
119 |
#define PN532_TIMEOUT 1000
|
|
|
120 |
|
|
|
121 |
typedef struct {
|
|
|
122 |
unsigned char IC;
|
|
|
123 |
unsigned char Ver;
|
|
|
124 |
unsigned char Rev;
|
|
|
125 |
unsigned char Support;
|
|
|
126 |
} NFC_FIRMWARE_VERSION;
|
|
|
127 |
|
| 126 |
Kevin |
128 |
typedef struct {
|
|
|
129 |
unsigned char TG;
|
|
|
130 |
unsigned char SENS_RES[2];
|
|
|
131 |
unsigned char SEL_RES;
|
|
|
132 |
unsigned char NFCID_LEN;
|
|
|
133 |
unsigned char NFCID[7];
|
|
|
134 |
} NFC_TargetDataMiFare;
|
|
|
135 |
// Size can be 9 or 12 bytes
|
|
|
136 |
|
| 121 |
Kevin |
137 |
typedef struct __NFC_DATA {
|
|
|
138 |
unsigned char packetbuffer[PN532_PACKBUFFSIZ];
|
|
|
139 |
} NFC_DATA;
|
|
|
140 |
|
|
|
141 |
void NFC_Init(void);
|
|
|
142 |
|
|
|
143 |
// Generic PN532 functions
|
|
|
144 |
unsigned char NFC_SAMConfig(void);
|
| 154 |
Kevin |
145 |
NFC_FIRMWARE_VERSION NFC_Get_Firmware_Version(void);
|
|
|
146 |
unsigned char NFC_Send_Command_Check_Ack(unsigned char *cmd, unsigned char cmdlen);
|
| 121 |
Kevin |
147 |
//unsigned char NFC_writeGPIO(unsigned char pinstate);
|
|
|
148 |
//unsigned char NFC_readGPIO(void);
|
|
|
149 |
|
|
|
150 |
// ISO14443A functions
|
| 154 |
Kevin |
151 |
unsigned char NFC_Read_Passive_Target_ID(NFC_TargetDataMiFare *uidData);
|
|
|
152 |
unsigned char NFC_Poll_Targets(unsigned char number, unsigned char period, NFC_TargetDataMiFare *uidData);
|
| 121 |
Kevin |
153 |
|
|
|
154 |
// Mifare Classic functions
|
|
|
155 |
unsigned char NFC_mifareclassic_IsFirstBlock(unsigned long uiBlock);
|
|
|
156 |
unsigned char NFC_mifareclassic_IsTrailerBlock(unsigned long uiBlock);
|
|
|
157 |
unsigned char NFC_mifareclassic_AuthenticateBlock(unsigned char *uid, unsigned char uidLen, unsigned long blockNumber, unsigned char keyNumber, unsigned char *keyData);
|
|
|
158 |
unsigned char NFC_mifareclassic_ReadDataBlock(unsigned char blockNumber, unsigned char *data);
|
|
|
159 |
unsigned char NFC_mifareclassic_WriteDataBlock(unsigned char blockNumber, unsigned char *data);
|
|
|
160 |
unsigned char NFC_mifareclassic_FormatNDEF(void);
|
|
|
161 |
unsigned char NFC_mifareclassic_WriteNDEFURI(unsigned char sectorNumber, unsigned char uriIdentifier, const char * url);
|
|
|
162 |
|
|
|
163 |
// Mifare Ultralight functions
|
|
|
164 |
//unsigned char NFC_mifareultralight_ReadPage(unsigned char page, unsigned char * buffer);
|
|
|
165 |
|
|
|
166 |
// Low level SPI functions
|
|
|
167 |
unsigned char NFC_I2C_Read_ACK(void);
|
|
|
168 |
unsigned char NFC_I2C_Read_Status(void);
|
|
|
169 |
void NFC_I2C_Read_Data(unsigned char *buffer, unsigned char length);
|
|
|
170 |
void NFC_I2C_Write_Cmd(unsigned char *cmd, unsigned char cmdlen);
|
|
|
171 |
|
|
|
172 |
#endif
|
|
|
173 |
|