📄 fm1715.c
字号:
Read_FIFO(buffer);
Save_UID(row, col, temp); //将收到的UID 放入UID 数组中
// Show_UID(); //显示UID
temp=ErrorFlag; //
temp=temp&0x01;
if(temp==0x00)
{
temp=Check_UID();
if(temp==FALSE)
{
return(FM1715_SERNRERR);
}
return(FM1715_OK);
}
else
{
temp=CollPos; //read collision register
row=temp/8;
col=temp%8;
buffer[0]=RF_CMD_ANTICOL; //set the count that the data to be sent
pre_row=pre_row+row;
for(i=0;i<pre_row+1;i++)
{
buffer[i+2]=UID[i];
}
if(col!=0x00)
{
row=pre_row+1;
}
else
{
row=pre_row;
}
temp=Command_Send(row+2,buffer,Transceive);
}
}
}
//********************************************************************
//name: Select_Card
//function: Select the card in FM1715 control range
//input: N/A
//output: FM1715_NOTAGERR:no card
// FM1715_PARITYERR:parity error
// FM1715_CRCERR: CRC verify error
// FM1715_BTECOUNTERR: Receive byte error
// FM1715_OK: ack succeed
// FM1715_SELERR: select error
//*********************************************************************
uchar Select_Card(void)
{
uchar temp,i;
CRCPresetLSB=0x63;
CWConductance=0x3f;
buffer[0]=RF_CMD_SELECT;
buffer[1]=0x70;
for(i=0;i<5;i++)
{
buffer[i+2]=UID[i];
}
ChannelRedundancy=0x0f;
temp=Command_Send(7,buffer,Transceive);
if(temp==FALSE)
{
return(FM1715_NOTAGERR);
}
else
{
temp=ErrorFlag;
if((temp&0x02)==0x02)
return(FM1715_PARITYERR);
if((temp&0x04)==0x04)
return(FM1715_FRAMINGERR);
if((temp&0x08)==0x08)
return(FM1715_CRCERR);
temp=FIFO_Length;
if(temp!=1)
return(FM1715_BYTECOUNTERR);
Read_FIFO(buffer); //Read FIFO information
temp=*buffer;
if(temp!=0) //check ack signal
return(FM1715_OK);
else
return(FM1715_SELERR);
}
}
//*************************************************************************
//name:Authentication
//function: password autherntication
//input: UID: card serial address
// SecNR: sector number
// mode: mode
//output: FM1715_NOTAGERR: no card
// FM1715_PARITYERR:parity error
// FM1715_CRCERR: CRC error
// FM1715_OK: ack succeed
// FM1715_AUTHERR: authentication error
//**************************************************************************
uchar Authentication(uchar idata *UID,uchar SecNR, uchar mode)
{
uchar idata i;
uchar idata temp,temp1;
CRCPresetLSB=0x63;
CWConductance=0x3f;
ModConductance=0x3f;
temp1=Control;
temp1=temp1&0xf7;
Control=temp1;
if(mode==1) //authentication
buffer[0]=RF_CMD_AUTH_LB;
else
buffer[0]=RF_CMD_AUTH_LA;
buffer[1]=SecNR*4 +3;
for(i=0;i<4;i++)
{
buffer[2+i]=UID[i];
}
ChannelRedundancy=0x0f; //start crc,parity verify
temp=Command_Send(6,buffer,Authent1);
if(temp==FALSE)
{
return FM1715_NOTAGERR;
}
temp=ErrorFlag;
if((temp&0x02)==0x02)
return FM1715_PARITYERR;
if((temp&0x04)==0x04)
return FM1715_FRAMINGERR;
if((temp&0x08)==0x08)
return FM1715_CRCERR;
temp=Command_Send(0,buffer,Authent2); //Authent2
if(temp==FALSE)
{
return FM1715_NOTAGERR;
}
temp=ErrorFlag;
if((temp&0x02)==0x02)
return FM1715_PARITYERR;
if((temp&0x04)==0x04)
return FM1715_FRAMINGERR;
if((temp&0x08)==0x08)
return FM1715_CRCERR;
temp1=Control;
temp1=temp1&0x08;
if(temp1==0x08)
{
return FM1715_OK;
}
return FM1715_AUTHERR;
}
//*********************************************************************
//name:MIF_Read
//fucntion: read Mifare card block value
//input:buff: buffer headaddress
// Block_Adr: Block address
//output: FM1715_NOTAGERR: no card
// FM1715_PARITYERR: parity error
// FM1715_CRCERR: CRC verify error
// FM1715_BYTECOUNTERR: receive error
// FM1715_OK: Ack succeed
//**********************************************************************
uchar MIF_READ(uchar idata *buff,uchar Block_Adr)
{
uchar idata temp;
CRCPresetLSB=0x63;
CWConductance=0x3f;
ModConductance=0x3f; //FM1702无此register
ChannelRedundancy=0x0f;
//Int_Req=0x7f;
buff[0]=RF_CMD_READ;
buff[1]=Block_Adr;
temp=Command_Send(2,buff,Transceive);
if(temp==0)
{
return FM1715_NOTAGERR;
}
temp=ErrorFlag;
if((temp&0x02)==0x02) return FM1715_PARITYERR;
if((temp&0x04)==0x04) return FM1715_FRAMINGERR;
if((temp&0x08)==0x08) return FM1715_CRCERR;
temp=FIFO_Length;
if(temp==0x10) //8k card read data length is 16
{
Read_FIFO(buff);
return FM1715_OK;
}
else if(temp==0x04) //Token card read data length is 16
{
Read_FIFO(buff);
return FM1715_OK;
}
else
{
return FM1715_BYTECOUNTERR;
}
}
/****************************************************************/
/*名称: MIF_Write */
/*功能: 该函数实现写 MIFARE 卡块的数值 */
/*输入: buff: 缓冲区首地址
*/
/* Block_Adr: 块地址
*/
/*输出: FM1715_NOTAGERR: 无卡 */
/* FM1715_BYTECOUNTERR: 接收字节错误 */
/* FM1715_NOTAUTHERR: 未经权威认证 */
/* FM1715_EMPTY: 数据溢出错误 */
/* FM1715_CRCERR: CRC 校验错 */
/* FM1715_PARITYERR: 奇偶校验错 */
/* FM1715_WRITEERR: 写卡块数据出错 */
/* FM1715_OK: 应答正确
*/
/****************************************************************/
uchar MIF_Write(uchar idata *buff,uchar Block_Adr)
{
uchar idata temp;
uchar idata *F_buff; CRCPresetLSB = 0x63; CWConductance = 0x3f; F_buff =
buff + 0x10; ChannelRedundancy = 0x07;
*F_buff = RF_CMD_WRITE;
*(F_buff + 1) = Block_Adr;
temp = Command_Send(2, F_buff, Transceive);
if (temp == FALSE)
{
return(FM1715_NOTAGERR);
}
temp = FIFO_Length;
if (temp == 0)
{
return(FM1715_BYTECOUNTERR);
}
Read_FIFO(F_buff);
temp = *F_buff;
switch (temp)
{
case 0x00 :
return(FM1715_NOTAUTHERR); //暂时屏蔽掉写错误
case 0x04:
return(FM1715_EMPTY);
case 0x0a:
break;
case 0x01:
return(FM1715_CRCERR);
case 0x05:
return(FM1715_PARITYERR);
default:
return(FM1715_WRITEERR);
}
temp = Command_Send(16, buff, Transceive);
if (temp == TRUE)
{
return(FM1715_OK);
}
else
{
temp = ErrorFlag;
if ((temp & 0x02)==0x02)
return(FM1715_PARITYERR);
else if((temp & 0x04)==0x04)
return(FM1715_FRAMINGERR);
else if ((temp & 0x08)==0x08)
return(FM1715_CRCERR);
else
return(FM1715_WRITEERR);
}
}
/****************************************************************/
/*名称: MIF_Increment */
/*功能: 该函数实现 MIFARE 卡自动增值操作 */
/*输入: buff: 四个字节数值起始地址 */
/* Block_Adr: 块地址 */
/*输出: FM1715_NOTAGERR: 无卡 */
/* FM1715_BYTECOUNTERR: 接收字节错误 */
/* FM1715_NOTAUTHERR: 未经权威认证 */
/* FM1715_EMPTY: 数据溢出错误 */
/* FM1715_CRCERR: CRC 校验错 */
/* FM1715_PARITYERR: 奇偶校验错 */
/* FM1715_INCRERR: 卡片增款操作失败 */
/* FM1715_OK: 应答正确
*/
/****************************************************************/
uchar MIF_Increment(uchar idata *buff,uchar Block_Adr)
{
uchar temp;
uchar idata *F_buff;
CRCPresetLSB = 0x63;
CWConductance = 0x3f;
F_buff = buff + 4;
*F_buff = RF_CMD_INC;
*(F_buff + 1) = Block_Adr;
ChannelRedundancy = 0x07;
temp = Command_Send(2, F_buff, Transceive);
if (temp == FALSE)
{
return FM1715_NOTAGERR;
}
temp = FIFO_Length;
if (temp == 0)
{
return FM1715_BYTECOUNTERR;
}
Read_FIFO(F_buff);
temp = *F_buff;
switch(temp)
{
case 0x00:
//break;
return(FM1715_NOTAUTHERR); //暂时屏蔽掉写错误
case 0x04:
return(FM1715_EMPTY);
case 0x0a:
break;
case 0x01:
return(FM1715_CRCERR);
case 0x05:
return(FM1715_PARITYERR);
default:
return(FM1715_INCRERR);
}
temp = Command_Send(4, buff, Transmit);
if (temp == FALSE)
{
return FM1715_INCRERR;
}
return FM1715_OK;
}
/****************************************************************/
/*名称: MIF_Decrement */
/*功能: 该函数实现 MIFARE 卡自动减值操作 */
/*输入: buff: 四个字节数值起始地址 */
/* Block_Adr: 块地址 */
/*输出: FM1715_NOTAGERR: 无卡 */
/* FM1715_BYTECOUNTERR: 接收字节错误 */
/* FM1715_NOTAUTHERR: 未经权威认证 */
/* FM1715_EMPTY: 数据溢出错误 */
/* FM1715_CRCERR: CRC 校验错 */
/* FM1715_PARITYERR: 奇偶校验错 */
/* FM1715_DECRERR: 卡片扣款操作失败 */
/* FM1715_OK: 应答正确
*/
/****************************************************************/
uchar MIF_Decrement(uchar idata *buff,uchar Block_Adr)
{
uchar temp;
uchar idata *F_buff;
CRCPresetLSB = 0x63;
CWConductance = 0x3f;
F_buff = buff + 4;
*F_buff = RF_CMD_DEC;
*(F_buff + 1) = Block_Adr;
ChannelRedundancy = 0x07;
temp = Command_Send(2, F_buff, Transceive);
if (temp == FALSE)
{
return FM1715_NOTAGERR;
}
temp = FIFO_Length;
if (temp == 0)
{
return FM1715_BYTECOUNTERR;
}
Read_FIFO(F_buff);
temp = *F_buff;
switch (temp)
{
case 0x00:
//break;
return(FM1715_NOTAUTHERR); //暂时屏蔽掉写错误
case 0x04:
return(FM1715_EMPTY);
case 0x0a:
break;
case 0x01:
return(FM1715_CRCERR);
case 0x05:
return(FM1715_PARITYERR);
default:
return(FM1715_DECRERR);
}
temp = Command_Send(4, buff, Transmit);
if (temp == FALSE)
{
return(FM1715_DECRERR);
}
return FM1715_OK;
}
/****************************************************************/
/*名称: MIF_Restore */
/*功能: 该函数实现 MIFARE 卡自动恢复,备份操作 */
/*输入: Block_Adr: 块地址 */
/*输出: FM1715_NOTAGERR: 无卡 */
/* FM1715_BYTECOUNTERR: 接收字节错误 */
/* FM1715_NOTAUTHERR: 未经权威认证 */
/* FM1715_EMPTY: 数据溢出错误 */
/* FM1715_CRCERR: CRC 校验错 */
/* FM1715_PARITYERR: 奇偶校验错 */
/* FM1715_RESTERR: 卡片恢复,备份操作失败 */
/* FM1715_OK: 应答正确
*/
/****************************************************************/
uchar MIF_Restore(uchar Block_Adr)
{
uchar temp,i;
CRCPresetLSB = 0x63;
CWConductance = 0x3f;
ChannelRedundancy = 0x07;
*buffer = RF_CMD_RESTORE;
*(buffer + 1) = Block_Adr;
temp = Command_Send(2, buffer, Transceive);
if (temp == FALSE)
{
return FM1715_NOTAGERR;
}
temp = FIFO_Length;
if (temp == 0)
{
return FM1715_BYTECOUNTERR;
}
Read_FIFO(buffer);
temp = *buffer;
switch (temp)
{
case 0x00 :
//break;
return(FM1715_NOTAUTHERR); //暂时屏蔽掉写错误
case 0x04:
return(FM1715_EMPTY);
case 0x0a:
break;
case 0x01:
return(FM1715_CRCERR);
case 0x05:
return(FM1715_PARITYERR);
default:
return(FM1715_RESTERR);
}
for (i = 0; i < 4; i++)
buffer[i] = 0x00;
temp = Command_Send(4, buffer, Transmit);
if (temp == FALSE)
{
return FM1715_RESTERR;
}
return FM1715_OK;
}
/****************************************************************/
/*名称: MIF_Transfer */
/*功能: 该函数实现 MIFARE 卡电子钱包保存操作 */
/*输入: Block_Adr: 块地址 */
/*输出: FM1715_NOTAGERR: 无卡 */
/* FM1715_BYTECOUNTERR: 接收字节错误 */
/* FM1715_NOTAUTHERR: 未经权威认证 */
/* FM1715_EMPTY: 数据溢出错误 */
/* FM1715_CRCERR: CRC 校验错 */
/* FM1715_PARITYERR: 奇偶校验错 */
/* FM1715_TRANSERR: 卡片恢复,备份操作失败 */
/* FM1715_OK: 应答正确
*/
/****************************************************************/
uchar MIF_Transfer(uchar Block_Adr)
{
uchar temp;
CRCPresetLSB = 0x63;
CWConductance = 0x3f;
ChannelRedundancy = 0x07;
buffer[0] = RF_CMD_TRANSFER;
buffer[1] = Block_Adr;
temp = Command_Send(2, buffer, Transceive);
if (temp == FALSE)
{
return FM1715_NOTAGERR;
}
temp = FIFO_Length;
if (temp == 0)
{
return FM1715_BYTECOUNTERR;
}
Read_FIFO(buffer);
temp = *buffer;
switch (temp)
{
case 0x00 :
//break;
return(FM1715_NOTAUTHERR); //暂时屏蔽掉写错误
case 0x04:
return(FM1715_EMPTY);
case 0x0a:
return(FM1715_OK);
case 0x01:
return(FM1715_CRCERR);
case 0x05:
return(FM1715_PARITYERR);
default:
return(FM1715_TRANSERR);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -