/****************************************************************************** * 函 数 名: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); }
/** * MFRC522Anticoll -> anticoll * Anti-detecció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 */ uint8_t RFID::anticoll(uint8_t *serNum) { uint8_t status; uint8_t i; uint8_t serNumCheck=0; uint16_t 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; }
/****************************************************************************** * 函 数 名:anticoll * 功能描述:防冲突检测,读取选中卡片的卡序列号 * 输入参数:serNum--返回4字节卡序列号,第5字节为校验字节 * 返 回 值:成功返回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) { //校验卡序列号 for (i=0; i<4; i++) serNumCheck ^= serNum[i]; if (serNumCheck != serNum[i]) status = MI_ERR; } //SetBitMask(CollReg, 0x80); //ValuesAfterColl=1 return status; }
/****************************************************************************** * 函 数 名:MFRC522Request * 功能描述:寻卡,读取卡类型号 * 输入参数:reqMode--寻卡方式, * TagType--返回卡片类型 * 0x4400 = Mifare_UltraLight * 0x0400 = Mifare_One(S50) * 0x0200 = Mifare_One(S70) * 0x0800 = Mifare_Pro(X) * 0x4403 = Mifare_DESFire * 返 回 值:成功返回MI_OK ******************************************************************************/ unsigned char RFID::MFRC522Request(unsigned char reqMode, unsigned char *TagType) { unsigned char status; unsigned int backBits; //接收到的数据位数 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; }
/* * Nombre de la función: MFRC522_Request * Descripción: Buscar las cartas, leer el número de tipo de tarjeta * Los pará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 */ uint8_t RFID::MFRC522Request(uint8_t reqMode, uint8_t *TagType) { uint8_t status; uint16_t backBits; // Recibió 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; }
/****************************************************************************** * 函 数 名:init * 功能描述:初始化RC522 * 输入参数:无 * 返 回 值:无 ******************************************************************************/ 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(); //打开天线 }
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; }
void RFID::clearBitMask(uint8_t reg, uint8_t mask) { uint8_t tmp; tmp = readMFRC522(reg); writeMFRC522(reg, tmp & (~mask)); // clear bit mask }
void RFID::setBitMask(uint8_t reg, uint8_t mask) { uint8_t tmp; tmp = readMFRC522(reg); writeMFRC522(reg, tmp | mask); // set bit mask }
void RFID::reset() { writeMFRC522(CommandReg, PCD_RESETPHASE); }
void RFID::clearBitMask(unsigned char reg, unsigned char mask) { unsigned char tmp; tmp = readMFRC522(reg); writeMFRC522(reg, tmp & (~mask)); // clear bit mask }
void RFID::setBitMask(unsigned char reg, unsigned char mask) { unsigned char tmp; tmp = readMFRC522(reg); writeMFRC522(reg, tmp | mask); // set bit mask }
/****************************************************************************** * 函 数 名: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; }