-
Notifications
You must be signed in to change notification settings - Fork 23
/
PN532_I2C.cpp
300 lines (243 loc) · 7.98 KB
/
PN532_I2C.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**************************************************************************/
/*!
@file PN532_I2C.cpp
@author Odopod, a Nurun Company
@license BSD
This code is refactored by Odopod (http://www.odopod.com) based on the
Adafruit NFC ShieldI2C Library (https://github.com/adafruit/Adafruit_NFCShield_I2C).
*/
/**************************************************************************/
#include "PN532_I2C.h"
static const byte PN532_ACK[6] = {0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00};
static const byte PN532_RESPONSE_FIRMWARE_VERSION[6] = {0x00, 0xFF, 0x06, 0xFA, 0xD5, 0x03};
static byte packetbuffer[PN532_PACKBUFFSIZE];
/**************************************************************************/
/*!
@brief Instantiates a new PN532 I2C class
@param irq Location of the IRQ pin
@param reset Location of the RSTPD_N pin
*/
/**************************************************************************/
PN532_I2C::PN532_I2C(uint8_t irq, uint8_t reset) {
_irq = irq;
_reset = reset;
pinMode(_irq, INPUT);
pinMode(_reset, OUTPUT);
}
/**************************************************************************/
/*!
@brief Setups the HW
*/
/**************************************************************************/
void PN532_I2C::begin(void) {
Wire.begin();
// Reset the PN532
digitalWrite(_reset, HIGH);
digitalWrite(_reset, LOW);
delay(400);
digitalWrite(_reset, HIGH);
}
/**************************************************************************/
/*!
@brief Checks the firmware version of the PN5xx chip
@returns The chip's firmware version and ID
*/
/**************************************************************************/
uint32_t PN532_I2C::getFirmwareVersion(void) {
uint32_t response;
packetbuffer[0] = PN532_COMMAND_GETFIRMWAREVERSION;
if (! sendCommandCheckAck(packetbuffer, 1))
return 0;
// read data packet
readdata(packetbuffer, 12);
// check some basic stuff
if (0 != strncmp((char *)packetbuffer, (char *)PN532_RESPONSE_FIRMWARE_VERSION, 6)) {
#ifdef PN532DEBUG
Serial.println("Firmware doesn't match!");
#endif
return 0;
}
response = packetbuffer[7];
response <<= 8;
response |= packetbuffer[8];
response <<= 8;
response |= packetbuffer[9];
response <<= 8;
response |= packetbuffer[10];
return response;
}
/**************************************************************************/
/*!
@brief Tries to read the PN532 ACK frame
(not to be confused with the ACK signal)
*/
/**************************************************************************/
boolean PN532_I2C::readack(void) {
uint8_t ackbuff[6];
readdata(ackbuff, 6);
return (0 == strncmp((char *)ackbuff, (char *)PN532_ACK, 6));
}
/**************************************************************************/
/*!
@brief Sends a command and waits a specified period for the ACK
@param cmd Pointer to the command buffer
@param cmdlen The size of the command in bytes
@param timeout timeout before giving up
@returns 1 if everything is OK, 0 if timeout occured before an
ACK was recieved
*/
/**************************************************************************/
// default timeout of one second
boolean PN532_I2C::sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, uint16_t timeout) {
uint16_t timer = 0;
// write the command
sendcommand(cmd, cmdlen);
// Wait for chip to say its ready!
while (readstatus() != PN532_READY) {
if (timeout != 0) {
timer+=10;
if (timer > timeout)
return false;
}
delay(80);
}
#ifdef PN532DEBUG
Serial.println("IRQ received");
#endif
// read acknowledgement
if (!readack()) {
#ifdef PN532DEBUG
Serial.println("No ACK frame received!");
#endif
return false;
}
return true; // ack'd command
}
/**************************************************************************/
/*!
@brief Checks if the PN532 is ready
@returns 0 if the PN532 is busy, 1 if it is free
*/
/**************************************************************************/
uint8_t PN532_I2C::readstatus(void) {
uint8_t x = digitalRead(_irq);
if (x == 1)
return PN532_BUSY;
else
return PN532_READY;
}
/**************************************************************************/
/*!
@brief Reads n bytes of data from the PN532 via I2C
@param buff Pointer to the buffer where data will be written
@param n Number of bytes to be read
*/
/**************************************************************************/
void PN532_I2C::readdata(uint8_t* buffer, uint8_t length) {
uint16_t timer = 0;
delay(5);
#ifdef PN532DEBUG
Serial.print("Reading: ");
#endif
// Start read (n+1 to take into account leading 0x01 with I2C)
Wire.requestFrom((uint8_t)PN532_I2C_ADDRESS, (uint8_t)(length+2));
// Discard the leading 0x01
wirerecv();
for (uint8_t i=0; i<length; i++) {
delay(1);
buffer[i] = wirerecv();
#ifdef PN532DEBUG
Serial.print(" 0x");
Serial.print(buffer[i], HEX);
#endif
}
// Discard trailing 0x00 0x00
// wirerecv();
#ifdef PN532DEBUG
Serial.println();
#endif
}
/**************************************************************************/
/*!
@brief Writes a command to the PN532, automatically inserting the
preamble and required frame details (checksum, len, etc.)
@param cmd Pointer to the command buffer
@param cmdlen Command length in bytes
*/
/**************************************************************************/
void PN532_I2C::sendcommand(uint8_t* cmd, uint8_t cmdlen) {
#ifdef PN532DEBUG
Serial.println("command in:");
for (uint8_t i=0; i<cmdlen; i++) {
Serial.print(" 0x"); Serial.print(cmd[i], HEX);
}
Serial.println("");
#endif
uint8_t checksum;
cmdlen++;
#ifdef PN532DEBUG
Serial.print("\nSending: ");
Serial.println(cmdlen);
#endif
delay(2); // or whatever the delay is for waking up the board
// I2C START
Wire.beginTransmission(PN532_I2C_ADDRESS);
checksum = PN532_PREAMBLE + PN532_PREAMBLE + PN532_STARTCODE2;
wiresend(PN532_PREAMBLE);
wiresend(PN532_PREAMBLE);
wiresend(PN532_STARTCODE2);
wiresend(cmdlen);
wiresend(~cmdlen + 1);
wiresend(PN532_HOSTTOPN532);
checksum += PN532_HOSTTOPN532;
#ifdef PN532DEBUG
Serial.print(" 0x"); Serial.print(PN532_PREAMBLE, HEX);
Serial.print(" 0x"); Serial.print(PN532_PREAMBLE, HEX);
Serial.print(" 0x"); Serial.print(PN532_STARTCODE2, HEX);
Serial.print(" 0x"); Serial.print(cmdlen, HEX);
Serial.print(" 0x"); Serial.print(~cmdlen + 1, HEX);
Serial.print(" 0x"); Serial.print(PN532_HOSTTOPN532, HEX);
Serial.println("");
#endif
for (uint8_t i=0; i<cmdlen-1; i++) {
wiresend(cmd[i]);
checksum += cmd[i];
#ifdef PN532DEBUG
Serial.print(" 0x"); Serial.print(cmd[i], HEX);
#endif
}
wiresend(~checksum);
wiresend(PN532_POSTAMBLE); // I2C STOP
Wire.endTransmission();
#ifdef PN532DEBUG
Serial.println("");
Serial.print(" 0x"); Serial.print(~checksum, HEX);
Serial.print(" 0x"); Serial.print(PN532_POSTAMBLE, HEX);
Serial.println("");
#endif
}
/**************************************************************************/
/*!
@brief Sends a single byte via I2C
@param x The byte to send
*/
/**************************************************************************/
void PN532_I2C::wiresend(uint8_t x){
#if ARDUINO >= 100
Wire.write((uint8_t)x);
#else
Wire.send(x);
#endif
}
/**************************************************************************/
/*!
@brief Reads a single byte via I2C
*/
/**************************************************************************/
uint8_t PN532_I2C::wirerecv(void){
#if ARDUINO >= 100
return Wire.read();
#else
return Wire.receive();
#endif
}