Problems with ENC28j60 and MFRC522

Hi

I'm trying to interface an enc28j60 ethernet module and a mfrc522 rfid reader, I'm using the ethercard library and this library for the rfid module: GitHub - miguelbalboa/rfid: Arduino RFID Library for MFRC522 the objective is to send the card ID to a server.

The code doesnt work together, the only thing that work correctly is the rfid reader, the ethernet module doesnt send anything, they have different CS pins and the hardware is correctly connected. If I remove the all the code relative to the rfid, the ethernet module starts working, I think the libraries dont play nice together, where should I start looking?

Other thing, I'm saving the card ID in a String variable, but the function to send it only allows const char type, whats the best way to convert it?

The code I'm using:

#include <SPI.h>
#include <MFRC522.h>
#include <EtherCard.h>

MFRC522 mfrc522(10, 14);

static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte myip[] = { 192,168,2,2 };
static byte gwip[] = { 192,168,2,1 };
static byte hisip[] = { 192,168,2,1 };
char website[] PROGMEM = "192.168.2.1";
byte Ethernet::buffer[500];
String id, lastId;

void setup()
{
  Serial.begin(57600);
  ether.begin(sizeof Ethernet::buffer, mymac, 8); 
  ether.staticSetup(myip, gwip);
  ether.copyIp(ether.hisip, hisip);
  SPI.begin();
  mfrc522.PCD_Init();
}

void loop()
{
  ether.packetLoop(ether.packetReceive());
  
  id = "";
  
  if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial())
  {
    for (int i = 0; i < 4; i++)
    {
      if (mfrc522.uid.uidByte[i] < 0x10) //adds a leading zero
        id += "0";
      
      id += String(mfrc522.uid.uidByte[i], HEX);
    }
    
    if (id != lastId)
    {
      Serial.println(id);
      ether.browseUrl(PSTR("/?id="), "card id here", website, reply);
      tone(9, 2300, 500);
      lastId = id;
    }
    
    else
    {
      tone(9, 2300, 100);
      delay(200);
      tone(9, 2300, 100);
    }
    
    mfrc522.PICC_HaltA();
    delay(1000);
  }
}

static void reply (byte status, word off, word len)
{
  Serial.println((const char*) Ethernet::buffer + off);
}

The EtherCard library is setting the SPI hardware to 8MHz which is probably too fast for the chip your using. As that library is not changing the SPI speed it probably expects it to be the standard 4MHz. The rest of the configuration seems to be identical, so I'd guess that this is your problem. You can try commenting out the last line of initSPI() in enc28j60.cpp.

I've commented that line, but the problem persists, I've read the datasheets and on the enc28j60 it says that when the /CS is high the other pins are put into hi-z, on the mfrc522 datasheet I cant find any reference to that, could that be the problem?

Thanks

Where is the link to that datasheet? You have to provide that information, don't let all forum helpers search for it.

sorry, here's the links:

enc28j60 - http://ww1.microchip.com/downloads/en/DeviceDoc/39662a.pdf
mfrc522 - http://www.nxp.com/documents/data_sheet/MFRC522.pdf

I've commented that line, but the problem persists, I've read the datasheets and on the enc28j60 it says that when the /CS is high the other pins are put into hi-z, on the mfrc522 datasheet I cant find any reference to that, could that be the problem?

Probably not. If the chip fulfills the SPI specifications it has to act like this. Have you checked that the chip is in SPI mode, so pin 1 is on ground, pin 32 is on Vcc? Because only if this is the case, the chip is in SPI mode and act as an SPI device.

Yes, they are connected in that configuration, I think the problem is on the MFRC522 library, I've comment everything related to the MFRC522 and uncommented line by line while testing, I've narrowed the problem to this line:

if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial())

the problem might be on one of these functions, but they don't seem to do anything that would break the ethernet code :~

I'll keep trying to find the problem, suggestions are welcome

Thanks

Please post the schematics of your project or make photos showing all the wiring. There are lots of configurations possible for these chips so it's relevant how they're wired in your setup.

In the line of code you posted the first read on the SPI is done from the MFRC code, so we might have to concentrate on the return of information.

Do you have a logic analyzer to check the SPI data transfers?

pic of the wiring, attachement 1, schematic of the ethernet module, attachement 2

I'm pretty sure that everything is correctly connected, I've triple checked and I know that the ethernet breakout works since I've used it in another project.

I have a logic analyzer and I've already checked the SPI data transfers, where's capture (attachement 3), trigger is on the enc28j60 CS

channel 0, mfrc522 cs
channel 1, enc28j60 cs
channel 2, sck
channel 3, mosi
channel 4, miso

What kind of Arduino processor module are you using? Post a link to the schematics.

Your logic analyzer seems unable to interpret the SPI data. Was the transfer to fast for it or do you have a problem there?

Please make another picture where the wiring from the ENC28J60 module to the ATmega is also visible.

The module is just a breakout board, nothing special about it...

About the data, it was just a zoom problem, the hex value didn't fit, I've attached another pic with the capture and the capture file, you can download the client here: http://www.lxtreme.nl/ols/

capture.ols (36.8 KB)

The module is just a breakout board, nothing special about it...

In this case post the pinout of it.

The pinout is almost the same of the atmega328

Thanks for your patience

pinout.png

MFRC522 mfrc522(10, 14);

The RST line of the MFRC522 is not connected to DIO 14 (=PC0) why do you define it then?

It wasn't connected when I took the picture, I had disconnected everything and reconnected and forgot that wire, it is connected now, it still doesn't work :frowning:

Does it still stop at the same line of code? It doesn't work is not a very detailed information.

Yes, same problem...

I'm testing another MRFC522 library, GitHub - ljos/MFRC522: Arduino RFID Library for MFRC522 , it happens the exact same thing, and I was thinking, might it be a timing issue?

Since the program is always searching for a new rfid tag, the SPI buffer doesn't have the ENC28J60 data for long and might not have time to send it, what do you think?

Thanks

Since the program is always searching for a new rfid tag, the SPI buffer doesn't have the ENC28J60 data for long and might not have time to send it, what do you think?

Which buffer? The SPI library doesn't use buffers.

Can you make a picture where I can see all connections?

I thought that SPI had a buffer...

A few more pictures in attachment...

MFRC522 module pinout:
(left to right)
3.3v - red
RST - orange
GND - black
IRQ - NC
MISO - yellow
MOSI - blue
SCK - white
SDA (CS) - orange

ENC28J60 module pinout:
(left to right)
VCC - red
Int - NC
MISO - yellow
MOSI - green
CLK - purple
CS - orange
RST - NC
GND - black

Try connecting the RST pin of the RFID module to Vcc, if it's left floating it might block the bus any time (going to power down mode).