?? main.c
字號:
#include <string.h>
#include <stdio.h>
#include <main.h>
#include <m500a.h>
#include <p89c51rx.h>
#include <Mfreg531.h>
#include <EvalOs.h>
#define LED P3_5
unsigned char ReadBuf[128];
unsigned char nkey[6]={0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5};
//unsigned char nkey[6]={0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
unsigned char BLK=0;
// TCL protocol's setting variables
unsigned char FWI=5,SFCI;
void init(void);
void Blink_LED(void);
char TCL_Protocol(unsigned char CID);
char TCL_Protocol2(unsigned char CID);
void delay_50us(unsigned int _50us);
char CL_Deselect(unsigned char CID);
char CL_GetChallenge(unsigned char CID, unsigned char *Buf);
char CL_SelectFile(unsigned char CID,
unsigned char P1,
unsigned char P2,
unsigned char *file,
unsigned char *Buf);
char WriteInfineonCard (unsigned char *Buf, unsigned int adr, unsigned char len);
char ReadInfineonCard (unsigned char *Buf, unsigned int adr, unsigned char len);
void PrepareValue (unsigned long value, unsigned char *buf);
void main(void)
{
char idata status,temp;
unsigned char idata i,atq[2],snr[10],sak,snr_len,TA;
init(); // Initialise system
status = M500PcdConfig(); // Initialise the RC500
// must be call in the initialisation
M500PcdMfOutSelect(mfout); // Route serial output to pin, for test
LED=0;
delay_50us(10000);
LED=1;
// LOADING KEYS
for(i=0; i<16; i++)
{
//M500PcdLoadMk(PICC_AUTHENT1A,i,nkey); // load RAM
M500PcdLoadKeyE2(PICC_AUTHENT1A,i,nkey); // load EEPROM
}
while(1)
{
// IS014443B
M500PcdConfigISOType(TYPEB);
status = M531PiccRequestB(PICC_REQBWUP,0,0,ReadBuf); // ReqB with 1 slot
if(status==MI_OK || status==MI_COLLERR)
{
if(status == MI_COLLERR) // more then 1 card
{
// Anti-collision by probabilistic
/*for(i=0; i<4; i++)
{
status = M531PiccRequestB(PICC_REQBSTD,0,2,ReadBuf); // ReqB with 4 slots
if(status==MI_OK) break;
}
if(status!=MI_OK) continue; //error occur
FWI = ReadBuf[11]>>4; // Set the FWT according to the protocol info
status = M531PiccAttrib(&ReadBuf[1],0,0,0); // Select PICC
*/
// Anti-collision using slot-marker
if((status=M531PiccRequestB(PICC_REQBSTD,0,2,ReadBuf)) != MI_OK) // ReqB with 4 slot
{
for (i=1; i<4; i++)
if((status=M531PiccSlotMarker(i,ReadBuf)) == MI_OK) break;
}
if (status != MI_OK) continue;
}
Blink_LED();
FWI = ReadBuf[11]>>4; // Set the FWT according to the protocol info
if((status=M531PiccAttrib(&ReadBuf[1],0,0,0,ReadBuf[10]&0x0f))!=MI_OK) continue; // Select PICC
status = TCL_Protocol(0);
}
// IS014443A
M500PcdConfigISOType(TYPEA);
while(1)
{
if((status=M500PiccActivateAll(0,atq,&sak,snr,&snr_len))==MI_OK)
{
if((sak&0x24) == 0x20) /* ISO1443A commands, T=CL */
{
ReadBuf[0]=0xE0; // RATS commands
ReadBuf[1]=0x50; // FSD=128, CID=0
if(M500PiccExchangeBlock(ReadBuf,4,ReadBuf,&temp,1,4)==MI_OK) // length incl CRC
{
/* ATS received in ReadBuf
START EXCHANGING TRANSPARENT DATA */
i = 2;
if(ReadBuf[1]&0x40) TA=ReadBuf[i++];
else TA=0;
if(ReadBuf[1]&0x20) FWI=ReadBuf[i]>>4;
SFCI=ReadBuf[1]&0x0f; //max PICC receive lenght
/*
if(TA) //request PPSS
{
ReadBuf[0]=0xd0; // PPSS commands
ReadBuf[1]=0x11; // PPS0
ReadBuf[2]=0x33; // PPS1, DRI=8, DSI=8
if(M500PiccExchangeBlock(ReadBuf,5,ReadBuf,&temp,1,FWI)==MI_OK) // length incl CRC
{
M500PcdWriteRegister(0x14,0x01); // ISO14443A, 848KBaud
M500PcdWriteRegister(0x19,0x13); // Rcv 848KBaud
}
} */
Blink_LED();
TCL_Protocol2(0);
CL_Deselect(0);
}
}
else if(!sak) /* Mifare Ultra-Light */
{
ReadBuf[0]=0x30;
ReadBuf[1]=0x7;
M500PiccExchangeBlock(ReadBuf,4,ReadBuf,&temp,1,FWI); //length incl CRC
ReadBuf[0]=0xa2;
ReadBuf[1]=0x7;
ReadBuf[2]=0x1;
ReadBuf[3]=0x3;
ReadBuf[4]=0x5;
ReadBuf[5]=0x7;
ReadBuf[6]=0x23;
ReadBuf[7]=0x76;
M500PiccExchangeBlock(ReadBuf,8,ReadBuf,&temp,1,FWI); //length incl CRC
}
else /* Mifare Classic commands */
{
//if((status=M500PiccAuth(PICC_AUTHENT1A,snr,0,0))!=MI_OK) continue;
if((status=M500PiccAuthE2(PICC_AUTHENT1A,snr,0,62))!=MI_OK) continue;
//PrepareValue(100,ReadBuf);
//if(M500PiccWrite(62,ReadBuf)!=MI_OK) continue;
if(M500PiccRead(62,ReadBuf)!=MI_OK) continue;
PrepareValue(1,ReadBuf);
if(M500PiccValue(PICC_DECREMENT,62,ReadBuf,62)!=MI_OK) continue;
if(M500PiccValue(PICC_RESTORE,62,ReadBuf,61)!=MI_OK) continue;
if(M500PiccRead(61,ReadBuf)!=MI_OK) continue;
status = M500PiccHalt();
Blink_LED();
}
}
break;
}
}
}
void init(void)
{
LED = 0;
P2_7 = 0; // Enable the CS for RC500
PMR = 0x41; // For emulation
IT0 = 1; // Config ext0 as edge trigger for RC500
EX0 = 1; // Enable ext0 interrupt for RC500
EA = 1; // Enable global interrupt
}
char TCL_Protocol(unsigned char CID)
{
char idata status,temp;
unsigned char buffer[16];
unsigned long duration;
LED = 1;
buffer[0] = 0x11;
buffer[1] = 0x22;
buffer[2] = 0x33;
buffer[4] = 0x44;
buffer[5] = 0x55;
buffer[6] = 0x66;
WriteInfineonCard (buffer, 0, 6);
ReadInfineonCard (ReadBuf, 0, 7);
buffer[0]=0x3f;
buffer[1]=0x00;
if(CL_SelectFile(CID,0,0,buffer,ReadBuf)==MI_OK)
{
LED = 1;
LED = 0;
}
M500PcdStopWatch(1); // start RC500 Timer Stop-watch
if(CL_GetChallenge(CID,ReadBuf)==MI_OK)
{
LED = 1;
LED = 0;
}
duration = M500PcdStopWatch(0); // stop RC500 Timer Stop-watch
delay_50us(1000);
LED = 0;
return(status);
}
char TCL_Protocol2(unsigned char CID)
{
char idata status,temp;
unsigned long duration;
BLK = 0; // initialise the 1st block
if(EvalOS_GetUID(CID,ReadBuf)==MI_OK)
{
Blink_LED();
}
if(EvalOS_GetRomInfo(CID,ReadBuf)==MI_OK)
{
Blink_LED();
}
if(EvalOS_GetChipInfo(CID,ReadBuf)==MI_OK)
{
Blink_LED();
}
delay_50us(1000);
return(status);
}
char CL_Deselect(unsigned char CID)
{
unsigned char len,Buf[6];
Buf[0]=0xCA; // S-block PCB with CID
Buf[1]=CID; // CID
if(M500PiccExchangeBlock(Buf,4,Buf,&len,1,FWI)!=MI_OK) return(!MI_OK);
if(Buf[0]==0x90 && Buf[1]==0x00) return(MI_OK);
else return(!MI_OK);
}
char CL_GetChallenge(unsigned char CID, unsigned char *Buf)
{
unsigned char len;
Buf[0]=0x0A; // I-block PCB with CID
Buf[1]=CID; // CID
Buf[2]=0xb8; // INF:CLA
Buf[3]=0x84; // INF:INS
Buf[4]=0x00; // INF:P1
Buf[5]=0x00; // INF:P2
Buf[6]=0x04; // INF:Le
if(M500PiccExchangeBlock(Buf,9,Buf,&len,1,FWI)!=MI_OK) return(!MI_OK);
if(Buf[len-4]==0x90) return(MI_OK);
else return(!MI_OK);
}
char CL_SelectFile(unsigned char CID,
unsigned char P1,
unsigned char P2,
unsigned char *file,
unsigned char *Buf)
{
unsigned char len;
Buf[0]=0x0A; // I-block PCB with CID
Buf[1]=CID; // CID
Buf[2]=0x00; // INF:CLA
Buf[3]=0xA4; // INF:INS
Buf[4]=P1; // INF:P1
Buf[5]=P2; // INF:P2
if(P1&0x04) // Selection by DF name
{
len=0;
while(file[len]!=0)
Buf[7+len] = file[len++];
Buf[6]=len;
len += 9;
}
else // Selection by FID
{
Buf[6]=2; // INF:Lc
Buf[7]=file[0]; // INF:Data
Buf[8]=file[1]; // INF:Data
len = 11;
}
if(M500PiccExchangeBlock(Buf,len,Buf,&len,1,FWI)!=MI_OK) return(!MI_OK);
if(Buf[len-4]==0x90) return(MI_OK);
else return(!MI_OK);
}
char WriteInfineonCard (unsigned char *Buf, unsigned int adr, unsigned char len)
{
unsigned char lenght;
for(lenght=len+5; lenght>5; lenght--)
Buf[lenght]=Buf[lenght-6];
Buf[0]=0x0A; // I-block PCB with CID
Buf[1]=0; // CID Buf[3]=0xB0;
Buf[2]=0xD0; // write a block
Buf[3]=(unsigned char)(adr>>8);
Buf[4]=(unsigned char)(adr);
Buf[5]=len; // lenght of data to be read
if(M500PiccExchangeBlock(Buf,len+8,Buf,&lenght,1,FWI)!=MI_OK) return(!MI_OK);
else return(MI_OK);
}
char ReadInfineonCard (unsigned char *Buf, unsigned int adr, unsigned char len)
{
unsigned char lenght;
Buf[0]=0x0A; // I-block PCB with CID
Buf[1]=0; // CID Buf[3]=0xB0;
Buf[2]=0xB0; // read a block
Buf[3]=(unsigned char)(adr>>8);
Buf[4]=(unsigned char)(adr);
Buf[5]=len; // lenght of data to be read
if(M500PiccExchangeBlock(Buf,8,Buf,&lenght,1,FWI)!=MI_OK) return(!MI_OK);
else
{
for(lenght=0; lenght<len; lenght++)
Buf[lenght]=Buf[lenght+4];
Buf[lenght]=0;
return(MI_OK);
}
}
void PrepareValue (unsigned long value, unsigned char *buf)
{
unsigned char i;
for (i=0; i<4; i++)
{
buf[i] = buf[8+i] = (unsigned char)(value>>(8*i));
buf[4+i] = ~buf[i];
}
buf[12] = 0;
buf[13] = 255;
buf[14] = 0;
buf[15] = 255;
}
void Blink_LED(void)
{
LED=0;
delay_50us(2500);
LED=1;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -