| Line 3... |
Line 3... |
| 3 |
#include "i2c.h"
|
3 |
#include "i2c.h"
|
| 4 |
#include <string.h>
|
4 |
#include <string.h>
|
| 5 |
#include <delays.h>
|
5 |
#include <delays.h>
|
| 6 |
|
6 |
|
| 7 |
static NFC_DATA nfc_data;
|
7 |
static NFC_DATA nfc_data;
|
| - |
|
8 |
static NFC_DATA *nfc_data_p = &nfc_data;
|
| 8 |
|
9 |
|
| 9 |
// Const value arrays for comparison use
|
10 |
// Const value arrays for comparison use
|
| 10 |
static char pn532response_firmwarevers[] = {0x01, 0x00, 0x00, 0xFF, 0x06, 0xFA, 0xD5, 0x03};
|
11 |
static char pn532response_firmwarevers[] = {0x01, 0x00, 0x00, 0xFF, 0x06, 0xFA, 0xD5, 0x03};
|
| 11 |
static char pn532ack[] = {0x01, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00};
|
12 |
static char pn532ack[] = {0x01, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00};
|
| 12 |
|
13 |
|
| Line 21... |
Line 22... |
| 21 |
LATCbits.LATC2 = 1;
|
22 |
LATCbits.LATC2 = 1;
|
| 22 |
}
|
23 |
}
|
| 23 |
|
24 |
|
| 24 |
// Configures the SAM (Secure Access Module)
|
25 |
// Configures the SAM (Secure Access Module)
|
| 25 |
unsigned char NFC_SAMConfig() {
|
26 |
unsigned char NFC_SAMConfig() {
|
| 26 |
nfc_data.packetbuffer[0] = PN532_COMMAND_SAMCONFIGURATION;
|
27 |
nfc_data_p->packetbuffer[0] = PN532_COMMAND_SAMCONFIGURATION;
|
| 27 |
nfc_data.packetbuffer[1] = 0x01; // Normal mode
|
28 |
nfc_data_p->packetbuffer[1] = 0x01; // Normal mode
|
| 28 |
nfc_data.packetbuffer[2] = 0x14; // Timeout 50ms * 20 = 1s
|
29 |
nfc_data_p->packetbuffer[2] = 0x14; // Timeout 50ms * 20 = 1s
|
| 29 |
nfc_data.packetbuffer[3] = 0x01; // Use IRQ pin
|
30 |
nfc_data_p->packetbuffer[3] = 0x01; // Use IRQ pin
|
| 30 |
|
31 |
|
| 31 |
if (!NFC_sendCommandCheckAck(nfc_data.packetbuffer, 4))
|
32 |
if (!NFC_sendCommandCheckAck(nfc_data_p->packetbuffer, 4))
|
| 32 |
return 0;
|
33 |
return 0;
|
| 33 |
|
34 |
|
| 34 |
NFC_I2C_Read_Data(nfc_data.packetbuffer, 8);
|
35 |
NFC_I2C_Read_Data(nfc_data_p->packetbuffer, 8);
|
| 35 |
|
36 |
|
| 36 |
return (nfc_data.packetbuffer[7] == 0x15);
|
37 |
return (nfc_data_p->packetbuffer[7] == 0x15);
|
| 37 |
}
|
38 |
}
|
| 38 |
|
39 |
|
| 39 |
// Checks the firmware version of the PN5xx chip
|
40 |
// Checks the firmware version of the PN5xx chip
|
| 40 |
NFC_FIRMWARE_VERSION NFC_getFirmwareVersion(void) {
|
41 |
NFC_FIRMWARE_VERSION NFC_getFirmwareVersion(void) {
|
| 41 |
NFC_FIRMWARE_VERSION response = {0, 0, 0, 0};
|
42 |
NFC_FIRMWARE_VERSION response = {0, 0, 0, 0};
|
| 42 |
|
43 |
|
| 43 |
// Create and send command
|
44 |
// Create and send command
|
| 44 |
nfc_data.packetbuffer[0] = PN532_COMMAND_GETFIRMWAREVERSION;
|
45 |
nfc_data_p->packetbuffer[0] = PN532_COMMAND_GETFIRMWAREVERSION;
|
| 45 |
|
46 |
|
| 46 |
if (!NFC_sendCommandCheckAck(nfc_data.packetbuffer, 1))
|
47 |
if (!NFC_sendCommandCheckAck(nfc_data_p->packetbuffer, 1))
|
| 47 |
return response;
|
48 |
return response;
|
| 48 |
|
49 |
|
| 49 |
// Read back data from the PN532
|
50 |
// Read back data from the PN532
|
| 50 |
NFC_I2C_Read_Data(nfc_data.packetbuffer, 12);
|
51 |
NFC_I2C_Read_Data(nfc_data_p->packetbuffer, 12);
|
| 51 |
|
52 |
|
| 52 |
// Compare and check returned values
|
53 |
// Compare and check returned values
|
| 53 |
if (strncmp((char *) nfc_data.packetbuffer, (char *) pn532response_firmwarevers, 8) != 0)
|
54 |
if (strncmp((char *) nfc_data_p->packetbuffer, (char *) pn532response_firmwarevers, 8) != 0)
|
| 54 |
return response;
|
55 |
return response;
|
| 55 |
|
56 |
|
| 56 |
// Save and return info
|
57 |
// Save and return info
|
| 57 |
response.IC = nfc_data.packetbuffer[8];
|
58 |
response.IC = nfc_data_p->packetbuffer[8];
|
| 58 |
response.Ver = nfc_data.packetbuffer[9];
|
59 |
response.Ver = nfc_data_p->packetbuffer[9];
|
| 59 |
response.Rev = nfc_data.packetbuffer[10];
|
60 |
response.Rev = nfc_data_p->packetbuffer[10];
|
| 60 |
response.Support = nfc_data.packetbuffer[11];
|
61 |
response.Support = nfc_data_p->packetbuffer[11];
|
| 61 |
|
62 |
|
| 62 |
return response;
|
63 |
return response;
|
| 63 |
}
|
64 |
}
|
| 64 |
|
65 |
|
| 65 |
// Sends a command and waits a specified period for the ACK
|
66 |
// Sends a command and waits a specified period for the ACK
|
| Line 88... |
Line 89... |
| 88 |
}
|
89 |
}
|
| 89 |
|
90 |
|
| 90 |
// Passive polling, waits for an ISO14443A target to enter the field
|
91 |
// Passive polling, waits for an ISO14443A target to enter the field
|
| 91 |
unsigned char NFC_readPassiveTargetID(NFC_TargetDataMiFare *cardData) {
|
92 |
unsigned char NFC_readPassiveTargetID(NFC_TargetDataMiFare *cardData) {
|
| 92 |
|
93 |
|
| 93 |
nfc_data.packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET;
|
94 |
nfc_data_p->packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET;
|
| 94 |
nfc_data.packetbuffer[1] = 2; // Max 2 cards at once
|
95 |
nfc_data_p->packetbuffer[1] = 2; // Max 2 cards at once
|
| 95 |
nfc_data.packetbuffer[2] = PN532_MIFARE_ISO14443A; // Mifare only
|
96 |
nfc_data_p->packetbuffer[2] = PN532_MIFARE_ISO14443A; // Mifare only
|
| 96 |
|
97 |
|
| 97 |
if (!NFC_sendCommandCheckAck(nfc_data.packetbuffer, 3))
|
98 |
if (!NFC_sendCommandCheckAck(nfc_data_p->packetbuffer, 3))
|
| 98 |
return 0;
|
99 |
return 0;
|
| 99 |
|
100 |
|
| 100 |
// Wait for IRQ line
|
101 |
// Wait for IRQ line
|
| 101 |
while (NFC_I2C_Read_Status() != PN532_I2C_READY);
|
102 |
while (NFC_I2C_Read_Status() != PN532_I2C_READY);
|
| 102 |
|
103 |
|
| 103 |
NFC_I2C_Read_Data(nfc_data.packetbuffer, 35);
|
104 |
NFC_I2C_Read_Data(nfc_data_p->packetbuffer, 35);
|
| 104 |
|
105 |
|
| 105 |
/* InListPassiveTarget response should be in the following format:
|
106 |
/* InListPassiveTarget response should be in the following format:
|
| 106 |
* Byte Description
|
107 |
* Byte Description
|
| 107 |
* ---------- ------------------
|
108 |
* ---------- ------------------
|
| 108 |
* b0 Data ACK
|
109 |
* b0 Data ACK
|
| Line 111... |
Line 112... |
| 111 |
* b9..N NFC_TargetDataMiFare[2]
|
112 |
* b9..N NFC_TargetDataMiFare[2]
|
| 112 |
* bN+1..N+2 Checksum + postamble
|
113 |
* bN+1..N+2 Checksum + postamble
|
| 113 |
*/
|
114 |
*/
|
| 114 |
|
115 |
|
| 115 |
// Check # of tags found
|
116 |
// Check # of tags found
|
| 116 |
if (!nfc_data.packetbuffer[8])
|
117 |
if (!nfc_data_p->packetbuffer[8])
|
| 117 |
return 0;
|
118 |
return 0;
|
| 118 |
|
119 |
|
| 119 |
// Save data from first card
|
120 |
// Save data from first card
|
| 120 |
if (nfc_data.packetbuffer[13] == 4) {
|
121 |
if (nfc_data_p->packetbuffer[13] == 4) {
|
| 121 |
memcpy((char *)&cardData[0], (const char *)&nfc_data.packetbuffer[9], 9);
|
122 |
memcpy((char *)&cardData[0], (const char *)&nfc_data_p->packetbuffer[9], 9);
|
| 122 |
} else {
|
123 |
} else {
|
| 123 |
memcpy((char *)&cardData[0], (const char *)&nfc_data.packetbuffer[9], 12);
|
124 |
memcpy((char *)&cardData[0], (const char *)&nfc_data_p->packetbuffer[9], 12);
|
| 124 |
}
|
125 |
}
|
| 125 |
|
126 |
|
| 126 |
// Save data from second card
|
127 |
// Save data from second card
|
| 127 |
if (nfc_data.packetbuffer[8] == 2) {
|
128 |
if (nfc_data_p->packetbuffer[8] == 2) {
|
| 128 |
// Offset will vary depending on length of first card
|
129 |
// Offset will vary depending on length of first card
|
| 129 |
if (nfc_data.packetbuffer[13] == 4) {
|
130 |
if (nfc_data_p->packetbuffer[13] == 4) {
|
| 130 |
if (nfc_data.packetbuffer[22] == 4) {
|
131 |
if (nfc_data_p->packetbuffer[22] == 4) {
|
| 131 |
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[18], 9);
|
132 |
memcpy((char *)&cardData[1], (const char *)&nfc_data_p->packetbuffer[18], 9);
|
| 132 |
} else {
|
133 |
} else {
|
| 133 |
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[18], 12);
|
134 |
memcpy((char *)&cardData[1], (const char *)&nfc_data_p->packetbuffer[18], 12);
|
| 134 |
}
|
135 |
}
|
| 135 |
} else { // Length of first UID is 7
|
136 |
} else { // Length of first UID is 7
|
| 136 |
if (nfc_data.packetbuffer[25] == 4) {
|
137 |
if (nfc_data_p->packetbuffer[25] == 4) {
|
| 137 |
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[21], 9);
|
138 |
memcpy((char *)&cardData[1], (const char *)&nfc_data_p->packetbuffer[21], 9);
|
| 138 |
} else {
|
139 |
} else {
|
| 139 |
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[21], 12);
|
140 |
memcpy((char *)&cardData[1], (const char *)&nfc_data_p->packetbuffer[21], 12);
|
| 140 |
}
|
141 |
}
|
| 141 |
}
|
142 |
}
|
| 142 |
}
|
143 |
}
|
| 143 |
|
144 |
|
| 144 |
// Return the number of cards detected
|
145 |
// Return the number of cards detected
|
| 145 |
return nfc_data.packetbuffer[8];
|
146 |
return nfc_data_p->packetbuffer[8];
|
| 146 |
}
|
147 |
}
|
| 147 |
|
148 |
|
| 148 |
// Active polling, returns number of cards in the field
|
149 |
// Active polling, returns number of cards in the field
|
| 149 |
unsigned char NFC_pollTargets(unsigned char number, unsigned char period, NFC_TargetDataMiFare *cardData) {
|
150 |
unsigned char NFC_pollTargets(unsigned char number, unsigned char period, NFC_TargetDataMiFare *cardData) {
|
| 150 |
|
151 |
|
| 151 |
nfc_data.packetbuffer[0] = PN532_COMMAND_INAUTOPOLL;
|
152 |
nfc_data_p->packetbuffer[0] = PN532_COMMAND_INAUTOPOLL;
|
| 152 |
nfc_data.packetbuffer[1] = number; // Number of polling
|
153 |
nfc_data_p->packetbuffer[1] = number; // Number of polling
|
| 153 |
nfc_data.packetbuffer[2] = period; // Polling period in units of 150ms
|
154 |
nfc_data_p->packetbuffer[2] = period; // Polling period in units of 150ms
|
| 154 |
nfc_data.packetbuffer[3] = 0x10; // Check for Mifare cards only
|
155 |
nfc_data_p->packetbuffer[3] = 0x10; // Check for Mifare cards only
|
| 155 |
|
156 |
|
| 156 |
if (!NFC_sendCommandCheckAck(nfc_data.packetbuffer, 4))
|
157 |
if (!NFC_sendCommandCheckAck(nfc_data_p->packetbuffer, 4))
|
| 157 |
return 0;
|
158 |
return 0;
|
| 158 |
|
159 |
|
| 159 |
// Wait for IRQ line
|
160 |
// Wait for IRQ line
|
| 160 |
while (NFC_I2C_Read_Status() != PN532_I2C_READY);
|
161 |
while (NFC_I2C_Read_Status() != PN532_I2C_READY);
|
| 161 |
|
162 |
|
| 162 |
NFC_I2C_Read_Data(nfc_data.packetbuffer, 37);
|
163 |
NFC_I2C_Read_Data(nfc_data_p->packetbuffer, 37);
|
| 163 |
|
164 |
|
| 164 |
/* InAutoPoll response should be in the following format:
|
165 |
/* InAutoPoll response should be in the following format:
|
| 165 |
* Byte Description
|
166 |
* Byte Description
|
| 166 |
* ---------- ------------------
|
167 |
* ---------- ------------------
|
| 167 |
* b0 Data ACK
|
168 |
* b0 Data ACK
|
| Line 172... |
Line 173... |
| 172 |
* b9..N NFC_TargetDataMiFare[1/2]
|
173 |
* b9..N NFC_TargetDataMiFare[1/2]
|
| 173 |
* bN+1..N+2 Checksum + postamble
|
174 |
* bN+1..N+2 Checksum + postamble
|
| 174 |
*/
|
175 |
*/
|
| 175 |
|
176 |
|
| 176 |
// Check # of tags found
|
177 |
// Check # of tags found
|
| 177 |
if (!nfc_data.packetbuffer[8])
|
178 |
if (!nfc_data_p->packetbuffer[8])
|
| 178 |
return 0;
|
179 |
return 0;
|
| 179 |
|
180 |
|
| 180 |
// Save data from first card
|
181 |
// Save data from first card
|
| 181 |
if (nfc_data.packetbuffer[15] == 4) {
|
182 |
if (nfc_data_p->packetbuffer[15] == 4) {
|
| 182 |
memcpy((char *)&cardData[0], (const char *)&nfc_data.packetbuffer[11], 9);
|
183 |
memcpy((char *)&cardData[0], (const char *)&nfc_data_p->packetbuffer[11], 9);
|
| 183 |
} else {
|
184 |
} else {
|
| 184 |
memcpy((char *)&cardData[0], (const char *)&nfc_data.packetbuffer[11], 12);
|
185 |
memcpy((char *)&cardData[0], (const char *)&nfc_data_p->packetbuffer[11], 12);
|
| 185 |
}
|
186 |
}
|
| 186 |
|
187 |
|
| 187 |
// Save data from second card
|
188 |
// Save data from second card
|
| 188 |
if (nfc_data.packetbuffer[8] == 2) {
|
189 |
if (nfc_data_p->packetbuffer[8] == 2) {
|
| 189 |
// Offset will vary depending on length of first card
|
190 |
// Offset will vary depending on length of first card
|
| 190 |
if (nfc_data.packetbuffer[15] == 4) {
|
191 |
if (nfc_data_p->packetbuffer[15] == 4) {
|
| 191 |
if (nfc_data.packetbuffer[26] == 4) {
|
192 |
if (nfc_data_p->packetbuffer[26] == 4) {
|
| 192 |
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[22], 9);
|
193 |
memcpy((char *)&cardData[1], (const char *)&nfc_data_p->packetbuffer[22], 9);
|
| 193 |
} else {
|
194 |
} else {
|
| 194 |
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[22], 12);
|
195 |
memcpy((char *)&cardData[1], (const char *)&nfc_data_p->packetbuffer[22], 12);
|
| 195 |
}
|
196 |
}
|
| 196 |
} else {
|
197 |
} else {
|
| 197 |
if (nfc_data.packetbuffer[29] == 4) {
|
198 |
if (nfc_data_p->packetbuffer[29] == 4) {
|
| 198 |
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[25], 9);
|
199 |
memcpy((char *)&cardData[1], (const char *)&nfc_data_p->packetbuffer[25], 9);
|
| 199 |
} else {
|
200 |
} else {
|
| 200 |
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[25], 12);
|
201 |
memcpy((char *)&cardData[1], (const char *)&nfc_data_p->packetbuffer[25], 12);
|
| 201 |
}
|
202 |
}
|
| 202 |
}
|
203 |
}
|
| 203 |
}
|
204 |
}
|
| 204 |
|
205 |
|
| 205 |
// Return the number of cards detected
|
206 |
// Return the number of cards detected
|
| 206 |
return nfc_data.packetbuffer[8];
|
207 |
return nfc_data_p->packetbuffer[8];
|
| 207 |
}
|
208 |
}
|
| 208 |
|
209 |
|
| 209 |
// Indicates whether the specified block number is the first block
|
210 |
// Indicates whether the specified block number is the first block
|
| 210 |
// in the sector (block 0 relative to the current sector)
|
211 |
// in the sector (block 0 relative to the current sector)
|
| 211 |
unsigned char NFC_mifareclassic_IsFirstBlock(unsigned long uiBlock) {
|
212 |
unsigned char NFC_mifareclassic_IsFirstBlock(unsigned long uiBlock) {
|
| Line 233... |
Line 234... |
| 233 |
// keyData = Pointer to a byte array containing the 6 byte key value
|
234 |
// keyData = Pointer to a byte array containing the 6 byte key value
|
| 234 |
|
235 |
|
| 235 |
unsigned char i;
|
236 |
unsigned char i;
|
| 236 |
|
237 |
|
| 237 |
// Assemble frame data
|
238 |
// Assemble frame data
|
| 238 |
nfc_data.packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; /* Data Exchange Header */
|
239 |
nfc_data_p->packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; /* Data Exchange Header */
|
| 239 |
nfc_data.packetbuffer[1] = 1; /* Max card numbers */
|
240 |
nfc_data_p->packetbuffer[1] = 1; /* Max card numbers */
|
| 240 |
nfc_data.packetbuffer[2] = (keyNumber) ? MIFARE_CMD_AUTH_A : MIFARE_CMD_AUTH_B;
|
241 |
nfc_data_p->packetbuffer[2] = (keyNumber) ? MIFARE_CMD_AUTH_A : MIFARE_CMD_AUTH_B;
|
| 241 |
nfc_data.packetbuffer[3] = blockNumber; /* Block Number (1K = 0..63, 4K = 0..255 */
|
242 |
nfc_data_p->packetbuffer[3] = blockNumber; /* Block Number (1K = 0..63, 4K = 0..255 */
|
| 242 |
for (i = 0; i < 6; i++) {
|
243 |
for (i = 0; i < 6; i++) {
|
| 243 |
nfc_data.packetbuffer[4 + i] = keyData[i];
|
244 |
nfc_data_p->packetbuffer[4 + i] = keyData[i];
|
| 244 |
}
|
245 |
}
|
| 245 |
for (i = 0; i < uidLen; i++) {
|
246 |
for (i = 0; i < uidLen; i++) {
|
| 246 |
nfc_data.packetbuffer[10 + i] = uid[i];
|
247 |
nfc_data_p->packetbuffer[10 + i] = uid[i];
|
| 247 |
}
|
248 |
}
|
| 248 |
|
249 |
|
| 249 |
// Send frame and check for ACK
|
250 |
// Send frame and check for ACK
|
| 250 |
if (!NFC_sendCommandCheckAck(nfc_data.packetbuffer, 10 + uidLen))
|
251 |
if (!NFC_sendCommandCheckAck(nfc_data_p->packetbuffer, 10 + uidLen))
|
| 251 |
return 0;
|
252 |
return 0;
|
| 252 |
|
253 |
|
| 253 |
// Read response from PN532
|
254 |
// Read response from PN532
|
| 254 |
NFC_I2C_Read_Data(nfc_data.packetbuffer, 12);
|
255 |
NFC_I2C_Read_Data(nfc_data_p->packetbuffer, 12);
|
| 255 |
|
256 |
|
| 256 |
return 1;
|
257 |
return 1;
|
| 257 |
}
|
258 |
}
|
| 258 |
|
259 |
|
| 259 |
// Tries to read an entire 16-byte data block at the specified block address
|
260 |
// Tries to read an entire 16-byte data block at the specified block address
|
| 260 |
unsigned char NFC_mifareclassic_ReadDataBlock(unsigned char blockNumber, unsigned char *data) {
|
261 |
unsigned char NFC_mifareclassic_ReadDataBlock(unsigned char blockNumber, unsigned char *data) {
|
| 261 |
unsigned char i;
|
262 |
unsigned char i;
|
| 262 |
|
263 |
|
| 263 |
// Assemble frame data
|
264 |
// Assemble frame data
|
| 264 |
nfc_data.packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
|
265 |
nfc_data_p->packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
|
| 265 |
nfc_data.packetbuffer[1] = 1; /* Card number */
|
266 |
nfc_data_p->packetbuffer[1] = 1; /* Card number */
|
| 266 |
nfc_data.packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */
|
267 |
nfc_data_p->packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */
|
| 267 |
nfc_data.packetbuffer[3] = blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */
|
268 |
nfc_data_p->packetbuffer[3] = blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */
|
| 268 |
|
269 |
|
| 269 |
// Send frame and check for ACK
|
270 |
// Send frame and check for ACK
|
| 270 |
if (!NFC_sendCommandCheckAck(nfc_data.packetbuffer, 4))
|
271 |
if (!NFC_sendCommandCheckAck(nfc_data_p->packetbuffer, 4))
|
| 271 |
return 0;
|
272 |
return 0;
|
| 272 |
|
273 |
|
| 273 |
// Read reponse
|
274 |
// Read reponse
|
| 274 |
NFC_I2C_Read_Data(nfc_data.packetbuffer, 26);
|
275 |
NFC_I2C_Read_Data(nfc_data_p->packetbuffer, 26);
|
| 275 |
|
276 |
|
| 276 |
// If byte 9 isnt 0x00 we probably have and error
|
277 |
// If byte 9 isnt 0x00 we probably have and error
|
| 277 |
if (nfc_data.packetbuffer[8] != 0x00) {
|
278 |
if (nfc_data_p->packetbuffer[8] != 0x00) {
|
| 278 |
return 0;
|
279 |
return 0;
|
| 279 |
}
|
280 |
}
|
| 280 |
|
281 |
|
| 281 |
// Copy the 16 data bytes into the data buffer
|
282 |
// Copy the 16 data bytes into the data buffer
|
| 282 |
// Block contents starts at byte 10 of a valid response
|
283 |
// Block contents starts at byte 10 of a valid response
|
| 283 |
for (i = 0; i < 16; i++) {
|
284 |
for (i = 0; i < 16; i++) {
|
| 284 |
data[i] = nfc_data.packetbuffer[9 + i];
|
285 |
data[i] = nfc_data_p->packetbuffer[9 + i];
|
| 285 |
}
|
286 |
}
|
| 286 |
|
287 |
|
| 287 |
return 1;
|
288 |
return 1;
|
| 288 |
}
|
289 |
}
|
| 289 |
|
290 |
|
| 290 |
// Tries to write an entire 16-byte data block at the specified block address
|
291 |
// Tries to write an entire 16-byte data block at the specified block address
|
| 291 |
unsigned char NFC_mifareclassic_WriteDataBlock(unsigned char blockNumber, unsigned char *data) {
|
292 |
unsigned char NFC_mifareclassic_WriteDataBlock(unsigned char blockNumber, unsigned char *data) {
|
| 292 |
unsigned char i;
|
293 |
unsigned char i;
|
| 293 |
|
294 |
|
| 294 |
// Assemble frame data
|
295 |
// Assemble frame data
|
| 295 |
nfc_data.packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
|
296 |
nfc_data_p->packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
|
| 296 |
nfc_data.packetbuffer[1] = 1; /* Card number */
|
297 |
nfc_data_p->packetbuffer[1] = 1; /* Card number */
|
| 297 |
nfc_data.packetbuffer[2] = MIFARE_CMD_WRITE; /* Mifare Write command = 0xA0 */
|
298 |
nfc_data_p->packetbuffer[2] = MIFARE_CMD_WRITE; /* Mifare Write command = 0xA0 */
|
| 298 |
nfc_data.packetbuffer[3] = blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */
|
299 |
nfc_data_p->packetbuffer[3] = blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */
|
| 299 |
for (i = 0; i < 16; i++) { /* Data Payload */
|
300 |
for (i = 0; i < 16; i++) { /* Data Payload */
|
| 300 |
nfc_data.packetbuffer[4 + i] = data[i];
|
301 |
nfc_data_p->packetbuffer[4 + i] = data[i];
|
| 301 |
}
|
302 |
}
|
| 302 |
|
303 |
|
| 303 |
// Send frame and check for ACK
|
304 |
// Send frame and check for ACK
|
| 304 |
if (!NFC_sendCommandCheckAck(nfc_data.packetbuffer, 20))
|
305 |
if (!NFC_sendCommandCheckAck(nfc_data_p->packetbuffer, 20))
|
| 305 |
return 0;
|
306 |
return 0;
|
| 306 |
|
307 |
|
| 307 |
// Read response
|
308 |
// Read response
|
| 308 |
NFC_I2C_Read_Data(nfc_data.packetbuffer, 26);
|
309 |
NFC_I2C_Read_Data(nfc_data_p->packetbuffer, 26);
|
| 309 |
|
310 |
|
| 310 |
return 1;
|
311 |
return 1;
|
| 311 |
}
|
312 |
}
|
| 312 |
|
313 |
|
| 313 |
// Formats a Mifare Classic card to store NDEF Records
|
314 |
// Formats a Mifare Classic card to store NDEF Records
|