Bump to upstream version.

This commit is contained in:
Kiara Navarro
2015-12-24 09:45:27 -05:00
27 changed files with 2327 additions and 785 deletions

File diff suppressed because it is too large Load Diff

148
MFRC522.h
View File

@@ -3,6 +3,7 @@
* Based on code Dr.Leong ( WWW.B2CQSHOP.COM )
* Created by Miguel Balboa (circuitito.com), Jan, 2012.
* Rewritten by Søren Thing Andersen (access.thing.dk), fall of 2013 (Translation to English, refactored, comments, anti collision, cascade levels.)
* Extended by Tom Clement with functionality to write to sector 0 of UID changeable Mifare cards.
* Released into the public domain.
*
* Please read this file for an overview and then MFRC522.cpp for comments on the specific functions.
@@ -39,7 +40,7 @@
* Bytes 6-8: Access Bits
* Bytes 9: User data
* Bytes 10-15: Key B (or user data)
* Block 0 is read only manufacturer data.
* Block 0 is read-only manufacturer data.
* To access a block, an authentication using a key from the block's sector must be performed first.
* Example: To read from block 10, first authenticate using a key from sector 3 (blocks 8-11).
* All keys are set to FFFFFFFFFFFFh at chip delivery.
@@ -57,13 +58,13 @@
* MIFARE Ultralight (MF0ICU1):
* Has 16 pages of 4 bytes = 64 bytes.
* Pages 0 + 1 is used for the 7-byte UID.
* Page 2 contains the last chech digit for the UID, one byte manufacturer internal data, and the lock bytes (see http://www.nxp.com/documents/data_sheet/MF0ICU1.pdf section 8.5.2)
* Page 2 contains the last check digit for the UID, one byte manufacturer internal data, and the lock bytes (see http://www.nxp.com/documents/data_sheet/MF0ICU1.pdf section 8.5.2)
* Page 3 is OTP, One Time Programmable bits. Once set to 1 they cannot revert to 0.
* Pages 4-15 are read/write unless blocked by the lock bytes in page 2.
* MIFARE Ultralight C (MF0ICU2):
* Has 48 pages of 4 bytes = 64 bytes.
* Has 48 pages of 4 bytes = 192 bytes.
* Pages 0 + 1 is used for the 7-byte UID.
* Page 2 contains the last chech digit for the UID, one byte manufacturer internal data, and the lock bytes (see http://www.nxp.com/documents/data_sheet/MF0ICU1.pdf section 8.5.2)
* Page 2 contains the last check digit for the UID, one byte manufacturer internal data, and the lock bytes (see http://www.nxp.com/documents/data_sheet/MF0ICU1.pdf section 8.5.2)
* Page 3 is OTP, One Time Programmable bits. Once set to 1 they cannot revert to 0.
* Pages 4-39 are read/write unless blocked by the lock bytes in page 2.
* Page 40 Lock bytes
@@ -77,6 +78,59 @@
#include <Arduino.h>
#include <SPI.h>
// Firmware data for self-test
// Reference values based on firmware version
// Hint: if needed, you can remove unused self-test data to save flash memory
//
// Version 0.0 (0x90)
// Philips Semiconductors; Preliminary Specification Revision 2.0 - 01 August 2005; 16.1 Sefttest
const byte MFRC522_firmware_referenceV0_0[] PROGMEM = {
0x00, 0x87, 0x98, 0x0f, 0x49, 0xFF, 0x07, 0x19,
0xBF, 0x22, 0x30, 0x49, 0x59, 0x63, 0xAD, 0xCA,
0x7F, 0xE3, 0x4E, 0x03, 0x5C, 0x4E, 0x49, 0x50,
0x47, 0x9A, 0x37, 0x61, 0xE7, 0xE2, 0xC6, 0x2E,
0x75, 0x5A, 0xED, 0x04, 0x3D, 0x02, 0x4B, 0x78,
0x32, 0xFF, 0x58, 0x3B, 0x7C, 0xE9, 0x00, 0x94,
0xB4, 0x4A, 0x59, 0x5B, 0xFD, 0xC9, 0x29, 0xDF,
0x35, 0x96, 0x98, 0x9E, 0x4F, 0x30, 0x32, 0x8D
};
// Version 1.0 (0x91)
// NXP Semiconductors; Rev. 3.8 - 17 September 2014; 16.1.1 Self test
const byte MFRC522_firmware_referenceV1_0[] PROGMEM = {
0x00, 0xC6, 0x37, 0xD5, 0x32, 0xB7, 0x57, 0x5C,
0xC2, 0xD8, 0x7C, 0x4D, 0xD9, 0x70, 0xC7, 0x73,
0x10, 0xE6, 0xD2, 0xAA, 0x5E, 0xA1, 0x3E, 0x5A,
0x14, 0xAF, 0x30, 0x61, 0xC9, 0x70, 0xDB, 0x2E,
0x64, 0x22, 0x72, 0xB5, 0xBD, 0x65, 0xF4, 0xEC,
0x22, 0xBC, 0xD3, 0x72, 0x35, 0xCD, 0xAA, 0x41,
0x1F, 0xA7, 0xF3, 0x53, 0x14, 0xDE, 0x7E, 0x02,
0xD9, 0x0F, 0xB5, 0x5E, 0x25, 0x1D, 0x29, 0x79
};
// Version 2.0 (0x92)
// NXP Semiconductors; Rev. 3.8 - 17 September 2014; 16.1.1 Self test
const byte MFRC522_firmware_referenceV2_0[] PROGMEM = {
0x00, 0xEB, 0x66, 0xBA, 0x57, 0xBF, 0x23, 0x95,
0xD0, 0xE3, 0x0D, 0x3D, 0x27, 0x89, 0x5C, 0xDE,
0x9D, 0x3B, 0xA7, 0x00, 0x21, 0x5B, 0x89, 0x82,
0x51, 0x3A, 0xEB, 0x02, 0x0C, 0xA5, 0x00, 0x49,
0x7C, 0x84, 0x4D, 0xB3, 0xCC, 0xD2, 0x1B, 0x81,
0x5D, 0x48, 0x76, 0xD5, 0x71, 0x61, 0x21, 0xA9,
0x86, 0x96, 0x83, 0x38, 0xCF, 0x9D, 0x5B, 0x6D,
0xDC, 0x15, 0xBA, 0x3E, 0x7D, 0x95, 0x3B, 0x2F
};
// Clone
// Fudan Semiconductor FM17522 (0x88)
const byte FM17522_firmware_reference[] PROGMEM = {
0x00, 0xD6, 0x78, 0x8C, 0xE2, 0xAA, 0x0C, 0x18,
0x2A, 0xB8, 0x7A, 0x7F, 0xD3, 0x6A, 0xCF, 0x0B,
0xB1, 0x37, 0x63, 0x4B, 0x69, 0xAE, 0x91, 0xC7,
0xC3, 0x97, 0xAE, 0x77, 0xF4, 0x37, 0xD7, 0x9B,
0x7C, 0xF5, 0x3C, 0x11, 0x8F, 0x15, 0xC3, 0xD7,
0xC1, 0x5B, 0x00, 0x2A, 0xD0, 0x75, 0xDE, 0x9E,
0x51, 0x64, 0xAB, 0x3E, 0xE9, 0x15, 0xB5, 0xAB,
0x56, 0x9A, 0x98, 0x82, 0x26, 0xEA, 0x2A, 0x62
};
class MFRC522 {
public:
// MFRC522 registers. Described in chapter 9 of the datasheet.
@@ -100,7 +154,7 @@ public:
CollReg = 0x0E << 1, // bit position of the first bit-collision detected on the RF interface
// 0x0F // reserved for future use
// Page 1:Command
// Page 1: Command
// 0x10 // reserved for future use
ModeReg = 0x11 << 1, // defines general modes for transmitting and receiving
TxModeReg = 0x12 << 1, // defines transmission data rate and framing
@@ -119,7 +173,7 @@ public:
SerialSpeedReg = 0x1F << 1, // selects the speed of the serial UART interface
// Page 2: Configuration
// 0x20 // reserved for future use
// 0x20 // reserved for future use
CRCResultRegH = 0x21 << 1, // shows the MSB and LSB values of the CRC calculation
CRCResultRegL = 0x22 << 1,
// 0x23 // reserved for future use
@@ -136,7 +190,7 @@ public:
TCounterValueRegH = 0x2E << 1, // shows the 16-bit timer value
TCounterValueRegL = 0x2F << 1,
// Page 3:Test Registers
// Page 3: Test Registers
// 0x30 // reserved for future use
TestSel1Reg = 0x31 << 1, // general test signal configuration
TestSel2Reg = 0x32 << 1, // general test signal configuration
@@ -155,7 +209,7 @@ public:
// 0x3F // reserved for production tests
};
// MFRC522 comands. Described in chapter 10 of the datasheet.
// MFRC522 commands. Described in chapter 10 of the datasheet.
enum PCD_Command {
PCD_Idle = 0x00, // no action, cancels current command execution
PCD_Mem = 0x01, // stores 25 bytes into the internal buffer
@@ -169,6 +223,22 @@ public:
PCD_SoftReset = 0x0F // resets the MFRC522
};
// MFRC522 RxGain[2:0] masks, defines the receiver's signal voltage gain factor (on the PCD).
// Described in 9.3.3.6 / table 98 of the datasheet at http://www.nxp.com/documents/data_sheet/MFRC522.pdf
enum PCD_RxGain {
RxGain_18dB = 0x00 << 4, // 000b - 18 dB, minimum
RxGain_23dB = 0x01 << 4, // 001b - 23 dB
RxGain_18dB_2 = 0x02 << 4, // 010b - 18 dB, it seems 010b is a duplicate for 000b
RxGain_23dB_2 = 0x03 << 4, // 011b - 23 dB, it seems 011b is a duplicate for 001b
RxGain_33dB = 0x04 << 4, // 100b - 33 dB, average, and typical default
RxGain_38dB = 0x05 << 4, // 101b - 38 dB
RxGain_43dB = 0x06 << 4, // 110b - 43 dB
RxGain_48dB = 0x07 << 4, // 111b - 48 dB, maximum
RxGain_min = 0x00 << 4, // 000b - 18 dB, minimum, convenience for RxGain_18dB
RxGain_avg = 0x04 << 4, // 100b - 33 dB, average, convenience for RxGain_33dB
RxGain_max = 0x07 << 4 // 111b - 48 dB, maximum, convenience for RxGain_48dB
};
// Commands sent to the PICC.
enum PICC_Command {
// The commands used by the PCD to manage communication with several PICCs (ISO 14443-3, Type A, section 6.4)
@@ -176,8 +246,8 @@ public:
PICC_CMD_WUPA = 0x52, // Wake-UP command, 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_SEL_CL2 = 0x95, // Anti collision/Select, Cascade Level 2
PICC_CMD_SEL_CL3 = 0x97, // Anti collision/Select, Cascade Level 3
PICC_CMD_HLTA = 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.
@@ -249,6 +319,7 @@ public:
/////////////////////////////////////////////////////////////////////////////////////
// Functions for setting up the Arduino
/////////////////////////////////////////////////////////////////////////////////////
MFRC522();
MFRC522(byte chipSelectPin, byte resetPowerDownPin);
void setSPIConfig();
@@ -262,52 +333,65 @@ public:
void setBitMask(unsigned char reg, unsigned char mask);
void PCD_SetRegisterBitMask(byte reg, byte mask);
void PCD_ClearRegisterBitMask(byte reg, byte mask);
byte PCD_CalculateCRC(byte *data, byte length, byte *result);
MFRC522::StatusCode PCD_CalculateCRC(byte *data, byte length, byte *result);
/////////////////////////////////////////////////////////////////////////////////////
// Functions for manipulating the MFRC522
/////////////////////////////////////////////////////////////////////////////////////
void PCD_Init();
void PCD_Init(byte chipSelectPin, byte resetPowerDownPin);
void PCD_Reset();
void PCD_AntennaOn();
void PCD_AntennaOff();
byte PCD_GetAntennaGain();
void PCD_SetAntennaGain(byte mask);
bool PCD_PerformSelfTest();
/////////////////////////////////////////////////////////////////////////////////////
// Functions for communicating with PICCs
/////////////////////////////////////////////////////////////////////////////////////
byte PCD_TransceiveData(byte *sendData, byte sendLen, byte *backData, byte *backLen, byte *validBits = NULL, byte rxAlign = 0, bool checkCRC = false);
byte PCD_CommunicateWithPICC(byte command, byte waitIRq, byte *sendData, byte sendLen, byte *backData = NULL, byte *backLen = NULL, byte *validBits = NULL, byte rxAlign = 0, bool checkCRC = false);
byte PICC_RequestA(byte *bufferATQA, byte *bufferSize);
byte PICC_WakeupA(byte *bufferATQA, byte *bufferSize);
byte PICC_REQA_or_WUPA( byte command, byte *bufferATQA, byte *bufferSize);
byte PICC_Select(Uid *uid, byte validBits = 0);
byte PICC_HaltA();
MFRC522::StatusCode PCD_TransceiveData(byte *sendData, byte sendLen, byte *backData, byte *backLen, byte *validBits = NULL, byte rxAlign = 0, bool checkCRC = false);
MFRC522::StatusCode PCD_CommunicateWithPICC(byte command, byte waitIRq, byte *sendData, byte sendLen, byte *backData = NULL, byte *backLen = NULL, byte *validBits = NULL, byte rxAlign = 0, bool checkCRC = false);
MFRC522::StatusCode PICC_RequestA(byte *bufferATQA, byte *bufferSize);
MFRC522::StatusCode PICC_WakeupA(byte *bufferATQA, byte *bufferSize);
MFRC522::StatusCode PICC_REQA_or_WUPA(byte command, byte *bufferATQA, byte *bufferSize);
MFRC522::StatusCode PICC_Select(Uid *uid, byte validBits = 0);
MFRC522::StatusCode PICC_HaltA();
/////////////////////////////////////////////////////////////////////////////////////
// Functions for communicating with MIFARE PICCs
/////////////////////////////////////////////////////////////////////////////////////
byte PCD_Authenticate(byte command, byte blockAddr, MIFARE_Key *key, Uid *uid);
MFRC522::StatusCode PCD_Authenticate(byte command, byte blockAddr, MIFARE_Key *key, Uid *uid);
void PCD_StopCrypto1();
byte MIFARE_Read(byte blockAddr, byte *buffer, byte *bufferSize);
byte MIFARE_Write(byte blockAddr, byte *buffer, byte bufferSize);
byte MIFARE_Decrement(byte blockAddr, long delta);
byte MIFARE_Increment(byte blockAddr, long delta);
byte MIFARE_Restore(byte blockAddr);
byte MIFARE_Transfer(byte blockAddr);
byte MIFARE_Ultralight_Write(byte page, byte *buffer, byte bufferSize);
MFRC522::StatusCode MIFARE_Read(byte blockAddr, byte *buffer, byte *bufferSize);
MFRC522::StatusCode MIFARE_Write(byte blockAddr, byte *buffer, byte bufferSize);
MFRC522::StatusCode MIFARE_Ultralight_Write(byte page, byte *buffer, byte bufferSize);
MFRC522::StatusCode MIFARE_Decrement(byte blockAddr, long delta);
MFRC522::StatusCode MIFARE_Increment(byte blockAddr, long delta);
MFRC522::StatusCode MIFARE_Restore(byte blockAddr);
MFRC522::StatusCode MIFARE_Transfer(byte blockAddr);
MFRC522::StatusCode MIFARE_GetValue(byte blockAddr, long *value);
MFRC522::StatusCode MIFARE_SetValue(byte blockAddr, long value);
/////////////////////////////////////////////////////////////////////////////////////
// Support functions
/////////////////////////////////////////////////////////////////////////////////////
byte PCD_MIFARE_Transceive( byte *sendData, byte sendLen, bool acceptTimeout = false);
const char *GetStatusCodeName(byte code);
byte PICC_GetType(byte sak);
const char *PICC_GetTypeName(byte type);
MFRC522::StatusCode PCD_MIFARE_Transceive(byte *sendData, byte sendLen, bool acceptTimeout = false);
// old function used too much memory, now name moved to flash; if you need char, copy from flash to memory
//const char *GetStatusCodeName(byte code);
const __FlashStringHelper *GetStatusCodeName(MFRC522::StatusCode code);
MFRC522::PICC_Type PICC_GetType(byte sak);
// old function used too much memory, now name moved to flash; if you need char, copy from flash to memory
//const char *PICC_GetTypeName(byte type);
const __FlashStringHelper *PICC_GetTypeName(byte type);
void PICC_DumpToSerial(Uid *uid);
void PICC_DumpMifareClassicToSerial(Uid *uid, byte piccType, MIFARE_Key *key);
void PICC_DumpMifareClassicSectorToSerial(Uid *uid, MIFARE_Key *key, byte sector);
void PICC_DumpMifareUltralightToSerial();
void MIFARE_SetAccessBits(byte *accessBitBuffer, byte g0, byte g1, byte g2, byte g3);
bool MIFARE_OpenUidBackdoor(bool logErrors);
bool MIFARE_SetUid(byte *newUid, byte uidSize, bool logErrors);
bool MIFARE_UnbrickUidSector(bool logErrors);
/////////////////////////////////////////////////////////////////////////////////////
// Convenience functions - does not add extra functionality
@@ -318,7 +402,7 @@ public:
private:
byte _chipSelectPin; // Arduino pin connected to MFRC522's SPI slave select input (Pin 24, NSS, active low)
byte _resetPowerDownPin; // Arduino pin connected to MFRC522's reset and power down input (Pin 6, NRSTPD, active low)
byte MIFARE_TwoStepHelper(byte command, byte blockAddr, long data);
MFRC522::StatusCode MIFARE_TwoStepHelper(byte command, byte blockAddr, long data);
};
#endif

17
Makefile Normal file
View File

@@ -0,0 +1,17 @@
# Makefile for MFRC522 library
#
all: package
help:
@echo "Please use \`make <target>' where <target> is one of"
@echo " clean to clean the project (e.g. remove process files)"
@echo " package to package the library (into a zip file)"
clean:
rm ./MFRC522.zip
@echo
@echo "Clean finished."
package:
zip -o ./MFRC522.zip ./MFRC522.h ./MFRC522.cpp

View File

@@ -1,41 +0,0 @@
rduino RFID Library for MFRC522
=======================================================
This library is use to read RFID Cards through MIFARE RC522 reader using SPI interface on Arduino Board.
Pin Layout for Arduino UNO
------------
* MOSI: Pin 11 / ICSP-4
* MISO: Pin 12 / ICSP-1
* SCK : Pin 13 / ISCP-3
* SS : Pin 10 (Configurable)
* RST : Pin 9 (Configurable)
Pin Layout for Arduino Mega
------------
* MOSI: Pin 51 / ICSP-4
* MISO: Pin 50 / ICSP-1
* SCK : Pin 52 / ISCP-3
* SS : Pin 53 (Configurable)
* RST : Pin 5 (Configurable)
Installation
------------
Clone the repository with:
```bash
git clone https://github.com/sophiekovalevsky/rfid.git
```
And place the repo into the your arduino libraries folder.
Credits
-----
This library was created by [Miguel Balboa.][1]
More comming soon.
-----
We need to document a lot.
[1]: https://github.com/miguelbalboa

179
README.rst Normal file
View File

@@ -0,0 +1,179 @@
MFRC522
=======
Arduino library for MFRC522 and other RFID RC522 based modules.
Read and write different types of Radio-Frequency IDentification (RFID) cards
on your Arduino using a RC522 based reader connected via the Serial Peripheral
Interface (SPI) interface.
Set the UID, write to sector 0, and unbrick Chinese UID changeable MIFARE cards.
.. _pin layout:
Pin Layout
----------
The following table shows the typical pin layout used:
+-----------+----------+-------------------------------------------------------------+--------+
| | PCD | Arduino | Teensy |
| +----------+-------------+---------+---------+---------------+-----------+--------+
| | MFRC522 | Uno | Mega | Nano v3 |Leonardo/Micro | Pro Micro | 3.1 |
+-----------+----------+-------------+---------+---------+---------------+-----------+--------+
| Signal | Pin | Pin | Pin | Pin | Pin | Pin | Pin |
+===========+==========+=============+=========+=========+===============+===========+========+
| RST/Reset | RST | 9 [1]_ | 5 [1]_ | D9 | RESET/ICSP-5 | RST | 9 |
+-----------+----------+-------------+---------+---------+---------------+-----------+--------+
| SPI SS | SDA [3]_ | 10 [2]_ | 53 [2]_ | D10 | 10 | 10 | 10 |
+-----------+----------+-------------+---------+---------+---------------+-----------+--------+
| SPI MOSI | MOSI | 11 / ICSP-4 | 51 | D11 | ICSP-4 | 16 | 11 |
+-----------+----------+-------------+---------+---------+---------------+-----------+--------+
| SPI MISO | MISO | 12 / ICSP-1 | 50 | D12 | ICSP-1 | 14 | 12 |
+-----------+----------+-------------+---------+---------+---------------+-----------+--------+
| SPI SCK | SCK | 13 / ICSP-3 | 52 | D13 | ICSP-3 | 15 | 13 |
+-----------+----------+-------------+---------+---------+---------------+-----------+--------+
.. [1] Configurable, typically defined as RST_PIN in sketch/program.
.. [2] Configurable, typically defined as SS_PIN in sketch/program.
.. [3] The SDA pin might be labeled SS on some/older MFRC522 boards.
Hardware
--------
There are three hardware components involved:
1. **Micro Controller**:
* An `Arduino`_ or compatible executing the Sketch using this library.
* Prices vary from USD 7 for clones, to USD 75 for "starter kits" (which
might be a good choice if this is your first exposure to Arduino;
check if such kit already includes the Arduino, Reader, and some Tags).
2. **Proximity Coupling Device (PCD)**:
* The PCD is the actual RFID **Reader** based on `NXP MFRC522`_ Contactless
Reader Integrated Circuit).
* Readers can be found on `eBay`_ for around USD 5: search for *"rc522"*.
* You can also find them at several web stores, they are often included in
*"starter kits"*; so check your favourite electronics provider as well.
3. **Proximity Integrated Circuit Card (PICC)**:
* The PICC is the RFID **Card** or **Tag** using the `ISO/IEC 14443A`_
interface, for example Mifare or NTAG203.
* One or two might be included with the Reader or *"starter kit"* already.
Protocols
---------
1. The micro controller and the reader use SPI for communication.
* The protocol is described in the `NXP MFRC522`_ datasheet.
* See the `Pin Layout`_ section for details on connecting the pins.
2. The reader and the tags communicate using a 13.56 MHz electromagnetic field.
* The protocol is defined in ISO/IEC 14443-3:2011 Part 3 Type A.
* Details are found in chapter 6 *"Type A Initialization and anticollision"*.
* See http://wg8.de/wg8n1496_17n3613_Ballot_FCD14443-3.pdf for a free version
of the final draft (which might be outdated in some areas).
* The reader do not support ISO/IEC 14443-3 Type B.
Troubleshooting
-------
* **I don't get input from reader** or **WARNING: Communication failure, is the MFRC522 properly connected?**
#. Check your connection, see `Pin Layout`_ .
#. Check voltage. Most breakouts work with 3.3V.
#. The SPI only works with 3.3V, most breakouts seems 5V tollerant, but try a level shifter.
* **Sometimes I get timeouts** or **tag/card sometimes not work.**
#. Try other site of the antenna.
#. Try to decrease distance between MFRC522.
#. Increase antenna gain per firmware: ``mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);``
#. Use better power supply.
#. Hardware corrupted, most products are from china and sometimes the quality is really low. Contact your seller.
* **My tag/card doesn't work.**
#. Distance between antenna and token too huge (>1cm).
#. You got wrong PICC. Is it really 13.56 MHz? Is it really a Mifare Type A?
#. NFC tokens are not supported. Some may work.
#. Animal marker are not supported. They use other frequency.
#. Hardware corrupted, most products are from china and sometimes the quality is really low. Contact your seller.
* **My mobile phone doesn't recognize the MFRC522** or **my MFRC522 can't read data from other MFRC522**
#. Card simmulation is not supported.
#. Communication with mobile phones is not supported.
#. Peer to peer communication is not supported.
* **I need more features.**
#. If software: code it and make a pull request.
#. If hardware: buy a more expensive like PN532 (supports NFC and many more, but costs about $15)
License
-------
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS 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.
For more information, please refer to http://unlicense.org/
History
-------
The MFRC522 library was first created in Jan 2012 by Miguel Balboa (from
http://circuitito.com) based on code by Dr. Leong (from http://B2CQSHOP.com)
for *"Arduino RFID module Kit 13.56 Mhz with Tags SPI W and R By COOQRobot"*.
It was translated into English and rewritten/refactored in the fall of 2013
by Søren Thing Andersen (from http://access.thing.dk).
It has been extended with functionality to alter sector 0 on Chinese UID changeable MIFARE card in Oct 2014 by Tom Clement (from http://tomclement.nl).
.. _arduino: http://arduino.cc/
.. _ebay: http://www.ebay.com/
.. _iso/iec 14443a: http://en.wikipedia.org/wiki/ISO/IEC_14443
.. _iso/iec 14443-3\:2011 part 3:
.. _nxp mfrc522: http://www.nxp.com/documents/data_sheet/MFRC522.pdf

View File

@@ -1,6 +0,0 @@
TODO:
Create Examples:
- Read more stuff than the serial number
- Write to a card
- Do some ethernet stuff (with an ethernet module)

24
UNLICENSE Normal file
View File

@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS 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.
For more information, please refer to <http://unlicense.org/>

View File

@@ -1,9 +1,57 @@
Renamed library from RFID to MFRC522 (RFID seemed to generic).
Register names changed to comply with datasheet.
Global defines moved into class.
All constants, functions and parameters are now commented in English.
Code refactored, most function names have changed.
Now supports ISO-14443-3 anti collission and 4/7/10 byte UIDs (cascade levels).
Added functions for MIFARE Classic Decrement/Increment/Restore/Transfer and MIFARE Ultralight Write.
New examples written.
5 Dec 2015
- recognize infineon cards correctly @mayatforest
- added multi reader support, see example @lmmeng
- functions return MFRC522::StatusCode instead of generic byte @rotzbua
10 Nov 2014
- Updated the changelog.
- Added makefile.
24 Oct 2014
- Added PlatformIO-based manifest file.
17 Jul 2014
- Written documentation for the library.
- Added rfid_default_keys example.
11 Jun 2014
- Updated example: ReadAndWrite.
14 Apr 2014
- Updated examples: DumpInfo, MifareClassicValueBlock, and ReadAndWrite.
12 Feb 2014
- Fixed resetPowerDownPin initial state.
29 Jan 2014
- Fixed chipSelectPin initial state.
30 Nov 2013
- Examples put in their own folders.
- Updated the keywords.txt file.
12 Nov 2013
- Updated examples: DumpInfo, MifareClassicValueBlock, and ReadAndWrite.
20 Oct 2013
- All constants, functions and parameters are now commented in English.
- Code refactored, most function names have changed.
- Support ISO-14443-3 anti collission and 4/7/10 byte UIDs (cascade levels).
- Added functions for MIFARE Classic Decrement/Increment/Restore/Transfer
and MIFARE Ultralight Write.
- New examples written.
19 Oct 2013
- Renamed library from RFID to MFRC522 (RFID seemed to generic).
- Register names changed to comply with datasheet.
- Global defines moved into class.
24 Sep 2013
- Turn off encryption when tag is halted.
27 Jan 2013
- Added README and small TODO list.
- Added example to show Serial on LCD display.
09 Sep 2012
- Initial commit to GitHub.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 282 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 162 KiB

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

BIN
doc/rfidmifare.doc Normal file

Binary file not shown.

BIN
doc/rfidmifare.pdf Normal file

Binary file not shown.

View File

@@ -0,0 +1,124 @@
/*
* MFRC522 - Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI W AND R BY COOQROBOT.
* The library file MFRC522.h has a wealth of useful info. Please read it.
* The functions are documented in MFRC522.cpp.
*
* Based on code Dr.Leong ( WWW.B2CQSHOP.COM )
* Created by Miguel Balboa (circuitito.com), Jan, 2012.
* Rewritten by Søren Thing Andersen (access.thing.dk), fall of 2013 (Translation to English, refactored, comments, anti collision, cascade levels.)
* Extended by Tom Clement with functionality to write to sector 0 of UID changeable Mifare cards.
*
* Released into the public domain.
*
* This sample shows how to set the UID on a UID changeable MIFARE card.
*
----------------------------------------------------------------------------- empty_skull
- Aggiunti pin per arduino Mega
- Scritto semplice codice per la scrittura e lettura
- add pin configuration for arduino mega
- write simple read/write Code for new entry user
http://mac86project.altervista.org/
----------------------------------------------------------------------------- Nicola Coppola
* Typical pin layout used:
* -----------------------------------------------------------------------------------------
* MFRC522 Arduino Arduino Arduino Arduino Arduino
* Reader/PCD Uno Mega Nano v3 Leonardo/Micro Pro Micro
* Signal Pin Pin Pin Pin Pin Pin
* -----------------------------------------------------------------------------------------
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
* SPI SS SDA(SS) 10 53 D10 10 10
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*
* The reader can be found on eBay for around 5 dollars. Search for "mf-rc522" on ebay.com.
*/
#include <SPI.h>
#include <MFRC522.h>
/* Set your new UID here! */
#define NEW_UID {0xDE, 0xAD, 0xBE, 0xEF}
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
MFRC522::MIFARE_Key key;
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println(F("Warning: this example overwrites the UID of your UID changeable card, use with care!"));
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
}
// Setting the UID can be as simple as this:
//void loop() {
// byte newUid[] = NEW_UID;
// if ( mfrc522.MIFARE_SetUid(newUid, (byte)4, true) ) {
// Serial.println("Wrote new UID to card.");
// }
// delay(1000);
//}
// But of course this is a more proper approach
void loop() {
// Look for new cards, and select one if present
if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
delay(50);
return;
}
// Now a card is selected. The UID and SAK is in mfrc522.uid.
// Dump UID
Serial.print(F("Card UID:"));
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.println();
// Dump PICC type
// byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
// Serial.print(F("PICC type: "));
// Serial.print(mfrc522.PICC_GetTypeName(piccType));
// Serial.print(F(" (SAK "));
// Serial.print(mfrc522.uid.sak);
// Serial.print(")\r\n");
// if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
// && piccType != MFRC522::PICC_TYPE_MIFARE_1K
// && piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
// Serial.println(F("This sample only works with MIFARE Classic cards."));
// return;
// }
// Set new UID
byte newUid[] = NEW_UID;
if ( mfrc522.MIFARE_SetUid(newUid, (byte)4, true) ) {
Serial.println(F("Wrote new UID to card."));
}
// Halt PICC and re-select it so DumpToSerial doesn't get confused
mfrc522.PICC_HaltA();
if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
return;
}
// Dump the new memory contents
Serial.println(F("New UID and contents:"));
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
delay(2000);
}

View File

@@ -1,44 +1,58 @@
/*
* MFRC522 - Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI W AND R BY COOQROBOT.
* The library file MFRC522.h has a wealth of useful info. Please read it.
* The functions are documented in MFRC522.cpp.
* ----------------------------------------------------------------------------
* This is a MFRC522 library example; see https://github.com/miguelbalboa/rfid
* for further details and other examples.
*
* NOTE: The library file MFRC522.h has a lot of useful info. Please read it.
*
* Based on code Dr.Leong ( WWW.B2CQSHOP.COM )
* Created by Miguel Balboa (circuitito.com), Jan, 2012.
* Rewritten by Søren Thing Andersen (access.thing.dk), fall of 2013 (Translation to English, refactored, comments, anti collision, cascade levels.)
* Released into the public domain.
* ----------------------------------------------------------------------------
* Example sketch/program showing how to read data from a PICC (that is: a RFID
* Tag or Card) using a MFRC522 based RFID Reader on the Arduino SPI interface.
*
* Sample program showing how to read data from a PICC using a MFRC522 reader on the Arduino SPI interface.
*----------------------------------------------------------------------------- empty_skull
* Aggiunti pin per arduino Mega
* add pin configuration for arduino mega
* http://mac86project.altervista.org/
----------------------------------------------------------------------------- Nicola Coppola
* Pin layout should be as follows:
* Signal Pin Pin Pin
* Arduino Uno Arduino Mega MFRC522 board
* ------------------------------------------------------------
* Reset 9 5 RST
* SPI SS 10 53 SDA
* SPI MOSI 11 51 MOSI
* SPI MISO 12 50 MISO
* SPI SCK 13 52 SCK
* When the Arduino and the MFRC522 module are connected (see the pin layout
* below), load this sketch into Arduino IDE then verify/compile and upload it.
* To see the output: use Tools, Serial Monitor of the IDE (hit Ctrl+Shft+M).
* When you present a PICC (that is: a RFID Tag or Card) at reading distance
* of the MFRC522 Reader/PCD, the serial output will show the ID/UID, type and
* any data blocks it can read. Note: you may see "Timeout in communication"
* messages when removing the PICC from reading distance too early.
*
* The reader can be found on eBay for around 5 dollars. Search for "mf-rc522" on ebay.com.
* If your reader supports it, this sketch/program will read all the PICCs
* presented (that is: multiple tag reading). So if you stack two or more
* PICCs on top of each other and present them to the reader, it will first
* output all details of the first and then the next PICC. Note that this
* may take some time as all data blocks are dumped, so keep the PICCs at
* reading distance until complete.
*
* Typical pin layout used:
* -----------------------------------------------------------------------------------------
* MFRC522 Arduino Arduino Arduino Arduino Arduino
* Reader/PCD Uno Mega Nano v3 Leonardo/Micro Pro Micro
* Signal Pin Pin Pin Pin Pin Pin
* -----------------------------------------------------------------------------------------
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
* SPI SS SDA(SS) 10 53 D10 10 10
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*/
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
#define RST_PIN 9 //
#define SS_PIN 10 //
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println("Scan PICC to see UID and type...");
mfrc522.PCD_Init(); // Init MFRC522
ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details
Serial.println(F("Scan PICC to see UID, type, and data blocks..."));
}
void loop() {
@@ -52,6 +66,24 @@ void loop() {
return;
}
// Dump debug info about the card. PICC_HaltA() is automatically called.
// Dump debug info about the card; PICC_HaltA() is automatically called
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
}
void ShowReaderDetails() {
// Get the MFRC522 software version
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
Serial.print(F("MFRC522 Software Version: 0x"));
Serial.print(v, HEX);
if (v == 0x91)
Serial.print(F(" = v1.0"));
else if (v == 0x92)
Serial.print(F(" = v2.0"));
else
Serial.print(F(" (unknown)"));
Serial.println("");
// When 0x00 or 0xFF is returned, communication probably failed
if ((v == 0x00) || (v == 0xFF)) {
Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
}
}

View File

@@ -0,0 +1,68 @@
/*
* MFRC522 - Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI W AND R BY COOQROBOT.
* The library file MFRC522.h has a wealth of useful info. Please read it.
* The functions are documented in MFRC522.cpp.
*
* Based on code Dr.Leong ( WWW.B2CQSHOP.COM )
* Created by Miguel Balboa (circuitito.com), Jan, 2012.
* Rewritten by Søren Thing Andersen (access.thing.dk), fall of 2013 (Translation to English, refactored, comments, anti collision, cascade levels.)
* Extended by Tom Clement with functionality to write to sector 0 of UID changeable Mifare cards.
*
* Released into the public domain.
*
* This sample shows how to fix a UID changeable MIFARE cards that have a corrupted sector 0.
*
----------------------------------------------------------------------------- empty_skull
- Aggiunti pin per arduino Mega
- Scritto semplice codice per la scrittura e lettura
- add pin configuration for arduino mega
- write simple read/write Code for new entry user
http://mac86project.altervista.org/
----------------------------------------------------------------------------- Nicola Coppola
* Typical pin layout used:
* -----------------------------------------------------------------------------------------
* MFRC522 Arduino Arduino Arduino Arduino Arduino
* Reader/PCD Uno Mega Nano v3 Leonardo/Micro Pro Micro
* Signal Pin Pin Pin Pin Pin Pin
* -----------------------------------------------------------------------------------------
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
* SPI SS SDA(SS) 10 53 D10 10 10
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*
* The reader can be found on eBay for around 5 dollars. Search for "mf-rc522" on ebay.com.
*/
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
MFRC522::MIFARE_Key key;
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println(F("Warning: this example clears your mifare UID, use with care!"));
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
}
void loop() {
if ( mfrc522.MIFARE_UnbrickUidSector(false) ) {
Serial.println(F("Cleared sector 0, set UID to 1234. Card should be responsive again now."));
}
delay(1000);
}

View File

@@ -1,202 +1,319 @@
/*
* MFRC522 - Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI W AND R BY COOQROBOT.
* The library file MFRC522.h has a wealth of useful info. Please read it.
* The functions are documented in MFRC522.cpp.
/**
* ----------------------------------------------------------------------------
* This is a MFRC522 library example; see https://github.com/miguelbalboa/rfid
* for further details and other examples.
*
* NOTE: The library file MFRC522.h has a lot of useful info. Please read it.
*
* Based on code Dr.Leong ( WWW.B2CQSHOP.COM )
* Created by Miguel Balboa (circuitito.com), Jan, 2012.
* Rewritten by Søren Thing Andersen (access.thing.dk), fall of 2013 (Translation to English, refactored, comments, anti collision, cascade levels.)
* Released into the public domain.
* ----------------------------------------------------------------------------
* This sample shows how to setup blocks on a MIFARE Classic PICC (= card/tag)
* to be in "Value Block" mode: in this mode the operations Increment/Decrement,
* Restore and Transfer can be used.
*
* This sample shows how to setup a block on a MIFARE Classic PICC to be in "Value Block" mode.
* In Value Block mode the operations Increment/Decrement/Restore and Transfer can be used.
* BEWARE: Data will be written to the PICC, in sector #1 (blocks #4 to #7).
*
----------------------------------------------------------------------------- empty_skull
* Aggiunti pin per arduino Mega
* add pin configuration for arduino mega
* http://mac86project.altervista.org/
----------------------------------------------------------------------------- Nicola Coppola
* Pin layout should be as follows:
* Signal Pin Pin Pin
* Arduino Uno Arduino Mega MFRC522 board
* ------------------------------------------------------------
* Reset 9 5 RST
* SPI SS 10 53 SDA
* SPI MOSI 11 51 MOSI
* SPI MISO 12 50 MISO
* SPI SCK 13 52 SCK
*
* The reader can be found on eBay for around 5 dollars. Search for "mf-rc522" on ebay.com.
* Typical pin layout used:
* -----------------------------------------------------------------------------------------
* MFRC522 Arduino Arduino Arduino Arduino Arduino
* Reader/PCD Uno Mega Nano v3 Leonardo/Micro Pro Micro
* Signal Pin Pin Pin Pin Pin Pin
* -----------------------------------------------------------------------------------------
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
* SPI SS SDA(SS) 10 53 D10 10 10
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*
*/
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
MFRC522::MIFARE_Key key;
/**
* Initialize.
*/
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println("Scan a MIFARE Classic PICC to demonstrate Value Blocks.");
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
// Prepare the key (used both as key A and as key B)
// using FFFFFFFFFFFFh which is the default at chip delivery from the factory
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
Serial.println(F("Scan a MIFARE Classic PICC to demonstrate Value Block mode."));
Serial.print(F("Using key (for A and B):"));
dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
Serial.println();
Serial.println(F("BEWARE: Data will be written to the PICC, in sector #1"));
}
/**
* Main loop.
*/
void loop() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
return;
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
// Now a card is selected. The UID and SAK is in mfrc522.uid.
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
return;
// Dump UID
Serial.print("Card UID:");
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.println();
// Show some details of the PICC (that is: the tag/card)
Serial.print(F("Card UID:"));
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println();
Serial.print(F("PICC type: "));
byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));
// Dump PICC type
byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.print("PICC type: ");
Serial.println(mfrc522.PICC_GetTypeName(piccType));
if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
&& piccType != MFRC522::PICC_TYPE_MIFARE_1K
&& piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println("This sample only works with MIFARE Classic cards.");
return;
}
// Check for compatibility
if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
&& piccType != MFRC522::PICC_TYPE_MIFARE_1K
&& piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println(F("This sample only works with MIFARE Classic cards."));
return;
}
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
// In this sample we use the second sector,
// that is: sector #1, covering block #4 up to and including block #7
byte sector = 1;
byte valueBlockA = 5;
byte valueBlockB = 6;
byte trailerBlock = 7;
MFRC522::StatusCode status;
byte buffer[18];
byte size = sizeof(buffer);
long value;
// In this sample we use the second sector (ie block 4-7).
byte sector = 1;
byte valueBlockA = 5;
byte valueBlockB = 6;
byte trailerBlock = 7;
// Authenticate using key A
Serial.println(F("Authenticating using key A..."));
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
// Authenticate using key A.
Serial.println("Authenticating using key A...");
byte status;
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print("PCD_Authenticate() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
// Show the whole sector as it currently is
Serial.println(F("Current data in sector:"));
mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
Serial.println();
// We need a sector trailer that defines blocks 5 and 6 as Value Blocks and enables key B.
byte trailerBuffer[] = { 255,255,255,255,255,255, 0,0,0, 0, 255,255,255,255,255,255}; // Keep default keys.
// g1=6 => Set block 5 as value block. Must use Key B towrite & increment, A or B can be used for derement.
// g2=6 => Same thing for block 6.
// g3=3 => Key B must be used to modify the Sector Trailer. Key B becomes valid.
mfrc522.MIFARE_SetAccessBits(&trailerBuffer[6], 0, 6, 6, 3);
// We need a sector trailer that defines blocks 5 and 6 as Value Blocks and enables key B
// The last block in a sector (block #3 for Mifare Classic 1K) is the Sector Trailer.
// See http://www.nxp.com/documents/data_sheet/MF1S503x.pdf sections 8.6 and 8.7:
// Bytes 0-5: Key A
// Bytes 6-8: Access Bits
// Bytes 9: User data
// Bytes 10-15: Key B (or user data)
byte trailerBuffer[] = {
255, 255, 255, 255, 255, 255, // Keep default key A
0, 0, 0,
0,
255, 255, 255, 255, 255, 255}; // Keep default key B
// The access bits are stored in a peculiar fashion.
// There are four groups:
// g[0] Access bits for block 0 (for sectors 0-31)
// or blocks 0-4 (for sectors 32-39)
// g[1] Access bits for block 1 (for sectors 0-31)
// or blocks 5-9 (for sectors 32-39)
// g[2] Access bits for block 2 (for sectors 0-31)
// or blocks 10-14 (for sectors 32-39)
// g[3] Access bits for the Sector Trailer: block 3 (for sectors 0-31)
// or block 15 (for sectors 32-39)
// Each group has access bits [C1 C2 C3], in this code C1 is MSB and C3 is LSB.
// Determine the bit pattern needed using MIFARE_SetAccessBits:
// g0=0 access bits for block 0 (of this sector) using [0 0 0] = 000b = 0
// which means key A|B have r/w for block 0 of this sector
// which (in this example) translates to block #4 within sector #1;
// this is the transport configuration (at factory delivery).
// g1=6 access bits for block 1 (of this sector) using [1 1 0] = 110b = 6
// which means block 1 (of this sector) is used as a value block,
// which (in this example) translates to block #5 within sector #1;
// where key A|B have r, key B has w, key B can increment,
// and key A|B can decrement, transfer, and restore.
// g2=6 same thing for block 2 (of this sector): set it to a value block;
// which (in this example) translates to block #6 within sector #1;
// g3=3 access bits for block 3 (of this sector): the Sector Trailer here;
// using [0 1 1] = 011b = 3 which means only key B has r/w access
// to the Sector Trailer (block 3 of this sector) from now on
// which (in this example) translates to block #7 within sector #1;
mfrc522.MIFARE_SetAccessBits(&trailerBuffer[6], 0, 6, 6, 3);
// Now we read the sector trailer and see if it is like we want it to be.
Serial.println("Reading sector trailer...");
byte buffer[18];
byte size = sizeof(buffer);
status = mfrc522.MIFARE_Read(trailerBlock, buffer, &size);
if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Read() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
if ( buffer[6] != trailerBuffer[6]
&& buffer[7] != trailerBuffer[7]
&& buffer[8] != trailerBuffer[8]) {
Serial.println("Writing new sector trailer...");
status = mfrc522.MIFARE_Write(trailerBlock, trailerBuffer, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Write() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
}
// Read the sector trailer as it is currently stored on the PICC
Serial.println(F("Reading sector trailer..."));
status = mfrc522.MIFARE_Read(trailerBlock, buffer, &size);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Read() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
// Check if it matches the desired access pattern already;
// because if it does, we don't need to write it again...
if ( buffer[6] != trailerBuffer[6]
&& buffer[7] != trailerBuffer[7]
&& buffer[8] != trailerBuffer[8]) {
// They don't match (yet), so write it to the PICC
Serial.println(F("Writing new sector trailer..."));
status = mfrc522.MIFARE_Write(trailerBlock, trailerBuffer, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
}
// Authenticate using key B.
Serial.println("Authenticating again using key B...");
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print("PCD_Authenticate() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
// Authenticate using key B
Serial.println(F("Authenticating again using key B..."));
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
// Value blocks has a 32 bit signed value stored three times and an 8 bit address stored 4 times.
// Make sure blocks valueBlockA and valueBlockB has that format.
formatBlock(valueBlockA);
formatBlock(valueBlockB);
// A value block has a 32 bit signed value stored three times
// and an 8 bit address stored 4 times. Make sure that valueBlockA
// and valueBlockB have that format (note that it will only format
// the block when it doesn't comply to the expected format already).
formatValueBlock(valueBlockA);
formatValueBlock(valueBlockB);
// Add 1 to the value of valueBlockA and store the result in valueBlockA.
Serial.print("Adding 1 to value of block "); Serial.println(valueBlockA);
status = mfrc522.MIFARE_Increment(valueBlockA, 1);
if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Increment() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
status = mfrc522.MIFARE_Transfer(valueBlockA);
if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Transfer() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
// Add 1 to the value of valueBlockA and store the result in valueBlockA.
Serial.print("Adding 1 to value of block "); Serial.println(valueBlockA);
status = mfrc522.MIFARE_Increment(valueBlockA, 1);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Increment() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
status = mfrc522.MIFARE_Transfer(valueBlockA);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Transfer() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
// Show the new value of valueBlockA
status = mfrc522.MIFARE_GetValue(valueBlockA, &value);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("mifare_GetValue() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
Serial.print("New value of value block "); Serial.print(valueBlockA);
Serial.print(" = "); Serial.println(value);
// Dump the result
mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
// Decrement 10 from the value of valueBlockB and store the result in valueBlockB.
Serial.print("Subtracting 10 from value of block "); Serial.println(valueBlockB);
status = mfrc522.MIFARE_Decrement(valueBlockB, 10);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Decrement() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
status = mfrc522.MIFARE_Transfer(valueBlockB);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Transfer() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
// Show the new value of valueBlockB
status = mfrc522.MIFARE_GetValue(valueBlockB, &value);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("mifare_GetValue() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
Serial.print(F("New value of value block ")); Serial.print(valueBlockB);
Serial.print(F(" = ")); Serial.println(value);
// Check some boundary...
if (value <= -100) {
Serial.println(F("Below -100, so resetting it to 255 = 0xFF just for fun..."));
status = mfrc522.MIFARE_SetValue(valueBlockB, 255);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("mifare_SetValue() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
}
// Halt PICC
mfrc522.PICC_HaltA();
// Dump the sector data
mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
Serial.println();
// Stop encryption on PCD
mfrc522.PCD_StopCrypto1();
// Halt PICC
mfrc522.PICC_HaltA();
// Stop encryption on PCD
mfrc522.PCD_StopCrypto1();
}
void formatBlock(byte blockAddr) {
Serial.print("Reading block "); Serial.println(blockAddr);
byte buffer[18];
byte size = sizeof(buffer);
byte status = mfrc522.MIFARE_Read(blockAddr, buffer, &size);
if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Read() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
/**
* Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
if ( (buffer[0] == (byte)~buffer[4])
&& (buffer[1] == (byte)~buffer[5])
&& (buffer[2] == (byte)~buffer[6])
&& (buffer[3] == (byte)~buffer[7])
/**
* Ensure that a given block is formatted as a Value Block.
*/
void formatValueBlock(byte blockAddr) {
byte buffer[18];
byte size = sizeof(buffer);
MFRC522::StatusCode status;
&& (buffer[0] == buffer[8])
&& (buffer[1] == buffer[9])
&& (buffer[2] == buffer[10])
&& (buffer[3] == buffer[11])
Serial.print(F("Reading block ")); Serial.println(blockAddr);
status = mfrc522.MIFARE_Read(blockAddr, buffer, &size);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Read() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
&& (buffer[12] == (byte)~buffer[13])
&& (buffer[12] == buffer[14])
&& (buffer[12] == (byte)~buffer[15])) {
Serial.println("Block has correct Block Value format.");
}
else {
Serial.println("Writing new value block...");
byte valueBlock[] = { 0,0,0,0, 255,255,255,255, 0,0,0,0, blockAddr,~blockAddr,blockAddr,~blockAddr };
status = mfrc522.MIFARE_Write(blockAddr, valueBlock, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Write() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
}
}
} // End formatBlock()
if ( (buffer[0] == (byte)~buffer[4])
&& (buffer[1] == (byte)~buffer[5])
&& (buffer[2] == (byte)~buffer[6])
&& (buffer[3] == (byte)~buffer[7])
&& (buffer[0] == buffer[8])
&& (buffer[1] == buffer[9])
&& (buffer[2] == buffer[10])
&& (buffer[3] == buffer[11])
&& (buffer[12] == (byte)~buffer[13])
&& (buffer[12] == buffer[14])
&& (buffer[12] == (byte)~buffer[15])) {
Serial.println(F("Block has correct Value Block format."));
}
else {
Serial.println(F("Formatting as Value Block..."));
byte valueBlock[] = {
0, 0, 0, 0,
255, 255, 255, 255,
0, 0, 0, 0,
blockAddr, ~blockAddr, blockAddr, ~blockAddr };
status = mfrc522.MIFARE_Write(blockAddr, valueBlock, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
}
}
}

View File

@@ -1,240 +1,200 @@
/*
* MFRC522 - Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI W AND R BY COOQROBOT.
* The library file MFRC522.h has a wealth of useful info. Please read it.
* The functions are documented in MFRC522.cpp.
/**
* ----------------------------------------------------------------------------
* This is a MFRC522 library example; see https://github.com/miguelbalboa/rfid
* for further details and other examples.
*
* Based on code Dr.Leong ( WWW.B2CQSHOP.COM )
* Created by Miguel Balboa (circuitito.com), Jan, 2012.
* Rewritten by Søren Thing Andersen (access.thing.dk), fall of 2013 (Translation to English, refactored, comments, anti collision, cascade levels.)
* NOTE: The library file MFRC522.h has a lot of useful info. Please read it.
*
* Released into the public domain.
* ----------------------------------------------------------------------------
* This sample shows how to read and write data blocks on a MIFARE Classic PICC
* (= card/tag).
*
* This sample shows how to setup a block on a MIFARE Classic PICC to be in "Value Block" mode.
* In Value Block mode the operations Increment/Decrement/Restore and Transfer can be used.
* BEWARE: Data will be written to the PICC, in sector #1 (blocks #4 to #7).
*
----------------------------------------------------------------------------- empty_skull
- Aggiunti pin per arduino Mega
- Scritto semplice codice per la scrittura e lettura
- add pin configuration for arduino mega
- write simple read/write Code for new entry user
http://mac86project.altervista.org/
----------------------------------------------------------------------------- Nicola Coppola
* Pin layout should be as follows:
* Signal Pin Pin Pin
* Arduino Uno Arduino Mega MFRC522 board
* ------------------------------------------------------------
* Reset 9 5 RST
* SPI SS 10 53 SDA
* SPI MOSI 11 51 MOSI
* SPI MISO 12 50 MISO
* SPI SCK 13 52 SCK
*
* The reader can be found on eBay for around 5 dollars. Search for "mf-rc522" on ebay.com.
* Typical pin layout used:
* -----------------------------------------------------------------------------------------
* MFRC522 Arduino Arduino Arduino Arduino Arduino
* Reader/PCD Uno Mega Nano v3 Leonardo/Micro Pro Micro
* Signal Pin Pin Pin Pin Pin Pin
* -----------------------------------------------------------------------------------------
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
* SPI SS SDA(SS) 10 53 D10 10 10
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*
*/
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 53
#define RST_PIN 5
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
MFRC522::MIFARE_Key key;
/**
* Initialize.
*/
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
//Serial.println("Scan a MIFARE Classic PICC to demonstrate Value Blocks.");
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
// Prepare the key (used both as key A and as key B)
// using FFFFFFFFFFFFh which is the default at chip delivery from the factory
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
Serial.println(F("Scan a MIFARE Classic PICC to demonstrate read and write."));
Serial.print(F("Using key (for A and B):"));
dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
Serial.println();
Serial.println(F("BEWARE: Data will be written to the PICC, in sector #1"));
}
/**
* Main loop.
*/
void loop() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
return;
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
return;
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
// Now a card is selected. The UID and SAK is in mfrc522.uid.
// Show some details of the PICC (that is: the tag/card)
Serial.print(F("Card UID:"));
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println();
Serial.print(F("PICC type: "));
byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));
// Dump UID
Serial.print("Card UID:");
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.println();
// Check for compatibility
if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
&& piccType != MFRC522::PICC_TYPE_MIFARE_1K
&& piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println(F("This sample only works with MIFARE Classic cards."));
return;
}
// Dump PICC type
byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.print("PICC type: ");
Serial.println(mfrc522.PICC_GetTypeName(piccType));
if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
&& piccType != MFRC522::PICC_TYPE_MIFARE_1K
&& piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
//Serial.println("This sample only works with MIFARE Classic cards.");
return;
}
// In this sample we use the second sector,
// that is: sector #1, covering block #4 up to and including block #7
byte sector = 1;
byte blockAddr = 4;
byte dataBlock[] = {
0x01, 0x02, 0x03, 0x04, // 1, 2, 3, 4,
0x05, 0x06, 0x07, 0x08, // 5, 6, 7, 8,
0x08, 0x09, 0xff, 0x0b, // 9, 10, 255, 12,
0x0c, 0x0d, 0x0e, 0x0f // 13, 14, 15, 16
};
byte trailerBlock = 7;
MFRC522::StatusCode status;
byte buffer[18];
byte size = sizeof(buffer);
// In this sample we use the second sector (ie block 4-7). the first sector is = 0
// scegliere settore di lettura da 0 = primo settore
byte sector = 1;
// block sector 0-3(sector0) 4-7(sector1) 8-11(sector2)
// blocchi di scrittura da 0-3(sector0) 4-7(sector1) 8-11(sector2)
byte valueBlockA = 4;
byte valueBlockB = 5;
byte valueBlockC = 6;
byte trailerBlock = 7;
byte status;
// Authenticate using key A.
// avvio l'autentificazione A
//Serial.println("Authenticating using key A...");
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print("PCD_Authenticate() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
// Authenticate using key B.
// avvio l'autentificazione B
//Serial.println("Authenticating again using key B...");
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print("PCD_Authenticate() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
// Authenticate using key A
Serial.println(F("Authenticating using key A..."));
status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
// Writing new value block A
// Scrivo i valori per il settore A
Serial.println("Writing new value block A(4) : the first of the sector TWO ");
byte value1Block[] = { 1,2,3,4, 5,6,7,8, 9,10,255,12, 13,14,15,16, valueBlockA,~valueBlockA,valueBlockA,~valueBlockA };
status = mfrc522.MIFARE_Write(valueBlockA, value1Block, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Write() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
}
/*
// Show the whole sector as it currently is
Serial.println(F("Current data in sector:"));
mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
Serial.println();
// Writing new value block B
// Scrivo i valori per il settore B
Serial.println("Writing new value block B");
byte value2Block[] = { 255,255,255,255, 0,0,0,0, 0,0,0,0, 255,255,255,255, valueBlockB,~valueBlockB,valueBlockB,~valueBlockB };
status = mfrc522.MIFARE_Write(valueBlockB, value2Block, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Write() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
}
// Read data from the block
Serial.print(F("Reading data from block ")); Serial.print(blockAddr);
Serial.println(F(" ..."));
status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(blockAddr, buffer, &size);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Read() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
}
Serial.print(F("Data in block ")); Serial.print(blockAddr); Serial.println(F(":"));
dump_byte_array(buffer, 16); Serial.println();
Serial.println();
// Writing new value block D
// Scrivo i valori per il settore C
Serial.println("Writing new value block C");
byte value3Block[] = { 255,255,255,255, 0,0,0,0, 0,0,0,0, 255,255,255,255, valueBlockC,~valueBlockC,valueBlockC,~valueBlockC };
status = mfrc522.MIFARE_Write(valueBlockC, value3Block, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_Write() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
}
// Authenticate using key B
Serial.println(F("Authenticating again using key B..."));
status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
*/
// Write data to the block
Serial.print(F("Writing data into block ")); Serial.print(blockAddr);
Serial.println(F(" ..."));
dump_byte_array(dataBlock, 16); Serial.println();
status = (MFRC522::StatusCode) mfrc522.MIFARE_Write(blockAddr, dataBlock, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
}
Serial.println();
// Read data from the block (again, should now be what we have written)
Serial.print(F("Reading data from block ")); Serial.print(blockAddr);
Serial.println(F(" ..."));
status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(blockAddr, buffer, &size);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Read() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
}
Serial.print(F("Data in block ")); Serial.print(blockAddr); Serial.println(F(":"));
dump_byte_array(buffer, 16); Serial.println();
Serial.println("Read block A(4) : the first of the sector TWO");
byte buffer[18];
byte size = sizeof(buffer);
// change this: valueBlockA , for read anather block
// cambiate valueBlockA per leggere un altro blocco
status = mfrc522.MIFARE_Read(valueBlockA, buffer, &size);
Serial.print("Settore : 0 Valore :");
Serial.println(buffer[0]);
Serial.print("Settore : 1 Valore :");
Serial.println(buffer[1]);
Serial.print("Settore : 2 Valore :");
Serial.println(buffer[2]);
Serial.print("Settore : 3 Valore :");
Serial.println(buffer[3]);
Serial.print("Settore : 4 Valore :");
Serial.println(buffer[4]);
Serial.print("Settore : 5 Valore :");
Serial.println(buffer[5]);
Serial.print("Settore : 6 Valore :");
Serial.println(buffer[6]);
Serial.print("Settore : 7 Valore :");
Serial.println(buffer[7]);
Serial.print("Settore : 8 Valore :");
Serial.println(buffer[8]);
Serial.print("Settore : 9 Valore :");
Serial.println(buffer[9]);
Serial.print("Settore :10 Valore :");
Serial.println(buffer[10]);
Serial.print("Settore :11 Valore :");
Serial.println(buffer[11]);
Serial.print("Settore :12 Valore :");
Serial.println(buffer[12]);
Serial.print("Settore :13 Valore :");
Serial.println(buffer[13]);
Serial.print("Settore :14 Valore :");
Serial.println(buffer[14]);
Serial.print("Settore :15 Valore :");
Serial.println(buffer[15]);
//byte value1Block[] = { 1,2,3,4, 5,6,7,8, 9,10,255,12, 13,14,15,16, valueBlockA,~valueBlockA,valueBlockA,~valueBlockA };
if (
buffer[0] == 1 &&
buffer[1] == 2 &&
buffer[2] == 3 &&
buffer[3] == 4 &&
buffer[4] == 5 &&
buffer[5] == 6 &&
buffer[6] == 7 &&
buffer[7] == 8 &&
buffer[8] == 9 &&
buffer[9] == 10 &&
buffer[10] == 255 &&
buffer[11] == 12 &&
buffer[12] == 13 &&
buffer[13] == 14 &&
buffer[14] == 15 &&
buffer[15] == 16
){
// sel a scrittura è uguale alla lettura allora e stato un successo !!
Serial.println("Read block A(4) : the first of the sector TWO : success");
Serial.println(":-)");
}else{
// scrittura Fallita
Serial.println("Read block A(4) : the first of the sector TWO : no match - write don't work fine ");
Serial.println(":-( ");
}
// risponde successo
//Serial.println(mfrc522.GetStatusCodeName(status));
// Dump the result
//mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
// Halt PICC
mfrc522.PICC_HaltA();
// Stop encryption on PCD
mfrc522.PCD_StopCrypto1();
// Check that data in block is what we have written
// by counting the number of bytes that are equal
Serial.println(F("Checking result..."));
byte count = 0;
for (byte i = 0; i < 16; i++) {
// Compare buffer (= what we've read) with dataBlock (= what we've written)
if (buffer[i] == dataBlock[i])
count++;
}
Serial.print(F("Number of bytes that match = ")); Serial.println(count);
if (count == 16) {
Serial.println(F("Success :-)"));
} else {
Serial.println(F("Failure, no match :-("));
Serial.println(F(" perhaps the write didn't work properly..."));
}
Serial.println();
// Dump the sector data
Serial.println(F("Current data in sector:"));
mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
Serial.println();
// Halt PICC
mfrc522.PICC_HaltA();
// Stop encryption on PCD
mfrc522.PCD_StopCrypto1();
}
/**
* Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}

View File

@@ -0,0 +1,94 @@
/**
* ----------------------------------------------------------------------------
* This is a MFRC522 library example; see https://github.com/miguelbalboa/rfid
* for further details and other examples.
*
* NOTE: The library file MFRC522.h has a lot of useful info. Please read it.
*
* Released into the public domain.
* ----------------------------------------------------------------------------
* This sample shows how to read and write data blocks on a MIFARE Classic PICC
* (= card/tag).
*
* BEWARE: Data will be written to the PICC, in sector #1 (blocks #4 to #7).
*
*
* Typical pin layout used:
* -----------------------------------------------------------------------------------------
* MFRC522 Arduino Arduino Arduino Arduino Arduino
* Reader/PCD Uno Mega Nano v3 Leonardo/Micro Pro Micro
* Signal Pin Pin Pin Pin Pin Pin
* -----------------------------------------------------------------------------------------
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
* SPI SS 1 SDA(SS) 10 53 D10 10 10
* SPI SS 2 SDA(SS) 2 53 D10 10 10
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*
*/
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 10 // Configurable, see typical pin layout above
#define SS_1_PIN 5 // Configurable, see typical pin layout above
#define SS_2_PIN 3 // Configurable, see typical pin layout above
#define NR_OF_READERS 2
byte ssPins[] = {SS_1_PIN, SS_2_PIN};
MFRC522 mfrc522[NR_OF_READERS]; // Create MFRC522 instance.
/**
* Initialize.
*/
void setup() {
Serial.begin(115200); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card
}
}
/**
* Main loop.
*/
void loop() {
for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
// Look for new cards
if (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()) {
Serial.print(F("Reader: "));
Serial.print(reader);
// Show some details of the PICC (that is: the tag/card)
Serial.print(F(" Card UID:"));
dump_byte_array(mfrc522[reader].uid.uidByte, mfrc522[reader].uid.size);
Serial.println();
Serial.print(F("PICC type: "));
byte piccType = mfrc522[reader].PICC_GetType(mfrc522[reader].uid.sak);
Serial.println(mfrc522[reader].PICC_GetTypeName(piccType));
// Halt PICC
mfrc522[reader].PICC_HaltA();
// Stop encryption on PCD
mfrc522[reader].PCD_StopCrypto1();
} //if (mfrc522[reader].PICC_IsNewC
} //for(uint8_t reader
}
/**
* Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}

View File

@@ -0,0 +1,81 @@
/*
* ----------------------------------------------------------------------------
* Example sketch/program to test your firmware.
* ----------------------------------------------------------------------------
* This is a MFRC522 library example; see https://github.com/miguelbalboa/rfid
* for further details and other examples.
*
* NOTE: The library file MFRC522.h has a lot of useful info. The functions are
* documented in MFRC522.cpp. Please read it.
*
* Released into the public domain.
*
* Typical pin layout used:
* -----------------------------------------------------------------------------------------
* MFRC522 Arduino Arduino Arduino Arduino Arduino
* Reader/PCD Uno Mega Nano v3 Leonardo/Micro Pro Micro
* Signal Pin Pin Pin Pin Pin Pin
* -----------------------------------------------------------------------------------------
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
* SPI SS SDA(SS) 10 53 D10 10 10
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*
* @author Rotzbua
*/
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 module
Serial.println(F("*****************************"));
Serial.println(F("MFRC522 Digital self test"));
Serial.println(F("*****************************"));
ShowReaderVersion(); // Show version of PCD - MFRC522 Card Reader
Serial.println(F("Performing test..."));
bool result = mfrc522.PCD_PerformSelfTest();
Serial.println(F("-----------------------------"));
Serial.print(F("Result: "));
if (result)
Serial.println(F("OK"));
else
Serial.println(F("DEFECT or UNKNOWN"));
Serial.println();
}
void loop() {} // nothing to do
/**
* Helper to print MFRC522 module info
*/
void ShowReaderVersion() {
// Get the MFRC522 firmware version
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
Serial.print(F("Firmware Version: 0x"));
Serial.print(v, HEX);
if (v == 0x88)
Serial.print(F(" = (clone)"));
else if (v == 0x90)
Serial.print(F(" = v0.0"));
else if (v == 0x91)
Serial.print(F(" = v1.0"));
else if (v == 0x92)
Serial.print(F(" = v2.0"));
else
Serial.print(F(" = (unknown)"));
Serial.println();
// When 0x00 or 0xFF is returned, communication probably failed
if ((v == 0x00) || (v == 0xFF))
Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
}

View File

@@ -0,0 +1,152 @@
/*
* ----------------------------------------------------------------------------
* This is a MFRC522 library example; see https://github.com/miguelbalboa/rfid
* for further details and other examples.
*
* NOTE: The library file MFRC522.h has a lot of useful info. Please read it.
*
* Released into the public domain.
* ----------------------------------------------------------------------------
* Example sketch/program which will try the most used default keys listed in
* https://code.google.com/p/mfcuk/wiki/MifareClassicDefaultKeys to dump the
* block 0 of a MIFARE RFID card using a RFID-RC522 reader.
*
* Typical pin layout used:
* -----------------------------------------------------------------------------------------
* MFRC522 Arduino Arduino Arduino Arduino Arduino
* Reader/PCD Uno Mega Nano v3 Leonardo/Micro Pro Micro
* Signal Pin Pin Pin Pin Pin Pin
* -----------------------------------------------------------------------------------------
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
* SPI SS SDA(SS) 10 53 D10 10 10
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*
*/
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
// Number of known default keys (hard-coded)
// NOTE: Synchronize the NR_KNOWN_KEYS define with the defaultKeys[] array
#define NR_KNOWN_KEYS 8
// Known keys, see: https://code.google.com/p/mfcuk/wiki/MifareClassicDefaultKeys
byte knownKeys[NR_KNOWN_KEYS][MFRC522::MF_KEY_SIZE] = {
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, // FF FF FF FF FF FF = factory default
{0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5}, // A0 A1 A2 A3 A4 A5
{0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5}, // B0 B1 B2 B3 B4 B5
{0x4d, 0x3a, 0x99, 0xc3, 0x51, 0xdd}, // 4D 3A 99 C3 51 DD
{0x1a, 0x98, 0x2c, 0x7e, 0x45, 0x9a}, // 1A 98 2C 7E 45 9A
{0xd3, 0xf7, 0xd3, 0xf7, 0xd3, 0xf7}, // D3 F7 D3 F7 D3 F7
{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, // AA BB CC DD EE FF
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 00 00 00 00 00 00
};
/*
* Initialize.
*/
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println(F("Try the most used default keys to print block 0 of a MIFARE PICC."));
}
/*
* Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
/*
* Try using the PICC (the tag/card) with the given key to access block 0.
* On success, it will show the key details, and dump the block data on Serial.
*
* @return true when the given key worked, false otherwise.
*/
boolean try_key(MFRC522::MIFARE_Key *key)
{
boolean result = false;
byte buffer[18];
byte block = 0;
MFRC522::StatusCode status;
// Serial.println(F("Authenticating using key A..."));
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
// Serial.print(F("PCD_Authenticate() failed: "));
// Serial.println(mfrc522.GetStatusCodeName(status));
return false;
}
// Read block
byte byteCount = sizeof(buffer);
status = mfrc522.MIFARE_Read(block, buffer, &byteCount);
if (status != MFRC522::STATUS_OK) {
// Serial.print(F("MIFARE_Read() failed: "));
// Serial.println(mfrc522.GetStatusCodeName(status));
}
else {
// Successful read
result = true;
Serial.print(F("Success with key:"));
dump_byte_array((*key).keyByte, MFRC522::MF_KEY_SIZE);
Serial.println();
// Dump block data
Serial.print(F("Block ")); Serial.print(block); Serial.print(F(":"));
dump_byte_array(buffer, 16);
Serial.println();
}
Serial.println();
mfrc522.PICC_HaltA(); // Halt PICC
mfrc522.PCD_StopCrypto1(); // Stop encryption on PCD
return result;
}
/*
* Main loop.
*/
void loop() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
return;
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
return;
// Show some details of the PICC (that is: the tag/card)
Serial.print(F("Card UID:"));
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println();
Serial.print(F("PICC type: "));
byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));
// Try the known default keys
MFRC522::MIFARE_Key key;
for (byte k = 0; k < NR_KNOWN_KEYS; k++) {
// Copy the known key into the MIFARE_Key structure
for (byte i = 0; i < MFRC522::MF_KEY_SIZE; i++) {
key.keyByte[i] = knownKeys[k][i];
}
// Try the key
if (try_key(&key)) {
// Found and reported on the key and block,
// no need to try other keys for this PICC
break;
}
}
}

View File

@@ -0,0 +1,153 @@
/*
* Write personal data of a MIFARE RFID card using a RFID-RC522 reader
* Uses MFRC522 - Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI W AND R BY COOQROBOT.
* -----------------------------------------------------------------------------------------
* MFRC522 Arduino Arduino Arduino Arduino Arduino
* Reader/PCD Uno Mega Nano v3 Leonardo/Micro Pro Micro
* Signal Pin Pin Pin Pin Pin Pin
* -----------------------------------------------------------------------------------------
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
* SPI SS SDA(SS) 10 53 D10 10 10
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*
* Hardware required:
* Arduino
* PCD (Proximity Coupling Device): NXP MFRC522 Contactless Reader IC
* PICC (Proximity Integrated Circuit Card): A card or tag using the ISO 14443A interface, eg Mifare or NTAG203.
* The reader can be found on eBay for around 5 dollars. Search for "mf-rc522" on ebay.com.
*/
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10 //Arduino Uno
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println(F("Write personal data on a MIFARE PICC "));
}
void loop() {
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) return;
Serial.print(F("Card UID:")); //Dump UID
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.print(F(" PICC type: ")); // Dump PICC type
byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));
byte buffer[34];
byte block;
MFRC522::StatusCode status;
byte len;
Serial.setTimeout(20000L) ; // wait until 20 seconds for input from serial
// Ask personal data: Family name
Serial.println(F("Type Family name, ending with #"));
len=Serial.readBytesUntil('#', (char *) buffer, 30) ; // read family name from serial
for (byte i = len; i < 30; i++) buffer[i] = ' '; // pad with spaces
block = 1;
//Serial.println(F("Authenticating using key A..."));
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else Serial.println(F("PCD_Authenticate() success: "));
// Write block
status = mfrc522.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else Serial.println(F("MIFARE_Write() success: "));
block = 2;
//Serial.println(F("Authenticating using key A..."));
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
// Write block
status = mfrc522.MIFARE_Write(block, &buffer[16], 16);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else Serial.println(F("MIFARE_Write() success: "));
// Ask personal data: First name
Serial.println(F("Type First name, ending with #"));
len=Serial.readBytesUntil('#', (char *) buffer, 20) ; // read first name from serial
for (byte i = len; i < 20; i++) buffer[i] = ' '; // pad with spaces
block = 4;
//Serial.println(F("Authenticating using key A..."));
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
// Write block
status = mfrc522.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else Serial.println(F("MIFARE_Write() success: "));
block = 5;
//Serial.println(F("Authenticating using key A..."));
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
// Write block
status = mfrc522.MIFARE_Write(block, &buffer[16], 16);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else Serial.println(F("MIFARE_Write() success: "));
Serial.println(" ");
mfrc522.PICC_HaltA(); // Halt PICC
mfrc522.PCD_StopCrypto1(); // Stop encryption on PCD
}

