uint8_t MFRC522_Request(uint8_t reqMode, uint8_t *TagType) { uint8_t status; uint32_t 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; }
/* Function: void MFRC522_Init(void) Description: Initialize all Hardware & Software configuration in RFID Reader. */ void MFRC522_Init(void) { MFRC522_Reset(); Write_MFRC522(TModeReg, 0x8D); //Tauto=1; f(Timer) = 6.78MHz/TPreScaler Write_MFRC522(TPrescalerReg, 0x3E); //TModeReg[3..0] + TPrescalerReg Write_MFRC522(TReloadRegL, 30); Write_MFRC522(TReloadRegH, 0); Write_MFRC522(TxAutoReg, 0x40); //100%ASK Write_MFRC522(ModeReg, 0x3D); //CRC初始值0x6363 ??? AntennaOn(); //打开天线 }
void RFID::MFRC522_Init(void) { digitalWrite(NRSTPD,HIGH); MFRC522_Reset(); Write_MFRC522(TModeReg, 0x8D); //Tauto=1; f(Timer) = 6.78MHz/TPreScaler Write_MFRC522(TPrescalerReg, 0x3E); //TModeReg[3..0] + TPrescalerReg Write_MFRC522(TReloadRegL, 30); Write_MFRC522(TReloadRegH, 0); Write_MFRC522(TxAutoReg, 0x40); //100%ASK Write_MFRC522(ModeReg, 0x3D); //CRC???0x6363 ??? AntennaOn(); //???? }
uchar RFID::MFRC522_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: //???? { irqEn = 0x12; waitIRq = 0x10; break; } case PCD_TRANSCEIVE: //??FIFO??? { irqEn = 0x77; waitIRq = 0x30; break; } default: break; } Write_MFRC522(CommIEnReg, irqEn|0x80); //?????? ClearBitMask(CommIrqReg, 0x80); //????????? SetBitMask(FIFOLevelReg, 0x80); //FlushBuffer=1, FIFO??? Write_MFRC522(CommandReg, PCD_IDLE); //NO action;?????? ??? //?FIFO????? for (i=0; i<sendLen; i++) { Write_MFRC522(FIFODataReg, sendData[i]); } //???? Write_MFRC522(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 = Read_MFRC522(CommIrqReg); i--; } while ((i!=0) && !(n&0x01) && !(n&waitIRq)); ClearBitMask(BitFramingReg, 0x80); //StartSend=0 if (i != 0) { if(!(Read_MFRC522(ErrorReg) & 0x1B)) //BufferOvfl Collerr CRCErr ProtecolErr { status = MI_OK; if (n & irqEn & 0x01) { status = MI_NOTAGERR; //?? } if (command == PCD_TRANSCEIVE) { n = Read_MFRC522(FIFOLevelReg); lastBits = Read_MFRC522(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] = Read_MFRC522(FIFODataReg); } } } else { status = MI_ERR; } } //SetBitMask(ControlReg,0x80); //timer stops //Write_MFRC522(CommandReg, PCD_IDLE); return status; }
void RFID::MFRC522_Reset(void) { Write_MFRC522(CommandReg, PCD_RESETPHASE); }
void RFID::ClearBitMask(uchar reg, uchar mask) { uchar tmp; tmp = Read_MFRC522(reg); Write_MFRC522(reg, tmp & (~mask)); }
void RFID::SetBitMask(uchar reg, uchar mask) { uchar tmp; tmp = Read_MFRC522(reg); Write_MFRC522(reg, tmp | mask); // set bit mask }
void ClearBitMask(uchar reg, uchar mask) { uchar tmp; tmp = Read_MFRC522(reg); Write_MFRC522(reg, tmp & (~mask)); // clear bit mask }
/* * Function name: MFRC522_ToCard * Description: RC522 and ISO14443 card communication * Input Parameters: command - MF522 command word, * SendData - RC522 sent to the card data, * SendLen - length of data sent * BackData - received the card data is returned, * BackLen - returns the bit length of the data * Return value: the successful return MI_OK */ unsigned char MFRC522_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: //Certification card secret { irqEn = 0x12; waitIRq = 0x10; break; } case PCD_TRANSCEIVE: //Transmit FIFO data { irqEn = 0x77; waitIRq = 0x30; break; } default: break; } Write_MFRC522(CommIEnReg, irqEn|0x80); //Interrupt request ClearBitMask(CommIrqReg, 0x80); //Clear all interrupt request bit SetBitMask(FIFOLevelReg, 0x80); //FlushBuffer=1, FIFO Initialization Write_MFRC522(CommandReg, PCD_IDLE); //NO action ??? !!! ATENTION //Data is written to the FIFO for (i=0; i<sendLen; i++) { Write_MFRC522(FIFODataReg, sendData[i]); } //Execute command Write_MFRC522(CommandReg, command); if (command == PCD_TRANSCEIVE) { SetBitMask(BitFramingReg, 0x80); //StartSend=1,transmission of data starts } //Waiting to receive data to complete i = 2000; // i clock frequency adjustment, operating maximum wait time of 25ms ??? do { //CommIrqReg[7..0] //Set1 TxIRq RxIRq IdleIRq HiAlerIRq LoAlertIRq ErrIRq TimerIRq n = Read_MFRC522(CommIrqReg); i--; } while ((i!=0) && !(n&0x01) && !(n&waitIRq)); ClearBitMask(BitFramingReg, 0x80); //StartSend=0 if (i != 0) { if(!(Read_MFRC522(ErrorReg) & 0x1B)) //BufferOvfl Collerr CRCErr ProtecolErr { status = MI_OK; if (n & irqEn & 0x01){ status = MI_NOTAGERR; //?? } if (command == PCD_TRANSCEIVE){ n = Read_MFRC522(FIFOLevelReg); lastBits = Read_MFRC522(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; } // Read the FIFO data received for (i=0; i<n; i++){ backData[i] = Read_MFRC522(FIFODataReg); } } } else{ status = MI_ERR; } } //SetBitMask(ControlReg,0x80); //timer stops //Write_MFRC522(CommandReg, PCD_IDLE); return status; }
void ClearBitMask(uint8_t reg, uint8_t mask) { uint8_t tmp; tmp = Read_MFRC522(reg); Write_MFRC522(reg, tmp & (~mask)); // clear bit mask }
void SetBitMask(uint8_t reg, uint8_t mask) { uint8_t tmp; tmp = Read_MFRC522(reg); Write_MFRC522(reg, tmp | mask); // set bit mask }