Major refactoring performed. Committed now to have an offsite backup ;-)

This commit is contained in:
Søren Thing Andersen
2013-10-19 00:27:20 +02:00
parent 9fce44f584
commit cc1294aa79
5 changed files with 1688 additions and 636 deletions

1365
MFRC522.cpp Normal file

File diff suppressed because it is too large Load Diff

319
MFRC522.h Normal file
View File

@@ -0,0 +1,319 @@
/**
* 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.
*
* 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 (MF0ICUC):
* 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.
PICC_CMD_AUTH_KEY_A = 0x60, // Perform authentication with Key A
PICC_CMD_AUTH_KEY_B = 0x61, // Perform authentication with Key B
PICC_CMD_READ = 0x30, // Reads one 16 byte block from the authenticated sector of the PICC. Also used for MIFARE Ultralight.
PICC_CMD_WRITE = 0xA0, // Writes one 16 byte block to the authenticated sector of the PICC. Called "COMPATIBILITY WRITE" for MIFARE Ultralight.
PICC_CMD_DECREMENT = 0xC0, // Decrements the contents of a block and stores the result in the internal data register.
PICC_CMD_INCREMENT = 0xC1, // Increments the contents of a block and stores the result in the internal data register.
PICC_CMD_RESTORE = 0xC2, // Reads the contents of a block into the internal data register.
PICC_CMD_TRANSFER = 0xB0 // Writes the contents of the internal data register to a block.
};
// 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 PICC_Read(byte blockAddr, byte *buffer, byte *bufferSize);
byte PICC_Write(byte blockAddr, byte *buffer, byte bufferSize);
/*
XXX Hvad med ultralight write/compatibility write()????
*/
byte PICC_Decrement(byte blockAddr, long delta);
byte PICC_Increment(byte blockAddr, long delta);
byte PICC_Restore(byte blockAddr);
byte PICC_Transfer(byte blockAddr);
/////////////////////////////////////////////////////////////////////////////////////
// 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 PICC_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 PICC_TwoStepHelper(byte command, byte blockAddr, long data);
};
#endif

485
RFID.cpp
View File

@@ -1,485 +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]);
status = MFRC522ToCard(PCD_TRANSCEIVE, buff, 4, buff,&unLen);
}

151
RFID.h
View File

@@ -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

4
changes.txt Normal file
View File

@@ -0,0 +1,4 @@
Renamed library from RFID to MFRC522 (RFID seemed to generic).
Register names changed to comply with datasheet.
Global defines moved into class.