cancel
Showing results for 
Search instead for 
Did you mean: 

probllem in a function

mhdizgah
Associate II
Posted on September 26, 2014 at 23:06

hi every body i have a problem with this function & dont know reason of it:

[CODE]

lib.h

char PcdValue(unsigned char dd_mode,unsigned char addr,unsigned char *pValue);

[/CODE]

main:

unsigned char mount[4];

status= PcdValue(0xC1,4,mount);

[CODE]

[/CODE]

[CODE]

lib.c

/////////////////////////////////////////////////////////////////////

// @ Function: debit and prepaid

// @ Parameter Description: dd_mode [IN]: command word

// 0xC0 = debit

// 0xC1 = recharge

// Addr [IN]: wallet address

// PValue [IN]: 4 bytes Increase (decrease) in value, low in the former

// @ Return: the successful return MI_OK

/////////////////////////////////////////////////////////////////////                

char PcdValue(unsigned char dd_mode,unsigned char addr,unsigned char *pValue)

{

    char status;

    unsigned int  unLen;

    unsigned char i,ucComMF522Buf[MAXRLEN]; 

    

    ucComMF522Buf[0] = dd_mode;

    ucComMF522Buf[1] = addr;

    CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]);

 

   [COLOR=''&sharpFF0000''] status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen);

[/COLOR]

if ((status != MI_OK) || (unLen != 4) || ((ucComMF522Buf[0] & 0x0F) != 0x0A))

    {   status = MI_ERR;   }

    if (status == MI_OK)

    {

        for (i=0; i<16; i++)

        {    ucComMF522Buf[i] = *(pValue+i);   }

        CalulateCRC(ucComMF522Buf,4,&ucComMF522Buf[4]);

        unLen = 0;

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

        if (status != MI_ERR)

        {    status = MI_OK;    }

    }

    

    if (status == MI_OK)

    {

        ucComMF522Buf[0] = PICC_TRANSFER;

        ucComMF522Buf[1] = addr;

        CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]); 

   

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

        if ((status != MI_OK) || (unLen != 4) || ((ucComMF522Buf[0] & 0x0F) != 0x0A))

        {   status = MI_ERR;   }

    }

    return status;

}

[/CODE]

[CODE]

/////////////////////////////////////////////////////////////////////

//@ Function: RC522 and ISO14443 card through communication

//@ Parameter Description: Command [IN]: RC522 command word

//PIn [IN]: data is sent to the card by RC522

//InLenByte [IN]: send data byte length

//POut [OUT]: return data received cards

//* POutLenBit [OUT]: Returns the data bit length

/////////////////////////////////////////////////////////////////////

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;

    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);

    WriteRawRC(CommandReg, PCD_IDLE);

    for (i=0; i<InLenByte; i++)

        { 

           WriteRawRC(FIFODataReg, pInData[i]); 

        }

    WriteRawRC(CommandReg, Command);

    if (Command == PCD_TRANSCEIVE)  {    SetBitMask(BitFramingReg,0x80);  }

    i = 2000 ; //600;//????????,??M1???????25ms

    do 

      {

         n = ReadRawRC(ComIrqReg);

         i--;

       }

    while ((i!=0) && !(n&0x01) && !(n&waitFor));

    ClearBitMask(BitFramingReg,0x80);

     

    if (i!=0)

    {    

         if(!(ReadRawRC(ErrorReg)&0x1B))

         {

             status = MI_OK;

             if (n & irqEn & 0x01) {status = MI_NOTAGERR;}

             if (Command == PCD_TRANSCEIVE)

                 {

                n = ReadRawRC(FIFOLevelReg);

              lastBits = ReadRawRC(ControlReg) & 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++)

                    {

                       pOutData[i] = ReadRawRC(FIFODataReg);

                    }

                 }

         }

         else

         {   status = MI_ERR;   }

        

   }

   SetBitMask(ControlReg,0x80);           // stop timer now

   WriteRawRC(CommandReg,PCD_IDLE); 

   return status;

}

[/CODE]

i know problem is related with PcdComMF522 function(red sectio) becouse after running this function 

outcome wi be status = MI-ERR(indicate error)

but i use PcdComMF522 function in other place and it work very well

thanks any body

#stm32f103
1 REPLY 1
Posted on September 26, 2014 at 23:32

i know problem is related with PcdComMF522 function(red section) because after running this function outcome will be status = MI-ERR(indicate error)

 

but i use PcdComMF522 function in other place and it work very well - thanks any body

Yeah, you'll need to confine the failure a bit better than that, nobody here is going to be able to replicate your failure condition. Check you have an adequate stack, and check the interaction with the functions it calls.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..