View File

@@ -8,6 +8,7 @@
MFRC522 KEYWORD1
PCD_Register KEYWORD1
PCD_Command KEYWORD1
PCD_RxGain KEYWORD1
PICC_Command KEYWORD1
MIFARE_Misc KEYWORD1
PICC_Type KEYWORD1
@@ -18,7 +19,10 @@ MIFARE_Key KEYWORD1
#######################################
# KEYWORD2 Methods and functions
#######################################
# Functions for setting up the Arduino
setSPIConfig KEYWORD2
# Basic interface functions for communicating with the MFRC522
PCD_WriteRegister KEYWORD2
PCD_WriteRegister KEYWORD2
PCD_ReadRegister KEYWORD2
@@ -27,9 +31,17 @@ setBitMask KEYWORD2
PCD_SetRegisterBitMask KEYWORD2
PCD_ClearRegisterBitMask KEYWORD2
PCD_CalculateCRC KEYWORD2
# Functions for manipulating the MFRC522
PCD_Init KEYWORD2
PCD_Reset KEYWORD2
PCD_AntennaOn KEYWORD2
PCD_AntennaOff KEYWORD2
PCD_GetAntennaGain KEYWORD2
PCD_SetAntennaGain KEYWORD2
PCD_PerformSelfTest KEYWORD2
# Functions for communicating with PICCs
PCD_TransceiveData KEYWORD2
PCD_CommunicateWithPICC KEYWORD2
PICC_RequestA KEYWORD2
@@ -37,19 +49,32 @@ PICC_WakeupA KEYWORD2
PICC_REQA_or_WUPA KEYWORD2
PICC_Select KEYWORD2
PICC_HaltA KEYWORD2
# Functions for communicating with MIFARE PICCs
PCD_Authenticate KEYWORD2
PCD_StopCrypto1 KEYWORD2
MIFARE_Read KEYWORD2
MIFARE_Write KEYWORD2
MIFARE_Increment KEYWORD2
MIFARE_Ultralight_Write KEYWORD2
MIFARE_GetValue KEYWORD2
MIFARE_SetValue KEYWORD2
# Support functions
PCD_MIFARE_Transceive KEYWORD2
GetStatusCodeName KEYWORD2
PICC_GetType KEYWORD2
PICC_GetTypeName KEYWORD2
PICC_DumpToSerial KEYWORD2
PICC_DumpMifareClassicToSerial KEYWORD2
PICC_DumpMifareClassicSectorToSerial KEYWORD2
PICC_DumpMifareUltralightToSerial KEYWORD2
MIFARE_SetAccessBits KEYWORD2
MIFARE_OpenUidBackdoor KEYWORD2
MIFARE_SetUid KEYWORD2
MIFARE_UnbrickUidSector KEYWORD2
# Convenience functions - does not add extra functionality
PICC_IsNewCardPresent KEYWORD2
PICC_ReadCardSerial KEYWORD2
@@ -120,6 +145,17 @@ PCD_Receive LITERAL1
PCD_Transceive LITERAL1
PCD_MFAuthent LITERAL1
PCD_SoftReset LITERAL1
RxGain_18dB LITERAL1
RxGain_23dB LITERAL1
RxGain_18dB_2 LITERAL1
RxGain_23dB_2 LITERAL1
RxGain_33dB LITERAL1
RxGain_38dB LITERAL1
RxGain_43dB LITERAL1
RxGain_48dB LITERAL1
RxGain_min LITERAL1
RxGain_avg LITERAL1
RxGain_max LITERAL1
PICC_CMD_REQA LITERAL1
PICC_CMD_WUPA LITERAL1
PICC_CMD_CT LITERAL1
@@ -158,4 +194,3 @@ STATUS_INVALID LITERAL1
STATUS_CRC_WRONG LITERAL1
STATUS_MIFARE_NACK LITERAL1
FIFO_SIZE LITERAL1

13
library.json Normal file
View File

@@ -0,0 +1,13 @@
{
"name": "MFRC522",
"keywords": "rfid, spi",
"description": "Read a card using a MFRC522 reader on your SPI interface",
"repository":
{
"type": "git",
"url": "https://github.com/miguelbalboa/rfid.git"
},
"exclude": "doc",
"frameworks": "arduino",
"platforms": ["atmelavr", "ststm32"]
}

10
library.properties Normal file
View File

@@ -0,0 +1,10 @@
name=MFRC522
#date as version - no leading zero
version=2015.9.4
author=GithubCommunity
maintainer=miguelbalboa
sentence=Arduino RFID Library for MFRC522 (SPI)
paragraph=Read/Write a RFID Card or Tag using the ISO/IEC 14443A/MIFARE interface.
category=Communication
url=https://github.com/miguelbalboa/rfid
architectures=avr,STM32F1