Issues with SPI communication in RFID Shield PN532 for arduino

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Issues with SPI communication in RFID Shield PN532 for arduino

Post by adafruit_support_rick »

Ah... OK. I'm not sure that would work, even if you could change the I2C addresses on the shields.
But it should work just fine with SPI.
So, for SPI on each shield, solder jumper wires as follows:

Code: Select all

Pin   Signal
 7     MOSI
 6     SS
 5     MISO
 4     SCK
Use the SPI version of the library. The example sketches use a different pin mapping, so change the pin definitions in the sketches as follows:

Code: Select all

#define SCK  (4)
#define MOSI (7)
#define SS   (6)
#define MISO (5)

Adafruit_PN532 nfc(SCK, MISO, MOSI, SS);

laguna_guy
 
Posts: 19
Joined: Wed Sep 04, 2013 12:37 pm

Re: Issues with SPI communication in RFID Shield PN532 for arduino

Post by laguna_guy »

Ok i'll try it out and post my result tomorrow. I really hope this works...

Thanks!

laguna_guy
 
Posts: 19
Joined: Wed Sep 04, 2013 12:37 pm

Re: Issues with SPI communication in RFID Shield PN532 for arduino

Post by laguna_guy »

Thanks a lot!! it worked! :D.

There's only one more question i got, when i use the printhexchar function it throws me this:

Reading Block 8:
61 64 61 66 72 75 69 74 2E 63 6F 6D 00 00 00 00 97100971021141171051164699111109....

when it should print chars it prints this numbers, this is my code:

Code: Select all

#include <Adafruit_PN532.h>

#define SCK  (4)
#define MOSI (7)
#define SS   (6)
#define MISO (5)

Adafruit_PN532 nfc(SCK, MISO, MOSI, SS);

void setup(void) {
  Serial.begin(9600);
  Serial.println("Hello!");

  nfc.begin();

  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board");
    while (1); // halt
  }
  // Got ok data, print it out!
  Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); 
  Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); 
  Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
  
  // configure board to read RFID tags
  nfc.SAMConfig();
  
  Serial.println("Waiting for an ISO14443A Card ...");
}


void loop(void) {
  uint8_t success;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
    
  // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
  // 'uid' will be populated with the UID, and uidLength will indicate
  // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  
  if (success) {
    // Display some basic information about the card
    Serial.println("Found an ISO14443A card");
    Serial.print("  UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
    Serial.print("  UID Value: ");
    nfc.PrintHex(uid, uidLength);
    Serial.println("");
    
    if (uidLength == 4)
    {
      // We probably have a Mifare Classic card ... 
      Serial.println("Seems to be a Mifare Classic card (4 byte UID)");
	  
      // Now we need to try to authenticate it for read/write access
      // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
      Serial.println("Trying to authenticate block 4 with default KEYA value");
      uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
	  
	  // Start with block 4 (the first block of sector 1) since sector 0
	  // contains the manufacturer data and it's probably better just
	  // to leave it alone unless you know what you're doing
      success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 8, 0, keya);
	  
      if (success)
      {
        Serial.println("Sector 1 (Blocks 4..7) has been authenticated");
        uint8_t data[16] = { 'a', 'd', 'a', 'f', 'r', 'u', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0};
		
        // If you want to write something to block 4 to test with, uncomment
		// the following line and this text should be read back in a minute
          
         success = nfc.mifareclassic_WriteDataBlock (8, data);

        // Try to read the contents of block 4
        success = nfc.mifareclassic_ReadDataBlock(8, data);
		
        if (success)
        {
          // Data seems to have been read ... spit it out
          Serial.println("Reading Block 8:");
          nfc.PrintHexChar(data, 16);
          Serial.println("");
		  
          // Wait a bit before reading the card again
          delay(1000);
        }
        else
        {
          Serial.println("Ooops ... unable to read the requested block.  Try another key?");
        }
      }
      else
      {
        Serial.println("Ooops ... authentication failed: Try another key?");
      }
    }
    
    if (uidLength == 7)
    {
      // We probably have a Mifare Ultralight card ...
      Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)");
	  
      // Try to read the first general-purpose user page (#4)
      Serial.println("Reading page 4");
      uint8_t data[32];
      success = nfc.mifareultralight_ReadPage (4, data);
      if (success)
      {
        // Data seems to have been read ... spit it out
        nfc.PrintHexChar(data, 4);
        Serial.println("");
		
        // Wait a bit before reading the card again
        delay(1000);
      }
      else
      {
        Serial.println("Ooops ... unable to read the requested page!?");
      }
    }
  }
}

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Issues with SPI communication in RFID Shield PN532 for arduino

Post by adafruit_support_rick »

laguna_guy wrote:Reading Block 8:
61 64 61 66 72 75 69 74 2E 63 6F 6D 00 00 00 00 97100971021141171051164699111109....

when it should print chars it prints this numbers
Wow - I have no answer for that at all. The printHexChar function simply doesn't do anything like that.
The only thought I had was that the serial output buffer was getting over-written by something else, but you've got a delay(1000) there, so that doesn't seem likely.

I'll see if I can replicate it. It'll take me a little while to get to it...

laguna_guy
 
Posts: 19
Joined: Wed Sep 04, 2013 12:37 pm

Re: Issues with SPI communication in RFID Shield PN532 for arduino

Post by laguna_guy »

Don't worry, i'll wait, it could be great if i could use it,but anyways that function is not necessary for what i'm doing.

But i'm interested in why does it print that... i'll wait for your response.

Thanks for everything! you have a great customer support! :)

laguna_guy
 
Posts: 19
Joined: Wed Sep 04, 2013 12:37 pm

Re: Issues with SPI communication in RFID Shield PN532 for arduino

Post by laguna_guy »

I'm having another issue, when i try to use the shield and the I2C communication to send the data that was read from the RFID tag, whenever i try to close the wire connection qith Wire.endtransmission, it freezes the arduino and does nothing else.

I've debuged with serial.prints and just in when i close the connection it freezes :( any idea why? here is my code:

Code: Select all

#include <Wire.h>
#include <Adafruit_PN532.h>

#define SCK  (4)
#define MOSI (7)
#define SS   (6)
#define MISO (5)
char datos[16];
Adafruit_PN532 nfc(SCK, MISO, MOSI, SS);

void setup()
{
  datos[0]="0";
          datos[1]="0";
          datos[2]="0";
          datos[3]="0";
          datos[4]="0";
          datos[5]="0";
          datos[6]="0";
          datos[7]="0";
          datos[8]="0";
          datos[9]="0";
          datos[10]="0";
          datos[11]="0";
          datos[12]="0";
          datos[13]="0";
          datos[14]=da;
  Wire.begin(); // join i2c bus (address optional for master)
  Serial.begin(9600);
  Serial.println("Hello!");

  nfc.begin();

  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board");
    while (1); // halt
  }
  // Got ok data, print it out!
  Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); 
  Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); 
  Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
  
  // configure board to read RFID tags
  nfc.SAMConfig();
  
  Serial.println("Waiting for an ISO14443A Card ...");
}

byte x = 0;

void loop()
{
  uint8_t success;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
    
  // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
  // 'uid' will be populated with the UID, and uidLength will indicate
  // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  
  if (success) {
    // Display some basic information about the card
    Serial.println("Found an ISO14443A card");
    Serial.print("  UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
    Serial.print("  UID Value: ");
    nfc.PrintHex(uid, uidLength);
    Serial.println("");
    
    if (uidLength == 4)
    {
      // We probably have a Mifare Classic card ... 
      Serial.println("Seems to be a Mifare Classic card (4 byte UID)");
	  
      // Now we need to try to authenticate it for read/write access
      // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
      Serial.println("Trying to authenticate block 4 with default KEYA value");
      uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
	  
	  // Start with block 4 (the first block of sector 1) since sector 0
	  // contains the manufacturer data and it's probably better just
	  // to leave it alone unless you know what you're doing
      success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 8, 0, keya);
	  
      if (success)
      {
        Serial.println("Sector 1 (Blocks 4..7) has been authenticated");
        uint8_t data[16] = { 'h', 'o', 'l', 'a', 'r', 'f', 'i', 'd', ':', 'D', 'o', 'm', ',m', 0, 0, 0};
      
        // If you want to write something to block 4 to test with, uncomment
		// the following line and this text should be read back in a minute
          
        // success = nfc.mifareclassic_WriteDataBlock (8, data);

        // Try to read the contents of block 4
        success = nfc.mifareclassic_ReadDataBlock(8, data);
		
        if (success)
        {
          // Data seems to have been read ... spit it out
          Serial.println("Reading Block 8:");
          nfc.PrintHexChar(data, 16);
          Serial.println("");
          Serial.println("comienza");
          datos[0]=data[0];
          datos[1]=data[1];
          datos[2]=data[2];
          datos[3]=data[3];
          datos[4]=data[4];
          datos[5]=data[5];
          datos[6]=data[6];
          datos[7]=data[7];
          datos[8]=data[8];
          datos[9]=data[9];
          datos[10]=data[10];
          datos[11]=data[11];
          datos[12]=data[12];
          datos[13]=data[13];
          datos[14]=data[14];
          mandar_info();
          delay(1000);// espera antes de leer de nuevo
        }
        else
        {
          Serial.println("Ooops ... unable to read the requested block.  Try another key?");
        }
      }
      else
      {
        Serial.println("Ooops ... authentication failed: Try another key?");
      }
    }
  } 
}
void mandar_info(){

Serial.println(datos[0]);
          Serial.println(datos[1]);
          Serial.println(datos[2]);
          Serial.println(datos[3]);
          Serial.println(datos[4]);
          Serial.println(datos[5]);
          Serial.println(datos[6]);
          Serial.println(datos[7]);
          Serial.println(datos[8]);
          Serial.println(datos[9]);
          Serial.println(datos[10]);
          Serial.println(datos[11]);
          Serial.println(datos[12]);
          Serial.println(datos[13]);
          Serial.println(datos[14]);
          Serial.println(datos[15]);
          Wire.beginTransmission(4); // transmit to device #4
          Serial.println("abre");
          Wire.write(datos);        // sends five bytes
          Serial.println("manda");
          Wire.endTransmission();    // stop transmitting
          Serial.println("sale");
  delay(500);
}

laguna_guy
 
Posts: 19
Joined: Wed Sep 04, 2013 12:37 pm

Re: Issues with SPI communication in RFID Shield PN532 for arduino

Post by laguna_guy »

Sorry about the large code, here is a more short example of what i mean:

Code: Select all

#include <Wire.h>
#include <Adafruit_PN532.h>

#define SCK  (4)
#define MOSI (7)
#define SS   (6)
#define MISO (5)
char datos[16];
Adafruit_PN532 nfc(SCK, MISO, MOSI, SS);
void setup()
{
  Wire.begin(); // join i2c bus (address optional for master)
  Serial.begin(9600);
}

byte x = 0;

void loop()
{
  Wire.beginTransmission(4); // transmit to device #4
  Wire.write("x is ");        // sends five bytes
  Wire.write(x);              // sends one byte  
  Wire.endTransmission();    // stop transmitting
Serial.println(x);
  x++;
  delay(500);
}
When i comment the line : Adafruit_PN532 nfc(SCK, MISO, MOSI, SS);
I can see X being constantly printed in the serial monitor, but if i try to initialize the object, the arduino freezes and there's no response whatsoever in the serial monitor.

It freezes in the line: Wire.endTransmission();

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Issues with SPI communication in RFID Shield PN532 for arduino

Post by adafruit_support_rick »

Have you got the I2C master arduino on the line? Your Wire transmission should be responding to a request from #4. If it isn't, I'm not surprised that endTransmission hangs. I'm not looking at the driver right now, but it makes sense to me that it might happen that way.

You are going to have to come up with a protocol for I2C communication. You can't just do writes whenever you want to.

Your Master is going to have to poll the three slaves, one by one. The slaves will either respond with their most recent RFID tag data, or they will respond with something that says "I got nothing".

Look at the Wire library documentation for masters and slaves.

laguna_guy
 
Posts: 19
Joined: Wed Sep 04, 2013 12:37 pm

Re: Issues with SPI communication in RFID Shield PN532 for arduino

Post by laguna_guy »

The code i wrote is for the master to communicate with a ceratin slave (Slave with address #4 that is in the I2C network) sending him data (when he detects the event Onreceive it begins to read from the buffer all the bytes), it didn't even started the communication because the transmission begins after the wire.endTransmission(), but i got it solved with this:

Code: Select all

          pinMode(SS, INPUT);
          pinMode(SCK, INPUT);
          pinMode(MOSI, INPUT);
          pinMode(MISO, OUTPUT);
          Wire.beginTransmission(4); // transmit to device #4
          Wire.write(datos);              // sends one byte  
          Wire.endTransmission();    // stop transmitting
          pinMode(SS, OUTPUT);
          pinMode(SCK, OUTPUT);
          pinMode(MOSI, OUTPUT);
          pinMode(MISO, INPUT);
The data went to the slave just fine and returned me the bytes i asked for.

The question i got is: is it safe for the arduino to change the pinMode every time it's going to send a message through I2C?

or could this be bad for the arduino or the shield?

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Issues with SPI communication in RFID Shield PN532 for arduino

Post by adafruit_support_rick »

You can change pin modes at will - it doesn't hurt anything. But I don't understand why you have to do that at all. I picked those pins for you because they have nothing to do with I2C. What kind of Arduino are you running this on?

laguna_guy
 
Posts: 19
Joined: Wed Sep 04, 2013 12:37 pm

Re: Issues with SPI communication in RFID Shield PN532 for arduino

Post by laguna_guy »

I know those pins are the ones for the SPI communication that only makes it weirder that this works... I'm curious on why is the reason.

I'm using an Arduino UNO R3 as a master and an arduino Mega 2560 R3 as slave.

Before setting the shield to SPI mode i tested the I2C communication between them with the shield on and readind RFID tags and it worked perfectly.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Issues with SPI communication in RFID Shield PN532 for arduino

Post by adafruit_support_rick »

D'Oh! Upon further review, MOSI, SCK and SS are shared with SDA and SCL at the 6-pin jumper pad.
So, you will have to change the direction of those three pins. You shouldn't have to change MISO, I don't think...

laguna_guy
 
Posts: 19
Joined: Wed Sep 04, 2013 12:37 pm

Re: Issues with SPI communication in RFID Shield PN532 for arduino

Post by laguna_guy »

I see thanks!, also there, shouldn't be a problem if i use an arduino mega as a master with the shield to transfer to the arduino UNO right?

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Issues with SPI communication in RFID Shield PN532 for arduino

Post by adafruit_support_rick »

Nope - that should work.

laguna_guy
 
Posts: 19
Joined: Wed Sep 04, 2013 12:37 pm

Re: Issues with SPI communication in RFID Shield PN532 for arduino

Post by laguna_guy »

Thank you very much for your time and the support rick! :D

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”