diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..d2073515 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.vscode/* +!.vscode/c_c_cpp_properties.json +.build/* diff --git a/examples/LinkStatus/LinkStatus.ino b/examples/LinkStatus/LinkStatus.ino index 84651d09..4522d86f 100644 --- a/examples/LinkStatus/LinkStatus.ino +++ b/examples/LinkStatus/LinkStatus.ino @@ -12,7 +12,7 @@ */ #include -#include +#include void setup() { // You can use Ethernet.init(pin) to configure the CS pin @@ -23,6 +23,21 @@ void setup() { //Ethernet.init(15); // ESP8266 with Adafruit FeatherWing Ethernet //Ethernet.init(33); // ESP32 with Adafruit FeatherWing Ethernet + // ==================================================================== + // W5500-Ether add-on for Spresense + + // Jumper setting is SJ1=12, use D21/EMMC_DATA3 + W5500ETH_reset(SJ1_12); + + // Jumper setting is SJ1=23, use D26/I2S_BCK + // W5500ETH_reset(SJ1_23); + + // When Jumper setting is SJ2=23, init() is not need. + + // Jumper setting is SJ2=12, use D19/I2S_DIN + // Ethernet.init(SJ2_12); + // ==================================================================== + Serial.begin(9600); } diff --git a/examples/TelnetClient/TelnetClient.ino b/examples/TelnetClient/TelnetClient.ino index ff554a5c..878fd74b 100644 --- a/examples/TelnetClient/TelnetClient.ino +++ b/examples/TelnetClient/TelnetClient.ino @@ -18,7 +18,7 @@ */ #include -#include +#include // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: @@ -45,6 +45,21 @@ void setup() { //Ethernet.init(15); // ESP8266 with Adafruit FeatherWing Ethernet //Ethernet.init(33); // ESP32 with Adafruit FeatherWing Ethernet + // ==================================================================== + // W5500-Ether add-on for Spresense + + // Jumper setting is SJ1=12, use D21/EMMC_DATA3 + W5500ETH_reset(SJ1_12); + + // Jumper setting is SJ1=23, use D26/I2S_BCK + // W5500ETH_reset(SJ1_23); + + // When Jumper setting is SJ2=23, init() is not need. + + // Jumper setting is SJ2=12, use D19/I2S_DIN + // Ethernet.init(SJ2_12); + // ==================================================================== + // start the Ethernet connection: Ethernet.begin(mac, ip); diff --git a/examples/WebServer/WebServer.ino b/examples/WebServer/WebServer.ino index f3929d2d..21cbc55b 100644 --- a/examples/WebServer/WebServer.ino +++ b/examples/WebServer/WebServer.ino @@ -18,7 +18,7 @@ */ #include -#include +#include // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: @@ -41,6 +41,21 @@ void setup() { //Ethernet.init(15); // ESP8266 with Adafruit FeatherWing Ethernet //Ethernet.init(33); // ESP32 with Adafruit FeatherWing Ethernet + // ==================================================================== + // W5500-Ether add-on for Spresense + + // Jumper setting is SJ1=12, use D21/EMMC_DATA3 + W5500ETH_reset(SJ1_12); + + // Jumper setting is SJ1=23, use D26/I2S_BCK + // W5500ETH_reset(SJ1_23); + + // When Jumper setting is SJ2=23, init() is not need. + + // Jumper setting is SJ2=12, use D19/I2S_DIN + // Ethernet.init(SJ2_12); + // ==================================================================== + // Open serial communications and wait for port to open: Serial.begin(9600); while (!Serial) { diff --git a/examples/AdvancedChatServer/AdvancedChatServer.ino b/extras/AdvancedChatServer/AdvancedChatServer.ino similarity index 100% rename from examples/AdvancedChatServer/AdvancedChatServer.ino rename to extras/AdvancedChatServer/AdvancedChatServer.ino diff --git a/examples/BarometricPressureWebServer/BarometricPressureWebServer.ino b/extras/BarometricPressureWebServer/BarometricPressureWebServer.ino similarity index 100% rename from examples/BarometricPressureWebServer/BarometricPressureWebServer.ino rename to extras/BarometricPressureWebServer/BarometricPressureWebServer.ino diff --git a/examples/ChatServer/ChatServer.ino b/extras/ChatServer/ChatServer.ino similarity index 100% rename from examples/ChatServer/ChatServer.ino rename to extras/ChatServer/ChatServer.ino diff --git a/examples/DhcpAddressPrinter/DhcpAddressPrinter.ino b/extras/DhcpAddressPrinter/DhcpAddressPrinter.ino similarity index 100% rename from examples/DhcpAddressPrinter/DhcpAddressPrinter.ino rename to extras/DhcpAddressPrinter/DhcpAddressPrinter.ino diff --git a/examples/DhcpChatServer/DhcpChatServer.ino b/extras/DhcpChatServer/DhcpChatServer.ino similarity index 100% rename from examples/DhcpChatServer/DhcpChatServer.ino rename to extras/DhcpChatServer/DhcpChatServer.ino diff --git a/examples/PagerServer/PagerServer.ino b/extras/PagerServer/PagerServer.ino similarity index 100% rename from examples/PagerServer/PagerServer.ino rename to extras/PagerServer/PagerServer.ino diff --git a/examples/UDPSendReceiveString/UDPSendReceiveString.ino b/extras/UDPSendReceiveString/UDPSendReceiveString.ino similarity index 100% rename from examples/UDPSendReceiveString/UDPSendReceiveString.ino rename to extras/UDPSendReceiveString/UDPSendReceiveString.ino diff --git a/examples/UdpNtpClient/UdpNtpClient.ino b/extras/UdpNtpClient/UdpNtpClient.ino similarity index 100% rename from examples/UdpNtpClient/UdpNtpClient.ino rename to extras/UdpNtpClient/UdpNtpClient.ino diff --git a/examples/WebClient/WebClient.ino b/extras/WebClient/WebClient.ino similarity index 100% rename from examples/WebClient/WebClient.ino rename to extras/WebClient/WebClient.ino diff --git a/examples/WebClientRepeating/WebClientRepeating.ino b/extras/WebClientRepeating/WebClientRepeating.ino similarity index 100% rename from examples/WebClientRepeating/WebClientRepeating.ino rename to extras/WebClientRepeating/WebClientRepeating.ino diff --git a/library.properties b/library.properties index f3f36166..59dfe381 100644 --- a/library.properties +++ b/library.properties @@ -1,10 +1,10 @@ -name=Ethernet -version=2.0.2 -author=Various (see AUTHORS file for details) -maintainer=Arduino -sentence=Enables network connection (local and Internet) using the Arduino Ethernet Board or Shield. -paragraph=With this library you can use the Arduino Ethernet (shield or board) to connect to Internet. The library provides both client and server functionalities. The library permits you to connect to a local network also with DHCP and to resolve DNS. +name=EthernetSP +version=1.0.0 +author=crane-elec based on Various +maintainer=crane-elec +sentence=Enables network connection using the W5500-Ether add-on for Spresense. +paragraph=This library is a modified version of the standard Arduino Ethernet library, specifically adapted for the "W5500-Ether" add-on board for Spresense. https://crane-elec.co.jp/products/vol-20/ category=Communication -url=https://www.arduino.cc/en/Reference/Ethernet -architectures=* -includes=Ethernet.h +url=https://github.com/crane-elec/EthernetSP +architectures=SPRESENSE +includes= diff --git a/src/Ethernet.cpp b/src/Ethernet.cpp index 8d9ce7fd..0a4d076d 100644 --- a/src/Ethernet.cpp +++ b/src/Ethernet.cpp @@ -23,6 +23,10 @@ #include "utility/w5100.h" #include "Dhcp.h" +#if defined(ARDUINO_ARCH_SPRESENSE) +#define SPI SPI5 +#endif + IPAddress EthernetClass::_dnsServerAddress; DhcpClass* EthernetClass::_dhcp = NULL; @@ -234,3 +238,5 @@ void EthernetClass::setRetransmissionCount(uint8_t num) EthernetClass Ethernet; + +#undef SPI diff --git a/src/EthernetClient.cpp b/src/EthernetClient.cpp index 5a20c748..5a17e7d7 100644 --- a/src/EthernetClient.cpp +++ b/src/EthernetClient.cpp @@ -23,6 +23,10 @@ #include "Dns.h" #include "utility/w5100.h" +#if defined(ARDUINO_ARCH_SPRESENSE) +#define SPI SPI5 +#endif + int EthernetClient::connect(const char * host, uint16_t port) { DNSClient dns; // Look up the host first @@ -211,3 +215,5 @@ uint16_t EthernetClient::remotePort() SPI.endTransaction(); return port; } + +#undef SPI diff --git a/src/EthernetSP.h b/src/EthernetSP.h new file mode 100644 index 00000000..49e1096c --- /dev/null +++ b/src/EthernetSP.h @@ -0,0 +1,31 @@ +#ifndef _ETHERNETSP_H_ +#define _ETHERNETSP_H_ + +// reset +#define SJ1_12 (21) // Jumper setting is SJ1=12, use D21/EMMC_DATA3 +#define SJ1_23 (26) // Jumper setting is SJ1=23, use D26/I2S_BCK + +// cs +#define SJ2_12 (19) // Jumper setting is SJ2=12, use D19/I2S_DIN +#define SJ2_23 (24) // Jumper setting is SJ2=23, use D24/SPI5_CS_X + + +#if defined(ARDUINO_ARCH_SPRESENSE) + +#include "Ethernet.h" +void W5500ETH_reset(uint8_t sj1_rst) +{ + pinMode(sj1_rst, OUTPUT); + digitalWrite(sj1_rst, LOW); + delay(50); + digitalWrite(sj1_rst, HIGH); + return; +} + +#else + #error ("This library use only Sony spresense with W5500-Ether add-on board.") +#endif + + + +#endif diff --git a/src/socket.cpp b/src/socket.cpp index 7dc83feb..fa815a59 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -22,6 +22,10 @@ #include "Ethernet.h" #include "utility/w5100.h" +#if defined(ARDUINO_ARCH_SPRESENSE) +#define SPI SPI5 +#endif + #if ARDUINO >= 156 && !defined(ARDUINO_ARCH_PIC32) extern void yield(void); #else @@ -536,3 +540,5 @@ bool EthernetClass::socketSendUDP(uint8_t s) /* Sent ok */ return true; } + +#undef SPI diff --git a/src/utility/w5100.cpp b/src/utility/w5100.cpp index 6e7dbd20..f42fc495 100644 --- a/src/utility/w5100.cpp +++ b/src/utility/w5100.cpp @@ -12,6 +12,9 @@ #include "Ethernet.h" #include "w5100.h" +#if defined(ARDUINO_ARCH_SPRESENSE) +#define SPI SPI5 +#endif /***************************************************/ /** Default SS pin setting **/ @@ -218,6 +221,10 @@ uint8_t W5100Class::softReset(void) uint8_t W5100Class::isW5100(void) { +#if defined(ARDUINO_ARCH_SPRESENSE) + // ignore to use W5500-Ether. + return 0; +#else chip = 51; //Serial.println("w5100.cpp: detect W5100 chip"); if (!softReset()) return 0; @@ -229,10 +236,15 @@ uint8_t W5100Class::isW5100(void) if (readMR() != 0x00) return 0; //Serial.println("chip is W5100"); return 1; +#endif } uint8_t W5100Class::isW5200(void) { +#if defined(ARDUINO_ARCH_SPRESENSE) + // ignore to use W5500-Ether. + return 0; +#else chip = 52; //Serial.println("w5100.cpp: detect W5200 chip"); if (!softReset()) return 0; @@ -248,6 +260,7 @@ uint8_t W5100Class::isW5200(void) if (ver != 3) return 0; //Serial.println("chip is W5200"); return 1; +#endif } uint8_t W5100Class::isW5500(void) @@ -368,6 +381,17 @@ uint16_t W5100Class::write(uint16_t addr, const uint8_t *buf, uint16_t len) } SPI.transfer(cmd, len + 3); } else { +#if defined(ARDUINO_ARCH_SPRESENSE) + // Spresense SPI peripheral must send one transfer, one time. + uint8_t *txbuf = (uint8_t*)(malloc(3 + len)); + if (txbuf) + { + memcpy(txbuf, cmd, 3); + memcpy(txbuf + 3, buf, len); + SPI.transfer(txbuf, 3 + len); + free(txbuf); + } +#else SPI.transfer(cmd, 3); #ifdef SPI_HAS_TRANSFER_BUF SPI.transfer(buf, NULL, len); @@ -376,6 +400,7 @@ uint16_t W5100Class::write(uint16_t addr, const uint8_t *buf, uint16_t len) for (uint16_t i=0; i < len; i++) { SPI.transfer(buf[i]); } +#endif #endif } resetSS(); @@ -457,9 +482,21 @@ uint16_t W5100Class::read(uint16_t addr, uint8_t *buf, uint16_t len) cmd[2] = ((addr >> 6) & 0xE0) | 0x18; // 2K buffers #endif } + #if defined(ARDUINO_ARCH_SPRESENSE) + uint8_t *rxbuf = (uint8_t*)(malloc(3 + len)); + if (rxbuf) + { + memset(rxbuf, 0, 3 + len); + memcpy(rxbuf, cmd, 3); + SPI.transfer(rxbuf, 3 + len);// [in,out] buf Buffer to send and receive. + memcpy(buf, rxbuf + 3, len); + free(rxbuf); + } + #else SPI.transfer(cmd, 3); memset(buf, 0, len); SPI.transfer(buf, len); + #endif resetSS(); } return len; @@ -472,3 +509,5 @@ void W5100Class::execCmdSn(SOCKET s, SockCMD _cmd) // Wait for command to complete while (readSnCR(s)) ; } + +#undef SPI diff --git a/src/utility/w5100.h b/src/utility/w5100.h index b2e8ec83..805975b1 100644 --- a/src/utility/w5100.h +++ b/src/utility/w5100.h @@ -33,6 +33,12 @@ #error "Ethernet.h must be included before w5100.h" #endif +// Spresense with W5500-Ether add-on. When SJ2 is "2-3 short", must be SPI_MODE3 +#if defined(ARDUINO_ARCH_SPRESENSE) +#undef SPI_ETHERNET_SETTINGS +#define SPI_ETHERNET_SETTINGS SPISettings(13000000, MSBFIRST, SPI_MODE3) +#define SPI5_CS 24 +#endif // Arduino 101's SPI can not run faster than 8 MHz. #if defined(ARDUINO_ARCH_ARC32) @@ -432,6 +438,19 @@ class W5100Class { inline static void resetSS() { *(ss_pin_reg+6) = ss_pin_mask; } +#elif defined(ARDUINO_ARCH_SPRESENSE) + inline static void initSS() { + if (ss_pin != SPI5_CS) + pinMode(ss_pin, OUTPUT); + } + inline static void setSS() { + if (ss_pin != SPI5_CS) + digitalWrite(ss_pin, LOW); + } + inline static void resetSS() { + if (ss_pin != SPI5_CS) + digitalWrite(ss_pin, HIGH); + } #else inline static void initSS() { pinMode(ss_pin, OUTPUT);