From e13ecc57c39827e465af4de1f053767e7c4f29f9 Mon Sep 17 00:00:00 2001 From: lathoub <4082369+lathoub@users.noreply.github.com> Date: Tue, 2 Feb 2021 07:39:30 +0100 Subject: [PATCH] optimized Ethernet for speed https://github.com/arduino-libraries/Ethernet/issues/151 https://github.com/arduino-libraries/Ethernet/issues/140 #define MAX_SOCK_NUM 4 #define ETHERNET_LARGE_BUFFERS --- src/Ethernet.h | 6 ++++-- src/utility/w5100.cpp | 12 +++++++++++- src/utility/w5100.h | 30 ++++++++++++++++++++---------- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/Ethernet.h b/src/Ethernet.h index 376e6c59..e6d09583 100644 --- a/src/Ethernet.h +++ b/src/Ethernet.h @@ -28,7 +28,7 @@ // these "friend" classes are now defined in the same header file. socket.h // was removed to avoid possible conflict with the C library header files. - +/* // Configure the maximum number of sockets to support. W5100 chips can have // up to 4 sockets. W5200 & W5500 can have up to 8 sockets. Several bytes // of RAM are used for each socket. Reducing the maximum can save RAM, but @@ -38,6 +38,8 @@ #else #define MAX_SOCK_NUM 8 #endif +*/ +#define MAX_SOCK_NUM 4 // By default, each socket uses 2K buffers inside the Wiznet chip. If // MAX_SOCK_NUM is set to fewer than the chip's maximum, uncommenting @@ -45,7 +47,7 @@ // can really help with UDP protocols like Artnet. In theory larger // buffers should allow faster TCP over high-latency links, but this // does not always seem to work in practice (maybe Wiznet bugs?) -//#define ETHERNET_LARGE_BUFFERS +#define ETHERNET_LARGE_BUFFERS #include diff --git a/src/utility/w5100.cpp b/src/utility/w5100.cpp index 4ae4ee7a..82d32dec 100644 --- a/src/utility/w5100.cpp +++ b/src/utility/w5100.cpp @@ -413,9 +413,14 @@ uint16_t W5100Class::read(uint16_t addr, uint8_t *buf, uint16_t len) cmd[1] = addr & 0xFF; cmd[2] = (len >> 8) & 0x7F; cmd[3] = len & 0xFF; +#ifdef SPI_HAS_TRANSFER_BUF + SPI.transfer(cmd, NULL, 4); + SPI.transfer(NULL, buf, len); +#else SPI.transfer(cmd, 4); memset(buf, 0, len); SPI.transfer(buf, len); +#endif resetSS(); } else { // chip == 55 setSS(); @@ -457,9 +462,14 @@ uint16_t W5100Class::read(uint16_t addr, uint8_t *buf, uint16_t len) cmd[2] = ((addr >> 6) & 0xE0) | 0x18; // 2K buffers #endif } +#ifdef SPI_HAS_TRANSFER_BUF + SPI.transfer(cmd, NULL, 3); + SPI.transfer(NULL, buf, len); +#else SPI.transfer(cmd, 3); memset(buf, 0, len); SPI.transfer(buf, len); +#endif resetSS(); } return len; @@ -471,4 +481,4 @@ void W5100Class::execCmdSn(SOCKET s, SockCMD _cmd) writeSnCR(s, _cmd); // Wait for command to complete while (readSnCR(s)) ; -} +} \ No newline at end of file diff --git a/src/utility/w5100.h b/src/utility/w5100.h index 099556a6..3741e774 100644 --- a/src/utility/w5100.h +++ b/src/utility/w5100.h @@ -197,15 +197,22 @@ class W5100Class { __GP_REGISTER_N(SIPR, 0x000F, 4); // Source IP address __GP_REGISTER8 (IR, 0x0015); // Interrupt __GP_REGISTER8 (IMR, 0x0016); // Interrupt Mask - __GP_REGISTER16(RTR, 0x0017); // Timeout address - __GP_REGISTER8 (RCR, 0x0019); // Retry count - __GP_REGISTER8 (RMSR, 0x001A); // Receive memory size (W5100 only) - __GP_REGISTER8 (TMSR, 0x001B); // Transmit memory size (W5100 only) - __GP_REGISTER8 (PATR, 0x001C); // Authentication type address in PPPoE mode - __GP_REGISTER8 (PTIMER, 0x0028); // PPP LCP Request Timer - __GP_REGISTER8 (PMAGIC, 0x0029); // PPP LCP Magic Number - __GP_REGISTER_N(UIPR, 0x002A, 4); // Unreachable IP address in UDP mode (W5100 only) - __GP_REGISTER16(UPORT, 0x002E); // Unreachable Port address in UDP mode (W5100 only) + +// https://github.com/PaulStoffregen/Ethernet/issues/47 +// https://github.com/arduino-libraries/Ethernet/issues/84 +// https://github.com/arduino-libraries/Ethernet/issues/140 + + __GP_REGISTER16(RTR, 0x0019); // Timeout address + __GP_REGISTER8 (RCR, 0x001B); // Retry count + __GP_REGISTER8 (PTIMER, 0x001C); // PPP LCP Request Timer + __GP_REGISTER8 (PMAGIC, 0x001D); // PPP LCP Magic Number + __GP_REGISTER_N(PHAR, 0x001E, 6); // PPP Destination MAC address + __GP_REGISTER16(PSID, 0x0024); // PPP Session ID + __GP_REGISTER16(PMRU, 0x0026); // PPP Maximum Segment Size + __GP_REGISTER_N(UIPR, 0x0028, 4); // Unreachable IP address in UDP mode (W5500 only) + __GP_REGISTER16(UPORT, 0x002C); // Unreachable Port address in UDP mode (W5500 only) + __GP_REGISTER8 (PHYCFGR_W5500, 0x002E); // PHY Configuration register, default: 10111xxx + __GP_REGISTER8 (VERSIONR_W5200,0x001F); // Chip Version Register (W5200 only) __GP_REGISTER8 (VERSIONR_W5500,0x0039); // Chip Version Register (W5500 only) __GP_REGISTER8 (PSTATUS_W5200, 0x0035); // PHY Status @@ -456,7 +463,10 @@ extern W5100Class W5100; #ifndef htons -#define htons(x) ( (((x)<<8)&0xFF00) | (((x)>>8)&0xFF) ) +//#define htons(x) ( (((x)<<8)&0xFF00) | (((x)>>8)&0xFF) ) +#define htons(x) ( ((( x )&0xFF)<<8) | ((( x )&0xFF00)>>8) ) + + #define ntohs(x) htons(x) #define htonl(x) ( ((x)<<24 & 0xFF000000UL) | \