Skip to content

Commit c613d69

Browse files
committed
Enable ETH over Arduino SPI and add example
1 parent 124cf00 commit c613d69

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed

libraries/Ethernet/examples/ETH_W5500_Arduino_SPI/.skip.esp32h2

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
This sketch shows the Ethernet event usage
3+
4+
*/
5+
6+
#include <ETH.h>
7+
#include <SPI.h>
8+
9+
// Set this to 1 to enable dual Ethernet support
10+
#define USE_TWO_ETH_PORTS 0
11+
12+
#define ETH_TYPE ETH_PHY_W5500
13+
#define ETH_ADDR 1
14+
#define ETH_CS 15
15+
#define ETH_IRQ 4
16+
#define ETH_RST 5
17+
18+
// SPI pins
19+
#define ETH_SPI_SCK 14
20+
#define ETH_SPI_MISO 12
21+
#define ETH_SPI_MOSI 13
22+
23+
#if USE_TWO_ETH_PORTS
24+
// Second port on shared SPI bus
25+
#define ETH1_TYPE ETH_PHY_W5500
26+
#define ETH1_ADDR 1
27+
#define ETH1_CS 32
28+
#define ETH1_IRQ 33
29+
#define ETH1_RST 18
30+
ETHClass ETH1(1);
31+
#endif
32+
33+
static bool eth_connected = false;
34+
35+
void onEvent(arduino_event_id_t event, arduino_event_info_t info)
36+
{
37+
switch (event) {
38+
case ARDUINO_EVENT_ETH_START:
39+
Serial.println("ETH Started");
40+
//set eth hostname here
41+
ETH.setHostname("esp32-eth0");
42+
break;
43+
case ARDUINO_EVENT_ETH_CONNECTED:
44+
Serial.println("ETH Connected");
45+
break;
46+
case ARDUINO_EVENT_ETH_GOT_IP:
47+
Serial.printf("ETH Got IP: '%s'\n", esp_netif_get_desc(info.got_ip.esp_netif));
48+
ETH.printInfo(Serial);
49+
#if USE_TWO_ETH_PORTS
50+
ETH1.printInfo(Serial);
51+
#endif
52+
eth_connected = true;
53+
break;
54+
case ARDUINO_EVENT_ETH_LOST_IP:
55+
Serial.println("ETH Lost IP");
56+
eth_connected = false;
57+
break;
58+
case ARDUINO_EVENT_ETH_DISCONNECTED:
59+
Serial.println("ETH Disconnected");
60+
eth_connected = false;
61+
break;
62+
case ARDUINO_EVENT_ETH_STOP:
63+
Serial.println("ETH Stopped");
64+
eth_connected = false;
65+
break;
66+
default:
67+
break;
68+
}
69+
}
70+
71+
void testClient(const char * host, uint16_t port)
72+
{
73+
Serial.print("\nconnecting to ");
74+
Serial.println(host);
75+
76+
WiFiClient client;
77+
if (!client.connect(host, port)) {
78+
Serial.println("connection failed");
79+
return;
80+
}
81+
client.printf("GET / HTTP/1.1\r\nHost: %s\r\n\r\n", host);
82+
while (client.connected() && !client.available());
83+
while (client.available()) {
84+
Serial.write(client.read());
85+
}
86+
87+
Serial.println("closing connection\n");
88+
client.stop();
89+
}
90+
91+
void setup()
92+
{
93+
Serial.begin(115200);
94+
WiFi.onEvent(onEvent);
95+
96+
SPI.begin(ETH_SPI_SCK, ETH_SPI_MISO, ETH_SPI_MOSI);
97+
ETH.begin(ETH_TYPE, ETH_ADDR, ETH_CS, ETH_IRQ, ETH_RST, SPI);
98+
#if USE_TWO_ETH_PORTS
99+
ETH1.begin(ETH1_TYPE, ETH1_ADDR, ETH1_CS, ETH1_IRQ, ETH1_RST, SPI);
100+
#endif
101+
}
102+
103+
104+
void loop()
105+
{
106+
if (eth_connected) {
107+
testClient("google.com", 80);
108+
}
109+
delay(10000);
110+
}

libraries/Ethernet/src/ETH.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
// #define ETH_PHY_SPI SPI
6262

6363
// This will be uncommented once custom SPI support is available in ESP-IDF
64-
#define ETH_SPI_SUPPORTS_CUSTOM 0
64+
#define ETH_SPI_SUPPORTS_CUSTOM 1
6565

6666
#include "WiFi.h"
6767
#if ETH_SPI_SUPPORTS_CUSTOM

0 commit comments

Comments
 (0)