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

📄 prd_rf_comm.c

📁 读RF卡的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
//=============================================================================
//                  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 : PICOREAD LIBRARY
// Project Name : PICOREAD LIBRARY
// Module Name  : PRD_RF_Comm.c
// Platform dev : Keil 礦ision 3 (IDE ) + Keil ARM Compiler
// Target       : LPC2103 (ARM7TDMI Core)
// Language     : C ANSI
// Revision     : 1.0
// Description  : Low level communication protocol with Picoread RF chip
//=============================================================================
// When		Who	Ver		What
// 06-04-24	MCY	1.1		restructuration
// 06-05-12	FPK	1.2		restructuration
//=============================================================================


// Defines standard


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

// Defines for PicoRead
#include "PRD_PicoReadRF_Pages_Parameters.h" // Definition of PicoRead chip registers
#include "PRD_RF_Comm.h" 

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

 #if ((defined PAYPASS) || (defined FULL))
#include "PayPass_Anticollision.h"
#include "PayPass_TTAL1_Default_Anticollision_Options.h"
#endif


#ifdef MODE_DEBUG_TCL
#include <stdio.h>
#endif


//-----------------------------------------------------------------------------
// Function name : void v_fnCS_reset( ) or v_fnSPI_reset()
//-----------------------------------------------------------------------------
// Description : Reset the SPI address
//
// IN       :   - none -
// OUT      :   - none -
// RETURN   :   - none -
// Notes    :   - none -
//-----------------------------------------------------------------------------
#define v_fnSPI_reset() v_fnCS_reset()
void v_fnCS_reset()
{
    setCS_PIN();
	delay_us(1);
    clearCS_PIN();
}


//-----------------------------------------------------------------------------
// Function name : void v_fnSPI_WaitForEOT( )
//-----------------------------------------------------------------------------
// Description : Wait for end of transmission from PicoRead (always happen).
//
// IN       :   - none -
// OUT      :   - none -
// RETURN   :   - none -
// Notes    :   - none -
//-----------------------------------------------------------------------------

void v_fnSPI_WaitForEOT()
{
  
	while(testSDI_PIN());
    while(!(testSDI_PIN()));
}


//-----------------------------------------------------------------------------
// Function name : void v_fnSPI_SendBuffer
//-----------------------------------------------------------------------------
// Description : Send buffer in defined protocol.
//
// IN       :   p_bBytesLength   : Number of bytes to send
//              p_bBitsLength    : Number of bits to send
//				p_pabBuffer		 : Pointer to Buffer to Send
//
// OUT      :   - none -
// RETURN   :   - none -
// Notes    :   - none -
//-----------------------------------------------------------------------------

void v_fnSPI_SendBuffer(unsigned char p_bBytesLength, unsigned char p_bBitsLength,unsigned char * p_pabBuffer)
{
	signed char l_bBitNum;
	unsigned char l_bByteNum;
	unsigned char l_bByteToSend;
	unsigned char l_ilocalParity=1;
 	
	delay_us(1); 

	for(l_bByteNum=0;l_bByteNum<p_bBytesLength;l_bByteNum++)						   //Byte cycling
	{
		l_bByteToSend =*p_pabBuffer;
		
		while(testSDI_PIN());

        for(l_bBitNum=7;l_bBitNum>=0;l_bBitNum--)	   //bit cycling
		{
			clearSDO_PIN();
		
			if ( (l_bByteToSend >> l_bBitNum) & 1 )
			{
				 #if ((defined PAYPASS) || (defined FULL))
				l_ilocalParity^=1;
				#endif
				setSDO_PIN();
			}
		   	setSCK_PIN();
	
			clearSCK_PIN();
	
		}
		p_pabBuffer++;
	 	
    }
	 #if ((defined PAYPASS) || (defined FULL))
	g_bLastBitSentValue=l_ilocalParity;
	#endif

    if (p_bBitsLength & 0x07) // Bits to send  //max 7 bits
    {
        // Wait until the FIFO is empty
        while(testSDI_PIN());

        while (p_bBitsLength--)
        { 
	    	// Position the correct SDI regarding the data
            clearSDO_PIN();
            if ( bit_testChar(p_pabBuffer,p_bBitsLength) ) 
			{
				setSDO_PIN();
			}
			 #if ((defined PAYPASS) || (defined FULL))
			if(p_bBitsLength==0) {g_bLastBitSentValue==bit_testChar(p_pabBuffer,0);}
			#endif
	    	// Make a clock cycle
            setSCK_PIN();
            clearSCK_PIN();
        }
    }



}


//-----------------------------------------------------------------------------
// Function name : void v_fnSPI_SendByte
//-----------------------------------------------------------------------------
// Description : Send one byte through SPI.
//
// IN       :   p_bData   : Byte to send
//
// OUT      :   - none -
// RETURN   :   - none -
// Notes    :   - none -
//-----------------------------------------------------------------------------

void v_fnSPI_SendByte(unsigned char p_bData)
{
     signed char l_bBitNum;
	  
	 for(l_bBitNum=7;l_bBitNum>=0;l_bBitNum--)	   //bit cycling
	 {
			clearSDO_PIN();
			if((p_bData >> l_bBitNum)&1)	
			{
				setSDO_PIN();
			}
		   	setSCK_PIN();
			//delay_us(1);			
			clearSCK_PIN();
			//delay_us(1);			
	 }
	 
}


//-----------------------------------------------------------------------------
// Function name : unsigned char b_fnSPI_ReceiveByte()
//-----------------------------------------------------------------------------
// Description : Receive one byte through SPI.
//
// IN       :   - none -
// OUT      :   - none -
//
// RETURN   :   Received byte from SPI
//
// Notes    :   - none -
//-----------------------------------------------------------------------------

unsigned char b_fnSPI_ReceiveByte()
{
    unsigned char l_bByte;
	signed char l_bBitNum;
	
	l_bByte = 0x00;
	for(l_bBitNum=7;l_bBitNum>=0;l_bBitNum--)	   //bit cycling
	{
		setSCK_PIN();
		//	delay_us(1);
		if(testSDI_PIN()) 
		{
			bit_setChar(&l_bByte,l_bBitNum);
		}
		clearSCK_PIN();
		//	delay_us(1);
	}

    return l_bByte;
}





//-----------------------------------------------------------------------------
// Function name : unsigned char b_fnSPI_GetBuffer(unsigned char* p_pabBuffer,unsigned char* p_pbPicoReadStatus,unsigned short* p_pwRFPicoReadByteCounter,unsigned char* p_pbRFPicoReadBitCounter, unsigned char* p_pbRFReceiveEOF)
//-----------------------------------------------------------------------------
// Description : Gets bytes from the SPI interface.
//
// IN       :   p_pwRFPicoReadByteCounter = Pointer to number of complete bytes received including CRC16
//          :   p_pbRFPicoReadBitCounter = Pointer to number of valid bits in the last received byte
//          :   p_pbPicoReadStatus = Pointer to PicoRead status after frame received
//			:   p_pabBuffer = Pointer to Buffer in which data will be stored
//			:   p_pbRFReceiveEOF = Pointer to value, that define (for ISO B) that EOF need to be taken into account or not. 
//					For ISOB 		: p_pbRFReceiveEOF=1 
//					Other Protocols : p_pbRFReceiveEOF=0
//
// OUT		:	*p_pwRFPicoReadByteCounter = Number of Bytes Received
//				*p_pbRFPicoReadBitCounter  = Number of Bits	Received after the array of byte
//				*p_pbPicoReadStatus = Pointer to the Status byte received by the PicoRead chip after having received the data bytes from the RF
//
// RETURN   :   Error code
//
// Notes    :   - none -
//			:	- none -
//-----------------------------------------------------------------------------
unsigned char b_fnSPI_GetBuffer(unsigned char* p_pabBuffer,unsigned char* p_pbPicoReadStatus,unsigned short* p_pwRFPicoReadByteCounter,unsigned char* p_pbRFPicoReadBitCounter, unsigned char* p_pbRFReceiveEOF)
{
	unsigned char l_bStatus = 0;
    unsigned char l_bTemp;
    unsigned char l_bExpectedDataLength = 255;   // Limit buffer to 256 bytes for a command
	unsigned char l_bError;
	signed char   l_bBitNum = 0;
	unsigned short l_bLoopNum = 0;  //Debug TA202  was char instead of short
	unsigned long l_btimervalue=0;
	unsigned char Timer0_IR_Debug=0;
	
	 #if ((defined PAYPASS) || (defined FULL))
	unsigned short g_iFDT_A_PICC_Shift=0;
	unsigned long l_lTimeOut;
	
	if(g_bPayPassTimeoutValue == (_FDT_A_PICC_MIN + _PICOREAD_TIMEOUT_OFFSET_A) )
	{
		// FP	
		l_lTimeOut=_FDT_A_PICCMIN_TC + g_iFDT_A_PICC_Shift + _PICOREAD_TIMEOUT_OFFS_A_TC;
		
		//FP  ADD A SHIFT TO THE FDTAPICC ACCORDING TO THE LAST TRANSMITTED BIT VALUE
		if (g_bLastBitSentValue==1) {g_iFDT_A_PICC_Shift=0x155;}	 // 84/fc
		else {g_iFDT_A_PICC_Shift=0x51;}	// 20/fc
		T0TC-= (g_iFDT_A_PICC_Shift+(4*ONE_U_SECOND));			// delay Timer of the shift
	}
	
															// In fact it adds the shift due to the last bit value emitted
															// And it adds 2 祍 more for the right limit window
	#endif
	*p_pbPicoReadStatus = 0x00;

	*p_pbRFPicoReadBitCounter = 0x00;
    *p_pwRFPicoReadByteCounter = 0x0000;

    //RESET ERROR
    l_bError = ERR_NO_ERROR;


WAIT_ANSWER_FROM_PICOREAD:

	Timer0_IR_Debug=bit_testTimer0MR0IF();
	 #if ((defined PAYPASS) || (defined FULL))
	l_btimervalue = T0TC;
	

	#endif	


	if(!(testSDI_PIN()))		
	{
		//	clearDEBUG_PIN();
		 #if ((defined PAYPASS) || (defined FULL))

		// TA301.02, 302.04, 304.02, 305.03	
		// FOR WUPA/REQ,ANTICOLLISION & SELECT COMMANDS
		// TIME < MAX VALUE
		//  THE WINDOWS IS 	(_FDT_A_PICCMIN_TC + g_iFDT_A_PICC_Shift + _PICOREAD_TIMEOUT_OFFS_A_TC) -8US<  < +4US
		// The windows is either [approx  <<86,43祍<<  ] or [approx  <<91,16祍<<   ]
		if(   (  ( l_btimervalue <= (l_lTimeOut-(6*ONE_U_SECOND)) )   || ( l_btimervalue >= (l_lTimeOut+(4*ONE_U_SECOND))  )   )     && (g_bPayPassTimeoutValue == (_FDT_A_PICC_MIN + _PICOREAD_TIMEOUT_OFFSET_A) )) 		// IF RESPONSE FROM PICC IS TOO FAST OR TOO LATE--> ERROR		FOR REQA WAKEUPA			
		{			
	 		bit_clearTimer0MR0IF(); 
	
			// GET_END_ERROR:
			l_bError = ERR_COLL;
				
			goto GET_END;
		}

	
	
		#endif

		
		goto RECEPTION;	
	}


/******************* DEBUG ***********************/
//	  if ((( l_btimervalue <= (l_lTimeOut-(8*ONE_U_SECOND))) || ( l_btimervalue >= (l_lTimeOut+(4*ONE_U_SECOND)))))
//	   {
//
//	   clearDEBUG_PIN();
//
//
//	   } else { setDEBUG_PIN();}

/******************* DEBUG ***********************/







	if(!(Timer0_IR_Debug))			
	{
		goto WAIT_ANSWER_FROM_PICOREAD;
	}

	bit_clearTimer0MR0IF();
	  
	l_bError = ERR_NOCARD;

	goto GET_END;
	
RECEPTION:
 
    //RECEIVE FRAME, TIME OUT IS NO MORE CHECKED BECAUSE PICOREAD
    do
    {
		while(testSDI_PIN());
        // Read event which is always 0
        setSCK_PIN();

⌨️ 快捷键说明

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