char MFRC522_Write( char blockAddr, char *writeData ) { char _status; unsigned recvBits; char i; char buff[18]; buff[0] = PICC_WRITE; buff[1] = blockAddr; MFRC522_CRC( buff, 2, &buff[2] ); _status = MFRC522_ToCard( 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++ ) //Data to the FIFO write 16Byte { buff[i] = writeData[i]; } MFRC522_CRC( buff, 16, &buff[16] ); _status = MFRC522_ToCard( PCD_TRANSCEIVE, buff, 18, buff, &recvBits ); if ( (_status != MI_OK) || (recvBits != 4) || ( (buff[0] & 0x0F) != 0x0A ) ) { _status = MI_ERR; } } return _status; }
/************************************************************************************************** * @fn MFRC522_WriteToBlock * @brief 写块数据 * @param BlockAddr -> 块地址(共0-63块),其中块0放了厂商的初始数据,包括卡号等。 WriteData -> 向块写16字节数据 * @return 成功返回 -> MI_OK ************************************************************************************************** */ uint8_t MFRC522_WriteToBlock(uint8_t BlockAddr, uint8_t *WriteData) { uint8_t Buff[18]; uint8_t i; uint8_t Status; uint32_t Bits; Buff[0] = PICC_WRITE; Buff[1] = BlockAddr; MFRC522_CrcCheck(Buff,2,&Buff[2]); Status = MFRC522_ToCard(PCD_TRANSCEIVE, Buff, 4, Buff, &Bits); if((Status != MFRC522_OK) || (Bits != 4) || ((Buff[0] & 0x0F) != 0x0A)) { Status = MFRC522_ERR; } if(Status == MFRC522_OK) { for(i=0; i<16; i++) { Buff[i] = WriteData[i]; } MFRC522_CrcCheck(Buff,16,&Buff[16]); Status = MFRC522_ToCard(PCD_TRANSCEIVE, Buff, 18, Buff, &Bits); if((Status != MFRC522_OK) || (Bits != 4) || ((Buff[0] & 0x0F) != 0x0A)) { Status = MFRC522_ERR; } } return Status; }
uint8_t MFRC522_Write(uint8_t blockAddr, uint8_t *writeData) { uint8_t status; uint32_t recvBits; uint8_t i; uint8_t buff[18]; buff[0] = PICC_WRITE; buff[1] = blockAddr; CalulateCRC(buff, 2, &buff[2]); status = MFRC522_ToCard(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++) { buff[i] = *(writeData+i); } CalulateCRC(buff, 16, &buff[16]); status = MFRC522_ToCard(PCD_TRANSCEIVE, buff, 18, buff, &recvBits); if ((status != MI_OK) || (recvBits != 4) || ((buff[0] & 0x0F) != 0x0A)) { status = MI_ERR; } } return status; }
uchar RFID::MFRC522_Write(uchar blockAddr, uchar *writeData) { uchar status; uint recvBits; uchar i; uchar buff[18]; buff[0] = PICC_WRITE; buff[1] = blockAddr; CalulateCRC(buff, 2, &buff[2]); status = MFRC522_ToCard(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?? { buff[i] = *(writeData+i); } CalulateCRC(buff, 16, &buff[16]); status = MFRC522_ToCard(PCD_TRANSCEIVE, buff, 18, buff, &recvBits); if ((status != MI_OK) || (recvBits != 4) || ((buff[0] & 0x0F) != 0x0A)) { status = MI_ERR; } } return status; }
/************************************************************************************************** * @fn MFRC522_Auth * @brief 验证卡片密码 * @param authMode -> 密码验证模式 0x60 = 验证A密钥 0x61 = 验证B密钥 BlockAddr -> 块地址(共0-63块),其中块0放了厂商的初始数据,包括卡号等。 SectorKey -> 扇区密码 6byte CardData -> 卡号,4Byte * @return 成功返回 -> MI_OK ************************************************************************************************** */ uint8_t MFRC522_Auth(uint8_t AuthMode, uint8_t BlockAddr, uint8_t *SectorKey, uint8_t *CardData) { uint8_t Buff[12]; uint8_t i; uint32_t Bits; uint8_t Status = MFRC522_ERR; /*验证指令1byte+块地址1byte+扇区密码6byte+卡序列号4byte*/ 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] = CardData[i]; } Status = MFRC522_ToCard(PCD_AUTHENT, Buff, 12, Buff, &Bits); if ((Status != MFRC522_OK) || (!(MFRC522_Read(Status2Reg) & 0x08))) //BIT3=MFCryptolOn(1) 成功执行Auth后才会置1,读写模式时使用 { Status = MFRC522_ERR; } return Status; }
/************************************************************************************************** * @fn MFRC522_SelectTag * @brief 选卡,读取卡存储器容量 * @param CardData -> 卡号数据 * @return Size -> 卡号容量 ************************************************************************************************** */ uint8_t MFRC522_SelectTag(uint8_t *CardData) { uint8_t Buff[9]; uint8_t i; uint32_t Bits; uint8_t Size; uint8_t Status = MFRC522_ERR; MFRC522_ClearBitMask(Status2Reg,0x08); //BIT3=MFCrypto1On(1) Buff[0] = PICC_SElECTTAG; Buff[1] = 0x70; //? for(i=0; i<5; i++) { Buff[i+2] = CardData[i]; } MFRC522_CrcCheck(Buff,7,&Buff[7]); //buff[7] buff[8]表明CRC校验的低八位和高八位 Status = MFRC522_ToCard(PCD_TRANSCEIVE, Buff, 9, Buff, &Bits); if((Status == MFRC522_OK) && (Bits == 0x18)) //0x18 = 24 24/8 =3Byte { Size = Buff[0]; } else { Size = 0; } return Size; }
/************************************************************************************************** * @fn MFRC522_Anticoll * @brief RC522和ISO14443卡通讯 * @param CardData -> 卡号数据 * @return ReqStatus -> 寻卡结果 ************************************************************************************************** */ uint8_t MFRC522_Anticoll(uint8_t *CardData) { uint8_t i; uint8_t AntiStatus = MFRC522_ERR; uint32_t AntiBits; //接收到的数据有效位数 uint8_t CardDataCheck = 0; MFRC522_ClearBitMask(Status2Reg,0x08); //关闭内部温度传感器 MFRC522_Write(BitFramingReg,0x00); //BIT2-BIT0=TxLastBists(000) 定义发送的最后一个字节的位数,000表示最后一个字节的所有位都应发送 MFRC522_ClearBitMask(CollReg,0x80); //BIT7=ValuesAfterColl(1) CardData[0] = PICC_ANTICOLL; CardData[1] = 0x20; AntiStatus = MFRC522_ToCard(PCD_TRANSCEIVE,CardData,2,CardData,&AntiBits); if(AntiStatus == MFRC522_OK) { /*校验卡号*/ for(i=0; i<4; i++) { CardDataCheck ^= CardData[i]; //1.datasheet/RFID/MifareCard/1.M1简单介绍,4byte卡号 1byte校验字 } if(CardDataCheck != CardData[i]) { AntiStatus = MFRC522_ERR; } } MFRC522_SetBitMask(CollReg,0x80); //BIT7=ValuesAfterColl(1) return AntiStatus; }
char MFRC522_Auth( char authMode, char BlockAddr, char *Sectorkey, char *serNum ) { char _status; unsigned recvBits; char i; char buff[12]; //Verify the command block address + sector + password + card serial number buff[0] = authMode; buff[1] = BlockAddr; for ( i = 2; i < 8; i++ ) { buff[i] = Sectorkey[i-2]; } for ( i = 8; i < 12; i++ ) { buff[i] = serNum[i-8]; } _status = MFRC522_ToCard( PCD_AUTHENT, buff, 12, buff, &recvBits ); if ( ( _status != MI_OK ) || !( MFRC522_Rd( STATUS2REG ) & 0x08 ) ) { _status = MI_ERR; } return _status; }
uchar RFID::MFRC522_SelectTag(uchar *serNum) { uchar i; uchar status; uchar size; uint recvBits; uchar buffer[9]; //ClearBitMask(Status2Reg, 0x08); //MFCrypto1On=0 buffer[0] = PICC_SElECTTAG; buffer[1] = 0x70; for (i=0; i<5; i++) { buffer[i+2] = *(serNum+i); } CalulateCRC(buffer, 7, &buffer[7]); //?? status = MFRC522_ToCard(PCD_TRANSCEIVE, buffer, 9, buffer, &recvBits); if ((status == MI_OK) && (recvBits == 0x18)) { size = buffer[0]; } else { size = 0; } return size; }
char MFRC522_SelectTag( char *serNum ) { char i; char _status; char size; unsigned recvBits; char buffer[9]; //MFRC522_Clear_Bit( STATUS2REG, 0x08 ); //MFCrypto1On=0 buffer[0] = PICC_SElECTTAG; buffer[1] = 0x70; for ( i=2; i < 7; i++ ) { buffer[i] = *serNum++; } MFRC522_CRC( buffer, 7, &buffer[7] ); _status = MFRC522_ToCard( PCD_TRANSCEIVE, buffer, 9, buffer, &recvBits ); if ( (_status == MI_OK) && (recvBits == 0x18) ) { size = buffer[0]; } else { size = 0; } return size; }
uchar RFID::MFRC522_Auth(uchar authMode, uchar BlockAddr, uchar *Sectorkey, uchar *serNum) { uchar status; uint recvBits; uchar i; uchar buff[12]; //????+???+????+???? 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 = MFRC522_ToCard(PCD_AUTHENT, buff, 12, buff, &recvBits); if ((status != MI_OK) || (!(Read_MFRC522(Status2Reg) & 0x08))) { status = MI_ERR; } return status; }
uchar RFID::MFRC522_Anticoll(uchar *serNum) { uchar status; uchar i; uchar serNumCheck=0; uint unLen; //ClearBitMask(Status2Reg, 0x08); //TempSensclear //ClearBitMask(CollReg,0x80); //ValuesAfterColl Write_MFRC522(BitFramingReg, 0x00); //TxLastBists = BitFramingReg[2..0] serNum[0] = PICC_ANTICOLL; serNum[1] = 0x20; status = MFRC522_ToCard(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; }
char MFRC522_AntiColl( char *serNum ) { char _status; char i; char serNumCheck = 0; unsigned unLen; MFRC522_Wr( BITFRAMINGREG, 0x00 ); //TxLastBists = BitFramingReg[2..0] serNum[0] = PICC_ANTICOLL; serNum[1] = 0x20; MFRC522_Clear_Bit( STATUS2REG, 0x08 ); _status = MFRC522_ToCard( PCD_TRANSCEIVE, serNum, 2, serNum, &unLen ); if (_status == MI_OK) { for ( i=0; i < 4; i++ ) { serNumCheck ^= serNum[i]; } if ( serNumCheck != serNum[4] ) { _status = MI_ERR; } } return _status; }
/* * Function name: MFRC522_Auth * Description: Verify card password * Input: authMode - password authentication mode 0x60 = Verify A key 0x61 = B key validation BlockAddr - block address Sectorkey - Sector password serNum - card serial number, 4 bytes * Return value: the successful return MI_OK */ uchar MFRC522_Auth(uchar authMode, uchar BlockAddr, uchar *Sectorkey, uchar *serNum) { uchar status; uint recvBits; uchar i; uchar buff[12]; // Verify command + block address + sectors password + card serial number 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 = MFRC522_ToCard(PCD_AUTHENT, buff, 12, buff, &recvBits); if ((status != MI_OK) || (!(Read_MFRC522(Status2Reg) & 0x08))) { status = MI_ERR; } return status; }
uint8_t MFRC522_Auth(uint8_t authMode, uint8_t BlockAddr, uint8_t *Sectorkey, uint8_t *serNum) { uint8_t status; uint32_t recvBits; uint8_t i; uint8_t buff[12]; 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 = MFRC522_ToCard(PCD_AUTHENT, buff, 12, buff, &recvBits); if ((status != MI_OK) || (!(Read_MFRC522(Status2Reg) & 0x08))) { status = MI_ERR; } return status; }
void MFRC522_Halt(void) { uint16_t unLen; uint8_t buff[4]; buff[0] = PICC_HALT; buff[1] = 0; MFRC522_CalculateCRC(buff, 2, &buff[2]); MFRC522_ToCard(PCD_TRANSCEIVE, buff, 4, buff, &unLen); }
uint8_t MFRC522_Request(uint8_t reqMode, uint8_t * TagType) { uint8_t status; uint16_t backBits; // The received data bits MFRC522_WriteRegister(MFRC522_REG_BIT_FRAMING, 0x07); // TxLastBists = BitFramingReg[2..0] TagType[0] = reqMode; status = MFRC522_ToCard(PCD_TRANSCEIVE, TagType, 1, TagType, &backBits); if ((status != MI_OK) || (backBits != 0x10)) status = MI_ERR; return status; }
void RFID::MFRC522_Halt(void) { uchar status; uint unLen; uchar buff[4]; buff[0] = PICC_HALT; buff[1] = 0; CalulateCRC(buff, 2, &buff[2]); status = MFRC522_ToCard(PCD_TRANSCEIVE, buff, 4, buff,&unLen); }
//hibernation void MFRC522_Halt() { unsigned unLen; char buff[4]; buff[0] = PICC_HALT; buff[1] = 0; MFRC522_CRC( buff, 2, &buff[2] ); MFRC522_Clear_Bit( STATUS2REG, 0x80 ); MFRC522_ToCard( PCD_TRANSCEIVE, buff, 4, buff, &unLen ); MFRC522_Clear_Bit( STATUS2REG, 0x08 ); }
void MFRC522_Halt(void) { uint8_t status; uint32_t unLen; uint8_t buff[4]; buff[0] = PICC_HALT; buff[1] = 0; CalulateCRC(buff, 2, &buff[2]); status = MFRC522_ToCard(PCD_TRANSCEIVE, buff, 4, buff,&unLen); }
uint8_t MFRC522_Write(uint8_t blockAddr, uint8_t * writeData) { uint8_t status; uint16_t recvBits; uint8_t i; uint8_t buff[18]; buff[0] = PICC_WRITE; buff[1] = blockAddr; MFRC522_CalculateCRC(buff, 2, &buff[2]); status = MFRC522_ToCard(PCD_TRANSCEIVE, buff, 4, buff, &recvBits); if ((status != MI_OK) || (recvBits != 4) || ((buff[0] & 0x0F) != 0x0A)) status = MI_ERR; if (status == MI_OK) { // Data to the FIFO write 16Byte for (i = 0; i < 16; i++) buff[i] = *(writeData+i); MFRC522_CalculateCRC(buff, 16, &buff[16]); status = MFRC522_ToCard(PCD_TRANSCEIVE, buff, 18, buff, &recvBits); if ((status != MI_OK) || (recvBits != 4) || ((buff[0] & 0x0F) != 0x0A)) status = MI_ERR; } return status; }
uint8_t MFRC522_Read(uint8_t blockAddr, uint8_t * recvData) { uint8_t status; uint16_t unLen; recvData[0] = PICC_READ; recvData[1] = blockAddr; MFRC522_CalculateCRC(recvData,2, &recvData[2]); status = MFRC522_ToCard(PCD_TRANSCEIVE, recvData, 4, recvData, &unLen); if ((status != MI_OK) || (unLen != 0x90)) status = MI_ERR; return status; }
uchar Desfire_get_version(void) { uchar status; uchar i; uchar serNumCheck=0; uint unLen; //ClearBitMask(Status2Reg, 0x08); //TempSensclear //ClearBitMask(CollReg,0x80); //ValuesAfterColl //Write_MFRC522(BitFramingReg, 0x00); //TxLastBists = BitFramingReg[2..0] serNum[0] = 0x60; status = MFRC522_ToCard(PCD_TRANSCEIVE, serNum, 1, serNum, &unLen); return status; }
char MFRC522_Request( char reqMode, char *TagType ) { char _status; unsigned backBits; //The received data bits MFRC522_Wr( BITFRAMINGREG, 0x07 ); //TxLastBists = BitFramingReg[2..0] ??? TagType[0] = reqMode; _status = MFRC522_ToCard( PCD_TRANSCEIVE, TagType, 1, TagType, &backBits ); if ( (_status != MI_OK) || (backBits != 0x10) ) { _status = MI_ERR; } return _status; }
/************************************************************************************************** * @fn MFRC522_ReadFromBlock * @brief 读块数据 * @param BlockAddr -> 块地址(共0-63块),其中块0放了厂商的初始数据,包括卡号等。 ReadData -> 向块写16字节数据 * @return 成功返回 -> MI_OK ************************************************************************************************** */ uint8_t MFRC522_ReadFromBlock(uint8_t BlockAddr, uint8_t *ReadData) { uint8_t Status; uint32_t Bits; ReadData[0] = PICC_READ; ReadData[1] = BlockAddr; MFRC522_CrcCheck(ReadData, 2, &ReadData[2]); Status = MFRC522_ToCard(PCD_TRANSCEIVE, ReadData, 4, ReadData, &Bits); if((Status != MFRC522_OK) || (Bits != 0x90)) //18*8 = 144 { Status = MFRC522_ERR; } return Status; }
/************************************************************************************************** * @fn MFRC522_Sleep * @brief 命令卡片进入休眠状态 * @param 无 * @return 无 ************************************************************************************************** */ uint8_t MFRC522_Sleep(void) { uint32_t Bits; uint8_t Buff[4]; Buff[0] = PICC_HALT; Buff[1] = 0; MFRC522_CrcCheck(Buff, 2, &Buff[2]); MFRC522_ToCard(PCD_TRANSCEIVE, Buff, 4, Buff, &Bits); return MFRC522_OK; }
uchar RFID::MFRC522_Read(uchar blockAddr, uchar *recvData) { uchar status; uint unLen; recvData[0] = PICC_READ; recvData[1] = blockAddr; CalulateCRC(recvData,2, &recvData[2]); status = MFRC522_ToCard(PCD_TRANSCEIVE, recvData, 4, recvData, &unLen); if ((status != MI_OK) || (unLen != 0x90)) { status = MI_ERR; } return status; }
uchar RFID::MFRC522_Request(uchar reqMode, uchar *TagType) { uchar status; uint backBits; //???????? Write_MFRC522(BitFramingReg, 0x07); //TxLastBists = BitFramingReg[2..0] ??? TagType[0] = reqMode; status = MFRC522_ToCard(PCD_TRANSCEIVE, TagType, 1, TagType, &backBits); if ((status != MI_OK) || (backBits != 0x10)) { status = MI_ERR; } return status; }
uint8_t MFRC522_Auth(uint8_t authMode, uint8_t BlockAddr, uint8_t * Sectorkey, uint8_t * serNum) { uint8_t status; uint16_t recvBits; uint8_t i; uint8_t buff[12]; // Verify the command block address + sector + password + card serial number 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 = MFRC522_ToCard(PCD_AUTHENT, buff, 12, buff, &recvBits); if ((status != MI_OK) || (!(MFRC522_ReadRegister(MFRC522_REG_STATUS2) & 0x08))) status = MI_ERR; return status; }
uint8_t MFRC522_SelectTag(uint8_t * serNum) { uint8_t i; uint8_t status; uint8_t size; uint16_t recvBits; uint8_t buffer[9]; buffer[0] = PICC_SElECTTAG; buffer[1] = 0x70; for (i = 0; i < 5; i++) buffer[i+2] = *(serNum+i); MFRC522_CalculateCRC(buffer, 7, &buffer[7]); //?? status = MFRC522_ToCard(PCD_TRANSCEIVE, buffer, 9, buffer, &recvBits); if ((status == MI_OK) && (recvBits == 0x18)) size = buffer[0]; else size = 0; return size; }