pcdshare.c

来自「此程序为13.56MHz的RFID底层读写卡程序」· C语言 代码 · 共 259 行

C
259
字号
///////////////////////////////////////////////////////////////////////////////
//    Copyright (c), Philips Semiconductors Gratkorn
//
//                  (C)PHILIPS Electronics N.V.2000
//                     All rights are reserved.
//  Philips reserves the right to make changes without notice at any time.
// Philips makes no warranty, expressed, implied or statutory, including but
// not limited to any implied warranty of merchantibility or fitness for any
//particular purpose, or that the use will not infringe any third party patent,
// copyright or trademark. Philips must not be liable for any loss or damage
//                          arising from its use.
///////////////////////////////////////////////////////////////////////////////
#include <string.h>
#include <stdio.h>

//#include "PcdShared.h"
#include "RcComm.h"
#include "PcdUtils.h"
#include "MfErrNo.h"
#include "MfReg.h"
#include "Hardware.h"

#define READER_RESET       RSTPD=1
#define READER_CLEAR_RESET RSTPD=0

extern MfCmdInfo MInfo;

char ExchangeByteStream(unsigned char Cmd,
                        unsigned char *send_data,
                        unsigned char send_bytelen,
                        unsigned char *rec_data,
                        unsigned char *rec_bytelen);

//char PcdGetSnr(unsigned char * snr);

char PcdReadE2(unsigned short startaddr,
               unsigned char length,
               unsigned char * DataBuffer);

char PcdWriteE2(  unsigned short startaddr,
                      unsigned char length,
                      unsigned char * DataBuffer);

char PcdReset(void);
char PcdRfReset(unsigned short ms);

///////////////////////////////////////////////////////////////////////
//      M I F A R E   M O D U L E   R E S E T
///////////////////////////////////////////////////////////////////////
char PcdReset(void)
{
   char    status = MI_OK;
   unsigned long   TimeOutCount;

   SleepMs(100);     	// wait
   READER_RESET;   		// reset RC500
   SleepMs(100);      	// wait
   READER_CLEAR_RESET; // clear reset pin
   //SleepMs(100);
///////////////////////////////////////////////////////////////////////////////
// Delete by Allen 9.14
///////////////////////////////////////////////////////////////////////////////
//   GT_vInitTmr(TIMER_3,0x87); // stop timer, count down with a period of 52 us
//   T3IC  = 0x00;               // reset interrupt request
//   GT_vLoadTmr(TIMER_3,0xAFFF);   // 0x9FFF * 52 us = 2.1 s
//   GT_vStartTmr(TIMER_3);   // start timer
//
//   // wait until reset command recognized
//   while (((ReadRawRC(RegCommand) & 0x3F) != 0x3F) && !T3IR);
//
//   // while reset sequence in progress
//   while ((ReadRawRC(RegCommand) & 0x3F) && !T3IR);
//
//   GT_vStopTmr(TIMER_3);  // stop timeout counter
//
//   if (T3IR)
///////////////////////////////////////////////////////////////////////////////
   //TimeOutCount=20250;

   TimeOutCount=20250;//40250;

 //  Temp=ReadRawRC(RegCommand);
   //Temp=WriteRawRC(
 //  Temp=ReadRawRC(RegCommand);
   // wait until reset command recognized   2.1s
   while (((ReadRawRC(RegCommand) & 0x3F) != 0x3F) && TimeOutCount)
   {
      TimeOutCount--;
   }

   // while reset sequence in progress
   while ((ReadRawRC(RegCommand) & 0x3F) && TimeOutCount)
   {
      TimeOutCount--;
   }


   if(!TimeOutCount)
   {
      status = MI_RESETERR; // respose of reader IC is not correct
      //T3IR   = 0;
   }

   if (status == MI_OK)
   {
      WriteRawRC(RegPage,0x80); // Dummy access in order to determine the bus
                                // configuration
      // necessary read access
      // after first write access, the returned value
      // should be zero ==> interface recognized
      if (ReadRawRC(RegCommand) != 0x00)
      {
          status = MI_INTERFACEERR;
      }
      else
      {
          // sequence is ok
      }
   }

   return status;
}

//////////////////////////////////////////////////////////////////////
//           E X C H A N G E   B Y T E   S T R E A M
///////////////////////////////////////////////////////////////////////
char ExchangeByteStream(unsigned char Cmd,
                        unsigned char *send_data,
                        unsigned char send_bytelen,
                        unsigned char *rec_data,
                        unsigned char *rec_bytelen)
{
   signed char status = MI_OK;

   FlushFIFO();    // empty FIFO
   ResetInfo(MInfo); // initialise ISR Info structure

   if (send_bytelen > 0)
   {
      memcpy(MRcvBuffer,send_data,send_bytelen); // write n bytes
      MInfo.nBytesToSend = send_bytelen;
      // write load command
      status = PcdSingleResponseCmd(Cmd,
                      MRcvBuffer,
                      MRcvBuffer,
                      &MInfo);

      if ( status == MI_OK )
      {
         *rec_bytelen = MInfo.nBytesReceived;
         if (*rec_bytelen)
         {
            memcpy(rec_data,MRcvBuffer,MInfo.nBytesReceived);
         }
      }
   }
   else
   {
      status = MI_WRONG_PARAMETER_VALUE;
   }
   return status;
}

///////////////////////////////////////////////////////////////////////
//          R E A D   S N R   O F   R E A D E R   I C
///////////////////////////////////////////////////////////////////////
/*char PcdGetSnr(unsigned char * snr)
{
   signed char xdata status;

   status = PcdReadE2(0x08,0x04,snr);
   return status;
}
          */

///////////////////////////////////////////////////////////////////////
//          E E P R O M   R E A D
///////////////////////////////////////////////////////////////////////
char PcdReadE2(unsigned short startaddr,
               unsigned char length,
               unsigned char * DataBuffer)
{
     char status = MI_OK;

     // ************* Cmd Sequence **********************************
     ResetInfo(MInfo);
     MRcvBuffer[0] = startaddr & 0xFF;
     MRcvBuffer[1] = (startaddr >> 8) & 0xFF;
     MRcvBuffer[2] = length;
     MInfo.nBytesToSend   = 3;
     status = PcdSingleResponseCmd(PCD_READE2,
                         MRcvBuffer,
                         MRcvBuffer,
                         &MInfo);

    if (status == MI_OK)
    {
       memcpy(DataBuffer,MRcvBuffer,length);
    }
    else   // Response Processing
    {
       DataBuffer[0] = 0;
    }
    return status ;
}


///////////////////////////////////////////////////////////////////////
//          E E P R O M   W R I T E
///////////////////////////////////////////////////////////////////////
char PcdWriteE2(  unsigned short startaddr,
                      unsigned char length,
                      unsigned char * DataBuffer)/////DataBuffer
{
   char status = MI_OK;
   //unsigned char Send3;

     // ************* Cmd Sequence **********************************
   ResetInfo(MInfo);
   MRcvBuffer[0] = startaddr & 0xFF;
   MRcvBuffer[1] = (startaddr >> 8) & 0xFF;
///////////////////////////////
// delete in formal version
 //  Send3=DataBuffer[0];


   memcpy(MRcvBuffer + 2,DataBuffer,length);

   MInfo.nBytesToSend   = length + 2;

   status = PcdSingleResponseCmd(PCD_WRITEE2,
                   MRcvBuffer,
                   MRcvBuffer,
                   &MInfo); // write e2

   return status;
}

//////////////////////////////////////////////////////////////////////
//                 R E S E T
///////////////////////////////////////////////////////////////////////
char PcdRfReset(unsigned short ms)
{
   char  status = MI_OK;

   ClearBitMask(RegTxControl,0x03);  // Tx2RF-En, Tx1RF-En disablen
   if (ms > 0)
   {
      //SleepUs(((ms * 1000) - 40) / 2);                      // Delay for 1 ms
      SleepMs(ms);
      SetBitMask(RegTxControl,0x03);    // Tx2RF-En, Tx1RF-En enable
   }

   return status;
}



⌨️ 快捷键说明

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