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

📄 m500auc.lst

📁 ISO14443协议的实现
💻 LST
📖 第 1 页 / 共 4 页
字号:
 650   2                               SerBuffer,
 651   2                               &MInfo);
 652   2      
 653   2      
 654   2            // in order to solve an inconsistancy in the anticollision sequence
 655   2            // (will be solved soon), the case of 7 bits has to be treated in a
 656   2            // separate way
 657   2      
 658   2      
 659   2            if ( status == MI_OK || status == MI_COLLERR)    // no other occured
 660   2            {
 661   3               for(Len=0;Len<MInfo.nBytesReceived;Len++)
 662   3               toPpc(Len+1,SerBuffer[Len]);
 663   3               complete=1;
 664   3            }
 665   2         }
 666   1      
 667   1      
 668   1         //----------------------Einstellungen aus Initialisierung ruecksetzen
 669   1         ClearBitMask(RegDecoderControl,0x20); // ZeroAfterColl disable
 670   1         return status;
 671   1      }
 672          
 673          
 674          uchar M500PiccCascSelect(unsigned char select_code)                   //根据反冲突命令中得到的序列号选择一
             -张卡片
C51 COMPILER V6.02  M500AUC                                                                11/20/2007 16:53:51 PAGE 12  

 675          
 676          {
 677   1         char idata status = MI_OK;
 678   1      
 679   1         M500PcdSetTmo(106);
 680   1      
 681   1         WriteIO(RegChannelRedundancy,0x0F); // RxCRC,TxCRC, Parity enable
 682   1         ClearBitMask(RegControl,0x08);    // disable crypto 1 unit
 683   1      
 684   1         //************* Cmd Sequence **********************************
 685   1         ResetInfo(MInfo);
 686   1         SerBuffer[5]=SerBuffer[3];
 687   1         SerBuffer[4]=SerBuffer[2];
 688   1         SerBuffer[3]=SerBuffer[1];
 689   1         SerBuffer[2]=SerBuffer[0];
 690   1         SerBuffer[1]=0x70;
 691   1         SerBuffer[0]=select_code;
 692   1         SerBuffer[6] = SerBuffer[2]
 693   1                         ^ SerBuffer[3]
 694   1                         ^ SerBuffer[4]
 695   1                         ^ SerBuffer[5];
 696   1         MInfo.nBytesToSend   = 7;
 697   1         status = M500PcdCmd(
 698   1                             SerBuffer,
 699   1                             &MInfo);
 700   1      
 701   1      
 702   1         if (status == MI_OK)    // no timeout occured
 703   1         {
 704   2      
 705   2      	 toPpc(0,SerBuffer[0]);
 706   2      
 707   2      
 708   2         }
 709   1      
 710   1         return status;
 711   1      }
 712          
 713          uchar M500PiccHalt(void)                                      //卡片中止命令
 714          {
 715   1         char idata status = MI_CODEERR;
 716   1         ResetInfo(MInfo);
 717   1         SerBuffer[0] = PICC_HALT ;
 718   1         SerBuffer[1] = 0x00;
 719   1         MInfo.nBytesToSend   = 2;
 720   1         status = M500PcdCmd(
 721   1                                SerBuffer,
 722   1                             &MInfo);
 723   1         if (status)
 724   1         {
 725   2           if (status == MI_NOTAGERR || status == MI_ACCESSTIMEOUT)
 726   2              status = MI_OK;
 727   2         }
 728   1         WriteIO(RegCommand,PCD_IDLE);
 729   1         return status;
 730   1      }
 731          
 732          uchar TransceivePICC(void)                                     //在卡片与PC间传送数据
 733          {
 734   1           uchar j;
 735   1           uint send_length,ceive_length;
 736   1           uchar send_num,WTX;
C51 COMPILER V6.02  M500AUC                                                                11/20/2007 16:53:51 PAGE 13  

 737   1           ceive_length=0;
 738   1           send_length=Len;
 739   1           send_num=send_length/61;     //发送次数(分组数)=总发送字节/(FIFO长度-3)
 740   1           if(send_length%61) send_num++;//如果还有剩余字节,发送次数再加一
 741   1           for(j=1;j<send_num;j++)       //连续发送数据
 742   1           {
 743   2             pcb=0x12;
 744   2             Len=61;
 745   2             for(ramlen=0;ramlen<Len;ramlen++)
 746   2             SerBuffer[ramlen+1]=fromPpc(ramlen+61*(j-1));
 747   2             status=Send_MF();
 748   2             if(status) return status;
 749   2             pcb_r=SerBuffer[0];
 750   2             if((pcb_r&0xf7)==0xf2)      //WTX等待请求
 751   2               { WTX=SerBuffer[1];
 752   3                 delay_50us(2*WTX);
 753   3                 Len=2;
 754   3                 status=Send_MF();
 755   3                 if(status) return status;
 756   3                }
 757   2              if((pcb_r&0xa6)==0xa2)    //请求下一分组数据
 758   2                {
 759   3      
 760   3                       block_num=~block_num;
 761   3                       send_length-=61;
 762   3                       continue;
 763   3      
 764   3                  }
 765   2               else if((pcb_r&0xe2)==0x02)	//卡片发来错误的带链接数据
 766   2      	       {
 767   3      		  status=0x87;	//not R_block,error
 768   3      		  return 1;
 769   3                     }
 770   2               else                          //未知的分组pcb
 771   2      	       {status=0x88;return 1;}
 772   2           }
 773   1           pcb=0x02;
 774   1           Len=send_length%61;
 775   1           for(ramlen=0;ramlen<Len;ramlen++)
 776   1             SerBuffer[ramlen+1]=fromPpc(ramlen+(send_num-1)*61);
 777   1           status=Send_MF();                 //发送最后的分组
 778   1           if(status) return status;
 779   1           while(1)                        // 数据接受
 780   1             {
 781   2                pcb_r=SerBuffer[0];        //取得分组的pcb
 782   2                if((pcb_r&0xf7)==0xf2)     //WTX等待请求
 783   2                  {
 784   3                   WTX=SerBuffer[1];
 785   3                   delay_50us(2*WTX);
 786   3                   Len=2;
 787   3                   status=Send_MF();
 788   3                   if(status) return status;
 789   3                  }
 790   2      
 791   2                if((pcb_r&0xf2)==0x02)     //全部数据接受完毕
 792   2                  {
 793   3                   Len=MInfo.nBytesReceived-1;
 794   3                   for(ramlen=0;ramlen<Len;ramlen++)
 795   3                       toPpc(ramlen+ceive_length,SerBuffer[ramlen+1]);
 796   3                   ceive_length+=Len;
 797   3                   Len=ceive_length;
 798   3                   break;
C51 COMPILER V6.02  M500AUC                                                                11/20/2007 16:53:51 PAGE 14  

 799   3                  }
 800   2      
 801   2                 else if((pcb_r&0xf2)==0x12) //还有剩余的数据
 802   2                  {
 803   3                   Len=MInfo.nBytesReceived-1;
 804   3                   for(ramlen=0;ramlen<Len;ramlen++)
 805   3                       toPpc(ramlen+ceive_length,SerBuffer[ramlen+1]);
 806   3                   ceive_length+=Len;
 807   3                   Len=0;
 808   3                   if(pcb_r&0x1)	{pcb=0xa2;}
 809   3                   else pcb=0xa3;
 810   3                   status=Send_MF();
 811   3                   if(status) return status;
 812   3                  }
 813   2                 else                          //未知分组pcb
 814   2                   {status=0x88;return 1;}
 815   2      
 816   2             }
 817   1      
 818   1      
 819   1           return status;
 820   1      }
 821          
 822          uchar SENDbyte0(uchar chr)           //向IC卡发送一个字符
 823          {
 824   1      	uchar i = 0;
 825   1      	int tt;
 826   1      	STATE_FLAG = 0;
 827   1              ET0 = 1;EA = 1;
 828   1      	delay_1ms(1);
 829   1      	while(1)
 830   1      	{
 831   2      		TXD = 1;
 832   2      		while(!TXD);
 833   2      
 834   2      //		TB8=1;
 835   2      		ACC = chr;
 836   2      		TB8 = P;
 837   2      		SBUF=chr;
 838   2      		while(!TI);
 839   2      		TI=0;
 840   2      		tt0 = timer0;
 841   2                      RXD = 1;
 842   2      //		while((timer0 - tt0) < 10);//10 ok //wait for 11 etu
 843   2                      if(T0_T1_FLAG) break;
 844   2      		if(RXD)
 845   2                          break;			//right
 846   2      		tt = timer0;EA = 1;
 847   2      		while(timer0 - tt < 2);	//error delay 2 etu
 848   2      		if(i++ == 4)
 849   2                             {RI = 0;STATE_FLAG = 1;return -1;}		//error
 850   2      	}
 851   1      
 852   1      	RI = 0;
 853   1      //ET0 = 0;
 854   1      	return 0;
 855   1      }
 856          
 857          uchar RECVbyte0(void)               //从IC卡接受一个字符
 858          {
 859   1      	int tt;
 860   1      	uchar i = 0;
C51 COMPILER V6.02  M500AUC                                                                11/20/2007 16:53:51 PAGE 15  

 861   1      
 862   1      	STATE_FLAG=0;
 863   1      	RI = 0;
 864   1              ET0 = 1;EA = 1;
 865   1      	while(1)
 866   1      	{
 867   2      		while(!RI)
 868   2      		{
 869   3      			if((timer0 - tt0) > (int)WI*960*DD)
 870   3                               {STATE_FLAG=1;return(STimeOut);}//time out
 871   3      		}
 872   2      		RI=0;
 873   2      		ACC = SBUF;
 874   2      		tt0 = timer0;
 875   2                      if(T0_T1_FLAG) return(SBUF);
 876   2      		if(RB8 != P)
 877   2      		{       EA = 1;
 878   3      			while((timer0 - tt0) < 10);//delay 10.5 etu
 879   3      			tt = timer0;
 880   3      			RXD = 0;
 881   3                              EA = 1;
 882   3      			while(timer0 - tt < 1);	//1 ~ 2 etu
 883   3      			RXD = 1;
 884   3      		}
 885   2      		else
 886   2      			return(SBUF);
 887   2      
 888   2      		if(i++ == 3)
 889   2      			{RI = 0;STATE_FLAG=1;return(-1);}//time out
 890   2      	}
 891   1      //ET0 = 0;
 892   1      }
 893          
 894          uchar RstICAnswer(void)               //IC卡复位应答
 895          	{
 896   1      	uchar kk,i,temp;
 897   1              uchar length,TD,Bcc;
 898   1              uchar dd0,wi0;
 899   1              kk = 0;dd0 = 1;wi0 = 10;
 900   1              TC1 = 0;DD = 1;//WI = 10;
 901   1              WI = 100;
 902   1              forPowerdown = 0;
 903   1      	if(ID==CPUtype)		//cpu card
 904   1      		{
 905   2                      if(ICPWR) {ICPWR = 0;
 906   3                          PowerDown = 0;
 907   3                          TR2 = 0;
 908   3                          delay_1ms(10);}
 909   2                      ICRST=0;
 910   2      		delay_1ms(11);//12 //11  // 40000/3579000 = 11ms
 911   2      		ICRST=1;
 912   2      		}
 913   1      	else if(ID==SAM1type)			//sam card
 914   1      		{
 915   2      		SAMRST=0;
 916   2      		delay_1ms(11);
 917   2      		SAMRST=1;
 918   2      		}
 919   1      
 920   1      
 921   1              tt0 = timer0 = 0;
 922   1              temp=RECVbyte0();
C51 COMPILER V6.02  M500AUC                                                                11/20/2007 16:53:51 PAGE 16  

 923   1      	if(STATE_FLAG)
 924   1                               return(temp);
 925   1      
 926   1      	if(temp!=0x3b)
 927   1      		{
 928   2      		STATE_FLAG=1;return(temp);//0x88
 929   2      		}
 930   1      
 931   1      	toPpc(0,0x3b);
 932   1      
 933   1      	Len=1;/*length*/
 934   1      
 935   1      	temp=RECVbyte0();
 936   1      	if(STATE_FLAG)
 937   1                               return(temp);
 938   1      	toPpc(1,temp);
 939   1      	Len++;
 940   1      
 941   1      	length=temp&0x0f;
 942   1      	Bcc=0;
 943   1      
 944   1      	T0_T1_FLAG=0;
 945   1      
 946   1      	TD=temp;
 947   1      	while(++kk)
 948   1      		{
 949   2      		if((TD&0x10)==0x10)
 950   2      			{
 951   3                              temp=RECVbyte0();
 952   3      			if(STATE_FLAG)
 953   3                                               return(temp);
 954   3                              if(kk == 1)       //TA1
 955   3                              {
 956   4                                      if(temp==1) dd0=1;
 957   4                                      if(temp==2) dd0=2;
 958   4                                      if(temp==3) dd0=4;
 959   4                                      if(temp==4) dd0=8;
 960   4                                      if(temp==5) dd0=16;
 961   4                              }
 962   3                              toPpc(Len,temp);/*TAi*/
 963   3      			Bcc^=temp;
 964   3      			Len++;}
 965   2      		if((TD&0x20)==0x20)
 966   2      			{
 967   3      		        temp=RECVbyte0();
 968   3      			if(STATE_FLAG)
 969   3                                               return(temp);
 970   3      			toPpc(Len,temp);/*TBi*/
 971   3      			Bcc^=temp;
 972   3      			Len++;}
 973   2      		if((TD&0x40)==0x40)
 974   2      			{
 975   3                              temp=RECVbyte0();
 976   3      			if(STATE_FLAG)
 977   3                                               return(temp);

⌨️ 快捷键说明

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