Beispiel #1
0
/**************************************************************************************************
 * @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;
}
Beispiel #2
0
/**************************************************************************************************
 * @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;
}
Beispiel #3
0
/**************************************************************************************************
 * @fn          MFRC522_CrcCheck
 * @brief       用MF522计算CRC校验
 * @param       CrcData   -> 要校验CRC的数据
                CrcLen    -> 数据长度
                CheckData -> CRC校验结果  
 * @return      Size -> 卡号容量 
 **************************************************************************************************
 */
void MFRC522_CrcCheck(uint8_t *CrcData, uint8_t CrcLen, uint8_t *CheckData)
{
  uint8_t i;
  uint8_t CrcIrq;
  
  MFRC522_ClearBitMask(DivIrqReg,0x04);     //BIT2=CRCIrq(0)
  MFRC522_SetBitMask(FIFOLevelReg,0x80);    //BIT7=FlushBuffer(1), FIFO初始化
  
  /*1.向FIFO中写入数据*/
  for(i=0; i<CrcLen; i++)
  {
    MFRC522_Write(FIFODataReg,CrcData[i]);
  }
  MFRC522_Write(CommandReg,PCD_CALCCRC);    //激活CRC协处理器或执行自测试
  
  /*2.等待CRC校验完成*/
  i = 0xFF;
  do
  {
    CrcIrq = MFRC522_Read(DivIrqReg);       //BIT2=CRCIrq,CRC协处理器在处理完FIFO缓冲区的所有数据后置位CRCIRq标志
    i--;
  }
  while((i!=0)&&!(CrcIrq & 0x04));          //1)i=0超时跳出 2)处理完FIFO缓冲区的所有数据跳出
  
  /*3.读取CRC校验结果*/
  CheckData[0] = MFRC522_Read(CRCResultRegL); 
  CheckData[1] = MFRC522_Read(CRCResultRegM);
  
}
void MFRC522_CalculateCRC(uint8_t *  pIndata, uint8_t len, uint8_t * pOutData) 
{
	uint8_t i, n;

	MFRC522_ClearBitMask(MFRC522_REG_DIV_IRQ, 0x04);													// CRCIrq = 0
	MFRC522_SetBitMask(MFRC522_REG_FIFO_LEVEL, 0x80);													// Clear the FIFO pointer
	// Write_MFRC522(CommandReg, PCD_IDLE);

	// Writing data to the FIFO	
	for (i = 0; i < len; i++) MFRC522_WriteRegister(MFRC522_REG_FIFO_DATA, *(pIndata+i));
	MFRC522_WriteRegister(MFRC522_REG_COMMAND, PCD_CALCCRC);

	// Wait CRC calculation is complete
	i = 0xFF;
	do {
		n = MFRC522_ReadRegister(MFRC522_REG_DIV_IRQ);
		i--;
	} while ((i!=0) && !(n&0x04));																		// CRCIrq = 1

	// Read CRC calculation result
	pOutData[0] = MFRC522_ReadRegister(MFRC522_REG_CRC_RESULT_L);
	pOutData[1] = MFRC522_ReadRegister(MFRC522_REG_CRC_RESULT_M);
}
uint8_t MFRC522_ToCard(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: {
			irqEn = 0x12;
			waitIRq = 0x10;
			break;
		}
		case PCD_TRANSCEIVE: {
			irqEn = 0x77;
			waitIRq = 0x30;
			break;
		}
		default:
		break;
	}

	MFRC522_WriteRegister(MFRC522_REG_COMM_IE_N, irqEn | 0x80);
	MFRC522_ClearBitMask(MFRC522_REG_COMM_IRQ, 0x80);
	MFRC522_SetBitMask(MFRC522_REG_FIFO_LEVEL, 0x80);
	MFRC522_WriteRegister(MFRC522_REG_COMMAND, PCD_IDLE);

	// Writing data to the FIFO
	for (i = 0; i < sendLen; i++) MFRC522_WriteRegister(MFRC522_REG_FIFO_DATA, sendData[i]);

	// Execute the command
	MFRC522_WriteRegister(MFRC522_REG_COMMAND, command);
	if (command == PCD_TRANSCEIVE) MFRC522_SetBitMask(MFRC522_REG_BIT_FRAMING, 0x80);					// StartSend=1,transmission of data starts 

	// Waiting to receive data to complete
	i = 2000;	// i according to the clock frequency adjustment, the operator M1 card maximum waiting time 25ms
	do {
		// CommIrqReg[7..0]
		// Set1 TxIRq RxIRq IdleIRq HiAlerIRq LoAlertIRq ErrIRq TimerIRq
		n = MFRC522_ReadRegister(MFRC522_REG_COMM_IRQ);
		i--;
	} while ((i!=0) && !(n&0x01) && !(n&waitIRq));

	MFRC522_ClearBitMask(MFRC522_REG_BIT_FRAMING, 0x80);												// StartSend=0

	if (i != 0)  {
		if (!(MFRC522_ReadRegister(MFRC522_REG_ERROR) & 0x1B)) {
			status = MI_OK;
			if (n & irqEn & 0x01) status = MI_NOTAGERR;
			if (command == PCD_TRANSCEIVE) {
				n = MFRC522_ReadRegister(MFRC522_REG_FIFO_LEVEL);
				lastBits = MFRC522_ReadRegister(MFRC522_REG_CONTROL) & 0x07;
				if (lastBits) *backLen = (n - 1) * 8 + lastBits; else *backLen = n * 8;
				if (n == 0) n = 1;
				if (n > MFRC522_MAX_LEN) n = MFRC522_MAX_LEN;
				for (i = 0; i < n; i++) backData[i] = MFRC522_ReadRegister(MFRC522_REG_FIFO_DATA);		// Reading the received data in FIFO
			}
		} else status = MI_ERR;
	}
	return status;
}
void MFRC522_AntennaOff(void) 
{
	MFRC522_ClearBitMask(MFRC522_REG_TX_CONTROL, 0x03);
}
Beispiel #7
0
/**************************************************************************************************
 * @fn          MFRC522_AntennaOff
 * @brief       关闭天线
 * @param       n
 * @return      无
 **************************************************************************************************
 */
