📄 main.lst
字号:
799 2 WriteIO(RegBitPhase,0xAD);
C51 COMPILER V6.23a MAIN 08/14/2006 14:40:24 PAGE 14
800 2 WriteIO(RegRxThreshold,0xFF);
801 2 WriteIO(RegRxControl2,0x01);
802 2 WriteIO(RegFIFOLevel,0x1A);
803 2 WriteIO(RegTimerControl,0x02);
804 2 WriteIO(RegIRqPinConfig,0x03);
805 2 M500PcdRfReset(1);
806 2 }
807 1 return status;
808 1 }
809
810 ///////////////////////////////////////////////////////////////
811 // Key loading into the MF RC500's EEPROM
812 ///////////////////////////////////////////////////////////////
813 char M500PcdLoadKeyE2(uchar key_type,
814 uchar sector,
815 uchar *uncoded_keys)
816 {
817 1 signed char status = MI_OK;
818 1 unsigned int e2addr = 0x80 + sector * 0x18;
819 1 uchar coded_keys[12];
820 1
821 1 if (key_type == PICC_AUTHENT1B)
822 1 {
823 2 e2addr += 12; // key B offset
824 2 }
825 1 if ((status = M500HostCodeKey(uncoded_keys,coded_keys)) == MI_OK)
826 1 {
827 2 status = PcdWriteE2(e2addr,12,coded_keys);
828 2 }
829 1 return status;
830 1 }
831
832 ///////////////////////////////////////////////////////////////////////
833 // Write the MF RC500's EEPROM
834 // 向RC500 EEPROM写数据
835 ///////////////////////////////////////////////////////////////////////
836 char PcdWriteE2(unsigned int startaddr,
837 uchar length,
838 uchar* _data)
839 {
840 1 char status = MI_OK;
841 1 ResetInfo(MInfo);
842 1 SerBuffer[0] = startaddr & 0xFF;
843 1 SerBuffer[1] = (startaddr >> 8) & 0xFF;
844 1 memcpy(SerBuffer + 2,_data,length);
845 1
846 1 MInfo.nBytesToSend = length + 2;
847 1
848 1 status = M500PcdCmd(PCD_WRITEE2,
849 1 SerBuffer,
850 1 &MInfo);
851 1 return status;
852 1 }
853
854 ///////////////////////////////////////////////////////////////////////
855 // Select Command defined in ISO14443(MIFARE)
856 ///////////////////////////////////////////////////////////////////////
857 char M500PcdMfOutSelect(uchar type)
858 {
859 1 WriteIO(RegMfOutSelect,type&0x7);
860 1 return MI_OK;
861 1 }
C51 COMPILER V6.23a MAIN 08/14/2006 14:40:24 PAGE 15
862
863 ///////////////////////////////////////////////////////////////////////
864 // Request Command defined in ISO14443(MIFARE)
865 // Request
866 // 寻卡
867 ///////////////////////////////////////////////////////////////////////
868 char M500PiccCommonRequest(uchar req_code,uchar *atq)
869 {
870 1 char idata status = MI_OK;
871 1
872 1 M500PcdSetTmo(3);
873 1 WriteIO(RegChannelRedundancy,0x03);
874 1 ClearBitMask(RegControl,0x08);
875 1 WriteIO(RegBitFraming,0x07);
876 1 SetBitMask(RegTxControl,0x03);
877 1 ResetInfo(MInfo);
878 1 SerBuffer[0] = req_code;
879 1 MInfo.nBytesToSend = 1;
880 1
881 1 status = M500PcdCmd(PCD_TRANSCEIVE,SerBuffer,&MInfo);
882 1 if (status)
883 1 {
884 2 *atq = 0;
885 2 }
886 1 else
887 1 {
888 2 if (MInfo.nBitsReceived != 16)
889 2 {
890 3 *atq = 0;
891 3 status = MI_BITCOUNTERR;
892 3 }
893 2 else
894 2 {
895 3 status = MI_OK;
896 3 memcpy(atq,SerBuffer,2);
897 3 }
898 2 }
899 1 return status;
900 1 }
901
902 ///////////////////////////////////////////////////////////////////
903 // Cascaded Anti-Collision Command defined in ISO14443(MIFARE)
904 // 防冲突 读卡的系列号 CardSnr
905 ///////////////////////////////////////////////////////////////////
906 char M500PiccCascAnticoll (uchar bcnt,uchar *snr)
907 {
908 1 char idata status = MI_OK;
909 1 char idata snr_in[4];
910 1 char idata nbytes = 0;
911 1 char idata nbits = 0;
912 1 char idata complete = 0;
913 1 char idata i = 0;
914 1 char idata byteOffset = 0;
915 1 uchar dummyShift1;
916 1 uchar dummyShift2;
917 1
918 1 M500PcdSetTmo(106);
919 1 memcpy(snr_in,snr,4);
920 1
921 1 WriteIO(RegDecoderControl,0x28);
922 1 ClearBitMask(RegControl,0x08);
923 1 complete = 0;
C51 COMPILER V6.23a MAIN 08/14/2006 14:40:24 PAGE 16
924 1 while (!complete && (status == MI_OK) )
925 1 {
926 2 ResetInfo(MInfo);
927 2 WriteIO(RegChannelRedundancy,0x03);
928 2 nbits = bcnt % 8;
929 2 if(nbits)
930 2 {
931 3 WriteIO(RegBitFraming,nbits << 4 | nbits);
932 3 nbytes = bcnt / 8 + 1;
933 3 if (nbits == 7)
934 3 {
935 4 MInfo.cmd = PICC_ANTICOLL1;
936 4 WriteIO(RegBitFraming,nbits);
937 4 }
938 3 }
939 2 else
940 2 {
941 3 nbytes = bcnt / 8;
942 3 }
943 2 SerBuffer[0] = 0x93;
944 2 SerBuffer[1] = 0x20 + ((bcnt/8) << 4) + nbits;
945 2
946 2 for (i = 0; i < nbytes; i++)
947 2 {
948 3 SerBuffer[i + 2] = snr_in[i];
949 3 }
950 2 MInfo.nBytesToSend = 2 + nbytes;
951 2
952 2 status = M500PcdCmd(PCD_TRANSCEIVE,SerBuffer,&MInfo);
953 2 if (nbits == 7)
954 2 {
955 3 dummyShift1 = 0x00;
956 3 for (i = 0; i < MInfo.nBytesReceived; i++)
957 3 {
958 4 dummyShift2 = SerBuffer[i];
959 4 SerBuffer[i] = (dummyShift1 >> (i+1)) | (SerBuffer[i] << (7-i));
960 4 dummyShift1 = dummyShift2;
961 4 }
962 3 MInfo.nBitsReceived -= MInfo.nBytesReceived;
963 3 if ( MInfo.collPos ) MInfo.collPos += 7 - (MInfo.collPos + 6) / 9;
964 3 }
965 2 if ( status == MI_OK || status == MI_COLLERR)
966 2 {
967 3 if ( MInfo.nBitsReceived != (40 - bcnt) )
968 3 {
969 4 status = MI_BITCOUNTERR;
970 4 }
971 3 else
972 3 {
973 4 byteOffset = 0;
974 4 if( nbits != 0 )
975 4 {
976 5 snr_in[nbytes - 1] = snr_in[nbytes - 1] | SerBuffer[0];
977 5 byteOffset = 1;
978 5 }
979 4
980 4 for ( i =0; i < (4 - nbytes); i++)
981 4 {
982 5 snr_in[nbytes + i] = SerBuffer[i + byteOffset];
983 5 }
984 4
985 4 if (status != MI_COLLERR )
C51 COMPILER V6.23a MAIN 08/14/2006 14:40:24 PAGE 17
986 4 {
987 5 dummyShift2 = snr_in[0] ^ snr_in[1] ^ snr_in[2] ^ snr_in[3];
988 5 dummyShift1 = SerBuffer[MInfo.nBytesReceived - 1];
989 5 if (dummyShift2 != dummyShift1)
990 5 {
991 6 status = MI_SERNRERR;
992 6 }
993 5 else
994 5 {
995 6 complete = 1;
996 6 }
997 5 }
998 4 else
999 4 {
1000 5 bcnt = bcnt + MInfo.collPos - nbits;
1001 5 status = MI_OK;
1002 5 }
1003 4 }
1004 3 }
1005 2 }
1006 1 if (status == MI_OK)
1007 1 {
1008 2 memcpy(snr,snr_in,4);
1009 2 }
1010 1 else
1011 1 {
1012 2 memcpy(snr,"0000",4);
1013 2 }
1014 1 ClearBitMask(RegDecoderControl,0x20);
1015 1
1016 1 return status;
1017 1 }
1018
1019 //////////////////////////////////////////////////////////////////
1020 // Cascaded Select command defined in ISO14443(MIFARE)
1021 // 选择卡 Select Card
1022 //////////////////////////////////////////////////////////////////
1023 char M500PiccCascSelect(uchar *snr,uchar *sak)
1024 {
1025 1 char idata status = MI_OK;
1026 1
1027 1 M500PcdSetTmo(106);
1028 1
1029 1 WriteIO(RegChannelRedundancy,0x0F);
1030 1 ClearBitMask(RegControl,0x08);
1031 1 ResetInfo(MInfo);
1032 1 SerBuffer[0] = 0x93;
1033 1 SerBuffer[1] = 0x70;
1034 1
1035 1 memcpy(SerBuffer + 2,snr,4);
1036 1 SerBuffer[6] = SerBuffer[2] ^ SerBuffer[3] ^ SerBuffer[4] ^ SerBuffer[5];
1037 1 MInfo.nBytesToSend = 7;
1038 1 status = M500PcdCmd(PCD_TRANSCEIVE,SerBuffer,&MInfo);
1039 1
1040 1 *sak = 0;
1041 1 if (status == MI_OK)
1042 1 {
1043 2 if (MInfo.nBitsReceived != 8)
1044 2 {
1045 3 status = MI_BITCOUNTERR;
1046 3 }
1047 2 else
C51 COMPILER V6.23a MAIN 08/14/2006 14:40:24 PAGE 18
1048 2 {
1049 3 *sak = SerBuffer[0];
1050 3 }
1051 2 }
1052 1 return status;
1053 1 }
1054
1055 ///////////////////////////////////////////////////////////////////////
1056 // Key loading into the MF RC500's EEPROM
1057 // 校验卡密码(E2)
1058 ///////////////////////////////////////////////////////////////////////
1059 char M500PiccAuthE2( uchar auth_mode,uchar *snr,
1060 uchar keynr,uchar block)
1061
1062 {
1063 1 char idata status = MI_OK;
1064 1 unsigned int e2addr = 0x80 + keynr * 0x18;
1065 1 uchar *e2addrbuf;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -