From f78bdb7fe0b75b37ee2f15259c29ef6edc914b56 Mon Sep 17 00:00:00 2001 From: taylor Date: Tue, 13 Nov 2018 12:39:41 +0900 Subject: [PATCH 01/14] Added W5100S for W5100S Ethernetshield --- keywords.txt | 1 + src/Ethernet.cpp | 1 + src/Ethernet.h | 3 ++- src/EthernetServer.cpp | 8 +++--- src/socket.cpp | 4 +-- src/utility/w5100.cpp | 55 ++++++++++++++++++++++++++++++++++++++---- src/utility/w5100.h | 7 ++++-- 7 files changed, 65 insertions(+), 14 deletions(-) diff --git a/keywords.txt b/keywords.txt index 9fd25419..0013e20a 100644 --- a/keywords.txt +++ b/keywords.txt @@ -65,3 +65,4 @@ EthernetNoHardware LITERAL1 EthernetW5100 LITERAL1 EthernetW5200 LITERAL1 EthernetW5500 LITERAL1 +EthernetW5100S LITERAL1 diff --git a/src/Ethernet.cpp b/src/Ethernet.cpp index 658bd642..28658623 100644 --- a/src/Ethernet.cpp +++ b/src/Ethernet.cpp @@ -114,6 +114,7 @@ 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; diff --git a/src/Ethernet.h b/src/Ethernet.h index 376e6c59..dbc9a63d 100644 --- a/src/Ethernet.h +++ b/src/Ethernet.h @@ -63,7 +63,8 @@ enum EthernetHardwareStatus { EthernetNoHardware, EthernetW5100, EthernetW5200, - EthernetW5500 + EthernetW5500, + EthernetW5100S }; class EthernetUDP; diff --git a/src/EthernetServer.cpp b/src/EthernetServer.cpp index ddebd154..5e91534b 100644 --- a/src/EthernetServer.cpp +++ b/src/EthernetServer.cpp @@ -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 chip never supports more than 4 sockets #endif for (uint8_t i=0; i < maxindex; i++) { if (server_port[i] == _port) { @@ -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) { @@ -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) { @@ -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++) { diff --git a/src/socket.cpp b/src/socket.cpp index f059dc92..43f068ef 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -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); @@ -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); diff --git a/src/utility/w5100.cpp b/src/utility/w5100.cpp index 4ae4ee7a..c8ac6895 100644 --- a/src/utility/w5100.cpp +++ b/src/utility/w5100.cpp @@ -157,7 +157,29 @@ uint8_t W5100Class::init(void) writeSnTX_SIZE(i, 0); } #endif - // Try W5100 last. This simple chip uses fixed 4 byte frames + // Try W5100S. Brandnew based W5100. + } else if (isW5100S()) { + CH_BASE_MSB = 0x04; +#ifdef ETHERNET_LARGE_BUFFERS +#if MAX_SOCK_NUM <= 1 + SSIZE = 8192; + writeTMSR(0x03); + writeRMSR(0x03); +#elif MAX_SOCK_NUM <= 2 + SSIZE = 4096; + writeTMSR(0x0A); + writeRMSR(0x0A); +#else + SSIZE = 2048; + writeTMSR(0x55); + writeRMSR(0x55); +#endif + SMASK = SSIZE - 1; +#else + writeTMSR(0x55); + writeRMSR(0x55); +#endif +// Try W5100 last. This simple chip uses fixed 4 byte frames // for every 8 bit access. Terribly inefficient, but so simple // it recovers from "hearing" unsuccessful W5100 or W5200 // communication. W5100 is also the only chip without a VERSIONR @@ -210,7 +232,7 @@ uint8_t W5100Class::softReset(void) uint8_t mr = readMR(); //Serial.print("mr="); //Serial.println(mr, HEX); - if (mr == 0) return 1; + if (mr == 0 || (mr == 3 && chip == 50)) return 1; delay(1); } while (++count < 20); return 0; @@ -231,6 +253,23 @@ uint8_t W5100Class::isW5100(void) return 1; } +uint8_t W5100Class::isW5100S(void) +{ + chip = 50; + //Serial.println("w5100.cpp: detect W5100S chip"); + if (!softReset()) return 0; + writeMR(0x13); + if (readMR() != 0x13) return 0; + writeMR(0x03); + if (readMR() != 0x03) return 0; + int ver = readVERSIONR_W5100S(); + //Serial.print("version="); + //Serial.println(ver); + if (ver != 81) return 0; + //Serial.println("chip is W5100S"); + return 1; +} + uint8_t W5100Class::isW5200(void) { chip = 52; @@ -275,6 +314,12 @@ W5100Linkstatus W5100Class::getLinkStatus() if (!init()) return UNKNOWN; switch (chip) { + case 50: + SPI.beginTransaction(SPI_ETHERNET_SETTINGS); + phystatus = readPHYCFGR_W5100S(); + SPI.endTransaction(); + if (phystatus & 0x01) return LINK_ON; + return LINK_OFF; case 52: SPI.beginTransaction(SPI_ETHERNET_SETTINGS); phystatus = readPSTATUS_W5200(); @@ -286,7 +331,7 @@ W5100Linkstatus W5100Class::getLinkStatus() phystatus = readPHYCFGR_W5500(); SPI.endTransaction(); if (phystatus & 0x01) return LINK_ON; - return LINK_OFF; + return LINK_OFF; default: return UNKNOWN; } @@ -296,7 +341,7 @@ uint16_t W5100Class::write(uint16_t addr, const uint8_t *buf, uint16_t len) { uint8_t cmd[8]; - if (chip == 51) { + if (chip == 51 || chip == 50) { for (uint16_t i=0; i Date: Fri, 4 Jan 2019 09:34:39 +0900 Subject: [PATCH 02/14] For Arduino MKR Vidor-4000 --- src/utility/w5100.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utility/w5100.cpp b/src/utility/w5100.cpp index c8ac6895..8d35e910 100644 --- a/src/utility/w5100.cpp +++ b/src/utility/w5100.cpp @@ -24,7 +24,7 @@ // MKR boards default to pin 5 for MKR ETH // Pins 8-10 are MOSI/SCK/MISO on MRK, so don't use pin 10 -#elif defined(USE_ARDUINO_MKR_PIN_LAYOUT) || defined(ARDUINO_SAMD_MKRZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRFox1200) || defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRWAN1300) +#elif defined(USE_ARDUINO_MKR_PIN_LAYOUT) || defined(ARDUINO_SAMD_MKRZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRFox1200) || defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRVIDOR4000) #define SS_PIN_DEFAULT 5 // For boards using AVR, assume shields with SS on pin 10 From 4e87244632e32260b7828d47e0d5489f08ab8442 Mon Sep 17 00:00:00 2001 From: "Taylor.An" <44388468+taylor-an@users.noreply.github.com> Date: Mon, 14 Jan 2019 16:53:47 +0900 Subject: [PATCH 03/14] W6100 IPv4 (#6) The W6100 IPv4 Arduino Example.(For W6100 Ethernet Shield) --- .../AdvancedChatServer/AdvancedChatServer.ino | 9 + .../BarometricPressureWebServer.ino | 6 + examples/ChatServer/ChatServer.ino | 13 +- examples/DhcpChatServer/DhcpChatServer.ino | 24 + examples/TelnetClient/TelnetClient.ino | 9 + .../UDPSendReceiveString.ino | 9 + examples/UdpNtpClient/UdpNtpClient.ino | 12 + examples/WebClient/WebClient.ino | 6 + .../WebClientRepeating/WebClientRepeating.ino | 6 + examples/WebServer/WebServer.ino | 6 + keywords.txt | 1 + src/Dhcp.cpp | 119 +++++ src/Dns.cpp | 41 ++ src/Ethernet.cpp | 40 ++ src/Ethernet.h | 76 ++- src/EthernetUdp.cpp | 226 ++++++++- src/socket.cpp | 100 ++++ src/utility/w5100.cpp | 439 +++++++++++++++++- src/utility/w5100.h | 283 ++++++++--- 19 files changed, 1331 insertions(+), 94 deletions(-) diff --git a/examples/AdvancedChatServer/AdvancedChatServer.ino b/examples/AdvancedChatServer/AdvancedChatServer.ino index 3b431c88..3ff3e716 100644 --- a/examples/AdvancedChatServer/AdvancedChatServer.ino +++ b/examples/AdvancedChatServer/AdvancedChatServer.ino @@ -22,16 +22,25 @@ #include #include +#define ON_TAYLOR_WORK_PC + // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network. // gateway and subnet are optional: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; +#if defined ON_TAYLOR_WORK_PC +IPAddress ip(192, 168, 0, 3); +IPAddress myDns(192, 168, 0, 1); +IPAddress gateway(192, 168, 0, 1); +IPAddress subnet(255, 255, 0, 0); +#else IPAddress ip(192, 168, 1, 177); IPAddress myDns(192, 168, 1, 1); IPAddress gateway(192, 168, 1, 1); IPAddress subnet(255, 255, 0, 0); +#endif // telnet defaults to port 23 diff --git a/examples/BarometricPressureWebServer/BarometricPressureWebServer.ino b/examples/BarometricPressureWebServer/BarometricPressureWebServer.ino index 56ccc66c..d632a280 100644 --- a/examples/BarometricPressureWebServer/BarometricPressureWebServer.ino +++ b/examples/BarometricPressureWebServer/BarometricPressureWebServer.ino @@ -27,14 +27,20 @@ // the sensor communicates using SPI, so include the library: #include +#define ON_TAYLOR_WORK_PC // assign a MAC address for the Ethernet controller. // fill in your address here: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; +#if defined ON_TAYLOR_WORK_PC +IPAddress ip(192, 168, 0, 3); +#else // assign an IP address for the controller: IPAddress ip(192, 168, 1, 20); +#endif + // Initialize the Ethernet server library diff --git a/examples/ChatServer/ChatServer.ino b/examples/ChatServer/ChatServer.ino index 740d7867..6fd92fe7 100644 --- a/examples/ChatServer/ChatServer.ino +++ b/examples/ChatServer/ChatServer.ino @@ -19,16 +19,27 @@ #include #include +#define ON_TAYLOR_WORK_PC +//#define DEBUG_CHATSERVER_INO_ETHERNET_BEGIN + // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network. // gateway and subnet are optional: +#if defined ON_TAYLOR_WORK_PC +byte mac[] = { +0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; +IPAddress ip(192, 168, 0, 3); +IPAddress myDns(192, 168, 0, 1); +IPAddress gateway(192, 168, 0, 1); +IPAddress subnet(255, 255, 0, 0); +#else byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192, 168, 1, 177); IPAddress myDns(192, 168, 1, 1); IPAddress gateway(192, 168, 1, 1); IPAddress subnet(255, 255, 0, 0); - +#endif // telnet defaults to port 23 EthernetServer server(23); diff --git a/examples/DhcpChatServer/DhcpChatServer.ino b/examples/DhcpChatServer/DhcpChatServer.ino index f4489d61..472eb85c 100644 --- a/examples/DhcpChatServer/DhcpChatServer.ino +++ b/examples/DhcpChatServer/DhcpChatServer.ino @@ -23,9 +23,20 @@ #include #include +#define ON_TAYLOR_WORK_PC +//#define DEBUG_DHCPCHATSERVER_INO_SETUP + // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network. // gateway and subnet are optional: +#ifdef ON_TAYLOR_WORK_PC +byte mac[] = { + 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; +IPAddress ip(192, 168, 0, 3); +IPAddress myDns(192, 168, 0, 1); +IPAddress gateway(192, 168, 0, 1); +IPAddress subnet(255, 255, 0, 0); +#else byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 }; @@ -33,6 +44,7 @@ IPAddress ip(192, 168, 1, 177); IPAddress myDns(192, 168, 1, 1); IPAddress gateway(192, 168, 1, 1); IPAddress subnet(255, 255, 0, 0); +#endif // telnet defaults to port 23 EthernetServer server(23); @@ -55,7 +67,14 @@ void setup() { // start the Ethernet connection: Serial.println("Trying to get an IP address using DHCP"); + #if defined DEBUG_DHCPCHATSERVER_INO_SETUP + PRINTSTR("Ethernet.begin(mac)"); + #endif if (Ethernet.begin(mac) == 0) { + #if defined DEBUG_DHCPCHATSERVER_INO_SETUP + PRINTSTR("Ethernet.begin(mac) == 0"); + #endif + Serial.println("Failed to configure Ethernet using DHCP"); // Check for Ethernet hardware present if (Ethernet.hardwareStatus() == EthernetNoHardware) { @@ -70,6 +89,11 @@ void setup() { // initialize the Ethernet device not using DHCP: Ethernet.begin(mac, ip, myDns, gateway, subnet); } + + #if defined DEBUG_DHCPCHATSERVER_INO_SETUP + PRINTLINE(); + #endif + // print your local IP address: Serial.print("My IP address: "); Serial.println(Ethernet.localIP()); diff --git a/examples/TelnetClient/TelnetClient.ino b/examples/TelnetClient/TelnetClient.ino index 85386b5f..34144fab 100644 --- a/examples/TelnetClient/TelnetClient.ino +++ b/examples/TelnetClient/TelnetClient.ino @@ -20,15 +20,24 @@ #include #include +#define ON_TAYLOR_WORK_PC + // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; +#if defined ON_TAYLOR_WORK_PC +IPAddress ip(192, 168, 0, 3); + +// Enter the IP address of the server you're connecting to: +IPAddress server(192, 168, 0, 2); +#else IPAddress ip(192, 168, 1, 177); // Enter the IP address of the server you're connecting to: IPAddress server(1, 1, 1, 1); +#endif // Initialize the Ethernet client library // with the IP address and port of the server diff --git a/examples/UDPSendReceiveString/UDPSendReceiveString.ino b/examples/UDPSendReceiveString/UDPSendReceiveString.ino index 0c76b022..b8b557ef 100644 --- a/examples/UDPSendReceiveString/UDPSendReceiveString.ino +++ b/examples/UDPSendReceiveString/UDPSendReceiveString.ino @@ -16,12 +16,18 @@ #include #include +#define ON_TAYLOR_WORK_PC + // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; +#if defined ON_TAYLOR_WORK_PC +IPAddress ip(192, 168, 0, 3); +#else IPAddress ip(192, 168, 1, 177); +#endif unsigned int localPort = 8888; // local port to listen on @@ -63,6 +69,9 @@ void setup() { // start UDP Udp.begin(localPort); + + Serial.print("Udp server address:"); + Serial.println(Ethernet.localIP()); } void loop() { diff --git a/examples/UdpNtpClient/UdpNtpClient.ino b/examples/UdpNtpClient/UdpNtpClient.ino index ec52717d..5eca1439 100644 --- a/examples/UdpNtpClient/UdpNtpClient.ino +++ b/examples/UdpNtpClient/UdpNtpClient.ino @@ -22,6 +22,9 @@ #include #include +#define TIMESERVER 2 +//#define DEBUG_UDPNTPCLIENT_INO_SENDNTPPACKET + // Enter a MAC address for your controller below. // Newer Ethernet shields have a MAC address printed on a sticker on the shield byte mac[] = { @@ -30,7 +33,11 @@ byte mac[] = { unsigned int localPort = 8888; // local port to listen for UDP packets +#if TIMESERVER == 1 const char timeServer[] = "time.nist.gov"; // time.nist.gov NTP server +#elif TIMESERVER == 2 +const char timeServer[] = "time.bora.net"; // time.bora.net NTP server +#endif const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message @@ -124,6 +131,11 @@ void loop() { // send an NTP request to the time server at the given address void sendNTPpacket(const char * address) { + + #if defined DEBUG_UDPNTPCLIENT_INO_SENDNTPPACKET + PRINTLINE(); + #endif + // set all bytes in the buffer to 0 memset(packetBuffer, 0, NTP_PACKET_SIZE); // Initialize values needed to form NTP request diff --git a/examples/WebClient/WebClient.ino b/examples/WebClient/WebClient.ino index 45e01eed..ebae18f4 100644 --- a/examples/WebClient/WebClient.ino +++ b/examples/WebClient/WebClient.ino @@ -17,6 +17,8 @@ #include #include +#define ON_TAYLOR_WORK_PC + // Enter a MAC address for your controller below. // Newer Ethernet shields have a MAC address printed on a sticker on the shield byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; @@ -27,7 +29,11 @@ byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; char server[] = "www.google.com"; // name address for Google (using DNS) // Set the static IP address to use if the DHCP fails to assign +#if defined ON_TAYLOR_WORK_PC +IPAddress ip(192, 168, 0, 3); +#else IPAddress ip(192, 168, 0, 177); +#endif IPAddress myDns(192, 168, 0, 1); // Initialize the Ethernet client library diff --git a/examples/WebClientRepeating/WebClientRepeating.ino b/examples/WebClientRepeating/WebClientRepeating.ino index 6607f438..f357632e 100644 --- a/examples/WebClientRepeating/WebClientRepeating.ino +++ b/examples/WebClientRepeating/WebClientRepeating.ino @@ -25,13 +25,19 @@ #include #include +#define ON_TAYLOR_WORK_PC + // assign a MAC address for the ethernet controller. // fill in your address here: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // Set the static IP address to use if the DHCP fails to assign +#if defined ON_TAYLOR_WORK_PC +IPAddress ip(192, 168, 0, 3); +#else IPAddress ip(192, 168, 0, 177); +#endif IPAddress myDns(192, 168, 0, 1); // initialize the library instance: diff --git a/examples/WebServer/WebServer.ino b/examples/WebServer/WebServer.ino index 06a85576..f9f66dd9 100644 --- a/examples/WebServer/WebServer.ino +++ b/examples/WebServer/WebServer.ino @@ -20,12 +20,18 @@ #include #include +#define ON_TAYLOR_WORK_PC + // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; +#if defined ON_TAYLOR_WORK_PC +IPAddress ip(192, 168, 0, 3); +#else IPAddress ip(192, 168, 1, 177); +#endif // Initialize the Ethernet server library // with the IP address and port you want to use diff --git a/keywords.txt b/keywords.txt index 0013e20a..9eaf4875 100644 --- a/keywords.txt +++ b/keywords.txt @@ -66,3 +66,4 @@ EthernetW5100 LITERAL1 EthernetW5200 LITERAL1 EthernetW5500 LITERAL1 EthernetW5100S LITERAL1 +EthernetW6100 LITERAL1 diff --git a/src/Dhcp.cpp b/src/Dhcp.cpp index 2bfd584b..ddc68289 100644 --- a/src/Dhcp.cpp +++ b/src/Dhcp.cpp @@ -6,6 +6,11 @@ #include "Dhcp.h" #include "utility/w5100.h" +//#define DEBUG_DHCP_CPP_BEGINWITHDHCP +//#define DEBUG_DHCP_CPP_DHCP_LEASE +//#define DEBUG_DHCP_CPP_DHCP_MESSAGE +//#define DEBUG_DHCP_CPP_DHCPRESPONSE + int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout) { _dhcpLeaseTime=0; @@ -15,9 +20,18 @@ int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long _responseTimeout = responseTimeout; // zero out _dhcpMacAddr + #if defined DEBUG_ETHERNET_CPP_BEGIN + PRINTLINE_DEF(DEBUG_ETHERNET_CPP_BEGIN); + #endif memset(_dhcpMacAddr, 0, 6); + #if defined DEBUG_ETHERNET_CPP_BEGIN + PRINTLINE_DEF(DEBUG_ETHERNET_CPP_BEGIN); + #endif reset_DHCP_lease(); + #if defined DEBUG_ETHERNET_CPP_BEGIN + PRINTLINE_DEF(DEBUG_ETHERNET_CPP_BEGIN); + #endif memcpy((void*)_dhcpMacAddr, (void*)mac, 6); _dhcp_state = STATE_DHCP_START; return request_DHCP_lease(); @@ -34,23 +48,52 @@ int DhcpClass::request_DHCP_lease() { uint8_t messageType = 0; + #if defined DEBUG_DHCP_CPP_DHCP_LEASE + PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_LEASE); + #endif + // Pick an initial transaction ID _dhcpTransactionId = random(1UL, 2000UL); _dhcpInitialTransactionId = _dhcpTransactionId; + #if defined DEBUG_DHCP_CPP_DHCP_LEASE + PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_LEASE); + #endif + _dhcpUdpSocket.stop(); + + #if defined DEBUG_DHCP_CPP_DHCP_LEASE + PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_LEASE); + #endif + if (_dhcpUdpSocket.begin(DHCP_CLIENT_PORT) == 0) { // Couldn't get a socket return 0; } + #if defined DEBUG_DHCP_CPP_DHCP_LEASE + PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_LEASE); + #endif + presend_DHCP(); + #if defined DEBUG_DHCP_CPP_DHCP_LEASE + PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_LEASE); + #endif + int result = 0; unsigned long startTime = millis(); + #if defined DEBUG_DHCP_CPP_DHCP_LEASE + PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_LEASE); + #endif + while (_dhcp_state != STATE_DHCP_LEASED) { + #if defined DEBUG_DHCP_CPP_DHCP_LEASE + PRINTVAR(_dhcp_state); + #endif + if (_dhcp_state == STATE_DHCP_START) { _dhcpTransactionId++; send_DHCP_MESSAGE(DHCP_DISCOVER, ((millis() - startTime) / 1000)); @@ -104,6 +147,10 @@ int DhcpClass::request_DHCP_lease() break; } + #if defined DEBUG_DHCP_CPP_DHCP_LEASE + PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_LEASE); + #endif + // We're done with the socket now _dhcpUdpSocket.stop(); _dhcpTransactionId++; @@ -122,6 +169,10 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed) memset(buffer, 0, 32); IPAddress dest_addr(255, 255, 255, 255); // Broadcast address + #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE + PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); + #endif + if (_dhcpUdpSocket.beginPacket(dest_addr, DHCP_SERVER_PORT) == -1) { //Serial.printf("DHCP transmit error\n"); // FIXME Need to return errors @@ -133,6 +184,10 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed) buffer[2] = DHCP_HLENETHERNET; // hlen buffer[3] = DHCP_HOPS; // hops + #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE + PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); + #endif + // xid unsigned long xid = htonl(_dhcpTransactionId); memcpy(buffer + 4, &(xid), 4); @@ -141,6 +196,10 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed) buffer[8] = ((secondsElapsed & 0xff00) >> 8); buffer[9] = (secondsElapsed & 0x00ff); + #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE + PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); + #endif + // flags unsigned short flags = htons(DHCP_FLAGSBROADCAST); memcpy(buffer + 10, &(flags), 2); @@ -150,21 +209,49 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed) // siaddr: already zeroed // giaddr: already zeroed + #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE + PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); + #endif + //put data in W5100 transmit buffer _dhcpUdpSocket.write(buffer, 28); + #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE + PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); + #endif + memset(buffer, 0, 32); // clear local buffer + #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE + PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); + #endif + memcpy(buffer, _dhcpMacAddr, 6); // chaddr + #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE + PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); + #endif + //put data in W5100 transmit buffer _dhcpUdpSocket.write(buffer, 16); + #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE + PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); + #endif + memset(buffer, 0, 32); // clear local buffer + #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE + PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); + #endif + // leave zeroed out for sname && file // put in W5100 transmit buffer x 6 (192 bytes) + #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE + PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); + #endif + for(int i = 0; i < 6; i++) { _dhcpUdpSocket.write(buffer, 32); } @@ -191,10 +278,18 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed) buffer[17] = strlen(HOST_NAME) + 6; // length of hostname + last 3 bytes of mac address strcpy((char*)&(buffer[18]), HOST_NAME); + #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE + PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); + #endif + printByte((char*)&(buffer[24]), _dhcpMacAddr[3]); printByte((char*)&(buffer[26]), _dhcpMacAddr[4]); printByte((char*)&(buffer[28]), _dhcpMacAddr[5]); + #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE + PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); + #endif + //put data in W5100 transmit buffer _dhcpUdpSocket.write(buffer, 30); @@ -227,9 +322,17 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed) buffer[7] = dhcpT2value; buffer[8] = endOption; + #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE + PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); + #endif + //put data in W5100 transmit buffer _dhcpUdpSocket.write(buffer, 9); + #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE + PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); + #endif + _dhcpUdpSocket.endPacket(); } @@ -240,16 +343,28 @@ uint8_t DhcpClass::parseDHCPResponse(unsigned long responseTimeout, uint32_t& tr unsigned long startTime = millis(); + #if defined DEBUG_DHCP_CPP_DHCPRESPONSE + PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCPRESPONSE); + #endif + while (_dhcpUdpSocket.parsePacket() <= 0) { if ((millis() - startTime) > responseTimeout) { return 255; } delay(50); } + + #if defined DEBUG_DHCP_CPP_DHCPRESPONSE + PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCPRESPONSE); + #endif // start reading in the packet RIP_MSG_FIXED fixedMsg; _dhcpUdpSocket.read((uint8_t*)&fixedMsg, sizeof(RIP_MSG_FIXED)); + #if defined DEBUG_DHCP_CPP_DHCPRESPONSE + PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCPRESPONSE); + #endif + if (fixedMsg.op == DHCP_BOOTREPLY && _dhcpUdpSocket.remotePort() == DHCP_SERVER_PORT) { transactionId = ntohl(fixedMsg.xid); if (memcmp(fixedMsg.chaddr, _dhcpMacAddr, 6) != 0 || @@ -334,6 +449,10 @@ uint8_t DhcpClass::parseDHCPResponse(unsigned long responseTimeout, uint32_t& tr } } + #if defined DEBUG_DHCP_CPP_DHCPRESPONSE + PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCPRESPONSE); + #endif + // Need to skip to end of the packet regardless here _dhcpUdpSocket.flush(); // FIXME diff --git a/src/Dns.cpp b/src/Dns.cpp index dfa91125..19c310ef 100644 --- a/src/Dns.cpp +++ b/src/Dns.cpp @@ -7,6 +7,7 @@ #include "Dns.h" #include "utility/w5100.h" +//#define DEBUG_W5100_CPP_GETHOSTBYNAME #define SOCKET_NONE 255 // Various flags and header field values for a DNS message @@ -87,6 +88,10 @@ int DNSClient::inet_aton(const char* address, IPAddress& result) int DNSClient::getHostByName(const char* aHostname, IPAddress& aResult, uint16_t timeout) { + #if defined DEBUG_W5100_CPP_GETHOSTBYNAME + PRINTLINE(); + #endif + int ret = 0; // See if it's a numeric IP address @@ -95,28 +100,56 @@ int DNSClient::getHostByName(const char* aHostname, IPAddress& aResult, uint16_t return 1; } + #if defined DEBUG_W5100_CPP_GETHOSTBYNAME + PRINTLINE(); + #endif + // Check we've got a valid DNS server to use if (iDNSServer == INADDR_NONE) { return INVALID_SERVER; } + #if defined DEBUG_W5100_CPP_GETHOSTBYNAME + PRINTLINE(); + #endif + // Find a socket to use if (iUdp.begin(1024+(millis() & 0xF)) == 1) { // Try up to three times int retries = 0; // while ((retries < 3) && (ret <= 0)) { // Send DNS request + + #if defined DEBUG_W5100_CPP_GETHOSTBYNAME + PRINTLINE(); + #endif + ret = iUdp.beginPacket(iDNSServer, DNS_PORT); if (ret != 0) { // Now output the request data + + #if defined DEBUG_W5100_CPP_GETHOSTBYNAME + PRINTLINE(); + #endif + ret = BuildRequest(aHostname); if (ret != 0) { // And finally send the request + + #if defined DEBUG_W5100_CPP_GETHOSTBYNAME + PRINTLINE(); + #endif + ret = iUdp.endPacket(); if (ret != 0) { // Now wait for a response int wait_retries = 0; ret = TIMED_OUT; + + #if defined DEBUG_W5100_CPP_GETHOSTBYNAME + PRINTLINE(); + #endif + while ((wait_retries < 3) && (ret == TIMED_OUT)) { ret = ProcessResponse(timeout, aResult); wait_retries++; @@ -127,10 +160,18 @@ int DNSClient::getHostByName(const char* aHostname, IPAddress& aResult, uint16_t retries++; //} + #if defined DEBUG_W5100_CPP_GETHOSTBYNAME + PRINTLINE(); + #endif + // We're done with the socket now iUdp.stop(); } + #if defined DEBUG_W5100_CPP_GETHOSTBYNAME + PRINTLINE(); + #endif + return ret; } diff --git a/src/Ethernet.cpp b/src/Ethernet.cpp index 28658623..f7655d68 100644 --- a/src/Ethernet.cpp +++ b/src/Ethernet.cpp @@ -23,6 +23,8 @@ #include "utility/w5100.h" #include "Dhcp.h" +//#define DEBUG_ETHERNET_CPP_BEGIN + IPAddress EthernetClass::_dnsServerAddress; DhcpClass* EthernetClass::_dhcp = NULL; @@ -34,23 +36,60 @@ int EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long resp // Initialise the basic info if (W5100.init() == 0) return 0; SPI.beginTransaction(SPI_ETHERNET_SETTINGS); + + #if defined DEBUG_ETHERNET_CPP_BEGIN + PRINTLINE(); + #endif W5100.setMACAddress(mac); + + #if defined DEBUG_ETHERNET_CPP_BEGIN + PRINTLINE(); + #endif W5100.setIPAddress(IPAddress(0,0,0,0).raw_address()); SPI.endTransaction(); + #if defined DEBUG_ETHERNET_CPP_BEGIN + PRINTLINE(); + #endif // Now try to get our config info from a DHCP server int ret = _dhcp->beginWithDHCP(mac, timeout, responseTimeout); if (ret == 1) { // We've successfully found a DHCP server and got our configuration // info, so set things accordingly SPI.beginTransaction(SPI_ETHERNET_SETTINGS); + + #if defined DEBUG_ETHERNET_CPP_BEGIN + PRINTLINE(); + #endif W5100.setIPAddress(_dhcp->getLocalIp().raw_address()); + #if defined DEBUG_ETHERNET_CPP_BEGIN + PRINTLINE(); + #endif W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address()); + #if defined DEBUG_ETHERNET_CPP_BEGIN + PRINTLINE(); + #endif W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address()); + #if defined DEBUG_ETHERNET_CPP_BEGIN + PRINTLINE(); + #endif SPI.endTransaction(); + #if defined DEBUG_ETHERNET_CPP_BEGIN + PRINTLINE(); + #endif _dnsServerAddress = _dhcp->getDnsServerIp(); + #if defined DEBUG_ETHERNET_CPP_BEGIN + PRINTLINE(); + #endif socketPortRand(micros()); + #if defined DEBUG_ETHERNET_CPP_BEGIN + PRINTLINE(); + #endif } + + #if defined DEBUG_ETHERNET_CPP_BEGIN + PRINTLINE(); + #endif return ret; } @@ -118,6 +157,7 @@ EthernetHardwareStatus EthernetClass::hardwareStatus() case 51: return EthernetW5100; case 52: return EthernetW5200; case 55: return EthernetW5500; + case 61: return EthernetW6100; default: return EthernetNoHardware; } } diff --git a/src/Ethernet.h b/src/Ethernet.h index dbc9a63d..969565be 100644 --- a/src/Ethernet.h +++ b/src/Ethernet.h @@ -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 #include "Client.h" @@ -64,7 +137,8 @@ enum EthernetHardwareStatus { EthernetW5100, EthernetW5200, EthernetW5500, - EthernetW5100S + EthernetW5100S, + EthernetW6100 }; class EthernetUDP; diff --git a/src/EthernetUdp.cpp b/src/EthernetUdp.cpp index 51669d69..fa91b958 100644 --- a/src/EthernetUdp.cpp +++ b/src/EthernetUdp.cpp @@ -31,12 +31,31 @@ #include "Dns.h" #include "utility/w5100.h" +//#define DEBUG_ETHERNET_UDP_CPP_PARSEPACKET +//#define DEBUG_ETHERNET_UDP_READ +//#define DEBUG_ETHERNETUDP_CPP_WRITE +//#define DEBUG_ETHERNET_UDP_CPP_BEGINPACKET +//#define DEBUG_ETHERNET_UDP_CPP_BEGIN + /* Start EthernetUDP socket, listening at local port PORT */ uint8_t EthernetUDP::begin(uint16_t port) { + #if defined DEBUG_ETHERNET_UDP_CPP_BEGIN + PRINTLINE(); + #endif if (sockindex < MAX_SOCK_NUM) Ethernet.socketClose(sockindex); + #if defined DEBUG_ETHERNET_UDP_CPP_BEGIN + PRINTLINE(); + #endif + sockindex = Ethernet.socketBegin(SnMR::UDP, port); - if (sockindex >= MAX_SOCK_NUM) return 0; + #if defined DEBUG_ETHERNET_UDP_CPP_BEGIN + PRINTLINE(); + PRINTVAR(sockindex); + #endif + + + if (sockindex >= MAX_SOCK_NUM) return 0; _port = port; _remaining = 0; return 1; @@ -60,19 +79,46 @@ void EthernetUDP::stop() int EthernetUDP::beginPacket(const char *host, uint16_t port) { + #if defined DEBUG_ETHERNET_UDP_CPP_BEGINPACKET + PRINTLINE(); + #endif + // Look up the host first int ret = 0; DNSClient dns; IPAddress remote_addr; + #if defined DEBUG_ETHERNET_UDP_CPP_BEGINPACKET + PRINTVAR_HEX(Ethernet.dnsServerIP()); + #endif + dns.begin(Ethernet.dnsServerIP()); + + #if defined DEBUG_ETHERNET_UDP_CPP_BEGINPACKET + PRINTLINE(); + #endif + ret = dns.getHostByName(host, remote_addr); + + #if defined DEBUG_ETHERNET_UDP_CPP_BEGINPACKET + PRINTLINE(); + #endif + if (ret != 1) return ret; + + #if defined DEBUG_ETHERNET_UDP_CPP_BEGINPACKET + PRINTLINE(); + #endif + return beginPacket(remote_addr, port); } int EthernetUDP::beginPacket(IPAddress ip, uint16_t port) { + #if defined DEBUG_ETHERNET_UDP_CPP_BEGINPACKET + PRINTLINE(); + #endif + _offset = 0; //Serial.printf("UDP beginPacket\n"); return Ethernet.socketStartUDP(sockindex, rawIPAddress(ip), port); @@ -90,9 +136,18 @@ size_t EthernetUDP::write(uint8_t byte) size_t EthernetUDP::write(const uint8_t *buffer, size_t size) { + #if defined DEBUG_ETHERNETUDP_CPP_WRITE + PRINTLINE(); + #endif + //Serial.printf("UDP write %d\n", size); uint16_t bytes_written = Ethernet.socketBufferData(sockindex, _offset, buffer, size); _offset += bytes_written; + + #if defined DEBUG_ETHERNETUDP_CPP_WRITE + PRINTLINE(); + #endif + return bytes_written; } @@ -108,22 +163,129 @@ 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; + int i; + + #if defined DEBUG_ETHERNET_UDP_CPP_PARSEPACKET + PRINTVAR_HEX(_remaining); + PRINTLINE_DEF(DEBUG_ETHERNET_UDP_CPP_PARSEPACKET); + #endif + + if(W5100.getChip() == 61) + { + //read 2 header bytes and get one IPv4 or IPv6 + ret = Ethernet.socketRecv(sockindex, tmpBuf, 2); + if(ret > 0) + { + #if defined DEBUG_ETHERNET_UDP_CPP_PARSEPACKET + for(i = 0; i < 2; i++) + { + PRINTVAR_HEX(tmpBuf[0+i]); + } + PRINTVAR(ret); + #endif + + _remaining = (tmpBuf[0] & (0x7))<<8 | tmpBuf[1]; + #if defined DEBUG_ETHERNET_UDP_CPP_PARSEPACKET + PRINTVAR(_remaining); + #endif + + if((tmpBuf[0] & W6100_UDP_HEADER_IPV) == W6100_UDP_HEADER_IPV6) + { + // IPv6 UDP Recived + // 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); + #if defined DEBUG_ETHERNET_UDP_CPP_PARSEPACKET + for(i = 0; i < 18; i++) + { + PRINTVAR_HEX(tmpBuf[2+i]); + } + PRINTVAR(ret); + #endif + + _remoteIP = &tmpBuf[2]; + #if defined DEBUG_ETHERNET_UDP_CPP_PARSEPACKET + for(i = 0; i < 16; i++) + { + PRINTVAR(tmpBuf[2+i]); + } + #endif + + _remotePort = (tmpBuf[18]<<8) | tmpBuf[19]; + #if defined DEBUG_ETHERNET_UDP_CPP_PARSEPACKET + PRINTVAR(_remotePort); + #endif + } + else + { + // IPv4 UDP Recived + // 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); + #if defined DEBUG_ETHERNET_UDP_CPP_PARSEPACKET + for(i = 0; i < 6; i++) + { + PRINTVAR_HEX(tmpBuf[2+i]); + } + PRINTVAR(ret); + #endif + + _remoteIP = &tmpBuf[2]; + #if defined DEBUG_ETHERNET_UDP_CPP_PARSEPACKET + for(i = 0; i < 4; i++) + { + PRINTVAR(tmpBuf[2+i]); + } + #endif + + _remotePort = (tmpBuf[6]<<8) | tmpBuf[7]; + #if defined DEBUG_ETHERNET_UDP_CPP_PARSEPACKET + PRINTVAR(_remotePort); + #endif + } + + 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; + } } + + #if defined DEBUG_ETHERNET_UDP_CPP_PARSEPACKET + PRINTLINE_DEF(DEBUG_ETHERNET_UDP_CPP_PARSEPACKET); + #endif + return ret; } + + #if defined DEBUG_ETHERNET_UDP_CPP_PARSEPACKET + PRINTLINE_DEF(DEBUG_ETHERNET_UDP_CPP_PARSEPACKET); + #endif + // There aren't any packets available return 0; } @@ -132,12 +294,30 @@ int EthernetUDP::read() { uint8_t byte; + #if defined DEBUG_ETHERNET_UDP_READ + PRINTLINE_DEF(DEBUG_ETHERNET_UDP_READ); + #endif + if ((_remaining > 0) && (Ethernet.socketRecv(sockindex, &byte, 1) > 0)) { + + #if defined DEBUG_ETHERNET_UDP_READ + PRINTVAR(_remaining); + #endif + // We read things without any problems _remaining--; + + #if defined DEBUG_ETHERNET_UDP_READ + PRINTVAR(_remaining); + #endif + return byte; } + #if defined DEBUG_ETHERNET_UDP_READ + PRINTLINE_DEF(DEBUG_ETHERNET_UDP_READ); + #endif + // If we get here, there's no data available return -1; } @@ -146,17 +326,37 @@ int EthernetUDP::read(unsigned char *buffer, size_t len) { if (_remaining > 0) { int got; + + #if defined DEBUG_ETHERNET_UDP_READ + PRINTVAR_HEX(len); + PRINTVAR_HEX(_remaining); + #endif + if (_remaining <= len) { // data should fit in the buffer got = Ethernet.socketRecv(sockindex, buffer, _remaining); + + #if defined DEBUG_ETHERNET_UDP_READ + PRINTVAR_HEX(got); + #endif + } else { // too much data for the buffer, // grab as much as will fit got = Ethernet.socketRecv(sockindex, buffer, len); + + #if defined DEBUG_ETHERNET_UDP_READ + PRINTVAR_HEX(got); + #endif } if (got > 0) { _remaining -= got; //Serial.printf("UDP read %d\n", got); + + #if defined DEBUG_ETHERNET_UDP_READ + PRINTVAR_HEX(got); + #endif + return got; } } diff --git a/src/socket.cpp b/src/socket.cpp index 43f068ef..156c2041 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -22,6 +22,13 @@ #include "Ethernet.h" #include "utility/w5100.h" +//#define DEBUG_SOCKET_CPP_WRITE_DATA +//#define DEBUG_SOCKET_CPP_SOCKETLISTEN +//#define DEBUG_SOCKET_CPP_SOCKETRECV +//#define DEBUG_SOCKET_CPP_SOCKETSENDUDP +//#define DEBUG_ETHERNET_UDP_CPP_SOCKETSTARTUDP +//#define DEBUG_SOCKET_CPP_BEGINE + #if ARDUINO >= 156 && !defined(ARDUINO_ARCH_PIC32) extern void yield(void); #else @@ -75,6 +82,11 @@ uint8_t EthernetClass::socketBegin(uint8_t protocol, uint16_t port) // look at all the hardware sockets, use any that are closed (unused) for (s=0; s < maxindex; s++) { status[s] = W5100.readSnSR(s); + + #if defined DEBUG_SOCKET_CPP_BEGINE + PRINTVAR_HEX(status[s]); + #endif + if (status[s] == SnSR::CLOSED) goto makesocket; } //Serial.printf("W5000socket step2\n"); @@ -305,9 +317,17 @@ int EthernetClass::socketRecv(uint8_t s, uint8_t *buf, int16_t len) { // Check how much data is available int ret = state[s].RX_RSR; + + #if defined DEBUG_SOCKET_CPP_SOCKETRECV + PRINTVAR_HEX(ret); + #endif + SPI.beginTransaction(SPI_ETHERNET_SETTINGS); if (ret < len) { uint16_t rsr = getSnRX_RSR(s); + #if defined DEBUG_SOCKET_CPP_SOCKETRECV + PRINTVAR_HEX(rsr); + #endif ret = rsr - state[s].RX_inc; state[s].RX_RSR = ret; //Serial.printf("Sock_RECV, RX_RSR=%d, RX_inc=%d\n", ret, state[s].RX_inc); @@ -315,6 +335,9 @@ int EthernetClass::socketRecv(uint8_t s, uint8_t *buf, int16_t len) if (ret == 0) { // No data available. uint8_t status = W5100.readSnSR(s); + #if defined DEBUG_SOCKET_CPP_SOCKETRECV + PRINTVAR_HEX(status); + #endif if ( status == SnSR::LISTEN || status == SnSR::CLOSED || status == SnSR::CLOSE_WAIT ) { // The remote end has closed its side of the connection, @@ -324,6 +347,9 @@ int EthernetClass::socketRecv(uint8_t s, uint8_t *buf, int16_t len) // The connection is still up, but there's no data waiting to be read ret = -1; } + #if defined DEBUG_SOCKET_CPP_SOCKETRECV + PRINTVAR_HEX(ret); + #endif } else { if (ret > len) ret = len; // more data available than buffer length uint16_t ptr = state[s].RX_RD; @@ -333,6 +359,9 @@ int EthernetClass::socketRecv(uint8_t s, uint8_t *buf, int16_t len) state[s].RX_RSR -= ret; uint16_t inc = state[s].RX_inc + ret; if (inc >= 250 || state[s].RX_RSR == 0) { + #if defined DEBUG_SOCKET_CPP_SOCKETRECV + PRINTVAR_HEX(inc); + #endif state[s].RX_inc = 0; W5100.writeSnRX_RD(s, ptr); W5100.execCmdSn(s, Sock_RECV); @@ -397,12 +426,33 @@ static uint16_t getSnTX_FSR(uint8_t s) static void write_data(uint8_t s, uint16_t data_offset, const uint8_t *data, uint16_t len) { + #if defined DEBUG_SOCKET_CPP_WRITE_DATA + PRINTSTR("write_data"); + #endif + uint16_t ptr = W5100.readSnTX_WR(s); + #if defined DEBUG_SOCKET_CPP_WRITE_DATA + PRINTVAR_HEX(ptr); + PRINTVAR_HEX(data_offset); + #endif ptr += data_offset; uint16_t offset = ptr & W5100.SMASK; uint16_t dstAddr = offset + W5100.SBASE(s); + #if defined DEBUG_SOCKET_CPP_WRITE_DATA + PRINTVAR_HEX(dstAddr); + PRINTVAR_HEX(W5100.SBASE(s)); + #endif + + #if defined DEBUG_SOCKET_CPP_WRITE_DATA + PRINTVAR_HEX(offset); + PRINTVAR_HEXT(s, data_offset, len); + #endif + if (W5100.hasOffsetAddressMapping() || offset + len <= W5100.SSIZE) { + #if defined DEBUG_SOCKET_CPP_WRITE_DATA + PRINTVAR_HEXT(dstAddr, *data, len); + #endif W5100.write(dstAddr, data, len); } else { // Wrap around circular buffer @@ -412,6 +462,11 @@ static void write_data(uint8_t s, uint16_t data_offset, const uint8_t *data, uin } ptr += len; W5100.writeSnTX_WR(s, ptr); + + #if defined DEBUG_SOCKET_CPP_WRITE_DATA + PRINTVAR_HEX(offset); + PRINTVAR_HEXT(s, data_offset, len); + #endif } @@ -498,22 +553,59 @@ uint16_t EthernetClass::socketBufferData(uint8_t s, uint16_t offset, const uint8 bool EthernetClass::socketStartUDP(uint8_t s, uint8_t* addr, uint16_t port) { + #if defined DEBUG_ETHERNET_UDP_CPP_SOCKETSTARTUDP + PRINTLINE(); + #endif + if ( ((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) || ((port == 0x00)) ) { return false; } SPI.beginTransaction(SPI_ETHERNET_SETTINGS); + + #if defined DEBUG_ETHERNET_UDP_CPP_SOCKETSTARTUDP + PRINTLINE(); + PRINTVAR_HEX(s); + PRINTVAR_HEX(addr[0]); + PRINTVAR_HEX(addr[1]); + PRINTVAR_HEX(addr[2]); + PRINTVAR_HEX(addr[3]); + #endif + W5100.writeSnDIPR(s, addr); + + #if defined DEBUG_ETHERNET_UDP_CPP_SOCKETSTARTUDP + PRINTLINE(); + #endif + W5100.writeSnDPORT(s, port); + + #if defined DEBUG_ETHERNET_UDP_CPP_SOCKETSTARTUDP + PRINTLINE(); + #endif + SPI.endTransaction(); + + #if defined DEBUG_ETHERNET_UDP_CPP_SOCKETSTARTUDP + PRINTLINE(); + #endif + return true; } bool EthernetClass::socketSendUDP(uint8_t s) { + #if defined DEBUG_SOCKET_CPP_SOCKETSENDUDP + PRINTLINE(); + #endif + SPI.beginTransaction(SPI_ETHERNET_SETTINGS); W5100.execCmdSn(s, Sock_SEND); + #if defined DEBUG_SOCKET_CPP_SOCKETSENDUDP + PRINTLINE(); + #endif + /* +2008.01 bj */ while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK ) { if (W5100.readSnIR(s) & SnIR::TIMEOUT) { @@ -528,10 +620,18 @@ bool EthernetClass::socketSendUDP(uint8_t s) SPI.beginTransaction(SPI_ETHERNET_SETTINGS); } + #if defined DEBUG_SOCKET_CPP_SOCKETSENDUDP + PRINTLINE(); + #endif + /* +2008.01 bj */ W5100.writeSnIR(s, SnIR::SEND_OK); SPI.endTransaction(); + #if defined DEBUG_SOCKET_CPP_SOCKETSENDUDP + PRINTLINE(); + #endif + //Serial.printf("sendUDP ok\n"); /* Sent ok */ return true; diff --git a/src/utility/w5100.cpp b/src/utility/w5100.cpp index 8d35e910..fb65c31e 100644 --- a/src/utility/w5100.cpp +++ b/src/utility/w5100.cpp @@ -12,6 +12,25 @@ #include "Ethernet.h" #include "w5100.h" +//#define DEBUG_W5100_CPP_ISW6100 +//#define DEBUG_W5100_CPP_ISW5200 +//#define DEBUG_W5100_CPP_ISW5500 +//#define DEBUG_W5100_CPP_ISW5100S +//#define DEBUG_W5100_CPP_ISW5100 + +//#define DEBUG_W5100_CPP_SOFTRESET + +//#define DEBUG_W5100_CPP_WRITE +//#define DEBUG_W5100_CPP_WRITE_COMMON +//#define DEBUG_W5100_CPP_WRITE_SOCKET +//#define DEBUG_W5100_CPP_WRITE_TX +//#define DEBUG_W5100_CPP_WRITE_RX + +//#define DEBUG_W5100_CPP_READ +//#define DEBUG_W5100_CPP_READ_COMMON +//#define DEBUG_W5100_CPP_READ_SOCKET +//#define DEBUG_W5100_CPP_READ_TX +//#define DEBUG_W5100_CPP_READ_RX /***************************************************/ /** Default SS pin setting **/ @@ -52,6 +71,7 @@ // W5100 controller instance uint8_t W5100Class::chip = 0; uint8_t W5100Class::CH_BASE_MSB; +uint16_t W5100Class::CH_SIZE; uint8_t W5100Class::ss_pin = SS_PIN_DEFAULT; #ifdef ETHERNET_LARGE_BUFFERS uint16_t W5100Class::SSIZE = 2048; @@ -100,6 +120,8 @@ uint8_t W5100Class::init(void) // reset time, this can be edited or removed. delay(560); //Serial.println("w5100 init"); + + CH_SIZE = 0x0100; // Default except W6100 SPI.begin(); initSS(); @@ -204,6 +226,30 @@ uint8_t W5100Class::init(void) #else writeTMSR(0x55); writeRMSR(0x55); +#endif + // Try W6100. Brandnew based W5500. + } else if (isW6100()) { + CH_BASE_MSB = 0x60; + CH_SIZE = 0x0400; // W6100 +#ifdef ETHERNET_LARGE_BUFFERS +#if MAX_SOCK_NUM <= 1 + SSIZE = 16384; +#elif MAX_SOCK_NUM <= 2 + SSIZE = 8192; +#elif MAX_SOCK_NUM <= 4 + SSIZE = 4096; +#else + SSIZE = 2048; +#endif + SMASK = SSIZE - 1; + for (i=0; i> 10); + writeSnTX_SIZE(i, SSIZE >> 10); + } + for (; i<8; i++) { + writeSnRX_SIZE(i, 0); + writeSnTX_SIZE(i, 0); + } #endif // No hardware seems to be present. Or it could be a W5200 // that's heard other SPI communication if its chip select @@ -223,23 +269,101 @@ uint8_t W5100Class::init(void) uint8_t W5100Class::softReset(void) { uint16_t count=0; + uint8_t sysr; + + if(chip == 61) + { + writeCHPLCKR_W6100(W6100_CHPLCKR_UNLOCK); // Unlock SYSR[CHPL] + count = 0; + do{ // Wait Unlock Complete + if(++count > 20) // Check retry count + { + #if defined DEBUG_W5100_CPP_SOFTRESET + PRINTSTR("count !< 20"); + #endif + return 0; // Over Limit retry count + } + #if defined DEBUG_W5100_CPP_SOFTRESET + PRINTVAR(count); + #endif + } while ((readSYSR_W6100() & W6100_SYSR_CHPL_LOCK) ^ W6100_SYSR_CHPL_ULOCK); // Exit Wait Unlock Complete + + writeSYCR0(0x0); // Software Reset + + do{ // Wait Lock Complete + if(++count > 20) // Check retry count + { + #if defined DEBUG_W5100_CPP_SOFTRESET + PRINTSTR("count !< 20"); + #endif + return 0; // Over Limit retry count + } + + #if defined DEBUG_W5100_CPP_SOFTRESET + PRINTVAR(count); + #endif + } while ((readSYSR_W6100() & W6100_SYSR_CHPL_LOCK) ^ W6100_SYSR_CHPL_LOCK); // Exit Wait Lock Complete + + return 1; + } + else + { + count = 0; + + //Serial.println("Wiznet soft reset"); + // write to reset bit + writeMR(0x80); + // then wait for soft reset to complete + do { + uint8_t mr = readMR(); + //Serial.print("mr="); + //Serial.println(mr, HEX); + if (mr == 0 || (mr == 3 && chip == 50)) return 1; + delay(1); + } while (++count < 20); + return 0; + } +} - //Serial.println("Wiznet soft reset"); - // write to reset bit - writeMR(0x80); - // then wait for soft reset to complete - do { - uint8_t mr = readMR(); - //Serial.print("mr="); - //Serial.println(mr, HEX); - if (mr == 0 || (mr == 3 && chip == 50)) return 1; - delay(1); - } while (++count < 20); - return 0; +uint8_t W5100Class::isW6100(void) +{ + #if defined DEBUG_W5100_CPP_ISW6100 + PRINTSTR("isW6100"); + #endif + + chip = 61; + CH_BASE_MSB = 0x80; + + if (!softReset()) return 0; + + // Unlock + writeCHPLCKR_W6100(W6100_CHPLCKR_UNLOCK); + writeNETLCKR_W6100(W6100_NETLCKR_UNLOCK); + writePHYLCKR_W6100(W6100_PHYLCKR_UNLOCK); + + // W6100 CIDR0 + // Version 97(dec) 0x61(hex) + int ver = readVERSIONR_W6100(); + + #if defined DEBUG_W5100_CPP_ISW6100 + Serial.print("version = 0x"); + Serial.println(ver, HEX); + #endif + + if (ver != 97) return 0; + #if defined DEBUG_W5100_CPP_ISW6100 + Serial.println("chip is W6100"); + #endif + + return 1; } uint8_t W5100Class::isW5100(void) { + #if defined DEBUG_W5100_CPP_ISW5100 + PRINTSTR("isW5100"); + #endif + chip = 51; //Serial.println("w5100.cpp: detect W5100 chip"); if (!softReset()) return 0; @@ -250,11 +374,20 @@ uint8_t W5100Class::isW5100(void) writeMR(0x00); if (readMR() != 0x00) return 0; //Serial.println("chip is W5100"); + + #if defined DEBUG_W5100_CPP_ISW5100 + PRINTSTR("chip is W5100"); + #endif + return 1; } uint8_t W5100Class::isW5100S(void) { + #if defined DEBUG_W5100_CPP_ISW5100S + PRINTSTR("isW5100S"); + #endif + chip = 50; //Serial.println("w5100.cpp: detect W5100S chip"); if (!softReset()) return 0; @@ -267,11 +400,20 @@ uint8_t W5100Class::isW5100S(void) //Serial.println(ver); if (ver != 81) return 0; //Serial.println("chip is W5100S"); + + #if defined DEBUG_W5100_CPP_ISW5100S + PRINTSTR("chip is W5100S"); + #endif + return 1; } uint8_t W5100Class::isW5200(void) { + #if defined DEBUG_W5100_CPP_ISW5200 + PRINTSTR("isW5200"); + #endif + chip = 52; //Serial.println("w5100.cpp: detect W5200 chip"); if (!softReset()) return 0; @@ -286,11 +428,20 @@ uint8_t W5100Class::isW5200(void) //Serial.println(ver); if (ver != 3) return 0; //Serial.println("chip is W5200"); + + #if defined DEBUG_W5100_CPP_ISW5200 + PRINTSTR("chip is W5200"); + #endif + return 1; } uint8_t W5100Class::isW5500(void) { + #if defined DEBUG_W5100_CPP_ISW5500 + PRINTSTR("isW5500"); + #endif + chip = 55; //Serial.println("w5100.cpp: detect W5500 chip"); if (!softReset()) return 0; @@ -305,6 +456,11 @@ uint8_t W5100Class::isW5500(void) //Serial.println(ver); if (ver != 4) return 0; //Serial.println("chip is W5500"); + + #if defined DEBUG_W5100_CPP_ISW5500 + PRINTSTR("chip is W5500"); + #endif + return 1; } @@ -332,6 +488,12 @@ W5100Linkstatus W5100Class::getLinkStatus() SPI.endTransaction(); if (phystatus & 0x01) return LINK_ON; return LINK_OFF; + case 61: + SPI.beginTransaction(SPI_ETHERNET_SETTINGS); + phystatus = readPHYCFGR_W6100(); + SPI.endTransaction(); + if (phystatus & 0x01) return LINK_ON; + return LINK_OFF; default: return UNKNOWN; } @@ -367,6 +529,139 @@ uint16_t W5100Class::write(uint16_t addr, const uint8_t *buf, uint16_t len) } #endif resetSS(); + } else if (chip == 61) { // chip == 61 + setSS(); + + #if defined DEBUG_W5100_CPP_WRITE + PRINTSTR("write"); + #endif + + if (addr < CH_BASE()) { + // common registers + #if defined DEBUG_W5100_CPP_WRITE_COMMON + PRINTSTR("common register"); + #endif + + cmd[0] = (addr>>8) & 0x7F; + cmd[1] = addr & 0xFF; + cmd[2] = W6100_SPI_FRAME_CTL_BSB_BLK(0) + | W6100_SPI_FRAME_CTL_BSB_COMM + | W6100_SPI_FRAME_CTL_WD + | W6100_SPI_FRAME_CTL_OPM_VDM + ; + + #if defined DEBUG_W5100_CPP_WRITE_COMMON + PRINTVAR_HEX(addr); + PRINTVAR_HEX(cmd[0]); + PRINTVAR_HEX(cmd[1]); + PRINTVAR_HEX(cmd[2]); + #endif + + } else if (addr < W6100_TX_BASE_ADDR) { + // socket registers + #if defined DEBUG_W5100_CPP_WRITE_SOCKET + PRINTSTR("socket register"); + #endif + + cmd[0] = (addr>>8) & 0x3; + cmd[1] = addr & 0xFF; + cmd[2] = W6100_SPI_FRAME_CTL_BSB_BLK((addr>>10)&0x7) + | W6100_SPI_FRAME_CTL_BSB_SOCK + | W6100_SPI_FRAME_CTL_WD + | W6100_SPI_FRAME_CTL_OPM_VDM + ; + + #if defined DEBUG_W5100_CPP_WRITE_SOCKET + PRINTVAR_HEX(addr); + PRINTVAR_HEX(cmd[0]); + PRINTVAR_HEX(cmd[1]); + PRINTVAR_HEX(cmd[2]); + #endif + + } else if (addr < W6100_RX_BASE_ADDR) { + // transmit buffers + + #if defined DEBUG_W5100_CPP_WRITE_TX + PRINTSTR("transmit buffer register"); + #endif + + cmd[0] = addr>>8; + cmd[1] = addr & 0xFF; + + #if defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 1 + cmd[2] = 0; // 16K buffers + #elif defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 2 + cmd[2] = ((addr >> 8) & 0x20); // 8K buffers + #elif defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 4 + cmd[2] = ((addr >> 7) & 0x60); // 4K buffers + #else + cmd[2] = ((addr >> 6) & 0xE0); // 2K buffers + #endif + + cmd[2] |= W6100_SPI_FRAME_CTL_BSB_BLK(0) + | W6100_SPI_FRAME_CTL_BSB_TXBF + | W6100_SPI_FRAME_CTL_WD + | W6100_SPI_FRAME_CTL_OPM_VDM + ; + + #if defined DEBUG_W5100_CPP_WRITE_TX + PRINTVAR_HEX(addr); + PRINTVAR_HEX(cmd[0]); + PRINTVAR_HEX(cmd[1]); + PRINTVAR_HEX(cmd[2]); + #endif + + } else { + // receive buffers + + #if defined DEBUG_W5100_CPP_WRITE_RX + PRINTSTR("receive buffer register"); + #endif + + cmd[0] = addr>>8; + cmd[1] = addr & 0xFF; + + #if defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 1 + cmd[2] = 0; // 16K buffers + #elif defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 2 + cmd[2] = ((addr >> 8) & 0x20); // 8K buffers + #elif defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 4 + cmd[2] = ((addr >> 7) & 0x60); // 4K buffers + #else + cmd[2] = ((addr >> 6) & 0xE0); // 2K buffers + #endif + + cmd[2] |= W6100_SPI_FRAME_CTL_BSB_BLK(0) + | W6100_SPI_FRAME_CTL_BSB_RXBF + | W6100_SPI_FRAME_CTL_WD + | W6100_SPI_FRAME_CTL_OPM_VDM + ; + + #if defined DEBUG_W5100_CPP_WRITE_RX + PRINTVAR_HEX(addr); + PRINTVAR_HEX(cmd[0]); + PRINTVAR_HEX(cmd[1]); + PRINTVAR_HEX(cmd[2]); + #endif + + } + if (len <= 5) { + for (uint8_t i=0; i < len; i++) { + cmd[i + 3] = buf[i]; + } + SPI.transfer(cmd, len + 3); + } else { + SPI.transfer(cmd, 3); +#ifdef SPI_HAS_TRANSFER_BUF + SPI.transfer(buf, NULL, len); +#else + // TODO: copy 8 bytes at a time to cmd[] and block transfer + for (uint16_t i=0; i < len; i++) { + SPI.transfer(buf[i]); + } +#endif + } + resetSS(); } else { // chip == 55 setSS(); if (addr < 0x100) { @@ -462,6 +757,126 @@ uint16_t W5100Class::read(uint16_t addr, uint8_t *buf, uint16_t len) memset(buf, 0, len); SPI.transfer(buf, len); resetSS(); + } else if (chip == 61) { // chip == 61 + setSS(); + + #if defined DEBUG_W5100_CPP_READ + PRINTSTR("read"); + #endif + + if (addr < CH_BASE()) { + // common registers + #if defined DEBUG_W5100_CPP_READ_COMMON + PRINTSTR("common register"); + #endif + + cmd[0] = (addr>>8) & 0x7F; + cmd[1] = addr & 0xFF; + cmd[2] = W6100_SPI_FRAME_CTL_BSB_BLK(0) + | W6100_SPI_FRAME_CTL_BSB_COMM + | W6100_SPI_FRAME_CTL_RD + | W6100_SPI_FRAME_CTL_OPM_VDM + ; + + #if defined DEBUG_W5100_CPP_READ_COMMON + PRINTVAR_HEX(addr); + PRINTVAR_HEX(cmd[0]); + PRINTVAR_HEX(cmd[1]); + PRINTVAR_HEX(cmd[2]); + #endif + + } else if (addr < W6100_TX_BASE_ADDR) { + // socket registers + #if defined DEBUG_W5100_CPP_READ_SOCKET + PRINTSTR("socket register"); + #endif + + cmd[0] = (addr>>8) & 0x3; + cmd[1] = addr & 0xFF; + cmd[2] = W6100_SPI_FRAME_CTL_BSB_BLK((addr>>10)&0x7) + | W6100_SPI_FRAME_CTL_BSB_SOCK + | W6100_SPI_FRAME_CTL_RD + | W6100_SPI_FRAME_CTL_OPM_VDM + ; + + #if defined DEBUG_W5100_CPP_READ_SOCKET + PRINTVAR_HEX(addr); + PRINTVAR_HEX(cmd[0]); + PRINTVAR_HEX(cmd[1]); + PRINTVAR_HEX(cmd[2]); + #endif + + } else if (addr < W6100_RX_BASE_ADDR) { + // transmit buffers + + #if defined DEBUG_W5100_CPP_READ_TX + PRINTSTR("transmit buffer register"); + #endif + + cmd[0] = addr>>8; + cmd[1] = addr & 0xFF; + + #if defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 1 + cmd[2] = 0; // 16K buffers + #elif defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 2 + cmd[2] = ((addr >> 8) & 0x20); // 8K buffers + #elif defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 4 + cmd[2] = ((addr >> 7) & 0x60); // 4K buffers + #else + cmd[2] = ((addr >> 6) & 0xE0); // 2K buffers + #endif + + cmd[2] |= W6100_SPI_FRAME_CTL_BSB_BLK(0) + | W6100_SPI_FRAME_CTL_BSB_TXBF + | W6100_SPI_FRAME_CTL_RD + | W6100_SPI_FRAME_CTL_OPM_VDM + ; + + #if defined DEBUG_W5100_CPP_READ_TX + PRINTVAR_HEX(addr); + PRINTVAR_HEX(cmd[0]); + PRINTVAR_HEX(cmd[1]); + PRINTVAR_HEX(cmd[2]); + #endif + + } else { + // receive buffers + + #if defined DEBUG_W5100_CPP_READ_RX + PRINTSTR("receive buffer register"); + #endif + + cmd[0] = addr>>8; + cmd[1] = addr & 0xFF; + + #if defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 1 + cmd[2] = 0; // 16K buffers + #elif defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 2 + cmd[2] = ((addr >> 8) & 0x20); // 8K buffers + #elif defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 4 + cmd[2] = ((addr >> 7) & 0x60); // 4K buffers + #else + cmd[2] = ((addr >> 6) & 0xE0); // 2K buffers + #endif + + cmd[2] |= W6100_SPI_FRAME_CTL_BSB_BLK(0) + | W6100_SPI_FRAME_CTL_BSB_RXBF + | W6100_SPI_FRAME_CTL_RD + | W6100_SPI_FRAME_CTL_OPM_VDM + ; + + #if defined DEBUG_W5100_CPP_READ_RX + PRINTVAR_HEX(addr); + PRINTVAR_HEX(cmd[0]); + PRINTVAR_HEX(cmd[1]); + PRINTVAR_HEX(cmd[2]); + #endif + + } + SPI.transfer(cmd, 3); + memset(buf, 0, len); + SPI.transfer(buf, len); + resetSS(); } else { // chip == 55 setSS(); if (addr < 0x100) { diff --git a/src/utility/w5100.h b/src/utility/w5100.h index 91e81814..bfc8deb6 100644 --- a/src/utility/w5100.h +++ b/src/utility/w5100.h @@ -17,6 +17,10 @@ #include #include +//#define DEBUG_W5100_H +//#define DEBUG_W5100_H_WRITESN +//#define DEBUG_W5100_H_READSN + // Safe for all chips #define SPI_ETHERNET_SETTINGS SPISettings(14000000, MSBFIRST, SPI_MODE0) @@ -161,58 +165,146 @@ class W5100Class { return data; } -#define __GP_REGISTER8(name, address) \ - static inline void write##name(uint8_t _data) { \ - write(address, _data); \ +#define W6100_SPI_FRAME_CTL_BSB_BLK(sn) ((sn)<<5) + +#define W6100_SPI_FRAME_CTL_BSB_COMM (0<<3) +#define W6100_SPI_FRAME_CTL_BSB_SOCK (1<<3) +#define W6100_SPI_FRAME_CTL_BSB_TXBF (2<<3) +#define W6100_SPI_FRAME_CTL_BSB_RXBF (3<<3) + +#define W6100_SPI_FRAME_CTL_RD (0<<2) +#define W6100_SPI_FRAME_CTL_WD (1<<2) + +#define W6100_SPI_FRAME_CTL_OPM_VDM (0<<0) +#define W6100_SPI_FRAME_CTL_OPM_FDM1 (1<<0) +#define W6100_SPI_FRAME_CTL_OPM_FDM2 (2<<0) +#define W6100_SPI_FRAME_CTL_OPM_FDM4 (3<<0) + +#define W6100_COMMON_BASE_ADDR (0x0000) +#define W6100_SOCKET_BASE_ADDR (0x6000) +#define W6100_TX_BASE_ADDR (0x8000) +#define W6100_RX_BASE_ADDR (0xC000) +#define W6100_SOCKET_NUM(_s) ((_s)<<10)) + +#define W6100_CHPLCKR_UNLOCK 0xCE +#define W6100_NETLCKR_UNLOCK 0x3A +#define W6100_PHYLCKR_UNLOCK 0x53 + +#define W6100_SYSR_CHPL_LOCK (1<<7) +#define W6100_SYSR_CHPL_ULOCK (0<<7) + +#define W6100_UDP_HEADER_IPV (1<<7) +#define W6100_UDP_HEADER_IPV4 (0<<7) +#define W6100_UDP_HEADER_IPV6 (1<<7) +#define W6100_UDP_HEADER_ALL (1<<6) +#define W6100_UDP_HEADER_MUL (1<<5) +#define W6100_UDP_HEADER_GUA (0<<3) +#define W6100_UDP_HEADER_LLA (1<<3) + +#define __GP_REGISTER8(name, address, adrss_w6100) \ + static inline void write##name(uint8_t _data) { \ + if(chip == 61) { \ + write(adrss_w6100, _data); \ + } else { \ + write(address, _data); \ + } \ } \ static inline uint8_t read##name() { \ - return read(address); \ - } -#define __GP_REGISTER16(name, address) \ + if(chip == 61) { \ + return read(adrss_w6100); \ + } else { \ + return read(address); \ + } \ + } \ + +#define __GP_REGISTER16(name, address, adrss_w6100) \ static void write##name(uint16_t _data) { \ uint8_t buf[2]; \ buf[0] = _data >> 8; \ buf[1] = _data & 0xFF; \ - write(address, buf, 2); \ + if(chip == 61) { \ + write(adrss_w6100, buf, 2); \ + } else { \ + write(address, buf, 2); \ + } \ } \ static uint16_t read##name() { \ uint8_t buf[2]; \ - read(address, buf, 2); \ + if(chip == 61) { \ + read(adrss_w6100, buf, 2); \ + } else { \ + read(address, buf, 2); \ + } \ return (buf[0] << 8) | buf[1]; \ } -#define __GP_REGISTER_N(name, address, size) \ + +#define __GP_REGISTER_N(name, address, adrss_w6100, size) \ static uint16_t write##name(const uint8_t *_buff) { \ - return write(address, _buff, size); \ - } \ - static uint16_t read##name(uint8_t *_buff) { \ - return read(address, _buff, size); \ + if(chip == 61) { \ + return write(adrss_w6100, _buff, size); \ + } else { \ + return write(address, _buff, size); \ + } \ + } \ + static uint16_t read##name(uint8_t *_buff) { \ + if(chip == 61) { \ + return read(adrss_w6100, _buff, size); \ + } else { \ + return read(address, _buff, size); \ + } \ } static W5100Linkstatus getLinkStatus(); public: - __GP_REGISTER8 (MR, 0x0000); // Mode - __GP_REGISTER_N(GAR, 0x0001, 4); // Gateway IP address - __GP_REGISTER_N(SUBR, 0x0005, 4); // Subnet mask address - __GP_REGISTER_N(SHAR, 0x0009, 6); // Source MAC address - __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) - __GP_REGISTER8 (VERSIONR_W5200,0x001F); // Chip Version Register (W5200 only) - __GP_REGISTER8 (VERSIONR_W5500,0x0039); // Chip Version Register (W5500 only) - __GP_REGISTER8 (VERSIONR_W5100S, 0x0080); // Chip Version Register (W5100S only) - __GP_REGISTER8 (PSTATUS_W5200, 0x0035); // PHY Status - __GP_REGISTER8 (PHYCFGR_W5500, 0x002E); // PHY Configuration register, default: 10111xxx - __GP_REGISTER8 (PHYCFGR_W5100S, 0x003C); // PHY Status - + __GP_REGISTER8 (MR, 0x0000, 0x4000); // Mode + __GP_REGISTER_N(GAR, 0x0001, 0x4130, 4); // Gateway IP address + __GP_REGISTER_N(SUBR, 0x0005, 0x4134, 4); // Subnet mask address + __GP_REGISTER_N(SHAR, 0x0009, 0x4120, 6); // Source MAC address + __GP_REGISTER_N(SIPR, 0x000F, 0x4138, 4); // Source IP address + __GP_REGISTER8 (IR, 0x0015, 0x2100); // Interrupt + __GP_REGISTER8 (IMR, 0x0016, 0x2104); // Interrupt Mask + __GP_REGISTER16(RTR, 0x0017, 0x2101); // Timeout address + __GP_REGISTER8 (RCR, 0x0019, 0x4200); // Retry count + __GP_REGISTER8 (RMSR, 0x001A, 0x0000); // Receive memory size (W5100 only) + __GP_REGISTER8 (TMSR, 0x001B, 0x0000); // Transmit memory size (W5100 only) + __GP_REGISTER8 (PATR, 0x001C, 0x0000); // Authentication type address in PPPoE mode + __GP_REGISTER8 (PTIMER, 0x0028, 0x4100); // PPP LCP Request Timer + __GP_REGISTER8 (PMAGIC, 0x0029, 0x4104); // PPP LCP Magic Number + __GP_REGISTER_N(UIPR, 0x002A, 0x0000, 4); // Unreachable IP address in UDP mode (W5100 only) + __GP_REGISTER16(UPORT, 0x002E, 0x0000); // Unreachable Port address in UDP mode (W5100 only) + __GP_REGISTER8 (VERSIONR_W5200, 0x001F, 0x0000); // Chip Version Register (W5200 only) + __GP_REGISTER8 (VERSIONR_W5500, 0x0039, 0x0000); // Chip Version Register (W5500 only) + __GP_REGISTER8 (VERSIONR_W5100S, 0x0080, 0x0000); // Chip Version Register (W5100S only) + __GP_REGISTER8 (PSTATUS_W5200, 0x0035, 0x0000); // PHY Status + __GP_REGISTER8 (PHYCFGR_W5500, 0x002E, 0x0000); // PHY Configuration register, default: 10111xxx + __GP_REGISTER8 (PHYCFGR_W5100S, 0x003C, 0x0000); // PHY Status + + // For W6100 + // System Command Register + __GP_REGISTER8 (SYCR0, 0x0000, 0x2004); + + // System Status Register + __GP_REGISTER8 (SYSR_W6100, 0x0000, 0x2000); + + // LOCK Register + + // Network Lock Register + __GP_REGISTER8 (NETLCKR_W6100, 0x0000, 0x41F5); + + // Chip Lock Register + __GP_REGISTER8 (CHPLCKR_W6100, 0x0000, 0x41F4); + + // PHY Lock Register + __GP_REGISTER8 (PHYLCKR_W6100, 0x0000, 0x41F6); + + // PHY Status + __GP_REGISTER8 (PHYCFGR_W6100, 0x0000, 0x3000); + + // Chip Version Register + __GP_REGISTER8 (VERSIONR_W6100, 0x0000, 0x0000); + + // Chip Version Register + __GP_REGISTER8 (CVERSIONR_W6100, 0x0000, 0x0002); #undef __GP_REGISTER8 #undef __GP_REGISTER16 @@ -225,72 +317,124 @@ class W5100Class { //if (chip == 55) return 0x1000; //if (chip == 52) return 0x4000; //return 0x0400; + + // 5500 : 0x10 << 8 = 0x1000 + // 6100 : 0x60 << 8 = 0x6000 (1000 0000 0000 0000) + return CH_BASE_MSB << 8; } + static uint8_t CH_BASE_MSB; // 1 redundant byte, saves ~80 bytes code on AVR - static const uint16_t CH_SIZE = 0x0100; + static uint16_t CH_SIZE; static inline uint8_t readSn(SOCKET s, uint16_t addr) { + #ifdef DEBUG_W5100_H_READSN + PRINTVAR_HEX(s); + PRINTVAR_HEX(addr); + PRINTVAR_HEX(CH_BASE() + s * CH_SIZE + addr); + #endif return read(CH_BASE() + s * CH_SIZE + addr); } static inline uint8_t writeSn(SOCKET s, uint16_t addr, uint8_t data) { + #ifdef DEBUG_W5100_H_WRITESN + PRINTVAR_HEX(s); + PRINTVAR_HEX(addr); + PRINTVAR_HEX(CH_BASE() + s * CH_SIZE + addr); + #endif return write(CH_BASE() + s * CH_SIZE + addr, data); } static inline uint16_t readSn(SOCKET s, uint16_t addr, uint8_t *buf, uint16_t len) { + #ifdef DEBUG_W5100_H_READSN + PRINTVAR_HEX(s); + PRINTVAR_HEX(addr); + PRINTVAR_HEX(CH_BASE() + s * CH_SIZE + addr); + #endif return read(CH_BASE() + s * CH_SIZE + addr, buf, len); } static inline uint16_t writeSn(SOCKET s, uint16_t addr, uint8_t *buf, uint16_t len) { + #ifdef DEBUG_W5100_H_WRITESN + PRINTVAR_HEX(s); + PRINTVAR_HEX(addr); + PRINTVAR_HEX(CH_BASE() + s * CH_SIZE + addr); + #endif return write(CH_BASE() + s * CH_SIZE + addr, buf, len); } -#define __SOCKET_REGISTER8(name, address) \ +#define __SOCKET_REGISTER8(name, address, adrss_w6100) \ static inline void write##name(SOCKET _s, uint8_t _data) { \ - writeSn(_s, address, _data); \ + if(chip == 61) { \ + writeSn(_s, adrss_w6100, _data); \ + } else { \ + writeSn(_s, address, _data); \ + } \ } \ static inline uint8_t read##name(SOCKET _s) { \ - return readSn(_s, address); \ + if(chip == 61) { \ + uint8_t data; \ + return readSn(_s, adrss_w6100); \ + } else { \ + return readSn(_s, address); \ + } \ } -#define __SOCKET_REGISTER16(name, address) \ + +#define __SOCKET_REGISTER16(name, address, adrss_w6100) \ static void write##name(SOCKET _s, uint16_t _data) { \ uint8_t buf[2]; \ buf[0] = _data >> 8; \ buf[1] = _data & 0xFF; \ - writeSn(_s, address, buf, 2); \ + if(chip == 61) { \ + writeSn(_s, adrss_w6100, buf, 2);\ + } else { \ + writeSn(_s, address, buf, 2); \ + } \ } \ static uint16_t read##name(SOCKET _s) { \ uint8_t buf[2]; \ - readSn(_s, address, buf, 2); \ + if(chip == 61) { \ + readSn(_s, adrss_w6100, buf, 2); \ + } else { \ + readSn(_s, address, buf, 2); \ + } \ return (buf[0] << 8) | buf[1]; \ } -#define __SOCKET_REGISTER_N(name, address, size) \ + +#define __SOCKET_REGISTER_N(name, address, adrss_w6100, size)\ static uint16_t write##name(SOCKET _s, uint8_t *_buff) { \ - return writeSn(_s, address, _buff, size); \ + if(chip == 61) { \ + return writeSn(_s, adrss_w6100, _buff, size); \ + } else { \ + return writeSn(_s, address, _buff, size); \ + } \ } \ static uint16_t read##name(SOCKET _s, uint8_t *_buff) { \ - return readSn(_s, address, _buff, size); \ + if(chip == 61) { \ + return readSn(_s, adrss_w6100, _buff, size); \ + } else { \ + return readSn(_s, address, _buff, size); \ + } \ } public: - __SOCKET_REGISTER8(SnMR, 0x0000) // Mode - __SOCKET_REGISTER8(SnCR, 0x0001) // Command - __SOCKET_REGISTER8(SnIR, 0x0002) // Interrupt - __SOCKET_REGISTER8(SnSR, 0x0003) // Status - __SOCKET_REGISTER16(SnPORT, 0x0004) // Source Port - __SOCKET_REGISTER_N(SnDHAR, 0x0006, 6) // Destination Hardw Addr - __SOCKET_REGISTER_N(SnDIPR, 0x000C, 4) // Destination IP Addr - __SOCKET_REGISTER16(SnDPORT, 0x0010) // Destination Port - __SOCKET_REGISTER16(SnMSSR, 0x0012) // Max Segment Size - __SOCKET_REGISTER8(SnPROTO, 0x0014) // Protocol in IP RAW Mode - __SOCKET_REGISTER8(SnTOS, 0x0015) // IP TOS - __SOCKET_REGISTER8(SnTTL, 0x0016) // IP TTL - __SOCKET_REGISTER8(SnRX_SIZE, 0x001E) // RX Memory Size (W5200 only) - __SOCKET_REGISTER8(SnTX_SIZE, 0x001F) // RX Memory Size (W5200 only) - __SOCKET_REGISTER16(SnTX_FSR, 0x0020) // TX Free Size - __SOCKET_REGISTER16(SnTX_RD, 0x0022) // TX Read Pointer - __SOCKET_REGISTER16(SnTX_WR, 0x0024) // TX Write Pointer - __SOCKET_REGISTER16(SnRX_RSR, 0x0026) // RX Free Size - __SOCKET_REGISTER16(SnRX_RD, 0x0028) // RX Read Pointer - __SOCKET_REGISTER16(SnRX_WR, 0x002A) // RX Write Pointer (supported?) + __SOCKET_REGISTER8(SnMR, 0x0000, 0x0000) // Mode + __SOCKET_REGISTER8(SnCR, 0x0001, 0x0010) // Command + __SOCKET_REGISTER8(SnIR, 0x0002, 0x0020) // Interrupt + __SOCKET_REGISTER8(SnSR, 0x0003, 0x0030) // Status + __SOCKET_REGISTER16(SnPORT, 0x0004, 0x0114) // Source Port + __SOCKET_REGISTER_N(SnDHAR, 0x0006, 0x0118, 6) // Destination Hardw Addr + __SOCKET_REGISTER_N(SnDIPR, 0x000C, 0x0120, 4) // Destination IP Addr + __SOCKET_REGISTER16(SnDPORT, 0x0010, 0x0140) // Destination Port + __SOCKET_REGISTER16(SnMSSR, 0x0012, 0x0110) // Max Segment Size + __SOCKET_REGISTER8(SnPROTO, 0x0014, 0x0000) // Protocol in IP RAW Mode + __SOCKET_REGISTER8(SnTOS, 0x0015, 0x0104) // IP TOS + __SOCKET_REGISTER8(SnTTL, 0x0016, 0x0108) // IP TTL + __SOCKET_REGISTER8(SnRX_SIZE, 0x001E, 0x0220) // RX Memory Size (W5200 only) + __SOCKET_REGISTER8(SnTX_SIZE, 0x001F, 0x0200) // TX Memory Size (W5200 only) + __SOCKET_REGISTER16(SnTX_FSR, 0x0020, 0x0204) // TX Free Size + __SOCKET_REGISTER16(SnTX_RD, 0x0022, 0x0208) // TX Read Pointer + __SOCKET_REGISTER16(SnTX_WR, 0x0024, 0x020C) // TX Write Pointer + __SOCKET_REGISTER16(SnRX_RSR, 0x0026, 0x0224) // RX Free Size + __SOCKET_REGISTER16(SnRX_RD, 0x0028, 0x0228) // RX Read Pointer + __SOCKET_REGISTER16(SnRX_WR, 0x002A, 0x022C) // RX Write Pointer (supported?) #undef __SOCKET_REGISTER8 #undef __SOCKET_REGISTER16 @@ -305,6 +449,7 @@ class W5100Class { static uint8_t isW5100S(void); static uint8_t isW5200(void); static uint8_t isW5500(void); + static uint8_t isW6100(void); public: static uint8_t getChip(void) { return chip; } @@ -318,6 +463,8 @@ class W5100Class { static uint16_t SBASE(uint8_t socknum) { if (chip == 51 || chip == 50) { return socknum * SSIZE + 0x4000; + } else if (chip == 61) { + return socknum * SSIZE + W6100_TX_BASE_ADDR; } else { return socknum * SSIZE + 0x8000; } @@ -325,13 +472,15 @@ class W5100Class { static uint16_t RBASE(uint8_t socknum) { if (chip == 51 || chip == 50) { return socknum * SSIZE + 0x6000; + } else if (chip == 61) { + return socknum * SSIZE + W6100_RX_BASE_ADDR; } else { return socknum * SSIZE + 0xC000; } } static bool hasOffsetAddressMapping(void) { - if (chip == 55) return true; + if (chip == 55 || chip == 61) return true; return false; } static void setSS(uint8_t pin) { ss_pin = pin; } From d101b29581c1d746db321a8426d9ecd4c51354ea Mon Sep 17 00:00:00 2001 From: "Taylor.An" <44388468+taylor-an@users.noreply.github.com> Date: Tue, 15 Jan 2019 12:56:00 +0900 Subject: [PATCH 04/14] Added W5100S for W5100S Ethernetshield (#7) First Merge from W5100S --- src/utility/w5100.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/utility/w5100.cpp b/src/utility/w5100.cpp index fb65c31e..516b623b 100644 --- a/src/utility/w5100.cpp +++ b/src/utility/w5100.cpp @@ -303,7 +303,6 @@ uint8_t W5100Class::softReset(void) PRINTVAR(count); #endif } while ((readSYSR_W6100() & W6100_SYSR_CHPL_LOCK) ^ W6100_SYSR_CHPL_LOCK); // Exit Wait Lock Complete - return 1; } else @@ -356,6 +355,7 @@ uint8_t W5100Class::isW6100(void) #endif return 1; + } uint8_t W5100Class::isW5100(void) @@ -408,6 +408,23 @@ uint8_t W5100Class::isW5100S(void) return 1; } +uint8_t W5100Class::isW5100S(void) +{ + chip = 50; + //Serial.println("w5100.cpp: detect W5100S chip"); + if (!softReset()) return 0; + writeMR(0x13); + if (readMR() != 0x13) return 0; + writeMR(0x03); + if (readMR() != 0x03) return 0; + int ver = readVERSIONR_W5100S(); + //Serial.print("version="); + //Serial.println(ver); + if (ver != 81) return 0; + //Serial.println("chip is W5100S"); + return 1; +} + uint8_t W5100Class::isW5200(void) { #if defined DEBUG_W5100_CPP_ISW5200 From 7b89eb8b3f1f10285efcc8d0eb028db62edf7b86 Mon Sep 17 00:00:00 2001 From: "Taylor.An" <44388468+taylor-an@users.noreply.github.com> Date: Tue, 15 Jan 2019 13:14:30 +0900 Subject: [PATCH 05/14] For Arduino MKR Vidor-4000 (#8) Second Merge from MKR_Vidor-4000 From 3f4f9aafa8f8385cf8b2dc6b28dd5d76d062883c Mon Sep 17 00:00:00 2001 From: "Taylor.An" <44388468+taylor-an@users.noreply.github.com> Date: Tue, 15 Jan 2019 13:51:11 +0900 Subject: [PATCH 06/14] Removed Debugging Codes (#10) Removed Debugging Codes --- .../AdvancedChatServer/AdvancedChatServer.ino | 9 - .../BarometricPressureWebServer.ino | 6 - examples/ChatServer/ChatServer.ino | 13 +- examples/DhcpChatServer/DhcpChatServer.ino | 24 - examples/TelnetClient/TelnetClient.ino | 9 - .../UDPSendReceiveString.ino | 9 - examples/UdpNtpClient/UdpNtpClient.ino | 12 - examples/WebClient/WebClient.ino | 6 - .../WebClientRepeating/WebClientRepeating.ino | 6 - examples/WebServer/WebServer.ino | 6 - keywords.txt | 1 - src/Dhcp.cpp | 119 ----- src/Dns.cpp | 41 -- src/Ethernet.cpp | 40 -- src/Ethernet.h | 76 +-- src/EthernetUdp.cpp | 226 +-------- src/socket.cpp | 100 ---- src/utility/w5100.cpp | 441 +----------------- src/utility/w5100.h | 283 +++-------- 19 files changed, 95 insertions(+), 1332 deletions(-) diff --git a/examples/AdvancedChatServer/AdvancedChatServer.ino b/examples/AdvancedChatServer/AdvancedChatServer.ino index 3ff3e716..3b431c88 100644 --- a/examples/AdvancedChatServer/AdvancedChatServer.ino +++ b/examples/AdvancedChatServer/AdvancedChatServer.ino @@ -22,25 +22,16 @@ #include #include -#define ON_TAYLOR_WORK_PC - // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network. // gateway and subnet are optional: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; -#if defined ON_TAYLOR_WORK_PC -IPAddress ip(192, 168, 0, 3); -IPAddress myDns(192, 168, 0, 1); -IPAddress gateway(192, 168, 0, 1); -IPAddress subnet(255, 255, 0, 0); -#else IPAddress ip(192, 168, 1, 177); IPAddress myDns(192, 168, 1, 1); IPAddress gateway(192, 168, 1, 1); IPAddress subnet(255, 255, 0, 0); -#endif // telnet defaults to port 23 diff --git a/examples/BarometricPressureWebServer/BarometricPressureWebServer.ino b/examples/BarometricPressureWebServer/BarometricPressureWebServer.ino index d632a280..56ccc66c 100644 --- a/examples/BarometricPressureWebServer/BarometricPressureWebServer.ino +++ b/examples/BarometricPressureWebServer/BarometricPressureWebServer.ino @@ -27,20 +27,14 @@ // the sensor communicates using SPI, so include the library: #include -#define ON_TAYLOR_WORK_PC // assign a MAC address for the Ethernet controller. // fill in your address here: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; -#if defined ON_TAYLOR_WORK_PC -IPAddress ip(192, 168, 0, 3); -#else // assign an IP address for the controller: IPAddress ip(192, 168, 1, 20); -#endif - // Initialize the Ethernet server library diff --git a/examples/ChatServer/ChatServer.ino b/examples/ChatServer/ChatServer.ino index 6fd92fe7..740d7867 100644 --- a/examples/ChatServer/ChatServer.ino +++ b/examples/ChatServer/ChatServer.ino @@ -19,27 +19,16 @@ #include #include -#define ON_TAYLOR_WORK_PC -//#define DEBUG_CHATSERVER_INO_ETHERNET_BEGIN - // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network. // gateway and subnet are optional: -#if defined ON_TAYLOR_WORK_PC -byte mac[] = { -0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; -IPAddress ip(192, 168, 0, 3); -IPAddress myDns(192, 168, 0, 1); -IPAddress gateway(192, 168, 0, 1); -IPAddress subnet(255, 255, 0, 0); -#else byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192, 168, 1, 177); IPAddress myDns(192, 168, 1, 1); IPAddress gateway(192, 168, 1, 1); IPAddress subnet(255, 255, 0, 0); -#endif + // telnet defaults to port 23 EthernetServer server(23); diff --git a/examples/DhcpChatServer/DhcpChatServer.ino b/examples/DhcpChatServer/DhcpChatServer.ino index 472eb85c..f4489d61 100644 --- a/examples/DhcpChatServer/DhcpChatServer.ino +++ b/examples/DhcpChatServer/DhcpChatServer.ino @@ -23,20 +23,9 @@ #include #include -#define ON_TAYLOR_WORK_PC -//#define DEBUG_DHCPCHATSERVER_INO_SETUP - // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network. // gateway and subnet are optional: -#ifdef ON_TAYLOR_WORK_PC -byte mac[] = { - 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; -IPAddress ip(192, 168, 0, 3); -IPAddress myDns(192, 168, 0, 1); -IPAddress gateway(192, 168, 0, 1); -IPAddress subnet(255, 255, 0, 0); -#else byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 }; @@ -44,7 +33,6 @@ IPAddress ip(192, 168, 1, 177); IPAddress myDns(192, 168, 1, 1); IPAddress gateway(192, 168, 1, 1); IPAddress subnet(255, 255, 0, 0); -#endif // telnet defaults to port 23 EthernetServer server(23); @@ -67,14 +55,7 @@ void setup() { // start the Ethernet connection: Serial.println("Trying to get an IP address using DHCP"); - #if defined DEBUG_DHCPCHATSERVER_INO_SETUP - PRINTSTR("Ethernet.begin(mac)"); - #endif if (Ethernet.begin(mac) == 0) { - #if defined DEBUG_DHCPCHATSERVER_INO_SETUP - PRINTSTR("Ethernet.begin(mac) == 0"); - #endif - Serial.println("Failed to configure Ethernet using DHCP"); // Check for Ethernet hardware present if (Ethernet.hardwareStatus() == EthernetNoHardware) { @@ -89,11 +70,6 @@ void setup() { // initialize the Ethernet device not using DHCP: Ethernet.begin(mac, ip, myDns, gateway, subnet); } - - #if defined DEBUG_DHCPCHATSERVER_INO_SETUP - PRINTLINE(); - #endif - // print your local IP address: Serial.print("My IP address: "); Serial.println(Ethernet.localIP()); diff --git a/examples/TelnetClient/TelnetClient.ino b/examples/TelnetClient/TelnetClient.ino index 34144fab..85386b5f 100644 --- a/examples/TelnetClient/TelnetClient.ino +++ b/examples/TelnetClient/TelnetClient.ino @@ -20,24 +20,15 @@ #include #include -#define ON_TAYLOR_WORK_PC - // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; -#if defined ON_TAYLOR_WORK_PC -IPAddress ip(192, 168, 0, 3); - -// Enter the IP address of the server you're connecting to: -IPAddress server(192, 168, 0, 2); -#else IPAddress ip(192, 168, 1, 177); // Enter the IP address of the server you're connecting to: IPAddress server(1, 1, 1, 1); -#endif // Initialize the Ethernet client library // with the IP address and port of the server diff --git a/examples/UDPSendReceiveString/UDPSendReceiveString.ino b/examples/UDPSendReceiveString/UDPSendReceiveString.ino index b8b557ef..0c76b022 100644 --- a/examples/UDPSendReceiveString/UDPSendReceiveString.ino +++ b/examples/UDPSendReceiveString/UDPSendReceiveString.ino @@ -16,18 +16,12 @@ #include #include -#define ON_TAYLOR_WORK_PC - // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; -#if defined ON_TAYLOR_WORK_PC -IPAddress ip(192, 168, 0, 3); -#else IPAddress ip(192, 168, 1, 177); -#endif unsigned int localPort = 8888; // local port to listen on @@ -69,9 +63,6 @@ void setup() { // start UDP Udp.begin(localPort); - - Serial.print("Udp server address:"); - Serial.println(Ethernet.localIP()); } void loop() { diff --git a/examples/UdpNtpClient/UdpNtpClient.ino b/examples/UdpNtpClient/UdpNtpClient.ino index 5eca1439..ec52717d 100644 --- a/examples/UdpNtpClient/UdpNtpClient.ino +++ b/examples/UdpNtpClient/UdpNtpClient.ino @@ -22,9 +22,6 @@ #include #include -#define TIMESERVER 2 -//#define DEBUG_UDPNTPCLIENT_INO_SENDNTPPACKET - // Enter a MAC address for your controller below. // Newer Ethernet shields have a MAC address printed on a sticker on the shield byte mac[] = { @@ -33,11 +30,7 @@ byte mac[] = { unsigned int localPort = 8888; // local port to listen for UDP packets -#if TIMESERVER == 1 const char timeServer[] = "time.nist.gov"; // time.nist.gov NTP server -#elif TIMESERVER == 2 -const char timeServer[] = "time.bora.net"; // time.bora.net NTP server -#endif const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message @@ -131,11 +124,6 @@ void loop() { // send an NTP request to the time server at the given address void sendNTPpacket(const char * address) { - - #if defined DEBUG_UDPNTPCLIENT_INO_SENDNTPPACKET - PRINTLINE(); - #endif - // set all bytes in the buffer to 0 memset(packetBuffer, 0, NTP_PACKET_SIZE); // Initialize values needed to form NTP request diff --git a/examples/WebClient/WebClient.ino b/examples/WebClient/WebClient.ino index ebae18f4..45e01eed 100644 --- a/examples/WebClient/WebClient.ino +++ b/examples/WebClient/WebClient.ino @@ -17,8 +17,6 @@ #include #include -#define ON_TAYLOR_WORK_PC - // Enter a MAC address for your controller below. // Newer Ethernet shields have a MAC address printed on a sticker on the shield byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; @@ -29,11 +27,7 @@ byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; char server[] = "www.google.com"; // name address for Google (using DNS) // Set the static IP address to use if the DHCP fails to assign -#if defined ON_TAYLOR_WORK_PC -IPAddress ip(192, 168, 0, 3); -#else IPAddress ip(192, 168, 0, 177); -#endif IPAddress myDns(192, 168, 0, 1); // Initialize the Ethernet client library diff --git a/examples/WebClientRepeating/WebClientRepeating.ino b/examples/WebClientRepeating/WebClientRepeating.ino index f357632e..6607f438 100644 --- a/examples/WebClientRepeating/WebClientRepeating.ino +++ b/examples/WebClientRepeating/WebClientRepeating.ino @@ -25,19 +25,13 @@ #include #include -#define ON_TAYLOR_WORK_PC - // assign a MAC address for the ethernet controller. // fill in your address here: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // Set the static IP address to use if the DHCP fails to assign -#if defined ON_TAYLOR_WORK_PC -IPAddress ip(192, 168, 0, 3); -#else IPAddress ip(192, 168, 0, 177); -#endif IPAddress myDns(192, 168, 0, 1); // initialize the library instance: diff --git a/examples/WebServer/WebServer.ino b/examples/WebServer/WebServer.ino index f9f66dd9..06a85576 100644 --- a/examples/WebServer/WebServer.ino +++ b/examples/WebServer/WebServer.ino @@ -20,18 +20,12 @@ #include #include -#define ON_TAYLOR_WORK_PC - // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; -#if defined ON_TAYLOR_WORK_PC -IPAddress ip(192, 168, 0, 3); -#else IPAddress ip(192, 168, 1, 177); -#endif // Initialize the Ethernet server library // with the IP address and port you want to use diff --git a/keywords.txt b/keywords.txt index 9eaf4875..0013e20a 100644 --- a/keywords.txt +++ b/keywords.txt @@ -66,4 +66,3 @@ EthernetW5100 LITERAL1 EthernetW5200 LITERAL1 EthernetW5500 LITERAL1 EthernetW5100S LITERAL1 -EthernetW6100 LITERAL1 diff --git a/src/Dhcp.cpp b/src/Dhcp.cpp index ddc68289..2bfd584b 100644 --- a/src/Dhcp.cpp +++ b/src/Dhcp.cpp @@ -6,11 +6,6 @@ #include "Dhcp.h" #include "utility/w5100.h" -//#define DEBUG_DHCP_CPP_BEGINWITHDHCP -//#define DEBUG_DHCP_CPP_DHCP_LEASE -//#define DEBUG_DHCP_CPP_DHCP_MESSAGE -//#define DEBUG_DHCP_CPP_DHCPRESPONSE - int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout) { _dhcpLeaseTime=0; @@ -20,18 +15,9 @@ int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long _responseTimeout = responseTimeout; // zero out _dhcpMacAddr - #if defined DEBUG_ETHERNET_CPP_BEGIN - PRINTLINE_DEF(DEBUG_ETHERNET_CPP_BEGIN); - #endif memset(_dhcpMacAddr, 0, 6); - #if defined DEBUG_ETHERNET_CPP_BEGIN - PRINTLINE_DEF(DEBUG_ETHERNET_CPP_BEGIN); - #endif reset_DHCP_lease(); - #if defined DEBUG_ETHERNET_CPP_BEGIN - PRINTLINE_DEF(DEBUG_ETHERNET_CPP_BEGIN); - #endif memcpy((void*)_dhcpMacAddr, (void*)mac, 6); _dhcp_state = STATE_DHCP_START; return request_DHCP_lease(); @@ -48,52 +34,23 @@ int DhcpClass::request_DHCP_lease() { uint8_t messageType = 0; - #if defined DEBUG_DHCP_CPP_DHCP_LEASE - PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_LEASE); - #endif - // Pick an initial transaction ID _dhcpTransactionId = random(1UL, 2000UL); _dhcpInitialTransactionId = _dhcpTransactionId; - #if defined DEBUG_DHCP_CPP_DHCP_LEASE - PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_LEASE); - #endif - _dhcpUdpSocket.stop(); - - #if defined DEBUG_DHCP_CPP_DHCP_LEASE - PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_LEASE); - #endif - if (_dhcpUdpSocket.begin(DHCP_CLIENT_PORT) == 0) { // Couldn't get a socket return 0; } - #if defined DEBUG_DHCP_CPP_DHCP_LEASE - PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_LEASE); - #endif - presend_DHCP(); - #if defined DEBUG_DHCP_CPP_DHCP_LEASE - PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_LEASE); - #endif - int result = 0; unsigned long startTime = millis(); - #if defined DEBUG_DHCP_CPP_DHCP_LEASE - PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_LEASE); - #endif - while (_dhcp_state != STATE_DHCP_LEASED) { - #if defined DEBUG_DHCP_CPP_DHCP_LEASE - PRINTVAR(_dhcp_state); - #endif - if (_dhcp_state == STATE_DHCP_START) { _dhcpTransactionId++; send_DHCP_MESSAGE(DHCP_DISCOVER, ((millis() - startTime) / 1000)); @@ -147,10 +104,6 @@ int DhcpClass::request_DHCP_lease() break; } - #if defined DEBUG_DHCP_CPP_DHCP_LEASE - PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_LEASE); - #endif - // We're done with the socket now _dhcpUdpSocket.stop(); _dhcpTransactionId++; @@ -169,10 +122,6 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed) memset(buffer, 0, 32); IPAddress dest_addr(255, 255, 255, 255); // Broadcast address - #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE - PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); - #endif - if (_dhcpUdpSocket.beginPacket(dest_addr, DHCP_SERVER_PORT) == -1) { //Serial.printf("DHCP transmit error\n"); // FIXME Need to return errors @@ -184,10 +133,6 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed) buffer[2] = DHCP_HLENETHERNET; // hlen buffer[3] = DHCP_HOPS; // hops - #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE - PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); - #endif - // xid unsigned long xid = htonl(_dhcpTransactionId); memcpy(buffer + 4, &(xid), 4); @@ -196,10 +141,6 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed) buffer[8] = ((secondsElapsed & 0xff00) >> 8); buffer[9] = (secondsElapsed & 0x00ff); - #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE - PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); - #endif - // flags unsigned short flags = htons(DHCP_FLAGSBROADCAST); memcpy(buffer + 10, &(flags), 2); @@ -209,49 +150,21 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed) // siaddr: already zeroed // giaddr: already zeroed - #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE - PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); - #endif - //put data in W5100 transmit buffer _dhcpUdpSocket.write(buffer, 28); - #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE - PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); - #endif - memset(buffer, 0, 32); // clear local buffer - #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE - PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); - #endif - memcpy(buffer, _dhcpMacAddr, 6); // chaddr - #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE - PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); - #endif - //put data in W5100 transmit buffer _dhcpUdpSocket.write(buffer, 16); - #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE - PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); - #endif - memset(buffer, 0, 32); // clear local buffer - #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE - PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); - #endif - // leave zeroed out for sname && file // put in W5100 transmit buffer x 6 (192 bytes) - #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE - PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); - #endif - for(int i = 0; i < 6; i++) { _dhcpUdpSocket.write(buffer, 32); } @@ -278,18 +191,10 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed) buffer[17] = strlen(HOST_NAME) + 6; // length of hostname + last 3 bytes of mac address strcpy((char*)&(buffer[18]), HOST_NAME); - #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE - PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); - #endif - printByte((char*)&(buffer[24]), _dhcpMacAddr[3]); printByte((char*)&(buffer[26]), _dhcpMacAddr[4]); printByte((char*)&(buffer[28]), _dhcpMacAddr[5]); - #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE - PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); - #endif - //put data in W5100 transmit buffer _dhcpUdpSocket.write(buffer, 30); @@ -322,17 +227,9 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed) buffer[7] = dhcpT2value; buffer[8] = endOption; - #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE - PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); - #endif - //put data in W5100 transmit buffer _dhcpUdpSocket.write(buffer, 9); - #if defined DEBUG_DHCP_CPP_DHCP_MESSAGE - PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCP_MESSAGE); - #endif - _dhcpUdpSocket.endPacket(); } @@ -343,28 +240,16 @@ uint8_t DhcpClass::parseDHCPResponse(unsigned long responseTimeout, uint32_t& tr unsigned long startTime = millis(); - #if defined DEBUG_DHCP_CPP_DHCPRESPONSE - PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCPRESPONSE); - #endif - while (_dhcpUdpSocket.parsePacket() <= 0) { if ((millis() - startTime) > responseTimeout) { return 255; } delay(50); } - - #if defined DEBUG_DHCP_CPP_DHCPRESPONSE - PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCPRESPONSE); - #endif // start reading in the packet RIP_MSG_FIXED fixedMsg; _dhcpUdpSocket.read((uint8_t*)&fixedMsg, sizeof(RIP_MSG_FIXED)); - #if defined DEBUG_DHCP_CPP_DHCPRESPONSE - PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCPRESPONSE); - #endif - if (fixedMsg.op == DHCP_BOOTREPLY && _dhcpUdpSocket.remotePort() == DHCP_SERVER_PORT) { transactionId = ntohl(fixedMsg.xid); if (memcmp(fixedMsg.chaddr, _dhcpMacAddr, 6) != 0 || @@ -449,10 +334,6 @@ uint8_t DhcpClass::parseDHCPResponse(unsigned long responseTimeout, uint32_t& tr } } - #if defined DEBUG_DHCP_CPP_DHCPRESPONSE - PRINTLINE_DEF(DEBUG_DHCP_CPP_DHCPRESPONSE); - #endif - // Need to skip to end of the packet regardless here _dhcpUdpSocket.flush(); // FIXME diff --git a/src/Dns.cpp b/src/Dns.cpp index 19c310ef..dfa91125 100644 --- a/src/Dns.cpp +++ b/src/Dns.cpp @@ -7,7 +7,6 @@ #include "Dns.h" #include "utility/w5100.h" -//#define DEBUG_W5100_CPP_GETHOSTBYNAME #define SOCKET_NONE 255 // Various flags and header field values for a DNS message @@ -88,10 +87,6 @@ int DNSClient::inet_aton(const char* address, IPAddress& result) int DNSClient::getHostByName(const char* aHostname, IPAddress& aResult, uint16_t timeout) { - #if defined DEBUG_W5100_CPP_GETHOSTBYNAME - PRINTLINE(); - #endif - int ret = 0; // See if it's a numeric IP address @@ -100,56 +95,28 @@ int DNSClient::getHostByName(const char* aHostname, IPAddress& aResult, uint16_t return 1; } - #if defined DEBUG_W5100_CPP_GETHOSTBYNAME - PRINTLINE(); - #endif - // Check we've got a valid DNS server to use if (iDNSServer == INADDR_NONE) { return INVALID_SERVER; } - #if defined DEBUG_W5100_CPP_GETHOSTBYNAME - PRINTLINE(); - #endif - // Find a socket to use if (iUdp.begin(1024+(millis() & 0xF)) == 1) { // Try up to three times int retries = 0; // while ((retries < 3) && (ret <= 0)) { // Send DNS request - - #if defined DEBUG_W5100_CPP_GETHOSTBYNAME - PRINTLINE(); - #endif - ret = iUdp.beginPacket(iDNSServer, DNS_PORT); if (ret != 0) { // Now output the request data - - #if defined DEBUG_W5100_CPP_GETHOSTBYNAME - PRINTLINE(); - #endif - ret = BuildRequest(aHostname); if (ret != 0) { // And finally send the request - - #if defined DEBUG_W5100_CPP_GETHOSTBYNAME - PRINTLINE(); - #endif - ret = iUdp.endPacket(); if (ret != 0) { // Now wait for a response int wait_retries = 0; ret = TIMED_OUT; - - #if defined DEBUG_W5100_CPP_GETHOSTBYNAME - PRINTLINE(); - #endif - while ((wait_retries < 3) && (ret == TIMED_OUT)) { ret = ProcessResponse(timeout, aResult); wait_retries++; @@ -160,18 +127,10 @@ int DNSClient::getHostByName(const char* aHostname, IPAddress& aResult, uint16_t retries++; //} - #if defined DEBUG_W5100_CPP_GETHOSTBYNAME - PRINTLINE(); - #endif - // We're done with the socket now iUdp.stop(); } - #if defined DEBUG_W5100_CPP_GETHOSTBYNAME - PRINTLINE(); - #endif - return ret; } diff --git a/src/Ethernet.cpp b/src/Ethernet.cpp index f7655d68..28658623 100644 --- a/src/Ethernet.cpp +++ b/src/Ethernet.cpp @@ -23,8 +23,6 @@ #include "utility/w5100.h" #include "Dhcp.h" -//#define DEBUG_ETHERNET_CPP_BEGIN - IPAddress EthernetClass::_dnsServerAddress; DhcpClass* EthernetClass::_dhcp = NULL; @@ -36,60 +34,23 @@ int EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long resp // Initialise the basic info if (W5100.init() == 0) return 0; SPI.beginTransaction(SPI_ETHERNET_SETTINGS); - - #if defined DEBUG_ETHERNET_CPP_BEGIN - PRINTLINE(); - #endif W5100.setMACAddress(mac); - - #if defined DEBUG_ETHERNET_CPP_BEGIN - PRINTLINE(); - #endif W5100.setIPAddress(IPAddress(0,0,0,0).raw_address()); SPI.endTransaction(); - #if defined DEBUG_ETHERNET_CPP_BEGIN - PRINTLINE(); - #endif // Now try to get our config info from a DHCP server int ret = _dhcp->beginWithDHCP(mac, timeout, responseTimeout); if (ret == 1) { // We've successfully found a DHCP server and got our configuration // info, so set things accordingly SPI.beginTransaction(SPI_ETHERNET_SETTINGS); - - #if defined DEBUG_ETHERNET_CPP_BEGIN - PRINTLINE(); - #endif W5100.setIPAddress(_dhcp->getLocalIp().raw_address()); - #if defined DEBUG_ETHERNET_CPP_BEGIN - PRINTLINE(); - #endif W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address()); - #if defined DEBUG_ETHERNET_CPP_BEGIN - PRINTLINE(); - #endif W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address()); - #if defined DEBUG_ETHERNET_CPP_BEGIN - PRINTLINE(); - #endif SPI.endTransaction(); - #if defined DEBUG_ETHERNET_CPP_BEGIN - PRINTLINE(); - #endif _dnsServerAddress = _dhcp->getDnsServerIp(); - #if defined DEBUG_ETHERNET_CPP_BEGIN - PRINTLINE(); - #endif socketPortRand(micros()); - #if defined DEBUG_ETHERNET_CPP_BEGIN - PRINTLINE(); - #endif } - - #if defined DEBUG_ETHERNET_CPP_BEGIN - PRINTLINE(); - #endif return ret; } @@ -157,7 +118,6 @@ EthernetHardwareStatus EthernetClass::hardwareStatus() case 51: return EthernetW5100; case 52: return EthernetW5200; case 55: return EthernetW5500; - case 61: return EthernetW6100; default: return EthernetNoHardware; } } diff --git a/src/Ethernet.h b/src/Ethernet.h index 969565be..dbc9a63d 100644 --- a/src/Ethernet.h +++ b/src/Ethernet.h @@ -47,79 +47,6 @@ // 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 #include "Client.h" @@ -137,8 +64,7 @@ enum EthernetHardwareStatus { EthernetW5100, EthernetW5200, EthernetW5500, - EthernetW5100S, - EthernetW6100 + EthernetW5100S }; class EthernetUDP; diff --git a/src/EthernetUdp.cpp b/src/EthernetUdp.cpp index fa91b958..51669d69 100644 --- a/src/EthernetUdp.cpp +++ b/src/EthernetUdp.cpp @@ -31,31 +31,12 @@ #include "Dns.h" #include "utility/w5100.h" -//#define DEBUG_ETHERNET_UDP_CPP_PARSEPACKET -//#define DEBUG_ETHERNET_UDP_READ -//#define DEBUG_ETHERNETUDP_CPP_WRITE -//#define DEBUG_ETHERNET_UDP_CPP_BEGINPACKET -//#define DEBUG_ETHERNET_UDP_CPP_BEGIN - /* Start EthernetUDP socket, listening at local port PORT */ uint8_t EthernetUDP::begin(uint16_t port) { - #if defined DEBUG_ETHERNET_UDP_CPP_BEGIN - PRINTLINE(); - #endif if (sockindex < MAX_SOCK_NUM) Ethernet.socketClose(sockindex); - #if defined DEBUG_ETHERNET_UDP_CPP_BEGIN - PRINTLINE(); - #endif - sockindex = Ethernet.socketBegin(SnMR::UDP, port); - #if defined DEBUG_ETHERNET_UDP_CPP_BEGIN - PRINTLINE(); - PRINTVAR(sockindex); - #endif - - - if (sockindex >= MAX_SOCK_NUM) return 0; + if (sockindex >= MAX_SOCK_NUM) return 0; _port = port; _remaining = 0; return 1; @@ -79,46 +60,19 @@ void EthernetUDP::stop() int EthernetUDP::beginPacket(const char *host, uint16_t port) { - #if defined DEBUG_ETHERNET_UDP_CPP_BEGINPACKET - PRINTLINE(); - #endif - // Look up the host first int ret = 0; DNSClient dns; IPAddress remote_addr; - #if defined DEBUG_ETHERNET_UDP_CPP_BEGINPACKET - PRINTVAR_HEX(Ethernet.dnsServerIP()); - #endif - dns.begin(Ethernet.dnsServerIP()); - - #if defined DEBUG_ETHERNET_UDP_CPP_BEGINPACKET - PRINTLINE(); - #endif - ret = dns.getHostByName(host, remote_addr); - - #if defined DEBUG_ETHERNET_UDP_CPP_BEGINPACKET - PRINTLINE(); - #endif - if (ret != 1) return ret; - - #if defined DEBUG_ETHERNET_UDP_CPP_BEGINPACKET - PRINTLINE(); - #endif - return beginPacket(remote_addr, port); } int EthernetUDP::beginPacket(IPAddress ip, uint16_t port) { - #if defined DEBUG_ETHERNET_UDP_CPP_BEGINPACKET - PRINTLINE(); - #endif - _offset = 0; //Serial.printf("UDP beginPacket\n"); return Ethernet.socketStartUDP(sockindex, rawIPAddress(ip), port); @@ -136,18 +90,9 @@ size_t EthernetUDP::write(uint8_t byte) size_t EthernetUDP::write(const uint8_t *buffer, size_t size) { - #if defined DEBUG_ETHERNETUDP_CPP_WRITE - PRINTLINE(); - #endif - //Serial.printf("UDP write %d\n", size); uint16_t bytes_written = Ethernet.socketBufferData(sockindex, _offset, buffer, size); _offset += bytes_written; - - #if defined DEBUG_ETHERNETUDP_CPP_WRITE - PRINTLINE(); - #endif - return bytes_written; } @@ -163,129 +108,22 @@ int EthernetUDP::parsePacket() if (Ethernet.socketRecvAvailable(sockindex) > 0) { //HACK - hand-parse the UDP packet using TCP recv method - uint8_t tmpBuf[20]; + uint8_t tmpBuf[8]; int ret=0; - int i; - - #if defined DEBUG_ETHERNET_UDP_CPP_PARSEPACKET - PRINTVAR_HEX(_remaining); - PRINTLINE_DEF(DEBUG_ETHERNET_UDP_CPP_PARSEPACKET); - #endif - - if(W5100.getChip() == 61) - { - //read 2 header bytes and get one IPv4 or IPv6 - ret = Ethernet.socketRecv(sockindex, tmpBuf, 2); - if(ret > 0) - { - #if defined DEBUG_ETHERNET_UDP_CPP_PARSEPACKET - for(i = 0; i < 2; i++) - { - PRINTVAR_HEX(tmpBuf[0+i]); - } - PRINTVAR(ret); - #endif - - _remaining = (tmpBuf[0] & (0x7))<<8 | tmpBuf[1]; - #if defined DEBUG_ETHERNET_UDP_CPP_PARSEPACKET - PRINTVAR(_remaining); - #endif - - if((tmpBuf[0] & W6100_UDP_HEADER_IPV) == W6100_UDP_HEADER_IPV6) - { - // IPv6 UDP Recived - // 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); - #if defined DEBUG_ETHERNET_UDP_CPP_PARSEPACKET - for(i = 0; i < 18; i++) - { - PRINTVAR_HEX(tmpBuf[2+i]); - } - PRINTVAR(ret); - #endif - - _remoteIP = &tmpBuf[2]; - #if defined DEBUG_ETHERNET_UDP_CPP_PARSEPACKET - for(i = 0; i < 16; i++) - { - PRINTVAR(tmpBuf[2+i]); - } - #endif - - _remotePort = (tmpBuf[18]<<8) | tmpBuf[19]; - #if defined DEBUG_ETHERNET_UDP_CPP_PARSEPACKET - PRINTVAR(_remotePort); - #endif - } - else - { - // IPv4 UDP Recived - // 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); - #if defined DEBUG_ETHERNET_UDP_CPP_PARSEPACKET - for(i = 0; i < 6; i++) - { - PRINTVAR_HEX(tmpBuf[2+i]); - } - PRINTVAR(ret); - #endif - - _remoteIP = &tmpBuf[2]; - #if defined DEBUG_ETHERNET_UDP_CPP_PARSEPACKET - for(i = 0; i < 4; i++) - { - PRINTVAR(tmpBuf[2+i]); - } - #endif - - _remotePort = (tmpBuf[6]<<8) | tmpBuf[7]; - #if defined DEBUG_ETHERNET_UDP_CPP_PARSEPACKET - PRINTVAR(_remotePort); - #endif - } - - 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; - } + //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 defined DEBUG_ETHERNET_UDP_CPP_PARSEPACKET - PRINTLINE_DEF(DEBUG_ETHERNET_UDP_CPP_PARSEPACKET); - #endif - return ret; } - - #if defined DEBUG_ETHERNET_UDP_CPP_PARSEPACKET - PRINTLINE_DEF(DEBUG_ETHERNET_UDP_CPP_PARSEPACKET); - #endif - // There aren't any packets available return 0; } @@ -294,30 +132,12 @@ int EthernetUDP::read() { uint8_t byte; - #if defined DEBUG_ETHERNET_UDP_READ - PRINTLINE_DEF(DEBUG_ETHERNET_UDP_READ); - #endif - if ((_remaining > 0) && (Ethernet.socketRecv(sockindex, &byte, 1) > 0)) { - - #if defined DEBUG_ETHERNET_UDP_READ - PRINTVAR(_remaining); - #endif - // We read things without any problems _remaining--; - - #if defined DEBUG_ETHERNET_UDP_READ - PRINTVAR(_remaining); - #endif - return byte; } - #if defined DEBUG_ETHERNET_UDP_READ - PRINTLINE_DEF(DEBUG_ETHERNET_UDP_READ); - #endif - // If we get here, there's no data available return -1; } @@ -326,37 +146,17 @@ int EthernetUDP::read(unsigned char *buffer, size_t len) { if (_remaining > 0) { int got; - - #if defined DEBUG_ETHERNET_UDP_READ - PRINTVAR_HEX(len); - PRINTVAR_HEX(_remaining); - #endif - if (_remaining <= len) { // data should fit in the buffer got = Ethernet.socketRecv(sockindex, buffer, _remaining); - - #if defined DEBUG_ETHERNET_UDP_READ - PRINTVAR_HEX(got); - #endif - } else { // too much data for the buffer, // grab as much as will fit got = Ethernet.socketRecv(sockindex, buffer, len); - - #if defined DEBUG_ETHERNET_UDP_READ - PRINTVAR_HEX(got); - #endif } if (got > 0) { _remaining -= got; //Serial.printf("UDP read %d\n", got); - - #if defined DEBUG_ETHERNET_UDP_READ - PRINTVAR_HEX(got); - #endif - return got; } } diff --git a/src/socket.cpp b/src/socket.cpp index 156c2041..43f068ef 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -22,13 +22,6 @@ #include "Ethernet.h" #include "utility/w5100.h" -//#define DEBUG_SOCKET_CPP_WRITE_DATA -//#define DEBUG_SOCKET_CPP_SOCKETLISTEN -//#define DEBUG_SOCKET_CPP_SOCKETRECV -//#define DEBUG_SOCKET_CPP_SOCKETSENDUDP -//#define DEBUG_ETHERNET_UDP_CPP_SOCKETSTARTUDP -//#define DEBUG_SOCKET_CPP_BEGINE - #if ARDUINO >= 156 && !defined(ARDUINO_ARCH_PIC32) extern void yield(void); #else @@ -82,11 +75,6 @@ uint8_t EthernetClass::socketBegin(uint8_t protocol, uint16_t port) // look at all the hardware sockets, use any that are closed (unused) for (s=0; s < maxindex; s++) { status[s] = W5100.readSnSR(s); - - #if defined DEBUG_SOCKET_CPP_BEGINE - PRINTVAR_HEX(status[s]); - #endif - if (status[s] == SnSR::CLOSED) goto makesocket; } //Serial.printf("W5000socket step2\n"); @@ -317,17 +305,9 @@ int EthernetClass::socketRecv(uint8_t s, uint8_t *buf, int16_t len) { // Check how much data is available int ret = state[s].RX_RSR; - - #if defined DEBUG_SOCKET_CPP_SOCKETRECV - PRINTVAR_HEX(ret); - #endif - SPI.beginTransaction(SPI_ETHERNET_SETTINGS); if (ret < len) { uint16_t rsr = getSnRX_RSR(s); - #if defined DEBUG_SOCKET_CPP_SOCKETRECV - PRINTVAR_HEX(rsr); - #endif ret = rsr - state[s].RX_inc; state[s].RX_RSR = ret; //Serial.printf("Sock_RECV, RX_RSR=%d, RX_inc=%d\n", ret, state[s].RX_inc); @@ -335,9 +315,6 @@ int EthernetClass::socketRecv(uint8_t s, uint8_t *buf, int16_t len) if (ret == 0) { // No data available. uint8_t status = W5100.readSnSR(s); - #if defined DEBUG_SOCKET_CPP_SOCKETRECV - PRINTVAR_HEX(status); - #endif if ( status == SnSR::LISTEN || status == SnSR::CLOSED || status == SnSR::CLOSE_WAIT ) { // The remote end has closed its side of the connection, @@ -347,9 +324,6 @@ int EthernetClass::socketRecv(uint8_t s, uint8_t *buf, int16_t len) // The connection is still up, but there's no data waiting to be read ret = -1; } - #if defined DEBUG_SOCKET_CPP_SOCKETRECV - PRINTVAR_HEX(ret); - #endif } else { if (ret > len) ret = len; // more data available than buffer length uint16_t ptr = state[s].RX_RD; @@ -359,9 +333,6 @@ int EthernetClass::socketRecv(uint8_t s, uint8_t *buf, int16_t len) state[s].RX_RSR -= ret; uint16_t inc = state[s].RX_inc + ret; if (inc >= 250 || state[s].RX_RSR == 0) { - #if defined DEBUG_SOCKET_CPP_SOCKETRECV - PRINTVAR_HEX(inc); - #endif state[s].RX_inc = 0; W5100.writeSnRX_RD(s, ptr); W5100.execCmdSn(s, Sock_RECV); @@ -426,33 +397,12 @@ static uint16_t getSnTX_FSR(uint8_t s) static void write_data(uint8_t s, uint16_t data_offset, const uint8_t *data, uint16_t len) { - #if defined DEBUG_SOCKET_CPP_WRITE_DATA - PRINTSTR("write_data"); - #endif - uint16_t ptr = W5100.readSnTX_WR(s); - #if defined DEBUG_SOCKET_CPP_WRITE_DATA - PRINTVAR_HEX(ptr); - PRINTVAR_HEX(data_offset); - #endif ptr += data_offset; uint16_t offset = ptr & W5100.SMASK; uint16_t dstAddr = offset + W5100.SBASE(s); - #if defined DEBUG_SOCKET_CPP_WRITE_DATA - PRINTVAR_HEX(dstAddr); - PRINTVAR_HEX(W5100.SBASE(s)); - #endif - - #if defined DEBUG_SOCKET_CPP_WRITE_DATA - PRINTVAR_HEX(offset); - PRINTVAR_HEXT(s, data_offset, len); - #endif - if (W5100.hasOffsetAddressMapping() || offset + len <= W5100.SSIZE) { - #if defined DEBUG_SOCKET_CPP_WRITE_DATA - PRINTVAR_HEXT(dstAddr, *data, len); - #endif W5100.write(dstAddr, data, len); } else { // Wrap around circular buffer @@ -462,11 +412,6 @@ static void write_data(uint8_t s, uint16_t data_offset, const uint8_t *data, uin } ptr += len; W5100.writeSnTX_WR(s, ptr); - - #if defined DEBUG_SOCKET_CPP_WRITE_DATA - PRINTVAR_HEX(offset); - PRINTVAR_HEXT(s, data_offset, len); - #endif } @@ -553,59 +498,22 @@ uint16_t EthernetClass::socketBufferData(uint8_t s, uint16_t offset, const uint8 bool EthernetClass::socketStartUDP(uint8_t s, uint8_t* addr, uint16_t port) { - #if defined DEBUG_ETHERNET_UDP_CPP_SOCKETSTARTUDP - PRINTLINE(); - #endif - if ( ((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) || ((port == 0x00)) ) { return false; } SPI.beginTransaction(SPI_ETHERNET_SETTINGS); - - #if defined DEBUG_ETHERNET_UDP_CPP_SOCKETSTARTUDP - PRINTLINE(); - PRINTVAR_HEX(s); - PRINTVAR_HEX(addr[0]); - PRINTVAR_HEX(addr[1]); - PRINTVAR_HEX(addr[2]); - PRINTVAR_HEX(addr[3]); - #endif - W5100.writeSnDIPR(s, addr); - - #if defined DEBUG_ETHERNET_UDP_CPP_SOCKETSTARTUDP - PRINTLINE(); - #endif - W5100.writeSnDPORT(s, port); - - #if defined DEBUG_ETHERNET_UDP_CPP_SOCKETSTARTUDP - PRINTLINE(); - #endif - SPI.endTransaction(); - - #if defined DEBUG_ETHERNET_UDP_CPP_SOCKETSTARTUDP - PRINTLINE(); - #endif - return true; } bool EthernetClass::socketSendUDP(uint8_t s) { - #if defined DEBUG_SOCKET_CPP_SOCKETSENDUDP - PRINTLINE(); - #endif - SPI.beginTransaction(SPI_ETHERNET_SETTINGS); W5100.execCmdSn(s, Sock_SEND); - #if defined DEBUG_SOCKET_CPP_SOCKETSENDUDP - PRINTLINE(); - #endif - /* +2008.01 bj */ while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK ) { if (W5100.readSnIR(s) & SnIR::TIMEOUT) { @@ -620,18 +528,10 @@ bool EthernetClass::socketSendUDP(uint8_t s) SPI.beginTransaction(SPI_ETHERNET_SETTINGS); } - #if defined DEBUG_SOCKET_CPP_SOCKETSENDUDP - PRINTLINE(); - #endif - /* +2008.01 bj */ W5100.writeSnIR(s, SnIR::SEND_OK); SPI.endTransaction(); - #if defined DEBUG_SOCKET_CPP_SOCKETSENDUDP - PRINTLINE(); - #endif - //Serial.printf("sendUDP ok\n"); /* Sent ok */ return true; diff --git a/src/utility/w5100.cpp b/src/utility/w5100.cpp index 516b623b..e9d5b906 100644 --- a/src/utility/w5100.cpp +++ b/src/utility/w5100.cpp @@ -12,25 +12,6 @@ #include "Ethernet.h" #include "w5100.h" -//#define DEBUG_W5100_CPP_ISW6100 -//#define DEBUG_W5100_CPP_ISW5200 -//#define DEBUG_W5100_CPP_ISW5500 -//#define DEBUG_W5100_CPP_ISW5100S -//#define DEBUG_W5100_CPP_ISW5100 - -//#define DEBUG_W5100_CPP_SOFTRESET - -//#define DEBUG_W5100_CPP_WRITE -//#define DEBUG_W5100_CPP_WRITE_COMMON -//#define DEBUG_W5100_CPP_WRITE_SOCKET -//#define DEBUG_W5100_CPP_WRITE_TX -//#define DEBUG_W5100_CPP_WRITE_RX - -//#define DEBUG_W5100_CPP_READ -//#define DEBUG_W5100_CPP_READ_COMMON -//#define DEBUG_W5100_CPP_READ_SOCKET -//#define DEBUG_W5100_CPP_READ_TX -//#define DEBUG_W5100_CPP_READ_RX /***************************************************/ /** Default SS pin setting **/ @@ -43,7 +24,7 @@ // MKR boards default to pin 5 for MKR ETH // Pins 8-10 are MOSI/SCK/MISO on MRK, so don't use pin 10 -#elif defined(USE_ARDUINO_MKR_PIN_LAYOUT) || defined(ARDUINO_SAMD_MKRZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRFox1200) || defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRVIDOR4000) +#elif defined(USE_ARDUINO_MKR_PIN_LAYOUT) || defined(ARDUINO_SAMD_MKRZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRFox1200) || defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRWAN1300) #define SS_PIN_DEFAULT 5 // For boards using AVR, assume shields with SS on pin 10 @@ -71,7 +52,6 @@ // W5100 controller instance uint8_t W5100Class::chip = 0; uint8_t W5100Class::CH_BASE_MSB; -uint16_t W5100Class::CH_SIZE; uint8_t W5100Class::ss_pin = SS_PIN_DEFAULT; #ifdef ETHERNET_LARGE_BUFFERS uint16_t W5100Class::SSIZE = 2048; @@ -120,8 +100,6 @@ uint8_t W5100Class::init(void) // reset time, this can be edited or removed. delay(560); //Serial.println("w5100 init"); - - CH_SIZE = 0x0100; // Default except W6100 SPI.begin(); initSS(); @@ -226,30 +204,6 @@ uint8_t W5100Class::init(void) #else writeTMSR(0x55); writeRMSR(0x55); -#endif - // Try W6100. Brandnew based W5500. - } else if (isW6100()) { - CH_BASE_MSB = 0x60; - CH_SIZE = 0x0400; // W6100 -#ifdef ETHERNET_LARGE_BUFFERS -#if MAX_SOCK_NUM <= 1 - SSIZE = 16384; -#elif MAX_SOCK_NUM <= 2 - SSIZE = 8192; -#elif MAX_SOCK_NUM <= 4 - SSIZE = 4096; -#else - SSIZE = 2048; -#endif - SMASK = SSIZE - 1; - for (i=0; i> 10); - writeSnTX_SIZE(i, SSIZE >> 10); - } - for (; i<8; i++) { - writeSnRX_SIZE(i, 0); - writeSnTX_SIZE(i, 0); - } #endif // No hardware seems to be present. Or it could be a W5200 // that's heard other SPI communication if its chip select @@ -269,101 +223,23 @@ uint8_t W5100Class::init(void) uint8_t W5100Class::softReset(void) { uint16_t count=0; - uint8_t sysr; - - if(chip == 61) - { - writeCHPLCKR_W6100(W6100_CHPLCKR_UNLOCK); // Unlock SYSR[CHPL] - count = 0; - do{ // Wait Unlock Complete - if(++count > 20) // Check retry count - { - #if defined DEBUG_W5100_CPP_SOFTRESET - PRINTSTR("count !< 20"); - #endif - return 0; // Over Limit retry count - } - #if defined DEBUG_W5100_CPP_SOFTRESET - PRINTVAR(count); - #endif - } while ((readSYSR_W6100() & W6100_SYSR_CHPL_LOCK) ^ W6100_SYSR_CHPL_ULOCK); // Exit Wait Unlock Complete - - writeSYCR0(0x0); // Software Reset - - do{ // Wait Lock Complete - if(++count > 20) // Check retry count - { - #if defined DEBUG_W5100_CPP_SOFTRESET - PRINTSTR("count !< 20"); - #endif - return 0; // Over Limit retry count - } - - #if defined DEBUG_W5100_CPP_SOFTRESET - PRINTVAR(count); - #endif - } while ((readSYSR_W6100() & W6100_SYSR_CHPL_LOCK) ^ W6100_SYSR_CHPL_LOCK); // Exit Wait Lock Complete - return 1; - } - else - { - count = 0; - - //Serial.println("Wiznet soft reset"); - // write to reset bit - writeMR(0x80); - // then wait for soft reset to complete - do { - uint8_t mr = readMR(); - //Serial.print("mr="); - //Serial.println(mr, HEX); - if (mr == 0 || (mr == 3 && chip == 50)) return 1; - delay(1); - } while (++count < 20); - return 0; - } -} - -uint8_t W5100Class::isW6100(void) -{ - #if defined DEBUG_W5100_CPP_ISW6100 - PRINTSTR("isW6100"); - #endif - - chip = 61; - CH_BASE_MSB = 0x80; - - if (!softReset()) return 0; - - // Unlock - writeCHPLCKR_W6100(W6100_CHPLCKR_UNLOCK); - writeNETLCKR_W6100(W6100_NETLCKR_UNLOCK); - writePHYLCKR_W6100(W6100_PHYLCKR_UNLOCK); - - // W6100 CIDR0 - // Version 97(dec) 0x61(hex) - int ver = readVERSIONR_W6100(); - - #if defined DEBUG_W5100_CPP_ISW6100 - Serial.print("version = 0x"); - Serial.println(ver, HEX); - #endif - - if (ver != 97) return 0; - #if defined DEBUG_W5100_CPP_ISW6100 - Serial.println("chip is W6100"); - #endif - - return 1; + //Serial.println("Wiznet soft reset"); + // write to reset bit + writeMR(0x80); + // then wait for soft reset to complete + do { + uint8_t mr = readMR(); + //Serial.print("mr="); + //Serial.println(mr, HEX); + if (mr == 0 || (mr == 3 && chip == 50)) return 1; + delay(1); + } while (++count < 20); + return 0; } uint8_t W5100Class::isW5100(void) { - #if defined DEBUG_W5100_CPP_ISW5100 - PRINTSTR("isW5100"); - #endif - chip = 51; //Serial.println("w5100.cpp: detect W5100 chip"); if (!softReset()) return 0; @@ -374,20 +250,11 @@ uint8_t W5100Class::isW5100(void) writeMR(0x00); if (readMR() != 0x00) return 0; //Serial.println("chip is W5100"); - - #if defined DEBUG_W5100_CPP_ISW5100 - PRINTSTR("chip is W5100"); - #endif - return 1; } uint8_t W5100Class::isW5100S(void) { - #if defined DEBUG_W5100_CPP_ISW5100S - PRINTSTR("isW5100S"); - #endif - chip = 50; //Serial.println("w5100.cpp: detect W5100S chip"); if (!softReset()) return 0; @@ -400,11 +267,6 @@ uint8_t W5100Class::isW5100S(void) //Serial.println(ver); if (ver != 81) return 0; //Serial.println("chip is W5100S"); - - #if defined DEBUG_W5100_CPP_ISW5100S - PRINTSTR("chip is W5100S"); - #endif - return 1; } @@ -427,10 +289,6 @@ uint8_t W5100Class::isW5100S(void) uint8_t W5100Class::isW5200(void) { - #if defined DEBUG_W5100_CPP_ISW5200 - PRINTSTR("isW5200"); - #endif - chip = 52; //Serial.println("w5100.cpp: detect W5200 chip"); if (!softReset()) return 0; @@ -445,20 +303,11 @@ uint8_t W5100Class::isW5200(void) //Serial.println(ver); if (ver != 3) return 0; //Serial.println("chip is W5200"); - - #if defined DEBUG_W5100_CPP_ISW5200 - PRINTSTR("chip is W5200"); - #endif - return 1; } uint8_t W5100Class::isW5500(void) { - #if defined DEBUG_W5100_CPP_ISW5500 - PRINTSTR("isW5500"); - #endif - chip = 55; //Serial.println("w5100.cpp: detect W5500 chip"); if (!softReset()) return 0; @@ -473,11 +322,6 @@ uint8_t W5100Class::isW5500(void) //Serial.println(ver); if (ver != 4) return 0; //Serial.println("chip is W5500"); - - #if defined DEBUG_W5100_CPP_ISW5500 - PRINTSTR("chip is W5500"); - #endif - return 1; } @@ -505,12 +349,6 @@ W5100Linkstatus W5100Class::getLinkStatus() SPI.endTransaction(); if (phystatus & 0x01) return LINK_ON; return LINK_OFF; - case 61: - SPI.beginTransaction(SPI_ETHERNET_SETTINGS); - phystatus = readPHYCFGR_W6100(); - SPI.endTransaction(); - if (phystatus & 0x01) return LINK_ON; - return LINK_OFF; default: return UNKNOWN; } @@ -546,139 +384,6 @@ uint16_t W5100Class::write(uint16_t addr, const uint8_t *buf, uint16_t len) } #endif resetSS(); - } else if (chip == 61) { // chip == 61 - setSS(); - - #if defined DEBUG_W5100_CPP_WRITE - PRINTSTR("write"); - #endif - - if (addr < CH_BASE()) { - // common registers - #if defined DEBUG_W5100_CPP_WRITE_COMMON - PRINTSTR("common register"); - #endif - - cmd[0] = (addr>>8) & 0x7F; - cmd[1] = addr & 0xFF; - cmd[2] = W6100_SPI_FRAME_CTL_BSB_BLK(0) - | W6100_SPI_FRAME_CTL_BSB_COMM - | W6100_SPI_FRAME_CTL_WD - | W6100_SPI_FRAME_CTL_OPM_VDM - ; - - #if defined DEBUG_W5100_CPP_WRITE_COMMON - PRINTVAR_HEX(addr); - PRINTVAR_HEX(cmd[0]); - PRINTVAR_HEX(cmd[1]); - PRINTVAR_HEX(cmd[2]); - #endif - - } else if (addr < W6100_TX_BASE_ADDR) { - // socket registers - #if defined DEBUG_W5100_CPP_WRITE_SOCKET - PRINTSTR("socket register"); - #endif - - cmd[0] = (addr>>8) & 0x3; - cmd[1] = addr & 0xFF; - cmd[2] = W6100_SPI_FRAME_CTL_BSB_BLK((addr>>10)&0x7) - | W6100_SPI_FRAME_CTL_BSB_SOCK - | W6100_SPI_FRAME_CTL_WD - | W6100_SPI_FRAME_CTL_OPM_VDM - ; - - #if defined DEBUG_W5100_CPP_WRITE_SOCKET - PRINTVAR_HEX(addr); - PRINTVAR_HEX(cmd[0]); - PRINTVAR_HEX(cmd[1]); - PRINTVAR_HEX(cmd[2]); - #endif - - } else if (addr < W6100_RX_BASE_ADDR) { - // transmit buffers - - #if defined DEBUG_W5100_CPP_WRITE_TX - PRINTSTR("transmit buffer register"); - #endif - - cmd[0] = addr>>8; - cmd[1] = addr & 0xFF; - - #if defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 1 - cmd[2] = 0; // 16K buffers - #elif defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 2 - cmd[2] = ((addr >> 8) & 0x20); // 8K buffers - #elif defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 4 - cmd[2] = ((addr >> 7) & 0x60); // 4K buffers - #else - cmd[2] = ((addr >> 6) & 0xE0); // 2K buffers - #endif - - cmd[2] |= W6100_SPI_FRAME_CTL_BSB_BLK(0) - | W6100_SPI_FRAME_CTL_BSB_TXBF - | W6100_SPI_FRAME_CTL_WD - | W6100_SPI_FRAME_CTL_OPM_VDM - ; - - #if defined DEBUG_W5100_CPP_WRITE_TX - PRINTVAR_HEX(addr); - PRINTVAR_HEX(cmd[0]); - PRINTVAR_HEX(cmd[1]); - PRINTVAR_HEX(cmd[2]); - #endif - - } else { - // receive buffers - - #if defined DEBUG_W5100_CPP_WRITE_RX - PRINTSTR("receive buffer register"); - #endif - - cmd[0] = addr>>8; - cmd[1] = addr & 0xFF; - - #if defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 1 - cmd[2] = 0; // 16K buffers - #elif defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 2 - cmd[2] = ((addr >> 8) & 0x20); // 8K buffers - #elif defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 4 - cmd[2] = ((addr >> 7) & 0x60); // 4K buffers - #else - cmd[2] = ((addr >> 6) & 0xE0); // 2K buffers - #endif - - cmd[2] |= W6100_SPI_FRAME_CTL_BSB_BLK(0) - | W6100_SPI_FRAME_CTL_BSB_RXBF - | W6100_SPI_FRAME_CTL_WD - | W6100_SPI_FRAME_CTL_OPM_VDM - ; - - #if defined DEBUG_W5100_CPP_WRITE_RX - PRINTVAR_HEX(addr); - PRINTVAR_HEX(cmd[0]); - PRINTVAR_HEX(cmd[1]); - PRINTVAR_HEX(cmd[2]); - #endif - - } - if (len <= 5) { - for (uint8_t i=0; i < len; i++) { - cmd[i + 3] = buf[i]; - } - SPI.transfer(cmd, len + 3); - } else { - SPI.transfer(cmd, 3); -#ifdef SPI_HAS_TRANSFER_BUF - SPI.transfer(buf, NULL, len); -#else - // TODO: copy 8 bytes at a time to cmd[] and block transfer - for (uint16_t i=0; i < len; i++) { - SPI.transfer(buf[i]); - } -#endif - } - resetSS(); } else { // chip == 55 setSS(); if (addr < 0x100) { @@ -774,126 +479,6 @@ uint16_t W5100Class::read(uint16_t addr, uint8_t *buf, uint16_t len) memset(buf, 0, len); SPI.transfer(buf, len); resetSS(); - } else if (chip == 61) { // chip == 61 - setSS(); - - #if defined DEBUG_W5100_CPP_READ - PRINTSTR("read"); - #endif - - if (addr < CH_BASE()) { - // common registers - #if defined DEBUG_W5100_CPP_READ_COMMON - PRINTSTR("common register"); - #endif - - cmd[0] = (addr>>8) & 0x7F; - cmd[1] = addr & 0xFF; - cmd[2] = W6100_SPI_FRAME_CTL_BSB_BLK(0) - | W6100_SPI_FRAME_CTL_BSB_COMM - | W6100_SPI_FRAME_CTL_RD - | W6100_SPI_FRAME_CTL_OPM_VDM - ; - - #if defined DEBUG_W5100_CPP_READ_COMMON - PRINTVAR_HEX(addr); - PRINTVAR_HEX(cmd[0]); - PRINTVAR_HEX(cmd[1]); - PRINTVAR_HEX(cmd[2]); - #endif - - } else if (addr < W6100_TX_BASE_ADDR) { - // socket registers - #if defined DEBUG_W5100_CPP_READ_SOCKET - PRINTSTR("socket register"); - #endif - - cmd[0] = (addr>>8) & 0x3; - cmd[1] = addr & 0xFF; - cmd[2] = W6100_SPI_FRAME_CTL_BSB_BLK((addr>>10)&0x7) - | W6100_SPI_FRAME_CTL_BSB_SOCK - | W6100_SPI_FRAME_CTL_RD - | W6100_SPI_FRAME_CTL_OPM_VDM - ; - - #if defined DEBUG_W5100_CPP_READ_SOCKET - PRINTVAR_HEX(addr); - PRINTVAR_HEX(cmd[0]); - PRINTVAR_HEX(cmd[1]); - PRINTVAR_HEX(cmd[2]); - #endif - - } else if (addr < W6100_RX_BASE_ADDR) { - // transmit buffers - - #if defined DEBUG_W5100_CPP_READ_TX - PRINTSTR("transmit buffer register"); - #endif - - cmd[0] = addr>>8; - cmd[1] = addr & 0xFF; - - #if defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 1 - cmd[2] = 0; // 16K buffers - #elif defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 2 - cmd[2] = ((addr >> 8) & 0x20); // 8K buffers - #elif defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 4 - cmd[2] = ((addr >> 7) & 0x60); // 4K buffers - #else - cmd[2] = ((addr >> 6) & 0xE0); // 2K buffers - #endif - - cmd[2] |= W6100_SPI_FRAME_CTL_BSB_BLK(0) - | W6100_SPI_FRAME_CTL_BSB_TXBF - | W6100_SPI_FRAME_CTL_RD - | W6100_SPI_FRAME_CTL_OPM_VDM - ; - - #if defined DEBUG_W5100_CPP_READ_TX - PRINTVAR_HEX(addr); - PRINTVAR_HEX(cmd[0]); - PRINTVAR_HEX(cmd[1]); - PRINTVAR_HEX(cmd[2]); - #endif - - } else { - // receive buffers - - #if defined DEBUG_W5100_CPP_READ_RX - PRINTSTR("receive buffer register"); - #endif - - cmd[0] = addr>>8; - cmd[1] = addr & 0xFF; - - #if defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 1 - cmd[2] = 0; // 16K buffers - #elif defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 2 - cmd[2] = ((addr >> 8) & 0x20); // 8K buffers - #elif defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 4 - cmd[2] = ((addr >> 7) & 0x60); // 4K buffers - #else - cmd[2] = ((addr >> 6) & 0xE0); // 2K buffers - #endif - - cmd[2] |= W6100_SPI_FRAME_CTL_BSB_BLK(0) - | W6100_SPI_FRAME_CTL_BSB_RXBF - | W6100_SPI_FRAME_CTL_RD - | W6100_SPI_FRAME_CTL_OPM_VDM - ; - - #if defined DEBUG_W5100_CPP_READ_RX - PRINTVAR_HEX(addr); - PRINTVAR_HEX(cmd[0]); - PRINTVAR_HEX(cmd[1]); - PRINTVAR_HEX(cmd[2]); - #endif - - } - SPI.transfer(cmd, 3); - memset(buf, 0, len); - SPI.transfer(buf, len); - resetSS(); } else { // chip == 55 setSS(); if (addr < 0x100) { diff --git a/src/utility/w5100.h b/src/utility/w5100.h index bfc8deb6..91e81814 100644 --- a/src/utility/w5100.h +++ b/src/utility/w5100.h @@ -17,10 +17,6 @@ #include #include -//#define DEBUG_W5100_H -//#define DEBUG_W5100_H_WRITESN -//#define DEBUG_W5100_H_READSN - // Safe for all chips #define SPI_ETHERNET_SETTINGS SPISettings(14000000, MSBFIRST, SPI_MODE0) @@ -165,146 +161,58 @@ class W5100Class { return data; } -#define W6100_SPI_FRAME_CTL_BSB_BLK(sn) ((sn)<<5) - -#define W6100_SPI_FRAME_CTL_BSB_COMM (0<<3) -#define W6100_SPI_FRAME_CTL_BSB_SOCK (1<<3) -#define W6100_SPI_FRAME_CTL_BSB_TXBF (2<<3) -#define W6100_SPI_FRAME_CTL_BSB_RXBF (3<<3) - -#define W6100_SPI_FRAME_CTL_RD (0<<2) -#define W6100_SPI_FRAME_CTL_WD (1<<2) - -#define W6100_SPI_FRAME_CTL_OPM_VDM (0<<0) -#define W6100_SPI_FRAME_CTL_OPM_FDM1 (1<<0) -#define W6100_SPI_FRAME_CTL_OPM_FDM2 (2<<0) -#define W6100_SPI_FRAME_CTL_OPM_FDM4 (3<<0) - -#define W6100_COMMON_BASE_ADDR (0x0000) -#define W6100_SOCKET_BASE_ADDR (0x6000) -#define W6100_TX_BASE_ADDR (0x8000) -#define W6100_RX_BASE_ADDR (0xC000) -#define W6100_SOCKET_NUM(_s) ((_s)<<10)) - -#define W6100_CHPLCKR_UNLOCK 0xCE -#define W6100_NETLCKR_UNLOCK 0x3A -#define W6100_PHYLCKR_UNLOCK 0x53 - -#define W6100_SYSR_CHPL_LOCK (1<<7) -#define W6100_SYSR_CHPL_ULOCK (0<<7) - -#define W6100_UDP_HEADER_IPV (1<<7) -#define W6100_UDP_HEADER_IPV4 (0<<7) -#define W6100_UDP_HEADER_IPV6 (1<<7) -#define W6100_UDP_HEADER_ALL (1<<6) -#define W6100_UDP_HEADER_MUL (1<<5) -#define W6100_UDP_HEADER_GUA (0<<3) -#define W6100_UDP_HEADER_LLA (1<<3) - -#define __GP_REGISTER8(name, address, adrss_w6100) \ - static inline void write##name(uint8_t _data) { \ - if(chip == 61) { \ - write(adrss_w6100, _data); \ - } else { \ - write(address, _data); \ - } \ +#define __GP_REGISTER8(name, address) \ + static inline void write##name(uint8_t _data) { \ + write(address, _data); \ } \ static inline uint8_t read##name() { \ - if(chip == 61) { \ - return read(adrss_w6100); \ - } else { \ - return read(address); \ - } \ - } \ - -#define __GP_REGISTER16(name, address, adrss_w6100) \ + return read(address); \ + } +#define __GP_REGISTER16(name, address) \ static void write##name(uint16_t _data) { \ uint8_t buf[2]; \ buf[0] = _data >> 8; \ buf[1] = _data & 0xFF; \ - if(chip == 61) { \ - write(adrss_w6100, buf, 2); \ - } else { \ - write(address, buf, 2); \ - } \ + write(address, buf, 2); \ } \ static uint16_t read##name() { \ uint8_t buf[2]; \ - if(chip == 61) { \ - read(adrss_w6100, buf, 2); \ - } else { \ - read(address, buf, 2); \ - } \ + read(address, buf, 2); \ return (buf[0] << 8) | buf[1]; \ } - -#define __GP_REGISTER_N(name, address, adrss_w6100, size) \ +#define __GP_REGISTER_N(name, address, size) \ static uint16_t write##name(const uint8_t *_buff) { \ - if(chip == 61) { \ - return write(adrss_w6100, _buff, size); \ - } else { \ - return write(address, _buff, size); \ - } \ - } \ - static uint16_t read##name(uint8_t *_buff) { \ - if(chip == 61) { \ - return read(adrss_w6100, _buff, size); \ - } else { \ - return read(address, _buff, size); \ - } \ + return write(address, _buff, size); \ + } \ + static uint16_t read##name(uint8_t *_buff) { \ + return read(address, _buff, size); \ } static W5100Linkstatus getLinkStatus(); public: - __GP_REGISTER8 (MR, 0x0000, 0x4000); // Mode - __GP_REGISTER_N(GAR, 0x0001, 0x4130, 4); // Gateway IP address - __GP_REGISTER_N(SUBR, 0x0005, 0x4134, 4); // Subnet mask address - __GP_REGISTER_N(SHAR, 0x0009, 0x4120, 6); // Source MAC address - __GP_REGISTER_N(SIPR, 0x000F, 0x4138, 4); // Source IP address - __GP_REGISTER8 (IR, 0x0015, 0x2100); // Interrupt - __GP_REGISTER8 (IMR, 0x0016, 0x2104); // Interrupt Mask - __GP_REGISTER16(RTR, 0x0017, 0x2101); // Timeout address - __GP_REGISTER8 (RCR, 0x0019, 0x4200); // Retry count - __GP_REGISTER8 (RMSR, 0x001A, 0x0000); // Receive memory size (W5100 only) - __GP_REGISTER8 (TMSR, 0x001B, 0x0000); // Transmit memory size (W5100 only) - __GP_REGISTER8 (PATR, 0x001C, 0x0000); // Authentication type address in PPPoE mode - __GP_REGISTER8 (PTIMER, 0x0028, 0x4100); // PPP LCP Request Timer - __GP_REGISTER8 (PMAGIC, 0x0029, 0x4104); // PPP LCP Magic Number - __GP_REGISTER_N(UIPR, 0x002A, 0x0000, 4); // Unreachable IP address in UDP mode (W5100 only) - __GP_REGISTER16(UPORT, 0x002E, 0x0000); // Unreachable Port address in UDP mode (W5100 only) - __GP_REGISTER8 (VERSIONR_W5200, 0x001F, 0x0000); // Chip Version Register (W5200 only) - __GP_REGISTER8 (VERSIONR_W5500, 0x0039, 0x0000); // Chip Version Register (W5500 only) - __GP_REGISTER8 (VERSIONR_W5100S, 0x0080, 0x0000); // Chip Version Register (W5100S only) - __GP_REGISTER8 (PSTATUS_W5200, 0x0035, 0x0000); // PHY Status - __GP_REGISTER8 (PHYCFGR_W5500, 0x002E, 0x0000); // PHY Configuration register, default: 10111xxx - __GP_REGISTER8 (PHYCFGR_W5100S, 0x003C, 0x0000); // PHY Status - - // For W6100 - // System Command Register - __GP_REGISTER8 (SYCR0, 0x0000, 0x2004); - - // System Status Register - __GP_REGISTER8 (SYSR_W6100, 0x0000, 0x2000); - - // LOCK Register - - // Network Lock Register - __GP_REGISTER8 (NETLCKR_W6100, 0x0000, 0x41F5); - - // Chip Lock Register - __GP_REGISTER8 (CHPLCKR_W6100, 0x0000, 0x41F4); - - // PHY Lock Register - __GP_REGISTER8 (PHYLCKR_W6100, 0x0000, 0x41F6); - - // PHY Status - __GP_REGISTER8 (PHYCFGR_W6100, 0x0000, 0x3000); - - // Chip Version Register - __GP_REGISTER8 (VERSIONR_W6100, 0x0000, 0x0000); - - // Chip Version Register - __GP_REGISTER8 (CVERSIONR_W6100, 0x0000, 0x0002); + __GP_REGISTER8 (MR, 0x0000); // Mode + __GP_REGISTER_N(GAR, 0x0001, 4); // Gateway IP address + __GP_REGISTER_N(SUBR, 0x0005, 4); // Subnet mask address + __GP_REGISTER_N(SHAR, 0x0009, 6); // Source MAC address + __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) + __GP_REGISTER8 (VERSIONR_W5200,0x001F); // Chip Version Register (W5200 only) + __GP_REGISTER8 (VERSIONR_W5500,0x0039); // Chip Version Register (W5500 only) + __GP_REGISTER8 (VERSIONR_W5100S, 0x0080); // Chip Version Register (W5100S only) + __GP_REGISTER8 (PSTATUS_W5200, 0x0035); // PHY Status + __GP_REGISTER8 (PHYCFGR_W5500, 0x002E); // PHY Configuration register, default: 10111xxx + __GP_REGISTER8 (PHYCFGR_W5100S, 0x003C); // PHY Status + #undef __GP_REGISTER8 #undef __GP_REGISTER16 @@ -317,124 +225,72 @@ class W5100Class { //if (chip == 55) return 0x1000; //if (chip == 52) return 0x4000; //return 0x0400; - - // 5500 : 0x10 << 8 = 0x1000 - // 6100 : 0x60 << 8 = 0x6000 (1000 0000 0000 0000) - return CH_BASE_MSB << 8; } - static uint8_t CH_BASE_MSB; // 1 redundant byte, saves ~80 bytes code on AVR - static uint16_t CH_SIZE; + static const uint16_t CH_SIZE = 0x0100; static inline uint8_t readSn(SOCKET s, uint16_t addr) { - #ifdef DEBUG_W5100_H_READSN - PRINTVAR_HEX(s); - PRINTVAR_HEX(addr); - PRINTVAR_HEX(CH_BASE() + s * CH_SIZE + addr); - #endif return read(CH_BASE() + s * CH_SIZE + addr); } static inline uint8_t writeSn(SOCKET s, uint16_t addr, uint8_t data) { - #ifdef DEBUG_W5100_H_WRITESN - PRINTVAR_HEX(s); - PRINTVAR_HEX(addr); - PRINTVAR_HEX(CH_BASE() + s * CH_SIZE + addr); - #endif return write(CH_BASE() + s * CH_SIZE + addr, data); } static inline uint16_t readSn(SOCKET s, uint16_t addr, uint8_t *buf, uint16_t len) { - #ifdef DEBUG_W5100_H_READSN - PRINTVAR_HEX(s); - PRINTVAR_HEX(addr); - PRINTVAR_HEX(CH_BASE() + s * CH_SIZE + addr); - #endif return read(CH_BASE() + s * CH_SIZE + addr, buf, len); } static inline uint16_t writeSn(SOCKET s, uint16_t addr, uint8_t *buf, uint16_t len) { - #ifdef DEBUG_W5100_H_WRITESN - PRINTVAR_HEX(s); - PRINTVAR_HEX(addr); - PRINTVAR_HEX(CH_BASE() + s * CH_SIZE + addr); - #endif return write(CH_BASE() + s * CH_SIZE + addr, buf, len); } -#define __SOCKET_REGISTER8(name, address, adrss_w6100) \ +#define __SOCKET_REGISTER8(name, address) \ static inline void write##name(SOCKET _s, uint8_t _data) { \ - if(chip == 61) { \ - writeSn(_s, adrss_w6100, _data); \ - } else { \ - writeSn(_s, address, _data); \ - } \ + writeSn(_s, address, _data); \ } \ static inline uint8_t read##name(SOCKET _s) { \ - if(chip == 61) { \ - uint8_t data; \ - return readSn(_s, adrss_w6100); \ - } else { \ - return readSn(_s, address); \ - } \ + return readSn(_s, address); \ } - -#define __SOCKET_REGISTER16(name, address, adrss_w6100) \ +#define __SOCKET_REGISTER16(name, address) \ static void write##name(SOCKET _s, uint16_t _data) { \ uint8_t buf[2]; \ buf[0] = _data >> 8; \ buf[1] = _data & 0xFF; \ - if(chip == 61) { \ - writeSn(_s, adrss_w6100, buf, 2);\ - } else { \ - writeSn(_s, address, buf, 2); \ - } \ + writeSn(_s, address, buf, 2); \ } \ static uint16_t read##name(SOCKET _s) { \ uint8_t buf[2]; \ - if(chip == 61) { \ - readSn(_s, adrss_w6100, buf, 2); \ - } else { \ - readSn(_s, address, buf, 2); \ - } \ + readSn(_s, address, buf, 2); \ return (buf[0] << 8) | buf[1]; \ } - -#define __SOCKET_REGISTER_N(name, address, adrss_w6100, size)\ +#define __SOCKET_REGISTER_N(name, address, size) \ static uint16_t write##name(SOCKET _s, uint8_t *_buff) { \ - if(chip == 61) { \ - return writeSn(_s, adrss_w6100, _buff, size); \ - } else { \ - return writeSn(_s, address, _buff, size); \ - } \ + return writeSn(_s, address, _buff, size); \ } \ static uint16_t read##name(SOCKET _s, uint8_t *_buff) { \ - if(chip == 61) { \ - return readSn(_s, adrss_w6100, _buff, size); \ - } else { \ - return readSn(_s, address, _buff, size); \ - } \ + return readSn(_s, address, _buff, size); \ } public: - __SOCKET_REGISTER8(SnMR, 0x0000, 0x0000) // Mode - __SOCKET_REGISTER8(SnCR, 0x0001, 0x0010) // Command - __SOCKET_REGISTER8(SnIR, 0x0002, 0x0020) // Interrupt - __SOCKET_REGISTER8(SnSR, 0x0003, 0x0030) // Status - __SOCKET_REGISTER16(SnPORT, 0x0004, 0x0114) // Source Port - __SOCKET_REGISTER_N(SnDHAR, 0x0006, 0x0118, 6) // Destination Hardw Addr - __SOCKET_REGISTER_N(SnDIPR, 0x000C, 0x0120, 4) // Destination IP Addr - __SOCKET_REGISTER16(SnDPORT, 0x0010, 0x0140) // Destination Port - __SOCKET_REGISTER16(SnMSSR, 0x0012, 0x0110) // Max Segment Size - __SOCKET_REGISTER8(SnPROTO, 0x0014, 0x0000) // Protocol in IP RAW Mode - __SOCKET_REGISTER8(SnTOS, 0x0015, 0x0104) // IP TOS - __SOCKET_REGISTER8(SnTTL, 0x0016, 0x0108) // IP TTL - __SOCKET_REGISTER8(SnRX_SIZE, 0x001E, 0x0220) // RX Memory Size (W5200 only) - __SOCKET_REGISTER8(SnTX_SIZE, 0x001F, 0x0200) // TX Memory Size (W5200 only) - __SOCKET_REGISTER16(SnTX_FSR, 0x0020, 0x0204) // TX Free Size - __SOCKET_REGISTER16(SnTX_RD, 0x0022, 0x0208) // TX Read Pointer - __SOCKET_REGISTER16(SnTX_WR, 0x0024, 0x020C) // TX Write Pointer - __SOCKET_REGISTER16(SnRX_RSR, 0x0026, 0x0224) // RX Free Size - __SOCKET_REGISTER16(SnRX_RD, 0x0028, 0x0228) // RX Read Pointer - __SOCKET_REGISTER16(SnRX_WR, 0x002A, 0x022C) // RX Write Pointer (supported?) + __SOCKET_REGISTER8(SnMR, 0x0000) // Mode + __SOCKET_REGISTER8(SnCR, 0x0001) // Command + __SOCKET_REGISTER8(SnIR, 0x0002) // Interrupt + __SOCKET_REGISTER8(SnSR, 0x0003) // Status + __SOCKET_REGISTER16(SnPORT, 0x0004) // Source Port + __SOCKET_REGISTER_N(SnDHAR, 0x0006, 6) // Destination Hardw Addr + __SOCKET_REGISTER_N(SnDIPR, 0x000C, 4) // Destination IP Addr + __SOCKET_REGISTER16(SnDPORT, 0x0010) // Destination Port + __SOCKET_REGISTER16(SnMSSR, 0x0012) // Max Segment Size + __SOCKET_REGISTER8(SnPROTO, 0x0014) // Protocol in IP RAW Mode + __SOCKET_REGISTER8(SnTOS, 0x0015) // IP TOS + __SOCKET_REGISTER8(SnTTL, 0x0016) // IP TTL + __SOCKET_REGISTER8(SnRX_SIZE, 0x001E) // RX Memory Size (W5200 only) + __SOCKET_REGISTER8(SnTX_SIZE, 0x001F) // RX Memory Size (W5200 only) + __SOCKET_REGISTER16(SnTX_FSR, 0x0020) // TX Free Size + __SOCKET_REGISTER16(SnTX_RD, 0x0022) // TX Read Pointer + __SOCKET_REGISTER16(SnTX_WR, 0x0024) // TX Write Pointer + __SOCKET_REGISTER16(SnRX_RSR, 0x0026) // RX Free Size + __SOCKET_REGISTER16(SnRX_RD, 0x0028) // RX Read Pointer + __SOCKET_REGISTER16(SnRX_WR, 0x002A) // RX Write Pointer (supported?) #undef __SOCKET_REGISTER8 #undef __SOCKET_REGISTER16 @@ -449,7 +305,6 @@ class W5100Class { static uint8_t isW5100S(void); static uint8_t isW5200(void); static uint8_t isW5500(void); - static uint8_t isW6100(void); public: static uint8_t getChip(void) { return chip; } @@ -463,8 +318,6 @@ class W5100Class { static uint16_t SBASE(uint8_t socknum) { if (chip == 51 || chip == 50) { return socknum * SSIZE + 0x4000; - } else if (chip == 61) { - return socknum * SSIZE + W6100_TX_BASE_ADDR; } else { return socknum * SSIZE + 0x8000; } @@ -472,15 +325,13 @@ class W5100Class { static uint16_t RBASE(uint8_t socknum) { if (chip == 51 || chip == 50) { return socknum * SSIZE + 0x6000; - } else if (chip == 61) { - return socknum * SSIZE + W6100_RX_BASE_ADDR; } else { return socknum * SSIZE + 0xC000; } } static bool hasOffsetAddressMapping(void) { - if (chip == 55 || chip == 61) return true; + if (chip == 55) return true; return false; } static void setSS(uint8_t pin) { ss_pin = pin; } From c3b8100066e88777bf6ab0016c6113a092353f18 Mon Sep 17 00:00:00 2001 From: taylor Date: Tue, 15 Jan 2019 14:02:48 +0900 Subject: [PATCH 07/14] Fixed There are two isw5100S()s in w5100.cpp --- src/utility/w5100.cpp | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/src/utility/w5100.cpp b/src/utility/w5100.cpp index e9d5b906..c8ac6895 100644 --- a/src/utility/w5100.cpp +++ b/src/utility/w5100.cpp @@ -270,23 +270,6 @@ uint8_t W5100Class::isW5100S(void) return 1; } -uint8_t W5100Class::isW5100S(void) -{ - chip = 50; - //Serial.println("w5100.cpp: detect W5100S chip"); - if (!softReset()) return 0; - writeMR(0x13); - if (readMR() != 0x13) return 0; - writeMR(0x03); - if (readMR() != 0x03) return 0; - int ver = readVERSIONR_W5100S(); - //Serial.print("version="); - //Serial.println(ver); - if (ver != 81) return 0; - //Serial.println("chip is W5100S"); - return 1; -} - uint8_t W5100Class::isW5200(void) { chip = 52; From 06d60d07c26856e0937add00a984b1df03fa0e00 Mon Sep 17 00:00:00 2001 From: taylor Date: Fri, 4 Jan 2019 09:34:39 +0900 Subject: [PATCH 08/14] For Arduino MKR Vidor-4000 --- src/utility/w5100.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utility/w5100.cpp b/src/utility/w5100.cpp index c8ac6895..8d35e910 100644 --- a/src/utility/w5100.cpp +++ b/src/utility/w5100.cpp @@ -24,7 +24,7 @@ // MKR boards default to pin 5 for MKR ETH // Pins 8-10 are MOSI/SCK/MISO on MRK, so don't use pin 10 -#elif defined(USE_ARDUINO_MKR_PIN_LAYOUT) || defined(ARDUINO_SAMD_MKRZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRFox1200) || defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRWAN1300) +#elif defined(USE_ARDUINO_MKR_PIN_LAYOUT) || defined(ARDUINO_SAMD_MKRZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRFox1200) || defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRVIDOR4000) #define SS_PIN_DEFAULT 5 // For boards using AVR, assume shields with SS on pin 10 From 4a72dd5029590fd4cb096b82c413fde3217959e3 Mon Sep 17 00:00:00 2001 From: "Taylor.An" <44388468+taylor-an@users.noreply.github.com> Date: Tue, 15 Jan 2019 14:53:16 +0900 Subject: [PATCH 09/14] Added W6100 for W6100 Ethernetshield IPv4 (#9) Third Merge from W6100 --- keywords.txt | 1 + src/Ethernet.cpp | 1 + src/Ethernet.h | 74 ++++++++++++ src/EthernetUdp.cpp | 60 ++++++++-- src/utility/w5100.cpp | 258 ++++++++++++++++++++++++++++++++++++++++-- src/utility/w5100.h | 238 +++++++++++++++++++++++++++----------- 6 files changed, 543 insertions(+), 89 deletions(-) diff --git a/keywords.txt b/keywords.txt index 0013e20a..2fc46c11 100644 --- a/keywords.txt +++ b/keywords.txt @@ -65,4 +65,5 @@ EthernetNoHardware LITERAL1 EthernetW5100 LITERAL1 EthernetW5200 LITERAL1 EthernetW5500 LITERAL1 +EthernetW6100 LITERAL1 EthernetW5100S LITERAL1 diff --git a/src/Ethernet.cpp b/src/Ethernet.cpp index 28658623..469f2587 100644 --- a/src/Ethernet.cpp +++ b/src/Ethernet.cpp @@ -118,6 +118,7 @@ EthernetHardwareStatus EthernetClass::hardwareStatus() case 51: return EthernetW5100; case 52: return EthernetW5200; case 55: return EthernetW5500; + case 61: return EthernetW6100; default: return EthernetNoHardware; } } diff --git a/src/Ethernet.h b/src/Ethernet.h index dbc9a63d..16387a34 100644 --- a/src/Ethernet.h +++ b/src/Ethernet.h @@ -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 #include "Client.h" @@ -64,6 +137,7 @@ enum EthernetHardwareStatus { EthernetW5100, EthernetW5200, EthernetW5500, + EthernetW6100, EthernetW5100S }; diff --git a/src/EthernetUdp.cpp b/src/EthernetUdp.cpp index 51669d69..ffe03d53 100644 --- a/src/EthernetUdp.cpp +++ b/src/EthernetUdp.cpp @@ -108,19 +108,55 @@ 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; + int i; + + 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 Recived + // 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 Recived + // 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; } diff --git a/src/utility/w5100.cpp b/src/utility/w5100.cpp index 8d35e910..6fafe497 100644 --- a/src/utility/w5100.cpp +++ b/src/utility/w5100.cpp @@ -52,6 +52,7 @@ // W5100 controller instance uint8_t W5100Class::chip = 0; uint8_t W5100Class::CH_BASE_MSB; +uint16_t W5100Class::CH_SIZE; uint8_t W5100Class::ss_pin = SS_PIN_DEFAULT; #ifdef ETHERNET_LARGE_BUFFERS uint16_t W5100Class::SSIZE = 2048; @@ -100,6 +101,8 @@ uint8_t W5100Class::init(void) // reset time, this can be edited or removed. delay(560); //Serial.println("w5100 init"); + + CH_SIZE = 0x0100; // Default except W6100 SPI.begin(); initSS(); @@ -204,6 +207,30 @@ uint8_t W5100Class::init(void) #else writeTMSR(0x55); writeRMSR(0x55); +#endif + // Try W6100. Brandnew based W5500. + } else if (isW6100()) { + CH_BASE_MSB = 0x60; + CH_SIZE = 0x0400; // W6100 +#ifdef ETHERNET_LARGE_BUFFERS +#if MAX_SOCK_NUM <= 1 + SSIZE = 16384; +#elif MAX_SOCK_NUM <= 2 + SSIZE = 8192; +#elif MAX_SOCK_NUM <= 4 + SSIZE = 4096; +#else + SSIZE = 2048; +#endif + SMASK = SSIZE - 1; + for (i=0; i> 10); + writeSnTX_SIZE(i, SSIZE >> 10); + } + for (; i<8; i++) { + writeSnRX_SIZE(i, 0); + writeSnTX_SIZE(i, 0); + } #endif // No hardware seems to be present. Or it could be a W5200 // that's heard other SPI communication if its chip select @@ -223,19 +250,67 @@ uint8_t W5100Class::init(void) uint8_t W5100Class::softReset(void) { uint16_t count=0; + uint8_t sysr; + + if(chip == 61) { + writeCHPLCKR_W6100(W6100_CHPLCKR_UNLOCK); // Unlock SYSR[CHPL] + count = 0; + do{ // Wait Unlock Complete + if(++count > 20) { // Check retry count + return 0; // Over Limit retry count + } + } while ((readSYSR_W6100() & W6100_SYSR_CHPL_LOCK) ^ W6100_SYSR_CHPL_ULOCK); // Exit Wait Unlock Complete - //Serial.println("Wiznet soft reset"); - // write to reset bit - writeMR(0x80); - // then wait for soft reset to complete - do { - uint8_t mr = readMR(); - //Serial.print("mr="); - //Serial.println(mr, HEX); - if (mr == 0 || (mr == 3 && chip == 50)) return 1; - delay(1); - } while (++count < 20); - return 0; + writeSYCR0(0x0); // Software Reset + + do{ // Wait Lock Complete + if(++count > 20) { // Check retry count + return 0; // Over Limit retry count + } + + } while ((readSYSR_W6100() & W6100_SYSR_CHPL_LOCK) ^ W6100_SYSR_CHPL_LOCK); // Exit Wait Lock Complete + + return 1; + } else { + count = 0; + + //Serial.println("Wiznet soft reset"); + // write to reset bit + writeMR(0x80); + // then wait for soft reset to complete + do { + uint8_t mr = readMR(); + //Serial.print("mr="); + //Serial.println(mr, HEX); + if (mr == 0 || (mr == 3 && chip == 50)) return 1; + delay(1); + } while (++count < 20); + return 0; + } +} + +uint8_t W5100Class::isW6100(void) +{ + chip = 61; + CH_BASE_MSB = 0x80; + + if (!softReset()) return 0; + + // Unlock + writeCHPLCKR_W6100(W6100_CHPLCKR_UNLOCK); + writeNETLCKR_W6100(W6100_NETLCKR_UNLOCK); + writePHYLCKR_W6100(W6100_PHYLCKR_UNLOCK); + + // W6100 CIDR0 + // Version 97(dec) 0x61(hex) + int ver = readVERSIONR_W6100(); + + //Serial.print("version = 0x"); + //Serial.println(ver, HEX); + + if (ver != 97) return 0; + //Serial.println("chip is W6100"); + return 1; } uint8_t W5100Class::isW5100(void) @@ -331,6 +406,12 @@ W5100Linkstatus W5100Class::getLinkStatus() phystatus = readPHYCFGR_W5500(); SPI.endTransaction(); if (phystatus & 0x01) return LINK_ON; + return LINK_OFF; + case 61: + SPI.beginTransaction(SPI_ETHERNET_SETTINGS); + phystatus = readPHYCFGR_W6100(); + SPI.endTransaction(); + if (phystatus & 0x01) return LINK_ON; return LINK_OFF; default: return UNKNOWN; @@ -367,6 +448,89 @@ uint16_t W5100Class::write(uint16_t addr, const uint8_t *buf, uint16_t len) } #endif resetSS(); + } else if (chip == 61) { // chip == 61 + setSS(); + + if (addr < CH_BASE()) { + // common registers + + cmd[0] = (addr>>8) & 0x7F; + cmd[1] = addr & 0xFF; + cmd[2] = W6100_SPI_FRAME_CTL_BSB_BLK(0) + | W6100_SPI_FRAME_CTL_BSB_COMM + | W6100_SPI_FRAME_CTL_WD + | W6100_SPI_FRAME_CTL_OPM_VDM + ; + } else if (addr < W6100_TX_BASE_ADDR) { + // socket registers + + cmd[0] = (addr>>8) & 0x3; + cmd[1] = addr & 0xFF; + cmd[2] = W6100_SPI_FRAME_CTL_BSB_BLK((addr>>10)&0x7) + | W6100_SPI_FRAME_CTL_BSB_SOCK + | W6100_SPI_FRAME_CTL_WD + | W6100_SPI_FRAME_CTL_OPM_VDM + ; + } else if (addr < W6100_RX_BASE_ADDR) { + // transmit buffers + + cmd[0] = addr>>8; + cmd[1] = addr & 0xFF; + + #if defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 1 + cmd[2] = 0; // 16K buffers + #elif defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 2 + cmd[2] = ((addr >> 8) & 0x20); // 8K buffers + #elif defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 4 + cmd[2] = ((addr >> 7) & 0x60); // 4K buffers + #else + cmd[2] = ((addr >> 6) & 0xE0); // 2K buffers + #endif + + cmd[2] |= W6100_SPI_FRAME_CTL_BSB_BLK(0) + | W6100_SPI_FRAME_CTL_BSB_TXBF + | W6100_SPI_FRAME_CTL_WD + | W6100_SPI_FRAME_CTL_OPM_VDM + ; + } else { + // receive buffers + + cmd[0] = addr>>8; + cmd[1] = addr & 0xFF; + + #if defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 1 + cmd[2] = 0; // 16K buffers + #elif defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 2 + cmd[2] = ((addr >> 8) & 0x20); // 8K buffers + #elif defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 4 + cmd[2] = ((addr >> 7) & 0x60); // 4K buffers + #else + cmd[2] = ((addr >> 6) & 0xE0); // 2K buffers + #endif + + cmd[2] |= W6100_SPI_FRAME_CTL_BSB_BLK(0) + | W6100_SPI_FRAME_CTL_BSB_RXBF + | W6100_SPI_FRAME_CTL_WD + | W6100_SPI_FRAME_CTL_OPM_VDM + ; + } + if (len <= 5) { + for (uint8_t i=0; i < len; i++) { + cmd[i + 3] = buf[i]; + } + SPI.transfer(cmd, len + 3); + } else { + SPI.transfer(cmd, 3); +#ifdef SPI_HAS_TRANSFER_BUF + SPI.transfer(buf, NULL, len); +#else + // TODO: copy 8 bytes at a time to cmd[] and block transfer + for (uint16_t i=0; i < len; i++) { + SPI.transfer(buf[i]); + } +#endif + } + resetSS(); } else { // chip == 55 setSS(); if (addr < 0x100) { @@ -462,6 +626,76 @@ uint16_t W5100Class::read(uint16_t addr, uint8_t *buf, uint16_t len) memset(buf, 0, len); SPI.transfer(buf, len); resetSS(); + } else if (chip == 61) { // chip == 61 + setSS(); + + if (addr < CH_BASE()) { + // common registers + + cmd[0] = (addr>>8) & 0x7F; + cmd[1] = addr & 0xFF; + cmd[2] = W6100_SPI_FRAME_CTL_BSB_BLK(0) + | W6100_SPI_FRAME_CTL_BSB_COMM + | W6100_SPI_FRAME_CTL_RD + | W6100_SPI_FRAME_CTL_OPM_VDM + ; + } else if (addr < W6100_TX_BASE_ADDR) { + // socket registers + + cmd[0] = (addr>>8) & 0x3; + cmd[1] = addr & 0xFF; + cmd[2] = W6100_SPI_FRAME_CTL_BSB_BLK((addr>>10)&0x7) + | W6100_SPI_FRAME_CTL_BSB_SOCK + | W6100_SPI_FRAME_CTL_RD + | W6100_SPI_FRAME_CTL_OPM_VDM + ; + } else if (addr < W6100_RX_BASE_ADDR) { + // transmit buffers + + cmd[0] = addr>>8; + cmd[1] = addr & 0xFF; + + #if defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 1 + cmd[2] = 0; // 16K buffers + #elif defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 2 + cmd[2] = ((addr >> 8) & 0x20); // 8K buffers + #elif defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 4 + cmd[2] = ((addr >> 7) & 0x60); // 4K buffers + #else + cmd[2] = ((addr >> 6) & 0xE0); // 2K buffers + #endif + + cmd[2] |= W6100_SPI_FRAME_CTL_BSB_BLK(0) + | W6100_SPI_FRAME_CTL_BSB_TXBF + | W6100_SPI_FRAME_CTL_RD + | W6100_SPI_FRAME_CTL_OPM_VDM + ; + } else { + // receive buffers + + cmd[0] = addr>>8; + cmd[1] = addr & 0xFF; + + #if defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 1 + cmd[2] = 0; // 16K buffers + #elif defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 2 + cmd[2] = ((addr >> 8) & 0x20); // 8K buffers + #elif defined(ETHERNET_LARGE_BUFFERS) && MAX_SOCK_NUM <= 4 + cmd[2] = ((addr >> 7) & 0x60); // 4K buffers + #else + cmd[2] = ((addr >> 6) & 0xE0); // 2K buffers + #endif + + cmd[2] |= W6100_SPI_FRAME_CTL_BSB_BLK(0) + | W6100_SPI_FRAME_CTL_BSB_RXBF + | W6100_SPI_FRAME_CTL_RD + | W6100_SPI_FRAME_CTL_OPM_VDM + ; + } + SPI.transfer(cmd, 3); + memset(buf, 0, len); + SPI.transfer(buf, len); + resetSS(); } else { // chip == 55 setSS(); if (addr < 0x100) { diff --git a/src/utility/w5100.h b/src/utility/w5100.h index 91e81814..8bb8a5e0 100644 --- a/src/utility/w5100.h +++ b/src/utility/w5100.h @@ -161,58 +161,129 @@ class W5100Class { return data; } -#define __GP_REGISTER8(name, address) \ - static inline void write##name(uint8_t _data) { \ - write(address, _data); \ +#define W6100_SPI_FRAME_CTL_BSB_BLK(sn) ((sn)<<5) + +#define W6100_SPI_FRAME_CTL_BSB_COMM (0<<3) +#define W6100_SPI_FRAME_CTL_BSB_SOCK (1<<3) +#define W6100_SPI_FRAME_CTL_BSB_TXBF (2<<3) +#define W6100_SPI_FRAME_CTL_BSB_RXBF (3<<3) + +#define W6100_SPI_FRAME_CTL_RD (0<<2) +#define W6100_SPI_FRAME_CTL_WD (1<<2) + +#define W6100_SPI_FRAME_CTL_OPM_VDM (0<<0) +#define W6100_SPI_FRAME_CTL_OPM_FDM1 (1<<0) +#define W6100_SPI_FRAME_CTL_OPM_FDM2 (2<<0) +#define W6100_SPI_FRAME_CTL_OPM_FDM4 (3<<0) + +#define W6100_COMMON_BASE_ADDR (0x0000) +#define W6100_SOCKET_BASE_ADDR (0x6000) +#define W6100_TX_BASE_ADDR (0x8000) +#define W6100_RX_BASE_ADDR (0xC000) +#define W6100_SOCKET_NUM(_s) ((_s)<<10)) + +#define W6100_CHPLCKR_UNLOCK 0xCE +#define W6100_NETLCKR_UNLOCK 0x3A +#define W6100_PHYLCKR_UNLOCK 0x53 + +#define W6100_SYSR_CHPL_LOCK (1<<7) +#define W6100_SYSR_CHPL_ULOCK (0<<7) + +#define W6100_UDP_HEADER_IPV (1<<7) +#define W6100_UDP_HEADER_IPV4 (0<<7) +#define W6100_UDP_HEADER_IPV6 (1<<7) +#define W6100_UDP_HEADER_ALL (1<<6) +#define W6100_UDP_HEADER_MUL (1<<5) +#define W6100_UDP_HEADER_GUA (0<<3) +#define W6100_UDP_HEADER_LLA (1<<3) + +#define __GP_REGISTER8(name, address, adrss_w6100) \ + static inline void write##name(uint8_t _data) { \ + if(chip == 61) { \ + write(adrss_w6100, _data); \ + } else { \ + write(address, _data); \ + } \ } \ static inline uint8_t read##name() { \ - return read(address); \ - } -#define __GP_REGISTER16(name, address) \ + if(chip == 61) { \ + return read(adrss_w6100); \ + } else { \ + return read(address); \ + } \ + } \ + +#define __GP_REGISTER16(name, address, adrss_w6100) \ static void write##name(uint16_t _data) { \ uint8_t buf[2]; \ buf[0] = _data >> 8; \ buf[1] = _data & 0xFF; \ - write(address, buf, 2); \ + if(chip == 61) { \ + write(adrss_w6100, buf, 2); \ + } else { \ + write(address, buf, 2); \ + } \ } \ static uint16_t read##name() { \ uint8_t buf[2]; \ - read(address, buf, 2); \ + if(chip == 61) { \ + read(adrss_w6100, buf, 2); \ + } else { \ + read(address, buf, 2); \ + } \ return (buf[0] << 8) | buf[1]; \ } -#define __GP_REGISTER_N(name, address, size) \ + +#define __GP_REGISTER_N(name, address, adrss_w6100, size) \ static uint16_t write##name(const uint8_t *_buff) { \ - return write(address, _buff, size); \ - } \ - static uint16_t read##name(uint8_t *_buff) { \ - return read(address, _buff, size); \ + if(chip == 61) { \ + return write(adrss_w6100, _buff, size); \ + } else { \ + return write(address, _buff, size); \ + } \ + } \ + static uint16_t read##name(uint8_t *_buff) { \ + if(chip == 61) { \ + return read(adrss_w6100, _buff, size); \ + } else { \ + return read(address, _buff, size); \ + } \ } static W5100Linkstatus getLinkStatus(); public: - __GP_REGISTER8 (MR, 0x0000); // Mode - __GP_REGISTER_N(GAR, 0x0001, 4); // Gateway IP address - __GP_REGISTER_N(SUBR, 0x0005, 4); // Subnet mask address - __GP_REGISTER_N(SHAR, 0x0009, 6); // Source MAC address - __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) - __GP_REGISTER8 (VERSIONR_W5200,0x001F); // Chip Version Register (W5200 only) - __GP_REGISTER8 (VERSIONR_W5500,0x0039); // Chip Version Register (W5500 only) + __GP_REGISTER8 (MR, 0x0000, 0x4000); // Mode + __GP_REGISTER_N(GAR, 0x0001, 0x4130, 4); // Gateway IP address + __GP_REGISTER_N(SUBR, 0x0005, 0x4134, 4); // Subnet mask address + __GP_REGISTER_N(SHAR, 0x0009, 0x4120, 6); // Source MAC address + __GP_REGISTER_N(SIPR, 0x000F, 0x4138, 4); // Source IP address + __GP_REGISTER8 (IR, 0x0015, 0x2100); // Interrupt + __GP_REGISTER8 (IMR, 0x0016, 0x2104); // Interrupt Mask + __GP_REGISTER16(RTR, 0x0017, 0x2101); // Timeout address + __GP_REGISTER8 (RCR, 0x0019, 0x4200); // Retry count + __GP_REGISTER8 (RMSR, 0x001A, 0x0000); // Receive memory size (W5100 only) + __GP_REGISTER8 (TMSR, 0x001B, 0x0000); // Transmit memory size (W5100 only) + __GP_REGISTER8 (PATR, 0x001C, 0x0000); // Authentication type address in PPPoE mode + __GP_REGISTER8 (PTIMER, 0x0028, 0x4100); // PPP LCP Request Timer + __GP_REGISTER8 (PMAGIC, 0x0029, 0x4104); // PPP LCP Magic Number + __GP_REGISTER_N(UIPR, 0x002A, 0x0000, 4); // Unreachable IP address in UDP mode (W5100 only) + __GP_REGISTER16(UPORT, 0x002E, 0x0000); // Unreachable Port address in UDP mode (W5100 only) + __GP_REGISTER8 (VERSIONR_W5200, 0x001F, 0x0000); // Chip Version Register (W5200 only) + __GP_REGISTER8 (VERSIONR_W5500, 0x0039, 0x0000); // Chip Version Register (W5500 only) __GP_REGISTER8 (VERSIONR_W5100S, 0x0080); // Chip Version Register (W5100S only) - __GP_REGISTER8 (PSTATUS_W5200, 0x0035); // PHY Status - __GP_REGISTER8 (PHYCFGR_W5500, 0x002E); // PHY Configuration register, default: 10111xxx + __GP_REGISTER8 (PSTATUS_W5200, 0x0035, 0x0000); // PHY Status + __GP_REGISTER8 (PHYCFGR_W5500, 0x002E, 0x0000); // PHY Configuration register, default: 10111xxx __GP_REGISTER8 (PHYCFGR_W5100S, 0x003C); // PHY Status - + + // For W6100 + __GP_REGISTER8 (SYCR0, 0x0000, 0x2004); // System Command Register + __GP_REGISTER8 (SYSR_W6100, 0x0000, 0x2000); // System Status Register + __GP_REGISTER8 (NETLCKR_W6100, 0x0000, 0x41F5); // Network Lock Register + __GP_REGISTER8 (CHPLCKR_W6100, 0x0000, 0x41F4); // Chip Lock Register + __GP_REGISTER8 (PHYLCKR_W6100, 0x0000, 0x41F6); // PHY Lock Register + __GP_REGISTER8 (PHYCFGR_W6100, 0x0000, 0x3000); // PHY Status + __GP_REGISTER8 (VERSIONR_W6100, 0x0000, 0x0000); // Chip Version Register + __GP_REGISTER8 (CVERSIONR_W6100, 0x0000, 0x0002); // Chip Version Register #undef __GP_REGISTER8 #undef __GP_REGISTER16 @@ -225,10 +296,15 @@ class W5100Class { //if (chip == 55) return 0x1000; //if (chip == 52) return 0x4000; //return 0x0400; + + // 5500 : 0x10 << 8 = 0x1000 + // 6100 : 0x60 << 8 = 0x6000 (1000 0000 0000 0000) + return CH_BASE_MSB << 8; } + static uint8_t CH_BASE_MSB; // 1 redundant byte, saves ~80 bytes code on AVR - static const uint16_t CH_SIZE = 0x0100; + static uint16_t CH_SIZE; static inline uint8_t readSn(SOCKET s, uint16_t addr) { return read(CH_BASE() + s * CH_SIZE + addr); @@ -243,54 +319,81 @@ class W5100Class { return write(CH_BASE() + s * CH_SIZE + addr, buf, len); } -#define __SOCKET_REGISTER8(name, address) \ +#define __SOCKET_REGISTER8(name, address, adrss_w6100) \ static inline void write##name(SOCKET _s, uint8_t _data) { \ - writeSn(_s, address, _data); \ + if(chip == 61) { \ + writeSn(_s, adrss_w6100, _data); \ + } else { \ + writeSn(_s, address, _data); \ + } \ } \ static inline uint8_t read##name(SOCKET _s) { \ - return readSn(_s, address); \ + if(chip == 61) { \ + uint8_t data; \ + return readSn(_s, adrss_w6100); \ + } else { \ + return readSn(_s, address); \ + } \ } -#define __SOCKET_REGISTER16(name, address) \ + +#define __SOCKET_REGISTER16(name, address, adrss_w6100) \ static void write##name(SOCKET _s, uint16_t _data) { \ uint8_t buf[2]; \ buf[0] = _data >> 8; \ buf[1] = _data & 0xFF; \ - writeSn(_s, address, buf, 2); \ + if(chip == 61) { \ + writeSn(_s, adrss_w6100, buf, 2);\ + } else { \ + writeSn(_s, address, buf, 2); \ + } \ } \ static uint16_t read##name(SOCKET _s) { \ uint8_t buf[2]; \ - readSn(_s, address, buf, 2); \ + if(chip == 61) { \ + readSn(_s, adrss_w6100, buf, 2); \ + } else { \ + readSn(_s, address, buf, 2); \ + } \ return (buf[0] << 8) | buf[1]; \ } -#define __SOCKET_REGISTER_N(name, address, size) \ + +#define __SOCKET_REGISTER_N(name, address, adrss_w6100, size)\ static uint16_t write##name(SOCKET _s, uint8_t *_buff) { \ - return writeSn(_s, address, _buff, size); \ + if(chip == 61) { \ + return writeSn(_s, adrss_w6100, _buff, size); \ + } else { \ + return writeSn(_s, address, _buff, size); \ + } \ } \ static uint16_t read##name(SOCKET _s, uint8_t *_buff) { \ - return readSn(_s, address, _buff, size); \ + if(chip == 61) { \ + return readSn(_s, adrss_w6100, _buff, size); \ + } else { \ + return readSn(_s, address, _buff, size); \ + } \ } public: - __SOCKET_REGISTER8(SnMR, 0x0000) // Mode - __SOCKET_REGISTER8(SnCR, 0x0001) // Command - __SOCKET_REGISTER8(SnIR, 0x0002) // Interrupt - __SOCKET_REGISTER8(SnSR, 0x0003) // Status - __SOCKET_REGISTER16(SnPORT, 0x0004) // Source Port - __SOCKET_REGISTER_N(SnDHAR, 0x0006, 6) // Destination Hardw Addr - __SOCKET_REGISTER_N(SnDIPR, 0x000C, 4) // Destination IP Addr - __SOCKET_REGISTER16(SnDPORT, 0x0010) // Destination Port - __SOCKET_REGISTER16(SnMSSR, 0x0012) // Max Segment Size - __SOCKET_REGISTER8(SnPROTO, 0x0014) // Protocol in IP RAW Mode - __SOCKET_REGISTER8(SnTOS, 0x0015) // IP TOS - __SOCKET_REGISTER8(SnTTL, 0x0016) // IP TTL - __SOCKET_REGISTER8(SnRX_SIZE, 0x001E) // RX Memory Size (W5200 only) - __SOCKET_REGISTER8(SnTX_SIZE, 0x001F) // RX Memory Size (W5200 only) - __SOCKET_REGISTER16(SnTX_FSR, 0x0020) // TX Free Size - __SOCKET_REGISTER16(SnTX_RD, 0x0022) // TX Read Pointer - __SOCKET_REGISTER16(SnTX_WR, 0x0024) // TX Write Pointer - __SOCKET_REGISTER16(SnRX_RSR, 0x0026) // RX Free Size - __SOCKET_REGISTER16(SnRX_RD, 0x0028) // RX Read Pointer - __SOCKET_REGISTER16(SnRX_WR, 0x002A) // RX Write Pointer (supported?) + __SOCKET_REGISTER8(SnMR, 0x0000, 0x0000) // Mode + __SOCKET_REGISTER8(SnCR, 0x0001, 0x0010) // Command + __SOCKET_REGISTER8(SnIR, 0x0002, 0x0020) // Interrupt + __SOCKET_REGISTER8(SnSR, 0x0003, 0x0030) // Status + __SOCKET_REGISTER16(SnPORT, 0x0004, 0x0114) // Source Port + __SOCKET_REGISTER_N(SnDHAR, 0x0006, 0x0118, 6) // Destination Hardw Addr + __SOCKET_REGISTER_N(SnDIPR, 0x000C, 0x0120, 4) // Destination IP Addr + __SOCKET_REGISTER16(SnDPORT, 0x0010, 0x0140) // Destination Port + __SOCKET_REGISTER16(SnMSSR, 0x0012, 0x0110) // Max Segment Size + __SOCKET_REGISTER8(SnPROTO, 0x0014, 0x0000) // Protocol in IP RAW Mode + __SOCKET_REGISTER8(SnTOS, 0x0015, 0x0104) // IP TOS + __SOCKET_REGISTER8(SnTTL, 0x0016, 0x0108) // IP TTL + __SOCKET_REGISTER8(SnRX_SIZE, 0x001E, 0x0220) // RX Memory Size (W5200 only) + __SOCKET_REGISTER8(SnTX_SIZE, 0x001F, 0x0200) // TX Memory Size (W5200 only) + __SOCKET_REGISTER16(SnTX_FSR, 0x0020, 0x0204) // TX Free Size + __SOCKET_REGISTER16(SnTX_RD, 0x0022, 0x0208) // TX Read Pointer + __SOCKET_REGISTER16(SnTX_WR, 0x0024, 0x020C) // TX Write Pointer + __SOCKET_REGISTER16(SnRX_RSR, 0x0026, 0x0224) // RX Free Size + __SOCKET_REGISTER16(SnRX_RD, 0x0028, 0x0228) // RX Read Pointer + __SOCKET_REGISTER16(SnRX_WR, 0x002A, 0x022C) // RX Write Pointer (supported?) #undef __SOCKET_REGISTER8 #undef __SOCKET_REGISTER16 @@ -305,6 +408,7 @@ class W5100Class { static uint8_t isW5100S(void); static uint8_t isW5200(void); static uint8_t isW5500(void); + static uint8_t isW6100(void); public: static uint8_t getChip(void) { return chip; } @@ -318,6 +422,8 @@ class W5100Class { static uint16_t SBASE(uint8_t socknum) { if (chip == 51 || chip == 50) { return socknum * SSIZE + 0x4000; + } else if (chip == 61) { + return socknum * SSIZE + W6100_TX_BASE_ADDR; } else { return socknum * SSIZE + 0x8000; } @@ -325,13 +431,15 @@ class W5100Class { static uint16_t RBASE(uint8_t socknum) { if (chip == 51 || chip == 50) { return socknum * SSIZE + 0x6000; + } else if (chip == 61) { + return socknum * SSIZE + W6100_RX_BASE_ADDR; } else { return socknum * SSIZE + 0xC000; } } static bool hasOffsetAddressMapping(void) { - if (chip == 55) return true; + if (chip == 55 || chip == 61) return true; return false; } static void setSS(uint8_t pin) { ss_pin = pin; } From a07596c08dd52038fcf35a6006e05e6f50955477 Mon Sep 17 00:00:00 2001 From: taylor Date: Thu, 17 Jan 2019 16:41:18 +0900 Subject: [PATCH 10/14] Missing second address for adrss_w6100 VERSIONR_W5100S PHYCFGR_W5100S --- src/utility/w5100.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utility/w5100.h b/src/utility/w5100.h index 8bb8a5e0..1f4fffcb 100644 --- a/src/utility/w5100.h +++ b/src/utility/w5100.h @@ -270,10 +270,10 @@ class W5100Class { __GP_REGISTER16(UPORT, 0x002E, 0x0000); // Unreachable Port address in UDP mode (W5100 only) __GP_REGISTER8 (VERSIONR_W5200, 0x001F, 0x0000); // Chip Version Register (W5200 only) __GP_REGISTER8 (VERSIONR_W5500, 0x0039, 0x0000); // Chip Version Register (W5500 only) - __GP_REGISTER8 (VERSIONR_W5100S, 0x0080); // Chip Version Register (W5100S only) + __GP_REGISTER8 (VERSIONR_W5100S, 0x0080, 0x0000); // Chip Version Register (W5100S only) __GP_REGISTER8 (PSTATUS_W5200, 0x0035, 0x0000); // PHY Status __GP_REGISTER8 (PHYCFGR_W5500, 0x002E, 0x0000); // PHY Configuration register, default: 10111xxx - __GP_REGISTER8 (PHYCFGR_W5100S, 0x003C); // PHY Status + __GP_REGISTER8 (PHYCFGR_W5100S, 0x003C, 0x0000); // PHY Status // For W6100 __GP_REGISTER8 (SYCR0, 0x0000, 0x2004); // System Command Register From a4c392bfdfa4782b88ba72fa71e207ecf0d6cae4 Mon Sep 17 00:00:00 2001 From: Philipp Serr Date: Sat, 5 Mar 2022 10:20:55 +0100 Subject: [PATCH 11/14] Fix spelling errors --- src/EthernetUdp.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/EthernetUdp.cpp b/src/EthernetUdp.cpp index b1197624..d8f9d62f 100644 --- a/src/EthernetUdp.cpp +++ b/src/EthernetUdp.cpp @@ -119,7 +119,7 @@ int EthernetUDP::parsePacket() _remaining = (tmpBuf[0] & (0x7))<<8 | tmpBuf[1]; if((tmpBuf[0] & W6100_UDP_HEADER_IPV) == W6100_UDP_HEADER_IPV6) { - // IPv6 UDP Recived + // IPv6 UDP Received // 0 1 // 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 // 18 19 @@ -129,7 +129,7 @@ int EthernetUDP::parsePacket() _remoteIP = &tmpBuf[2]; _remotePort = (tmpBuf[18]<<8) | tmpBuf[19]; } else { - // IPv4 UDP Recived + // IPv4 UDP Received // 0 1 // 2 3 4 5 // 6 7 From 44ec4a09e4d149a48bbf1a8e11d6b34686bca899 Mon Sep 17 00:00:00 2001 From: Philipp Serr Date: Sat, 5 Mar 2022 10:45:42 +0100 Subject: [PATCH 12/14] Remove unused local variables --- src/EthernetUdp.cpp | 1 - src/utility/w5100.cpp | 1 - src/utility/w5100.h | 1 - 3 files changed, 3 deletions(-) diff --git a/src/EthernetUdp.cpp b/src/EthernetUdp.cpp index d8f9d62f..b4e18a2a 100644 --- a/src/EthernetUdp.cpp +++ b/src/EthernetUdp.cpp @@ -110,7 +110,6 @@ int EthernetUDP::parsePacket() //HACK - hand-parse the UDP packet using TCP recv method uint8_t tmpBuf[20]; int ret=0; - int i; if(W5100.getChip() == 61) { //read 2 header bytes and get one IPv4 or IPv6 diff --git a/src/utility/w5100.cpp b/src/utility/w5100.cpp index dafdff8b..ae7eb67e 100644 --- a/src/utility/w5100.cpp +++ b/src/utility/w5100.cpp @@ -250,7 +250,6 @@ uint8_t W5100Class::init(void) uint8_t W5100Class::softReset(void) { uint16_t count=0; - uint8_t sysr; if(chip == 61) { writeCHPLCKR_W6100(W6100_CHPLCKR_UNLOCK); // Unlock SYSR[CHPL] diff --git a/src/utility/w5100.h b/src/utility/w5100.h index 329d9ae0..71c29322 100644 --- a/src/utility/w5100.h +++ b/src/utility/w5100.h @@ -329,7 +329,6 @@ class W5100Class { } \ static inline uint8_t read##name(SOCKET _s) { \ if(chip == 61) { \ - uint8_t data; \ return readSn(_s, adrss_w6100); \ } else { \ return readSn(_s, address); \ From 23b487da32cc8eac98dbc58f4a2a5e870a4194e9 Mon Sep 17 00:00:00 2001 From: Philipp Serr Date: Wed, 16 Mar 2022 14:59:49 +0100 Subject: [PATCH 13/14] Improve comments --- src/EthernetServer.cpp | 2 +- src/utility/w5100.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/EthernetServer.cpp b/src/EthernetServer.cpp index 5e91534b..43a1f4b8 100644 --- a/src/EthernetServer.cpp +++ b/src/EthernetServer.cpp @@ -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 || chip == 50) 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) { diff --git a/src/utility/w5100.cpp b/src/utility/w5100.cpp index ae7eb67e..73882165 100644 --- a/src/utility/w5100.cpp +++ b/src/utility/w5100.cpp @@ -182,7 +182,7 @@ uint8_t W5100Class::init(void) writeTMSR(0x55); writeRMSR(0x55); #endif -// Try W5100 last. This simple chip uses fixed 4 byte frames +// Try W5100. This simple chip uses fixed 4 byte frames // for every 8 bit access. Terribly inefficient, but so simple // it recovers from "hearing" unsuccessful W5100 or W5200 // communication. W5100 is also the only chip without a VERSIONR From 195e146079f7ad3c3f2824effbe1067ed244da0e Mon Sep 17 00:00:00 2001 From: Philipp Serr Date: Wed, 16 Mar 2022 15:03:35 +0100 Subject: [PATCH 14/14] Fix indentation --- src/utility/w5100.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utility/w5100.cpp b/src/utility/w5100.cpp index 73882165..446201ad 100644 --- a/src/utility/w5100.cpp +++ b/src/utility/w5100.cpp @@ -182,7 +182,7 @@ uint8_t W5100Class::init(void) writeTMSR(0x55); writeRMSR(0x55); #endif -// Try W5100. This simple chip uses fixed 4 byte frames + // Try W5100. This simple chip uses fixed 4 byte frames // for every 8 bit access. Terribly inefficient, but so simple // it recovers from "hearing" unsuccessful W5100 or W5200 // communication. W5100 is also the only chip without a VERSIONR