forked from Seeed-Studio/PN532
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPN532_SPI.cpp
More file actions
234 lines (191 loc) · 4.97 KB
/
PN532_SPI.cpp
File metadata and controls
234 lines (191 loc) · 4.97 KB
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
#ifdef NFC_INTERFACE_SPI
#include "PN532_SPI.h"
#include "PN532_debug.h"
#include "Arduino.h"
#define STATUS_READ 2
#define DATA_WRITE 1
#define DATA_READ 3
PN532_SPI::PN532_SPI(SPIClass &spi, uint8_t ss)
{
command = 0;
_spi = &spi;
_ss = ss;
}
void PN532_SPI::begin()
{
pinMode(_ss, OUTPUT);
_spi->begin();
_spi->beginTransaction(SPISettings(1000000, LSBFIRST, SPI_MODE0));
_spi->endTransaction();
#if defined(ARDUINO_XIAO_RA4M1) || defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_RP2350)
#else
// _spi->setDataMode(SPI_MODE0); // PN532 only supports mode0
// _spi->setBitOrder(LSBFIRST);
#if defined __SAM3X8E__
/** DUE spi library does not support SPI_CLOCK_DIV8 macro */
_spi->setClockDivider(42); // set clock 2MHz(max: 5MHz)
#elif defined __SAMD21G18A__
/** M0 spi library does not support SPI_CLOCK_DIV8 macro */
_spi->setClockDivider(24); // set clock 2MHz(max: 5MHz)
#else
_spi->setClockDivider(SPI_CLOCK_DIV8); // set clock 2MHz(max: 5MHz)
#endif
#endif
}
void PN532_SPI::wakeup()
{
digitalWrite(_ss, LOW);
delay(2);
digitalWrite(_ss, HIGH);
}
int8_t PN532_SPI::writeCommand(const uint8_t *header, uint8_t hlen, const uint8_t *body, uint8_t blen)
{
command = header[0];
writeFrame(header, hlen, body, blen);
uint8_t timeout = PN532_ACK_WAIT_TIME;
while (!isReady())
{
delay(1);
timeout--;
if (0 == timeout)
{
DMSG("Time out when waiting for ACK\n");
return -2;
}
}
if (readAckFrame())
{
DMSG("Invalid ACK\n");
return PN532_INVALID_ACK;
}
return 0;
}
int16_t PN532_SPI::readResponse(uint8_t buf[], uint8_t len, uint16_t timeout)
{
uint16_t time = 0;
while (!isReady())
{
delay(1);
time++;
if (time > timeout)
{
return PN532_TIMEOUT;
}
}
digitalWrite(_ss, LOW);
delay(1);
int16_t result;
do
{
write(DATA_READ);
if (0x00 != read() || // PREAMBLE
0x00 != read() || // STARTCODE1
0xFF != read() // STARTCODE2
)
{
result = PN532_INVALID_FRAME;
break;
}
uint8_t length = read();
if (0 != (uint8_t)(length + read()))
{ // checksum of length
result = PN532_INVALID_FRAME;
break;
}
uint8_t cmd = command + 1; // response command
if (PN532_PN532TOHOST != read() || (cmd) != read())
{
result = PN532_INVALID_FRAME;
break;
}
DMSG("read: ");
DMSG_HEX(cmd);
length -= 2;
if (length > len)
{
for (uint8_t i = 0; i < length; i++)
{
DMSG_HEX(read()); // dump message
}
DMSG("\nNot enough space\n");
read();
read();
result = PN532_NO_SPACE; // not enough space
break;
}
uint8_t sum = PN532_PN532TOHOST + cmd;
for (uint8_t i = 0; i < length; i++)
{
buf[i] = read();
sum += buf[i];
DMSG_HEX(buf[i]);
}
DMSG('\n');
uint8_t checksum = read();
if (0 != (uint8_t)(sum + checksum))
{
DMSG("checksum is not ok\n");
result = PN532_INVALID_FRAME;
break;
}
read(); // POSTAMBLE
result = length;
} while (0);
digitalWrite(_ss, HIGH);
return result;
}
bool PN532_SPI::isReady()
{
digitalWrite(_ss, LOW);
write(STATUS_READ);
uint8_t status = read() & 1;
digitalWrite(_ss, HIGH);
return status;
}
void PN532_SPI::writeFrame(const uint8_t *header, uint8_t hlen, const uint8_t *body, uint8_t blen)
{
digitalWrite(_ss, LOW);
delay(2); // wake up PN532
write(DATA_WRITE);
write(PN532_PREAMBLE);
write(PN532_STARTCODE1);
write(PN532_STARTCODE2);
uint8_t length = hlen + blen + 1; // length of data field: TFI + DATA
write(length);
write(~length + 1); // checksum of length
write(PN532_HOSTTOPN532);
uint8_t sum = PN532_HOSTTOPN532; // sum of TFI + DATA
DMSG("write: ");
for (uint8_t i = 0; i < hlen; i++)
{
write(header[i]);
sum += header[i];
DMSG_HEX(header[i]);
}
for (uint8_t i = 0; i < blen; i++)
{
write(body[i]);
sum += body[i];
DMSG_HEX(body[i]);
}
uint8_t checksum = ~sum + 1; // checksum of TFI + DATA
write(checksum);
write(PN532_POSTAMBLE);
digitalWrite(_ss, HIGH);
DMSG('\n');
}
int8_t PN532_SPI::readAckFrame()
{
const uint8_t PN532_ACK[] = {0, 0, 0xFF, 0, 0xFF, 0};
uint8_t ackBuf[sizeof(PN532_ACK)];
digitalWrite(_ss, LOW);
delay(1);
write(DATA_READ);
for (uint8_t i = 0; i < sizeof(PN532_ACK); i++)
{
ackBuf[i] = read();
}
digitalWrite(_ss, HIGH);
return memcmp(ackBuf, PN532_ACK, sizeof(PN532_ACK));
}
#endif // NFC_INTERFACE_SPI