W5500 Ethernet Shield SPI issues on Mega 2560

A year or so back i created a device that uses a Mega 2560 and an Ethernet Shield 2.
I now need to create more of these devices and plan to replace the Ethernet Shield 2 with a W5500 Ethernet Shield.
The W5500 Ethernet Shield looks like it should replace the Ethernet Shield 2 with no hardware or software modifications (i was expecting too much lol!) but i have been unable to get the W5500 Ethernet Shield ethernet or sd card features to work on a Mega 2560.

Let me list what works and what does not work:

Mega 2560 + Ethernet Shield 2 = both ethernet and sdcard WORK
Mega 2560 + W5500 Ethernet Shield = both ethernet and sdcard FAIL
Uno + W5500 Ethernet Shield = both ethernet and sdcard WORK

By FAIL i mean:

Ethernet
The Ethernet2 library examples all fail to return from the call to Ethernet.begin(mac):

Serial.println("Before Ethernet.begin");
int result = Ethernet.begin(mac);
Serial.print("After Ethernet.begin, result=");
Serial.println(result);

The serial console shows the 'before' message but not the 'after' message.
A google search suggests that DHCP is failing and that if i leave the sketch a minute or two then the call will return 0 indicating DHCP failure.
I've left the sketch for many minutes and this call never returns.

sdcard
The SdFat library examples all fail with error code 0x20.
A search of the SdFat library files doesn't tell me what this error code means, and neither does a google search.

I spent all of yesterday trying to find a solution but got nowhere, so am asking if anyone can suggest how to debug the problem.
Why do both SPI devices on the W5500 Ethernet Shield fail to initialize on a Mega 2560 but work fine on a Uno?
(The ethernet and sdcard features are both SPI features).

Note that in the library examples for both features i added code to set the (unused) Mega 2560 SS pin (pin 53) as an OUTPUT as recommended in various documentation.
I also added code to the Ethernet2 example to disable sdcard on CS 4:

//  set mega 2560 SS pin to output mode
pinMode(SS, OUTPUT);
  
 //  disable sd card
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);

The SdFat example already contains code to disable any secondary SPI device:

 * Set DISABLE_CHIP_SELECT to disable a second SPI device.
 * For example, with the Ethernet shield, set DISABLE_CHIP_SELECT
 * to 10 to disable the Ethernet controller.
 */
const int8_t DISABLE_CHIP_SELECT = 10;

I can post the exact sketches that fail on the Mega 2560 but work on the Uno if required.

Thanks.

Typically the "Uno works, Mega doesn't" result indicates a poorly designed shield that makes SPI bus connections to pins 11, 12, 13. Those are the SPI pins on Uno, but not on Mega (50, 51, 52). The solution for compatibility with both is to make the connection via the ICSP header, which is in the same place on both boards. To determine whether this is the case you could try connecting the shield to your Mega using jumper wires from the Mega's SPI pins to the shield's pins 11, 12, 13.

Have you tried the w5500 shield without a SD card inserted into the shield?

Thanks for the replies.

I've tried connecting the shield SPI pins (11, 12 + 13) to the Mega 2560 SPI pins (50, 51 + 52) and it didn't solve the problem.
I did notice the Arduino IDE occasionally reporting 'Port ?? busy' when i tried to connect, not sure if that was related to the SPI jumpers or whether my PC was just playing up...
A reboot got rid of the message.
I currently have the shield SPI pins connected to the shield ICSP pins with an additional jumper connecting the ICSP reset pin to the shield reset pin.
Again there is no change, it appears as if the shield is simply not detected by the Mega 2560.

Note that the official documentation for the shield does list the Mega as a supported board so that surely implies that the shield's ICSP pins are correctly connected?
http://wizwiki.net/wiki/doku.php?id=osh:w5500_ethernet_shield:start#available_board_list

Further down the same webpage it has a pin diagram:

See the green box that refers to the shield's SPI pins 11, 12 + 13?
The mount and not mount options seem obscure but maybe mean that ICSP is the default configuration but pins 11, 12 + 13 can instead be enabled by removing the surface mount resisters R31, R32 and R33?

The page also states:

Caution) When the user uses 5V Platform, we cannot confirm a stable operation of SD-CARD. Therefore, though 5V Platform is safe to use after going through enough tests, it is highly recommended to mount buffer & 100nF Capacitor for a secure operation.

Hmm again it's meaning is obscure to me...
The shield works on my 5v Uno so i assumed my 5v Mega 2560 is ok too - should i be trying to decrypt the meaning here mount buffer & 100nF Capacitor?

SurferTim:
Have you tried the w5500 shield without a SD card inserted into the shield?

Yes i've tried with and without an sdcard inserted many times, also tried different sdcards.
Again the same result - the Mega 2560 just doesn't seem to detect the shield.

I ran both the Ethernet2 and SdFat library example sketches on the Mega 2560 without the W5500 Ethernet Shield attached and the output from each sketch was the same as the output i get when the shield is attached:

SdFat example reports error 0x20

Ethernet2 example freezes on Ethernet.begin(mac)

Any more ideas?

Try this test sketch. It tests the SPI bus and SPI side of the w5500. If it displays 192.168.0.2, the SPI bus is ok. Otherwise, you have a problem with the SPI bus.

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,2);

