📄 main.#3
字号:
}
else
{
status = MI_OK;
for(i=0;i<2;i++) atq[i]=MRcvBuffer[i];
}
}
return status;
}
/****************************************************************************
*在一个成功的AntiCollision指令之后,或在任何时候当程序员想实际地与已知序列号
卡片进行通信时,必须使用Select指令,以建立与所选卡的通信。为了允许在Select指令
以后对卡片能进行Read/Write等指令的操作,Select指令是很重要的,必须首先被使用
被选择的卡片将给出其自己的存储器容量---已编码的一个BYTE(字节) *
* Function: Mf500PiccSelect *
* *
* Input: SNR 卡号 *
* Output: Size *
* *
****************************************************************************/
char Mf500PiccSelect(unsigned char *snr,unsigned char *sak)
{
return Mf500PiccCascSelect(0x93,snr,sak); // first cascade level
}
char Mf500PiccCascSelect(unsigned char select_code,
unsigned char *snr,
unsigned char *sak)
{
char i;
status = MI_OK;
PcdSetTmo(106);
WriteRC(RegChannelRedundancy,0x0F); // RxCRC,TxCRC, Parity enable
ClearBitMask(RegControl,0x08); // disable crypto 1 unit
ResetInfo();
MSndBuffer[0] = select_code;
MSndBuffer[1] = 0x70; // number of bytes send
for(i=0;i<4;i++) MSndBuffer[2+i]=snr[i];
//memcpy(MSndBuffer + 2,snr,4);
MSndBuffer[6] = MSndBuffer[2]
^ MSndBuffer[3]
^ MSndBuffer[4]
^ MSndBuffer[5];
MInfo.nBytesToSend = 7;
status = PcdSingleResponseCmd(PCD_TRANSCEIVE);
sak[0] = 0;
if (status == MI_OK) // no timeout occured
{
if (MInfo.nBitsReceived != 8) // last byte is not complete
{
status = MI_BITCOUNTERR;
}
else
{
sak[0] = MRcvBuffer[0];
memcpy(MLastSelectedSnr,snr,4);
}
}
return status;
}
/****************************************************************************
*如果有多于一张的Mifare 1卡片在MCM之天线的有效的工作范围(距离)内 ,必须使用
AntiCollision指令,使MCM能够在这一叠 Mifare 1卡片中选择个别的一张卡片。
*
* Function: Mf500PiccAnticoll//防碰撞函数 *
* *
* Input: Bcnt, SNR *
* Output: SNR *
* *
****************************************************************************/
char Mf500PiccAnticoll (unsigned char bcnt,
unsigned char *snr)
{
return Mf500PiccCascAnticoll(0x93,bcnt,snr); // first cascade level
}
char Mf500PiccCascAnticoll (unsigned char select_code,
unsigned char bcnt,
unsigned char *snr)
{
char snr_in[4]; // copy of the input parameter snr
char nbytes = 0; // how many bytes received
char nbits = 0; // how many bits received
char complete = 0; // complete snr recived
char i = 0;
char byteOffset = 0;
unsigned char snr_crc; // check byte calculation
unsigned char snr_check;
unsigned char dummyShift1; // dummy byte for snr shift
unsigned char dummyShift2; // dummy byte for snr shift
status = MI_OK;
PcdSetTmo(2);
memcpy(snr_in,snr,4);
WriteRC(RegDecoderControl,0x28); // ZeroAfterColl aktivieren
ClearBitMask(RegControl,0x08); // disable crypto 1 unit
complete=0;
while (!complete && (status == MI_OK) )
{
ResetInfo();
WriteRC(RegChannelRedundancy,0x03); // RxCRC and TxCRC disable, parity enable
nbits = bcnt % 8;
if (nbits)
{
WriteRC(RegBitFraming,nbits << 4 | nbits); // TxLastBits/RxAlign auf nb_bi
nbytes = bcnt / 8 + 1;
if (nbits == 7 )
{
MInfo.cmd = PICC_ANTICOLL1; // pass command flag to ISR
WriteRC(RegBitFraming,nbits); // reset RxAlign to zero
}
}
else
{
nbytes = bcnt / 8;
}
MSndBuffer[0] = select_code;
MSndBuffer[1] = 0x20 + ((bcnt/8) << 4) + nbits; //number of bytes send
for (i = 0; i < nbytes; i++) // Sende Buffer beschreiben
{
MSndBuffer[i + 2] = snr_in[i];
}
MInfo.nBytesToSend = 2 + nbytes;
status = PcdSingleResponseCmd(PCD_TRANSCEIVE);
// in order to solve an inconsistancy in the anticollision sequence
// (will be solved soon), the case of 7 bits has to be treated in a
// separate way
if (nbits == 7)
{
// reorder received bits
dummyShift1 = 0x00;
for (i = 0; i < MInfo.nBytesReceived; i++)
{
dummyShift2 = MRcvBuffer[i];
MRcvBuffer[i] = (dummyShift1 >> (i+1)) | (MRcvBuffer[i] << (7-i));
dummyShift1 = dummyShift2;
}
MInfo.nBitsReceived -= MInfo.nBytesReceived; // subtract received parity bits
// recalculation of collision position
if ( MInfo.collPos ) MInfo.collPos += 7 - (MInfo.collPos + 6) / 9;
}
if ( status == MI_OK || status == MI_COLLERR) // no other occured
{
// R e s p o n s e P r o c e s s i n g
if ( MInfo.nBitsReceived != 40) // not 5 bytes answered
{
status = MI_BITCOUNTERR;
}
else
{
byteOffset = 0;
if ( nbits != 0 ) // last byte was not complete
{
snr_in[nbytes - 1] = snr_in[nbytes - 1] | MRcvBuffer[0];
byteOffset = 1;
}
for ( i =0; i < (4 - nbytes); i++)
{
snr_in[nbytes + i] = MRcvBuffer[i + byteOffset];
}
if (status != MI_COLLERR ) // no error and no collision
{
// SerCh check
snr_crc = snr_in[0] ^ snr_in[1] ^ snr_in[2] ^ snr_in[3];
snr_check = MRcvBuffer[4];
if (snr_crc != snr_check)
{
status = MI_SERNRERR;
}
else
{
complete = 1;
}
}
else // collision occured
{
bcnt = bcnt + MInfo.collPos - nbits;
status = MI_OK;
}
}
}
}
if (status == MI_OK)
{
for(i=0;i<4;i++)
{
// csn[i]=MRcvBuffer[i];
snr[i]=MRcvBuffer[i];
}
}
else
{
_nop_();
}
ClearBitMask(RegDecoderControl,0x20); // ZeroAfterColl disable
return status;
}
///////////////////////////////////////////////////////////////////////
// M I F A R E A U T H E N T I C A T I O N
// calling compatible version
///////////////////////////////////////////////////////////////////////
/*
char Mf500PiccAuth(unsigned char key_type, // PICC_AUTHENT1A or PICC_AUTHENT1B
unsigned char key_addr, // key address in reader storage
unsigned char block) // block number which should be
// authenticated
{
char status = MI_OK;
status = Mf500PiccAuthE2( key_type,
MLastSelectedSnr,
key_addr,
block);
return status;
}
*/
///////////////////////////////////////////////////////////////////////
// A U T H E N T I C A T I O N
// W I T H K E Y S F R O M E 2 P R O M
///////////////////////////////////////////////////////////////////////
char Mf500PiccAuthE2(unsigned char auth_mode, // PICC_AUTHENT1A or PICC_AUTHENT1B
unsigned char *snr, // 4 bytes card serial number
unsigned char key_sector, // 0 <= key_sector <= 15
unsigned char block) // 0 <= block <= 63
{
// char i;
// eeprom address calculation
// 0x80 ... offset
// key_sector ... sector
// 0x18 ... 2 * 12 = 24 = 0x18
unsigned short e2addr = 0x80 + key_sector * 0x18;
unsigned char *e2addrbuf = (unsigned char*)&e2addr;
status = MI_OK;
PcdSetTmo(2);
if (auth_mode == PICC_AUTHENT1B)
e2addr += 12; // key B offset
FlushFIFO(); // empty FIFO
ResetInfo();
//memcpy(MSndBuffer,e2addrbuf,2); // write low and high byte of address
///////////////////////////////////////////////////
MSndBuffer[0] = e2addr & 0xFF;
MSndBuffer[1] = (e2addr >> 8) & 0xFF;
///////////////////////////////////////////////////////
// for(i=0;i<2;i++) MSndBuffer[i]=e2addrbuf[i];
MInfo.nBytesToSend = 2;
// write load command
status=PcdSingleResponseCmd(PCD_LOADKEYE2);
if(status==MI_OK)
{
// execute authentication
status = Mf500PiccAuthState(auth_mode,snr,block);
}
return status;
}
//////////////////////////////////////////////////////////
//注意程序只能读出非密钥存贮区
///////////////////////////////////////////////////////////
/*
char PcdReadE2(unsigned char sector,
unsigned char length,
unsigned char *mdata)
{
unsigned short e2addr = 0x30 + sector * 0x18; ///非密钥存贮区地址偏移,30为可读出的起始地址偏移
status = MI_OK;
ResetInfo();
MSndBuffer[0] = e2addr & 0xFF;
MSndBuffer[1] = (e2addr >> 8) & 0xFF;
MSndBuffer[2] = length;
MInfo.nBytesToSend = 3;
status = PcdSingleResponseCmd(PCD_READE2);
if (status == MI_OK)
{
memcpy(mdata,MRcvBuffer,length);
}
else // Response Processing
{
mdata[0] = 0;
}
return status ;
}
*/
//////////////////////////////////////////////////
//////////////////////////////////////////////////
char PcdWriteE2( unsigned int startaddr,
unsigned char length,
unsigned char *mdata)
{
status = MI_OK;
ResetInfo();
MSndBuffer[0] = startaddr & 0xFF;
MSndBuffer[1] = (startaddr >> 8) & 0xFF;
memcpy(MSndBuffer + 2,mdata,length);
MInfo.nBytesToSend = length + 2;
status = PcdSingleResponseCmd(PCD_WRITEE2); // write e2
return status;
}
///////////////////////////////////////////////////////////////////////
// C O D E K E Y S 将密钥转换成RC500格式
///////////////////////////////////////////////////////////////////////
char Mf500HostCodeKey( unsigned char *uncoded, // 6 bytes key value uncoded
unsigned char *coded) // 12 bytes key value coded
{
unsigned char cnt = 0;
unsigned char ln = 0; // low nibble
unsigned char hn = 0; // high nibble
status = MI_OK;
for (cnt = 0; cnt < 6; cnt++)
{
ln = uncoded[cnt] & 0x0F;
hn = uncoded[cnt] >> 4;
coded[cnt * 2 + 1] = (~ln << 4) | ln;
coded[cnt * 2 ] = (~hn << 4) | hn;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -