How to read a card on pn512?

cancel
Showing results for 
Search instead for 
Did you mean: 

How to read a card on pn512?

115 Views
Contributor I

Hello i am developing i2c for pn512
u-com is using nrf51822
But I can't read the card. Please look at the source code

캡처5555.PNG

But I can't read the card. Please look at the source code.

What is the source code for reading cards?

 main(){

 mpu6050_register_write(CommandReg,PCD_RESETPHASE);  
    nrf_delay_ms(1);  
      
    mpu6050_register_write(ModeReg,0x3D);            //??Mifare??????CRC??'?0x6363  
    mpu6050_register_write(TReloadRegL,30);             
    mpu6050_register_write(TReloadRegH,0);  
    mpu6050_register_write(TModeReg,0x8D);  
    mpu6050_register_write(TPrescalerReg,0x3E);  
      
    mpu6050_register_write(TxAutoReg,0x40);//?????  
   PcdAntennaOff();    
    PcdAntennaOn();
  
   ClearBitMask(Status2Reg,0x08);  
       mpu6050_register_write(ModeReg,0x3D);//3F  0x3D
       mpu6050_register_write(RxSelReg,0x86);//84   0x86
       mpu6050_register_write(RFCfgReg,0x7F);   //4F   0x7F
       mpu6050_register_write(TReloadRegL,30);  
       mpu6050_register_write(TReloadRegH,0);  
       mpu6050_register_write(TModeReg,0x8D);  
       mpu6050_register_write(TPrescalerReg,0x3E);  
       nrf_delay_ms(1);  
       PcdAntennaOn();  
  

 for (;;)
    {
   
 status = PcdRequest(PICC_REQALL, g_ucTempbuf);  //status = error
 
         status = PcdAnticoll(g_ucTempbuf);  //status = error
        
       status = PcdSelect(g_ucTempbuf);  //status = error
              
         status = PcdAuthState(PICC_AUTHENT1A, 1, DefaultKey, g_ucTempbuf);  //status = OK
          
         status = PcdRead(1, g_ucTempbuf); //status = error

        PcdHalt();   //status = error
}
}
char PcdComMF522_5(unsigned char Command,
                 unsigned char *pInData,
                 unsigned char InLenByte,
                 unsigned char *pOutData,
                 unsigned int  *pOutLenBit)
{
 uint8_t data[20]={0x00,};
    char status = MI_OK;
    unsigned char irqEn   = 0x00;
    unsigned char waitFor = 0x00;
    unsigned char lastBits;
    unsigned char n;
    unsigned int i;
    switch (Command)
    {
       case PCD_AUTHENT:
          irqEn   = 0x12;
          waitFor = 0x10;
          break;
       case PCD_TRANSCEIVE:
          irqEn   = 0x77;
          waitFor = 0x30;
          break;
       default:
         break;
    }
 
  mpu6050_register_write(ComIEnReg,irqEn|0x80);
    ClearBitMask(ComIrqReg,0x80);
  mpu6050_register_write(CommandReg,PCD_IDLE);
    SetBitMask(FIFOLevelReg,0x80);
    for (i=0; i<InLenByte; i++)
    {  mpu6050_register_write(FIFODataReg, pInData[i]);  
         }
  
   mpu6050_register_write(CommandReg, Command);
    
    if (Command == PCD_TRANSCEIVE)
    {    SetBitMask(BitFramingReg,0x80);  }
   
    i = 100;//????????,??M1???????25ms
    do
    {
   
        // n = ReadRawRC(ComIrqReg);
     mpu6050_register_read(ComIrqReg,data,1);
       n = data[0];
         i--;
    }
    while ((i!=0) && !(n&0x01) && !(n&waitFor));
    ClearBitMask(BitFramingReg,0x80);
      
    if (i!=0)
    {   
   mpu6050_register_read(ErrorReg,data,1);
   
         if(!(data[0]&0x1B))
         {
             status = MI_OK;
       
             if (n & irqEn & 0x01)
             {   status = MI_NOTAGERR; 
              }
             if (Command == PCD_TRANSCEIVE)
             {
                mpu6050_register_read(FIFOLevelReg,data,1);
         n = data[0];
       
        mpu6050_register_read(ControlReg,data,1);
        lastBits = data[0];
               lastBits = (lastBits & 0x07);
                if (lastBits)
                {   *pOutLenBit = (n-1)*8 + lastBits;   }
                else
                {   *pOutLenBit = n*8;   }
                if (n == 0)
                {   n = 1;    }
                if (n > MAXRLEN)
                {   n = MAXRLEN;   }
        
                for (i=0; i<n; i++)
                {  
          mpu6050_register_read(FIFODataReg,data,n);
          pOutData[i] = data[i];  
          app_uart_put(pOutData[i]);
            }
            }
         }
         else
         {   status = MI_ERR; 
     
      }
       
   }
   
   SetBitMask(ControlReg,0x80);           // stop timer now
   mpu6050_register_write(CommandReg,PCD_IDLE);
   return status;
}

char PcdAuthState(unsigned char auth_mode,unsigned char addr,unsigned char *pKey,unsigned char *pSnr)
{
 uint8_t data[2]={0x00,};
    char status;
    unsigned int  unLen;
    unsigned char i,ucComMF522Buf[MAXRLEN];
    ucComMF522Buf[0] = auth_mode;
    ucComMF522Buf[1] = addr;
    for (i=0; i<6; i++)
    {    ucComMF522Buf[i+2] = *(pKey+i);   }
    for (i=0; i<6; i++)
    {    ucComMF522Buf[i+8] = *(pSnr+i);   }
 //   memcpy(&ucComMF522Buf[2], pKey, 6);
 //   memcpy(&ucComMF522Buf[8], pSnr, 4);
   
    status = PcdComMF522_5(PCD_AUTHENT,ucComMF522Buf,12,ucComMF522Buf,&unLen);
  mpu6050_register_read(Status2Reg,data,1);
    if ((status != MI_OK) || (!(data[0]) & 0x08))
    {   status = MI_ERR;   }
   
    return status;
}
void CalulateCRC(unsigned char *pIndata,unsigned char len,unsigned char *pOutData)
{
    unsigned char i,n;
 unsigned char data[2] = {0x00,};
    ClearBitMask(DivIrqReg,0x04);
    mpu6050_register_write(CommandReg,PCD_IDLE);
    SetBitMask(FIFOLevelReg,0x80);
    for (i=0; i<len; i++)
    {   mpu6050_register_write(FIFODataReg, *(pIndata+i));   }
    mpu6050_register_write(CommandReg, PCD_CALCCRC);
    i = 0xFF;
    do
    {
   mpu6050_register_read(DivIrqReg,data,1);
        n = data[0];
        i--;
    }
    while ((i!=0) && !(n&0x04));
  mpu6050_register_read(CRCResultRegL,data,1);
    pOutData[0] = data[0];
  mpu6050_register_read(CRCResultRegM,data,1);
    pOutData[1] = data[0];
}
char PcdRead(unsigned char addr,unsigned char *pData)
{
    char status;
    unsigned int  unLen;
    unsigned char i,ucComMF522Buf[MAXRLEN];
    ucComMF522Buf[0] = PICC_READ;
    ucComMF522Buf[1] = addr;
    CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]);
  
    status = PcdComMF522_5(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen);
  
 if (status == MI_OK) //&& (unLen == 0x90))
 //   {   memcpy(pData, ucComMF522Buf, 16);   }
    {
        for (i=0; i<16; i++)
        {    *(pData+i) = ucComMF522Buf[i];  
    app_uart_put(ucComMF522Buf[i]);
     }
    
      
    }
    else
    {   status = MI_ERR;   }
   
    return status;
}
char PcdSelect(unsigned char *pSnr)
{
    char status;
    unsigned char i;
    unsigned int  unLen;
    unsigned char ucComMF522Buf[MAXRLEN];
   
    ucComMF522Buf[0] = PICC_ANTICOLL1;
    ucComMF522Buf[1] = 0x70;
    ucComMF522Buf[6] = 0;
    for (i=0; i<4; i++)
    {
     ucComMF522Buf[i+2] = *(pSnr+i);
     ucComMF522Buf[6]  ^= *(pSnr+i);
    }
    CalulateCRC(ucComMF522Buf,7,&ucComMF522Buf[7]);
 
    ClearBitMask(Status2Reg,0x08);
    status = PcdComMF522_5(PCD_TRANSCEIVE,ucComMF522Buf,9,ucComMF522Buf,&unLen);
   
    if (status == MI_OK)// && (unLen == 0x18))
    {   status = MI_OK;  }
    else
    {   status = MI_ERR;    }
    return status;
}
char PcdAnticoll(unsigned char *pSnr)
{
    char status;
    unsigned char i,snr_check=0;
    unsigned int  unLen;
    unsigned char ucComMF522Buf[MAXRLEN]; 
   
    ClearBitMask(Status2Reg,0x08);
    mpu6050_register_write(BitFramingReg,0x00);
    ClearBitMask(CollReg,0x80);
 
    ucComMF522Buf[0] = PICC_ANTICOLL1;
    ucComMF522Buf[1] = 0x20;
    status = PcdComMF522_5(PCD_TRANSCEIVE,ucComMF522Buf,2,ucComMF522Buf,&unLen);
  
    if (status == MI_OK)
    {
      for (i=0; i<4; i++)
         {  
             *(pSnr+i)  = ucComMF522Buf[i];
             snr_check ^= ucComMF522Buf[i];
     
         }
         if (snr_check != ucComMF522Buf[i])
         {   //status = MI_ERR;   
     
      }
    }
   
    SetBitMask(CollReg,0x80);
    return status;
}
char PcdRequest(unsigned char req_code,unsigned char *pTagType)
{
   char status; 
   unsigned int  unLen;
   unsigned char ucComMF522Buf[MAXRLEN];
   ClearBitMask(Status2Reg,0x08);
   mpu6050_register_write(BitFramingReg,0x07);//WriteRawRC(BitFramingReg,0x07);
   SetBitMask(TxControlReg,0x03);
 
   ucComMF522Buf[0] = req_code;
   status = PcdComMF522_5(PCD_TRANSCEIVE,ucComMF522Buf,1,ucComMF522Buf,&unLen);
 
 
   if (status == MI_OK) //&& (unLen == 0x10))
   {   
       *pTagType     = ucComMF522Buf[0];
       *(pTagType+1) = ucComMF522Buf[1];
   }
   else
   {   status = MI_ERR;   }
  
   return status;
}
char PcdHalt(void)
{
    char status;
    unsigned int  unLen;
    unsigned char ucComMF522Buf[MAXRLEN];
    ucComMF522Buf[0] = PICC_HALT;
    ucComMF522Buf[1] = 0;
    CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]);
 
    status = PcdComMF522_5(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen);
  
    return MI_OK;
}
What process is required when a card is touched?
I would like a quick confirmation. thank you
0 Kudos
1 Reply

19 Views
NXP TechSupport
NXP TechSupport

You can download the App. Notes from this web page

PN512 | standard NFC frontend | NXP 

For example.

  • AN11802

It talk about the library and the examples source code. You can refer to the example NfcrdlibEx1_BasicDiscoveryLoop.

0 Kudos