-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathMFRC522.cpp
174 lines (149 loc) · 4.15 KB
/
MFRC522.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
#include <Arduino.h>
#include "MFRC522.h"
//------------------MFRC522 register ---------------
#define COMMAND_WAIT 0x02
#define COMMAND_READBLOCK 0x03
#define COMMAND_WRITEBLOCK 0x04
#define MFRC522_HEADER 0xAB
#define STATUS_ERROR 0
#define STATUS_OK 1
#define MIFARE_KEYA 0x00
#define MIFARE_KEYB 0x01
/**
* Constructor.
*/
MFRC522::MFRC522() {
_Serial = NULL;
}
/**
* Description: Obtiene control del Serial para controlar MFRC522.
* Ademas, pone MFRC522 en modo de espera.
*/
void MFRC522::begin(HardwareSerial *serial) {
_Serial = serial;
_Serial->begin(9600);
wait();
}
/**
* Description:Pone MFRC522 en modo espera.
*/
void MFRC522::wait() {
_Serial->write(COMMAND_WAIT);
}
/**
* Description:Returns true if detect card in MFRC522.
*/
bool MFRC522::available() {
return (_Serial->available() > 0);
}
/**
* Description:Read the serial number of the card.
*/
void MFRC522::readCardSerial() {
for (int i = 0; i < sizeof(cardSerial); ){
if (available()){
cardSerial[i] = read();
i++;
}
}
}
/**
* Description:Returns a pointer to array with card serial.
*/
byte *MFRC522::getCardSerial() {
return cardSerial;
}
/**
* Description:Read a block data of the card.
* Return:Return STATUS_OK if success.
*/
bool MFRC522::getBlock(byte block, byte keyType, byte *key, byte *returnBlock) {
byte sendData[8] = {
block, // block
keyType, // key type
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF // key
};
byte returnBlockLength;
for (int i = 0; i < 6; ++i) {
sendData[2 + i] = key[i];
}
return communicate(
COMMAND_READBLOCK, // command
sendData, // sendData
0x0A, // length
returnBlock, // returnData
&returnBlockLength // returnDataLength
);
}
/**
* Description:Write a block data in the card.
* Return:Return STATUS_OK if success.
*/
bool MFRC522::writeBlock(byte block, byte keyType, byte *key, byte *data) {
byte sendData[24] = {
block, // block
keyType, // key type
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // key
0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Data
};
byte returnBlock[3];
byte returnBlockLength;
for (int i = 0; i < 6; ++i) {
sendData[2 + i] = key[i];
}
for (int i = 0; i < 16; ++i) {
sendData[8 + i] = data[i];
}
byte result = communicate(
COMMAND_WRITEBLOCK, // command
sendData, // sendData
0x1A, // length
returnBlock, // returnData
&returnBlockLength // returnDataLength
);
return result;
}
/**
* Description:Comunication between MFRC522 and ISO14443.
* Return:Return STATUS_OK if success.
*/
bool MFRC522::communicate(byte command, byte *sendData, byte sendDataLength, byte *returnData, byte *returnDataLength) {
// Send instruction to MFRC522
write(MFRC522_HEADER); // Header
write(sendDataLength); // Length (Length + Command + Data)
write(command); // Command
for (int i = 0; i < sendDataLength - 2; i++) {
write(sendData[i]); // Data
}
// Read response to MFRC522
while (!available());
byte header = read(); // Header
while (!available());
*returnDataLength = read(); // Length (Length + Command + Data)
while (!available());
byte commandResult = read(); // Command result
for (int i = 0; i < *returnDataLength - 2; i=i) {
if (available()) {
returnData[i] = read(); // Data
i++;
}
}
// Return
if (command != commandResult) {
return STATUS_ERROR;
}
return STATUS_OK;
}
/*
* Description:Write a byte data into MFRC522.
*/
void MFRC522::write(byte value) {
_Serial->write(value);
}
/*
* Description:Read a byte data of MFRC522
* Return:Return the read value.
*/
byte MFRC522::read() {
return _Serial->read();
}