NFC shield, issue with card emulation

Hello,

I was trying to do some BT handover with my NFC shield V2 and then exchange data between the SD card on my arduino ethernet but I have some problems with card emulation. It finally managed to init both the SD and the NFC (only activated the pn532 without emulation) with the spi by switching the mode from LSB to MSB but as soon as I add the code for starting emulation nothing work anymore (not even the start message on the console). Same thing happens if I use the “setNdefFile” in the setup. Here is the test code I’m curently using to make the NFC emulation and SD card work at the same time.


#include "SD.h"
#include "SPI.h"
#include "PN532_SPI.h"
#include "emulatetag.h"
#include "NdefMessage.h"

//def SD
#define SD_CS 4

//def nfc
#define PN532_CS 9

//def nfc
PN532_SPI pn532spi(SPI, PN532_CS);
EmulateTag nfc(pn532spi);

uint8_t ndefBuf[120];
NdefMessage msgnfc;
int msgnfcSize;

uint8_t uid[3] = { 0x12, 0x34, 0x56 };

/* Chip select routine */
void spiSelect(int CS) {
  // disable all SPI
  digitalWrite(PN532_CS,HIGH);
  digitalWrite(SD_CS,HIGH);
  // enable the chip we want
  digitalWrite(CS,LOW);  
  switch(CS){
    case PN532_CS:
      SPI.setBitOrder(LSBFIRST);
      break;
    default:
      SPI.setBitOrder(MSBFIRST);
      break;
  }
}

void setup() {
  //init console 
  Serial.begin(19200);
  Serial.println("Start test");
  
  pinMode(PN532_CS,OUTPUT);
  pinMode(SD_CS,OUTPUT);

  spiSelect(SD_CS);
  
  if (!SD.begin(4)) {
    Serial.println("Error init SD!");
    return;
  } else {
    Serial.println("init SD Ok.");
  }

  spiSelect(PN532_CS); 
  
  //init nfc tag
  msgnfc = NdefMessage();
  msgnfc.addUriRecord("weceipt-box:autoLaunch?boxId=0.0.0.1");
  msgnfcSize = msgnfc.getEncodedSize();
  if (msgnfcSize > sizeof(ndefBuf)) {
      Serial.println("ndefBuf is too small");
      while (1) { }
  }
  
  Serial.print("Ndef encoded message size: ");
  Serial.println(msgnfcSize);

  msgnfc.encode(ndefBuf);
  
  // comment out this coMmmand for no ndef message
  //nfc.setNdefFile(ndefBuf, msgnfcSize);
  
  // uid must be 3 bytes!
  nfc.setUid(uid);
  if(!nfc.init()) {
    Serial.println("Error init NFC!");
    return;
  } else {
    Serial.println("init NFC Ok.");
  }
  
}

void loop() {
  
   spiSelect(PN532_CS);
   // NFC uses LSB first so we have to explicitly set that
  //nfc.emulate();
}

If you have any idea it would be great :slight_smile:

Thank you!

Perhaps it’s just your Arduino is out of memory. Stack overflow!
If it’s the case, you can use Seeeduino Mega(seeedstudio.com/depot/seeedu … ?cPath=6_7) or Arduino Mega to get more flash and RAM.

Is there a way for me to test the RAM and kno if it is “simply” a problem of stack overflow? It seems strange as i’m only adding a SD card to the nfc emulation. I already managed to make the Bluetooth and the NFC work at the same time without any problems of overflow.

SD card library at least needs 512 bytes RAM and Arduino Uno or Seeeduino only have 2048 bytes RAM.
Adafruit writes a nice document about memory problem. learn.adafruit.com/memories-of-a … ot-dot-dot
In the document, you will learn how to measure memory usage.

I had little time to run some tests but it was in fact a RAM issue. I’m still trying to change my code to make both the cards work (even with ~900 bytes available for the SD, the system keeps crashing if I had the SD Library). Anyway, thank you for your link it was really helpfull.