#include "RFID.h"
#include <Arduino.h>
//#include "softspi.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C LCD(0x27,16,2);
SOFTSPI softSpi;
void RFID::begin(uchar csnPin, uchar sckPin, uchar mosiPin, uchar misoPin, uchar chipSelectPin, uchar NRSTPD)
{
softSpi.begin(csnPin, sckPin, mosiPin, misoPin);
pinMode(chipSelectPin, OUTPUT);
digitalWrite(chipSelectPin, LOW); //Drive RFID to read mode
pinMode(NRSTPD, OUTPUT);
_chipSelectPin = chipSelectPin;
_NRSTPD = NRSTPD;
}
/**********************************************************
* Function:ShowCardID
* Description:Show Card ID
* Input parameter:ID string
* Return:Null
**********************************************************/
void RFID::showCardID(uchar *id)
{
int IDlen=4;
for(int i=0; i<IDlen; i++){
Serial.print(0x0F & (id[i]>>4), HEX);
LCD.print(0x0F & (id[i]>>4), HEX);
Serial.print(0x0F & id[i],HEX);
LCD.print(0x0F & id[i],HEX);
}
//Serial.println();
//Serial.println();
}
/**********************************************************
* Function:ShowCardType
* Description:Show Card type
* Input parameter:Type string
* Return:Null
**********************************************************/
void RFID::showCardType(uchar* type)
{
Serial.print("Card type: ");
if(type[0]==0x04&&type[1]==0x00)
Serial.println("MFOne-S50");
else if(type[0]==0x02&&type[1]==0x00)
Serial.println("MFOne-S70");
else if(type[0]==0x44&&type[1]==0x00)
Serial.println("MF-UltraLight");
else if(type[0]==0x08&&type[1]==0x00)
Serial.println("MF-Pro");
else if(type[0]==0x44&&type[1]==0x03)
Serial.println("MF Desire");
else
Serial.println("Unknown");
}
/**********************************************************
* Function:Write_MFRC5200
* Description:write a byte data into one register of MR RC522
* Input parameter:addr--register address;val--the value that need to write in
* Return:Null
**********************************************************/
void RFID::writeTo(uchar addr, uchar val)
{
digitalWrite(_chipSelectPin, LOW);
//address format:0XXXXXX0
softSpi.writeByte((addr<<1)&0x7E);
softSpi.writeByte(val);
digitalWrite(_chipSelectPin, HIGH);
}
/**********************************************************
* Function:Read_MFRC522
* Description:read a byte data into one register of MR RC522
* Input parameter:addr--register address
* Return:return the read value
**********************************************************/
uchar RFID::readFrom(uchar addr)
{
uchar val;
digitalWrite(_chipSelectPin, LOW);
//address format:1XXXXXX0
softSpi.writeByte(((addr<<1)&0x7E) | 0x80);
val = softSpi.SPI_RW(0x00);
digitalWrite(_chipSelectPin, HIGH);
return val;
}
/**********************************************************
* Function:SetBitMask
* Description:set RC522 register bit
* Input parameter:reg--register address;mask--value
* Return:null
**********************************************************/
void RFID::setBitMask(uchar reg, uchar mask)
{
uchar tmp;
tmp = readFrom(reg);
writeTo(reg, tmp | mask); // set bit mask
}
/**********************************************************
* Function:ClearBitMask
* Description:clear RC522 register bit
* Input parameter:reg--register address;mask--value
* Return:null
**********************************************************/
void RFID::clearBitMask(uchar reg, uchar mask)
{
uchar tmp;
tmp = readFrom(reg);
writeTo(reg, tmp & (~mask)); // clear bit mask
}
/**********************************************************
* Function:AntennaOn
* Description:Turn on antenna, every time turn on or shut down antenna need at least 1ms delay
* Input parameter:null
* Return:null
* Return:null
**********************************************************/
void RFID::antennaOn(void)
{
uchar temp;
temp = readFrom(TxControlReg);
if (!(temp & 0x03))
{
setBitMask(TxControlReg, 0x03);
}
}
/**********************************************************
* Function:AntennaOff
* Description:Turn off antenna, every time turn on or shut down antenna need at least 1ms delay
* Input parameter:null
* Return:null
**********************************************************/
void RFID::antennaOff(void)
{
clearBitMask(TxControlReg, 0x03);
}
/*
* Function:ResetMFRC522
* Description: reset RC522
* Input parameter:null
* Return:null
*/
void RFID::reset(void)
{
writeTo(CommandReg, PCD_RESETPHASE);
}
/*
* Function:InitMFRC522
* Description:initilize RC522
* Input parameter:null
* Return:null
*/
void RFID::init(void)
{
digitalWrite(_NRSTPD,HIGH);
reset();
//Timer: TPrescaler*TreloadVal/6.78MHz = 24ms
writeTo(TModeReg, 0x8D); //Tauto=1; f(Timer) = 6.78MHz/TPreScaler
writeTo(TPrescalerReg, 0x3E); //TModeReg[3..0] + TPrescalerReg
writeTo(TReloadRegL, 30);
writeTo(TReloadRegH, 0);
writeTo(TxAutoReg, 0x40); //100%ASK
writeTo(ModeReg, 0x3D); //CRC initilizate value 0x6363 ???
//ClearBitMask(Status2Reg, 0x08); //MFCrypto1On=0
//Write_MFRC522(RxSelReg, 0x86); //RxWait = RxSelReg[5..0]
//Write_MFRC522(RFCfgReg, 0x7F); //RxGain = 48dB
antennaOn(); //turn on antenna
}
/*
* Function:MFRC522_Request
* Description:Searching card, read card type
* Input parameter:reqMode--search methods,
* TagType--return card types
* 0x4400 = Mifare_UltraLight
* 0x0400 = Mifare_One(S50)
* 0x0200 = Mifare_One(S70)
* 0x0800 = Mifare_Pro(X)
* 0x4403 = Mifare_DESFire
* return:return MI_OK if successed
*/
uchar RFID::request(uchar reqMode, uchar *TagType)
{
uchar status;
uint backBits; //the data bits that received
writeTo(BitFramingReg, 0x07); //TxLastBists = BitFramingReg[2..0] ???
TagType[0] = reqMode;
status = toCard(PCD_TRANSCEIVE, TagType, 1, TagType, &backBits);
if ((status != MI_OK) || (backBits != 0x10))
{
status = MI_ERR;
}
return status;
}
/*
* Function:MFRC522_ToCard
* Description:communicate between RC522 and ISO14443
* Input parameter:command--MF522 command bits
* sendData--send data to card via rc522
* sendLen--send data length
* backData--the return data from card
* backLen--the length of return data
* return:return MI_OK if successed
*/
uchar RFID::toCard(uchar command, uchar *sendData, uchar sendLen, uchar *backData, uint *backLen)
{
uchar status = MI_ERR;
uchar irqEn = 0x00;
uchar waitIRq = 0x00;
uchar lastBits;
uchar n;
uint i;
switch (command)
{
case PCD_AUTHENT: //verify card password
{
irqEn = 0x12;
waitIRq = 0x10;
break;
}
case PCD_TRANSCEIVE: //send data in the FIFO
{
irqEn = 0x77;
waitIRq = 0x30;
break;
}
default:
break;
}
writeTo(CommIEnReg, irqEn|0x80); //Allow interruption
clearBitMask(CommIrqReg, 0x80); //Clear all the interrupt bits
setBitMask(FIFOLevelReg, 0x80); //FlushBuffer=1, FIFO initilizate
writeTo(CommandReg, PCD_IDLE); //NO action;cancel current command ???
//write data into FIFO
for (i=0; i<sendLen; i++)
{
writeTo(FIFODataReg, sendData[i]);
}
//procceed it
writeTo(CommandReg, command);
if (command == PCD_TRANSCEIVE)
{
setBitMask(BitFramingReg, 0x80); //StartSend=1,transmission of data starts
}
//waite receive data is finished
i = 2000; //i should adjust accord
评论0
最新资源