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

📄 armspi.c

📁 用ARM的LPC2200来 SPI通信方式下读卡器驱动程序 其读卡器为FM172
💻 C
📖 第 1 页 / 共 4 页
字号:
         {
             MRcvBuffer[0] &= 0x0f;  // mask out upper nibble
             if ((MRcvBuffer[0] & 0x0a) == 0)
             {
                status = MI_NOTAUTHERR;
             }
             else
             {
                status = MI_CODEERR;
             }
          }
      }
      memcpy1(_data,_16ge0,16); // in case of an error initialise 
                                          // data
   }
   else   // Response Processing
   {
      if (MInfo.nBytesReceived != 16)
      {
         status = MI_BYTECOUNTERR;
         memcpy1(_data,_16ge0,16);
      }
      else
      {
         memcpy1(_data,(uint8*)MRcvBuffer,16);
      }
   }
   FM1702PcdSetTmo(1);               // short timeout   
   return status; 
}

///////////////////////////////////////////////////////////////////////
//          M I F A R E   W R I T E     
///////////////////////////////////////////////////////////////////////
int8 FM1702PiccWrite( uint8 block,uint8 *_data)
                  
{
   int8  rtcrc,status = MI_OK;

     // ************* Cmd Sequence ********************************** 
     ResetInfo(MInfo);   
     MSndBuffer[0] = PICC_WRITE;        // Write command code
     MSndBuffer[1] = block;
     MInfo.nBytesToSend   = 2;
     rtcrc=ReadIO(RegChannelRedundancy);
     WriteIO(RegChannelRedundancy,0x07);
     status = FM1702PcdCmd(PCD_TRANSCEIVE,MSndBuffer,MRcvBuffer, &MInfo);
   
     if (status != MI_NOTAGERR)   // no timeout error
     {
        if (MInfo.nBitsReceived != 4)   // 4 bits are necessary
        {
           status = MI_BITCOUNTERR;
        }
        else                     // 4 bit received
        {
           MRcvBuffer[0] &= 0x0f; // mask out upper nibble
           if ((MRcvBuffer[0] & 0x0a) == 0)
           {
              status = MI_NOTAUTHERR;                   
           }
           else
           {
              if (MRcvBuffer[0] == 0x0a)
              {
                 status = MI_OK;
              }
              else 
              {
                 status = MI_CODEERR;
              }
           }
        }
     }

     if ( status == MI_OK)
     {

        FM1702PcdSetTmo(3);     // long timeout 

        ResetInfo(MInfo);   
        memcpy1((uint8*)MSndBuffer,_data,16);
        MInfo.nBytesToSend   = 16;
        status = FM1702PcdCmd(PCD_TRANSCEIVE, MSndBuffer, MRcvBuffer, &MInfo);     
          WriteIO(RegChannelRedundancy,rtcrc);   
        if (status & 0x80)    // timeout occured
        {
           status = MI_NOTAGERR;
        }
        else
        {
         
           if (MInfo.nBitsReceived != 4)  // 4 bits are necessary
           {
              status = MI_BITCOUNTERR;
           }
           else                     // 4 bit received
           {
              MRcvBuffer[0] &= 0x0f; // mask out upper nibble
              if ((MRcvBuffer[0] & 0x0a) == 0)
              {
                 status = MI_WRITEERR;
              }
              else
              {
                 if (MRcvBuffer[0] == 0x0a)
                 {
                    status = MI_OK;
                 }
                 else 
                 {
                    status = MI_CODEERR;
                 }
              }     
           }
        }        
        FM1702PcdSetTmo(1);    // short timeout
        
     }
      WriteIO(RegChannelRedundancy, rtcrc); 
  return status;
}

///////////////////////////////////////////////////////////////////////
//                V A L U E   M A N I P U L A T I O N 
///////////////////////////////////////////////////////////////////////
int8 FM1702PiccValue(uint8 dd_mode,uint8 addr,uint8 *value, uint8 trans_addr)
{
   int8 rtcrc,status = MI_OK;

   FM1702PcdSetTmo(1);    // short timeout
   // ************* Cmd Sequence ********************************** 
   ResetInfo(MInfo);   
   MSndBuffer[0] = dd_mode;        // Inc,Dec command code
   MSndBuffer[1] = addr;
   MInfo.nBytesToSend   = 2;
   rtcrc=ReadIO(RegChannelRedundancy);
   WriteIO(RegChannelRedundancy,0x07); 
   status = FM1702PcdCmd(PCD_TRANSCEIVE,MSndBuffer,MRcvBuffer,&MInfo);
   if (status != MI_NOTAGERR)   // no timeout error
   {
        if (MInfo.nBitsReceived != 4)   // 4 bits are necessary
        {
           status = MI_BITCOUNTERR;
        }
        else                     // 4 bit received
        {
           MRcvBuffer[0] &= 0x0f; // mask out upper nibble
           switch(MRcvBuffer[0])
           {
              case 0x00: 
                 status = MI_NOTAUTHERR;
                 break;
              case 0x0a:
                 status = MI_OK;
                 break;
              case 0x01:
                 status = MI_VALERR;
                 break;
              default:
                 status = MI_CODEERR;
                 break;
           }
        }
     }

     if ( status == MI_OK)
     {
        FM1702PcdSetTmo(3);     // long timeout 

        ResetInfo(MInfo);   
        memcpy1((uint8*)MSndBuffer,value,4);
        MInfo.nBytesToSend   = 4;
        status = FM1702PcdCmd(PCD_TRANSCEIVE, MSndBuffer, MRcvBuffer,&MInfo);     
        if (status == MI_OK)    // no timeout occured
        {
            if (MInfo.nBitsReceived != 4)   // 4 bits are necessary
            {
               status = MI_BITCOUNTERR;
            }
            else                     // 4 bit received
            {
               MRcvBuffer[0] &= 0x0f; // mask out upper nibble
               switch(MRcvBuffer[0])
               {
                  case 0x00: 
                     status = MI_NOTAUTHERR;
                     break;
                  case 0x01:
                     status = MI_VALERR;
                     break;
                  default:
                     status = MI_CODEERR;
                     break;
               }
            }
         }        
         else
         if (status == MI_NOTAGERR )
            status = MI_OK;  // no response after 4 byte value - 
                             // transfer command has to follow
     }
     if ( status == MI_OK)
     {
        ResetInfo(MInfo);   
        MSndBuffer[0] = PICC_TRANSFER;        // transfer command code
        MSndBuffer[1] = trans_addr;
        MInfo.nBytesToSend   = 2;
        status = FM1702PcdCmd(PCD_TRANSCEIVE,MSndBuffer, MRcvBuffer, &MInfo);       
        if (status != MI_NOTAGERR)    // timeout occured
        {
            if (MInfo.nBitsReceived != 4)   // 4 bits are necessary
            {
               status = MI_BITCOUNTERR;
            }
            else                     // 4 bit received
            {
               MRcvBuffer[0] &= 0x0f; // mask out upper nibble
               switch(MRcvBuffer[0])
               {
                  case 0x00: 
                     status = MI_NOTAUTHERR;
                     break;
                  case 0x0a:
                     status = MI_OK;
                     break;
                  case 0x01:
                     status = MI_VALERR;
                     break;
                  default:
                     status = MI_CODEERR;
                     break;
               }
            }
        }        
     }
     WriteIO(RegChannelRedundancy,rtcrc);

   return status;
}
///////////////////////////////////////////////////////////////////////
//          M I F A R E     H A L T
///////////////////////////////////////////////////////////////////////
int8 FM1702PiccHalt(void)
{
   int8  status = MI_CODEERR;

   // ************* Cmd Sequence ********************************** 
   ResetInfo(MInfo);   
   MSndBuffer[0] = PICC_HALT ;     // Halt command code
   MSndBuffer[1] = 0x00;         // dummy address
   MInfo.nBytesToSend   = 2;
   status = FM1702PcdCmd(PCD_TRANSCEIVE, MSndBuffer, MRcvBuffer,&MInfo);                          
   if (status)
   {
     // timeout error ==> no NAK received ==> OK
     if (status == MI_NOTAGERR || status == MI_ACCESSTIMEOUT)
        status = MI_OK;
   }
   //reset command register - no response from tag
   WriteIO(RegCommand,PCD_IDLE);
   return status; 
}

//////////////////////////////////////////////////////////////////////
//                 M I F A R E    R E S E T 
///////////////////////////////////////////////////////////////////////
int8 FM1702PcdRfReset(uint32 ms)
{
   int8  status = MI_OK;

   if(ms)
   {
     ClearBitMask(RegTxControl,0x03);  // Tx2RF-En, Tx1RF-En disablen
     delay_50us(ms*20);                // Delay for 1 ms
     SetBitMask(RegTxControl,0x03);    // Tx2RF-En, Tx1RF-En enable
   }
   else
     ClearBitMask(RegTxControl,0x03);  // Tx2RF-En, Tx1RF-En disablen
     
   return status;
}

/*---------------------------------------------------------------------
//          C O N F I G   M F O U T   S E L E C T 

---------------------------------------------------------------------*/
int8 FM1702PcdMfOutSelect(uint8 type)
{
   WriteIO(RegMfOutSelect,type&0x07);
   return MI_OK;
}
     
/*----------------------------------------------------------------------------
函数:延时函数
-----------------------------------------------------------------------------*/
void delay_50us(uint32 _50us)
{
 uint32 i;
 for(i=0;i<5*_50us;i++);     
}
//----------------------------------------------------------------------------
// FUNCTION:     start_timeout                                                                                                                                                                                    
//----------------------------------------------------------------------------
void start_timeout(uint32 _50us)
{
 VICIntEnClr|=(1<<5);
 T1TCR=0;         
 T1TC=0;   
 T1MR0=DELAY50 * _50us; 
 T1MCR=5;  
 
  Timer1out_flag = 0;	
  T1TCR=1;         
 
 VICIntEnable|=(1<<5);
}
//----------------------------------------------------------------------------
// FUNCTION:     stop_timeout 
//----------------------------------------------------------------------------
void stop_timeout(void)
{
  VICIntEnClr|=(1<<5);//关Timer1中断
  //T1IR=T1IR|0X01;    //清中断
  T1TCR=0;           //关闭定时器1
}
///////////////////////////////////////////////////////////////////////////////
//                  Interrupt Handler TIMER1
///////////////////////////////////////////////////////////////////////////////
void TIMER1_Exception (void) 
{   
	Timer1out_flag = 1;	// Set timeout state
   	T1TCR=0;           //关闭定时器1
    T1IR=T1IR|0x01;    //清中断
    VICVectAddr=0;
}


⌨️ 快捷键说明

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