FM1702.c
资源名称:FM1702.rar [点击查看]
上传用户:guangya188
上传日期:2019-10-26
资源大小:4644k
文件大小:27k
源码类别:
RFID编程
开发平台:
C/C++
- #include <reg52.h>
- #define __SRC
- #include "main.h"
- #undef __SRC
- #include <intrins.h>
- //#include <string.h>
- //#include <stdio.h>
- //#include <absacc.h>
- #include "FM1702.h"
- #define uchar unsigned char
- #define uint unsigned int
- sbit RF_MISO = P1^0;
- sbit RF_MOSI = P1^1;
- sbit RF_SCK = P1^2;
- sbit RF_NSS = P1^3;
- sbit RF_RST = P1^4;
- sbit CARD_LED = P2^0;
- sbit SPEAKER = P2^0;
- char spi_byte_transceive(char sendbyte)
- {
- char i,temp;
- for(i=0;i<8;i++)
- {
- RF_SCK=0;
- if(sendbyte & 0x80) {
- RF_MOSI=1;
- }
- else
- {
- RF_MOSI=0;
- }
- sendbyte <<= 1;
- RF_SCK=1;
- temp <<= 1;
- if(RF_MISO)
- temp |= 0x01;
- }
- RF_SCK=0;
- _nop_();
- _nop_();
- RF_MOSI=0;
- return (temp);
- }
- void SPIWrite(char reg_ad,char reg_data)
- {
- RF_SCK=0;
- reg_ad <<= 1;
- RF_NSS=0;
- reg_ad &= 0x7F;
- spi_byte_transceive(reg_ad);
- spi_byte_transceive(reg_data);
- RF_NSS=1;
- return;
- }
- unsigned char SPIRead(char reg_ad)
- { char temp;
- RF_SCK=0;
- _nop_();
- _nop_();
- RF_NSS=0;
- reg_ad <<= 1;
- reg_ad |= 0x80;
- spi_byte_transceive(reg_ad);
- temp=spi_byte_transceive(0x00);
- RF_NSS=1;
- return (temp);
- }
- void main(void)
- {
- uchar baud;
- InitSystem();
- while (1)
- {
- //检查命令标志
- if (CmdValid) {
- CmdValid = FALSE;
- if(RevBuffer[0]==11)
- {
- RevBuffer[2]=RevBuffer[1];
- RevBuffer[0]=1;
- RevBuffer[1]=0;
- CALL_isr_UART();
- SPEAKER=0;
- delay_10ms(RevBuffer[2]);
- SPEAKER=1;
- }
- else if(RevBuffer[0]==13) {
- switch(RevBuffer[1])
- {
- case 0:
- baud=BAUD_9600;
- break;
- case 1:
- baud=BAUD_14400;
- break;
- case 2:
- baud=BAUD_19200;
- break;
- case 3:
- baud=BAUD_28800;
- break;
- case 4:
- baud=BAUD_38400;
- break;
- case 5:
- baud=BAUD_57600;
- break;
- case 6:
- baud=BAUD_115200;
- break;
- default:
- baud=BAUD_19200;
- break;
- }
- RevBuffer[0]=1;
- RevBuffer[1]=0;
- CALL_isr_UART();
- delay_10ms(5);
- TR1 = 0;
- TH1 = baud;
- TL1 = TH1;
- delay_10ms(2);
- TR1 = TRUE;
- }//if LEVEL 2
- else
- {
- cmd_process();
- CALL_isr_UART();
- }
- }
- }//while循环体
- }
- void InitSystem(void)
- {
- RF_NSS=1;
- RF_RST=0;
- ET2 = 0;
- T2CON = 0x04;
- PCON = 0x80;
- SCON = 0x70;
- //TMOD = 0x22;
- TMOD = 0x21;
- TH1 = BAUD_19200;
- TL1 = TH1;
- TR1 = TRUE;
- TH0 = 0x60;
- TL0 = 0x60;
- TR0 = 0;
- ET0=0;
- ET1=0;
- EA=1;
- EX0=1;
- IT0 = 1;
- TR2=0;
- ES = TRUE;
- CmdValid=0;
- SPEAKER=0;
- delay_10ms(10);
- delay_10ms(10);
- SPEAKER=1;
- Init_FM1702(0);
- }
- void isr_UART(void) interrupt 4 using 1
- {
- uchar len, i;
- unsigned int j=0;
- if(RI)
- {
- len=SBUF;
- RI=0;
- for(i=0;i<len;i++)
- {
- while(!RI)
- {
- j++;
- if(j>1000)
- {
- break;
- }
- }
- if(j<1000)
- {
- RevBuffer[i]=SBUF;
- RI=0;
- j=0;
- }
- else
- {
- break;
- }
- }
- if(i==len)
- {
- REN=0;
- CmdValid=1;
- }
- }
- else if(!RI && TI)
- {
- TI=0;
- len=RevBuffer[0];
- for(i=0;i<len+1;i++)
- {
- SBUF=RevBuffer[i];
- while(!TI);
- TI=0;
- }
- REN=1;
- }
- }
- void cmd_process(void)
- {
- uchar cmd;
- uchar status;
- cmd = RevBuffer[0];
- switch(cmd)
- {
- case 1:
- status=MIF_Halt();
- RevBuffer[0]=1;
- RevBuffer[1]=status;
- break;
- case 2:
- status = Request(RF_CMD_REQUEST_ALL);
- if(status != FM1702_OK)
- {
- status = Request(RF_CMD_REQUEST_ALL);
- if(status != FM1702_OK)
- {
- RevBuffer[0] = 1;
- RevBuffer[1] = FM1702_REQERR;
- break;
- }
- }
- if(tagtype[0]==2)
- cardtype=mifarepro;
- else if(tagtype[0]==4)
- cardtype=mifare1;
- else if(tagtype[0]==16)
- cardtype=mifarelight;
- else
- cardtype=unknowncard;
- RevBuffer[0]=3;
- RevBuffer[1]=status;
- RevBuffer[2]=tagtype[0];
- RevBuffer[3]=tagtype[1];
- break;
- case 3:
- status = AntiColl();;
- if(status!=FM1702_OK)
- {
- RevBuffer[0]=1;
- RevBuffer[1]=FM1702_ANTICOLLERR;
- break;
- }
- //memcpy(MLastSelectedSnr,&RevBuffer[2],4);
- RevBuffer[0]=5;
- RevBuffer[1]=status;
- RevBuffer[2]=UID[0];
- RevBuffer[3]=UID[1];
- RevBuffer[4]=UID[2];
- RevBuffer[5]=UID[3];
- break;
- case 4:
- status=Select_Card();
- if(status!=FM1702_OK)
- {
- RevBuffer[0]=1;
- RevBuffer[1]=FM1702_SELERR;
- break;
- }
- RevBuffer[0]=1;
- RevBuffer[1]=status;
- break;
- case 5:
- status = Load_keyE2(RevBuffer[2],RevBuffer[1]);
- status = Authentication(UID, RevBuffer[2], RevBuffer[1]);
- if(status != FM1702_OK)
- {
- RevBuffer[0]=1;
- RevBuffer[1]=status;
- break;
- }
- RevBuffer[0]=1;
- RevBuffer[1]=status;
- break;
- case 6:
- status = Load_keyE2_CPY(RevBuffer[2],RevBuffer[1]); //%40
- if(status != FM1702_OK)
- {
- RevBuffer[0]=1;
- RevBuffer[1]=status;
- break;
- }
- RevBuffer[0]=1;
- RevBuffer[1]=status;
- break;
- case 8:
- status=MIF_READ(&RevBuffer[2],RevBuffer[1]);
- if(status != FM1702_OK)
- {
- RevBuffer[0]=1;
- RevBuffer[1]=status;
- break;
- }
- else
- {
- if(cardtype==mifare1||cardtype==mifarepro)
- RevBuffer[0]=17;
- else if(cardtype==1)
- RevBuffer[0]=9;
- else
- RevBuffer[0]=16;
- }
- RevBuffer[1]=status;
- break;
- case 9:
- status=MIF_Write(&RevBuffer[2],RevBuffer[1]);
- RevBuffer[0]=1;
- RevBuffer[1]=status;
- break;
- case 10:
- if(RevBuffer[1] == RF_CMD_INC)
- {
- status = MIF_Increment(&RevBuffer[3],RevBuffer[2]);
- MIF_Transfer(RevBuffer[2]);
- }
- else if(RevBuffer[1] == RF_CMD_DEC)
- {
- status = MIF_Decrement(&RevBuffer[3],RevBuffer[2]);
- MIF_Transfer(RevBuffer[2]);
- }
- else
- {
- status = 1;
- }
- RevBuffer[0]=1;
- RevBuffer[1]=status;
- break;
- case 12:
- RevBuffer[0]=1;
- RevBuffer[1]=0;
- break;
- default:
- RevBuffer[0] = 1;
- RevBuffer[1] = 1;
- break;
- }
- }
- uchar Clear_FIFO(void)
- {
- uchar temp;
- uint i;
- temp = SPIRead(Control);
- temp = (temp | 0x01);
- SPIWrite(Control,temp);
- for(i = 0; i < RF_TimeOut; i++)
- {
- temp = SPIRead(FIFO_Length);
- if(temp == 0)
- {
- return TRUE;
- }
- }
- return FALSE;
- }
- void Write_FIFO(uchar count, uchar idata *buff)
- {
- uchar i;
- for(i = 0; i < count; i++)
- {
- SPIWrite(FIFO,*(buff + i));
- }
- }
- uchar Read_FIFO(uchar idata *buff)
- {
- uchar temp;
- uchar i;
- temp = SPIRead(FIFO_Length);
- if(temp == 0)
- {
- return 0;
- }
- if(temp >= 24)
- {
- temp = 24;
- }
- for(i = 0; i < temp; i++)
- {
- *(buff + i) = SPIRead(FIFO);
- }
- return temp;
- }
- uchar Judge_Req(uchar idata *buff)
- {
- uchar temp1, temp2;
- temp1 = *buff;
- temp2 = *(buff + 1);
- if((temp1 == 0x02) || (temp1 == 0x04) || (temp1 == 0x05) || (temp1 == 0x53) || (temp1 == 0x03))
- {
- if (temp2 == 0x00)
- {
- return TRUE;
- }
- }
- return FALSE;
- }
- uchar Check_UID(void)
- {
- uchar temp;
- uchar i;
- temp = 0x00;
- for(i = 0; i < 5; i++)
- {
- temp = temp ^ UID[i];
- }
- if(temp == 0)
- {
- return TRUE;
- }
- return FALSE;
- }
- void Save_UID(uchar row, uchar col, uchar length)
- {
- uchar i;
- uchar temp;
- uchar temp1;
- if((row == 0x00) && (col == 0x00))
- {
- for(i = 0; i < length; i++)
- {
- UID[i] = RevBuffer[i];
- }
- }
- else
- {
- temp = RevBuffer[0];
- temp1 = UID[row - 1];
- switch(col)
- {
- case 0: temp1 = 0x00; row = row + 1; break;
- case 1: temp = temp & 0xFE; temp1 = temp1 & 0x01; break;
- case 2: temp = temp & 0xFC; temp1 = temp1 & 0x03; break;
- case 3: temp = temp & 0xF8; temp1 = temp1 & 0x07; break;
- case 4: temp = temp & 0xF0; temp1 = temp1 & 0x0F; break;
- case 5: temp = temp & 0xE0; temp1 = temp1 & 0x1F; break;
- case 6: temp = temp & 0xC0; temp1 = temp1 & 0x3F; break;
- case 7: temp = temp & 0x80; temp1 = temp1 & 0x7F; break;
- default: break;
- }
- RevBuffer[0] = temp;
- UID[row - 1] = temp1 | temp;
- for(i = 1; i < length; i++)
- {
- UID[row - 1 + i] = RevBuffer[i];
- }
- }
- }
- void Set_BitFraming(uchar row, uchar col)
- {
- switch(row)
- {
- case 0: RevBuffer[1] = 0x20; break;
- case 1: RevBuffer[1] = 0x30; break;
- case 2: RevBuffer[1] = 0x40; break;
- case 3: RevBuffer[1] = 0x50; break;
- case 4: RevBuffer[1] = 0x60; break;
- default: break;
- }
- switch(col)
- {
- case 0: SPIWrite(Bit_Frame,0x00); break;
- case 1: SPIWrite(Bit_Frame,0x11); RevBuffer[1] = (RevBuffer[1] | 0x01); break;
- case 2: SPIWrite(Bit_Frame,0x22); RevBuffer[1] = (RevBuffer[1] | 0x02); break;
- case 3: SPIWrite(Bit_Frame,0x33); RevBuffer[1] = (RevBuffer[1] | 0x03); break;
- case 4: SPIWrite(Bit_Frame,0x44); RevBuffer[1] = (RevBuffer[1] | 0x04); break;
- case 5: SPIWrite(Bit_Frame,0x55); RevBuffer[1] = (RevBuffer[1] | 0x05); break;
- case 6: SPIWrite(Bit_Frame,0x66); RevBuffer[1] = (RevBuffer[1] | 0x06); break;
- case 7: SPIWrite(Bit_Frame,0x77); RevBuffer[1] = (RevBuffer[1] | 0x07); break;
- default: break;
- }
- }
- void Init_FM1702(uchar mode)
- {
- uchar temp;
- uint i;
- // unsigned int idata timecnt=0;
- RF_SCK = 1;
- RF_MISO = 1;
- RF_MOSI = 1;
- RF_RST = 1;
- for(i = 0; i < 0x1fff; i++)
- {
- _nop_();
- }
- RF_RST = 0;
- for(i = 0; i < 0x1fff; i++)
- {
- _nop_();
- }
- ////////////////////////////////////////////////////////////////////////////
- while(temp = SPIRead(Command) != 0)
- {
- _nop_();
- }
- ////////////////////////////////////////////////////////////////////////////
- // delay_10ms(1);
- SPIWrite(Page_Sel,0x80);
- for(i = 0; i < 0x1fff; i++) /* 延时 */
- {
- if(temp=SPIRead(Command) == 0x00)
- {
- SPIWrite(Page_Sel,0x00);
- }
- }
- // P2 = SPIRead(TimerClock);
- SPIWrite(TimerClock,0x0b);
- SPIWrite(TimerControl,0x02);
- SPIWrite(TimerReload,0x42);
- SPIWrite(InterruptEn,0x7f);
- // temp = SPIRead(InterruptEn);
- SPIWrite(Int_Req,0x7f);
- SPIWrite(TxControl,0x5b);
- SPIWrite(RxControl2,0x01);
- // SPIWrite(RxControl1,0x73);
- SPIWrite(RxWait,0x07);
- if(mode ==2)
- {
- SPIWrite(TypeSH,0x01);
- }
- else
- {
- SPIWrite(TypeSH,0x00);
- }
- }
- uchar Command_Send(uchar count, uchar idata *buff, uchar Comm_Set)
- {
- uint j;
- uchar idata temp, temp1;
- SPIWrite(Command,0x00);
- Clear_FIFO();
- Write_FIFO(count, buff);
- SPIWrite(Command,Comm_Set); /* 命令执行 */
- for(j = 0; j < RF_TimeOut; j++) /* 检查命令执行否 */
- {
- temp = SPIRead(Command);
- temp1 = SPIRead(Int_Req) & 0x80;
- if(temp == 0x00)
- {
- return TRUE;
- }
- }
- return FALSE;
- }
- uchar Read_E2(uchar lsb, uchar msb, uchar count, uchar idata *buff)
- {
- uchar temp;
- *buff = lsb;
- *(buff + 1) = msb;
- *(buff + 2) = count;
- temp = Command_Send(3, buff, ReadE2);
- Read_FIFO(buff);
- if(temp == FALSE) return(TRUE);
- return(FALSE);
- }
- uchar Write_E2(uchar lsb, uchar msb, uchar count, uchar idata *buff)
- {
- uchar idata temp, i;
- for(i = 0; i < count; i++)
- {
- *(buff + count - i + 2) = *(buff - i + count);
- }
- *buff = lsb;
- *(buff + 1) = msb;
- temp = Command_Send(count + 2, buff, WriteE2);
- temp = SPIRead(SecondaryStatus);
- temp = temp & 0x40;
- if(temp == 0x40)
- {
- SPIWrite(Command,0x00);
- return TRUE;
- }
- return FALSE;
- }
- uchar MIF_Halt(void)
- {
- uchar temp;
- uint i;
- SPIWrite(CRCPresetLSB,0x63);
- SPIWrite(CWConductance,0x3f);
- SPIWrite(ChannelRedundancy,0x03);
- *RevBuffer = RF_CMD_HALT;
- *(RevBuffer + 1) = 0x00;
- temp = Command_Send(2, RevBuffer, Transmit);
- if(temp == TRUE)
- {
- for(i = 0; i < 0x50; i++)
- {
- _nop_();
- }
- return FM1702_OK;
- }
- else
- {
- temp = SPIRead(ErrorFlag);
- if((temp & 0x02) == 0x02)
- {
- return(FM1702_PARITYERR);
- }
- if((temp & 0x04) == 0x04)
- {
- return(FM1702_FRAMINGERR);
- }
- return(FM1702_NOTAGERR);
- }
- }
- char M500HostCodeKey( uchar *uncoded, uchar *coded)
- {
- char idata status = FM1702_OK;
- uchar idata cnt = 0;
- uchar idata ln = 0;
- uchar idata hn = 0;
- 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;
- }
- return FM1702_OK;
- }
- uchar Load_keyE2_CPY(uchar Secnr, uchar Mode)
- {
- char idata status;
- uchar temp;
- uchar msb = 0;
- uchar lsb = 0;
- uchar coded_keys[12];
- uchar temp1;
- if(Secnr >= 0x20)
- {
- temp1 = Secnr - 0x20;
- Secnr = 0x20 + temp1 * 4;
- }
- temp = Secnr * 12;
- if(Mode == RF_CMD_AUTH_LA)
- {
- if(temp >= 0x80)
- {
- lsb = temp - 0x80;
- msb = 0x01;
- }
- else
- {
- lsb = temp + 0x80;
- msb = 0x00;
- }
- }
- else
- {
- msb = 0x01;
- lsb = temp + 0x40;
- }
- status = M500HostCodeKey(&RevBuffer[3],coded_keys);////////////////
- status = Write_E2(lsb, msb, 12, coded_keys); ////////////////
- if(status != FM1702_OK)
- {
- return FALSE;
- }
- return TRUE;
- }
- uchar Load_keyE2(uchar Secnr, uchar Mode)
- {
- uchar temp;
- uchar msb = 0;
- uchar lsb = 0;
- uchar temp1;
- if(Secnr >= 0x20)
- {
- temp1 = Secnr - 0x20;
- Secnr = 0x20 + temp1 * 4;
- }
- temp = Secnr * 12;
- if(Mode == RF_CMD_AUTH_LA)
- {
- if(temp >= 0x80)
- {
- lsb = temp - 0x80;
- msb = 0x01;
- }
- else
- {
- lsb = temp + 0x80;
- msb = 0x00;
- }
- }
- else
- {
- msb = 0x01;
- lsb = temp + 0x40;
- }
- RevBuffer[0] = lsb;
- RevBuffer[1] = msb;
- temp = Command_Send(2, RevBuffer, LoadKeyE2);
- temp = SPIRead(ErrorFlag) & 0x40;
- if(temp == 0x40)
- {
- return FALSE;
- }
- return TRUE;
- }
- uchar Request(uchar mode)
- {
- uchar idata temp;
- // SPIWrite(CRCPresetLSB,0x63);
- // SPIWrite(CWConductance,0x3f);
- RevBuffer[0] = mode;
- SPIWrite(Bit_Frame,0x07);
- SPIWrite(ChannelRedundancy,0x03);
- temp = SPIRead(Control);
- temp = temp & (0xf7);
- SPIWrite(Control,temp); //Control reset value is 00
- temp = Command_Send(1, RevBuffer, Transceive); //Transceive=0x1E
- if(temp == FALSE)
- {
- return FM1702_NOTAGERR;
- }
- Read_FIFO(RevBuffer);
- temp = Judge_Req(RevBuffer);
- if(temp == TRUE)
- {
- tagtype[0] = RevBuffer[0];
- tagtype[1] = RevBuffer[1];
- return FM1702_OK;
- }
- return FM1702_REQERR;
- }
- uchar AntiColl(void)
- {
- uchar temp;
- uchar i;
- uchar row, col;
- uchar pre_row;
- row = 0;
- col = 0;
- pre_row = 0;
- SPIWrite(CRCPresetLSB,0x63);
- SPIWrite(CWConductance,0x3f);
- // SPIWrite(ModConductance,0x3f);
- RevBuffer[0] = RF_CMD_ANTICOL;
- RevBuffer[1] = 0x20;
- SPIWrite(ChannelRedundancy,0x03);
- temp = Command_Send(2, RevBuffer, Transceive);
- while(1)
- {
- if(temp == FALSE)
- {
- return(FM1702_NOTAGERR);
- }
- //temp = ErrorFlag;
- temp = SPIRead(FIFO_Length);
- if(temp == 0)
- {
- return FM1702_BYTECOUNTERR;
- }
- Read_FIFO(RevBuffer);
- Save_UID(row, col, temp);
- temp = SPIRead(ErrorFlag);
- temp = temp & 0x01;
- if(temp == 0x00)
- {
- temp = Check_UID();
- if(temp == FALSE)
- {
- return(FM1702_SERNRERR);
- }
- return(FM1702_OK);
- }
- else
- {
- temp = SPIRead(CollPos); row = temp / 8;
- col = temp % 8;
- RevBuffer[0] = RF_CMD_ANTICOL;
- Set_BitFraming(row + pre_row, col);
- pre_row = pre_row + row;
- for(i = 0; i < pre_row + 1; i++)
- {
- RevBuffer[i + 2] = UID[i];
- }
- if(col != 0x00)
- {
- row = pre_row + 1;
- }
- else
- {
- row = pre_row;
- }
- temp = Command_Send(row + 2, RevBuffer, Transceive);
- }
- }
- }
- uchar Select_Card(void)
- {
- uchar temp, i;
- SPIWrite(CRCPresetLSB,0x63);
- SPIWrite(CWConductance,0x3f);
- RevBuffer[0] = RF_CMD_SELECT;
- RevBuffer[1] = 0x70;
- for(i = 0; i < 5; i++)
- {
- RevBuffer[i + 2] = UID[i];
- }
- SPIWrite(ChannelRedundancy,0x0f);
- temp = Command_Send(7, RevBuffer, Transceive);
- if(temp == FALSE)
- {
- return(FM1702_NOTAGERR);
- }
- else
- {
- temp = SPIRead(ErrorFlag);
- if((temp & 0x02) == 0x02) return(FM1702_PARITYERR);
- if((temp & 0x04) == 0x04) return(FM1702_FRAMINGERR);
- if((temp & 0x08) == 0x08) return(FM1702_CRCERR);
- temp = SPIRead(FIFO_Length);
- if(temp != 1) return(FM1702_BYTECOUNTERR);
- Read_FIFO(RevBuffer);
- temp = *RevBuffer;
- if((temp == 0x18) || (temp == 0x08) || (temp == 0x88) || (temp ==0x53)) return(FM1702_OK);
- else
- return(FM1702_SELERR);
- }
- }
- uchar Authentication(uchar idata *UID, uchar SecNR, uchar mode)
- {
- uchar idata i;
- uchar idata temp, temp1;
- uchar temp0;
- if(SecNR >= 0x20)
- {
- temp0 = SecNR -0x20;
- SecNR = 0x20 + temp0 * 4;
- }
- SPIWrite(CRCPresetLSB,0x63);
- SPIWrite(CWConductance,0x3f);
- // SPIWrite(ModConductance,0X3f);
- // temp1 = SPIRead(Control);
- // temp1 = temp1 & 0xf7;
- // SPIWrite(Control,temp1);
- if(mode == RF_CMD_AUTH_LB)
- RevBuffer[0] = RF_CMD_AUTH_LB;
- else
- RevBuffer[0] = RF_CMD_AUTH_LA;
- RevBuffer[1] = SecNR * 4 + 3;
- for(i = 0; i < 4; i++)
- {
- RevBuffer[2 + i] = UID[i];
- }
- SPIWrite(ChannelRedundancy,0x0f);
- temp = Command_Send(6, RevBuffer, Authent1);
- if(temp == FALSE)
- {
- return 0x99;
- }
- temp = SPIRead(ErrorFlag); //ErrorFlag address is 0x0A.
- if((temp & 0x02) == 0x02) return FM1702_PARITYERR;
- if((temp & 0x04) == 0x04) return FM1702_FRAMINGERR;
- if((temp & 0x08) == 0x08) return FM1702_CRCERR;
- temp = Command_Send(0, RevBuffer, Authent2);
- if(temp == FALSE)
- {
- return 0x88;
- }
- temp = SPIRead(ErrorFlag);
- if((temp & 0x02) == 0x02) return FM1702_PARITYERR;
- if((temp & 0x04) == 0x04) return FM1702_FRAMINGERR;
- if((temp & 0x08) == 0x08) return FM1702_CRCERR;
- temp1 = SPIRead(Control);
- temp1 = temp1 & 0x08;
- if(temp1 == 0x08)
- {
- return FM1702_OK;
- }
- return FM1702_AUTHERR;
- }
- uchar MIF_READ(uchar idata *buff, uchar Block_Adr)
- {
- uchar idata temp;
- SPIWrite(CRCPresetLSB,0x63);
- SPIWrite(CWConductance,0x3f);
- SPIWrite(ModConductance,0x3f);
- SPIWrite(ChannelRedundancy,0x0f);
- buff[0] = RF_CMD_READ;
- buff[1] = Block_Adr;
- temp = Command_Send(2, buff, Transceive);
- if(temp == 0)
- {
- return FM1702_NOTAGERR;
- }
- temp = SPIRead(ErrorFlag);
- if((temp & 0x02) == 0x02) return FM1702_PARITYERR;
- if((temp & 0x04) == 0x04) return FM1702_FRAMINGERR;
- if((temp & 0x08) == 0x08) return FM1702_CRCERR;
- temp = SPIRead(FIFO_Length);
- if(temp == 0x10)
- {
- Read_FIFO(buff);
- return FM1702_OK;
- }
- else if(temp == 0x04)
- {
- Read_FIFO(buff);
- return FM1702_OK;
- }
- else
- {
- return FM1702_BYTECOUNTERR;
- }
- }
- uchar MIF_Write(uchar idata *buff, uchar Block_Adr)
- {
- uchar idata temp;
- uchar idata *F_buff;
- SPIWrite(CRCPresetLSB,0x63);
- SPIWrite(CWConductance,0x3f);
- F_buff = buff + 0x10;
- SPIWrite(ChannelRedundancy,0x07); /* Note: this line is for 1702, different from RC500*/
- *F_buff = RF_CMD_WRITE;
- *(F_buff + 1) = Block_Adr;
- temp = Command_Send(2, F_buff, Transceive);
- if(temp == FALSE)
- {
- return(FM1702_NOTAGERR);
- }
- temp = SPIRead(FIFO_Length);
- if(temp == 0)
- {
- return(FM1702_BYTECOUNTERR);
- }
- Read_FIFO(F_buff);
- temp = *F_buff;
- switch(temp)
- {
- case 0x00: return(FM1702_NOTAUTHERR);
- case 0x04: return(FM1702_EMPTY);
- case 0x0a: break;
- case 0x01: return(FM1702_CRCERR);
- case 0x05: return(FM1702_PARITYERR);
- default: return(FM1702_WRITEERR);
- }
- temp = Command_Send(16, buff, Transceive);
- if(temp == TRUE)
- {
- return(FM1702_OK);
- }
- else
- {
- temp = SPIRead(ErrorFlag);
- if((temp & 0x02) == 0x02)
- return(FM1702_PARITYERR);
- else if((temp & 0x04) == 0x04)
- return(FM1702_FRAMINGERR);
- else if((temp & 0x08) == 0x08)
- return(FM1702_CRCERR);
- else
- return(FM1702_WRITEERR);
- }
- }
- uchar MIF_Increment(uchar idata *buff, uchar Block_Adr)
- {
- uchar temp;
- uchar idata *F_buff;
- SPIWrite(CRCPresetLSB,0x63);
- SPIWrite(CWConductance,0x3f);
- F_buff = buff + 4;
- *F_buff = RF_CMD_INC;
- *(F_buff + 1) = Block_Adr;
- SPIWrite(ChannelRedundancy,0x07);
- temp = Command_Send(2, F_buff, Transceive);
- if(temp == FALSE)
- {
- return FM1702_NOTAGERR;
- }
- temp = SPIRead(FIFO_Length);
- if(temp == 0)
- {
- return FM1702_BYTECOUNTERR;
- }
- Read_FIFO(F_buff);
- temp = *F_buff;
- switch(temp)
- {
- case 0x00: /* break; */return(FM1702_NOTAUTHERR);
- case 0x04: return(FM1702_EMPTY);
- case 0x0a: break;
- case 0x01: return(FM1702_CRCERR);
- case 0x05: return(FM1702_PARITYERR);
- default: return(FM1702_INCRERR);
- }
- temp = Command_Send(4, buff, Transmit);
- if(temp == FALSE)
- {
- return FM1702_INCRERR;
- }
- return FM1702_OK;
- }
- uchar MIF_Decrement(uchar idata *buff, uchar Block_Adr)
- {
- uchar temp;
- uchar idata *F_buff;
- SPIWrite(CRCPresetLSB,0x63);
- SPIWrite(CWConductance,0x3f);
- F_buff = buff + 4;
- *F_buff = RF_CMD_DEC;
- *(F_buff + 1) = Block_Adr;
- SPIWrite(ChannelRedundancy,0x07);
- temp = Command_Send(2, F_buff, Transceive);
- if(temp == FALSE)
- {
- return FM1702_NOTAGERR;
- }
- temp = SPIRead(FIFO_Length);
- if(temp == 0)
- {
- return FM1702_BYTECOUNTERR;
- }
- Read_FIFO(F_buff);
- temp = *F_buff;
- switch(temp)
- {
- case 0x00: /* break; */return(FM1702_NOTAUTHERR);
- case 0x04: return(FM1702_EMPTY);
- case 0x0a: break;
- case 0x01: return(FM1702_CRCERR);
- case 0x05: return(FM1702_PARITYERR);
- default: return(FM1702_DECRERR);
- }
- temp = Command_Send(4, buff, Transmit);
- if(temp == FALSE)
- {
- return(FM1702_DECRERR);
- }
- return FM1702_OK;
- }
- uchar MIF_Restore(uchar Block_Adr)
- {
- uchar temp, i;
- SPIWrite(CRCPresetLSB,0x63);
- SPIWrite(CWConductance,0x3f);
- SPIWrite(ChannelRedundancy,0x07);
- *RevBuffer = RF_CMD_RESTORE;
- *(RevBuffer + 1) = Block_Adr;
- temp = Command_Send(2, RevBuffer, Transceive);
- if(temp == FALSE)
- {
- return FM1702_NOTAGERR;
- }
- temp = SPIRead(FIFO_Length);
- if(temp == 0)
- {
- return FM1702_BYTECOUNTERR;
- }
- Read_FIFO(RevBuffer);
- temp = *RevBuffer;
- switch(temp)
- {
- case 0x00: /* break; */return(FM1702_NOTAUTHERR);
- case 0x04: return(FM1702_EMPTY);
- case 0x0a: break;
- case 0x01: return(FM1702_CRCERR);
- case 0x05: return(FM1702_PARITYERR);
- default: return(FM1702_RESTERR);
- }
- for(i = 0; i < 4; i++) RevBuffer[i] = 0x00;
- temp = Command_Send(4, RevBuffer, Transmit);
- if(temp == FALSE)
- {
- return FM1702_RESTERR;
- }
- return FM1702_OK;
- }
- uchar MIF_Transfer(uchar Block_Adr)
- {
- uchar temp;
- SPIWrite(CRCPresetLSB,0x63);
- SPIWrite(CWConductance,0x3f);
- SPIWrite(ChannelRedundancy,0x07);
- RevBuffer[0] = RF_CMD_TRANSFER;
- RevBuffer[1] = Block_Adr;
- temp = Command_Send(2, RevBuffer, Transceive);
- if(temp == FALSE)
- {
- return FM1702_NOTAGERR;
- }
- temp = SPIRead(FIFO_Length);
- if(temp == 0)
- {
- return FM1702_BYTECOUNTERR;
- }
- Read_FIFO(RevBuffer);
- temp = *RevBuffer;
- switch(temp)
- {
- case 0x00: /* break; */return(FM1702_NOTAUTHERR);
- case 0x04: return(FM1702_EMPTY);
- case 0x0a: return(FM1702_OK);
- case 0x01: return(FM1702_CRCERR);
- case 0x05: return(FM1702_PARITYERR);
- default: return(FM1702_TRANSERR);
- }
- }
- #if 0
- uchar HL_Active(uchar Block_Adr, uchar Mode)
- {
- uchar temp;
- Secnr = Block_Adr / 4;
- MIF_Halt(); /* Halt */
- temp = Request(RF_CMD_REQUEST_STD);
- if(temp != FM1702_OK)
- {
- return(FM1702_REQERR);
- }
- temp = AntiColl();
- if(temp != FM1702_OK)
- {
- return(FM1702_ANTICOLLERR);
- }
- temp = Select_Card();
- if(temp != FM1702_OK)
- {
- return(FM1702_SELERR);
- }
- Load_keyE2_CPY(Secnr, Mode); //%40
- temp = Authentication(UID, Secnr, Mode);
- if(temp != FM1702_OK)
- {
- return(FM1702_AUTHERR);
- }
- return FM1702_OK;
- }
- uchar MIF_Initival(uchar idata *buff, uchar Block_Adr)
- {
- uchar idata temp;
- uchar i;
- for(i = 0; i < 4; i++)
- {
- *(buff + 4 + i) = ~(*(buff + i));
- }
- for(i = 0; i < 4; i++)
- {
- *(buff + 8 + i) = *(buff + i);
- }
- *(buff + 12) = Block_Adr;
- *(buff + 13) = ~Block_Adr;
- *(buff + 14) = Block_Adr;
- *(buff + 15) = ~Block_Adr;
- temp = MIF_Write(buff, Block_Adr);
- return temp;
- }
- #endif
- #if 0
- uchar HL_Read(uchar idata *buff, uchar Block_Adr, uchar Mode)
- {
- uchar temp;
- temp = HL_Active(Block_Adr, Mode);
- if(temp != FM1702_OK)
- {
- return temp;
- }
- temp = MIF_READ(buff, Block_Adr);
- if(temp != FM1702_OK)
- {
- return temp;
- }
- return FM1702_OK;
- }
- uchar HL_Write(uchar idata *buff, uchar Block_Adr, uchar Mode)
- {
- uchar temp;
- temp = HL_Active(Block_Adr, Mode);
- if(temp != FM1702_OK)
- {
- return temp;
- }
- temp = MIF_Write(buff, Block_Adr);
- if(temp != FM1702_OK)
- {
- return FM1702_WRITEERR;
- }
- return FM1702_OK;
- }
- #endif
- ///////////////////////////////////////////////////////////////////////
- // Delay 50us
- ///////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////
- // Delay 50us
- ///////////////////////////////////////////////////////////////////////
- void delay_50us(unsigned char _50us)
- {
- while(_50us--)
- {
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- }
- }
- ///////////////////////////////////////////////////////////////////////
- // Delay 10ms
- ///////////////////////////////////////////////////////////////////////
- void delay_10ms(unsigned int _10ms)
- {
- #ifndef NO_TIMER2
- RCAP2LH = RCAP2_10ms;
- T2LH = RCAP2_10ms;
- TR2 = TRUE;
- while (_10ms--)
- {
- while (!TF2);
- TF2 = FALSE;
- }
- TR2 = FALSE;
- #else
- while (_10ms--)
- {
- delay_50us(19);
- if (CmdValid)
- return;
- delay_50us(20);
- if (CmdValid)
- return;
- delay_50us(20);
- if (CmdValid)
- return;
- delay_50us(20);
- if (CmdValid)
- return;
- delay_50us(20);
- if (CmdValid )
- return;
- delay_50us(20);
- if (CmdValid)
- return;
- delay_50us(20);
- if (CmdValid)
- return;
- delay_50us(20);
- if (CmdValid)
- return;
- delay_50us(20);
- if (CmdValid)
- return;
- delay_50us(19);
- if (CmdValid)
- return;
- }
- #endif
- }