RFID reader/writer (again).. help on where to begin in the datasheet?

Hi Nick-

I was editing my post as you posted... :slight_smile:

does it matter yours is an ID-12 reader and mine is SPI based?

side questions.. which is much more, beginner "C" question Im guessing..

some noob explanation of ow str[] is getting populated with any data? here in the beginning of the script (loop).. you see this:

***edit: I think I see how ts getting populated eventually, at the end of the MFRC522_ToCard() function call...
how and with what..I havent tried to trace it that closely.. but at least I found that part..

</end edit>

#include <SPI.h>

#define	uchar	unsigned char
#define	uint	unsigned int
#define MAX_LEN 16

void loop()
{
  uchar i,tmp;
  uchar status;
  uchar str[MAX_LEN];
  uchar RC_size;
  uchar blockAddr;  //Select operation block address  0 - 63

  //Search card, return card types
  status = MFRC522_Request(PICC_REQIDL, str);	
  if (status == MI_OK){
    Serial.println("CARD DETECTED IN PROXIMITY....");
    Serial.print("CARD TYPE: ");
    Serial.print(str[0],DEC);
    Serial.print(" / ");
    Serial.print("STATUS CODE: ");
    Serial.println(str[1],DEC);
    //Serial.println(" ");
    //delay(500);
  }
}

create a var called MAX_LEN = 16
then, to me.. we create a new (empty) array called str[] in the beginning.. it has 16 index/slots long? (based on the str[MAX_LEN] parameter correct? (rule is: you have to give your arrays a length in C, that cant be 'dynamically filled' to whatever amount?)

Im used to be able to do:

var someArray:Array = new Array();
and I can push/populate that to my hearts content (no defining the length prior).. I understand I have been spoiled by this.. but is this correct you must define a length.. (in C)..... (better for memory management I guess?)

here is the function called above, that sets the 'STATUS' var... (again Im curious as to how str[] is being populated.. so I am able to grab the data in each index like in the above snippet)

//MFRC522_Request
//----------------------------------------------------------//
/*
* Function: MFRC522_Request()
 * Description: Searching card, read card type
 * Input parameter: (reqMode--search methods, TagType--return card types)
 
 *     -RETURN CARD TYPE VALUES -
 *     0x4400 = Mifare_UltraLight
 *     0x0400 = Mifare_One(S50)
 *     0x0200 = Mifare_One(S70)
 *     0x0800 = Mifare_Pro(X)
 *     0x4403 = Mifare_DESFire
 
 * return: return MI_OK (if success)
 */
uchar MFRC522_Request(uchar reqMode, uchar *TagType){
  uchar status;  
  uint backBits;      //the data bits that received
  Write_MFRC522(BitFramingReg, 0x07);	//TxLastBists = BitFramingReg[2..0]	???

  TagType[0] = reqMode;
  status = MFRC522_ToCard(PCD_TRANSCEIVE, TagType, 1, TagType, &backBits);

  if ((status != MI_OK) || (backBits != 0x10)){    
    status = MI_ERR;
  }
  return status;
}

I see the (empty?) array is passed along to this function, as the second parameter/argument..(as TagType)... (! what does the "" mean in front? !)

then it looks like we are populating index 0 of the str[] array..with the reqMode (parameter we just passed the same function?) value?

from there... Im a bit lost.. seems we call another function (write) and send this array (as the *sendData * *backData arguments of that function?

(posting it here in case it helps?)..

I dont wanna post to much and clutter.. and i dont wanna get too lost either.. (Im trying to understand piece by piece here..LOL)

toCard function:

//MFRC522_ToCard
//----------------------------------------------------------//
/*
* Function: MFRC522_ToCard()
 * Description: communicate between RC522 and ISO14443
 * Input parameter: (command--MF522 command bits, 
 		     sendData--send data to card via rc522, 
 		     sendLen--send data length, 
 		     backData--the return data from card, 
 		     backLen--the length of return data)
 
 *return: return MI_OK (if success)
 */
uchar MFRC522_ToCard(uchar command, uchar *sendData, uchar sendLen, uchar *backData, uint *backLen){
  uchar status = MI_ERR;
  uchar irqEn = 0x00;
  uchar waitIRq = 0x00;
  uchar lastBits;
  uchar n;
  uint i;
  switch (command){
  case PCD_AUTHENT: // verify card password
    {
      irqEn = 0x12;
      waitIRq = 0x10;
      break;
    }
  case PCD_TRANSCEIVE: // send data in the FIFO
    {
      irqEn = 0x77;
      waitIRq = 0x30;
      break;
    }
  default:
    break;
  }

  Write_MFRC522(CommIEnReg, irqEn|0x80);	//Allow interruption
  ClearBitMask(CommIrqReg, 0x80);		//Clear all the interrupt bits
  SetBitMask(FIFOLevelReg, 0x80);		//FlushBuffer=1, FIFO initilizate

  Write_MFRC522(CommandReg, PCD_IDLE);	//NO action / cancel current command?

  for (i=0; i<sendLen; i++){   
    Write_MFRC522(FIFODataReg, sendData[i]);    
  }

  Write_MFRC522(CommandReg, command);
  if (command == PCD_TRANSCEIVE){    
    SetBitMask(BitFramingReg, 0x80);		//StartSend=1,transmission of data starts  
  }

  //wait receive data is finished
  i = 2000;	//i should adjust according the clock, the maxium the waiting time should be 25 ms???
  do{
    //CommIrqReg[7..0]
    //Set1 TxIRq RxIRq IdleIRq HiAlerIRq LoAlertIRq ErrIRq TimerIRq
    n = Read_MFRC522(CommIrqReg);
    i--;
  }
  while ((i!=0) && !(n&0x01) && !(n&waitIRq));

  ClearBitMask(BitFramingReg, 0x80);			//StartSend=0
  if (i != 0){    
    if(!(Read_MFRC522(ErrorReg) & 0x1B)){  // BufferOvfl Collerr CRCErr ProtecolErr
      status = MI_OK;
      if (n & irqEn & 0x01){   
        status = MI_NOTAGERR;			//??   
      }
      if (command == PCD_TRANSCEIVE){
        n = Read_MFRC522(FIFOLevelReg);
        lastBits = Read_MFRC522(ControlReg) & 0x07;
        if (lastBits){   
          *backLen = (n-1)*8 + lastBits;   
        }
        else{   
          *backLen = n*8;   
        }
        if (n == 0){   
          n = 1;    
        }
        if (n > MAX_LEN){   
          n = MAX_LEN;   
        }

        //read the data from FIFO
        for (i=0; i<n; i++){   
          backData[i] = Read_MFRC522(FIFODataReg);    
        }
      }
    }
    else{   
      status = MI_ERR;  
    }
  }
  //SetBitMask(ControlReg,0x80);           //timer stops
  //Write_MFRC522(CommandReg, PCD_IDLE); 
  return status;
}

Looking at some 'Jeremy Blum' tutorial on-line.. it looks as if the UART version is easier to work with? (shame as I already have this and it was only $20.00)

(my only redemption I have is that every thread/forum/post I have read..everyone to seems to be in the same place as I am with this 'demo code' LOL)

thanks

-xl