Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge WIZnet fork to support more devices #185

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@ EthernetNoHardware LITERAL1
EthernetW5100 LITERAL1
EthernetW5200 LITERAL1
EthernetW5500 LITERAL1
EthernetW6100 LITERAL1
EthernetW5100S LITERAL1
2 changes: 2 additions & 0 deletions src/Ethernet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,11 @@ EthernetLinkStatus EthernetClass::linkStatus()
EthernetHardwareStatus EthernetClass::hardwareStatus()
{
switch (W5100.getChip()) {
case 50: return EthernetW5100S;
case 51: return EthernetW5100;
case 52: return EthernetW5200;
case 55: return EthernetW5500;
case 61: return EthernetW6100;
default: return EthernetNoHardware;
}
}
Expand Down
77 changes: 76 additions & 1 deletion src/Ethernet.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,79 @@
// does not always seem to work in practice (maybe WIZnet bugs?)
//#define ETHERNET_LARGE_BUFFERS

//#define USE_SERIAL_DEBUG_PRINT

#ifdef USE_SERIAL_DEBUG_PRINT
#define PRINTLINE_DEF(var) \
PRINTLINE(); \
Serial.println("PRINTLINE_DEF("#var")");
#else
#define PRINTLINE_DEF(var)
#endif

#ifdef USE_SERIAL_DEBUG_PRINT
#define PRINTLINE() \
Serial.print("\r\n"); \
Serial.print(__FILE__); \
Serial.print(" "); \
Serial.println(__LINE__);
#else
#define PRINTLINE()
#endif

#ifdef USE_SERIAL_DEBUG_PRINT
#define PRINTVAR_HEX(var) \
PRINTLINE(); \
Serial.print("PRINTVAR_HEX("#var")"); \
Serial.print(" = 0x"); \
Serial.println(var, HEX);
#else
#define PRINTVAR_HEX(var)
#endif

#ifdef USE_SERIAL_DEBUG_PRINT
#define PRINTVAR_HEXD(var1, var2) \
PRINTLINE(); \
Serial.print("PRINTVAR_HEXD("#var1", "#var2")"); \
Serial.print(" = 0x"); \
Serial.print(var1, HEX); \
Serial.print(" = 0x"); \
Serial.println(var2, HEX);
#else
#define PRINTVAR_HEXD(var1, var2)
#endif

#ifdef USE_SERIAL_DEBUG_PRINT
#define PRINTVAR_HEXT(var1, var2, var3) \
PRINTLINE(); \
Serial.print("PRINTVAR_HEXT("#var1", "#var2", "#var3")"); \
Serial.print(" = 0x"); \
Serial.print(var1, HEX); \
Serial.print(" = 0x"); \
Serial.print(var2, HEX); \
Serial.print(" = 0x"); \
Serial.println(var3, HEX);
#else
#define PRINTVAR_HEXT(var1, var2, var3)
#endif

#ifdef USE_SERIAL_DEBUG_PRINT
#define PRINTVAR(var) \
PRINTLINE(); \
Serial.print("PRINTVAR("#var")"); \
Serial.print(" = "); \
Serial.println(var);
#else
#define PRINTVAR(var)
#endif

#ifdef USE_SERIAL_DEBUG_PRINT
#define PRINTSTR(var) \
PRINTLINE(); \
Serial.println("PRINTVAR_STR("#var")");
#else
#define PRINTSTR(var)
#endif

#include <Arduino.h>
#include "Client.h"
Expand All @@ -63,7 +136,9 @@ enum EthernetHardwareStatus {
EthernetNoHardware,
EthernetW5100,
EthernetW5200,
EthernetW5500
EthernetW5500,
EthernetW6100,
EthernetW5100S
};

class EthernetUDP;
Expand Down
8 changes: 4 additions & 4 deletions src/EthernetServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ EthernetClient EthernetServer::available()
chip = W5100.getChip();
if (!chip) return EthernetClient(MAX_SOCK_NUM);
#if MAX_SOCK_NUM > 4
if (chip == 51) maxindex = 4; // W5100 chip never supports more than 4 sockets
if (chip == 51 || chip == 50) maxindex = 4; // W5100+W5100S chips never support more than 4 sockets
#endif
for (uint8_t i=0; i < maxindex; i++) {
if (server_port[i] == _port) {
Expand Down Expand Up @@ -81,7 +81,7 @@ EthernetClient EthernetServer::accept()
chip = W5100.getChip();
if (!chip) return EthernetClient(MAX_SOCK_NUM);
#if MAX_SOCK_NUM > 4
if (chip == 51) maxindex = 4; // W5100 chip never supports more than 4 sockets
if (chip == 51 || chip == 50) maxindex = 4; // W5100 chip never supports more than 4 sockets
#endif
for (uint8_t i=0; i < maxindex; i++) {
if (server_port[i] == _port) {
Expand All @@ -108,7 +108,7 @@ EthernetServer::operator bool()
{
uint8_t maxindex=MAX_SOCK_NUM;
#if MAX_SOCK_NUM > 4
if (W5100.getChip() == 51) maxindex = 4; // W5100 chip never supports more than 4 sockets
if (W5100.getChip() == 51 || W5100.getChip() == 50) maxindex = 4; // W5100 chip never supports more than 4 sockets
#endif
for (uint8_t i=0; i < maxindex; i++) {
if (server_port[i] == _port) {
Expand Down Expand Up @@ -165,7 +165,7 @@ size_t EthernetServer::write(const uint8_t *buffer, size_t size)
chip = W5100.getChip();
if (!chip) return 0;
#if MAX_SOCK_NUM > 4
if (chip == 51) maxindex = 4; // W5100 chip never supports more than 4 sockets
if (chip == 51 || chip == 50) maxindex = 4; // W5100 chip never supports more than 4 sockets
#endif
available();
for (uint8_t i=0; i < maxindex; i++) {
Expand Down
59 changes: 47 additions & 12 deletions src/EthernetUdp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,54 @@ int EthernetUDP::parsePacket()

if (Ethernet.socketRecvAvailable(sockindex) > 0) {
//HACK - hand-parse the UDP packet using TCP recv method
uint8_t tmpBuf[8];
uint8_t tmpBuf[20];
int ret=0;
//read 8 header bytes and get IP and port from it
ret = Ethernet.socketRecv(sockindex, tmpBuf, 8);
if (ret > 0) {
_remoteIP = tmpBuf;
_remotePort = tmpBuf[4];
_remotePort = (_remotePort << 8) + tmpBuf[5];
_remaining = tmpBuf[6];
_remaining = (_remaining << 8) + tmpBuf[7];

// When we get here, any remaining bytes are the data
ret = _remaining;

if(W5100.getChip() == 61) {
//read 2 header bytes and get one IPv4 or IPv6
ret = Ethernet.socketRecv(sockindex, tmpBuf, 2);
if(ret > 0) {
_remaining = (tmpBuf[0] & (0x7))<<8 | tmpBuf[1];

if((tmpBuf[0] & W6100_UDP_HEADER_IPV) == W6100_UDP_HEADER_IPV6) {
// IPv6 UDP Received
// 0 1
// 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// 18 19

//read 16 header bytes and get IP and port from it
ret = Ethernet.socketRecv(sockindex, &tmpBuf[2], 18);
_remoteIP = &tmpBuf[2];
_remotePort = (tmpBuf[18]<<8) | tmpBuf[19];
} else {
// IPv4 UDP Received
// 0 1
// 2 3 4 5
// 6 7

//read 6 header bytes and get IP and port from it
ret = Ethernet.socketRecv(sockindex, &tmpBuf[2], 6);
_remoteIP = &tmpBuf[2];
_remotePort = (tmpBuf[6]<<8) | tmpBuf[7];
}

ret = _remaining;
}
} else {
//read 8 header bytes and get IP and port from it
ret = Ethernet.socketRecv(sockindex, tmpBuf, 8);

if (ret > 0) {

_remoteIP = tmpBuf;
_remotePort = tmpBuf[4];
_remotePort = (_remotePort << 8) + tmpBuf[5];
_remaining = tmpBuf[6];
_remaining = (_remaining << 8) + tmpBuf[7];

// When we get here, any remaining bytes are the data
ret = _remaining;
}
}
return ret;
}
Expand Down
4 changes: 2 additions & 2 deletions src/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ uint8_t EthernetClass::socketBegin(uint8_t protocol, uint16_t port)
chip = W5100.getChip();
if (!chip) return MAX_SOCK_NUM; // immediate error if no hardware detected
#if MAX_SOCK_NUM > 4
if (chip == 51) maxindex = 4; // W5100 chip never supports more than 4 sockets
if (chip == 51 || chip == 50) maxindex = 4; // W5100 chip never supports more than 4 sockets
#endif
//Serial.printf("W5000socket begin, protocol=%d, port=%d\n", protocol, port);
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
Expand Down Expand Up @@ -132,7 +132,7 @@ uint8_t EthernetClass::socketBeginMulticast(uint8_t protocol, IPAddress ip, uint
chip = W5100.getChip();
if (!chip) return MAX_SOCK_NUM; // immediate error if no hardware detected
#if MAX_SOCK_NUM > 4
if (chip == 51) maxindex = 4; // W5100 chip never supports more than 4 sockets
if (chip == 51 || chip == 50) maxindex = 4; // W5100 chip never supports more than 4 sockets
#endif
//Serial.printf("W5000socket begin, protocol=%d, port=%d\n", protocol, port);
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
Expand Down
Loading