Thanks SurferTim!
I do manage SPI select/deselect of Ethernet, SD card and NFC through digital PINs 10, 4 and 5.
I stack the Ethernet shield on top of Arduino Uno and then I wire the required PINs to the SeeedStudio NFC shield (also to the ICSP PINS of the NFC shield).
If I reuse the three SPI PINs (11,12 & 13) in the NFC shield, the NFC shield stops reading tags as soon as I select the Ethernet chip for the first time (even when I deselect it afterwards - PIN10.HIGH).
However if I use three different digital PINs for NFC shield (3,6 & 9), the NFC shield keeps working and reading tags after the selection of Ethernet chip. In fact I can read tags and then send the data to my server.
That is why I wonder if the bug of W5100 SPI selection is still there.
See my code attached:
#include <PN532.h>
#include <SPI.h>
#include <Ethernet.h>
#define SCK 9 //13
#define MOSI 3 //11
#define SS 5
#define MISO 6 //12
PN532 nfc(SCK, MISO, MOSI, SS);
byte mac[] = { 0x90, 0xA2, 0xDA, 0xDA, 0xDA, 0xDA };
EthernetClient client;
unsigned long time;
unsigned long responseTime;
uint32_t tagId=0;
uint32_t flowState =0;
#define STATE_IDDLE 0
#define STATE_SENDDATA 15
#define STATE_RESPONSE 20
void setup()
{
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
time = millis();
Serial.begin(19200);
Serial.println("Starting setup method...");
//Initialise NFC reader
nfc.begin();
nfc.RFConfiguration(0x14); // default is 0xFF (try forever; ultimately it does time out but after a long while
// modifies NFC library to set up a timeout while searching for RFID tags
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't find PN53x board");
// stop
for(;;);
}
// ok, print received data!
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);
Serial.print("Supports "); Serial.println(versiondata & 0xFF, HEX);
// configure board to read RFID tags and cards
nfc.SAMConfig();
//Initialise Ethernet connection
Serial.println("StartEthernet");
digitalWrite(5, HIGH); //SPI deselect RFID reader
digitalWrite(10, LOW); //SPI select Ethernet
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// stop
for(;;);
}
// give the Ethernet shield a second to initialize:
delay(1000);
digitalWrite(10, HIGH); //SPI deselect Ethernet
Serial.println("NFC and Ethernet initialised OK");
flowState=STATE_IDDLE;
delay(2000);
}
void loop()
{
if ((millis()-time > 1000)&&(flowState==STATE_IDDLE)) {
Serial.println("Checking NFC...");
// look for Mifare type cards every second
time=millis();
digitalWrite(10, HIGH);//SPI deselect Ethernet
digitalWrite(5, LOW);//SPI select RFID reader
tagId = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A);
digitalWrite(5, HIGH);//SPI deselect RFID reader
digitalWrite(10, LOW);//SPI select Ethernet
if (tagId != 0)
{
Serial.print("Read card #"); Serial.println(tagId);
time=millis();
flowState=STATE_SENDDATA;
return;
}
}
if (flowState==STATE_SENDDATA) {
Serial.println("Connecting to server ...");
// if you get a connection, report back via serial:
if (client.connect("myserver", 80)) { //fill in your server ip address/URL and port
Serial.println("Connected. Making GET http request");
// Make a HTTP request:
client.print("GET /TestServlet?TagId=");
client.println(tagId);
client.println(" HTTP/1.1");
client.println();
responseTime=millis();
flowState=STATE_RESPONSE;
} else {
Serial.println("Connection to server failed");
flowState=STATE_IDDLE;
}
}
if (flowState== STATE_RESPONSE) {
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if ((millis() - responseTime)>2000) {
Serial.println();
Serial.println("Closing connection to server");
client.stop();
flowState=STATE_IDDLE;
}
}
}