📄 mfrc500uc.lst
字号:
682 if ((atq[0] & 0x1F) == 0x00) // check lower 5 bits, for tag-type
683 // all tags within this 5 bits have to
684 // provide a bitwise anticollision
685 {
686 status = MI_NOBITWISEANTICOLL;
687 }
688 }
689 if (status == MI_OK)
690 {
691 //Get UID in 1 - 3 levels (standard, [double], [triple] )
692 //-------
693 switch(br)
694 {
695 case 0: cmdASEL = PICC_ANTICOLL1; break;
696 default:
697 status = MI_BAUDRATE_NOT_SUPPORTED; break;
698 }
699 }
700 if (status == MI_OK)
701 {
702 //Select UID in up to 3 cascade levels (standard, [double], [triple] )
703 //------------------------------------
704 cascade_level = 0;
705 uid_index = 0;
706 tmpuid[0] = 0x88; //first byte of cascaded UIDs is 0x88 (cascaded tag)
707
708 do
709 {
710 sel_code = cmdASEL + (2 * cascade_level);
711 cmdASEL = PICC_ANTICOLL1; // reset anticollistion level for calculation
712 //get the next UID part if we need to cascade
713 if((uid_len - uid_index) > 4)
714 {
715 //ok, we need to cascade the UID
716 memcpy(&tmpuid[1], &uid[uid_index], 3);
717 uid_index += 3;
718 }
719 else
720 {
721 //ah, how nice. no need to cascade
722 memcpy(tmpuid, &uid[uid_index], 4);
723 uid_index += 4;
724 }
725
726 status = Mf500PiccCascSelect(sel_code, tmpuid, sak);
727
728 if(status == MI_OK)
729 {
730 cascade_level++;
731 }
732 }
733 while((status == MI_OK ) // error occured
734 && (*sak & 0x04) // no further cascade level
735 && ((uid_index + 1) < uid_len) // all bytes of snr sent
C51 COMPILER V8.02 MFRC500UC 04/12/2009 10:36:17 PAGE 13
736 && (cascade_level < 3)); // highest cascade level reached
737 }
738 if ( status == MI_OK)
739 {
740 //Exit function, if UID length is not of expected length
741 if ((uid_index) != uid_len)
742 {
743 status = MI_SERNRERR ;
744 }
745 }
746 if (status == MI_OK)
747 {
748 //Exit function, if cascade level is triple and sak indicates another
749 //cascase level.
750 if ((cascade_level == 3) && (*sak & 0x04))
751 {
752 status = MI_SERNRERR;
753 }
754 }
755 return status;
756 }
757
758 ///////////////////////////////////////////////////////////////////////
759 // M I F A R E A U T H E N T I C A T I O N
760 // calling compatible version
761 ///////////////////////////////////////////////////////////////////////
762 char Mf500PiccAuth(unsigned char key_type, // PICC_AUTHENT1A or PICC_AUTHENT1B
763 unsigned char key_addr, // key address in reader storage
764 unsigned char block) // block number which should be
765 // authenticated
766 {
767 char status = MI_OK;
768
769 status = Mf500PiccAuthE2( key_type,
770 MLastSelectedSnr,
771 key_addr,
772 block);
773 return status;
774 }
775
776 ///////////////////////////////////////////////////////////////////////
777 // A U T H E N T I C A T I O N
778 // W I T H K E Y S F R O M E 2 P R O M
779 ///////////////////////////////////////////////////////////////////////
780 char Mf500PiccAuthE2( unsigned char auth_mode, // PICC_AUTHENT1A or PICC_AUTHENT1B
781 unsigned char *snr, // 4 bytes card serial number
782 unsigned char key_sector, // 0 <= key_sector <= 15
783 unsigned char block) // 0 <= block <= 256
784 {
785 char status = MI_OK;
786 // eeprom address calculation
787 // 0x80 ... offset
788 // key_sector ... sector
789 // 0x18 ... 2 * 12 = 24 = 0x18
790 unsigned short e2addr = 0x80 + key_sector * 0x18;
791 unsigned char *e2addrbuf = (unsigned char*)&e2addr;
792
793 PcdSetTmo(106);
794 if (auth_mode == PICC_AUTHENT1B)
795 e2addr += 12; // key B offset
796 FlushFIFO(); // empty FIFO
797 ResetInfo(MInfo);
C51 COMPILER V8.02 MFRC500UC 04/12/2009 10:36:17 PAGE 14
798
799 memcpy(MSndBuffer,e2addrbuf,2); // write low and high byte of address
800 MInfo.nBytesToSend = 2;
801 // write load command
802 if ((status=PcdSingleResponseCmd(PCD_LOADKEYE2,MSndBuffer,MRcvBuffer,&MInfo)) == MI_OK)
803 {
804 // execute authentication
805 status = Mf500PiccAuthState(auth_mode,snr,block);
806 }
807 return status;
808 }
809
810 ///////////////////////////////////////////////////////////////////////
811 // C O D E K E Y S
812 ///////////////////////////////////////////////////////////////////////
813 char Mf500HostCodeKey( unsigned char *uncoded, // 6 bytes key value uncoded
814 unsigned char *coded) // 12 bytes key value coded
815 {
816 char status = MI_OK;
817 unsigned char cnt = 0;
818 unsigned char ln = 0; // low nibble
819 unsigned char hn = 0; // high nibble
820
821 for (cnt = 0; cnt < 6; cnt++)
822 {
823 ln = uncoded[cnt] & 0x0F;
824 hn = uncoded[cnt] >> 4;
825 coded[cnt * 2 + 1] = (~ln << 4) | ln;
826 coded[cnt * 2 ] = (~hn << 4) | hn;
827
828 }
829 return MI_OK;
830 }
831
832 ///////////////////////////////////////////////////////////////////////
833 // A U T H E N T I C A T I O N
834 // W I T H P R O V I D E D K E Y S
835 ///////////////////////////////////////////////////////////////////////
836 char Mf500PiccAuthKey( unsigned char auth_mode,
837 unsigned char *snr,
838 unsigned char *keys,
839 unsigned char block)
840 {
841 char status = MI_OK;
842 unsigned char i = 0;
843
844 PcdSetTmo(106);
845 FlushFIFO(); // empty FIFO
846 ResetInfo(MInfo);
847 memcpy(MSndBuffer,keys,12); // write 12 bytes of the key
848 MInfo.nBytesToSend = 12;
849 // write load command
850 if ((status=PcdSingleResponseCmd(PCD_LOADKEY,MSndBuffer,MRcvBuffer,&MInfo)) == MI_OK)
851 {
852 // execute authentication
853 status = Mf500PiccAuthState(auth_mode,snr,block);
854 }
855 return status;
856 }
857
858 ///////////////////////////////////////////////////////////////////////
859 // S T O R E K E Y S I N E E P R O M
C51 COMPILER V8.02 MFRC500UC 04/12/2009 10:36:17 PAGE 15
860 ///////////////////////////////////////////////////////////////////////
861 char Mf500PcdLoadKeyE2(unsigned char key_type,
862 unsigned char sector,
863 unsigned char *uncoded_keys)
864 {
865 // eeprom address calculation
866 // 0x80 ... offset
867 // key_sector ... sector
868 // 0x18 ... 2 * 12 = 24 = 0x18
869 signed char status = MI_OK;
870 unsigned short e2addr = 0x80 + sector * 0x18;
871 unsigned char coded_keys[12];
872
873 if (key_type == PICC_AUTHENT1B)
874 e2addr += 12; // key B offset
875 if ((status = Mf500HostCodeKey(uncoded_keys,coded_keys)) == MI_OK)
876 status = PcdWriteE2( e2addr,12,coded_keys);
877 return status;
878 }
879
880 ///////////////////////////////////////////////////////////////////////
881 // A U T H E N T I C A T I O N S T A T E S
882 ///////////////////////////////////////////////////////////////////////
883 char Mf500PiccAuthState( unsigned char auth_mode,
884 unsigned char *snr,
885 unsigned char block)
886 {
887 char status = MI_OK;
888 unsigned char i = 0;
889
890 status = ReadRC(RegErrorFlag); // read error flags of the previous
891 // key load
892 if (status != MI_OK)
893 {
894 if (status & 0x40) // key error flag set
895 status = MI_KEYERR;
896 else
897 status = MI_AUTHERR; // generic authentication error
898 }
899 else
900 {
901 PcdSetTmo(106);
902 MSndBuffer[0] = auth_mode; // write authentication command
903
904 MSndBuffer[1] = block; // write block number for authentication
905 memcpy(MSndBuffer + 2,snr,4); // write 4 bytes card serial number
906 ResetInfo(MInfo);
907 MInfo.nBytesToSend = 6;
908 if ((status = PcdSingleResponseCmd(PCD_AUTHENT1,
909 MSndBuffer,
910 MRcvBuffer,
911 &MInfo)) == MI_OK)
912 {
913 if (ReadRC(RegSecondaryStatus) & 0x07) // RxLastBits mu
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -