📄 m500auc.c
字号:
{
if (status & 0x40) // key error flag set
status = MI_KEYERR;
else
status = MI_AUTHERR; // generic authentication error
}
else
{
Pt = &MSndBuffer[0];
MSndBuffer[0] = auth_mode; // write authentication command
MSndBuffer[1] = block; // write block number for authentication
memcpy(MSndBuffer + 2,snr,4); // write 4 bytes card serial number
ResetInfo(MInfo);
MInfo.nBytesToSend = 6;
if ((status = M500PcdCmd(PCD_AUTHENT1,
MSndBuffer,
MRcvBuffer,
&MInfo)) == MI_OK)
{
if (ReadIO(RegSecondaryStatus) & 0x07) // Check RxLastBits for error
{
status = MI_BITCOUNTERR;
}
else
{
ResetInfo(MInfo);
MInfo.nBytesToSend = 0;
if ((status = M500PcdCmd(PCD_AUTHENT2,
MSndBuffer,
MRcvBuffer,
&MInfo)) == MI_OK)
{
if ( ReadIO(RegControl) & 0x08 ) // Crypto1 activated
{
status = MI_OK;
AccOk = 1;
}
else
{
status = MI_AUTHERR;
}
}
}
}
}
return status;
}
///////////////////////////////////////////////////////////////////////
// M I F A R E R E A D
///////////////////////////////////////////////////////////////////////
char M500PiccRead( unsigned char addr,
unsigned char *_data)
{
char data status = MI_OK;
char data tmp = 0;
FlushFIFO(); // empty FIFO
M500PcdSetTmo(3); // long timeout
WriteIO(RegChannelRedundancy,0x0F); // RxCRC, TxCRC, Parity enable
// ************* Cmd Sequence **********************************
ResetInfo(MInfo);
Pt = MSndBuffer ;
MSndBuffer[0] = PICC_READ; // read command code
MSndBuffer[1] = addr;
MInfo.nBytesToSend = 2;
status = M500PcdCmd(PCD_TRANSCEIVE,
MSndBuffer,
MRcvBuffer,
&MInfo);
if (status != MI_OK)
{
if (status != MI_NOTAGERR ) // no timeout occured
{
if (MInfo.nBitsReceived == 4) // NACK
{
MRcvBuffer[0] &= 0x0f; // mask out upper nibble
if ((MRcvBuffer[0] & 0x0a) == 0)
{
status = MI_NOTAUTHERR;
}
else
{
status = MI_CODEERR;
}
}
}
memcpy(_data,"0000000000000000",16); // in case of an error initialise
// data
}
else // Response Processing
{
if (MInfo.nBytesReceived != 16)
{
status = MI_BYTECOUNTERR;
memcpy(_data,"0000000000000000",16);
}
else
{
Pt = MRcvBuffer;
Pt = _data;
memcpy(_data,MRcvBuffer,16);
// RingOn();
}
}
M500PcdSetTmo(1); // short timeout
return status;
}
///////////////////////////////////////////////////////////////////////
// M I F A R E W R I T E
///////////////////////////////////////////////////////////////////////
char M500PiccWrite( unsigned char addr,
unsigned char *_data)
{
char data status = MI_OK;
// ************* Cmd Sequence **********************************
ResetInfo(MInfo);
MSndBuffer[0] = PICC_WRITE; // Write command code
MSndBuffer[1] = addr;
MInfo.nBytesToSend = 2;
status = M500PcdCmd(PCD_TRANSCEIVE,
MSndBuffer,
MRcvBuffer,
&MInfo);
if (status != MI_NOTAGERR) // no timeout error
{
if (MInfo.nBitsReceived != 4) // 4 bits are necessary
{
status = MI_BITCOUNTERR;
}
else // 4 bit received
{
MRcvBuffer[0] &= 0x0f; // mask out upper nibble
if ((MRcvBuffer[0] & 0x0a) == 0)
{
status = MI_NOTAUTHERR;
}
else
{
if (MRcvBuffer[0] == 0x0a)
{
status = MI_OK;
}
else
{
status = MI_CODEERR;
}
}
}
}
if ( status == MI_OK)
{
M500PcdSetTmo(3); // long timeout
ResetInfo(MInfo);
memcpy(MSndBuffer,_data,16);
MInfo.nBytesToSend = 16;
status = M500PcdCmd(PCD_TRANSCEIVE,
MSndBuffer,
MRcvBuffer,
&MInfo);
if (status & 0x80) // timeout occured
{
status = MI_NOTAGERR;
}
else
{
if (MInfo.nBitsReceived != 4) // 4 bits are necessary
{
status = MI_BITCOUNTERR;
}
else // 4 bit received
{
MRcvBuffer[0] &= 0x0f; // mask out upper nibble
if ((MRcvBuffer[0] & 0x0a) == 0)
{
status = MI_WRITEERR;
}
else
{
if (MRcvBuffer[0] == 0x0a)
{
status = MI_OK;
}
else
{
status = MI_CODEERR;
}
}
}
}
M500PcdSetTmo(1); // short timeout
}
return status;
}
///////////////////////////////////////////////////////////////////////
// V A L U E M A N I P U L A T I O N
///////////////////////////////////////////////////////////////////////
char M500PiccValue(unsigned char dd_mode,
unsigned char addr,
unsigned char *value,
unsigned char trans_addr)
{
char status = MI_OK;
M500PcdSetTmo(1); // short timeout
// ************* Cmd Sequence **********************************
ResetInfo(MInfo);
MSndBuffer[0] = dd_mode; // Inc,Dec command code
MSndBuffer[1] = addr;
MInfo.nBytesToSend = 2;
status = M500PcdCmd(PCD_TRANSCEIVE,
MSndBuffer,
MRcvBuffer,
&MInfo);
if (status != MI_NOTAGERR) // no timeout error
{
if (MInfo.nBitsReceived != 4) // 4 bits are necessary
{
status = MI_BITCOUNTERR;
}
else // 4 bit received
{
MRcvBuffer[0] &= 0x0f; // mask out upper nibble
switch(MRcvBuffer[0])
{
case 0x00:
status = MI_NOTAUTHERR;
break;
case 0x0a:
status = MI_OK;
break;
case 0x01:
status = MI_VALERR;
break;
default:
status = MI_CODEERR;
break;
}
}
}
if ( status == MI_OK)
{
M500PcdSetTmo(3); // long timeout
ResetInfo(MInfo);
memcpy(MSndBuffer,value,4);
MInfo.nBytesToSend = 4;
status = M500PcdCmd(PCD_TRANSCEIVE,
MSndBuffer,
MRcvBuffer,
&MInfo);
if (status == MI_OK) // no timeout occured
{
if (MInfo.nBitsReceived != 4) // 4 bits are necessary
{
status = MI_BITCOUNTERR;
}
else // 4 bit received
{
MRcvBuffer[0] &= 0x0f; // mask out upper nibble
switch(MRcvBuffer[0])
{
case 0x00:
status = MI_NOTAUTHERR;
break;
case 0x01:
status = MI_VALERR;
break;
default:
status = MI_CODEERR;
break;
}
}
}
else
if (status == MI_NOTAGERR )
status = MI_OK; // no response after 4 byte value -
// transfer command has to follow
}
if ( status == MI_OK)
{
ResetInfo(MInfo);
MSndBuffer[0] = PICC_TRANSFER; // transfer command code
MSndBuffer[1] = trans_addr;
MInfo.nBytesToSend = 2;
status = M500PcdCmd(PCD_TRANSCEIVE,
MSndBuffer,
MRcvBuffer,
&MInfo);
if (status != MI_NOTAGERR) // timeout occured
{
if (MInfo.nBitsReceived != 4) // 4 bits are necessary
{
status = MI_BITCOUNTERR;
}
else // 4 bit received
{
MRcvBuffer[0] &= 0x0f; // mask out upper nibble
switch(MRcvBuffer[0])
{
case 0x00:
status = MI_NOTAUTHERR;
break;
case 0x0a:
status = MI_OK;
break;
case 0x01:
status = MI_VALERR;
break;
default:
status = MI_CODEERR;
break;
}
}
}
}
return status;
}
///////////////////////////////////////////////////////////////////////
// V A L U E M A N I P U L A T I O N W I T H B A C K U P
///////////////////////////////////////////////////////////////////////
char M500PiccValueDebit(unsigned char dd_mode,
unsigned char addr,
unsigned char *value)
{
char status = MI_OK;
M500PcdSetTmo(1); // short timeout
ResetInfo(MInfo);
MSndBuffer[0] = dd_mode; // Inc,Dec
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -