Merge from Sthing
This commit is contained in:
1373
MFRC522.cpp
Normal file
1373
MFRC522.cpp
Normal file
File diff suppressed because it is too large
Load Diff
324
MFRC522.h
Normal file
324
MFRC522.h
Normal file
@@ -0,0 +1,324 @@
|
|||||||
|
/**
|
||||||
|
* MFRC522.h - Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI W AND R BY COOQROBOT.
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* Please read this file for an overview and then MFRC522.cpp for comments on the specific functions.
|
||||||
|
* Search for "mf-rc522" on ebay.com to purchase the MF-RC522 board.
|
||||||
|
*
|
||||||
|
* There are three hardware components involved:
|
||||||
|
* 1) The micro controller: An Arduino
|
||||||
|
* 2) The PCD (short for Proximity Coupling Device): NXP MFRC522 Contactless Reader IC
|
||||||
|
* 3) The PICC (short for Proximity Integrated Circuit Card): A card or tag using the ISO 14443A interface, eg Mifare or NTAG203.
|
||||||
|
*
|
||||||
|
* The microcontroller and card reader uses SPI for communication.
|
||||||
|
* The protocol is described in the MFRC522 datasheet: http://www.nxp.com/documents/data_sheet/MFRC522.pdf
|
||||||
|
*
|
||||||
|
* The card reader and the tags communicate using a 13.56MHz electromagnetic field.
|
||||||
|
* The protocol is defined in ISO/IEC 14443-3 Identification cards -- Contactless integrated circuit cards -- Proximity cards -- Part 3: Initialization and anticollision".
|
||||||
|
* A free version of the final draft can be found at http://wg8.de/wg8n1496_17n3613_Ballot_FCD14443-3.pdf
|
||||||
|
* Details are found in chapter 6, Type A – Initialization and anticollision.
|
||||||
|
*
|
||||||
|
* If only the PICC UID is wanted, the above documents has all the needed information.
|
||||||
|
* To read and write from MIFARE PICCs, the MIFARE protocol is used after the PICC has been selected.
|
||||||
|
* The MIFARE Classic chips and protocol is described in the datasheets:
|
||||||
|
* 1K: http://www.nxp.com/documents/data_sheet/MF1S503x.pdf
|
||||||
|
* 4K: http://www.nxp.com/documents/data_sheet/MF1S703x.pdf
|
||||||
|
* Mini: http://www.idcardmarket.com/download/mifare_S20_datasheet.pdf
|
||||||
|
* The MIFARE Ultralight chip and protocol is described in the datasheets:
|
||||||
|
* Ultralight: http://www.nxp.com/documents/data_sheet/MF0ICU1.pdf
|
||||||
|
* Ultralight C: http://www.nxp.com/documents/short_data_sheet/MF0ICU2_SDS.pdf
|
||||||
|
*
|
||||||
|
* MIFARE Classic 1K (MF1S503x):
|
||||||
|
* Has 16 sectors * 4 blocks/sector * 16 bytes/block = 1024 bytes.
|
||||||
|
* The blocks are numbered 0-63.
|
||||||
|
* Block 3 in each sector 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)
|
||||||
|
* 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.
|
||||||
|
* Warning: Please read section 8.7 "Memory Access". It includes this text: if the PICC detects a format violation the whole sector is irreversibly blocked.
|
||||||
|
* To use a block in "value block" mode (for Increment/Decrement operations) you need to change the sector trailer. Use PICC_SetAccessBits() to calculate the bit patterns.
|
||||||
|
* MIFARE Classic 4K (MF1S703x):
|
||||||
|
* Has (32 sectors * 4 blocks/sector + 8 sectors * 16 blocks/sector) * 16 bytes/block = 4096 bytes.
|
||||||
|
* The blocks are numbered 0-255.
|
||||||
|
* The last block in each sector is the Sector Trailer like above.
|
||||||
|
* MIFARE Classic Mini (MF1 IC S20):
|
||||||
|
* Has 5 sectors * 4 blocks/sector * 16 bytes/block = 320 bytes.
|
||||||
|
* The blocks are numbered 0-19.
|
||||||
|
* The last block in each sector is the Sector Trailer like above.
|
||||||
|
*
|
||||||
|
* 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 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.
|
||||||
|
* 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 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
|
||||||
|
* Page 41 16 bit one way counter
|
||||||
|
* Pages 42-43 Authentication configuration
|
||||||
|
* Pages 44-47 Authentication key
|
||||||
|
*/
|
||||||
|
#ifndef MFRC522_h
|
||||||
|
#define MFRC522_h
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include <SPI.h>
|
||||||
|
|
||||||
|
class MFRC522 {
|
||||||
|
public:
|
||||||
|
// MFRC522 registers. Described in chapter 9 of the datasheet.
|
||||||
|
// When using SPI all addresses are shifted one bit left in the "SPI address byte" (section 8.1.2.3)
|
||||||
|
enum PCD_Register {
|
||||||
|
// Page 0: Command and status
|
||||||
|
// 0x00 // reserved for future use
|
||||||
|
CommandReg = 0x01 << 1, // starts and stops command execution
|
||||||
|
ComIEnReg = 0x02 << 1, // enable and disable interrupt request control bits
|
||||||
|
DivIEnReg = 0x03 << 1, // enable and disable interrupt request control bits
|
||||||
|
ComIrqReg = 0x04 << 1, // interrupt request bits
|
||||||
|
DivIrqReg = 0x05 << 1, // interrupt request bits
|
||||||
|
ErrorReg = 0x06 << 1, // error bits showing the error status of the last command executed
|
||||||
|
Status1Reg = 0x07 << 1, // communication status bits
|
||||||
|
Status2Reg = 0x08 << 1, // receiver and transmitter status bits
|
||||||
|
FIFODataReg = 0x09 << 1, // input and output of 64 byte FIFO buffer
|
||||||
|
FIFOLevelReg = 0x0A << 1, // number of bytes stored in the FIFO buffer
|
||||||
|
WaterLevelReg = 0x0B << 1, // level for FIFO underflow and overflow warning
|
||||||
|
ControlReg = 0x0C << 1, // miscellaneous control registers
|
||||||
|
BitFramingReg = 0x0D << 1, // adjustments for bit-oriented frames
|
||||||
|
CollReg = 0x0E << 1, // bit position of the first bit-collision detected on the RF interface
|
||||||
|
// 0x0F // reserved for future use
|
||||||
|
|
||||||
|
// 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
|
||||||
|
RxModeReg = 0x13 << 1, // defines reception data rate and framing
|
||||||
|
TxControlReg = 0x14 << 1, // controls the logical behavior of the antenna driver pins TX1 and TX2
|
||||||
|
TxASKReg = 0x15 << 1, // controls the setting of the transmission modulation
|
||||||
|
TxSelReg = 0x16 << 1, // selects the internal sources for the antenna driver
|
||||||
|
RxSelReg = 0x17 << 1, // selects internal receiver settings
|
||||||
|
RxThresholdReg = 0x18 << 1, // selects thresholds for the bit decoder
|
||||||
|
DemodReg = 0x19 << 1, // defines demodulator settings
|
||||||
|
// 0x1A // reserved for future use
|
||||||
|
// 0x1B // reserved for future use
|
||||||
|
MfTxReg = 0x1C << 1, // controls some MIFARE communication transmit parameters
|
||||||
|
MfRxReg = 0x1D << 1, // controls some MIFARE communication receive parameters
|
||||||
|
// 0x1E // reserved for future use
|
||||||
|
SerialSpeedReg = 0x1F << 1, // selects the speed of the serial UART interface
|
||||||
|
|
||||||
|
// Page 2: Configuration
|
||||||
|
// 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
|
||||||
|
ModWidthReg = 0x24 << 1, // controls the ModWidth setting?
|
||||||
|
// 0x25 // reserved for future use
|
||||||
|
RFCfgReg = 0x26 << 1, // configures the receiver gain
|
||||||
|
GsNReg = 0x27 << 1, // selects the conductance of the antenna driver pins TX1 and TX2 for modulation
|
||||||
|
CWGsPReg = 0x28 << 1, // defines the conductance of the p-driver output during periods of no modulation
|
||||||
|
ModGsPReg = 0x29 << 1, // defines the conductance of the p-driver output during periods of modulation
|
||||||
|
TModeReg = 0x2A << 1, // defines settings for the internal timer
|
||||||
|
TPrescalerReg = 0x2B << 1, // the lower 8 bits of the TPrescaler value. The 4 high bits are in TModeReg.
|
||||||
|
TReloadRegH = 0x2C << 1, // defines the 16-bit timer reload value
|
||||||
|
TReloadRegL = 0x2D << 1,
|
||||||
|
TCounterValueRegH = 0x2E << 1, // shows the 16-bit timer value
|
||||||
|
TCounterValueRegL = 0x2F << 1,
|
||||||
|
|
||||||
|
// Page 3:Test Registers
|
||||||
|
// 0x30 // reserved for future use
|
||||||
|
TestSel1Reg = 0x31 << 1, // general test signal configuration
|
||||||
|
TestSel2Reg = 0x32 << 1, // general test signal configuration
|
||||||
|
TestPinEnReg = 0x33 << 1, // enables pin output driver on pins D1 to D7
|
||||||
|
TestPinValueReg = 0x34 << 1, // defines the values for D1 to D7 when it is used as an I/O bus
|
||||||
|
TestBusReg = 0x35 << 1, // shows the status of the internal test bus
|
||||||
|
AutoTestReg = 0x36 << 1, // controls the digital self test
|
||||||
|
VersionReg = 0x37 << 1, // shows the software version
|
||||||
|
AnalogTestReg = 0x38 << 1, // controls the pins AUX1 and AUX2
|
||||||
|
TestDAC1Reg = 0x39 << 1, // defines the test value for TestDAC1
|
||||||
|
TestDAC2Reg = 0x3A << 1, // defines the test value for TestDAC2
|
||||||
|
TestADCReg = 0x3B << 1 // shows the value of ADC I and Q channels
|
||||||
|
// 0x3C // reserved for production tests
|
||||||
|
// 0x3D // reserved for production tests
|
||||||
|
// 0x3E // reserved for production tests
|
||||||
|
// 0x3F // reserved for production tests
|
||||||
|
};
|
||||||
|
|
||||||
|
// MFRC522 comands. 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
|
||||||
|
PCD_GenerateRandomID = 0x02, // generates a 10-byte random ID number
|
||||||
|
PCD_CalcCRC = 0x03, // activates the CRC coprocessor or performs a self test
|
||||||
|
PCD_Transmit = 0x04, // transmits data from the FIFO buffer
|
||||||
|
PCD_NoCmdChange = 0x07, // no command change, can be used to modify the CommandReg register bits without affecting the command, for example, the PowerDown bit
|
||||||
|
PCD_Receive = 0x08, // activates the receiver circuits
|
||||||
|
PCD_Transceive = 0x0C, // transmits data from FIFO buffer to antenna and automatically activates the receiver after transmission
|
||||||
|
PCD_MFAuthent = 0x0E, // performs the MIFARE standard authentication as a reader
|
||||||
|
PCD_SoftReset = 0x0F // resets the MFRC522
|
||||||
|
};
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
PICC_CMD_REQA = 0x26, // REQuest command, Type A. Invites PICCs in state IDLE to go to READY and prepare for anticollision or selection. 7 bit frame.
|
||||||
|
PICC_CMD_WUPA = 0x52, // Wake-UP 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_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.
|
||||||
|
// The read/write commands can also be used for MIFARE Ultralight.
|
||||||
|
PICC_CMD_MF_AUTH_KEY_A = 0x60, // Perform authentication with Key A
|
||||||
|
PICC_CMD_MF_AUTH_KEY_B = 0x61, // Perform authentication with Key B
|
||||||
|
PICC_CMD_MF_READ = 0x30, // Reads one 16 byte block from the authenticated sector of the PICC. Also used for MIFARE Ultralight.
|
||||||
|
PICC_CMD_MF_WRITE = 0xA0, // Writes one 16 byte block to the authenticated sector of the PICC. Called "COMPATIBILITY WRITE" for MIFARE Ultralight.
|
||||||
|
PICC_CMD_MF_DECREMENT = 0xC0, // Decrements the contents of a block and stores the result in the internal data register.
|
||||||
|
PICC_CMD_MF_INCREMENT = 0xC1, // Increments the contents of a block and stores the result in the internal data register.
|
||||||
|
PICC_CMD_MF_RESTORE = 0xC2, // Reads the contents of a block into the internal data register.
|
||||||
|
PICC_CMD_MF_TRANSFER = 0xB0, // Writes the contents of the internal data register to a block.
|
||||||
|
// The commands used for MIFARE Ultralight (from http://www.nxp.com/documents/data_sheet/MF0ICU1.pdf, Section 8.6)
|
||||||
|
// The PICC_CMD_MF_READ and PICC_CMD_MF_WRITE can also be used for MIFARE Ultralight.
|
||||||
|
PICC_CMD_UL_WRITE = 0xA2 // Writes one 4 byte page to the PICC.
|
||||||
|
};
|
||||||
|
|
||||||
|
// MIFARE constants that does not fit anywhere else
|
||||||
|
enum MIFARE_Misc {
|
||||||
|
MF_ACK = 0xA, // The MIFARE Classic uses a 4 bit ACK/NAK. Any other value than 0xA is NAK.
|
||||||
|
MF_KEY_SIZE = 6 // A Mifare Crypto1 key is 6 bytes.
|
||||||
|
};
|
||||||
|
|
||||||
|
// PICC types we can detect. Remember to update PICC_GetTypeName() if you add more.
|
||||||
|
enum PICC_Type {
|
||||||
|
PICC_TYPE_UNKNOWN = 0,
|
||||||
|
PICC_TYPE_ISO_14443_4 = 1, // PICC compliant with ISO/IEC 14443-4
|
||||||
|
PICC_TYPE_ISO_18092 = 2, // PICC compliant with ISO/IEC 18092 (NFC)
|
||||||
|
PICC_TYPE_MIFARE_MINI = 3, // MIFARE Classic protocol, 320 bytes
|
||||||
|
PICC_TYPE_MIFARE_1K = 4, // MIFARE Classic protocol, 1KB
|
||||||
|
PICC_TYPE_MIFARE_4K = 5, // MIFARE Classic protocol, 4KB
|
||||||
|
PICC_TYPE_MIFARE_UL = 6, // MIFARE Ultralight or Ultralight C
|
||||||
|
PICC_TYPE_MIFARE_PLUS = 7, // MIFARE Plus
|
||||||
|
PICC_TYPE_TNP3XXX = 8, // Only mentioned in NXP AN 10833 MIFARE Type Identification Procedure
|
||||||
|
PICC_TYPE_NOT_COMPLETE = 255 // SAK indicates UID is not complete.
|
||||||
|
};
|
||||||
|
|
||||||
|
// Return codes from the functions in this class. Remember to update GetStatusCodeName() if you add more.
|
||||||
|
enum StatusCode {
|
||||||
|
STATUS_OK = 1, // Success
|
||||||
|
STATUS_ERROR = 2, // Error in communication
|
||||||
|
STATUS_COLLISION = 3, // Collission detected
|
||||||
|
STATUS_TIMEOUT = 4, // Timeout in communication.
|
||||||
|
STATUS_NO_ROOM = 5, // A buffer is not big enough.
|
||||||
|
STATUS_INTERNAL_ERROR = 6, // Internal error in the code. Should not happen ;-)
|
||||||
|
STATUS_INVALID = 7, // Invalid argument.
|
||||||
|
STATUS_CRC_WRONG = 8, // The CRC_A does not match
|
||||||
|
STATUS_MIFARE_NACK = 9 // A MIFARE PICC responded with NAK.
|
||||||
|
};
|
||||||
|
|
||||||
|
// A struct used for passing the UID of a PICC.
|
||||||
|
typedef struct {
|
||||||
|
byte size; // Number of bytes in the UID. 4, 7 or 10.
|
||||||
|
byte uidByte[10];
|
||||||
|
byte sak; // The SAK (Select acknowledge) byte returned from the PICC after successful selection.
|
||||||
|
} Uid;
|
||||||
|
|
||||||
|
// A struct used for passing a MIFARE Crypto1 key
|
||||||
|
typedef struct {
|
||||||
|
byte keyByte[MF_KEY_SIZE];
|
||||||
|
} MIFARE_Key;
|
||||||
|
|
||||||
|
// Member variables
|
||||||
|
Uid uid; // Used by PICC_ReadCardSerial().
|
||||||
|
|
||||||
|
// Size of the MFRC522 FIFO
|
||||||
|
static const byte FIFO_SIZE = 64; // The FIFO is 64 bytes.
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Functions for setting up the Arduino
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
MFRC522(byte chipSelectPin, byte resetPowerDownPin);
|
||||||
|
void setSPIConfig();
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Basic interface functions for communicating with the MFRC522
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
void PCD_WriteRegister(byte reg, byte value);
|
||||||
|
void PCD_WriteRegister(byte reg, byte count, byte *values);
|
||||||
|
byte PCD_ReadRegister(byte reg);
|
||||||
|
void PCD_ReadRegister(byte reg, byte count, byte *values, byte rxAlign = 0);
|
||||||
|
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);
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Functions for manipulating the MFRC522
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
void PCD_Init();
|
||||||
|
void PCD_Reset();
|
||||||
|
void PCD_AntennaOn();
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// 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();
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Functions for communicating with MIFARE PICCs
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
byte 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);
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// 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);
|
||||||
|
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);
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Convenience functions - does not add extra functionality
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
bool PICC_IsNewCardPresent();
|
||||||
|
bool PICC_ReadCardSerial();
|
||||||
|
|
||||||
|
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);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
487
RFID.cpp
487
RFID.cpp
@@ -1,487 +0,0 @@
|
|||||||
/*
|
|
||||||
* RFID.cpp - Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI W AND R BY COOQROBOT.
|
|
||||||
* Based on code Dr.Leong ( WWW.B2CQSHOP.COM )
|
|
||||||
* Created by Miguel Balboa, Jan, 2012.
|
|
||||||
* Released into the public domain.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/******************************************************************************
|
|
||||||
* Includes
|
|
||||||
******************************************************************************/
|
|
||||||
#include <Arduino.h>
|
|
||||||
#include <RFID.h>
|
|
||||||
|
|
||||||
/******************************************************************************
|
|
||||||
* User API
|
|
||||||
******************************************************************************/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construct RFID
|
|
||||||
* int chipSelectPin RFID /ENABLE pin
|
|
||||||
*/
|
|
||||||
RFID::RFID(int chipSelectPin, int NRSTPD)
|
|
||||||
{
|
|
||||||
_chipSelectPin = chipSelectPin;
|
|
||||||
|
|
||||||
pinMode(_chipSelectPin,OUTPUT); // Set digital as OUTPUT to connect it to the RFID /ENABLE pin
|
|
||||||
digitalWrite(_chipSelectPin, LOW);
|
|
||||||
|
|
||||||
|
|
||||||
pinMode(NRSTPD,OUTPUT); // Set digital pin, Not Reset and Power-down
|
|
||||||
digitalWrite(NRSTPD, HIGH);
|
|
||||||
_NRSTPD = NRSTPD;
|
|
||||||
}
|
|
||||||
/******************************************************************************
|
|
||||||
* User API
|
|
||||||
******************************************************************************/
|
|
||||||
|
|
||||||
bool RFID::isCard()
|
|
||||||
{
|
|
||||||
unsigned char status;
|
|
||||||
unsigned char str[MAX_LEN];
|
|
||||||
|
|
||||||
status = MFRC522Request(PICC_REQIDL, str);
|
|
||||||
if (status == MI_OK) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool RFID::readCardSerial(){
|
|
||||||
|
|
||||||
unsigned char status;
|
|
||||||
unsigned char str[MAX_LEN];
|
|
||||||
|
|
||||||
// Anti-colisi<73>n, devuelva el n<>mero de serie de tarjeta de 4 bytes
|
|
||||||
status = anticoll(str);
|
|
||||||
memcpy(serNum, str, 5);
|
|
||||||
|
|
||||||
if (status == MI_OK) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/******************************************************************************
|
|
||||||
* Dr.Leong ( WWW.B2CQSHOP.COM )
|
|
||||||
******************************************************************************/
|
|
||||||
|
|
||||||
void RFID::init()
|
|
||||||
{
|
|
||||||
digitalWrite(_NRSTPD,HIGH);
|
|
||||||
|
|
||||||
reset();
|
|
||||||
|
|
||||||
//Timer: TPrescaler*TreloadVal/6.78MHz = 24ms
|
|
||||||
writeMFRC522(TModeReg, 0x8D); //Tauto=1; f(Timer) = 6.78MHz/TPreScaler
|
|
||||||
writeMFRC522(TPrescalerReg, 0x3E); //TModeReg[3..0] + TPrescalerReg
|
|
||||||
writeMFRC522(TReloadRegL, 30);
|
|
||||||
writeMFRC522(TReloadRegH, 0);
|
|
||||||
|
|
||||||
writeMFRC522(TxAutoReg, 0x40); //100%ASK
|
|
||||||
writeMFRC522(ModeReg, 0x3D); // CRC valor inicial de 0x6363
|
|
||||||
|
|
||||||
//ClearBitMask(Status2Reg, 0x08); //MFCrypto1On=0
|
|
||||||
//writeMFRC522(RxSelReg, 0x86); //RxWait = RxSelReg[5..0]
|
|
||||||
//writeMFRC522(RFCfgReg, 0x7F); //RxGain = 48dB
|
|
||||||
|
|
||||||
antennaOn(); //Abre la antena
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
void RFID::reset()
|
|
||||||
{
|
|
||||||
writeMFRC522(CommandReg, PCD_RESETPHASE);
|
|
||||||
}
|
|
||||||
|
|
||||||
void RFID::writeMFRC522(unsigned char addr, unsigned char val)
|
|
||||||
{
|
|
||||||
digitalWrite(_chipSelectPin, LOW);
|
|
||||||
|
|
||||||
//0XXXXXX0 formato de direcci<63>n
|
|
||||||
SPI.transfer((addr<<1)&0x7E);
|
|
||||||
SPI.transfer(val);
|
|
||||||
|
|
||||||
digitalWrite(_chipSelectPin, HIGH);
|
|
||||||
}
|
|
||||||
|
|
||||||
void RFID::antennaOn(void)
|
|
||||||
{
|
|
||||||
unsigned char temp;
|
|
||||||
|
|
||||||
temp = readMFRC522(TxControlReg);
|
|
||||||
if (!(temp & 0x03))
|
|
||||||
{
|
|
||||||
setBitMask(TxControlReg, 0x03);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Read_MFRC522 Nombre de la funci<63>n: Read_MFRC522
|
|
||||||
* Descripci<63>n: Desde el MFRC522 leer un byte de un registro de datos
|
|
||||||
* Los par<61>metros de entrada: addr - la direcci<63>n de registro
|
|
||||||
* Valor de retorno: Devuelve un byte de datos de lectura
|
|
||||||
*/
|
|
||||||
unsigned char RFID::readMFRC522(unsigned char addr)
|
|
||||||
{
|
|
||||||
unsigned char val;
|
|
||||||
digitalWrite(_chipSelectPin, LOW);
|
|
||||||
SPI.transfer(((addr<<1)&0x7E) | 0x80);
|
|
||||||
val =SPI.transfer(0x00);
|
|
||||||
digitalWrite(_chipSelectPin, HIGH);
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
|
|
||||||
void RFID::setBitMask(unsigned char reg, unsigned char mask)
|
|
||||||
{
|
|
||||||
unsigned char tmp;
|
|
||||||
tmp = readMFRC522(reg);
|
|
||||||
writeMFRC522(reg, tmp | mask); // set bit mask
|
|
||||||
}
|
|
||||||
|
|
||||||
void RFID::clearBitMask(unsigned char reg, unsigned char mask)
|
|
||||||
{
|
|
||||||
unsigned char tmp;
|
|
||||||
tmp = readMFRC522(reg);
|
|
||||||
writeMFRC522(reg, tmp & (~mask)); // clear bit mask
|
|
||||||
}
|
|
||||||
|
|
||||||
void RFID::calculateCRC(unsigned char *pIndata, unsigned char len, unsigned char *pOutData)
|
|
||||||
{
|
|
||||||
unsigned char i, n;
|
|
||||||
|
|
||||||
clearBitMask(DivIrqReg, 0x04); //CRCIrq = 0
|
|
||||||
setBitMask(FIFOLevelReg, 0x80); //Claro puntero FIFO
|
|
||||||
//Write_MFRC522(CommandReg, PCD_IDLE);
|
|
||||||
|
|
||||||
//Escribir datos en el FIFO
|
|
||||||
for (i=0; i<len; i++)
|
|
||||||
{
|
|
||||||
writeMFRC522(FIFODataReg, *(pIndata+i));
|
|
||||||
}
|
|
||||||
writeMFRC522(CommandReg, PCD_CALCCRC);
|
|
||||||
|
|
||||||
// Esperar a la finalizaci<63>n de c<>lculo del CRC
|
|
||||||
i = 0xFF;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
n = readMFRC522(DivIrqReg);
|
|
||||||
i--;
|
|
||||||
}
|
|
||||||
while ((i!=0) && !(n&0x04)); //CRCIrq = 1
|
|
||||||
|
|
||||||
//Lea el c<>lculo de CRC
|
|
||||||
pOutData[0] = readMFRC522(CRCResultRegL);
|
|
||||||
pOutData[1] = readMFRC522(CRCResultRegM);
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char RFID::MFRC522ToCard(unsigned char command, unsigned char *sendData, unsigned char sendLen, unsigned char *backData, unsigned int *backLen)
|
|
||||||
{
|
|
||||||
unsigned char status = MI_ERR;
|
|
||||||
unsigned char irqEn = 0x00;
|
|
||||||
unsigned char waitIRq = 0x00;
|
|
||||||
unsigned char lastBits;
|
|
||||||
unsigned char n;
|
|
||||||
unsigned int i;
|
|
||||||
|
|
||||||
switch (command)
|
|
||||||
{
|
|
||||||
case PCD_AUTHENT: // Tarjetas de certificaci<63>n cerca
|
|
||||||
{
|
|
||||||
irqEn = 0x12;
|
|
||||||
waitIRq = 0x10;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case PCD_TRANSCEIVE: //La transmisi<73>n de datos FIFO
|
|
||||||
{
|
|
||||||
irqEn = 0x77;
|
|
||||||
waitIRq = 0x30;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
writeMFRC522(CommIEnReg, irqEn|0x80); //De solicitud de interrupci<63>n
|
|
||||||
clearBitMask(CommIrqReg, 0x80); // Borrar todos los bits de petici<63>n de interrupci<63>n
|
|
||||||
setBitMask(FIFOLevelReg, 0x80); //FlushBuffer=1, FIFO de inicializaci<63>n
|
|
||||||
|
|
||||||
writeMFRC522(CommandReg, PCD_IDLE); //NO action;Y cancelar el comando
|
|
||||||
|
|
||||||
//Escribir datos en el FIFO
|
|
||||||
for (i=0; i<sendLen; i++)
|
|
||||||
{
|
|
||||||
writeMFRC522(FIFODataReg, sendData[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
//???? ejecutar el comando
|
|
||||||
writeMFRC522(CommandReg, command);
|
|
||||||
if (command == PCD_TRANSCEIVE)
|
|
||||||
{
|
|
||||||
setBitMask(BitFramingReg, 0x80); //StartSend=1,transmission of data starts
|
|
||||||
}
|
|
||||||
|
|
||||||
// A la espera de recibir datos para completar
|
|
||||||
i = 2000; //i????????,??M1???????25ms ??? i De acuerdo con el ajuste de frecuencia de reloj, el tiempo m<>ximo de espera operaci<63>n M1 25ms tarjeta??
|
|
||||||
do
|
|
||||||
{
|
|
||||||
//CommIrqReg[7..0]
|
|
||||||
//Set1 TxIRq RxIRq IdleIRq HiAlerIRq LoAlertIRq ErrIRq TimerIRq
|
|
||||||
n = readMFRC522(CommIrqReg);
|
|
||||||
i--;
|
|
||||||
}
|
|
||||||
while ((i!=0) && !(n&0x01) && !(n&waitIRq));
|
|
||||||
|
|
||||||
clearBitMask(BitFramingReg, 0x80); //StartSend=0
|
|
||||||
|
|
||||||
if (i != 0)
|
|
||||||
{
|
|
||||||
if(!(readMFRC522(ErrorReg) & 0x1B)) //BufferOvfl Collerr CRCErr ProtecolErr
|
|
||||||
{
|
|
||||||
status = MI_OK;
|
|
||||||
if (n & irqEn & 0x01)
|
|
||||||
{
|
|
||||||
status = MI_NOTAGERR; //??
|
|
||||||
}
|
|
||||||
|
|
||||||
if (command == PCD_TRANSCEIVE)
|
|
||||||
{
|
|
||||||
n = readMFRC522(FIFOLevelReg);
|
|
||||||
lastBits = readMFRC522(ControlReg) & 0x07;
|
|
||||||
if (lastBits)
|
|
||||||
{
|
|
||||||
*backLen = (n-1)*8 + lastBits;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
*backLen = n*8;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (n == 0)
|
|
||||||
{
|
|
||||||
n = 1;
|
|
||||||
}
|
|
||||||
if (n > MAX_LEN)
|
|
||||||
{
|
|
||||||
n = MAX_LEN;
|
|
||||||
}
|
|
||||||
|
|
||||||
//??FIFO??????? Lea los datos recibidos en el FIFO
|
|
||||||
for (i=0; i<n; i++)
|
|
||||||
{
|
|
||||||
backData[i] = readMFRC522(FIFODataReg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
status = MI_ERR;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//SetBitMask(ControlReg,0x80); //timer stops
|
|
||||||
//Write_MFRC522(CommandReg, PCD_IDLE);
|
|
||||||
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Nombre de la funci<63>n: MFRC522_Request
|
|
||||||
* Descripci<63>n: Buscar las cartas, leer el n<>mero de tipo de tarjeta
|
|
||||||
* Los par<61>metros de entrada: reqMode - encontrar el modo de tarjeta,
|
|
||||||
* Tagtype - Devuelve el tipo de tarjeta
|
|
||||||
* 0x4400 = Mifare_UltraLight
|
|
||||||
* 0x0400 = Mifare_One(S50)
|
|
||||||
* 0x0200 = Mifare_One(S70)
|
|
||||||
* 0x0800 = Mifare_Pro(X)
|
|
||||||
* 0x4403 = Mifare_DESFire
|
|
||||||
* Valor de retorno: el retorno exitoso MI_OK
|
|
||||||
*/
|
|
||||||
unsigned char RFID::MFRC522Request(unsigned char reqMode, unsigned char *TagType)
|
|
||||||
{
|
|
||||||
unsigned char status;
|
|
||||||
unsigned int backBits; // Recibi<62> bits de datos
|
|
||||||
|
|
||||||
writeMFRC522(BitFramingReg, 0x07); //TxLastBists = BitFramingReg[2..0] ???
|
|
||||||
|
|
||||||
TagType[0] = reqMode;
|
|
||||||
status = MFRC522ToCard(PCD_TRANSCEIVE, TagType, 1, TagType, &backBits);
|
|
||||||
|
|
||||||
if ((status != MI_OK) || (backBits != 0x10))
|
|
||||||
{
|
|
||||||
status = MI_ERR;
|
|
||||||
}
|
|
||||||
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* MFRC522Anticoll -> anticoll
|
|
||||||
* Anti-detecci<63>n de colisiones, la lectura del n<>mero de serie de la tarjeta de tarjeta
|
|
||||||
* @param serNum - devuelve el n<>mero de tarjeta 4 bytes de serie, los primeros 5 bytes de bytes de paridad
|
|
||||||
* @return retorno exitoso MI_OK
|
|
||||||
*/
|
|
||||||
unsigned char RFID::anticoll(unsigned char *serNum)
|
|
||||||
{
|
|
||||||
unsigned char status;
|
|
||||||
unsigned char i;
|
|
||||||
unsigned char serNumCheck=0;
|
|
||||||
unsigned int unLen;
|
|
||||||
|
|
||||||
|
|
||||||
//ClearBitMask(Status2Reg, 0x08); //TempSensclear
|
|
||||||
//ClearBitMask(CollReg,0x80); //ValuesAfterColl
|
|
||||||
writeMFRC522(BitFramingReg, 0x00); //TxLastBists = BitFramingReg[2..0]
|
|
||||||
|
|
||||||
serNum[0] = PICC_ANTICOLL;
|
|
||||||
serNum[1] = 0x20;
|
|
||||||
status = MFRC522ToCard(PCD_TRANSCEIVE, serNum, 2, serNum, &unLen);
|
|
||||||
|
|
||||||
if (status == MI_OK)
|
|
||||||
{
|
|
||||||
//?????? Compruebe el n<>mero de serie de la tarjeta
|
|
||||||
for (i=0; i<4; i++)
|
|
||||||
{
|
|
||||||
serNumCheck ^= serNum[i];
|
|
||||||
}
|
|
||||||
if (serNumCheck != serNum[i])
|
|
||||||
{
|
|
||||||
status = MI_ERR;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//SetBitMask(CollReg, 0x80); //ValuesAfterColl=1
|
|
||||||
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* MFRC522Auth -> auth
|
|
||||||
* Verificar la contrase<73>a de la tarjeta
|
|
||||||
* Los par<61>metros de entrada: AuthMode - Modo de autenticaci<63>n de contrase<73>a
|
|
||||||
0x60 = A 0x60 = validaci<63>n KeyA
|
|
||||||
0x61 = B 0x61 = validaci<63>n KeyB
|
|
||||||
BlockAddr-- bloque de direcciones
|
|
||||||
Sectorkey-- sector contrase<73>a
|
|
||||||
serNum--,4? Tarjeta de n<>mero de serie, 4 bytes
|
|
||||||
* MI_OK Valor de retorno: el retorno exitoso MI_OK
|
|
||||||
*/
|
|
||||||
unsigned char RFID::auth(unsigned char authMode, unsigned char BlockAddr, unsigned char *Sectorkey, unsigned char *serNum)
|
|
||||||
{
|
|
||||||
unsigned char status;
|
|
||||||
unsigned int recvBits;
|
|
||||||
unsigned char i;
|
|
||||||
unsigned char buff[12];
|
|
||||||
|
|
||||||
//????+???+????+???? Verifique la direcci<63>n de comandos de bloques del sector + + contrase<73>a + n<>mero de la tarjeta de serie
|
|
||||||
buff[0] = authMode;
|
|
||||||
buff[1] = BlockAddr;
|
|
||||||
for (i=0; i<6; i++)
|
|
||||||
{
|
|
||||||
buff[i+2] = *(Sectorkey+i);
|
|
||||||
}
|
|
||||||
for (i=0; i<4; i++)
|
|
||||||
{
|
|
||||||
buff[i+8] = *(serNum+i);
|
|
||||||
}
|
|
||||||
status = MFRC522ToCard(PCD_AUTHENT, buff, 12, buff, &recvBits);
|
|
||||||
|
|
||||||
if ((status != MI_OK) || (!(readMFRC522(Status2Reg) & 0x08)))
|
|
||||||
{
|
|
||||||
status = MI_ERR;
|
|
||||||
}
|
|
||||||
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* MFRC522Read -> read
|
|
||||||
* Lectura de datos de bloque
|
|
||||||
* Los par<61>metros de entrada: blockAddr - direcci<63>n del bloque; recvData - leer un bloque de datos
|
|
||||||
* MI_OK Valor de retorno: el retorno exitoso MI_OK
|
|
||||||
*/
|
|
||||||
unsigned char RFID::read(unsigned char blockAddr, unsigned char *recvData)
|
|
||||||
{
|
|
||||||
unsigned char status;
|
|
||||||
unsigned int unLen;
|
|
||||||
|
|
||||||
recvData[0] = PICC_READ;
|
|
||||||
recvData[1] = blockAddr;
|
|
||||||
calculateCRC(recvData,2, &recvData[2]);
|
|
||||||
status = MFRC522ToCard(PCD_TRANSCEIVE, recvData, 4, recvData, &unLen);
|
|
||||||
|
|
||||||
if ((status != MI_OK) || (unLen != 0x90))
|
|
||||||
{
|
|
||||||
status = MI_ERR;
|
|
||||||
}
|
|
||||||
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* MFRC522Write -> write
|
|
||||||
* La escritura de datos de bloque
|
|
||||||
* blockAddr - direcci<63>n del bloque; WriteData - para escribir 16 bytes del bloque de datos
|
|
||||||
* Valor de retorno: el retorno exitoso MI_OK
|
|
||||||
*/
|
|
||||||
unsigned char RFID::write(unsigned char blockAddr, unsigned char *writeData)
|
|
||||||
{
|
|
||||||
unsigned char status;
|
|
||||||
unsigned int recvBits;
|
|
||||||
unsigned char i;
|
|
||||||
unsigned char buff[18];
|
|
||||||
|
|
||||||
buff[0] = PICC_WRITE;
|
|
||||||
buff[1] = blockAddr;
|
|
||||||
calculateCRC(buff, 2, &buff[2]);
|
|
||||||
status = MFRC522ToCard(PCD_TRANSCEIVE, buff, 4, buff, &recvBits);
|
|
||||||
|
|
||||||
if ((status != MI_OK) || (recvBits != 4) || ((buff[0] & 0x0F) != 0x0A))
|
|
||||||
{
|
|
||||||
status = MI_ERR;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (status == MI_OK)
|
|
||||||
{
|
|
||||||
for (i=0; i<16; i++) //?FIFO?16Byte?? Datos a la FIFO 16Byte escribir
|
|
||||||
{
|
|
||||||
buff[i] = *(writeData+i);
|
|
||||||
}
|
|
||||||
calculateCRC(buff, 16, &buff[16]);
|
|
||||||
status = MFRC522ToCard(PCD_TRANSCEIVE, buff, 18, buff, &recvBits);
|
|
||||||
|
|
||||||
if ((status != MI_OK) || (recvBits != 4) || ((buff[0] & 0x0F) != 0x0A))
|
|
||||||
{
|
|
||||||
status = MI_ERR;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* MFRC522Halt -> halt
|
|
||||||
* Cartas de Mando para dormir
|
|
||||||
* Los par<61>metros de entrada: Ninguno
|
|
||||||
* Valor devuelto: Ninguno
|
|
||||||
*/
|
|
||||||
void RFID::halt()
|
|
||||||
{
|
|
||||||
unsigned char status;
|
|
||||||
unsigned int unLen;
|
|
||||||
unsigned char buff[4];
|
|
||||||
|
|
||||||
buff[0] = PICC_HALT;
|
|
||||||
buff[1] = 0;
|
|
||||||
calculateCRC(buff, 2, &buff[2]);
|
|
||||||
|
|
||||||
clearBitMask(Status2Reg, 0x08); // turn off encryption
|
|
||||||
|
|
||||||
status = MFRC522ToCard(PCD_TRANSCEIVE, buff, 4, buff,&unLen);
|
|
||||||
}
|
|
||||||
151
RFID.h
151
RFID.h
@@ -1,151 +0,0 @@
|
|||||||
/* RFID.h - Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI W AND R BY COOQROBOT.
|
|
||||||
* Based on code Dr.Leong ( WWW.B2CQSHOP.COM )
|
|
||||||
* Created by Miguel Balboa (circuitito.com), Jan, 2012.
|
|
||||||
*/
|
|
||||||
#ifndef RFID_h
|
|
||||||
#define RFID_h
|
|
||||||
|
|
||||||
#include <Arduino.h>
|
|
||||||
#include <SPI.h>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/******************************************************************************
|
|
||||||
* Definitions
|
|
||||||
******************************************************************************/
|
|
||||||
#define MAX_LEN 16 // Largo m<>ximo de la matriz
|
|
||||||
|
|
||||||
//MF522 comando palabra
|
|
||||||
#define PCD_IDLE 0x00 // NO action; Y cancelar el comando
|
|
||||||
#define PCD_AUTHENT 0x0E // autenticaci<63>n de clave
|
|
||||||
#define PCD_RECEIVE 0x08 // recepci<63>n de datos
|
|
||||||
#define PCD_TRANSMIT 0x04 // Enviar datos
|
|
||||||
#define PCD_TRANSCEIVE 0x0C // Enviar y recibir datos
|
|
||||||
#define PCD_RESETPHASE 0x0F // reajustar
|
|
||||||
#define PCD_CALCCRC 0x03 // CRC calcular
|
|
||||||
|
|
||||||
//Mifare_One Tarjeta Mifare_One comando palabra
|
|
||||||
#define PICC_REQIDL 0x26 // <20>rea de la antena no est<73> tratando de entrar en el estado de reposo
|
|
||||||
#define PICC_REQALL 0x52 // Todas las cartas para encontrar el <20>rea de la antena
|
|
||||||
#define PICC_ANTICOLL 0x93 // anti-colisi<73>n
|
|
||||||
#define PICC_SElECTTAG 0x93 // elecci<63>n de tarjeta
|
|
||||||
#define PICC_AUTHENT1A 0x60 // verificaci<63>n key A
|
|
||||||
#define PICC_AUTHENT1B 0x61 // verificaci<63>n Key B
|
|
||||||
#define PICC_READ 0x30 // leer bloque
|
|
||||||
#define PICC_WRITE 0xA0 // Escribir en el bloque
|
|
||||||
#define PICC_DECREMENT 0xC0 // cargo
|
|
||||||
#define PICC_INCREMENT 0xC1 // recargar
|
|
||||||
#define PICC_RESTORE 0xC2 // Transferencia de datos de bloque de buffer
|
|
||||||
#define PICC_TRANSFER 0xB0 // Guardar los datos en el b<>fer
|
|
||||||
#define PICC_HALT 0x50 // inactividad
|
|
||||||
|
|
||||||
//MF522 C<>digo de error de comunicaci<63>n cuando regres<65>
|
|
||||||
#define MI_OK 0
|
|
||||||
#define MI_NOTAGERR 1
|
|
||||||
#define MI_ERR 2
|
|
||||||
|
|
||||||
//------------------ MFRC522 registro---------------
|
|
||||||
//Page 0:Command and Status
|
|
||||||
#define Reserved00 0x00
|
|
||||||
#define CommandReg 0x01
|
|
||||||
#define CommIEnReg 0x02
|
|
||||||
#define DivlEnReg 0x03
|
|
||||||
#define CommIrqReg 0x04
|
|
||||||
#define DivIrqReg 0x05
|
|
||||||
#define ErrorReg 0x06
|
|
||||||
#define Status1Reg 0x07
|
|
||||||
#define Status2Reg 0x08
|
|
||||||
#define FIFODataReg 0x09
|
|
||||||
#define FIFOLevelReg 0x0A
|
|
||||||
#define WaterLevelReg 0x0B
|
|
||||||
#define ControlReg 0x0C
|
|
||||||
#define BitFramingReg 0x0D
|
|
||||||
#define CollReg 0x0E
|
|
||||||
#define Reserved01 0x0F
|
|
||||||
//Page 1:Command
|
|
||||||
#define Reserved10 0x10
|
|
||||||
#define ModeReg 0x11
|
|
||||||
#define TxModeReg 0x12
|
|
||||||
#define RxModeReg 0x13
|
|
||||||
#define TxControlReg 0x14
|
|
||||||
#define TxAutoReg 0x15
|
|
||||||
#define TxSelReg 0x16
|
|
||||||
#define RxSelReg 0x17
|
|
||||||
#define RxThresholdReg 0x18
|
|
||||||
#define DemodReg 0x19
|
|
||||||
#define Reserved11 0x1A
|
|
||||||
#define Reserved12 0x1B
|
|
||||||
#define MifareReg 0x1C
|
|
||||||
#define Reserved13 0x1D
|
|
||||||
#define Reserved14 0x1E
|
|
||||||
#define SerialSpeedReg 0x1F
|
|
||||||
//Page 2:CFG
|
|
||||||
#define Reserved20 0x20
|
|
||||||
#define CRCResultRegM 0x21
|
|
||||||
#define CRCResultRegL 0x22
|
|
||||||
#define Reserved21 0x23
|
|
||||||
#define ModWidthReg 0x24
|
|
||||||
#define Reserved22 0x25
|
|
||||||
#define RFCfgReg 0x26
|
|
||||||
#define GsNReg 0x27
|
|
||||||
#define CWGsPReg 0x28
|
|
||||||
#define ModGsPReg 0x29
|
|
||||||
#define TModeReg 0x2A
|
|
||||||
#define TPrescalerReg 0x2B
|
|
||||||
#define TReloadRegH 0x2C
|
|
||||||
#define TReloadRegL 0x2D
|
|
||||||
#define TCounterValueRegH 0x2E
|
|
||||||
#define TCounterValueRegL 0x2F
|
|
||||||
//Page 3:TestRegister
|
|
||||||
#define Reserved30 0x30
|
|
||||||
#define TestSel1Reg 0x31
|
|
||||||
#define TestSel2Reg 0x32
|
|
||||||
#define TestPinEnReg 0x33
|
|
||||||
#define TestPinValueReg 0x34
|
|
||||||
#define TestBusReg 0x35
|
|
||||||
#define AutoTestReg 0x36
|
|
||||||
#define VersionReg 0x37
|
|
||||||
#define AnalogTestReg 0x38
|
|
||||||
#define TestDAC1Reg 0x39
|
|
||||||
#define TestDAC2Reg 0x3A
|
|
||||||
#define TestADCReg 0x3B
|
|
||||||
#define Reserved31 0x3C
|
|
||||||
#define Reserved32 0x3D
|
|
||||||
#define Reserved33 0x3E
|
|
||||||
#define Reserved34 0x3F
|
|
||||||
//-----------------------------------------------
|
|
||||||
|
|
||||||
class RFID
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
RFID(int chipSelectPin, int NRSTPD);
|
|
||||||
|
|
||||||
bool isCard();
|
|
||||||
bool readCardSerial();
|
|
||||||
|
|
||||||
void init();
|
|
||||||
void reset();
|
|
||||||
void writeMFRC522(unsigned char addr, unsigned char val);
|
|
||||||
void antennaOn(void);
|
|
||||||
unsigned char readMFRC522(unsigned char addr);
|
|
||||||
void setBitMask(unsigned char reg, unsigned char mask);
|
|
||||||
void clearBitMask(unsigned char reg, unsigned char mask);
|
|
||||||
void calculateCRC(unsigned char *pIndata, unsigned char len, unsigned char *pOutData);
|
|
||||||
unsigned char MFRC522Request(unsigned char reqMode, unsigned char *TagType);
|
|
||||||
unsigned char MFRC522ToCard(unsigned char command, unsigned char *sendData, unsigned char sendLen, unsigned char *backData, unsigned int *backLen);
|
|
||||||
unsigned char anticoll(unsigned char *serNum);
|
|
||||||
unsigned char auth(unsigned char authMode, unsigned char BlockAddr, unsigned char *Sectorkey, unsigned char *serNum);
|
|
||||||
unsigned char read(unsigned char blockAddr, unsigned char *recvData);
|
|
||||||
unsigned char write(unsigned char blockAddr, unsigned char *writeData);
|
|
||||||
void halt();
|
|
||||||
|
|
||||||
unsigned char serNum[5]; // Constante para guardar el numero de serie leido.
|
|
||||||
unsigned char AserNum[5]; // Constante para guardar el numero d serie de la secion actual.
|
|
||||||
|
|
||||||
private:
|
|
||||||
int _chipSelectPin;
|
|
||||||
int _NRSTPD;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
9
changes.txt
Normal file
9
changes.txt
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
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.
|
||||||
|
|
||||||
52
examples/DumpInfo/DumpInfo.ino
Normal file
52
examples/DumpInfo/DumpInfo.ino
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
* 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.)
|
||||||
|
* Released into the public domain.
|
||||||
|
*
|
||||||
|
* Sample program showing how to read data from a PICC using a MFRC522 reader on the Arduino SPI interface.
|
||||||
|
* Pin layout should be as follows:
|
||||||
|
* Signal Pin Pin
|
||||||
|
* Arduino Uno MFRC522 board
|
||||||
|
* -----------------------------------------
|
||||||
|
* Reset 9 RST
|
||||||
|
* SPI SS 10 SDA
|
||||||
|
* SPI MOSI 11 MOSI
|
||||||
|
* SPI MISO 12 MISO
|
||||||
|
* SPI SCK 13 SCK
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
|
||||||
|
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 PICC to see UID and type...");
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
// Look for new cards
|
||||||
|
if ( ! mfrc522.PICC_IsNewCardPresent()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Select one of the cards
|
||||||
|
if ( ! mfrc522.PICC_ReadCardSerial()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dump debug info about the card. PICC_HaltA() is automatically called.
|
||||||
|
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
|
||||||
|
}
|
||||||
197
examples/MifareClassicValueBlock/MifareClassicValueBlock.ino
Normal file
197
examples/MifareClassicValueBlock/MifareClassicValueBlock.ino
Normal file
@@ -0,0 +1,197 @@
|
|||||||
|
/*
|
||||||
|
* 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.)
|
||||||
|
* Released into the public domain.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* Pin layout should be as follows:
|
||||||
|
* Signal Pin Pin
|
||||||
|
* Arduino Uno MFRC522 board
|
||||||
|
* -----------------------------------------
|
||||||
|
* Reset 9 RST
|
||||||
|
* SPI SS 10 SDA
|
||||||
|
* SPI MOSI 11 MOSI
|
||||||
|
* SPI MISO 12 MISO
|
||||||
|
* SPI SCK 13 SCK
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
|
||||||
|
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.");
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
// 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();
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 (ie block 4-7).
|
||||||
|
byte sector = 1;
|
||||||
|
byte valueBlockA = 5;
|
||||||
|
byte valueBlockB = 6;
|
||||||
|
byte trailerBlock = 7;
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dump the result
|
||||||
|
mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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("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()
|
||||||
@@ -1,126 +0,0 @@
|
|||||||
/**
|
|
||||||
* Read a card using a mfrc522 reader on your SPI interface
|
|
||||||
* Pin layout should be as follows (on Arduino Uno):
|
|
||||||
* MOSI: Pin 11 / ICSP-4
|
|
||||||
* MISO: Pin 12 / ICSP-1
|
|
||||||
* SCK: Pin 13 / ISCP-3
|
|
||||||
* SS: Pin 10
|
|
||||||
* RST: Pin 9
|
|
||||||
*
|
|
||||||
* Script is based on the script of Miguel Balboa.
|
|
||||||
* Serial number is shown on a HD44780 compatible display
|
|
||||||
*
|
|
||||||
* The circuit:
|
|
||||||
* LCD RS pin to digital pin (7)
|
|
||||||
* LCD Enable pin to digital pin (6)
|
|
||||||
* LCD D4 pin to digital pin 5
|
|
||||||
* LCD D5 pin to digital pin 4
|
|
||||||
* LCD D6 pin to digital pin 3
|
|
||||||
* LCD D7 pin to digital pin 2
|
|
||||||
* LCD R/W pin to ground
|
|
||||||
* 10K resistor:
|
|
||||||
* ends to +5V and ground
|
|
||||||
* wiper to LCD VO pin (pin 3)
|
|
||||||
*
|
|
||||||
* @version 0.1
|
|
||||||
* @author Henri de Jong
|
|
||||||
* @since 27-01-2013
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <SPI.h>
|
|
||||||
#include <RFID.h>
|
|
||||||
#include <LiquidCrystal.h>
|
|
||||||
|
|
||||||
#define SS_PIN 10
|
|
||||||
#define RST_PIN 9
|
|
||||||
|
|
||||||
RFID rfid(SS_PIN, RST_PIN);
|
|
||||||
|
|
||||||
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
|
|
||||||
|
|
||||||
// Setup variables:
|
|
||||||
int serNum0;
|
|
||||||
int serNum1;
|
|
||||||
int serNum2;
|
|
||||||
int serNum3;
|
|
||||||
int serNum4;
|
|
||||||
|
|
||||||
void setup()
|
|
||||||
{
|
|
||||||
Serial.begin(9600);
|
|
||||||
lcd.begin(16, 2);
|
|
||||||
SPI.begin();
|
|
||||||
rfid.init();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
|
|
||||||
if (rfid.isCard()) {
|
|
||||||
if (rfid.readCardSerial()) {
|
|
||||||
if (rfid.serNum[0] != serNum0
|
|
||||||
&& rfid.serNum[1] != serNum1
|
|
||||||
&& rfid.serNum[2] != serNum2
|
|
||||||
&& rfid.serNum[3] != serNum3
|
|
||||||
&& rfid.serNum[4] != serNum4
|
|
||||||
) {
|
|
||||||
/* With a new cardnumber, show it. */
|
|
||||||
Serial.println(" ");
|
|
||||||
Serial.println("Card found");
|
|
||||||
serNum0 = rfid.serNum[0];
|
|
||||||
serNum1 = rfid.serNum[1];
|
|
||||||
serNum2 = rfid.serNum[2];
|
|
||||||
serNum3 = rfid.serNum[3];
|
|
||||||
serNum4 = rfid.serNum[4];
|
|
||||||
|
|
||||||
//Serial.println(" ");
|
|
||||||
Serial.println("Cardnumber:");
|
|
||||||
Serial.print("Dec: ");
|
|
||||||
Serial.print(rfid.serNum[0],DEC);
|
|
||||||
Serial.print(", ");
|
|
||||||
Serial.print(rfid.serNum[1],DEC);
|
|
||||||
Serial.print(", ");
|
|
||||||
Serial.print(rfid.serNum[2],DEC);
|
|
||||||
Serial.print(", ");
|
|
||||||
Serial.print(rfid.serNum[3],DEC);
|
|
||||||
Serial.print(", ");
|
|
||||||
Serial.print(rfid.serNum[4],DEC);
|
|
||||||
Serial.println(" ");
|
|
||||||
|
|
||||||
Serial.print("Hex: ");
|
|
||||||
Serial.print(rfid.serNum[0],HEX);
|
|
||||||
Serial.print(", ");
|
|
||||||
Serial.print(rfid.serNum[1],HEX);
|
|
||||||
Serial.print(", ");
|
|
||||||
Serial.print(rfid.serNum[2],HEX);
|
|
||||||
Serial.print(", ");
|
|
||||||
Serial.print(rfid.serNum[3],HEX);
|
|
||||||
Serial.print(", ");
|
|
||||||
Serial.print(rfid.serNum[4],HEX);
|
|
||||||
Serial.println(" ");
|
|
||||||
|
|
||||||
/* Write the HEX code to the display */
|
|
||||||
lcd.clear();
|
|
||||||
lcd.setCursor(0, 0);
|
|
||||||
lcd.print("Cardno (hex):");
|
|
||||||
lcd.setCursor(0,1);
|
|
||||||
lcd.print(rfid.serNum[0], HEX);
|
|
||||||
lcd.print(',');
|
|
||||||
lcd.print(rfid.serNum[1], HEX);
|
|
||||||
lcd.print(',');
|
|
||||||
lcd.print(rfid.serNum[2], HEX);
|
|
||||||
lcd.print(',');
|
|
||||||
lcd.print(rfid.serNum[3], HEX);
|
|
||||||
lcd.print(',');
|
|
||||||
lcd.print(rfid.serNum[4], HEX);
|
|
||||||
} else {
|
|
||||||
/* If we have the same ID, just write a dot. */
|
|
||||||
Serial.print(".");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
rfid.halt();
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,96 +0,0 @@
|
|||||||
/**
|
|
||||||
* Read a card using a mfrc522 reader on your SPI interface
|
|
||||||
* Pin layout should be as follows (on Arduino Uno):
|
|
||||||
* MOSI: Pin 11 / ICSP-4
|
|
||||||
* MISO: Pin 12 / ICSP-1
|
|
||||||
* SCK: Pin 13 / ISCP-3
|
|
||||||
* SS: Pin 10
|
|
||||||
* RST: Pin 9
|
|
||||||
*
|
|
||||||
* Script is based on the script of Miguel Balboa.
|
|
||||||
* New cardnumber is printed when card has changed. Only a dot is printed
|
|
||||||
* if card is the same.
|
|
||||||
*
|
|
||||||
* @version 0.1
|
|
||||||
* @author Henri de Jong
|
|
||||||
* @since 06-01-2013
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <SPI.h>
|
|
||||||
#include <RFID.h>
|
|
||||||
|
|
||||||
#define SS_PIN 10
|
|
||||||
#define RST_PIN 9
|
|
||||||
|
|
||||||
RFID rfid(SS_PIN, RST_PIN);
|
|
||||||
|
|
||||||
// Setup variables:
|
|
||||||
int serNum0;
|
|
||||||
int serNum1;
|
|
||||||
int serNum2;
|
|
||||||
int serNum3;
|
|
||||||
int serNum4;
|
|
||||||
|
|
||||||
void setup()
|
|
||||||
{
|
|
||||||
Serial.begin(9600);
|
|
||||||
SPI.begin();
|
|
||||||
rfid.init();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
|
|
||||||
if (rfid.isCard()) {
|
|
||||||
if (rfid.readCardSerial()) {
|
|
||||||
if (rfid.serNum[0] != serNum0
|
|
||||||
&& rfid.serNum[1] != serNum1
|
|
||||||
&& rfid.serNum[2] != serNum2
|
|
||||||
&& rfid.serNum[3] != serNum3
|
|
||||||
&& rfid.serNum[4] != serNum4
|
|
||||||
) {
|
|
||||||
/* With a new cardnumber, show it. */
|
|
||||||
Serial.println(" ");
|
|
||||||
Serial.println("Card found");
|
|
||||||
serNum0 = rfid.serNum[0];
|
|
||||||
serNum1 = rfid.serNum[1];
|
|
||||||
serNum2 = rfid.serNum[2];
|
|
||||||
serNum3 = rfid.serNum[3];
|
|
||||||
serNum4 = rfid.serNum[4];
|
|
||||||
|
|
||||||
//Serial.println(" ");
|
|
||||||
Serial.println("Cardnumber:");
|
|
||||||
Serial.print("Dec: ");
|
|
||||||
Serial.print(rfid.serNum[0],DEC);
|
|
||||||
Serial.print(", ");
|
|
||||||
Serial.print(rfid.serNum[1],DEC);
|
|
||||||
Serial.print(", ");
|
|
||||||
Serial.print(rfid.serNum[2],DEC);
|
|
||||||
Serial.print(", ");
|
|
||||||
Serial.print(rfid.serNum[3],DEC);
|
|
||||||
Serial.print(", ");
|
|
||||||
Serial.print(rfid.serNum[4],DEC);
|
|
||||||
Serial.println(" ");
|
|
||||||
|
|
||||||
Serial.print("Hex: ");
|
|
||||||
Serial.print(rfid.serNum[0],HEX);
|
|
||||||
Serial.print(", ");
|
|
||||||
Serial.print(rfid.serNum[1],HEX);
|
|
||||||
Serial.print(", ");
|
|
||||||
Serial.print(rfid.serNum[2],HEX);
|
|
||||||
Serial.print(", ");
|
|
||||||
Serial.print(rfid.serNum[3],HEX);
|
|
||||||
Serial.print(", ");
|
|
||||||
Serial.print(rfid.serNum[4],HEX);
|
|
||||||
Serial.println(" ");
|
|
||||||
} else {
|
|
||||||
/* If we have the same ID, just write a dot. */
|
|
||||||
Serial.print(".");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
rfid.halt();
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
#include <SPI.h>
|
|
||||||
#include <RFID.h>
|
|
||||||
|
|
||||||
RFID rfid(10,5);
|
|
||||||
|
|
||||||
void setup()
|
|
||||||
{
|
|
||||||
Serial.begin(9600);
|
|
||||||
SPI.begin();
|
|
||||||
rfid.init();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
if (rfid.isCard()) {
|
|
||||||
|
|
||||||
Serial.println("IS CARD");
|
|
||||||
|
|
||||||
if (rfid.readCardSerial()) {
|
|
||||||
|
|
||||||
Serial.println(" ");
|
|
||||||
Serial.println("El numero de serie de la tarjeta es : ");
|
|
||||||
Serial.print(rfid.serNum[0],DEC);
|
|
||||||
Serial.print(" , ");
|
|
||||||
Serial.print(rfid.serNum[1],DEC);
|
|
||||||
Serial.print(" , ");
|
|
||||||
Serial.print(rfid.serNum[2],DEC);
|
|
||||||
Serial.print(" , ");
|
|
||||||
Serial.print(rfid.serNum[3],DEC);
|
|
||||||
Serial.print(" , ");
|
|
||||||
Serial.print(rfid.serNum[4],DEC);
|
|
||||||
Serial.println(" ");
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
rfid.halt();
|
|
||||||
}
|
|
||||||
169
keywords.txt
169
keywords.txt
@@ -1,32 +1,161 @@
|
|||||||
#######################################
|
#######################################
|
||||||
# Syntax Coloring Map RFID
|
# Syntax Coloring Map for library MFRC522
|
||||||
#######################################
|
#######################################
|
||||||
|
|
||||||
#######################################
|
#######################################
|
||||||
# Datatypes (KEYWORD1)
|
# KEYWORD1 Classes, datatypes, and C++ keywords
|
||||||
#######################################
|
#######################################
|
||||||
|
MFRC522 KEYWORD1
|
||||||
rfid KEYWORD1
|
PCD_Register KEYWORD1
|
||||||
|
PCD_Command KEYWORD1
|
||||||
|
PICC_Command KEYWORD1
|
||||||
|
MIFARE_Misc KEYWORD1
|
||||||
|
PICC_Type KEYWORD1
|
||||||
|
StatusCode KEYWORD
|
||||||
|
Uid KEYWORD
|
||||||
|
MIFARE_Key KEYWORD
|
||||||
|
|
||||||
#######################################
|
#######################################
|
||||||
# Methods and Functions (KEYWORD2)
|
# KEYWORD2 Methods and functions
|
||||||
#######################################
|
#######################################
|
||||||
init KEYWORD2
|
setSPIConfig KEYWORD2
|
||||||
reset KEYWORD2
|
PCD_WriteRegister KEYWORD2
|
||||||
writeMFRC522 KEYWORD2
|
PCD_WriteRegister KEYWORD2
|
||||||
antennaOn KEYWORD2
|
PCD_ReadRegister KEYWORD2
|
||||||
readMFRC522 KEYWORD2
|
PCD_ReadRegister KEYWORD2
|
||||||
setBitMask KEYWORD2
|
setBitMask KEYWORD2
|
||||||
clearBitMask KEYWORD2
|
PCD_SetRegisterBitMask KEYWORD2
|
||||||
calculateCRC KEYWORD2
|
PCD_ClearRegisterBitMask KEYWORD2
|
||||||
MFRC522ToCard KEYWORD2
|
PCD_CalculateCRC KEYWORD2
|
||||||
MFRC522Anticoll KEYWORD2
|
PCD_Init KEYWORD2
|
||||||
MFRC522Auth KEYWORD2
|
PCD_Reset KEYWORD2
|
||||||
MFRC522Read KEYWORD2
|
PCD_AntennaOn KEYWORD2
|
||||||
MFRC522Write KEYWORD2
|
PCD_TransceiveData KEYWORD2
|
||||||
MFRC522Halt KEYWORD2
|
PCD_CommunicateWithPICC KEYWORD2
|
||||||
|
PICC_RequestA KEYWORD2
|
||||||
|
PICC_WakeupA KEYWORD2
|
||||||
|
PICC_REQA_or_WUPA KEYWORD2
|
||||||
|
PICC_Select KEYWORD2
|
||||||
|
PICC_HaltA KEYWORD2
|
||||||
|
PCD_Authenticate KEYWORD2
|
||||||
|
PCD_StopCrypto1 KEYWORD2
|
||||||
|
MIFARE_Read KEYWORD2
|
||||||
|
MIFARE_Write KEYWORD2
|
||||||
|
MIFARE_Increment KEYWORD2
|
||||||
|
MIFARE_Ultralight_Write KEYWORD2
|
||||||
|
PCD_MIFARE_Transceive KEYWORD2
|
||||||
|
PICC_GetType KEYWORD2
|
||||||
|
PICC_DumpToSerial KEYWORD2
|
||||||
|
PICC_DumpMifareClassicToSerial KEYWORD2
|
||||||
|
PICC_DumpMifareClassicSectorToSerial KEYWORD2
|
||||||
|
PICC_DumpMifareUltralightToSerial KEYWORD2
|
||||||
|
MIFARE_SetAccessBits KEYWORD2
|
||||||
|
PICC_IsNewCardPresent KEYWORD2
|
||||||
|
PICC_ReadCardSerial KEYWORD2
|
||||||
|
|
||||||
#######################################
|
#######################################
|
||||||
# Constants (LITERAL1)
|
# KEYWORD3 setup and loop functions, as well as the Serial keywords
|
||||||
#######################################
|
#######################################
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
LITERAL1 Constants
|
||||||
|
#######################################
|
||||||
|
CommandReg LITERAL1
|
||||||
|
ComIEnReg LITERAL1
|
||||||
|
DivIEnReg LITERAL1
|
||||||
|
ComIrqReg LITERAL1
|
||||||
|
DivIrqReg LITERAL1
|
||||||
|
ErrorReg LITERAL1
|
||||||
|
Status1Reg LITERAL1
|
||||||
|
Status2Reg LITERAL1
|
||||||
|
FIFODataReg LITERAL1
|
||||||
|
FIFOLevelReg LITERAL1
|
||||||
|
WaterLevelReg LITERAL1
|
||||||
|
ControlReg LITERAL1
|
||||||
|
BitFramingReg LITERAL1
|
||||||
|
CollReg LITERAL1
|
||||||
|
ModeReg LITERAL1
|
||||||
|
TxModeReg LITERAL1
|
||||||
|
RxModeReg LITERAL1
|
||||||
|
TxControlReg LITERAL1
|
||||||
|
TxASKReg LITERAL1
|
||||||
|
TxSelReg LITERAL1
|
||||||
|
RxSelReg LITERAL1
|
||||||
|
RxThresholdReg LITERAL1
|
||||||
|
DemodReg LITERAL1
|
||||||
|
MfTxReg LITERAL1
|
||||||
|
MfRxReg LITERAL1
|
||||||
|
SerialSpeedReg LITERAL1
|
||||||
|
CRCResultRegH LITERAL1
|
||||||
|
CRCResultRegL LITERAL1
|
||||||
|
ModWidthReg LITERAL1
|
||||||
|
RFCfgReg LITERAL1
|
||||||
|
GsNReg LITERAL1
|
||||||
|
CWGsPReg LITERAL1
|
||||||
|
ModGsPReg LITERAL1
|
||||||
|
TModeReg LITERAL1
|
||||||
|
TPrescalerReg LITERAL1
|
||||||
|
TReloadRegH LITERAL1
|
||||||
|
TReloadRegL LITERAL1
|
||||||
|
TCounterValueRegH LITERAL1
|
||||||
|
TCounterValueRegL LITERAL1
|
||||||
|
TestSel1Reg LITERAL1
|
||||||
|
TestSel2Reg LITERAL1
|
||||||
|
TestPinEnReg LITERAL1
|
||||||
|
TestPinValueReg LITERAL1
|
||||||
|
TestBusReg LITERAL1
|
||||||
|
AutoTestReg LITERAL1
|
||||||
|
VersionReg LITERAL1
|
||||||
|
AnalogTestReg LITERAL1
|
||||||
|
TestDAC1Reg LITERAL1
|
||||||
|
TestDAC2Reg LITERAL1
|
||||||
|
TestADCReg LITERAL1
|
||||||
|
PCD_Idle LITERAL1
|
||||||
|
PCD_Mem LITERAL1
|
||||||
|
PCD_GenerateRandomID LITERAL1
|
||||||
|
PCD_CalcCRC LITERAL1
|
||||||
|
PCD_Transmit LITERAL1
|
||||||
|
PCD_NoCmdChange LITERAL1
|
||||||
|
PCD_Receive LITERAL1
|
||||||
|
PCD_Transceive LITERAL1
|
||||||
|
PCD_MFAuthent LITERAL1
|
||||||
|
PCD_SoftReset LITERAL1
|
||||||
|
PICC_CMD_REQA LITERAL1
|
||||||
|
PICC_CMD_WUPA LITERAL1
|
||||||
|
PICC_CMD_CT LITERAL1
|
||||||
|
PICC_CMD_SEL_CL1 LITERAL1
|
||||||
|
PICC_CMD_SEL_CL2 LITERAL1
|
||||||
|
PICC_CMD_SEL_CL3 LITERAL1
|
||||||
|
PICC_CMD_HLTA LITERAL1
|
||||||
|
PICC_CMD_MF_AUTH_KEY_A LITERAL1
|
||||||
|
PICC_CMD_MF_AUTH_KEY_B LITERAL1
|
||||||
|
PICC_CMD_MF_READ LITERAL1
|
||||||
|
PICC_CMD_MF_WRITE LITERAL1
|
||||||
|
PICC_CMD_MF_DECREMENT LITERAL1
|
||||||
|
PICC_CMD_MF_INCREMENT LITERAL1
|
||||||
|
PICC_CMD_MF_RESTORE LITERAL1
|
||||||
|
PICC_CMD_MF_TRANSFER LITERAL1
|
||||||
|
PICC_CMD_UL_WRITE LITERAL1
|
||||||
|
MF_ACK LITERAL1
|
||||||
|
MF_KEY_SIZE LITERAL1
|
||||||
|
PICC_TYPE_UNKNOWN LITERAL1
|
||||||
|
PICC_TYPE_ISO_14443_4 LITERAL1
|
||||||
|
PICC_TYPE_ISO_18092 LITERAL1
|
||||||
|
PICC_TYPE_MIFARE_MINI LITERAL1
|
||||||
|
PICC_TYPE_MIFARE_1K LITERAL1
|
||||||
|
PICC_TYPE_MIFARE_4K LITERAL1
|
||||||
|
PICC_TYPE_MIFARE_UL LITERAL1
|
||||||
|
PICC_TYPE_MIFARE_PLUS LITERAL1
|
||||||
|
PICC_TYPE_TNP3XXX LITERAL1
|
||||||
|
PICC_TYPE_NOT_COMPLETE LITERAL1
|
||||||
|
STATUS_OK LITERAL1
|
||||||
|
STATUS_ERROR LITERAL1
|
||||||
|
STATUS_COLLISION LITERAL1
|
||||||
|
STATUS_TIMEOUT LITERAL1
|
||||||
|
STATUS_NO_ROOM LITERAL1
|
||||||
|
STATUS_INTERNAL_ERROR LITERAL1
|
||||||
|
STATUS_INVALID LITERAL1
|
||||||
|
STATUS_CRC_WRONG LITERAL1
|
||||||
|
STATUS_MIFARE_NACK LITERAL1
|
||||||
|
FIFO_SIZE LITERAL1
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user