void MFRC522_AntennaOff(void)
{
  MFRC522_ClearBitMask(TxControlReg, 0x03); //TX2 TX1清0,,关闭了天线
}
Beispiel #8
0
/**************************************************************************************************
 * @fn          MFRC522_ToCard
 * @brief       RC522和ISO14443卡通讯
 * @param       Command       ->  MF522命令字
                SendData      ->  过RC522发送到卡片的数据,
                SendLen       ->  发送的数据长度
                ReceiveData   ->  接收到的卡片返回数据
                ReveiveLen    ->  返回数据的位长度
 * @return      ReqStatus     ->  寻卡结果 
 **************************************************************************************************
 */
uint8_t MFRC522_ToCard(uint8_t Command, uint8_t *SendData, uint8_t SendLen, uint8_t *ReceiveData, uint32_t *ReveiveBits)
{
  uint8_t Status  = MFRC522_ERR; 
  uint8_t IrqEn   = 0x00;
  uint8_t WaitIrq = 0x00; 
  uint32_t i;
  uint8_t IRqStatus;
  uint8_t FIFOLevel  = 0x00;       //FIFOLevelReg BIT6-BIT0 FIFO缓冲区中保存的字节数,BIT7被读时返回为0无须担心
  uint8_t RxLastBits = 0x00;        //ControlReg   BIT2-BIT0 显示最后接收到的字节的有效位数目,如果该位为0,整个字节有效 
    
  switch(Command)
  {
    case PCD_AUTHENT:     IrqEn   = 0x12;    //认证卡密
                          WaitIrq = 0x10;
                          break;
                      
    case PCD_TRANSCEIVE:  IrqEn   = 0x77;    //发送FIFO中数据
                          WaitIrq = 0x30;
                          break;
                        
    default:              break;
  }
  
  MFRC522_Write(CommIEnReg, IrqEn|0x80);     //允许中断请求
  MFRC522_ClearBitMask(CommIrqReg, 0x80);    //清除所有中断请求位
  MFRC522_SetBitMask(FIFOLevelReg, 0x80);    //BIT7=FlushBuffer(1), FIFO初始化
  MFRC522_Write(CommandReg, PCD_IDLE);       //无动作,取消当前动作的执行
  
  /*1.向FIFO中写入数据*/
  for(i=0; i<SendLen; i++)
  {
    MFRC522_Write(FIFODataReg,SendData[i]);
  }
  
  /*2.执行发送命令,将FIFO缓冲区的数据发送到天线,发送完成后自动激活接收器*/
  MFRC522_Write(CommandReg,Command);
  if((Command == PCD_TRANSCEIVE) ||(Command == PCD_AUTHENT))               
  { 
    MFRC522_SetBitMask(BitFramingReg,0x80);   //BIT7=StartSend(1):启动数据发送
  }
  
  /*3.等待数据接收完成*/
  i = 2000;                                   //i根据时钟频率调整,操作M1卡最大等待时间15ms?注意上面MFRC522_Reset函数中定时器的设置时间是15ms
  do
  {
    IRqStatus = MFRC522_Read(CommIrqReg);
    i--;
  }
  while((i!=0)&& !(IRqStatus&0x01) && !(IRqStatus&WaitIrq)); //1)i=0超时跳出 2)定时器(15ms)减到0时跳出 3)命令终止或者接收完成跳出
  MFRC522_ClearBitMask(BitFramingReg,0x80);   //停止数据发送
  
  
  /*4.检测寻卡状态*/
  if(i != 0)
  {
    /*4.1.寻卡未发生错误*/
    if(!(MFRC522_Read(ErrorReg) & 0x1B))      //未发生以下错误:BufferOvfl Collerr CRCErr ProtecolErr
    {
      Status = MFRC522_OK;                    //这里可以设一个断点查看,表明未发生错误
      
      if(IRqStatus & IrqEn & 0x01)            //定时器递减到零,寻卡超时
      {
        Status = MFRC522_TIMEOUT;              
      }
      
       /*4.1.1 接收到的数据处理*/
      if(Command == PCD_TRANSCEIVE)                     
      {
         FIFOLevel  = MFRC522_Read(FIFOLevelReg);       //接收到的字节数
         RxLastBits = MFRC522_Read(ControlReg) & 0x07;  //最后一个字节的有效数
         
         if(RxLastBits)                                 //如果不是0,最后一个字节并不是整个字节有效
         {
           *ReveiveBits = (FIFOLevel-1)*8 + RxLastBits;  
         }
         else                                           //如果是0,整个字节有效
         {
           *ReveiveBits = FIFOLevel * 8;
         }
         if(FIFOLevel == 0)
         {
           FIFOLevel = 1;
         }
         if(FIFOLevel > FIFOLEVEL_MAX)
         {
           FIFOLevel = FIFOLEVEL_MAX;
         }
         
         /*4.1.2 读取接收到的数据*/
         for(i=0; i<FIFOLevel; i++)
         {
           ReceiveData[i] = MFRC522_Read(FIFODataReg);    //读取FIFO缓冲区的数据
         }
      }
    }
    
    /*4.2.寻卡发生错误,错误类型未知*/
    else                                                  
    {
      Status = MFRC522_ERR;
    }
  }
  
  /*5.清除*/
  MFRC522_SetBitMask(ControlReg,0x80);  //停止定时器
  MFRC522_Write(CommandReg,PCD_IDLE);   //无动作,取消当前动作的执行
  
  return Status;
}