Ethernet Shield R3 with W5100 TCP Data corruption at other than default settings

Hi,

I am working on a project where the default TCP read performance is not good enough and I cannot use UDP because of reasons. So I am trying to improve the performance of TCP reads in my sketch. I am trying to read fixed size packets (321 bytes each).

I have added two changes in w5100.c and w5100.h that actually improved the performance to desired levels.

  1. I increased the SPI speed to 14Mhz.
  2. I have only one socket in the system and increased the Rx Memory size to 8k. (The rationale being that I could read in my sketch in bulk avoiding small reads and thus increasing performance.)

But having either of them changes or both together corrupts the data. I send a file to Arduino using netcat and I dump what I read on the Arduino. The corruption is found in the dumped files. I compare the sent file and the dump and there are a lot of differences throughout the file. If I don't have the improvements in the w5100.* files, then there is no corruption.

I might be making some mistake in the changes that I have made in the w5100.* files. I suspect the read functions in w5100.c also need changes. But I am unable to determine them. Can someone please help fix my problem?

I have attached my w5100.c and w5100.h files and the test sketch (DumpCompare_Forum.ino). The changes I have made are present under the macro "#if PBD".

Please let me know if you need any more information from me.

Thanks!

DumpCompare_Forum.ino (2.96 KB)

w5100.h (14.8 KB)

w5100.cpp (5.78 KB)

It would be essential to know exactly what changes you made to the W5100 header and source files.

PaulS:
It would be essential to know exactly what changes you made to the W5100 header and source files.

I attached the w5100.c and w5100.h files to my original post. All the changes are under the flag #if PBD in the w5100.c and w5100.h files. A quick search will highlight all of them. I haven't made changes to any other network files.

Did you add those changes one at a time? I would start again with the default w5100.h and w5100.cpp and get just the SPI speed increase to work. That should make the biggest speed difference. Personally, I wouldn't mess with the socket settings. Leave the socket size and number alone.

SurferTim:
Did you add those changes one at a time? I would start again with the default w5100.h and w5100.cpp and get just the SPI speed increase to work. That should make the biggest speed difference. Personally, I wouldn't mess with the socket settings. Leave the socket size and number alone.

I had tried setting just the SPI speed to 14Mhz. It corrupts the data and there is no improvement in the TCP read performance because I guess the network stack on the ethernet shield has no relation with the SPI speed. SPI speed probably just increases the speed of communication between the Arduino Due and Ethernet shield. I am not sure about this though.

That is right. The SPI bus speed is the limiting factor on the speed. If you are using the Due, then the second define is the one you should change. Try using 8MHz.

#if defined(ARDUINO_ARCH_AVR)
#define SPI_ETHERNET_SETTINGS SPISettings(4000000, MSBFIRST, SPI_MODE0)
#else
#define SPI_ETHERNET_SETTINGS SPI_CS,SPISettings(4000000, MSBFIRST, SPI_MODE0)
#endif

The copy of the ethernet library located in this GIT repository Bitbucket has been updated with several speed enhancements:

  • SPI speed increased
  • Functions added to allow you to set socket buffer sizes
  • Skip functions added which are a faster way to skip over bytes in packets you dont actually need. Much faster than reading them and discarding them.

These are tested and working for UDP packets. Adapting TCP functions to use them should not be difficult. In fact I only think you would need to add a skip function.