📄 mfrc500.c
字号:
/****************************************************************************
*
* File: MFRC500.C(for ChinaVision Internal use only)
*
* Created: 2002.12.25
*
* Editor JOHN CHENG
*
* Compiler: KEIL C51 V7.00
*
* Description: T89C51RD2-Firmware for RC500 Serial Reader
*
*****************************************************************************
*
* Version | Date | PCB | Description
* --------+----------+-------+------------------------------------------------
* V100 | 25/12/02 | | Initial version,TypeA and TypeB
* V110 | 15/01/03 | | reset pin control by FIFO out pins.Adding two new command
* | | | (READ EEPROM C6 and WRITE EEPROM C7).
* | | | delay using a fixed time,not "for loop".
* | | | New routine funtions is generated in main funtion.
* V120 | 20/01/03 | | Modify reader.h,adding rc500cmd.h
* V130 | 08/02/03 | | Adding some funtions about SAM
* | | |
****************************************************************************/
#include <absacc.h>
#include "string.h"
#include "stdio.h"
#include "intrins.h"
#include "reader.h"
#include "632MfErrNo.h"
#include "Rc632CV.h"
#include "Rc632reg.h"
//#include "des.h"
///////////////////////////////////////////////////////////////////////
// M I F A R E M O D U L E C O N F I G U R A T I O N
///////////////////////////////////////////////////////////////////////
//extern union u_ram;
//des_ctx dc;
//#define inf_NACK 0x08 //Tag card error
unsigned int MRC500_CNT; //Delay Counter
unsigned char CID;
extern unsigned char Tiflag;
//unsigned char cmd_len[1];
//unsigned char a[64];
unsigned char card_desuid[9];
unsigned char des_cid;
unsigned char select_key[17];
unsigned char session_key[16];
//unsigned char master_key_set;
//unsigned char max_keys;
unsigned char DesFire_KeyNoForAuth;
unsigned char des_app_no[3]; //应用号
unsigned char des_file_no; //文件号
//unsigned char des_file_type;
//unsigned char des_encrypt_mode;
unsigned char des_offset[3];
unsigned char des_read_len;
//unsigned char des_key_no;
extern bit TxFlag;
extern unsigned char STX_Pointerunin;
extern unsigned char STX_Pointer; //Start of transmit data pointer
extern unsigned char ETX_Pointer; //End of transmit data pointer]
extern unsigned char SEQ;
extern unsigned char SlaveADR;
extern unsigned char weigandFlag;
extern int des(unsigned char *source,unsigned char * dest,unsigned char * inkey, int flg);
extern void ComputeCrc(int CRCType, char *Data, int Length,unsigned char *TransmitFirst, unsigned char *TransmitSecond);
void CBC_3Des_SendToPICC(unsigned char *Data,unsigned char *Key,unsigned char *aSessionKey,int Len8);
void CBC_3Des_DataEnciphering(unsigned char *Data,unsigned char *Key,unsigned char *aSessionKey,int Len8);
void CBC_3Des_ReceiveFromPICC(unsigned char *Data,unsigned char *Key,unsigned char *aSessionKey,int Len8);
//#define rats_arg 5*16+des_cid
#define rats_arg 5*16
//unsigned char code packet_size[]={16,24,32,40,48,64,96,128,256};
void PcdSetFWI(unsigned char fwi)
{
RegTimerReload = 128;
RegTimerClock = fwi + 5;
}
void Mf500PcdConfig(void)
{
RegClockQControl=0x0;
RegClockQControl=0x40;
MRC500_CNT=0;
while(MRC500_CNT <= 10000){MRC500_CNT+=1;} //About 20 ms
RegClockQControl = RegClockQControl & (~0x40);
// The following values for RegBitPhase and
// RegRxThreshold represents an optimal
// value for our demo package. For user
// implementation some changes could be
// necessary
// initialize bit phase
RegBitPhase=0xAD;
// initialize minlevel
RegRxThreshold=0xFF;
// disable auto power down
RegRxControl2=1;
// Depending on the processing speed of the
// operation environment, the waterlevel
// can be adapted. (not very critical for
// mifare applications)
// initialize waterlevel to value 4
RegFIFOLevel=0x1A; // initialize to 26d
//Timer Konfiguration
RegTimerControl=0x02; // TStopRxEnd=0,TStopRxBeg=0,
// TStartTxEnd=1,TStartTxBeg=0
// timer must be stopped manually
RegIRqPinConfig=0x3; // interrupt active low enable
RegCwConductance=0xff;
PcdRfReset(1); // Rf - reset and enable output driver
}
//////////////////////////////////////////////////////////////////////
// RF R E S E T
///////////////////////////////////////////////////////////////////////
void PcdRfReset(unsigned char ms)
{unsigned char i;
RegTxControl = RegTxControl & (~0x03);
if (ms > 0)
{ for (i=0;i<ms;i++)
{ MRC500_CNT=0;
while(MRC500_CNT <=10000){MRC500_CNT+=1;}
}
RegTxControl = RegTxControl | 0x01;
}
}
///////////////////////////////////////////////////////////////////////
// S e t T i m e o u t L E N G T H
///////////////////////////////////////////////////////////////////////
void PcdSetTmo(unsigned char tmoLength)
{
switch(tmoLength)
{ // timer clock frequency 13,56 MHz
case 1: // short timeout (1,0 ms)
RegTimerClock=0x07; // TAutoRestart=0,TPrescale=128
RegTimerReload=0x6a;// TReloadVal = 'h6a =106(dec)
break;
case 2: // medium timeout (1,5 ms)
RegTimerClock=0x07; // TAutoRestart=0,TPrescale=128
RegTimerReload=0xa0;// TReloadVal = 'ha0 =160(dec)
break;
case 3: // long timeout (6 ms)
RegTimerClock=0x09; // TAutoRestart=0,TPrescale=4*128
RegTimerReload=0xa0;// TReloadVal = 'ha0 =160(dec)
break;
case 4: // long timeout (9.6 ms)
RegTimerClock=0x09; // TAutoRestart=0,TPrescale=4*128
RegTimerReload=0xff;// TReloadVal = 'ff =255(dec)
break;
default: // short timeout (1,0 ms)
RegTimerClock=0x07; // TAutoRestart=0,TPrescale=128
RegTimerReload=tmoLength;// TReloadVal = tmoLength
break;
}
}
//-------------------------------------------------------------
char MCRReset_1(void)
{
char i;
while (RegCommand & 0x3F) ;
RegPage=0;//0x80;//WriteRawRC(RegPage,0x80); // Dummy access in order to determine the bus
// configuration
// necessary read access
// after first write access, the returned value
// should be zero ==> interface recognized
if (RegCommand != 0x00)//if (ReadRawRC(RegCommand) != 0x00)
{ while(1)
i = MI_INTERFACEERR;
}
else
{
i = MI_OK; // sequence is ok
}
return i;
}
///////////////////////////////////////////////////////////////////////
// M I F A R E C O M M O N R E Q U E S T
///////////////////////////////////////////////////////////////////////
char Mf500PiccCommonRequest(unsigned char req_code, unsigned char *atq)
{
char status; // = MI_OK;
if(Tiflag != 1) settypeA();
//************* initialize ******************************
PcdSetTmo(106);
RegChannelRedundancy=0x03; // RxCRC and TxCRC disable, parity enable
RegControl = RegControl & (~0x08); // disable crypto 1 unit
RegBitFraming=0x07; // set TxLastBits to 7
RegInterruptEn=0x7F; // disable all interrupts
RegInterruptRq=0x7F; // reset interrupt requests
RegCommand=0x00; //PCD_IDLE; // terminate probably running command
RegControl=RegControl | 0x01;//clear FIFO
RegFIFOData=req_code; //write request code
RegInterruptEn=0xA4; //enable TimerIRq and IdleIRq
RegCommand=PCD_TRANSCEIVE; //0x1e; //start command
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -