zizag 发表于 2012-6-4 11:17:33

咨询一个问题mfrc523的问题

本帖最后由 zizag 于 2012-6-4 11:19 编辑

我现在在调试mfrc523的驱动,
char PcdComMF522(unsigned char Command,
               unsigned char *pInData,
               unsigned char InLenByte,
               unsigned char *pOutData,
               unsigned int*pOutLenBit)
{
    char status = MI_ERR;
    unsigned char irqEn   = 0x00;
    unsigned char waitFor = 0x00;
    unsigned char lastBits;
    unsigned char n;
    unsigned int i;
    unsigned char getvalue = 0;
    switch (Command)
    {
       case PCD_AUTHENT:
          irqEn   = 0x12;
          waitFor = 0x10;
          break;
       case PCD_TRANSCEIVE:
          irqEn   = 0x77;
          waitFor = 0x30;
          break;
       default:
         break;
    }
   
    WriteRawRC(ComIEnReg,irqEn|0x80);
    ClearBitMask(ComIrqReg,0x80);
    WriteRawRC(CommandReg,PCD_IDLE);
    SetBitMask(FIFOLevelReg,0x80);
   
    for (i=0; i<InLenByte; i++)
    {   WriteRawRC(FIFODataReg, pInData);    }
    WriteRawRC(CommandReg, Command);
   
   
    if (Command == PCD_TRANSCEIVE)
    {    SetBitMask(BitFramingReg,0x80);}
   
    //i = 600;//根据时钟频率调整,操作M1卡最大等待时间25ms
    i = 6000;//根据时钟频率调整,操作M1卡最大等待时间25ms
   
    do
    {
         n = ReadRawRC(ComIrqReg);

//                if(sp_delay)
//                        RF4_Delay(1200);
               
         i--;
    }
    while ((i!=0) && !(n&0x01) && !(n&waitFor));

        if(sp_delay)
        {
                unsigned charst= ReadRawRC(Status2Reg);
          Uart_Trace("PcdComMF522 Status2Reg=%x",st);               
          Uart_Trace("PcdComMF522 timeoutH=%x",(i&0xFF00)>>8);
          Uart_Trace("PcdComMF522 timeoutL=%x",i&0xFF);
          Uart_Trace("PcdComMF522 ComIrqReg=%x",n);
        }

    ClearBitMask(BitFramingReg,0x80);
       

       
    if (i!=0)
    {   
              getvalue = ReadRawRC(ErrorReg);
                  if(sp_delay)
              Uart_Trace("PcdComMF522 ErrorReg=%x",getvalue);
                  
         //if(!(ReadRawRC(ErrorReg)&0x1B))
         if(!(getvalue&0x1B))
         {
             status = MI_OK;
             if (n & irqEn & 0x01)
             {   status = MI_NOTAGERR;   }
             if (Command == PCD_TRANSCEIVE)
             {
                       n = ReadRawRC(FIFOLevelReg);
                               
                    lastBits = ReadRawRC(ControlReg) & 0x07;

                                if(sp_delay)
                                   {
                                           Uart_Trace("PcdComMF522 FIFOLevelReg=%x",n);
                                           Uart_Trace("PcdComMF522 ControlReg=%x",lastBits);
                                }

                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++)
                {   pOutData = ReadRawRC(FIFODataReg);    }
            }
         }
         else
         {   status = MI_ERR;   }
      
   }
   

   SetBitMask(ControlReg,0x80);         // stop timer now
   WriteRawRC(CommandReg,PCD_IDLE);
   return status;
}

/////////////////////////////////////////////////////////////////////
//功    能:写数据到M1卡一块
//参数说明: addr:块地址
//          pData:写入的数据,16字节
//返    回: 成功返回MI_OK
/////////////////////////////////////////////////////////////////////                  
char PcdWrite(unsigned char addr,unsigned char *pData)
{
    char status;
    unsigned intunLen;
    unsigned char i,ucComMF522Buf;
   
    ucComMF522Buf = PICC_WRITE;
    ucComMF522Buf = addr;
    CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf);

    status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen);

    if ((status != MI_OK) || (unLen != 4) || ((ucComMF522Buf & 0x0F) != 0x0A))
    {   status = MI_ERR;   
                Uart_Trace("PcdWrite err1",0);
        }
      
    if (status == MI_OK)
    {
      //memcpy(ucComMF522Buf, pData, 16);
      for (i=0; i<16; i++)
      {    ucComMF522Buf = *(pData+i);   }
      CalulateCRC(ucComMF522Buf,16,&ucComMF522Buf);

                sp_delay = 1;
      status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,18,ucComMF522Buf,&unLen);
      if ((status != MI_OK) || (unLen != 4) || ((ucComMF522Buf & 0x0F) != 0x0A))
      {   status = MI_ERR;   
                        Uart_Trace("PcdWrite 2 unLen=%d",unLen);
                        Uart_Trace("PcdWrite 2 ucComMF522Buf=%x",ucComMF522Buf);
                }
               
                sp_delay = 0;
    }
   
    return status;
}


请问这个ucComMF522Buf & 0x0F) != 0x0A
中的返回值为什么是0xa.为什么我得到的老是0x05,写也没成功。
还有这个返回值什么文档里定义了?谢谢。

Hz01800475 发表于 2012-11-9 21:14:16

好惨的帖子

server240 发表于 2012-11-27 11:14:18

同命相连啊,我读RC522里任意寄存器的值,返回都为0xff。{:cry:}

LingYi 发表于 2013-7-29 09:27:37

楼主,可以给个参考电路吗?
页: [1]
查看完整版本: 咨询一个问题mfrc523的问题