/****************************************************************************** * 函 数 名:calculateCRC * 功能描述:用MF522计算CRC * 输入参数:pIndata--要读数CRC的数据,len--数据长度,pOutData--计算的CRC结果 * 返 回 值:无 ******************************************************************************/ void RFID::calculateCRC(unsigned char *pIndata, unsigned char len, unsigned char *pOutData) { unsigned char i, n; clearBitMask(DivIrqReg, 0x04); //CRCIrq = 0 setBitMask(FIFOLevelReg, 0x80); //清FIFO指针 //Write_MFRC522(CommandReg, PCD_IDLE); //向FIFO中写入数据 for (i=0; i<len; i++) writeMFRC522(FIFODataReg, *(pIndata+i)); writeMFRC522(CommandReg, PCD_CALCCRC); //等待CRC计算完成 i = 0xFF; do { n = readMFRC522(DivIrqReg); i--; } while ((i!=0) && !(n&0x04)); //CRCIrq = 1 //读取CRC计算结果 pOutData[0] = readMFRC522(CRCResultRegL); pOutData[1] = readMFRC522(CRCResultRegM); }
void RFID::calculateCRC(uint8_t *pIndata, uint8_t len, uint8_t *pOutData) { uint8_t 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ó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); }
void RFID::antennaOn(void) { uint8_t temp; temp = readMFRC522(TxControlReg); if (!(temp & 0x03)) { setBitMask(TxControlReg, 0x03); } }
void MFRC522::begin() { digitalWrite(_sad, HIGH); reset(); //Timer: TPrescaler*TreloadVal/6.78MHz = 24ms writeToRegister(TModeReg, 0x8D); // Tauto=1; f(Timer) = 6.78MHz/TPreScaler writeToRegister(TPrescalerReg, 0x3E); // TModeReg[3..0] + TPrescalerReg writeToRegister(TReloadRegL, 30); writeToRegister(TReloadRegH, 0); writeToRegister(TxAutoReg, 0x40); // 100%ASK writeToRegister(ModeReg, 0x3D); // CRC initial value 0x6363 setBitMask(TxControlReg, 0x03); // Turn antenna on. }
void MFRC522::calculateCRC(byte *data, int len, byte *result) { int i; byte n; clearBitMask(DivIrqReg, 0x04); // CRCIrq = 0 setBitMask(FIFOLevelReg, 0x80); // Clear the FIFO pointer //Writing data to the FIFO. for (i = 0; i < len; i++) { writeToRegister(FIFODataReg, data[i]); } writeToRegister(CommandReg, MFRC522_CALCCRC); // Wait for the CRC calculation to complete. i = 0xFF; do { n = readFromRegister(DivIrqReg); i--; } while ((i != 0) && !(n & 0x04)); //CRCIrq = 1 // Read the result from the CRC calculation. result[0] = readFromRegister(CRCResultRegL); result[1] = readFromRegister(CRCResultRegM); }
uint8_t RFID::MFRC522ToCard(uint8_t command, uint8_t *sendData, uint8_t sendLen, uint8_t *backData, uint16_t *backLen) { uint8_t status = MI_ERR; uint8_t irqEn = 0x00; uint8_t waitIRq = 0x00; uint8_t lastBits; uint8_t n; uint16_t i; switch (command) { case PCD_AUTHENT: // Tarjetas de certificación cerca { irqEn = 0x12; waitIRq = 0x10; break; } case PCD_TRANSCEIVE: //La transmisión de datos FIFO { irqEn = 0x77; waitIRq = 0x30; break; } default: break; } writeMFRC522(CommIEnReg, irqEn|0x80); //De solicitud de interrupción clearBitMask(CommIrqReg, 0x80); // Borrar todos los bits de petición de interrupción setBitMask(FIFOLevelReg, 0x80); //FlushBuffer=1, FIFO de inicializació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ó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; }
int MFRC522::commandTag(byte cmd, byte *data, int dlen, byte *result, int *rlen) { int status = MI_ERR; byte irqEn = 0x00; byte waitIRq = 0x00; byte lastBits, n; int i; switch (cmd) { case MFRC522_AUTHENT: irqEn = 0x12; waitIRq = 0x10; break; case MFRC522_TRANSCEIVE: irqEn = 0x77; waitIRq = 0x30; break; default: break; } writeToRegister(CommIEnReg, irqEn|0x80); // interrupt request clearBitMask(CommIrqReg, 0x80); // Clear all interrupt requests bits. setBitMask(FIFOLevelReg, 0x80); // FlushBuffer=1, FIFO initialization. writeToRegister(CommandReg, MFRC522_IDLE); // No action, cancel the current command. // Write to FIFO for (i=0; i < dlen; i++) { writeToRegister(FIFODataReg, data[i]); } // Execute the command. writeToRegister(CommandReg, cmd); if (cmd == MFRC522_TRANSCEIVE) { setBitMask(BitFramingReg, 0x80); // StartSend=1, transmission of data starts } // Waiting for the command to complete so we can receive data. i = 25; // Max wait time is 25ms. do { delay(1); // CommIRqReg[7..0] // Set1 TxIRq RxIRq IdleIRq HiAlerIRq LoAlertIRq ErrIRq TimerIRq n = readFromRegister(CommIrqReg); i--; } while ((i!=0) && !(n&0x01) && !(n&waitIRq)); clearBitMask(BitFramingReg, 0x80); // StartSend=0 if (i != 0) { // Request did not time out. if(!(readFromRegister(ErrorReg) & 0x1D)) { // BufferOvfl Collerr CRCErr ProtocolErr status = MI_OK; if (n & irqEn & 0x01) { status = MI_NOTAGERR; } if (cmd == MFRC522_TRANSCEIVE) { n = readFromRegister(FIFOLevelReg); lastBits = readFromRegister(ControlReg) & 0x07; if (lastBits) { *rlen = (n-1)*8 + lastBits; } else { *rlen = n*8; } if (n == 0) { n = 1; } if (n > MAX_LEN) { n = MAX_LEN; } // Reading the recieved data from FIFO. for (i=0; i<n; i++) { result[i] = readFromRegister(FIFODataReg); } } } else { status = MI_ERR; } } return status; }
/****************************************************************************** * 函 数 名:MFRC522ToCard * 功能描述:RC522和ISO14443卡通讯 * 输入参数:command--MF522命令字, * sendData--通过RC522发送到卡片的数据, * sendLen--发送的数据长度 * backData--接收到的卡片返回数据, * backLen--返回数据的位长度 * 返 回 值:成功返回MI_OK ******************************************************************************/ 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: //认证卡密 { irqEn = 0x12; waitIRq = 0x10; break; } case PCD_TRANSCEIVE: //发送FIFO中数据 { irqEn = 0x77; waitIRq = 0x30; break; } default: break; } writeMFRC522(CommIEnReg, irqEn|0x80); //允许中断请求 clearBitMask(CommIrqReg, 0x80); //清除所有中断请求位 setBitMask(FIFOLevelReg, 0x80); //FlushBuffer=1, FIFO初始化 writeMFRC522(CommandReg, PCD_IDLE); //无动作,取消当前命令 //向FIFO中写入数据 for (i=0; i<sendLen; i++) writeMFRC522(FIFODataReg, sendData[i]); //执行命令 writeMFRC522(CommandReg, command); if (command == PCD_TRANSCEIVE) setBitMask(BitFramingReg, 0x80); //StartSend=1,transmission of data starts //等待接收数据完成 i = 2000; //i根据时钟频率调整,操作M1卡最大等待时间25ms 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中接收到的数据 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; }