#include "reg52.h"
#include "string.h"
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
//define spi
#define SPI_REN() IE0=0;EX0=1
#define SPI_RDIS() EX0=0
//spi Error
#define SPI_OK 0
#define SPI_ERR 255
extern uchar spi_send(uchar idata *dp,uchar lenght);
extern uchar spi_rcv(uchar idata *dp);
extern uchar spi_init(void);
uchar idata spi_buffer[26];
//define spi_buffer
#define SEQNR 0
#define COMMAND 1
#define STATUS 1
#define LENGTH 2
#define MODE 3
#define BCNT 3
#define ADR 3
#define SERNR 3
#define SIZE 3
#define TIME 3
#define TAGTYPE 3
#define INFO 3
#define DATA 3
#define SECNR 4
#define DATABYTES 4
#define VALUE 4
#define NKEY 5
#define AUTHMODE 7
#define ADRCHKWR 8
#define DATACHKWR 9
bit bdata newdata=0;
//mifare error
#define MI_OK 0
#define MI_CRCERR 2
//Timer 0
#define OSC_FREQ 11059200L //MCU晶振频率
#define EVLT_50MS 65536L-OSC_FREQ/240L //50ms的初值
#define H_50MS EVLT_50MS>>8
#define L_50MS EVLT_50MS
#define T0_evaluate() TH0=H_50MS;TL0=L_50MS; //TH1=0x2c;TL1=0xb0;
void spi_int(void) interrupt 0 using 1
{
spi_rcv(spi_buffer);
newdata=1;
}
uchar zlg500cmd(uchar length)
{ uchar spi_wdg=10;
uchar i,BCC_sum;
spi_buffer[length]=spi_buffer[0];
for(i=1;i<length;i++)
{
spi_buffer[length]^=spi_buffer[i];
}
spi_buffer[length]=~spi_buffer[length];
T0_evaluate();
if(spi_send(spi_buffer,length+1)!=SPI_OK)
return SPI_ERR;
ET0=0;
TF0=0;
TR0=1;
SPI_REN();
while(newdata==0&&spi_wdg!=0)
{
if(TF0==1)
{
T0_evaluate();TF0=0;
spi_wdg--;
}
}
TR0=0;
SPI_RDIS();
if(newdata==0) return SPI_ERR;
newdata=0;
BCC_sum=spi_buffer[0];
for(i=1;i<(spi_buffer[LENGTH]+3);i++)
{
BCC_sum^=spi_buffer[i];
}
if(BCC_sum==~spi_buffer[spi_buffer[LENGTH]+3])
return SPI_OK;
else return MI_CRCERR;
}
uchar mifs_request(uchar _Mode,uchar idata *_TagType)
{
spi_buffer[SEQNR]=0;
spi_buffer[COMMAND]=0x41;
spi_buffer[LENGTH]=0x01;
spi_buffer[MODE]=_Mode;
if(zlg500cmd(4)!=SPI_OK)
return SPI_ERR;
if(spi_buffer[STATUS]==MI_OK)
{
_TagType[0]=spi_buffer[TAGTYPE];
_TagType[1]=spi_buffer[TAGTYPE+1];
}
return spi_buffer[STATUS];
}
uchar mifs_anticoll(uchar _Bcnt,uchar idata *_SNR)
{
spi_buffer[SEQNR]=0;
spi_buffer[COMMAND]=0x42;
spi_buffer[LENGTH]=0x01;
spi_buffer[MODE]=_Bcnt;
if(zlg500cmd(4)!=SPI_OK)
return SPI_ERR;
if(spi_buffer[STATUS]==MI_OK)
{
memcpy(_SNR,&spi_buffer[SERNR],4);
}
return spi_buffer[STATUS];
}
uchar mifs_select(uchar idata *_SNR,uchar idata *_Size)
{
spi_buffer[SEQNR]=0;
spi_buffer[COMMAND]=0x43;
spi_buffer[LENGTH]=0x04;
memcpy(&spi_buffer[SERNR],_SNR,4);
if(zlg500cmd(7)!=SPI_OK)
return SPI_ERR;
if(spi_buffer[STATUS]==MI_OK)
{
*_Size=spi_buffer[SIZE];
}
return spi_buffer[STATUS];
}
uchar mifs_authentication(uchar _Mode,uchar _SecNr)
{
spi_buffer[SEQNR]=0;
spi_buffer[COMMAND]=0x44;
spi_buffer[LENGTH]=0x02;
spi_buffer[MODE]=_Mode;
spi_buffer[SECNR]=_SecNr;
if(zlg500cmd(5)!=SPI_OK)
return SPI_ERR;
return spi_buffer[STATUS];
}
uchar mifs_authentication2(uchar _Mode,uchar _SecNr,uchar _KeyNr)
{
spi_buffer[SEQNR]=0;
spi_buffer[COMMAND]=0x72;
spi_buffer[LENGTH]=0x03;
spi_buffer[MODE]=_Mode;
spi_buffer[SECNR]=_SecNr;
spi_buffer[DATA+2]=_KeyNr;
if(zlg500cmd(6)!=SPI_OK)
return SPI_ERR;
return spi_buffer[STATUS];
}
uchar mifs_authKey(uchar _Mode,uchar _SecNr,uchar *_Key)
{
spi_buffer[SEQNR]=0;
spi_buffer[COMMAND]=0x73;
spi_buffer[LENGTH]=0x08;
spi_buffer[DATA]=_Mode;
spi_buffer[DATA+1]=_SecNr;
memcpy(&spi_buffer[DATA+2],_Key,6);
if(zlg500cmd(11)!=SPI_OK)
return SPI_ERR;
return spi_buffer[STATUS];
}
uchar mifs_halt(void)
{
spi_buffer[SEQNR]=0;
spi_buffer[COMMAND]=0x45;
spi_buffer[LENGTH]=0x00;
if(zlg500cmd(3)!=SPI_OK)
return SPI_ERR;
return spi_buffer[STATUS];
}
uchar mifs_read(uchar _Adr,uchar idata *_Data)
{
spi_buffer[SEQNR]=0;
spi_buffer[COMMAND]=0x46;
spi_buffer[LENGTH]=0x01;
spi_buffer[ADR]=_Adr;
if(zlg500cmd(4)!=SPI_OK)
return SPI_ERR;
if(spi_buffer[STATUS]==MI_OK)
{
memcpy(_Data,&spi_buffer[DATABYTES-1],16);
}
return spi_buffer[STATUS];
}
uchar mifs_write(uchar _Adr,uchar idata *_Data)
{
spi_buffer[SEQNR]=0;
spi_buffer[COMMAND]=0x47;
spi_buffer[LENGTH]=17;
spi_buffer[ADR]=_Adr;
memcpy(&spi_buffer[DATABYTES],_Data,16);
if(zlg500cmd(20)!=SPI_OK)
return SPI_ERR;
return spi_buffer[STATUS];
}
uchar mifs_increment(uchar _Adr,long idata *_Value)
{ uchar *temp=(uchar *)_Value;
spi_buffer[SEQNR]=0;
spi_buffer[COMMAND]=0x48;
spi_buffer[LENGTH]=5;
spi_buffer[ADR]=_Adr;
spi_buffer[VALUE]=*(temp+3);
spi_buffer[VALUE+1]=*(temp+2);
spi_buffer[VALUE+2]=*(temp+1);
spi_buffer[VALUE+3]=*temp;
if(zlg500cmd(8)!=SPI_OK)
return SPI_ERR;
return spi_buffer[STATUS];
}
uchar mifs_decrement(uchar _Adr,long idata *_Value)
{
uchar *temp=(uchar *)_Value;
spi_buffer[SEQNR]=0;
spi_buffer[COMMAND]=0x49;
spi_buffer[LENGTH]=5;
spi_buffer[ADR]=_Adr;
spi_buffer[VALUE]=*(temp+3);
spi_buffer[VALUE+1]=*(temp+2);
spi_buffer[VALUE+2]=*(temp+1);
spi_buffer[VALUE+3]=*temp;
if(zlg500cmd(8)!=SPI_OK)
return SPI_ERR;
return spi_buffer[STATUS];
}
uchar mifs_restore(uchar _Adr)
{
spi_buffer[SEQNR]=0;
spi_buffer[COMMAND]=0x4A;
spi_buffer[LENGTH]=1;
spi_buffer[ADR]=_Adr;
if(zlg500cmd(4)!=SPI_OK)
return SPI_ERR;
return spi_buffer[STATUS];
}
uchar mifs_transfer(uchar _Adr)
{
spi_buffer[SEQNR]=0;
spi_buffer[COMMAND]=0x4B;
spi_buffer[LENGTH]=1;
spi_buffer[ADR]=_Adr;
if(zlg500cmd(4)!=SPI_OK)
return SPI_ERR;
return spi_buffer[STATUS];
}
uchar mifs_load_key(uchar _Mode,uchar _SecNr,uchar *_Nkey)
{
spi_buffer[SEQNR]=0;
spi_buffer[COMMAND]=0x4C;
spi_buffer[LENGTH]=0x08;
spi_buffer[MODE]=_Mode;
spi_buffer[SECNR]=_SecNr;
memcpy(&spi_buffer[NKEY],_Nkey,6);
if(zlg500cmd(11)!=SPI_OK)
return SPI_ERR;
return spi_buffer[STATUS];
}
uchar mifs_reset(uchar _Msec)
{
spi_buffer[SEQNR]=0;
spi_buffer[COMMAND]=0x4E;
spi_buffer[LENGTH]=1;
spi_buffer[TIME]=_Msec;
if(zlg500cmd(4)!=SPI_OK)
return SPI_ERR;
return spi_buffer[STATUS];
}
uchar mifs_get_info(uchar idata *_Info)
{
spi_buffer[SEQNR]=0;
spi_buffer[COMMAND]=0x4F;
spi_buffer[LENGTH]=0;
if(zlg500cmd(3)!=SPI_OK)
return SPI_ERR;
if(spi_buffer[STATUS]==MI_OK)
{
memcpy(_Info,&spi_buffer[INFO],spi_buffer[LENGTH]);
}
return spi_buffer[STATUS];
}
uchar mifs_close(void)
{
spi_buffer[SEQNR]=0;
spi_buffer[COMMAND]=0x3F;
spi_buffer[LENGTH]=0;
if(zlg500cmd(3)!=SPI_OK)
return SPI_ERR;
return spi_buffer[STATUS];
}
uchar mifs_config(void)
{
spi_buffer[SEQNR]=0;
spi_buffer[COMMAND]=0x52;
spi_buffer[LENGTH]=0;
if(zlg500cmd(3)!=SPI_OK)
return SPI_ERR;
return spi_buffer[STATUS];
}
uchar mifs_check_write(uchar idata *_SNR,uchar _Authmode,uchar _Adr,uchar idata *_Data)
{
spi_buffer[0]=0;
spi_buffer[COMMAND]=0x53;
spi_buffer[LENGTH]=22;
memcpy(&spi_buffer[SERNR],_SNR,4);
spi_buffer[AUTHMODE]=_Authmode;
spi_buffer[ADRCHKWR]=_Adr;
memcpy(&spi_buffer[DATACHKWR],_Data,16);
if(zlg500cmd(25)!=SPI_OK)
return SPI_ERR;
return spi_buffer[STATUS];
}
uchar mifs_set_control_bit()
{
spi_buffer[0]=0;
spi_buffer[COMMAND]=0x50;
spi_buffer[LENGTH]=0;
if(zlg500cmd(3)!=SPI_OK)
return SPI_ERR;
return spi_buffer[STATUS];
}
u