Skip to content

Commit 8698fef

Browse files
committed
feat: Ethernet - Arduino Ethernet library compatibility layer
1 parent a45b5af commit 8698fef

File tree

6 files changed

+463
-14
lines changed

6 files changed

+463
-14
lines changed

Diff for: CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ set(ARDUINO_LIBRARY_ESP_SR_SRCS
139139

140140
set(ARDUINO_LIBRARY_ESPmDNS_SRCS libraries/ESPmDNS/src/ESPmDNS.cpp)
141141

142-
set(ARDUINO_LIBRARY_Ethernet_SRCS libraries/Ethernet/src/ETH.cpp)
142+
set(ARDUINO_LIBRARY_Ethernet_SRCS
143+
libraries/Ethernet/src/ETH.cpp
144+
libraries/Ethernet/src/EthernetCompat.cpp)
143145

144146
set(ARDUINO_LIBRARY_FFat_SRCS libraries/FFat/src/FFat.cpp)
145147

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
Web client
3+
4+
This sketch connects to a website (http://www.google.com)
5+
using an Arduino WIZnet Ethernet shield.
6+
7+
Circuit:
8+
* Ethernet shield attached to default SPI pins
9+
10+
created 18 Dec 2009
11+
by David A. Mellis
12+
modified 9 Apr 2012
13+
by Tom Igoe, based on work by Adrian McEwen
14+
*/
15+
16+
#include <SPI.h>
17+
#include <EthernetCompat.h>
18+
19+
// Enter a MAC address for your controller below.
20+
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
21+
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
22+
23+
// if you don't want to use DNS (and reduce your sketch size)
24+
// use the numeric IP instead of the name for the server:
25+
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
26+
char server[] = "www.google.com"; // name address for Google (using DNS)
27+
28+
// Set the static IP address to use if the DHCP fails to assign
29+
IPAddress ip(192, 168, 0, 177);
30+
IPAddress myDns(192, 168, 0, 1);
31+
32+
// Initialize the Ethernet client library
33+
// with the IP address and port of the server
34+
// that you want to connect to (port 80 is default for HTTP):
35+
EthernetClient client;
36+
37+
// Variables to measure the speed
38+
unsigned long beginMicros, endMicros;
39+
unsigned long byteCount = 0;
40+
bool printWebData = true; // set to false for better speed measurement
41+
42+
void setup() {
43+
// You can use Ethernet.init to configure the pins
44+
// the values in the commented out init are the defaults
45+
// #define IRQ_PIN 4 //IRQ pin must be wired
46+
// #define RESET_PIN -1 // reset pin is optional
47+
// Ethernet.init(ETH_PHY_W5500, ETH_PHY_ADDR_AUTO, SS, IRQ_PIN, RESET_PIN);
48+
// Ethernet.init(ETH_PHY_W5500, ETH_PHY_ADDR_AUTO, SS, IRQ_PIN, RESET_PIN, SPI, SCK, MISO, MOSI);
49+
50+
// Open serial communications and wait for port to open:
51+
Serial.begin(115200);
52+
while (!Serial) {
53+
; // wait for serial port to connect. Needed for native USB port only
54+
}
55+
56+
// start the Ethernet connection:
57+
Serial.println("Initialize Ethernet with DHCP:");
58+
if (Ethernet.begin(mac) == 0) {
59+
Serial.println("Failed to configure Ethernet using DHCP");
60+
// Check for Ethernet hardware present
61+
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
62+
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
63+
while (true) {
64+
delay(1); // do nothing, no point running without Ethernet hardware
65+
}
66+
}
67+
if (Ethernet.linkStatus() == LinkOFF) {
68+
Serial.println("Ethernet cable is not connected.");
69+
}
70+
// try to configure using IP address instead of DHCP:
71+
Ethernet.begin(mac, ip, myDns);
72+
} else {
73+
Serial.print(" DHCP assigned IP ");
74+
Serial.println(Ethernet.localIP());
75+
}
76+
// give the Ethernet shield a second to initialize:
77+
delay(1000);
78+
Serial.print("connecting to ");
79+
Serial.print(server);
80+
Serial.println("...");
81+
82+
// if you get a connection, report back via serial:
83+
if (client.connect(server, 80)) {
84+
Serial.print("connected to ");
85+
Serial.println(client.remoteIP());
86+
// Make a HTTP request:
87+
client.println("GET /search?q=arduino HTTP/1.1");
88+
client.println("Host: www.google.com");
89+
client.println("Connection: close");
90+
client.println();
91+
} else {
92+
// if you didn't get a connection to the server:
93+
Serial.println("connection failed");
94+
}
95+
beginMicros = micros();
96+
}
97+
98+
void loop() {
99+
// if there are incoming bytes available
100+
// from the server, read them and print them:
101+
int len = client.available();
102+
if (len > 0) {
103+
byte buffer[80];
104+
if (len > 80) {
105+
len = 80;
106+
}
107+
client.read(buffer, len);
108+
if (printWebData) {
109+
Serial.write(buffer, len); // show in the serial monitor (slows some boards)
110+
}
111+
byteCount = byteCount + len;
112+
}
113+
114+
// if the server's disconnected, stop the client:
115+
if (!client.connected()) {
116+
endMicros = micros();
117+
Serial.println();
118+
Serial.println("disconnecting.");
119+
client.stop();
120+
Serial.print("Received ");
121+
Serial.print(byteCount);
122+
Serial.print(" bytes in ");
123+
float seconds = (float) (endMicros - beginMicros) / 1000000.0;
124+
Serial.print(seconds, 4);
125+
float rate = (float) byteCount / seconds / 1000.0;
126+
Serial.print(", rate = ");
127+
Serial.print(rate);
128+
Serial.print(" kbytes/second");
129+
Serial.println();
130+
131+
// do nothing forevermore:
132+
while (true) {
133+
delay(1);
134+
}
135+
}
136+
}

Diff for: libraries/Ethernet/src/ETH.cpp

+16-12
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ esp_err_t ETHClass::eth_spi_write(uint32_t cmd, uint32_t addr, const void *data,
415415
}
416416
#endif
417417

418-
bool ETHClass::beginSPI(eth_phy_type_t type, int32_t phy_addr, int cs, int irq, int rst,
418+
bool ETHClass::beginSPI(eth_phy_type_t type, int32_t phy_addr, uint8_t* mac_addr_p, int cs, int irq, int rst,
419419
#if ETH_SPI_SUPPORTS_CUSTOM
420420
SPIClass *spi,
421421
#endif
@@ -611,16 +611,20 @@ bool ETHClass::beginSPI(eth_phy_type_t type, int32_t phy_addr, int cs, int irq,
611611
return false;
612612
}
613613

614-
// Derive a new MAC address for this interface
615-
uint8_t base_mac_addr[ETH_ADDR_LEN];
616-
ret = esp_efuse_mac_get_default(base_mac_addr);
617-
if (ret != ESP_OK) {
618-
log_e("Get EFUSE MAC failed: %d", ret);
619-
return false;
620-
}
621614
uint8_t mac_addr[ETH_ADDR_LEN];
622-
base_mac_addr[ETH_ADDR_LEN - 1] += _eth_index; //Increment by the ETH number
623-
esp_derive_local_mac(mac_addr, base_mac_addr);
615+
if (mac_addr_p != nullptr) {
616+
memcpy(mac_addr, mac_addr_p, ETH_ADDR_LEN);
617+
} else {
618+
// Derive a new MAC address for this interface
619+
uint8_t base_mac_addr[ETH_ADDR_LEN];
620+
ret = esp_efuse_mac_get_default(base_mac_addr);
621+
if (ret != ESP_OK) {
622+
log_e("Get EFUSE MAC failed: %d", ret);
623+
return false;
624+
}
625+
base_mac_addr[ETH_ADDR_LEN - 1] += _eth_index; //Increment by the ETH number
626+
esp_derive_local_mac(mac_addr, base_mac_addr);
627+
}
624628

625629
ret = esp_eth_ioctl(_eth_handle, ETH_CMD_S_MAC_ADDR, mac_addr);
626630
if (ret != ESP_OK) {
@@ -721,13 +725,13 @@ bool ETHClass::beginSPI(eth_phy_type_t type, int32_t phy_addr, int cs, int irq,
721725
#if ETH_SPI_SUPPORTS_CUSTOM
722726
bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int cs, int irq, int rst, SPIClass &spi, uint8_t spi_freq_mhz) {
723727

724-
return beginSPI(type, phy_addr, cs, irq, rst, &spi, -1, -1, -1, SPI2_HOST, spi_freq_mhz);
728+
return beginSPI(type, phy_addr, nullptr, cs, irq, rst, &spi, -1, -1, -1, SPI2_HOST, spi_freq_mhz);
725729
}
726730
#endif
727731

728732
bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int cs, int irq, int rst, spi_host_device_t spi_host, int sck, int miso, int mosi, uint8_t spi_freq_mhz) {
729733

730-
return beginSPI(type, phy_addr, cs, irq, rst,
734+
return beginSPI(type, phy_addr, nullptr, cs, irq, rst,
731735
#if ETH_SPI_SUPPORTS_CUSTOM
732736
NULL,
733737
#endif

Diff for: libraries/Ethernet/src/ETH.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,13 @@ class ETHClass : public NetworkInterface {
192192
#endif /* CONFIG_ETH_USE_ESP32_EMAC */
193193

194194
static bool ethDetachBus(void *bus_pointer);
195-
bool beginSPI(eth_phy_type_t type, int32_t phy_addr, int cs, int irq, int rst,
195+
bool beginSPI(eth_phy_type_t type, int32_t phy_addr, uint8_t* mac_addr, int cs, int irq, int rst,
196196
#if ETH_SPI_SUPPORTS_CUSTOM
197197
SPIClass *spi,
198198
#endif
199199
int sck, int miso, int mosi, spi_host_device_t spi_host, uint8_t spi_freq_mhz);
200+
201+
friend class EthernetClass; // to access beginSPI
200202
};
201203

202204
extern ETHClass ETH;

0 commit comments

Comments
 (0)