#include "sys.h"
#include "rc522.h"
#include "delay.h"
#include "string.h"
#include "cba.h"
#include "Timer.h"
#include "canp_hostcom.h"
#define MAXRLEN 18
#define MF522_RST PFout(1)
static uint8_t Rc522_LinkFlag;
//#define RC522_LINKTEST_RT(rt) if(Rc522_LinkFlag == 0) return (rt)
//#define RC522_LINKTEST_NULL() if(Rc522_LinkFlag == 0) return
uint32_t Rs522_cmd_cnt;
#define WriteRawRC(addr,datas) if(WriteRawRC_HDL(addr,datas)!= STATUS_SUCCESS) return MI_ERR
void delay_ns(u32 ns)
{
static volatile uint32_t i;
for(i=0;i<ns;i++)
{
__nop();
__nop();
__nop();
}
}
uint8_t TXRFID[16] = {0x41,0x31,0x42,0x32,0x43,0x33,0x44,0x34,0x46,0x31,0x42,0x32,0x43,0x33,0x44,0x34};
uint8_t RXRFID[16];
/*
º¯Êý¹¦ÄÜ£ºÈ«×Ô¶¯¶Á¿¨º¯Êý
²Î Êý£ºÎÞ
·µ »Ø Öµ£ºÎÞ
**/
void Read_Card(void)
{
char status = MI_ERR;
uint8_t CT[2]; //¿¨ÀàÐÍ
uint8_t SN[4]; //¿¨ºÅ
uint8_t KEY[6]={0xff,0xff,0xff,0xff,0xff,0xff}; //ÃÜÔ¿
uint8_t s = 0x01;
uint8_t RXRFIDH[8];
#define DATA_LEN 16 //¶¨ÒåÊý¾Ý×Ö½Ú³¤¶È
LED1 = 0;
LED2 = 0;
LED3 = 0;
LED4 = 0;
status = PcdRequest(PICC_REQALL,CT); //Ñ°¿¨
if(status == MI_OK) //Ñ°¿¨³É¹¦
{
status=MI_ERR;
LED1 = 1;
status = PcdAnticoll(SN); //·À³åײ
if(status == MI_OK)
{
status=MI_ERR;
LED2 = 1;
status =PcdSelect(SN); //Ñ¡¶¨´Ë¿¨
if(status == MI_OK) //Ñ¡¶¨³É¹¦
{
status=MI_ERR;
LED3 = 1;
MP_SPK = 1;
status =PcdAuthState(0x60,0x03,KEY,SN); //ÑéÖ¤ÃÜÔ¿
if(status == MI_OK)
{
status = MI_ERR;
status = PcdWrite(s,TXRFID); //д¿¨
if(status == MI_OK)
{
status = MI_ERR;
}
status=PcdRead(s,RXRFID); //¶Á¿¨
if(status == MI_OK)
{
status = MI_ERR;
LED4 = 1; //¶Á¿¨³É¹¦
MP_SPK = 0;
Send_InfoData_To_Fifo(RXRFID,16);
Send_InfoData_To_Fifo("\n",2);
}
}
}
}
}
}
//³õʼ»¯IO ´®¿Ú1
//bound:²¨ÌØÂÊ
void RC522_Uart_init(u32 baudrate)
{
GPIO_InitTypeDef GPIO_TypeDefStructure;
USART_InitTypeDef USART_TypeDefStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
//PA9-Tx
GPIO_TypeDefStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;
GPIO_TypeDefStructure.GPIO_Mode = GPIO_Mode_AF; //¸´Óù¦ÄÜ
GPIO_TypeDefStructure.GPIO_OType = GPIO_OType_PP; //ÍÆÍìÊä³ö
GPIO_TypeDefStructure.GPIO_PuPd = GPIO_PuPd_UP; //ÉÏÀ
GPIO_TypeDefStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA,&GPIO_TypeDefStructure);
USART_TypeDefStructure.USART_BaudRate = baudrate; //²¨ÌØÂÊ
USART_TypeDefStructure.USART_HardwareFlowControl = //ÎÞÓ²¼þ¿ØÖÆÁ÷
USART_HardwareFlowControl_None;
USART_TypeDefStructure.USART_Mode = USART_Mode_Tx|USART_Mode_Rx; //½ÓÊÕÓë·¢ËÍģʽ
USART_TypeDefStructure.USART_Parity = USART_Parity_No; //ÎÞУÑéλ
USART_TypeDefStructure.USART_StopBits = USART_StopBits_1; //ֹͣλ1
USART_TypeDefStructure.USART_WordLength = USART_WordLength_8b; //Êý¾Ýλ8λ
USART_Init(USART1,&USART_TypeDefStructure);
USART_Cmd(USART1, ENABLE); //ʹÄÜ´®¿Ú
Rc522_LinkFlag = 0;
}
char InitRc522(void)
{
if(PcdReset() != MI_OK)
return MI_ERR;
PcdAntennaOff();
delay_ms(2);
PcdAntennaOn();
if(M500PcdConfigISOType( 'A' ) != MI_OK)
return MI_ERR;
Rc522_LinkFlag = 1;
Rc522_ToSRst();
return MI_OK;
}
void Readcard_daivce_Init(void)
{
RC522_Uart_init(9600); // ´®¿Ú³õʼ»¯Îª9600
delay_ms(500);
InitRc522(); //¶Á¿¨Æ÷³õʼ»¯
}
void Reset_RC522(void)
{
PcdReset();
PcdAntennaOff();
delay_ms(2);
PcdAntennaOn();
}
/////////////////////////////////////////////////////////////////////
//¹¦ ÄÜ£ºÑ°¿¨
//²ÎÊý˵Ã÷: req_code[IN]:Ñ°¿¨·½Ê½
// 0x52 = Ñ°¸ÐÓ¦ÇøÄÚËùÓзûºÏ14443A±ê×¼µÄ¿¨
// 0x26 = Ѱδ½øÈëÐÝÃß״̬µÄ¿¨
// pTagType[OUT]£º¿¨Æ¬ÀàÐÍ´úÂë
// 0x4400 = Mifare_UltraLight
// 0x0400 = Mifare_One(S50)
// 0x0200 = Mifare_One(S70)
// 0x0800 = Mifare_Pro(X)
// 0x4403 = Mifare_DESFire
//·µ »Ø: ³É¹¦·µ»ØMI_OK
/////////////////////////////////////////////////////////////////////
char PcdRequest(unsigned char req_code,unsigned char *pTagType)
{
char status;
unsigned int unLen;
unsigned char ucComMF522Buf[MAXRLEN];
// unsigned char xTest ;
ClearBitMask(Status2Reg,0x08);
WriteRawRC(BitFramingReg,0x07);
// xTest = ReadRawRC(BitFramingReg);
// if(xTest == 0x07 )
// { LED_GREEN =0 ;}
// else {LED_GREEN =1 ;while(1){}}
SetBitMask(TxControlReg,0x03);
ucComMF522Buf[0] = req_code;
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,1,ucComMF522Buf,&unLen);
// if(status == MI_OK )
// { LED_GREEN =0 ;}
// else {LED_GREEN =1 ;}
if ((status == MI_OK) && (unLen == 0x10))
{
*pTagType = ucComMF522Buf[0];
*(pTagType+1) = ucComMF522Buf[1];
}
else
{ status = MI_ERR; }
return status;
}
/////////////////////////////////////////////////////////////////////
//¹¦ ÄÜ£º·À³åײ
//²ÎÊý˵Ã÷: pSnr[OUT]:¿¨Æ¬ÐòÁкţ¬4×Ö½Ú
//·µ »Ø: ³É¹¦·µ»ØMI_OK
/////////////////////////////////////////////////////////////////////
char PcdAnticoll(unsigned char *pSnr)
{
char status;
unsigned char i,snr_check=0;
unsigned int unLen;
unsigned char ucComMF522Buf[MAXRLEN];
ClearBitMask(Status2Reg,0x08);
WriteRawRC(BitFramingReg,0x00);
ClearBitMask(CollReg,0x80);
ucComMF522Buf[0] = PICC_ANTICOLL1;
ucComMF522Buf[1] = 0x20;
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,2,ucComMF522Buf,&unLen);
if (status == MI_OK)
{
for (i=0; i<4; i++)
{
*(pSnr+i) = ucComMF522Buf[i];
snr_check ^= ucComMF522Buf[i];
}
if (snr_check != ucComMF522Buf[i])
{ status = MI_ERR; }
}
SetBitMask(CollReg,0x80);
return status;
}
/////////////////////////////////////////////////////////////////////
//¹¦ ÄÜ£ºÑ¡¶¨¿¨Æ¬
//²ÎÊý˵Ã÷: pSnr[IN]:¿¨Æ¬ÐòÁкţ¬4×Ö½Ú
//·µ »Ø: ³É¹¦·µ»ØMI_OK
/////////////////////////////////////////////////////////////////////
char PcdSelect(unsigned char *pSnr)
{
char status;
unsigned char i;
unsigned int unLen;
unsigned char ucComMF522Buf[MAXRLEN];
ucComMF522Buf[0] = PICC_ANTICOLL1;
ucComMF522Buf[1] = 0x70;
ucComMF522Buf[6] = 0;
for (i=0; i<4; i++)
{
ucComMF522Buf[i+2] = *(pSnr+i);
ucComMF522Buf[6] ^= *(pSnr+i);
}
CalulateCRC(ucComMF522Buf,7,&ucComMF522Buf[7]);
ClearBitMask(Status2Reg,0x08);
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,9,ucComMF522Buf,&unLen);
if ((status == MI_OK) && (unLen == 0x18))
{ status = MI_OK; }
else
{ status = MI_ERR; }
return status;
}
/////////////////////////////////////////////////////////////////////
//¹¦ ÄÜ£ºÑéÖ¤¿¨Æ¬ÃÜÂë
//²ÎÊý˵Ã÷: auth_mode[IN]: ÃÜÂëÑé֤ģʽ
// 0x60 = ÑéÖ¤AÃÜÔ¿
// 0x61 = ÑéÖ¤BÃÜÔ¿
// addr[IN]£º¿éµØÖ·
// pKey[IN]£ºÃÜÂë
// pSnr[IN]£º¿¨Æ¬ÐòÁкţ¬4×Ö½Ú
//·µ »Ø: ³É¹¦·µ»ØMI_OK
/////////////////////////////////////////////////////////////////////
char PcdAuthState(unsigned char auth_mode,unsigned char addr,unsigned char *pKey,unsigned char *pSnr)
{
char status;
unsigned int unLen;
unsigned char i,ucComMF522Buf[MAXRLEN];
ucComMF522Buf[0] = auth_mode;
ucComMF522Buf[1] = addr;
for (i=0; i<6; i++)
{ ucComMF522Buf[i+2] = *(pKey+i); }
for (i=0; i<6; i++)
{ ucComMF522Buf[i+8] = *(pSnr+i); }
// memcpy(&ucComMF522Buf[2], pKey, 6);
// memcpy(&ucComMF522Buf[8], pSnr, 4);
status = PcdComMF522(PCD_AUTHENT,ucComMF522Buf,12,ucComMF522Buf,&unLen);
if ((status != MI_OK) || (!(ReadRawRC(Status2Reg) & 0x08)))
{ status = MI_ERR; }
return status;
}
/////////////////////////////////////////////////////////////////////
//¹¦ ÄÜ£º¶ÁÈ¡M1¿¨Ò»¿éÊý¾Ý
//²ÎÊý˵Ã÷: addr[IN]£º¿éµØÖ·
// pData[OUT]£º¶Á³öµÄÊý¾Ý£¬16×Ö½Ú
//·µ »Ø: ³É¹¦·µ»ØMI_OK
/////////////////////////////////////////////////////////////////////
char PcdRead(unsigned char addr,unsigned char *pData)
{
char status;
unsigned int unLen;
unsigned char i,ucComMF522Buf[MAXRLEN];
ucComMF522Buf[0] = PICC_READ;
ucComMF522Buf[1] = addr;
CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]);
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen);
if ((status == MI_OK) && (unLen == 0x90))
// { memcpy(pData, ucComMF522Buf, 16); }
{
for (i=0; i<16; i++)
{ *(pData+i) = ucComMF522Buf[i]; }
}
else
{ status = MI_ERR; }
return status;
}
/////////////////////////////////////////////////////////////////////
//¹¦ ÄÜ£ºÐ´Êý¾Ýµ½M1¿¨Ò»¿é
//²ÎÊý˵Ã÷: addr[IN]£º¿éµØÖ·
// pData[IN]£ºÐ´ÈëµÄÊý¾Ý£¬16×Ö½Ú
//·µ »Ø: ³É¹¦·µ»ØMI_OK
/////////////////////////////////////////////////////////////////////
char PcdWrite(unsigned char addr,unsigned char *pData)
{
char status;
unsigned int unLen;
unsigned char i,ucComMF522Buf[MAXRLEN];
ucComMF522Buf[0] = PICC_WRITE;
ucComMF522Buf[1] = addr;
CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]);
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen);
if ((status != MI_OK) || (unLen != 4) || ((ucComMF522Buf[0] & 0x0F) != 0x0A))
{ status = MI_ERR; }
if (status == MI_OK)
{
//memcpy(ucComMF522Buf, pData, 16);
for (i=0; i<16; i++)
{ ucComMF522Buf[i] = *(pData+i); }
CalulateCRC(ucComMF522Buf,16,&ucComMF522Buf[16]);
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,18,ucComMF522Buf,&unLen);
if ((status != MI_OK) || (unLen != 4) || ((ucComMF522Buf[0] & 0x0F) != 0x0A))
{ status = MI_ERR; }
}
return status;
}
/////////////////////////////////////////////////////////////////////
//¹¦ ÄÜ£ºÃüÁƬ½øÈëÐÝÃß״̬
//·µ »Ø: ³É¹¦·µ»ØMI_OK
/////////////////////////////////////////////////////////////////////
char PcdHalt(void)
{
char status;
unsigned int unLen;
unsigned char ucComMF522Buf[MAXRLEN];
ucComMF522Buf[0] = PICC_HALT;
ucComMF522Buf[1] = 0;
CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]);
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen);
return status;//MI_OK;
}
/////////////////////////////////////////////////////////////////////
//ÓÃMF522¼ÆËãCRC16º¯Êý
/////////////////////////////////////////////////////////////////////
char CalulateCRC(unsigned char *pIndata,unsigned char len,unsigned char *pOutData)
{
unsigned char i,n;
ClearBitMask(DivIrqReg,0x04);
WriteRawRC(CommandReg,PCD_IDLE);
SetBitMask(FIFOLevelReg,0x80);
for (i=0; i<len; i++)
{ WriteRawRC(FIFODataReg, *(pIndata+i)); }
WriteRawRC(CommandReg, PCD_CALCCRC);
i = 0xFF;
do
{
n = ReadRawRC(DivIrqReg);
i--;
}
while ((i!=0) && !(n&0x04));
pOutData[0] = ReadRawRC(CRCResultRegL);
pOutData[1] = ReadRawRC(CRCResultRegM);
return MI_OK;
}
/////////////////////////////////////////////////////////////////////
//¹¦ ÄÜ£º¸´Î»RC522
//·µ »Ø: ³É¹¦·µ»ØMI_OK
/////////////////////////////////////////////////////////////////////
char PcdReset(void)
{
/*
MF522_RST=1;
delay_ns(10);
MF522_RST=0;
delay_ns(10);
MF522_RST=1;
delay_ns(10);
*/
if(Rc522_OutSRst()!=MI_OK)
return MI_ERR;
WriteRawRC(CommandReg,PCD_RESETPHASE);
delay_ns(10);
WriteRawRC(ModeReg,0x3D); //ºÍMifare¿¨Í¨Ñ¶£¬CRC³õʼֵ0x6363
WriteRawRC(TReloadRegL,30);
WriteRawRC(TReloadRegH,0);
WriteRawRC(TModeReg,0x8D);
WriteRawRC(TPrescalerReg,0x3E);
WriteRawRC(TxAutoReg,0x40);
return MI_OK;
// return MI_ERR;
}
//////////////////////////////////////////////////////////////////////
//ÉèÖÃRC522µÄ¹¤×÷·½Ê½
//////////////////////////////////////////////////////////////////////
char M500PcdConfigISOType(unsigned char type)
{
if (type == 'A') //ISO14443_A
{
ClearBitMask(Status2Reg,0x08);
WriteRawRC(ModeReg,0x3D);//3F
WriteRawRC(RxSelReg,0x86);//84
WriteRawRC(RFCfgReg,0x7F); //4F
WriteRawRC(TReloadRegL,30);//tmoLength);// TReloadVal = 'h6a =tmoLength(dec)
WriteRawRC(TReloadRegH,0);
WriteRawRC(TModeReg,0x8D);
WriteRawRC(TPrescalerReg,0x3E);
delay_ms(10);
PcdAntennaOn();
// WriteRawRC(SerialSpeedReg,0x7A);//
//WriteRawRC(CommandReg,0x00);//
}
else{ return -1; }
return MI_OK;
}
/*************************************************
Function: Send_data
Description:
write a byte to serial port
Parameter:
ch the byte to write
Return:
None
**************************************************/
void Send_data(unsigned char ch)
{
USART1->SR;
while (!(USART1->SR & USART_FLAG_TXE));
USART_SendData(USART1,ch);
//while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
}
/*************************************************
Function: Rece_data
Description:
get a byte from serial port
Parameter:
ch the byte to save the received byte
WaitTime the counter for polling, avoid endless loop.
Return:
STATUS_SUCCESS get a byte data successfully
STATUS_IO_TIMEOUT time out
**************************************************/
short Rece_data(unsigned char *ch, unsigned int WaitTime)
{
/*
unsigned int j;
j=0;
while(!(USART1->SR & USART_FLAG_RXNE))
{
j++;
if(j>=WaitTime) //avoid endless loop
break;
}
if(j<WaitTime)
{
*ch = USART_ReceiveData(USART1);
//RI=0;
return STATUS_SUCCESS;
}
else
return STATUS_IO_TIMEOUT;
*/
// while(!(UART4->SR & USART_FLAG_RXNE));
// *ch = (uint8_t)USART_ReceiveData(UART4);
// return STATUS_SUCCESS;
// while(!(USART3->SR & USART_FLAG_RXNE));
// *ch = (uint8_t)USART_ReceiveData(USART3);
// return STATUS_SUCCESS;
uint32_t tt;
tt = gt_get() + WaitTime/2000;
while(gt_get_sub(tt))
{
if(USART1->SR & USART_FLAG_RXNE)
{
*ch = (uint8_t)USART_ReceiveData(USART1);
return STATUS_SUCCESS;
}
}
Rc522_LinkFlag = 0;
return STATUS_IO_TIMEOUT;
}
/////////////////////////////////////////////////////////////////////
//¹¦ ÄÜ£º¶ÁRC632¼Ä´æÆ÷
//²ÎÊý˵Ã÷£ºAddress[IN]:¼Ä´æÆ÷µØÖ·
//·µ »Ø£º¶Á³öµÄÖµ
/////////////////////////////////////////////////////////////////////
unsigned char ReadRawRC(unsigned char Address)
{
unsigned char RegVal;
short status;
Address = (Address & 0x3f) | 0x80; //code the first byte
Send_data(Address);
status = Rece_data(&RegVal, 10000);
if(status != STATUS_SUCCESS)
return 0xff;
return RegVal;
}
/////////////////////////////////////////////////////////////////////
//¹¦ ÄÜ£ºÐ´RC632¼Ä´æÆ÷
//²ÎÊý˵Ã÷£ºAddress[IN]:¼Ä´æÆ÷µØÖ·
// value[IN]:дÈëµÄÖµ
/////////////////////////////////////////////////////////////////////
//short WriteRawRC(unsigned char Address, unsigned char value)
//{
// unsigned char EchoByte;
// short status;
// Address &= 0x3f; //code the first byte
// Send_data(Address);
// Send_data(value);
//
// status = Rece_data(&EchoByte, 10000);
// return status;
//}
short WriteRawRC_HDL2(unsigned char Address, unsigned char value)
{
unsigned char EchoByte;
short status;
Address &= 0x3f; //code the first byte
Send_data(Address);
Send_data(value);
status = Rece_data(&EchoByte, 10000);
//delay_ns(10);
if(status == STATUS_SUCCESS)
{
if(Address == EchoByte)
Rs522_cmd_cnt++;
else status = STARUS_ADDR_RERR;
//Send_data(value);
}
// delay_ms(2);
return status;
}
short WriteRawRC_HDL(unsigned char Address, unsigned char value)
{
unsigned char EchoByte;
short status;
uint8_t e = 3;
Address &= 0x3f; //code the first byte
for(e = 0;e<3;e++)
{
Send_data(Address);
status = Rece_data(&EchoByte, 10000);
//delay_ns(10);
if(status == STATUS_SUCCESS)
{
if(Address == EchoByte)
{
Rs522_cmd_cnt++;
Send_data(value);
break;
}
else
{
//Rc522_OutSRst();
status = STARUS_ADDR_RERR;
}
}
}
// delay_ms(2);
return status;
}
/////////////////////////////////////////////////////////////////////
//¹¦ ÄÜ£ºÖÃRC522¼Ä´æÆ÷λ
//²ÎÊý˵Ã÷£ºreg[IN]:¼Ä´æÆ÷µØÖ·
// mask[IN]:ÖÃλֵ
/////////////////////////////////////////////////////////////////////
char SetBitMask(unsigned char reg,unsigned char mask)
{
char tmp = 0x0;
tmp = ReadRawRC(reg);
WriteRawRC(reg,tmp | mask); // set bit mask
return MI_OK;
}
/////////////////////////////////////////////////////////////////////
//¹¦ ÄÜ£ºÇåRC522¼Ä´æÆ÷λ
//²ÎÊý˵Ã÷£ºreg[IN]:¼Ä´æÆ÷µØÖ·
// mask[IN]:Çåλֵ
/////////////////////////////////////////////////////////////////////
char ClearBitMask(unsigned char reg,unsigned char mask)
{
char tmp = 0x0;
tmp = ReadRawRC(reg);
WriteRawRC(reg, tmp & ~mask); // clear bit mask
return MI_OK;
}
/////////////////////////////////////////////////////////////////////
//¹¦ ÄÜ£ºÍ¨¹ýRC522ºÍISO14443¿¨Í¨Ñ¶
//²ÎÊý˵Ã÷£ºCommand[IN]:RC522ÃüÁî×Ö
// pInData[IN]:ͨ¹ýRC522·¢Ë͵½¿¨Æ¬µÄÊý¾Ý
// InLenByte[IN]:·¢ËÍÊý¾ÝµÄ×Ö½Ú³¤¶È
// pOutData[OUT]:½ÓÊÕµ½µÄ¿¨Æ¬·µ»ØÊý¾Ý
// *pOutLenBit[OUT]:·µ»ØÊý¾ÝµÄ볤¶È
/////////////////////////////////////////////////////////////////////
char PcdComMF522(unsigned char Command,
unsigned char *pInData,
unsigned char InLenByte,
unsigned char *pOutData,
unsigned int *pOutLenBit)
{
char status = MI_ERR;
unsigned char irqEn = 0x00;
unsigned char waitFor = 0x00;
unsigned char lastBits;
unsigned char n;
unsigned int i;
switch (Command)
{
case PCD_AUTHENT:
irqEn = 0x12;
waitFor = 0x10;
break;
case PCD_TRANSCEIVE:
irqEn = 0x77;
waitFor = 0x30;
break;
default:
break;
}
WriteRawRC(ComIEnReg,irqEn|0x80);
ClearBitMask(ComIrqReg,0x80);
WriteRawRC(CommandReg,PCD_IDLE);
SetBitMask(FIFOLevelReg,0x80);
for (i=0; i<InLenByte; i++)
{ WriteRawRC(FIFODataReg, pInData[i]); }
WriteRawRC(CommandReg, Command);
if (Command == PCD_TRANSCEIVE)
{ SetBitMask(BitFramingReg,0x80); }
i = 600;//¸ù¾ÝʱÖÓƵÂʵ÷Õû£¬²Ù×÷M1¿¨×î´óµÈ´ýʱ¼ä25ms
//i = 2000;
do
{
n = ReadRawRC(ComIrqReg);
i--;
}
while ((i!=0) && !(n&0x01) && !(n&waitFor));
ClearBitMask(BitFramingReg,0x80);
if (i!=0)
{
if(!(ReadRawRC(ErrorReg)&0x1B))
{
status = MI_OK;
if (n & irqEn & 0x01)
{ status = MI_NOTAGERR; }
if (Command == PCD_TRANSCEIVE)
{
n = ReadRawRC(FIFOLevelReg);
lastBits = ReadRawRC(ControlReg) & 0x07;
if (lastBits)
{ *pOutLenBit = (n-1)*8 + lastBits; }
else
{ *pOutLenBit = n*8; }
if (n == 0)
{ n = 1; }
if (n > MAXRLEN)
{ n = MAXRLEN; }
for (i=0; i<n; i++)
{ pOutData[i] = ReadRawRC(FIFODataReg); }
}
}
else
{ status = MI_ERR; }
}
SetBitMask(ControlReg,0x80); // stop timer now
WriteRawRC(CommandReg,PCD_IDLE);
return status;
}
/////////////////////////////////////////////////////////////////////
//¿ªÆôÌìÏß
//ÿ´ÎÆô¶¯»ò¹Ø±ÕÌìÏÕ·¢ÉäÖ®¼äÓ¦ÖÁÉÙÓÐ1msµÄ¼ä¸ô
/////////////////////////////////////////////////////////////////////
void PcdAntennaOn()
{
unsigned char i;
i = ReadRawRC(TxControlReg);
if (!(i & 0x03))
{
SetBitMask(TxControlReg, 0x03);
}
}
/////////////////////////////////////////////////////////////////////
//¹Ø±ÕÌìÏß
/////////////////////////////////////////////////////////////////////
void PcdAntennaOff()
{
ClearBitMask(TxControlReg, 0x03);
}
//µÈ´ý¿¨À뿪
void WaitCardOff(void)
{
unsigned char status, TagType[2];
while(1)
{
status = PcdRequest(REQ_ALL, TagType);
if(status)
{
status = PcdRequest(REQ_ALL, TagType);
if(status)
{
status = PcdRequest(REQ_ALL, TagType);
if(status)
{
return;
}
}
}
delay_ms(10);
}
}
//_______________________________________________________________
uint8_t Rc522_GetLinkFlag(void)
{
return Rc522_LinkFlag;
}
void Rc522_LinkTest(void)
{
ReadRawRC(TxControlReg);
}
int8_t Rc522_OutSRst(void)
{
uint16_t i;
uint8_t Rd;
uint8_t Rt = MI_ERR;
for(i = 0;i<100;i++)
{
Send_data(0x55);
if(Rece_data(&Rd, 10000) == STATUS_SUCCESS)
//if(Rece_data(&Rd, 1000) == STATUS_SUCCESS)
{
//if (Rd == 0)
//Rc522_LinkTest();
Send_data(0x55);
delay_ms(100);
Rt = MI_OK;
break;
}
}
return Rt;
}
int8_t Rc522_ToSRst(void)
{
//WriteRawRC(CommandReg,PCD_IDLE|0x10);
return 0;
}
//extern void Send_InfoData_To_Fifo( uint8_t *p ,uint8_t len);
//extern void Full_Diplay(uint32_t vluae);
void Rc522_DispCnt(void)
{
//Send_InfoData_To_Fifo((uint8_t *)"Rc522ccnt=",10);
//Full_Diplay(Rs522_cmd_cnt);
}
//_______________________________________________________________
补充程序代码 |