Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[ETH] Implement SPI support, multiple interfaces and more #8575

Merged
merged 2 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions libraries/Ethernet/examples/ETH_LAN8720/ETH_LAN8720.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@

*/

// Important to be defined BEFORE including ETH.h for ETH.begin() to work.
// Example RMII LAN8720 (Olimex, etc.)
#define ETH_PHY_TYPE ETH_PHY_LAN8720
#define ETH_PHY_ADDR 0
#define ETH_PHY_MDC 23
#define ETH_PHY_MDIO 18
#define ETH_PHY_POWER -1
#define ETH_CLK_MODE ETH_CLOCK_GPIO0_IN

#include <ETH.h>

static bool eth_connected = false;

void WiFiEvent(WiFiEvent_t event)
void onEvent(arduino_event_id_t event, arduino_event_info_t info)
{
switch (event) {
case ARDUINO_EVENT_ETH_START:
Expand All @@ -19,18 +28,14 @@ void WiFiEvent(WiFiEvent_t event)
Serial.println("ETH Connected");
break;
case ARDUINO_EVENT_ETH_GOT_IP:
Serial.print("ETH MAC: ");
Serial.print(ETH.macAddress());
Serial.print(", IPv4: ");
Serial.print(ETH.localIP());
if (ETH.fullDuplex()) {
Serial.print(", FULL_DUPLEX");
}
Serial.print(", ");
Serial.print(ETH.linkSpeed());
Serial.println("Mbps");
Serial.println("ETH Got IP");
ETH.printInfo(Serial);
eth_connected = true;
break;
case ARDUINO_EVENT_ETH_LOST_IP:
Serial.println("ETH Lost IP");
eth_connected = false;
break;
case ARDUINO_EVENT_ETH_DISCONNECTED:
Serial.println("ETH Disconnected");
eth_connected = false;
Expand Down Expand Up @@ -67,7 +72,7 @@ void testClient(const char * host, uint16_t port)
void setup()
{
Serial.begin(115200);
WiFi.onEvent(WiFiEvent);
WiFi.onEvent(onEvent);
ETH.begin();
}

Expand Down
27 changes: 12 additions & 15 deletions libraries/Ethernet/examples/ETH_TLK110/ETH_TLK110.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@

#include <ETH.h>

#define ETH_TYPE ETH_PHY_TLK110
#define ETH_ADDR 31
#define ETH_POWER_PIN 17
#define ETH_MDC_PIN 23
#define ETH_MDIO_PIN 18
#define ETH_TYPE ETH_PHY_TLK110
#define ETH_POWER_PIN 17
#define ETH_CLK_MODE ETH_CLOCK_GPIO0_IN

static bool eth_connected = false;

void WiFiEvent(WiFiEvent_t event)
void onEvent(arduino_event_id_t event, arduino_event_info_t info)
{
switch (event) {
case ARDUINO_EVENT_ETH_START:
Expand All @@ -25,18 +26,14 @@ void WiFiEvent(WiFiEvent_t event)
Serial.println("ETH Connected");
break;
case ARDUINO_EVENT_ETH_GOT_IP:
Serial.print("ETH MAC: ");
Serial.print(ETH.macAddress());
Serial.print(", IPv4: ");
Serial.print(ETH.localIP());
if (ETH.fullDuplex()) {
Serial.print(", FULL_DUPLEX");
}
Serial.print(", ");
Serial.print(ETH.linkSpeed());
Serial.println("Mbps");
Serial.println("ETH Got IP");
ETH.printInfo(Serial);
eth_connected = true;
break;
case ARDUINO_EVENT_ETH_LOST_IP:
Serial.println("ETH Lost IP");
eth_connected = false;
break;
case ARDUINO_EVENT_ETH_DISCONNECTED:
Serial.println("ETH Disconnected");
eth_connected = false;
Expand Down Expand Up @@ -73,8 +70,8 @@ void testClient(const char * host, uint16_t port)
void setup()
{
Serial.begin(115200);
WiFi.onEvent(WiFiEvent);
ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE);
WiFi.onEvent(onEvent);
ETH.begin(ETH_TYPE, ETH_ADDR, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_POWER_PIN, ETH_CLK_MODE);
}


Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
This sketch shows the Ethernet event usage

*/

#include <ETH.h>
#include <SPI.h>

// Set this to 1 to enable dual Ethernet support
#define USE_TWO_ETH_PORTS 0

#define ETH_TYPE ETH_PHY_W5500
#define ETH_ADDR 1
#define ETH_CS 15
#define ETH_IRQ 4
#define ETH_RST 5

// SPI pins
#define ETH_SPI_SCK 14
#define ETH_SPI_MISO 12
#define ETH_SPI_MOSI 13

#if USE_TWO_ETH_PORTS
// Second port on shared SPI bus
#define ETH1_TYPE ETH_PHY_W5500
#define ETH1_ADDR 1
#define ETH1_CS 32
#define ETH1_IRQ 33
#define ETH1_RST 18
ETHClass ETH1(1);
#endif

static bool eth_connected = false;

void onEvent(arduino_event_id_t event, arduino_event_info_t info)
{
switch (event) {
case ARDUINO_EVENT_ETH_START:
Serial.println("ETH Started");
//set eth hostname here
ETH.setHostname("esp32-eth0");
break;
case ARDUINO_EVENT_ETH_CONNECTED:
Serial.println("ETH Connected");
break;
case ARDUINO_EVENT_ETH_GOT_IP:
Serial.printf("ETH Got IP: '%s'\n", esp_netif_get_desc(info.got_ip.esp_netif));
ETH.printInfo(Serial);
#if USE_TWO_ETH_PORTS
ETH1.printInfo(Serial);
#endif
eth_connected = true;
break;
case ARDUINO_EVENT_ETH_LOST_IP:
Serial.println("ETH Lost IP");
eth_connected = false;
break;
case ARDUINO_EVENT_ETH_DISCONNECTED:
Serial.println("ETH Disconnected");
eth_connected = false;
break;
case ARDUINO_EVENT_ETH_STOP:
Serial.println("ETH Stopped");
eth_connected = false;
break;
default:
break;
}
}

void testClient(const char * host, uint16_t port)
{
Serial.print("\nconnecting to ");
Serial.println(host);

WiFiClient client;
if (!client.connect(host, port)) {
Serial.println("connection failed");
return;
}
client.printf("GET / HTTP/1.1\r\nHost: %s\r\n\r\n", host);
while (client.connected() && !client.available());
while (client.available()) {
Serial.write(client.read());
}

Serial.println("closing connection\n");
client.stop();
}

void setup()
{
Serial.begin(115200);
WiFi.onEvent(onEvent);

SPI.begin(ETH_SPI_SCK, ETH_SPI_MISO, ETH_SPI_MOSI);
ETH.begin(ETH_TYPE, ETH_ADDR, ETH_CS, ETH_IRQ, ETH_RST, SPI);
#if USE_TWO_ETH_PORTS
ETH1.begin(ETH1_TYPE, ETH1_ADDR, ETH1_CS, ETH1_IRQ, ETH1_RST, SPI);
#endif
}


void loop()
{
if (eth_connected) {
testClient("google.com", 80);
}
delay(10000);
}
Empty file.
107 changes: 107 additions & 0 deletions libraries/Ethernet/examples/ETH_W5500_IDF_SPI/ETH_W5500_IDF_SPI.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
This sketch shows the Ethernet event usage

*/

#include <ETH.h>

// Set this to 1 to enable dual Ethernet support
#define USE_TWO_ETH_PORTS 0

#define ETH_TYPE ETH_PHY_W5500
#define ETH_ADDR 1
#define ETH_CS 15
#define ETH_IRQ 4
#define ETH_RST 5
#define ETH_SPI_HOST SPI2_HOST
#define ETH_SPI_SCK 14
#define ETH_SPI_MISO 12
#define ETH_SPI_MOSI 13

#if USE_TWO_ETH_PORTS
// Second port on shared SPI bus
#define ETH1_TYPE ETH_PHY_W5500
#define ETH1_ADDR 1
#define ETH1_CS 32
#define ETH1_IRQ 33
#define ETH1_RST 18
ETHClass ETH1(1);
#endif

static bool eth_connected = false;

void onEvent(arduino_event_id_t event, arduino_event_info_t info)
{
switch (event) {
case ARDUINO_EVENT_ETH_START:
Serial.println("ETH Started");
//set eth hostname here
ETH.setHostname("esp32-eth0");
break;
case ARDUINO_EVENT_ETH_CONNECTED:
Serial.println("ETH Connected");
break;
case ARDUINO_EVENT_ETH_GOT_IP:
Serial.printf("ETH Got IP: '%s'\n", esp_netif_get_desc(info.got_ip.esp_netif));
ETH.printInfo(Serial);
#if USE_TWO_ETH_PORTS
ETH1.printInfo(Serial);
#endif
eth_connected = true;
break;
case ARDUINO_EVENT_ETH_LOST_IP:
Serial.println("ETH Lost IP");
eth_connected = false;
break;
case ARDUINO_EVENT_ETH_DISCONNECTED:
Serial.println("ETH Disconnected");
eth_connected = false;
break;
case ARDUINO_EVENT_ETH_STOP:
Serial.println("ETH Stopped");
eth_connected = false;
break;
default:
break;
}
}

void testClient(const char * host, uint16_t port)
{
Serial.print("\nconnecting to ");
Serial.println(host);

WiFiClient client;
if (!client.connect(host, port)) {
Serial.println("connection failed");
return;
}
client.printf("GET / HTTP/1.1\r\nHost: %s\r\n\r\n", host);
while (client.connected() && !client.available());
while (client.available()) {
Serial.write(client.read());
}

Serial.println("closing connection\n");
client.stop();
}

void setup()
{
Serial.begin(115200);
WiFi.onEvent(onEvent);
ETH.begin(ETH_TYPE, ETH_ADDR, ETH_CS, ETH_IRQ, ETH_RST, ETH_SPI_HOST, ETH_SPI_SCK, ETH_SPI_MISO, ETH_SPI_MOSI);
#if USE_TWO_ETH_PORTS
// Since SPI bus is shared, we should skip the SPI pins when calling ETH1.begin()
ETH1.begin(ETH1_TYPE, ETH1_ADDR, ETH1_CS, ETH1_IRQ, ETH1_RST, ETH_SPI_HOST);
#endif
}


void loop()
{
if (eth_connected) {
testClient("google.com", 80);
}
delay(10000);
}
Loading