?? rc500.c
字號:
#include <REGX52.H>
#include <absacc.h>
#include <string.h>
#include <RC500Err.h>
#include <RC500Reg.h>
#include <RC500.h>
typedef struct
{
unsigned char cmd;
char status;
unsigned char nBytesSent;
unsigned char nBytesToSend;
unsigned char nBytesReceived;
unsigned short nBitsReceived;
unsigned char irqSource;
unsigned char collPos;
} MfCmdInfo;
#define ResetInfo(info) \
info.cmd = 0;\
info.status = MI_OK;\
info.irqSource = 0;\
info.nBytesSent = 0;\
info.nBytesToSend = 0;\
info.nBytesReceived = 0;\
info.nBitsReceived = 0;\
info.collPos = 0;
// 往一個地址寫一個數據
//void WriteRawIO(unsigned char Address,unsigned char value);
// 從一個地址讀出一個數據
//unsigned char ReadRawIO(unsigned char Address);
// 往一個地址寫一個數據(EEPROM)
//void WriteIO(unsigned char Address, unsigned char value);
// 從一個地址讀出一個數據(EEPROM)
// unsigned char ReadIO(unsigned char Address);
// 設置定時時間
void M500PcdSetTmo(unsigned char tmoLength);
// Request Command defined in ISO14443(Mifare)
char M500PcdCmd(unsigned char cmd,
volatile unsigned char data *rcv,
MfCmdInfo idata *info);
// 置一個bit
char SetBitMask(unsigned char reg,unsigned char mask);
// 清一個bit
char ClearBitMask(unsigned char reg,unsigned char mask);
//
void FlushFIFO(void);
// Value format operations for Mifare Standard card ICs
char M500PiccValue(unsigned char dd_mode,
unsigned char addr,
unsigned char *value,
unsigned char trans_addr);
// Set card in HALT-state
// 終止卡的操作
char M500PiccHalt(void);
// Reset the MF RC500
//char M500PcdReset(void);
// Configures the MF RC500
char M500PcdConfig(void);
// Key loading into the MF RC500's EEPROM
char M500PcdLoadKeyE2(unsigned char key_type,
unsigned char sector,
unsigned char *uncoded_keys);
// Write the MF RC500's EEPROM
char PcdWriteE2(unsigned int startaddr,
unsigned char length,
unsigned char* _data);
// Select Command defined in ISO14443(MIFARE)
char M500PcdMfOutSelect(unsigned char type);
// Request Command defined in ISO14443(MIFARE)
// Request,Anticoll,Select,return CardType(2 bytes)+CardSerialNo(4 bytes)
// 尋卡,防沖突,選擇卡 返回卡類型(2 bytes)+ 卡系列號(4 bytes)
char M500PiccCommonRequest(unsigned char req_code,unsigned char *atq);
// Cascaded Anti-Collision Command defined in ISO14443(MIFARE)
// 防沖突 讀卡的系列號 MLastSelectedSnr
char M500PiccCascAnticoll (unsigned char bcnt,unsigned char *snr);
// Cascaded Select command defined in ISO14443(MIFARE)
// 選擇卡 Select Card
char M500PiccCascSelect(unsigned char *snr,unsigned char *sak);
// Key loading into the MF RC500's EEPROM
// 校驗卡密碼(E2)
char M500PiccAuthE2( unsigned char auth_mode,unsigned char *snr,
unsigned char keynr,unsigned char block);
// Authentication key coding
char M500HostCodeKey( unsigned char *uncoded, unsigned char *coded);
// Authentication with direct key loading from the uC
// 直接校驗密碼
//char M500PiccAuthKey( unsigned char auth_mode,
// unsigned char *snr,
// unsigned char *keys,
// unsigned char block);
//
char M500PiccAuthState( unsigned char auth_mode,unsigned char *snr,unsigned char block);
// Read the mifare card
// 讀卡
char M500PiccRead(unsigned char addr,unsigned char *_data);
// Write the mifare card
// 寫卡 下載密碼
char M500PiccWrite( unsigned char addr,unsigned char *_data);
// Reset Rf Card
char M500PcdRfReset(unsigned char ms);
// Config
char MfConfig(void);
static volatile MfCmdInfo idata MInfo;
//static volatile MfCmdInfo *MpIsrInfo = 0;
static volatile MfCmdInfo *MpIsrInfo;
//static volatile unsigned char *MpIsrOut = 0;
static volatile unsigned char *MpIsrOut;
unsigned char SerBuffer[20];
void delay_100us()
{
unsigned char i=100;
while(i--);
}
void delay_ms(unsigned int ms)
{
unsigned char i;
while(ms--)
{
i=112;
while(i--);
}
}
///////////////////////////////////////////////////////////////////////
// 往一個地址寫一個數據
///////////////////////////////////////////////////////////////////////
void WriteRawIO(unsigned char Address,unsigned char value)
{
XBYTE[Address]=value;
}
///////////////////////////////////////////////////////////////////////
// 從一個地址讀出一個數據
///////////////////////////////////////////////////////////////////////
unsigned char ReadRawIO(unsigned char Address)
{
return XBYTE[Address];
}
///////////////////////////////////////////////////////////////////////
// 往一個地址寫一個數據(EEPROM)
///////////////////////////////////////////////////////////////////////
void WriteIO(unsigned char Address, unsigned char value)
{
WriteRawIO(0x00,GetRegPage(Address));
WriteRawIO(Address,value);
}
///////////////////////////////////////////////////////////////////////
// 從一個地址讀出一個數據(EEPROM)
///////////////////////////////////////////////////////////////////////
unsigned char ReadIO(unsigned char Address)
{
WriteRawIO(0x00,GetRegPage(Address));
return ReadRawIO(Address);
}
///////////////////////////////////////////////////////////////////////
// 設置定時時間
///////////////////////////////////////////////////////////////////////
void M500PcdSetTmo(unsigned char tmoLength)
{
switch(tmoLength)
{
case 1:
WriteIO(RegTimerClock,0x07);
WriteIO(RegTimerReload,0x6a);
break;
case 2:
WriteIO(RegTimerClock,0x07);
WriteIO(RegTimerReload,0xa0);
break;
case 3:
WriteIO(RegTimerClock,0x09);
WriteIO(RegTimerReload,0xa0);
break;
case 4:
WriteIO(RegTimerClock,0x09);
WriteIO(RegTimerReload,0xff);
break;
case 5:
WriteIO(RegTimerClock,0x0b);
WriteIO(RegTimerReload,0xff);
break;
case 6:
WriteIO(RegTimerClock,0x0d);
WriteIO(RegTimerReload,0xff);
break;
case 7:
WriteIO(RegTimerClock,0x0f);
WriteIO(RegTimerReload,0xff);
break;
default:
WriteIO(RegTimerClock,0x07);
WriteIO(RegTimerReload,tmoLength);
break;
}
}
///////////////////////////////////////////////////////////////////////
// Request Command defined in ISO14443(Mifare)
///////////////////////////////////////////////////////////////////////
char M500PcdCmd(unsigned char cmd,
volatile unsigned char data *rcv,
MfCmdInfo idata *info)
{
char idata status = MI_OK;
char idata tmpStatus ;
unsigned char idata lastBits;
unsigned int idata timecnt = 0;
unsigned char idata irqEn = 0x00;
unsigned char idata waitFor = 0x00;
unsigned char idata timerCtl = 0x00;
WriteIO(RegInterruptEn,0x7F);
WriteIO(RegInterruptRq,0x7F);
WriteIO(RegCommand,PCD_IDLE);
FlushFIFO();
MpIsrInfo = info;
MpIsrOut = rcv;
info->irqSource = 0x00;
switch(cmd)
{
case PCD_IDLE:
irqEn = 0x00;
waitFor = 0x00;
break;
case PCD_WRITEE2:
irqEn = 0x11;
waitFor = 0x10;
break;
case PCD_READE2:
irqEn = 0x07;
waitFor = 0x04;
break;
case PCD_LOADCONFIG:
case PCD_LOADKEYE2:
case PCD_AUTHENT1:
irqEn = 0x05;
waitFor = 0x04;
break;
case PCD_CALCCRC:
irqEn = 0x11;
waitFor = 0x10;
break;
case PCD_AUTHENT2:
irqEn = 0x04;
waitFor = 0x04;
break;
case PCD_RECEIVE:
info->nBitsReceived = -(ReadIO(RegBitFraming) >> 4);
irqEn = 0x06;
waitFor = 0x04;
break;
case PCD_LOADKEY:
irqEn = 0x05;
waitFor = 0x04;
break;
case PCD_TRANSMIT:
irqEn = 0x05;
waitFor = 0x04;
break;
case PCD_TRANSCEIVE:
info->nBitsReceived = -(ReadIO(RegBitFraming) >> 4);
irqEn = 0x3D;
waitFor = 0x04;
break;
default:
status = MI_UNKNOWN_COMMAND;
}
if (status == MI_OK)
{
irqEn |= 0x20;
waitFor |= 0x20;
timecnt=1000;
WriteIO(RegInterruptEn,irqEn | 0x80);
WriteIO(RegCommand,cmd);
while (!(MpIsrInfo->irqSource & waitFor||!(timecnt--)));
WriteIO(RegInterruptEn,0x7F);
WriteIO(RegInterruptRq,0x7F);
SetBitMask(RegControl,0x04);
WriteIO(RegCommand,PCD_IDLE);
if (!(MpIsrInfo->irqSource & waitFor))
{
status = MI_ACCESSTIMEOUT;
}
else
{
status = MpIsrInfo->status;
}
if (status == MI_OK)
{
if (tmpStatus = (ReadIO(RegErrorFlag) & 0x17))
{
if (tmpStatus & 0x01)
{
info->collPos = ReadIO(RegCollPos);
status = MI_COLLERR;
}
else
{
info->collPos = 0;
if (tmpStatus & 0x02)
{
status = MI_PARITYERR;
}
}
if (tmpStatus & 0x04)
{
status = MI_FRAMINGERR;
}
if (tmpStatus & 0x10)
{
FlushFIFO();
status = MI_OVFLERR;
}
if (tmpStatus & 0x08)
{
status = MI_CRCERR;
}
if (status == MI_OK)
status = MI_NY_IMPLEMENTED;
}
if (cmd == PCD_TRANSCEIVE)
{
lastBits = ReadIO(RegSecondaryStatus) & 0x07;
if (lastBits)
info->nBitsReceived += (info->nBytesReceived-1) * 8 + lastBits;
else
info->nBitsReceived += info->nBytesReceived * 8;
}
}
else
{
info->collPos = 0x00;
}
}
MpIsrInfo = 0;
MpIsrOut = 0;
return status;
}
///////////////////////////////////////////////////////////////////////
// 置一個bit
///////////////////////////////////////////////////////////////////////
char SetBitMask(unsigned char reg,unsigned char mask)
{
char idata tmp = 0x00;
tmp = ReadIO(reg);
WriteIO(reg,tmp | mask); // set bit mask
return 0x00;
}
///////////////////////////////////////////////////////////////////////
// 清一個bit
///////////////////////////////////////////////////////////////////////
char ClearBitMask(unsigned char reg,unsigned char mask)
{
char idata tmp = 0x00;
tmp = ReadIO(reg);
WriteIO(reg,tmp & ~mask); // clear bit mask
return 0x00;
}
///////////////////////////////////////////////////////////////////////
//清除FIFO
///////////////////////////////////////////////////////////////////////
void FlushFIFO(void)
{
SetBitMask(RegControl,0x01);
}
///////////////////////////////////////////////////////////////////////
// Value format operations for Mifare Standard card ICs
///////////////////////////////////////////////////////////////////////
/*char M500PiccValue(unsigned char dd_mode,
unsigned char addr,
unsigned char *value,
unsigned char trans_addr)
{
char status = MI_OK;
M500PcdSetTmo(1);
ResetInfo(MInfo);
SerBuffer[0] = dd_mode;
SerBuffer[1] = addr;
MInfo.nBytesToSend = 2;
status = M500PcdCmd(PCD_TRANSCEIVE,
SerBuffer,
&MInfo);
if (status != MI_NOTAGERR)
{
if (MInfo.nBitsReceived != 4)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -