📄 m500auc.c
字号:
MInfo.nBytesToSend = 2;
WriteIO(RegChannelRedundancy,0x07);
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 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 0x0a:
status = MI_OK;
break;
case 0x05:
case 0x01:
status = MI_VALERR;
break;
default:
status = MI_CODEERR;
break;
}
}
}
}
return status;
}
//////////////////////////////////////////////////////////////////////
// P I C C E X C H A N G E B L O C K
///////////////////////////////////////////////////////////////////////
char M500PiccExchangeBlock(unsigned char *send_data,
unsigned char send_bytelen,
unsigned char *rec_data,
unsigned char *rec_bytelen,
unsigned char append_crc,
unsigned char timeout )
{
char status = MI_OK;
if (append_crc)
{
// RxCRC and TxCRC enable, parity enable
WriteIO(RegChannelRedundancy,0x0F);
send_bytelen -= 2;
}
else
{
// RxCRC and TxCRC disable, parity enable
WriteIO(RegChannelRedundancy,0x03);
}
M500PcdSetTmo(timeout);
status = ExchangeByteStream(PCD_TRANSCEIVE,
send_data,
send_bytelen,
rec_data,
rec_bytelen);
if ( status == MI_OK )
{
if (append_crc)
{
*rec_bytelen += 2; // for two CRC bytes
rec_data[*rec_bytelen - 2] = 0x00;
rec_data[*rec_bytelen - 1] = 0x00;
}
}
else
{
*rec_bytelen = 0;
}
return status;
}
//////////////////////////////////////////////////////////////////////
// E X C H A N G E B Y T E S T R E A M
///////////////////////////////////////////////////////////////////////
char ExchangeByteStream(unsigned char Cmd,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -