⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 m500auc.c

📁 ISO14443协议的实现
💻 C
📖 第 1 页 / 共 3 页
字号:
{
   char idata status = MI_OK;
   unsigned int idata timecnt=0;
   RC500RST = 0;
   delay_1ms(25);
   RC500RST = 1;
   delay_50us(50);
   RC500RST = 0;
   delay_50us(50);
   timecnt=1000;
   while ((ReadIO(RegCommand) & 0x3F)&&timecnt--);
  if(!timecnt)
   {
      status = MI_RESETERR;
   }
   if (status == MI_OK)
   {
//      WriteIO(RegPage,0x80);
      if (ReadIO(RegCommand) != 0x00)
      {
          status = MI_INTERFACEERR;
      }
   }
   return status;
}

uchar M500PcdRfReset(unsigned char ms)              //复位射频信号
{
   char idata status = MI_OK;

   if(ms)
   {
     ClearBitMask(RegTxControl,0x03);
     delay_1ms(2);
     SetBitMask(RegTxControl,0x03);
   }
  else
     ClearBitMask(RegTxControl,0x03);

   return status;
}
uchar M500PcdConfig(void)                           //RC500寄存器恢复初始设置
{
   char idata status;

   if ((status = M500PcdReset()) == MI_OK)
   {
     WriteIO(RegClockQControl,0x0);
     WriteIO(RegClockQControl,0x40);
     delay_50us(2);
     ClearBitMask(RegClockQControl,0x40);
     WriteIO(RegBitPhase,0xAD);
     WriteIO(RegRxThreshold,0xFF);
     WriteIO(RegRxControl2,0x01);
     WriteIO(RegFIFOLevel,0x1A);
     WriteIO(RegTimerControl,0x02);
     WriteIO(RegIRqPinConfig,0x03);
     M500PcdRfReset(1);
   }
   return status;
}




uchar M500PiccCommonRequest(unsigned char req_code)            //卡片请求命令

{
   char idata status = MI_OK;

   M500PcdSetTmo(3); //6ms
   WriteIO(RegChannelRedundancy,0x03);
   ClearBitMask(RegControl,0x08);
   WriteIO(RegBitFraming,0x07);
   SetBitMask(RegTxControl,0x03);
   ResetInfo(MInfo);
   SerBuffer[0] = req_code;
   MInfo.nBytesToSend   = 1;
   status = M500PcdCmd(
                      SerBuffer,
                      &MInfo);

   if (status)
   {
      Len=0;
   }
   else
   {
      if (MInfo.nBitsReceived != 16)
      {
         Len=0;
         status = MI_BITCOUNTERR;
      }
      else
      {
         status = MI_OK;
         for(ramlen=0;ramlen<2;ramlen++)
         toPpc(ramlen,SerBuffer[ramlen]);
      }
   }
   return status;
}

uchar M500PiccActive(void)                                //卡片激活命令
{
   char idata status = MI_OK;

   M500PcdSetTmo(3); //6ms
   ResetInfo(MInfo);
   SerBuffer[0] = 0xe0;
   SerBuffer[1] = 0x50;
   MInfo.nBytesToSend   = 2;
   status = M500PcdCmd(
                      SerBuffer,
                      &MInfo);

   if (status)
   {
      Len=0;
   }
   else
   {
      if (MInfo.nBitsReceived == 0)
      {
         Len=0;
         status = MI_BITCOUNTERR;
      }
      else
      {
         status = MI_OK;
         for(ramlen=0;ramlen<MInfo.nBytesReceived;ramlen++)
         toPpc(ramlen,SerBuffer[ramlen]);

      }
   }
   block_num=0;
   return status;
}

uchar Send_MF(void)                                        //向卡片直接发送数据SerBuffer 长度=Len
{

   char idata status = MI_OK;
   M500PcdSetTmo(3);
   ResetInfo(MInfo);
   if(block_num)
        if((pcb&0xc0)==0)
          pcb|=0x01;
   SerBuffer[0]=pcb;
   MInfo.nBytesToSend   = Len+1;
   if (SerBuffer[2]==0x0e) tt0=10;
   else tt0=1;
   status = M500PcdCmd(
                       SerBuffer,
                       &MInfo);

if (status)
   {
     MInfo.nBytesReceived=1;
     Len=1;
     return status;
   }
   else
   {
      if (MInfo.nBitsReceived == 0)
      {
         Len = 0;
         return 0x1;
      }
      else
      {
         status = MI_OK;

      }
   }
   return 0;
}

uchar M500PiccCascAnticoll (unsigned char select_code,
                           unsigned char bcnt)                 //反冲突命令
{
   char idata status = MI_OK;

   char idata nbytes = 0;
   char idata nbits = 0;
   char idata complete = 0;
   char idata i        = 0;
   char idata byteOffset = 0;

       // dummy byte for snr shift

   //************* Initialisation ******************************
   M500PcdSetTmo(106);


   WriteIO(RegDecoderControl,0x28); // ZeroAfterColl aktivieren
   ClearBitMask(RegControl,0x08);    // disable crypto 1 unit

   //************** Anticollision Loop ***************************
   complete = 0;
//   bcnt = 0;   // no part of the snr is known
   while (!complete && (status == MI_OK) )
   {
      ResetInfo(MInfo);
      WriteIO(RegChannelRedundancy,0x03); // RxCRC and TxCRC disable, parity enable
      nbits = bcnt % 8;   // remaining number of bits


      SerBuffer[0] = select_code;
      SerBuffer[1] = 0x20 + ((bcnt/8) << 4) + nbits; //number of bytes send


      MInfo.nBytesToSend   = 2 ;

      status = M500PcdCmd(
                         SerBuffer,
                         &MInfo);


      // in order to solve an inconsistancy in the anticollision sequence
      // (will be solved soon), the case of 7 bits has to be treated in a
      // separate way


      if ( status == MI_OK || status == MI_COLLERR)    // no other occured
      {
         for(Len=0;Len<MInfo.nBytesReceived;Len++)
         toPpc(Len+1,SerBuffer[Len]);
         complete=1;
      }
   }


   //----------------------Einstellungen aus Initialisierung ruecksetzen
   ClearBitMask(RegDecoderControl,0x20); // ZeroAfterColl disable
   return status;
}


uchar M500PiccCascSelect(unsigned char select_code)                   //根据反冲突命令中得到的序列号选择一张卡片

{
   char idata status = MI_OK;

   M500PcdSetTmo(106);

   WriteIO(RegChannelRedundancy,0x0F); // RxCRC,TxCRC, Parity enable
   ClearBitMask(RegControl,0x08);    // disable crypto 1 unit

   //************* Cmd Sequence **********************************
   ResetInfo(MInfo);
   SerBuffer[5]=SerBuffer[3];
   SerBuffer[4]=SerBuffer[2];
   SerBuffer[3]=SerBuffer[1];
   SerBuffer[2]=SerBuffer[0];
   SerBuffer[1]=0x70;
   SerBuffer[0]=select_code;
   SerBuffer[6] = SerBuffer[2]
                   ^ SerBuffer[3]
                   ^ SerBuffer[4]
                   ^ SerBuffer[5];
   MInfo.nBytesToSend   = 7;
   status = M500PcdCmd(
                       SerBuffer,
                       &MInfo);


   if (status == MI_OK)    // no timeout occured
   {

	 toPpc(0,SerBuffer[0]);


   }

   return status;
}

uchar M500PiccHalt(void)                                      //卡片中止命令
{
   char idata status = MI_CODEERR;
   ResetInfo(MInfo);
   SerBuffer[0] = PICC_HALT ;
   SerBuffer[1] = 0x00;
   MInfo.nBytesToSend   = 2;
   status = M500PcdCmd(
                          SerBuffer,
                       &MInfo);
   if (status)
   {
     if (status == MI_NOTAGERR || status == MI_ACCESSTIMEOUT)
        status = MI_OK;
   }
   WriteIO(RegCommand,PCD_IDLE);
   return status;
}

uchar TransceivePICC(void)                                     //在卡片与PC间传送数据
{
     uchar j;
     uint send_length,ceive_length;
     uchar send_num,WTX;
     ceive_length=0;
     send_length=Len;
     send_num=send_length/61;     //发送次数(分组数)=总发送字节/(FIFO长度-3)
     if(send_length%61) send_num++;//如果还有剩余字节,发送次数再加一
     for(j=1;j<send_num;j++)       //连续发送数据
     {
       pcb=0x12;
       Len=61;
       for(ramlen=0;ramlen<Len;ramlen++)
       SerBuffer[ramlen+1]=fromPpc(ramlen+61*(j-1));
       status=Send_MF();
       if(status) return status;
       pcb_r=SerBuffer[0];
       if((pcb_r&0xf7)==0xf2)      //WTX等待请求
         { WTX=SerBuffer[1];
           delay_50us(2*WTX);
           Len=2;
           status=Send_MF();
           if(status) return status;
          }
        if((pcb_r&0xa6)==0xa2)    //请求下一分组数据
          {

                 block_num=~block_num;
                 send_length-=61;
                 continue;

            }
         else if((pcb_r&0xe2)==0x02)	//卡片发来错误的带链接数据
	       {
		  status=0x87;	//not R_block,error
		  return 1;
               }
         else                          //未知的分组pcb
	       {status=0x88;return 1;}
     }
     pcb=0x02;
     Len=send_length%61;
     for(ramlen=0;ramlen<Len;ramlen++)
       SerBuffer[ramlen+1]=fromPpc(ramlen+(send_num-1)*61);
     status=Send_MF();                 //发送最后的分组
     if(status) return status;
     while(1)                        // 数据接受
       {
          pcb_r=SerBuffer[0];        //取得分组的pcb
          if((pcb_r&0xf7)==0xf2)     //WTX等待请求
            {
             WTX=SerBuffer[1];
             delay_50us(2*WTX);
             Len=2;
             status=Send_MF();
             if(status) return status;
            }

          if((pcb_r&0xf2)==0x02)     //全部数据接受完毕
            {
             Len=MInfo.nBytesReceived-1;
             for(ramlen=0;ramlen<Len;ramlen++)
                 toPpc(ramlen+ceive_length,SerBuffer[ramlen+1]);
             ceive_length+=Len;
             Len=ceive_length;
             break;
            }

           else if((pcb_r&0xf2)==0x12) //还有剩余的数据
            {
             Len=MInfo.nBytesReceived-1;
             for(ramlen=0;ramlen<Len;ramlen++)
                 toPpc(ramlen+ceive_length,SerBuffer[ramlen+1]);
             ceive_length+=Len;
             Len=0;
             if(pcb_r&0x1)	{pcb=0xa2;}
             else pcb=0xa3;
             status=Send_MF();
             if(status) return status;
            }
           else                          //未知分组pcb
             {status=0x88;return 1;}

       }


     return status;
}

uchar SENDbyte0(uchar chr)           //向IC卡发送一个字符
{
	uchar i = 0;
	int tt;
	STATE_FLAG = 0;
        ET0 = 1;EA = 1;
	delay_1ms(1);
	while(1)
	{
		TXD = 1;
		while(!TXD);

//		TB8=1;
		ACC = chr;
		TB8 = P;
		SBUF=chr;
		while(!TI);
		TI=0;
		tt0 = timer0;
                RXD = 1;
//		while((timer0 - tt0) < 10);//10 ok //wait for 11 etu
                if(T0_T1_FLAG) break;
		if(RXD)
                    break;			//right
		tt = timer0;EA = 1;
		while(timer0 - tt < 2);	//error delay 2 etu
		if(i++ == 4)
                       {RI = 0;STATE_FLAG = 1;return -1;}		//error
	}

	RI = 0;
//ET0 = 0;
	return 0;
}

uchar RECVbyte0(void)               //从IC卡接受一个字符
{
	int tt;
	uchar i = 0;

	STATE_FLAG=0;
	RI = 0;
        ET0 = 1;EA = 1;
	while(1)
	{
		while(!RI)
		{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -