⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 paypass_anticollision.c

📁 读RF卡的源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
//=============================================================================
//                  Copyright (C) INSIDE Contactless 1998-2005
//
// INSIDE Contactless reserves the right to make changes, without notice,
// to any product (including application note) herein to improve
// reliability, functionality, or design. INSIDE Contactless advises its
// customers to obtain the latest version of device data sheets to verify,
// before placing orders, that the information being relied upon by the
// customer is current.
//
// INSIDE Contactless makes no warranty that the use will not infringe any
// third party patent, copyright or trademark.
//
// Information furnished by INSIDE Contactless is believed to be accurate
// and reliable. However, INSIDE Contactless does not assume any liability
// resulting from the application or use of any product described within.
//
// All rights are reserved. Reproduction in whole or in part is prohibited
// without the written consent of the copyright owner.
//
//      Bat 11a,
//      Parc Club du Golf,
//      Z.A.C. du Pichaury                      Tel :   +33 (0)4.42.39.33.00
//      13856 Aix-en-Provence Cedex 3           Fax :   +33 (0)4.42.39.63.19
//      FRANCE                                  Email : info@insidefr.com
//
//-----------------------------------------------------------------------------
// Project Code : PAYPASS LIBRARY
// Project Name : PAYPASS LIBRARY
// Module Name  : PayPass_Anticollision.c
// Platform dev : Keil 礦ision 3 (IDE ) + Keil ARM Compiler
// Target       : LPC2103 (ARM7TDMI Core)
// Language     : C ANSI
// Revision     : 1.0
// Description  : PayPass Anticollision functions 
//=============================================================================
// When		Who	Ver		What
// 06-04-24	MCY	1.1		restructuration
// 06-05-12	FPK	1.2		corrections
//=============================================================================

// C ANSI Defines 
#include <stdio.h>
#include <string.h>

// Defines for PicoRead
#include "PRD_PicoReadRF_Pages_Parameters.h" 	 // Definition of PicoRead chip registers
#include "PRD_Protocols_Defines.h"   // READER configuration parameters
#include "PRD_Config.h"   	// Reader Configuration functions
#include "PRD_RF_Comm.h" 
#include "PRD_BufferExchange.h"


// Defines for LPC2129
#include "Target.H"		// Definition of processor registers  & Low level functions to be modified according to the target, and the user needs

// ISO A Commands
#include "PRD_ISO14443A_Commands.h"

// ISO B Commands
#include "PRD_ISO14443B_Commands.h"

// Defines TCL Functions
#include "PayPass_TCL.h"
// Externals + Definitions
#include "PayPass_Anticollision_Log.h"						// Option for Host mode  
//#include "PayPass_Init.h"
// Defines the PayPass Anticollision functions
#include "PayPass_Anticollision.h"


// Defines for common C writting
#include "Defines_C.h"  	// TRUE, FALSE, etc
 

unsigned char	g_abP6Config14443A_PayPass[8];
unsigned char 	g_abP6Config14443B_PayPass[8];
unsigned short 	g_bATQBLength;
unsigned char 	g_abATQAResponse[2];
unsigned short 	g_bATQALength;
unsigned char 	g_abATQBResponse[16];
unsigned char 	g_abATQResponse[16];
unsigned char   g_abUID[16];
unsigned long 	g_FWT_SFGT;
unsigned char 	g_abRATS_ATTRIB[30];
unsigned char 	g_abAPDU_Data[300];
unsigned short  g_bATQLength;
unsigned short	g_bAPDULength;
unsigned char 	g_bUIDLength;	  
unsigned char   g_Anticollision_choice;
unsigned long 	g_bPayPassTimeoutValue;
unsigned char 	g_bLastBitSentValue;
unsigned short  g_iFDT_A_PICC_Shift=0;



 #if ((defined PAYPASS) || (defined FULL))

//-----------------------------------------------------------------------------
// Function name : 	v_fnProtocolChange(unsigned char p_pProtocolChoice, unsigned char* p_pP6Config, unsigned char p_pRFReceiveEOF, StructReader* p_pStructReader)
//-----------------------------------------------------------------------------
// Description : Change the protocol used and put the correpondant default values
//				 for the structure
//               
// IN       :   p_pProtocolChoice : choice of the protocol: ISOA_PROTOCOL or ISOB_PROTOCOL
//				p_pP6Config		  :	load the good config in page 6
//				p_pRFReceiveEOF	  : 0 for ISO-A, 1 for ISO-B
//				p_pStructReader   : Pointer to the used StructReader structure.
//
// OUT      :  	p_pStructReader   : Pointer to the used StructReader structure.
//  
// RETURN   :	- none -
//
// Notes    :  	- none -
//-----------------------------------------------------------------------------
void v_fnProtocolChange(unsigned char p_pProtocolChoice, unsigned char* p_pP6Config, unsigned char p_pRFReceiveEOF, StructReader* p_pStructReader)
{
	// DEFAULT CONFIG ISO14443A or ISO14443B USING DEFAULT ANTICOLLISION OPTIONS (see defines above, ISO150_DEFAULT... )
	v_fnConfigReader(CONFIG_ANALOG,&p_pP6Config[0],&p_pRFReceiveEOF,p_pStructReader);
	v_fnConfigReader(CONFIG_DEFAULT,&p_pProtocolChoice,&p_pRFReceiveEOF,p_pStructReader);

	if (p_pProtocolChoice == ISOA_PROTOCOL)		// ISO14443-A
  	{
	    // DEFAULT VALUES
		// TIMEOUT OF 90 祍 for REQA, WUPA, SELECT & ANTICOLLISION CMD + OFFSET PICOREAD IN A
		p_pStructReader->s_bRTimeoutUsed = (_FDT_A_PICC_MIN + _PICOREAD_TIMEOUT_OFFSET_A);	
		p_pStructReader->s_bRFrameSizeUsed = p_pStructReader->s_bRFrameSizeMax & 0xFE;
		p_pStructReader->s_bCommunicationSupportedSpeed = (P0_S106K | P0_R106K);
		v_fnConfigBitRate(p_pStructReader->s_bCommunicationSupportedSpeed,&p_pRFReceiveEOF,p_pStructReader);
		p_pStructReader->s_bCommunicationSupportedSpeed = p_pStructReader->s_bReaderSupportedSpeed;
	
	}
	else										// ISO14443-B
	{
		// DEFAULT VALUES
		// TIMEOUT OF 566 祍 for REQB & WUPB CMD + OFFSET PICOREAD IN B
		p_pStructReader->s_bRTimeoutUsed = (_FWT_ATQB + _PICOREAD_TIMEOUT_OFFSET_B);		
		p_pStructReader->s_bRFrameSizeUsed = p_pStructReader->s_bRFrameSizeMax & 0xFE;
		v_fnConfigBitRate((P0_S106K | P0_R106K),&p_pRFReceiveEOF,p_pStructReader);
		p_pStructReader->s_bCommunicationSupportedSpeed = P0_S106K | P0_R106K;
	}
	// FOR TESTING RECEPTION OF BUFFER FOR FAST RESPONSE IN ISOA
	g_bPayPassTimeoutValue = p_pStructReader->s_bRTimeoutUsed;
}


//-----------------------------------------------------------------------------
// Function name : 	v_fnResetWait()
//-----------------------------------------------------------------------------
// Description : Reset the field and wait tP = 9,44ms
//               
// IN       :   - none -
//
// OUT      :  	- none -
//  
// RETURN   :	- none -
//
// Notes    :  	- none -
//-----------------------------------------------------------------------------
void v_fnResetWait()		
{
    unsigned char l_bDelay;

	// CARRIER OFF
	v_fnCS_reset();
    v_fnSPI_SendByte(P0_REG_NO_RST | P0_PAGE_SELECT_1);			  
	
	// WAIT tP = 9,44ms  (20 x 472 = 9440祍)
	for(l_bDelay=20;l_bDelay>0;l_bDelay--)
	{ 
		delay_us(472);		  
	}
	
	// CARRIER ON
	v_fnCS_reset();
	v_fnSPI_SendByte(P0_REG_NO_RST | P0_PAGE_SELECT_1 | P0_RFON); 
}
//-----------------------------------------------------------------------------
// Function name : 	v_fnResetField()
//-----------------------------------------------------------------------------
// Description : Reset the field and wait tP = 9,44ms
//               
// IN       :   - none -
//
// OUT      :  	- none -
//  
// RETURN   :	- none -
//
// Notes    :  	- none -
//-----------------------------------------------------------------------------
void v_fnResetField(short p_dMicroSec)		
{
    unsigned char l_bDelay;

	// CARRIER OFF
	v_fnCS_reset();
    v_fnSPI_SendByte(P0_REG_NO_RST | P0_PAGE_SELECT_1);			  
	
	// WAIT tP = 9,44ms  (20 x 472 = 9440祍)
	for(l_bDelay=20;l_bDelay>0;l_bDelay--)
	{ 
		delay_us(p_dMicroSec);		  
	}
	
	// CARRIER ON
	v_fnCS_reset();
	v_fnSPI_SendByte(P0_REG_NO_RST | P0_PAGE_SELECT_1 | P0_RFON); 
}
//-----------------------------------------------------------------------------
// Function name : 	b_fnFWT_SFGTCalculate(unsigned char p_pchoice,unsigned char p_pabBuffer)
//-----------------------------------------------------------------------------
// Description : Calculate the FWT(ISO-B) or the SFGT(ISO-A) thanks to the FWI or SFGI
//               
// IN       :   p_pchoice   : Pointer to choose to calculate FWT or SFGT
//				p_pabBuffer : Byte where there is FWI
//
// OUT      :   - none -
//  
// RETURN   :	l_bFWT	    :  value of the timeout
//
// Notes    :  	- none -
//-----------------------------------------------------------------------------
unsigned long b_fnFWT_SFGTCalculate(unsigned char p_pchoice,unsigned char p_pabBuffer,unsigned p_bAddDelta)
{	
	unsigned long l_bFWI;
	unsigned long l_bFWT;
	

	// EXTRACT FROM BYTE THE FWI OR SFGI TO CALCULATE FWT(ISO-B or ISO-B for T=CL) OR SFGT(ISO-A)
	if(p_pchoice == _FWT) 		      // Requirements 4.4
	{
		l_bFWI = ((p_pabBuffer )>>4);	
	}
	if(p_pchoice == _SFGT) 		      // Requirements 4.8
	{
		l_bFWI = (p_pabBuffer & 0x0F);	    
	}
	l_bFWT=1;

	// l_bFWT = FWT + Delta(FWT) = 32*(2^FWI) + 3*(2^FWI) = 35*(2^(FWI)) etu
	while(l_bFWI!=0)	//l_bFWT = 2^FWI
	{
		l_bFWT *= 2;
		l_bFWI -= 1;
	}

	if(p_bAddDelta==0) {l_bFWT *= 302080;}	// l_bFWT * ((32+Delta)=35) * 9440 = in ns		 (9,44祍 = 1 etu)
	else {l_bFWT *= 330400;}
	l_bFWT /= 1000;		// in 祍

	return l_bFWT;
}


//-----------------------------------------------------------------------------
// Function name : b_fnException_Processing(unsigned char p_error_type)
//-----------------------------------------------------------------------------
// Description : When there is an error, there is a reset
//
// IN       :   p_error_type : error type(transmission, protocol or time out error)
//
// OUT      :  - none - 
//  
// RETURN   :  - none -	
//
// Notes    :  - none -
//-----------------------------------------------------------------------------
void v_fnExceptionProcessing(unsigned char p_error_type)
{
#ifdef MODE_DEBUG	
	printf("\nReset PICCS ");

	// Transmission, Protocol or Time-out errors
	if (p_error_type == TRANSMISSION_ERROR)
	{
		printf(" : after a transmission error\n");
	}
	if (p_error_type == PROTOCOL_ERROR)	    
	{
		printf(" : after a protocol error\n");
	}
	if (p_error_type == TIME_OUT_ERROR)	    
	{
		printf(" : after a time-out error\n");
 	}
#endif
	 p_error_type; // NOT USED

/*	#ifdef TTAL1
	if(g_sLog.enable)	   
	{
		// BUFFER CONCATENATION
		sprintf(g_sLog.buffer,"Reset PICCS because of a ");
		if (p_error_type == TRANSMISSION_ERROR)
		{
			sprintf(g_sLog.buffer + strlen(g_sLog.buffer),"Transmission error\n");
		}
		if (p_error_type == PROTOCOL_ERROR)	    
		{
			sprintf(g_sLog.buffer + strlen(g_sLog.buffer),"Protocol error\n");
		}
		if (p_error_type == TIME_OUT_ERROR)	    
		{
			sprintf(g_sLog.buffer + strlen(g_sLog.buffer),"Timeout error\n");
		}
	}
	#endif
*/
	// RESET FIELD AND WAIT DURING Treset	
	v_fnResetWait();
}

#ifdef TTAL1
//-----------------------------------------------------------------------------
// Function name : 	b_fnSendReceiveREQAWUPA_Transparent(unsigned char p_pcmd,unsigned char* p_pabBuffer,StructReader* p_pStructReader, unsigned short* p_bATQALength)
//-----------------------------------------------------------------------------
// Description : Send the REQA/WUPA Command in Transparent Mode
//               
// IN       :   p_pStructReader : Pointer to the used StructReader structure.
//				p_pcmd  		: choice between REQA and WUPA
//
// RETURN   :	l_bStatus :	   Error code
//
// Notes    :  	- none -
//-----------------------------------------------------------------------------
unsigned char b_fnSendReceiveREQAWUPA_Transparent(unsigned char p_pcmd,StructReader* p_pStructReader )
{
	unsigned char l_abBuffer[1];
	unsigned char l_bStatus;

	// _ISOA_REQA (0x26) or _ISOA_WUPA (0x52)
	l_abBuffer[0] = p_pcmd;			  
	
	v_fnSendBuffer(0,0,7,&l_abBuffer[0],p_pStructReader);
	
	// wait end of transmission
	delay_us(30);

	// bitset  PIN P0.16(LPC2103) or P0.18(LPC2129)
//	setDEBUG_PIN2();
	setSDO_PIN();

	// tempo
	delay_us(270);
	
	// clear bit
	clearSDO_PIN();
//	clearDEBUG_PIN2();

	return l_bStatus;
}


//-----------------------------------------------------------------------------
// Function name : 	b_fnSendReceiveREQBWUPB_Transparent(unsigned char p_pcmd,unsigned char* p_pabBuffer,StructReader* p_pStructReader, unsigned short* p_bATQBLength)
//-----------------------------------------------------------------------------
// Description : Send the REQB/WUPB Command in Transparent Mode
//               
// IN       :   p_pStructReader : Pointer to the used StructReader structure.
//				p_pcmd  		: choice between REQB and WUPB
//  
// RETURN   :	l_bStatus :	   Error code
//
// Notes    :  	- none -
//-----------------------------------------------------------------------------
unsigned char b_fnSendReceiveREQBWUPB_Transparent(unsigned char p_pcmd,StructReader* p_pStructReader)
{
	unsigned char l_abBuffer[3];
	unsigned char l_bStatus;

	l_abBuffer[0] = _ISOB_REQB;		  // 0x05
	
	// Application Family Indicator --> all application families	
	l_abBuffer[1] = _ISOB_AFI_ALL; 
	
	// Coding of PARAM Byte included in REQB and WUPB command
	// Number of slots (N) = '000' (Requirements 6.3)
	// _ISOB_REQB_NO_SLOT (0x00) or _ISOB_WUPB_NO_SLOT (0x08)      
	l_abBuffer[2] = p_pcmd;    		 
	
	v_fnSendBuffer(_SENDCRC,3,0,&l_abBuffer[0],p_pStructReader); 
	
	return l_bStatus;
}

#endif


//-----------------------------------------------------------------------------
// Function name : 	b_fnSendReceiveREQBWUPB_PayPass(unsigned char p_pcmd,unsigned char* p_pabBuffer,StructReader* p_pStructReader, unsigned short* p_bATQBLength)
//-----------------------------------------------------------------------------
// Description : Send the REQB/WUPB Command
//               
// IN       :   p_pStructReader : Pointer to the used StructReader structure.
//				p_pcmd  		: choice between REQB and WUPB
//
// OUT      :  	p_bATQBLength   : size of ATQB
//				p_pabBuffer     : ATQB
//  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -