├── LICENSE ├── MFRC522.cpp ├── MFRC522.h ├── README.md └── examples └── Basic-example.ino /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 zodier 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /MFRC522.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "MFRC522.h" 3 | 4 | //------------------MFRC522 register --------------- 5 | #define COMMAND_WAIT 0x02 6 | #define COMMAND_READBLOCK 0x03 7 | #define COMMAND_WRITEBLOCK 0x04 8 | #define MFRC522_HEADER 0xAB 9 | 10 | #define STATUS_ERROR 0 11 | #define STATUS_OK 1 12 | 13 | #define MIFARE_KEYA 0x00 14 | #define MIFARE_KEYB 0x01 15 | 16 | /** 17 | * Constructor. 18 | */ 19 | MFRC522::MFRC522() { 20 | _Serial = NULL; 21 | } 22 | 23 | /** 24 | * Description: Obtiene control del Serial para controlar MFRC522. 25 | * Ademas, pone MFRC522 en modo de espera. 26 | */ 27 | void MFRC522::begin(HardwareSerial *serial) { 28 | _Serial = serial; 29 | _Serial->begin(9600); 30 | wait(); 31 | } 32 | 33 | /** 34 | * Description:Pone MFRC522 en modo espera. 35 | */ 36 | void MFRC522::wait() { 37 | _Serial->write(COMMAND_WAIT); 38 | } 39 | 40 | /** 41 | * Description:Returns true if detect card in MFRC522. 42 | */ 43 | bool MFRC522::available() { 44 | return (_Serial->available() > 0); 45 | } 46 | 47 | /** 48 | * Description:Read the serial number of the card. 49 | */ 50 | void MFRC522::readCardSerial() { 51 | for (int i = 0; i < sizeof(cardSerial); ){ 52 | if (available()){ 53 | cardSerial[i] = read(); 54 | i++; 55 | } 56 | } 57 | } 58 | 59 | /** 60 | * Description:Returns a pointer to array with card serial. 61 | */ 62 | byte *MFRC522::getCardSerial() { 63 | return cardSerial; 64 | } 65 | 66 | /** 67 | * Description:Read a block data of the card. 68 | * Return:Return STATUS_OK if success. 69 | */ 70 | bool MFRC522::getBlock(byte block, byte keyType, byte *key, byte *returnBlock) { 71 | byte sendData[8] = { 72 | block, // block 73 | keyType, // key type 74 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF // key 75 | }; 76 | byte returnBlockLength; 77 | 78 | for (int i = 0; i < 6; ++i) { 79 | sendData[2 + i] = key[i]; 80 | } 81 | 82 | return communicate( 83 | COMMAND_READBLOCK, // command 84 | sendData, // sendData 85 | 0x0A, // length 86 | returnBlock, // returnData 87 | &returnBlockLength // returnDataLength 88 | ); 89 | } 90 | 91 | /** 92 | * Description:Write a block data in the card. 93 | * Return:Return STATUS_OK if success. 94 | */ 95 | bool MFRC522::writeBlock(byte block, byte keyType, byte *key, byte *data) { 96 | byte sendData[24] = { 97 | block, // block 98 | keyType, // key type 99 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // key 100 | 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Data 101 | }; 102 | byte returnBlock[3]; 103 | byte returnBlockLength; 104 | 105 | for (int i = 0; i < 6; ++i) { 106 | sendData[2 + i] = key[i]; 107 | } 108 | 109 | for (int i = 0; i < 16; ++i) { 110 | sendData[8 + i] = data[i]; 111 | } 112 | 113 | byte result = communicate( 114 | COMMAND_WRITEBLOCK, // command 115 | sendData, // sendData 116 | 0x1A, // length 117 | returnBlock, // returnData 118 | &returnBlockLength // returnDataLength 119 | ); 120 | 121 | return result; 122 | } 123 | 124 | /** 125 | * Description:Comunication between MFRC522 and ISO14443. 126 | * Return:Return STATUS_OK if success. 127 | */ 128 | bool MFRC522::communicate(byte command, byte *sendData, byte sendDataLength, byte *returnData, byte *returnDataLength) { 129 | // Send instruction to MFRC522 130 | write(MFRC522_HEADER); // Header 131 | write(sendDataLength); // Length (Length + Command + Data) 132 | write(command); // Command 133 | 134 | for (int i = 0; i < sendDataLength - 2; i++) { 135 | write(sendData[i]); // Data 136 | } 137 | 138 | // Read response to MFRC522 139 | while (!available()); 140 | byte header = read(); // Header 141 | while (!available()); 142 | *returnDataLength = read(); // Length (Length + Command + Data) 143 | while (!available()); 144 | byte commandResult = read(); // Command result 145 | 146 | for (int i = 0; i < *returnDataLength - 2; i=i) { 147 | if (available()) { 148 | returnData[i] = read(); // Data 149 | i++; 150 | } 151 | } 152 | 153 | // Return 154 | if (command != commandResult) { 155 | return STATUS_ERROR; 156 | } 157 | 158 | return STATUS_OK; 159 | } 160 | 161 | /* 162 | * Description:Write a byte data into MFRC522. 163 | */ 164 | void MFRC522::write(byte value) { 165 | _Serial->write(value); 166 | } 167 | 168 | /* 169 | * Description:Read a byte data of MFRC522 170 | * Return:Return the read value. 171 | */ 172 | byte MFRC522::read() { 173 | return _Serial->read(); 174 | } 175 | -------------------------------------------------------------------------------- /MFRC522.h: -------------------------------------------------------------------------------- 1 | class MFRC522 { 2 | 3 | public: 4 | MFRC522(); 5 | void begin(HardwareSerial *serial); 6 | bool available(); 7 | void wait(); 8 | void readCardSerial(); 9 | byte *getCardSerial(); 10 | bool getBlock(byte block, byte keyType, byte *key, byte *returnBlock); 11 | bool writeBlock(byte block, byte keyType, byte *key, byte *data); 12 | bool communicate(byte command, byte *sendData, byte sendDataLength, byte *returnData, byte *returnDataLength); 13 | void write(byte value); 14 | byte read(); 15 | 16 | private: 17 | HardwareSerial *_Serial; 18 | byte cardSerial[4]; 19 | 20 | }; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | MFRC522-UART-Arduino 2 | ====================== 3 | 4 | Arduino library for MFRC522 based modules via Serial UART interface. 5 | 6 | 7 | Methods 8 | ======= 9 | 10 | #### void begin(HardwareSerial *serial); 11 | 12 | #### bool available(); 13 | 14 | #### void wait(); 15 | 16 | #### void readCardSerial(); 17 | 18 | #### byte *getCardSerial(); 19 | 20 | #### bool getBlock(byte block, byte keyType, byte *key, byte *returnBlock); 21 | 22 | #### bool writeBlock(byte block, byte keyType, byte *key, byte *data); -------------------------------------------------------------------------------- /examples/Basic-example.ino: -------------------------------------------------------------------------------- 1 | // example.ino 2 | 3 | #include "MFRC522.h" 4 | 5 | MFRC522 Conector; 6 | 7 | void setup() { 8 | Conector.begin(&Serial); 9 | } 10 | 11 | void loop() { 12 | if (Conector.available()) { 13 | Conector.readCardSerial(); // Mandatory 14 | 15 | // Detected card at the reader! 16 | 17 | Conector.wait(); // Mandatory 18 | } 19 | } 20 | --------------------------------------------------------------------------------