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

📄 prd_iso14443b_anticollision.c

📁 读RF卡的源代码
💻 C
字号:
//=============================================================================
//                  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 RF ISO15693-3 READER
// Project Name : APPLICATION NOTE
// Module Name  : PRD_ISO14443B_ANTICOLLISION.H
// Platform dev : Keil 礦ision 3 (IDE ) + Keil ARM Compiler
// Target       : GENERIC
// Language     : C ANSI
// Revision     : 1.1
// Description  : 
//=============================================================================

// Defines standard
#include <string.h>      // Standard C Libray file containing basic functions to use serial host

// 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_Config.h"   	// Reader Configuration functions
#include "PRD_RF_Comm.h" 
#include "PRD_BufferExchange.h"


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

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

#include "PRD_COMMON_Anticollision_Parameters.h" 		//Common Anticollision parameters defines
#include "PRD_ISO14443B_Anticollision_Parameters.h"  	//Anticollision parameters defines for ISO14443B
#include "PRD_ISO14443B_Anticollision_Default_Parameters.h" //Default Anticollsion parameters for ISO14443B
#include "PRD_ISO14443B_Anticollision.h"     // Anticollision functions for ISO14443B


//-----------------------------------------------------------------------------
// Function name : unsigned char b_fn14443B_Anticol(unsigned char* p_abATQBResponse, unsigned char* p_bATQBLength,StructReader* p_pStructReader)

//-----------------------------------------------------------------------------
// Description : Extract one card from x card and return ATQB response
//               of the selected card.
//
// IN       :   p_pStructReader : Pointer to the used StructReader structure.
// OUT      :   p_abATQBResponse : ATQB response in ISOB
//              p_bATQBLength    : ATQB response length
//
// RETURN   :   true       1 if a card is selected.
//              false      0 no card selected.
//
// Notes    :   - none -
//-----------------------------------------------------------------------------

unsigned char b_fnISO14443B_Anticol(unsigned char* p_abATQBResponse, unsigned short* p_bATQBLength,StructReader* p_pStructReader)
{

	unsigned char l_bPicoReadStatus;
	unsigned char l_bRFReceiveEOF=1;
	unsigned char l_abBuffer[255];
    unsigned char l_bStatus;
    unsigned char l_bDelay;
    unsigned char l_bRetry;
    unsigned short l_bDataBytesReceived;
    unsigned char l_bDataBitsReceived;
	unsigned short l_wRFPicoReadByteCounter;
	unsigned char l_bRFPicoReadBitCounter;
	unsigned char l_bBitsToReceive;
  	unsigned char l_bSlotMarker;
 	unsigned char l_bSlotNumber;
 
   l_bBitsToReceive=0;
   l_bRFPicoReadBitCounter=0;
   l_wRFPicoReadByteCounter=0;

  
   
   memset(&l_abBuffer[0],0,20);
  
  l_bRetry=(p_pStructReader->s_bRAnticolOpt1 & 0x03);

lbl_Loop:

  // DEFAULT VALUES
  v_fnConfigBitRate((P0_S106K | P0_R106K),&l_bRFReceiveEOF,p_pStructReader);
  p_pStructReader->s_bRFrameSizeUsed=p_pStructReader->s_bRFrameSizeMax & 0xFE;
  p_pStructReader->s_bRTimeoutUsed=TO_4MS83;
  p_pStructReader->s_bCommunicationSupportedSpeed=P0_S106K | P0_R106K;

  // NUMBER OF SLOT MARKERS TO SEND
  l_bSlotNumber=1;
  l_bSlotMarker=p_pStructReader->s_bRAnticolOpt0 & 0x07;
  while(l_bSlotMarker--)
  {
      l_bSlotNumber*=2;
  }
  l_bSlotNumber-=1;
  l_bSlotMarker=l_bSlotNumber;

  // DELAY 5MS
  for(l_bDelay=25;l_bDelay>0;l_bDelay--) delay_us(200);

  // CONFIGURE DATA TO SEND
  l_abBuffer[0] = _ISOB_REQB;
  l_abBuffer[1] = p_pStructReader->s_bRAnticolOpt2; // AFI
  l_abBuffer[2] = (p_pStructReader->s_bRAnticolOpt0 & 0x0F);  // REQB or WUPB with slot markers number

  // SEND REQB OR WUPB CMD TO THE CHIP
  v_fnSendBuffer(_SENDCRC,3,0,&l_abBuffer[0],p_pStructReader);
  l_bStatus=b_fnReceiveBuffer(_CHECKCRC,&p_abATQBResponse[0],p_pStructReader,p_bATQBLength,&l_bDataBitsReceived,&l_bPicoReadStatus,&l_bRFReceiveEOF,&l_bBitsToReceive);
 		
 
  if(l_bStatus != ERR_NO_ERROR)
  {
      while (l_bSlotMarker--)
      {
         // INC THE MARQUER SLOT NUMBER
         // 15...25...35...45...55...65...75...85...95...A5...B5...C5...D5...E5...F5
         l_abBuffer[0] = (((l_bSlotNumber-l_bSlotMarker)<<4)|_ISOB_REQB);
         v_fnSendBuffer(_SENDCRC,1,0,&l_abBuffer[0],p_pStructReader);
		 l_bStatus=b_fnReceiveBuffer(_CHECKCRC,&p_abATQBResponse[0],p_pStructReader,&l_bDataBytesReceived,&l_bDataBitsReceived,&l_bPicoReadStatus,&l_bRFReceiveEOF,&l_bBitsToReceive);
         if(l_bStatus == ERR_NO_ERROR) goto lbl_ATQBResponse;
      }
      goto lbl_Retry;
  }

lbl_ATQBResponse:
  // CONFIGURE BIT RATE ACCORDING TO ATQB RESPONSE AND READER CONFIG
  if(p_pStructReader->s_bReaderSupportedSpeed & P0_USE_CHIP_BR)
  {
      p_pStructReader->s_bCommunicationSupportedSpeed=(p_abATQBResponse[9] & p_pStructReader->s_bReaderSupportedSpeed);
      if (bit_testChar(&p_abATQBResponse[9],7)) // Same speed during emission and reception
      {
          p_pStructReader->s_bCommunicationSupportedSpeed &= ((p_pStructReader->s_bCommunicationSupportedSpeed & 0xF0) >> 0x04);
          p_pStructReader->s_bCommunicationSupportedSpeed |= ((p_pStructReader->s_bCommunicationSupportedSpeed & 0x0F) << 0x04);
      }
  }

  // CONFIGURE FRAME SIZE ACCORDING TO ATQB RESPONSE AND READER CONFIG
  if(p_pStructReader->s_bRFrameSizeMax & P1_USE_CHIP_FS)
  {
      if(p_abATQBResponse[10] > p_pStructReader->s_bRFrameSizeMax) p_pStructReader->s_bRFrameSizeUsed = p_pStructReader->s_bRFrameSizeMax & 0xFE;
      else p_pStructReader->s_bRFrameSizeUsed = (p_abATQBResponse[10]&0xF0);
  }

  // CONFIGURE TIMEOUT ACCORDING TO ATQB RESPONSE AND READER CONFIG
  if(p_pStructReader->s_bRTimeoutDefault & USE_CHIP_TO) p_pStructReader->s_bRTimeoutUsed = ((p_abATQBResponse[11]&0xF0)>>4);
  else p_pStructReader->s_bRTimeoutUsed = p_pStructReader->s_bRTimeoutDefault & 0xEF;


  if(p_pStructReader->s_bRAnticolOpt0 & ISOB0_ATTRIB) goto lbl_Attrib;
  if(p_pStructReader->s_bRAnticolOpt0 & ISOB0_HALT) goto lbl_Halt;
  return TRUE;

lbl_Attrib:
  // CONFIGURE DATA TO SEND
  l_abBuffer[0] = _ISOB_ATTRIB ;
  memcpy(&l_abBuffer[1],&p_abATQBResponse[1],4);  
  l_abBuffer[9] = p_abATQBResponse[5] ;
  l_abBuffer[10] = p_abATQBResponse[6] ;
  l_abBuffer[11] = p_abATQBResponse[7] ;
  l_abBuffer[12] = p_abATQBResponse[8] ;  
  l_abBuffer[5] = 0 ;
  l_abBuffer[6] = p_pStructReader->s_bRFrameSizeUsed>>4;

  if(p_pStructReader->s_bReaderSupportedSpeed & P0_USE_CHIP_BR)
  {
      if (bit_testChar(&p_pStructReader->s_bCommunicationSupportedSpeed,2)) l_abBuffer[6]|=0x30; // Emission 847 kbps
      else if (bit_testChar(&p_pStructReader->s_bCommunicationSupportedSpeed,1)) l_abBuffer[6]|=0x20; // Emission 423 kbps
      else if (bit_testChar(&p_pStructReader->s_bCommunicationSupportedSpeed,0)) l_abBuffer[6]|=0x10; // Emission 212 kbps

      if (bit_testChar(&p_pStructReader->s_bCommunicationSupportedSpeed,6)) l_abBuffer[6]|=0xC0; // Reception 847 kbps
      else if (bit_testChar(&p_pStructReader->s_bCommunicationSupportedSpeed,5)) l_abBuffer[6]|=0x80; // Reception 423 kbps
      else if (bit_testChar(&p_pStructReader->s_bCommunicationSupportedSpeed,4)) l_abBuffer[6]|=0x40; // Reception 212 kbps
  }
  l_abBuffer[7] = p_abATQBResponse[10] & 0x01;
  l_abBuffer[8] = 0 ; 

  // SEND ATTRIB CMD TO THE CHIP
  v_fnSendBuffer(_SENDCRC,13,0,&l_abBuffer[0],p_pStructReader);
  l_bStatus=b_fnReceiveBuffer(_CHECKCRC,&l_abBuffer[0],p_pStructReader,&l_bDataBytesReceived,&l_bDataBitsReceived,&l_bPicoReadStatus,&l_bRFReceiveEOF,&l_bBitsToReceive);
  if(l_bStatus != ERR_NO_ERROR) goto lbl_Retry;

  // CHANGE READER SPEED IF NECESSARY
  if ((p_pStructReader->s_bCommunicationSupportedSpeed & 0x77) != (P0_S106K | P0_R106K)) v_fnConfigBitRate(p_pStructReader->s_bCommunicationSupportedSpeed,&l_bRFReceiveEOF,p_pStructReader);

lbl_Halt:
  if(p_pStructReader->s_bRAnticolOpt0 & ISOB0_HALT)
  {
      // CONFIGURE DATA TO SEND
      l_abBuffer[0] = _ISOB_HALT;

      // COPY SN TO OUTPUT BUFFER
      memcpy(&l_abBuffer[1],&p_abATQBResponse[1],4);

      // SEND HALT CMD TO THE CHIP
      v_fnSendBuffer(_SENDCRC,5,0,&l_abBuffer[0],p_pStructReader);
	  l_bStatus=b_fnReceiveBuffer(_CHECKCRC,&l_abBuffer[0],p_pStructReader,&l_bDataBytesReceived,&l_bDataBitsReceived,&l_bPicoReadStatus,&l_bRFReceiveEOF,&l_bBitsToReceive);
      if(l_bStatus != ERR_NO_ERROR) goto lbl_Retry;
  }
  return TRUE;

lbl_Retry:
  if(l_bRetry--)
  {
      if(p_pStructReader->s_bRAnticolOpt1&ISOB1_RESET)
      {
         v_fnCS_reset();
         v_fnSPI_SendByte(P0_REG_NO_RST | P0_PAGE_SELECT_1);
         // DELAY 2MS
         for(l_bDelay=10;l_bDelay>0;l_bDelay--) delay_us(200);
         v_fnCS_reset();
         v_fnSPI_SendByte(P0_REG_NO_RST | P0_PAGE_SELECT_1 | P0_RFON);
      }
    goto lbl_Loop;
  }
  return FALSE;
}

⌨️ 快捷键说明

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