void setup() {
  Serial.begin(9600);

  // disable SD card if one in the slot
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Serial.println("Starting w5500");
  Ethernet.begin(mac,ip);

  Serial.println(Ethernet.localIP());
}

void loop() {
}

SurferTim:
Try this test sketch. It tests the SPI bus and SPI side of the w5500. If it displays 192.168.0.2, the SPI bus is ok. Otherwise, you have a problem with the SPI bus.

#include <SPI.h>

#include <Ethernet.h>

byte mac = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,2);

void setup() {
  Serial.begin(9600);

// disable SD card if one in the slot
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

Serial.println(“Starting w5500”);
  Ethernet.begin(mac,ip);

Serial.println(Ethernet.localIP());
}

void loop() {
}

I tried setting a manual IP address yesterday just as your example code does, i used 192.168.100.171 for my home network.
The sketch ran and output 0.0.0.0 as the local IP.

Then you have a problem with that shield and the SPI bus on the Mega.

Did you set 53 on the Mega to an OUTPUT? It needs to be an output for the Mega to be SPI master, just as D10 has to be an output for the Uno to be SPI master.

CrossRoads:
Did you set 53 on the Mega to an OUTPUT? It needs to be an output for the Mega to be SPI master, just as D10 has to be an output for the Uno to be SPI master.

Hi.
Yes, buried in my original post i wrote:

Note that in the library examples for both features i added code to set the (unused) Mega 2560 SS pin (pin 53) as an OUTPUT as recommended in various documentation.

SurferTim:
Then you have a problem with that shield and the SPI bus on the Mega.

or with Ethernet.h for Ethernet 2 shield

@juraj: You are correct. I forgot to change that to “include <Ethernet2.h>”.

What if is something wrong with the Mega?

Juraj:
What if is something wrong with the Mega?

Could be. Maybe a bad solder joint on the ICSP pins, or a solder bridge on the Mega's CPU.

I've finished work for the day now but will just add that i've tested the W5500 Ethernet Shield on 2 different Mega 2560s.
One is a cheap clone and the other is an official Mega ADK (remember the version for android?).
A colleague has also tested another W5500 Ethernet Shield on another cheap clone Mega 2560 and on all 3 Megas we get the same failure.

Juraj:
or with Ethernet.h for Ethernet 2 shield

That led me to look at the Ethernet2.h file and i see mention of WIZ550io_WITH_MACADDRESS:

#if defined(WIZ550io_WITH_MACADDRESS)
  // Initialize function when use the ioShield serise (included WIZ550io)
  // WIZ550io has a MAC address which is written after reset.
  // Default IP, Gateway and subnet address are also writen.
  // so, It needs some initial time. please refer WIZ550io Datasheet in details.
  int begin(void);
  void begin(IPAddress local_ip);
  void begin(IPAddress local_ip, IPAddress dns_server);
  void begin(IPAddress local_ip, IPAddress dns_server, IPAddress gateway);
  void begin(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet);
#else
  // Initialize the Ethernet shield to use the provided MAC address and gain the rest of the
  // configuration through DHCP.
  // Returns 0 if the DHCP configuration failed, and 1 if it succeeded
  int begin(uint8_t *mac_address);
  void begin(uint8_t *mac_address, IPAddress local_ip);
  void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server);
  void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway);
  void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet);

#endif

See how the begin() method requires no MAC address if WIZ550io_WITH_MACADDRESS is defined?
This certainly looks promising and i'll look at it more tomorrow.

However i don't see this as being a fix for the sdcard...

I'll be back tomorrow!

I've made some progress and the ethernet part of the shield is now working.
I simply jumpered the shield ICSP pins to the Mega ICSP pins and then jumpered pin 10 from shield to Mega.
(I thought i'd done exactly this previously but am not sure).
So ethernet works.

I then jumpered pin 4 from shield to Mega.
Ethernet still works - with and without an sdcard inserted into the shield.

However all sketches fail to access the sdcard part of the shield, they still fail as before with the SdFat example showing error code 0x20.
The sdcard sketches i have tried all set the Mega SS pin to OUTPUT mode HIGH and ethernet CS pin 10 to OUTPUT mode HIGH.

I shall experiment more but if anyone has any suggestions i'd be glad to read them.

Did Wiznet ever fix that buggy implementation of SPI in the W5000 chip or is that still a problem with the W5500 chip?

If what you say is true (which I presume it is), then it is the shield design at fault, not the W5500.

Edit: There have been reports of shields where the shield's ICSP socket is too short, and doesn't allow the Arduino ICSP pins to insert fully into the shield's socket.

SurferTim:
There have been reports of shields where the shield's ICSP socket is too short, and doesn't allow the Arduino ICSP pins to insert fully into the shield's socket.

With my working setup i attached female jumpers to the ICSP (passthru) male pins on the ethernet shield and male jumpers on the Mega ICSP socket so this wouldn't be the (ongoing) problem.

I don't have much more time to spend trying to get both features of this board working.
I have started a thread on the Wiznet forum but no solution there either.

Looks like we'll have to source the (more expensive) Ethernet 2 shield and use that instead - at least we know it does work.