-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmfrc522.h
More file actions
276 lines (256 loc) · 10.7 KB
/
mfrc522.h
File metadata and controls
276 lines (256 loc) · 10.7 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
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
/**
* Mifare MFRC522 RFID Card reader
* It works on 13.56 MHz.
*
* This library uses SPI for driving MFRC255 chip.
*origin
* @author Tilen Majerle
* @email tilen@majerle.eu
* @website http://stm32f4-discovery.com
* @link http://stm32f4-discovery.com/2014/07/library-23-read-rfid-tag-mfrc522-stm32f4xx-devices/
* @version v1.0
* @ide Keil uVision
* @license GNU GPL v3
*this
* @author GiTetsu
* @mail gitetsu88@gmail.com
* |----------------------------------------------------------------------
* | Copyright (C) Tilen Majerle,Gitetsu 2014
* |
* | This program is free software: you can redistribute it and/or modify
* | it under the terms of the GNU General Public License as published by
* | the Free Software Foundation, either version 3 of the License, or
* | any later version.
* |
* | This program is distributed in the hope that it will be useful,
* | but WITHOUT ANY WARRANTY; without even the implied warranty of
* | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* | GNU General Public License for more details.
* |
* | You should have received a copy of the GNU General Public License
* | along with this program. If not, see <http://www.gnu.org/licenses/>.
* |----------------------------------------------------------------------
*/
#ifndef MFRC522_H
#define MFRC522_H 100
/**
* Library dependencies
* - STM32F4xx
* - STM32F4xx RCC
* - STM32F4xx GPIO
* - TM SPI
* - defines.h
*/
/**
* Includes
*/
#include <stdint.h>
/**
* Pinout
*
* Can be overwritten in defines.h file
*/
/**
* Status enumeration
*
* Used with most functions
*/
#define MI_OK (0)
#define MI_NOTAGERR (-1)
#define MI_ERR (-2)
typedef int MFRC522_Status_t;
#define MFRC522_CS_LOW MFRC522_CS_PORT->BSRRH = MFRC522_CS_PIN;
#define MFRC522_CS_HIGH MFRC522_CS_PORT->BSRRL = MFRC522_CS_PIN;
//MF522 Command word
#define PCD_IDLE 0x00 //NO action; Cancel the current command
#define PCD_AUTHENT 0x0E //Authentication Key
#define PCD_RECEIVE 0x08 //Receive Data
#define PCD_TRANSMIT 0x04 //Transmit data
#define PCD_TRANSCEIVE 0x0C //Transmit and receive data,
#define PCD_RESETPHASE 0x0F //Reset
#define PCD_CALCCRC 0x03 //CRC Calculate
// Mifare_One card command word
#define PICC_REQIDL 0x26 // find the antenna area does not enter hibernation
#define PICC_REQALL 0x52 // find all the cards antenna area
#define PICC_ANTICOLL 0x93 // anti-collision
#define PICC_SElECTTAG 0x93 // election card
#define PICC_AUTHENT1A 0x60 // authentication key A
#define PICC_AUTHENT1B 0x61 // authentication key B
#define PICC_READ 0x30 // Read Block
#define PICC_WRITE 0xA0 // write block
#define PICC_DECREMENT 0xC0 // debit
#define PICC_INCREMENT 0xC1 // recharge
#define PICC_RESTORE 0xC2 // transfer block data to the buffer
#define PICC_TRANSFER 0xB0 // save the data in the buffer
#define PICC_HALT 0x50 // Sleep
typedef enum PICC_Command {
// The commands used by the PCD to manage communication with several PICCs (ISO 14443-3, Type A, section 6.4)
PICC_CMD_REQA = 0x26, // REQuest command, Type A. Invites PICCs in state IDLE to go to READY and prepare for anticollision or selection. 7 bit frame.
PICC_CMD_WUPA = 0x52, // Wake-UP coE = 0xC2, // Reads thE = 0xC2, // Reads the contents of a block into the internal data register.e contents of a block into the internal data register.mmand, Type A. Invites PICCs in state IDLE and HALT to go to READY(*) and prepare for anticollision or selection. 7 bit frame.
PICC_CMD_CT = 0x88, // Cascade Tag. Not really a command, but used during anti collision.
PICC_CMD_SEL_CL1 = 0x93, // Anti collision/Select, Cascade Level 1
PICC_CMD_SEL_CL2 = 0x95, // Anti collision/Select, Cascade Level 1
PICC_CMD_SEL_CL3 = 0x97, // Anti collision/Select, Cascade Level 1
PICC_CMD_HALT = 0x50, // HaLT command, Type A. Instructs an ACTIVE PICC to go to state HALT.
// The commands used for MIFARE Classic (from http://www.nxp.com/documents/data_sheet/MF1S503x.pdf, Section 9)
// Use PCD_MFAuthent to authenticate access to a sector, then use these commands to read/write/modify the blocks on the sector.
// The read/write commands can also be used for MIFARE Ultralight.
PICC_CMD_MF_AUTH_KEY_A = 0x60, // Perform authentication with Key A
PICC_CMD_MF_AUTH_KEY_B = 0x61, // Perform authentication with Key B
PICC_CMD_MF_READ = 0x30, // Reads one 16 byte block from the authenticated sector of the PICC. Also used for MIFARE Ultralight.
PICC_CMD_MF_WRITE = 0xA0, // Writes one 16 byte block to the authenticated sector of the PICC. Called "COMPATIBILITY WRITE" for MIFARE Ultralight.
PICC_CMD_MF_DECREMENT = 0xC0, // Decrements the contents of a block and stores the result in the internal data register.
PICC_CMD_MF_INCREMENT = 0xC1, // Increments the contents of a block and stores the result in the internal data register.
PICC_CMD_MF_RESTORE = 0xC2, // Reads the contents of a block into the internal data register.
PICC_CMD_MF_TRANSFER = 0xB0, // Writes the contents of the internal data register to a block.
// The commands used for MIFARE Ultralight (from http://www.nxp.com/documents/data_sheet/MF0ICU1.pdf, Section 8.6)
// The PICC_CMD_MF_READ and PICC_CMD_MF_WRITE can also be used for MIFARE Ultralight.
PICC_CMD_UL_WRITE = 0xA2 // Writes one 4 byte page to the PICC.
}PICC_CMD_t;
typedef enum{
PICC_TYPE_NOT_COMPLETE = 0,
PICC_TYPE_MIFARE_MINI,
PICC_TYPE_MIFARE_1K,
PICC_TYPE_MIFARE_4K,
PICC_TYPE_MIFARE_UL,
PICC_TYPE_MIFARE_PLUS,
PICC_TYPE_TNP3XXX,
PICC_TYPE_ISO_14443_4,
PICC_TYPE_ISO_18092,
PICC_TYPE_UNKNOWN
} PICC_TYPE_t;
//MFRC Registers
//Page 0: Command and Status
#define MFRC522_REG_RESERVED00 0x00
#define MFRC522_REG_COMMAND 0x01
#define MFRC522_REG_COMM_IE_N 0x02
#define MFRC522_REG_DIV1_EN 0x03
#define MFRC522_REG_COMM_IRQ 0x04
#define MFRC522_REG_DIV_IRQ 0x05
#define MFRC522_REG_ERROR 0x06
#define MFRC522_REG_STATUS1 0x07
#define MFRC522_REG_STATUS2 0x08
#define MFRC522_REG_FIFO_DATA 0x09
#define MFRC522_REG_FIFO_LEVEL 0x0A
#define MFRC522_REG_WATER_LEVEL 0x0B
#define MFRC522_REG_CONTROL 0x0C
#define MFRC522_REG_BIT_FRAMING 0x0D
#define MFRC522_REG_COLL 0x0E
#define MFRC522_REG_RESERVED01 0x0F
//Page 1: Command
#define MFRC522_REG_RESERVED10 0x10
#define MFRC522_REG_MODE 0x11
#define MFRC522_REG_TX_MODE 0x12
#define MFRC522_REG_RX_MODE 0x13
#define MFRC522_REG_TX_CONTROL 0x14
#define MFRC522_REG_TX_AUTO 0x15
#define MFRC522_REG_TX_SELL 0x16
#define MFRC522_REG_RX_SELL 0x17
#define MFRC522_REG_RX_THRESHOLD 0x18
#define MFRC522_REG_DEMOD 0x19
#define MFRC522_REG_RESERVED11 0x1A
#define MFRC522_REG_RESERVED12 0x1B
#define MFRC522_REG_MIFARE 0x1C
#define MFRC522_REG_RESERVED13 0x1D
#define MFRC522_REG_RESERVED14 0x1E
#define MFRC522_REG_SERIALSPEED 0x1F
//Page 2: CFG
#define MFRC522_REG_RESERVED20 0x20
#define MFRC522_REG_CRC_RESULT_M 0x21
#define MFRC522_REG_CRC_RESULT_L 0x22
#define MFRC522_REG_RESERVED21 0x23
#define MFRC522_REG_MOD_WIDTH 0x24
#define MFRC522_REG_RESERVED22 0x25
#define MFRC522_REG_RF_CFG 0x26
#define MFRC522_REG_GS_N 0x27
#define MFRC522_REG_CWGS_PREG 0x28
#define MFRC522_REG__MODGS_PREG 0x29
#define MFRC522_REG_T_MODE 0x2A
#define MFRC522_REG_T_PRESCALER 0x2B
#define MFRC522_REG_T_RELOAD_H 0x2C
#define MFRC522_REG_T_RELOAD_L 0x2D
#define MFRC522_REG_T_COUNTER_VALUE_H 0x2E
#define MFRC522_REG_T_COUNTER_VALUE_L 0x2F
//Page 3:TestRegister
#define MFRC522_REG_RESERVED30 0x30
#define MFRC522_REG_TEST_SEL1 0x31
#define MFRC522_REG_TEST_SEL2 0x32
#define MFRC522_REG_TEST_PIN_EN 0x33
#define MFRC522_REG_TEST_PIN_VALUE 0x34
#define MFRC522_REG_TEST_BUS 0x35
#define MFRC522_REG_AUTO_TEST 0x36
#define MFRC522_REG_VERSION 0x37
#define MFRC522_REG_ANALOG_TEST 0x38
#define MFRC522_REG_TEST_ADC1 0x39
#define MFRC522_REG_TEST_ADC2 0x3A
#define MFRC522_REG_TEST_ADC0 0x3B
#define MFRC522_REG_RESERVED31 0x3C
#define MFRC522_REG_RESERVED32 0x3D
#define MFRC522_REG_RESERVED33 0x3E
#define MFRC522_REG_RESERVED34 0x3F
//Dummy byte
#define MFRC522_DUMMY 0x00
#define MFRC522_MAX_LEN 16
/**
* Public functions
*/
/**
* Initialize MFRC522 RFID reader
*
* Prepare MFRC522 to work with RFIDs
*
*/
extern int MFRC522_Init(char Type);
/**
* Check for RFID card existance
*
* Parameters:
* - uint8_t* id:
* Pointer to 5bytes long memory to store valid card id in.
* ID is valid only if card is detected, so when function returns MI_OK
*
* Returns MI_OK if card is detected
*/
extern MFRC522_Status_t MFRC522_Check(uint8_t* id);
/**
* Compare 2 RFID ID's
* Useful if you have known ID (database with allowed IDs), to compare detected card with with your ID
*
* Parameters:
* - uint8_t* CardID:
* Pointer to 5bytes detected card ID
* - uint8_t* CompareID:
* Pointer to 5bytes your ID
*
* Returns MI_OK if IDs are the same, or MI_ERR if not
*/
extern MFRC522_Status_t MFRC522_Compare(uint8_t* CardID, uint8_t* CompareID);
/**
* Private functions
*/
extern void MFRC522_WriteRegister(uint8_t addr, uint8_t val);
extern uint8_t MFRC522_ReadRegister(uint8_t addr);
extern void MFRC522_SetBitMask(uint8_t reg, uint8_t mask);
extern void MFRC522_ClearBitMask(uint8_t reg, uint8_t mask);
extern void MFRC522_AntennaOn(void);
extern void MFRC522_AntennaOff(void);
extern void MFRC522_Reset(void);
int MFRC522_Setup(char Type);
extern MFRC522_Status_t MFRC522_Request(uint8_t reqMode, uint8_t* TagType);
extern MFRC522_Status_t MFRC522_ToCard(uint8_t command, uint8_t* sendData, uint8_t sendLen, uint8_t* backData, uint16_t* backLen);
extern MFRC522_Status_t MFRC522_Anticoll(uint8_t* serNum);
extern void MFRC522_CalculateCRC(uint8_t* pIndata, uint8_t len, uint8_t* pOutData);
extern uint8_t MFRC522_SelectTag(uint8_t* serNum);
extern MFRC522_Status_t MFRC522_Auth(uint8_t authMode, uint8_t BlockAddr, uint8_t* Sectorkey, uint8_t* serNum);
extern MFRC522_Status_t MFRC522_Read(uint8_t blockAddr, uint8_t* recvData);
extern MFRC522_Status_t MFRC522_Write(uint8_t blockAddr, uint8_t* writeData);
extern void MFRC522_Halt(void);
char *MFRC522_TypeToString(PICC_TYPE_t type);
int MFRC522_ParseType(uint8_t TagSelectRet);
int MFRC522_Debug_DumpSector(uint8_t *CardID, uint8_t sector_addr);
int MFRC522_Debug_CardDump(uint8_t *CardID);
extern const char* __Reg_ToString[];
void MFRC522_Debug_RegDump(uint8_t Reg_Addr);
int MFRC522_Debug_Write(const char blockaddr, const char *Write_Data,
const int len);
#endif