Skip to content

Commit

Permalink
optimized Ethernet for speed
Browse files Browse the repository at this point in the history
arduino-libraries#151
arduino-libraries#140

#define MAX_SOCK_NUM 4
#define ETHERNET_LARGE_BUFFERS
  • Loading branch information
lathoub committed Feb 2, 2021
1 parent 75a3c37 commit e13ecc5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/Ethernet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -38,14 +38,16 @@
#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
// this will use larger buffers within the Wiznet chip. Large buffers
// 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 <Arduino.h>
Expand Down
12 changes: 11 additions & 1 deletion src/utility/w5100.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -471,4 +481,4 @@ void W5100Class::execCmdSn(SOCKET s, SockCMD _cmd)
writeSnCR(s, _cmd);
// Wait for command to complete
while (readSnCR(s)) ;
}
}
30 changes: 20 additions & 10 deletions src/utility/w5100.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) | \
Expand Down

0 comments on commit e13ecc5

Please sign in to comment.