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

📄 prd_rf_comm.c

📁 读RF卡的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
        clearSCK_PIN();

        // Read status
        setSCK_PIN();
        if(testSDI_PIN())   // Exit if status = 1
        {
            clearSCK_PIN();
            l_bStatus = 1;
            break;
        }
        clearSCK_PIN();

		*p_pabBuffer = 0x00;
		
		for(l_bBitNum=7;l_bBitNum>=0;l_bBitNum--)	   //bit cycling
		{
			setSCK_PIN();
			if(testSDI_PIN()) 
			{	
				bit_setChar(p_pabBuffer,l_bBitNum);
			}
			clearSCK_PIN();
		}

		p_pabBuffer++;

		l_bLoopNum++;		

    } 
	while (l_bExpectedDataLength--);

	*p_pwRFPicoReadByteCounter = l_bLoopNum;

    //CHECK IF STATUS BYTE
    if (l_bStatus == 1)
    {
        // Read it
        (*p_pbPicoReadStatus) = b_fnSPI_ReceiveByte() ;
        
        // Read number of bits
        (*p_pbRFPicoReadBitCounter) = (*p_pbPicoReadStatus & STATUS_NB_BITS) >> 4;

		if((*p_pbRFPicoReadBitCounter)!=0) 
		{
		 	(*p_pwRFPicoReadByteCounter)++;
		} 

        // Adjust g_wRFPicoReadByteCounter for ISOB reception
        if (   ((*p_pbRFReceiveEOF) == 1) && ((*p_pwRFPicoReadByteCounter) !=0)     ) 
		{	
			(*p_pwRFPicoReadByteCounter)--;
    	}
	}
    else // 256 bytes received without status byte
    {
        // Maximum number of bytes to be read (status byte + EOF only in ISOB) 
        l_bTemp = (*p_pbRFReceiveEOF) + 1; // p_pbRFReceiveEOF=1 if ISOB protocol used and 0 else
          
        while(l_bTemp>0)
        {
            l_bTemp--;
            while(testSDI_PIN());

            // Read event which is always 0
            setSCK_PIN();
            clearSCK_PIN();

            // Read status
            setSCK_PIN();
            if(testSDI_PIN()) // status = 1
            {
               clearSCK_PIN();
               
               // Read it
               (*p_pbPicoReadStatus) = b_fnSPI_ReceiveByte() ;
               
               // Read number of bits
               (*p_pbRFPicoReadBitCounter) = ((*p_pbPicoReadStatus) & STATUS_NB_BITS) >> 4;
               
               l_bError = ERR_NO_ERROR;

               // Adjust g_wRFPicoReadByteCounter
               if (l_bTemp != 0) 
			   {
			   		(*p_pwRFPicoReadByteCounter)--; // EOF to remove
               }
			   goto GET_END;
            }
            else // Error if Status byte was not received during p_pbRFReceiveEOF+1 loops
            {
               clearSCK_PIN();
               
               // Receive Byte
               b_fnSPI_ReceiveByte();
               l_bError = ERR_ISOB;
            }
        }
    }
#ifdef MODE_DEBUG_TCL
printf("                    Aswer Received at : %ld us\n",(unsigned short)((l_btimervalue/ONE_U_SECOND)-77));
#endif

				   
GET_END:

    return l_bError ;
}

//------------------------------------------------------------------------
// Function : unsigned char b_fnCheckCRCValid(unsigned char* p_pbPicoReadStatus,unsigned short* p_pwRFPicoReadByteCounter)
//------------------------------------------------------------------------
// Description : Check if the CRC is the good one
//               Adjust the datalength to return
//
// IN       :   p_pwRFPicoReadByteCounter = Pointer to number of complete bytes received including CRC16
//          :   p_pbPicoReadStatus = Pointer to the Status byte received by the PicoRead chip after having received the data bytes from the RF

// OUT      :   *p_pwRFPicoReadByteCounter = number of complete bytes received without CRC16
//
// RETURN   :   1 if CRC valid, 0 else
//
// Notes    :   - none -
// ------------------------------------------------------------------------

unsigned char b_fnCheckCRCValid(unsigned char p_lbPicoReadStatus,unsigned short* p_pwRFPicoReadByteCounter)
{
    if ((p_lbPicoReadStatus) & STATUS_ERR_CRC) // CRC is not valid
    {
        return 0;
    }
    else // CRC is valid
    {
        // Remove the 2 CRC byte from the buffer
        if (*p_pwRFPicoReadByteCounter > 2) 
		{
			(*p_pwRFPicoReadByteCounter) -= 2;
        }
		return 1;
    }
}


#ifdef LIB_RASR
//-----------------------------------------------------------------------------
// Function : void v_fnSendRFCommand(unsigned char p_bSendOption,unsigned char* p_pabBuffer, unsigned short* p_pwRFPicoReadByteCounter, unsigned char* p_pbRFPicoReadBitCounter)
//-----------------------------------------------------------------------------
// Description : Data to send are stored at g_abBuffer[0]
//
// IN       :   p_bSendOption : Sending options
//                                   b2..b0  : RFU
//                                   b3      : Send CRC
//                                   b5..b4  : PicoRead Protocol page
//                                   b7..b6  : RFU
//				p_pabBuffer = Pointer to Buffer in which command to send is stored
//              p_pwRFPicoReadByteCounter = Pointer to number of complete bytes to send
//              p_pbRFPicoReadBitCounter = Pointer to number of bits to send
//
// OUT      :   - none -
// RETURN   :   - none -
// Notes    :   - none -
//-----------------------------------------------------------------------------

void v_fnSendRFCommand(unsigned char p_bSendOption,unsigned char* p_pabBuffer, unsigned short* p_pwRFPicoReadByteCounter, unsigned char* p_pbRFPicoReadBitCounter)
{
    unsigned char l_bP5_R0=0;
    unsigned char l_bP5_EXT_REG;

    if (*p_pwRFPicoReadByteCounter != 255) // If 0 bytes, then it means the Chip doesn't want to send data back
    {
        // Prepare the sending configuration
        l_bP5_R0 = P5_ACCESS_P5 | P5_EMI_REC_EMISSION | (p_bSendOption & 0x38);
        l_bP5_EXT_REG = 0x00;

        if (*p_pbRFPicoReadBitCounter)
        {
            l_bP5_R0 |= P5_EXT_REG;
            l_bP5_EXT_REG = *p_pbRFPicoReadBitCounter << 4;
        }

        if ((*p_pwRFPicoReadByteCounter == 1) && (*p_pbRFPicoReadBitCounter == 0))
        {
            // Send only one byte
            l_bP5_R0 |= P5_EXT_REG;
            l_bP5_EXT_REG = P5_OOB | P5_NB_BIT_8BITS;
        }

        if (*p_pwRFPicoReadByteCounter == 0)
        {
            if (*p_pbRFPicoReadBitCounter != 0)
            {
                l_bP5_EXT_REG |= P5_OOB;
                *p_pbRFPicoReadBitCounter = 0;
                (*p_pwRFPicoReadByteCounter)++ ;
            }
            else
            {
                // Only send EOF
                l_bP5_R0 |= P5_EOF_ONLY;
            }
        }

        // Send the buffer
        v_fnCS_reset();

        v_fnSPI_SendByte(l_bP5_R0);
        if (l_bP5_R0 & 0x01) 
		{
			v_fnSPI_SendByte(l_bP5_EXT_REG);
        }
		v_fnSPI_SendBuffer(*p_pwRFPicoReadByteCounter,*p_pbRFPicoReadBitCounter,p_pabBuffer);
        v_fnSPI_WaitForEOT();



    }
}


//-----------------------------------------------------------------------------
// Function : unsigned char b_fnReceiveRFResponse(unsigned char p_bReceiveOption,unsigned char* p_pabBuffer,unsigned short* p_pwRFPicoReadByteCounter,unsigned char* p_pbRFPicoReadBitCounter,unsigned char* p_pbPicoReadStatus,unsigned char* p_pbRFReceiveEOF)
//-----------------------------------------------------------------------------
// Description : Received data are stored at g_abBuffer[0]
//
// IN       :   p_bReceiveOption : Receiving options
//                                      b3..b0  : RFU
//                                      b5..b4  : PicoRead Protocol page
//                                      b6      : RFU
//                                      b7      : Receive SOF only
//          :   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 received datas 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 -
//-----------------------------------------------------------------------------
 
unsigned char b_fnReceiveRFResponse(unsigned char p_bReceiveOption,unsigned char* p_pabBuffer,unsigned short* p_pwRFPicoReadByteCounter,unsigned char* p_pbRFPicoReadBitCounter,unsigned char* p_pbPicoReadStatus,unsigned char* p_pbRFReceiveEOF,unsigned char p_bBitsToReceive)
{
	unsigned char l_bReturnByte;
	v_fnCS_reset();
	
	if(!bit_testChar(&p_bReceiveOption,7))
	{
		if(p_bBitsToReceive & 0x07)
		{
			v_fnSPI_SendByte(P5_ACCESS_P5 | P5_EMI_REC_RECEPTION | (p_bReceiveOption & 0x30) | P5_CRC_EN | P5_EXT_REG);
			v_fnSPI_SendByte(p_bBitsToReceive << 0x04);
		}
		else 
		{
			v_fnSPI_SendByte(P5_ACCESS_P5 | P5_EMI_REC_RECEPTION | (p_bReceiveOption & 0x30) | P5_CRC_EN);
		}
	}
	else 
	{
		v_fnSPI_SendByte(P5_ACCESS_P5 | P5_EMI_REC_RECEPTION | P5_SOF_ONLY | (p_bReceiveOption & 0x30));
	}
	l_bReturnByte = b_fnSPI_GetBuffer(p_pabBuffer,p_pbPicoReadStatus,p_pwRFPicoReadByteCounter,p_pbRFPicoReadBitCounter,p_pbRFReceiveEOF);
	return l_bReturnByte;
}	
#endif  // LIB_RASR

#ifdef LIB_RASC
//-----------------------------------------------------------------------------
// Function : void v_fnReceiveRFCommand(unsigned char p_bReceiveOption,unsigned char* p_pabBuffer,unsigned short* p_pwRFPicoReadByteCounter,unsigned char* p_pbRFPicoReadBitCounter,unsigned char* p_pbPicoReadStatus,unsigned char* p_pbRFReceiveEOF)
//-----------------------------------------------------------------------------
// Description : Received data are stored at g_abBuffer[0]
//
// IN       :   p_bReceiveOption : Receiving options
//                                      b3..b0  : RFU
//                                      b5..b4  : PicoRead Protocol page
//                                      b6      : RFU
//                                      b7      : Receive EOF only
//          :   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 received datas 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 = PicoRead Status
//
// RETURN   :   - none -
//-----------------------------------------------------------------------------
void v_fnReceiveRFCommand(unsigned char p_bReceiveOption,unsigned char* p_pabBuffer,unsigned short* p_pwRFPicoReadByteCounter,unsigned char* p_pbRFPicoReadBitCounter,unsigned char* p_pbPicoReadStatus,unsigned char* p_pbRFReceiveEOF)
{
	v_fnCS_reset();
	
	if(!bit_testChar(&p_bReceiveOption,7))
	{
		v_fnSPI_SendByte(P5_ACCESS_P5 | P5_RECEIVE_MODE | (p_bReceiveOption & 0x30) | P5_CRC_EN);
	}
	else 
	{
		v_fnSPI_SendByte(P5_ACCESS_P5 | P5_RECEIVE_MODE | P5_EOF_ONLY | (p_bReceiveOption & 0x30));
	}
	// receive bytes until not errors
	b_fnSPI_GetBuffer(p_pabBuffer,p_pbPicoReadStatus,p_pwRFPicoReadByteCounter,p_pbRFPicoReadBitCounter,p_pbRFReceiveEOF);
}


//-----------------------------------------------------------------------------
// Function : v_fnSendRFResponse(unsigned char p_bSendOption,unsigned char* p_pabBuffer, unsigned short* p_pwRFPicoReadByteCounter, unsigned char* p_pbRFPicoReadBitCounter)
//-----------------------------------------------------------------------------
// Description : Data to send are stored at g_abBuffer[0]
//
// IN       :   p_bSendOption : Sending options
//                                   b2..b0  : RFU
//                                   b3      : Send CRC
//                                   b5..b4  : PicoRead Protocol page
//                                   b7..b6  : RFU
//				p_pabBuffer = Pointer to Buffer in which command to send is stored
//              p_pwRFPicoReadByteCounter = Pointer to number of complete bytes to send
//              p_pbRFPicoReadBitCounter = Pointer to number of bits to send
//
// OUT      :   if bit(s) to send, adjust *p_pwRFPicoReadByteCounter
// RETURN   :   - none -
// Notes    :   - none -
//-----------------------------------------------------------------------------
void v_fnSendRFResponse(unsigned char p_bSendOption,unsigned char* p_pabBuffer, unsigned short* p_pwRFPicoReadByteCounter, unsigned char* p_pbRFPicoReadBitCounter)
{
	unsigned char l_bP5_R0;
	unsigned char l_bP5_EXT_REG;

	if (*p_pwRFPicoReadByteCounter != 255) // If 0 bytes, then it mean the Chip doesn't want to send data back
	{
		// Prepare the sending configuration
		l_bP5_R0 = P5_ACCESS_P5 | P5_SEND_MODE | (p_bSendOption & 0x38);
		
		l_bP5_EXT_REG = 0x00;
		
		if (*p_pbRFPicoReadBitCounter)
		{
			l_bP5_R0 |= P5_EXT_REG;
			l_bP5_EXT_REG = *p_pbRFPicoReadBitCounter << 4;
		}
	
		if ((*p_pwRFPicoReadByteCounter == 1) && (*p_pbRFPicoReadBitCounter == 0))
		{
			// Send only one byte
			l_bP5_R0 |= P5_EXT_REG;
		l_bP5_EXT_REG = P5_OOB | P5_8_BITS;
			}
	
		if (*p_pwRFPicoReadByteCounter == 0)
		{
			if (*p_pbRFPicoReadBitCounter !=0)
			{
				l_bP5_EXT_REG |= P5_OOB;
			}
			else
			{
				// Only send SOF
				l_bP5_R0 |= P5_SOF_ONLY;
			}
		}

      // Adjust the data length : if bits to send then send a full byte
      if (*p_pbRFPicoReadBitCounter) 
	  {
		  (*p_pwRFPicoReadByteCounter)++ ;
	  }
      // Send the buffer
      v_fnCS_reset();
      v_fnSPI_SendByte(l_bP5_R0);
      if (l_bP5_R0 & 0x01) 
	  {
	  		v_fnSPI_SendByte(l_bP5_EXT_REG);
      }
	  v_fnSPI_SendBuffer(*p_pwRFPicoReadByteCounter,0,p_pabBuffer);
      v_fnSPI_WaitForEOT();
   }
}
#endif  // LIB_RASC

⌨️ 快捷键说明

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