-
Notifications
You must be signed in to change notification settings - Fork 21
/
pn532_uno.cpp
245 lines (222 loc) · 7.65 KB
/
pn532_uno.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
/**************************************************************************
* @file pn532_uno.c
* @author Yehui from Waveshare
* @license BSD
*
* This implements the peripheral interfaces.
*
* Check out the links above for our tutorials and wiring diagrams
* These chips use SPI communicate.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documnetation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
**************************************************************************/
#include <stdbool.h>
#include <stdint.h>
#include <SPI.h>
#include <Wire.h>
#include "pn532_uno.h"
#define PN532_SPI_STATREAD (0x02)
#define PN532_SPI_DATAWRITE (0x01)
#define PN532_SPI_DATAREAD (0x03)
#define PN532_SPI_READY (0x01)
#define PN532_SPI_TIMEOUT (10)
// This indicates if the bits read/write should be reversed
#define PN532_SPI_HARDWARE_LSB
#define PN532_NSS (4)
#define PN532_REQ (2)
#define PN532_RST (3)
#define PN532_I2C_READY (0x01)
#define PN532_I2C_ADDRESS (0x48 >> 1)
/**************************************************************************
* Reset and Log implements
**************************************************************************/
int PN532_Reset(void) {
digitalWrite(PN532_RST, HIGH);
delay(100);
digitalWrite(PN532_RST, LOW);
delay(500);
digitalWrite(PN532_RST, HIGH);
delay(100);
return PN532_STATUS_OK;
}
void PN532_Log(const char* log) {
Serial.println(log);
}
/**************************************************************************
* End: Reset and Log implements
**************************************************************************/
/**************************************************************************
* SPI
**************************************************************************/
uint8_t reverse_bit(uint8_t num) {
uint8_t result = 0;
for (uint8_t i = 0; i < 8; i++) {
result <<= 1;
result += (num & 1);
num >>= 1;
}
return result;
}
void spi_rw(uint8_t* data, uint8_t count) {
digitalWrite(PN532_NSS, LOW);
SPI.transfer(data, count);
digitalWrite(PN532_NSS, HIGH);
}
int PN532_SPI_ReadData(uint8_t* data, uint16_t count) {
uint8_t frame[count + 1];
frame[0] = PN532_SPI_DATAREAD;
delay(5);
spi_rw(frame, count + 1);
for (uint8_t i = 0; i < count; i++) {
data[i] = frame[i + 1];
}
return PN532_STATUS_OK;
}
int PN532_SPI_WriteData(uint8_t* data, uint16_t count) {
uint8_t frame[count + 1];
frame[0] = PN532_SPI_DATAWRITE;
for (uint8_t i = 0; i < count; i++) {
frame[i + 1] = data[i];
}
spi_rw(frame, count + 1);
return PN532_STATUS_OK;
}
bool PN532_SPI_WaitReady(uint32_t timeout) {
uint8_t status[] = {PN532_SPI_STATREAD, 0x00};
uint32_t tickstart = millis();
while (millis() - tickstart < timeout) {
delay(10);
spi_rw(status, sizeof(status));
if (status[1] == PN532_SPI_READY) {
return true;
} else {
delay(5);
}
}
return false;
}
int PN532_SPI_Wakeup(void) {
// Send any special commands/data to wake up PN532
uint8_t data[] = {0x00};
delay(1000);
digitalWrite(PN532_NSS, LOW);
delay(2); // T_osc_start
spi_rw(data, 1);
delay(2); // T_osc_start
digitalWrite(PN532_NSS, HIGH);
delay(1000);
return PN532_STATUS_OK;
}
void PN532_SPI_Init(PN532* pn532) {
// init the pn532 functions
pn532->reset = PN532_Reset;
pn532->read_data = PN532_SPI_ReadData;
pn532->write_data = PN532_SPI_WriteData;
pn532->wait_ready = PN532_SPI_WaitReady;
pn532->wakeup = PN532_SPI_Wakeup;
pn532->log = PN532_Log;
Serial.begin(115200);
pinMode(PN532_REQ, OUTPUT);
pinMode(PN532_RST, OUTPUT);
pinMode(PN532_NSS, OUTPUT);
digitalWrite(PN532_REQ, HIGH);
digitalWrite(PN532_RST, HIGH);
digitalWrite(PN532_NSS, HIGH);
SPI.begin();
SPI.beginTransaction(SPISettings(10000, LSBFIRST, SPI_MODE0));
// hardware wakeup
pn532->wakeup();
}
/**************************************************************************
* End: SPI
**************************************************************************/
/**************************************************************************
* I2C
**************************************************************************/
void i2c_read(uint8_t* data, uint16_t count) {
Wire.requestFrom(PN532_I2C_ADDRESS, count);
for (uint16_t i = 0; i < count; i++) {
data[i] = Wire.read();
}
}
void i2c_write(uint8_t* data, uint16_t count) {
Wire.beginTransmission(PN532_I2C_ADDRESS);
Wire.write(data, count);
Wire.endTransmission();
}
int PN532_I2C_ReadData(uint8_t* data, uint16_t count) {
uint8_t status[] = {0x00};
uint8_t frame[count + 1];
i2c_read(status, sizeof(status));
if (status[0] != PN532_I2C_READY) {
return PN532_STATUS_ERROR;
}
i2c_read(frame, count + 1);
for (uint8_t i = 0; i < count; i++) {
data[i] = frame[i + 1];
}
return PN532_STATUS_OK;
}
int PN532_I2C_WriteData(uint8_t* data, uint16_t count) {
i2c_write(data, count);
return PN532_STATUS_OK;
}
bool PN532_I2C_WaitReady(uint32_t timeout) {
uint8_t status[] = {0x00};
uint32_t tickstart = millis();
while (millis() - tickstart < timeout) {
i2c_read(status, sizeof(status));
if (status[0] == PN532_I2C_READY) {
return true;
} else {
delay(5);
}
}
return false;
}
int PN532_I2C_Wakeup(void) {
digitalWrite(PN532_REQ, HIGH);
delay(100);
digitalWrite(PN532_REQ, LOW);
delay(100);
digitalWrite(PN532_REQ, HIGH);
delay(500);
return PN532_STATUS_OK;
}
void PN532_I2C_Init(PN532* pn532) {
// init the pn532 functions
pn532->reset = PN532_Reset;
pn532->read_data = PN532_I2C_ReadData;
pn532->write_data = PN532_I2C_WriteData;
pn532->wait_ready = PN532_I2C_WaitReady;
pn532->wakeup = PN532_I2C_Wakeup;
pn532->log = PN532_Log;
Serial.begin(115200);
pinMode(PN532_REQ, OUTPUT);
pinMode(PN532_RST, OUTPUT);
Wire.begin();
Wire.beginTransmission(PN532_I2C_ADDRESS);
// hardware reset
pn532->reset();
// hardware wakeup
pn532->wakeup();
}
/**************************************************************************
* End: I2C
**************************************************************************/