Skip to content

Commit da42146

Browse files
committed
Handle Ethernet lost IP and more
1 parent 73daef8 commit da42146

File tree

7 files changed

+102
-54
lines changed

7 files changed

+102
-54
lines changed

libraries/Ethernet/examples/ETH_LAN8720/ETH_LAN8720.ino

+6-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
static bool eth_connected = false;
1818

19-
void WiFiEvent(WiFiEvent_t event)
19+
void onEvent(arduino_event_id_t event, arduino_event_info_t info)
2020
{
2121
switch (event) {
2222
case ARDUINO_EVENT_ETH_START:
@@ -32,6 +32,10 @@ void WiFiEvent(WiFiEvent_t event)
3232
ETH.printInfo(Serial);
3333
eth_connected = true;
3434
break;
35+
case ARDUINO_EVENT_ETH_LOST_IP:
36+
Serial.println("ETH Lost IP");
37+
eth_connected = false;
38+
break;
3539
case ARDUINO_EVENT_ETH_DISCONNECTED:
3640
Serial.println("ETH Disconnected");
3741
eth_connected = false;
@@ -68,7 +72,7 @@ void testClient(const char * host, uint16_t port)
6872
void setup()
6973
{
7074
Serial.begin(115200);
71-
WiFi.onEvent(WiFiEvent);
75+
WiFi.onEvent(onEvent);
7276
ETH.begin();
7377
}
7478

libraries/Ethernet/examples/ETH_TLK110/ETH_TLK110.ino

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
static bool eth_connected = false;
1616

17-
void WiFiEvent(WiFiEvent_t event)
17+
void onEvent(arduino_event_id_t event, arduino_event_info_t info)
1818
{
1919
switch (event) {
2020
case ARDUINO_EVENT_ETH_START:
@@ -30,6 +30,10 @@ void WiFiEvent(WiFiEvent_t event)
3030
ETH.printInfo(Serial);
3131
eth_connected = true;
3232
break;
33+
case ARDUINO_EVENT_ETH_LOST_IP:
34+
Serial.println("ETH Lost IP");
35+
eth_connected = false;
36+
break;
3337
case ARDUINO_EVENT_ETH_DISCONNECTED:
3438
Serial.println("ETH Disconnected");
3539
eth_connected = false;
@@ -66,7 +70,7 @@ void testClient(const char * host, uint16_t port)
6670
void setup()
6771
{
6872
Serial.begin(115200);
69-
WiFi.onEvent(WiFiEvent);
73+
WiFi.onEvent(onEvent);
7074
ETH.begin(ETH_TYPE, ETH_ADDR, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_POWER_PIN, ETH_CLK_MODE);
7175
}
7276

libraries/Ethernet/examples/ETH_W5500_IDF_SPI/ETH_W5500_IDF_SPI.ino

+29-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
44
*/
55

6+
#include <ETH.h>
7+
8+
// Set this to 1 to enable dual Ethernet support
9+
#define USE_TWO_ETH_PORTS 0
10+
611
#define ETH_TYPE ETH_PHY_W5500
712
#define ETH_ADDR 1
813
#define ETH_CS 15
@@ -13,26 +18,41 @@
1318
#define ETH_SPI_MISO 12
1419
#define ETH_SPI_MOSI 13
1520

16-
#include <ETH.h>
21+
#if USE_TWO_ETH_PORTS
22+
// Second port on shared SPI bus
23+
#define ETH1_TYPE ETH_PHY_W5500
24+
#define ETH1_ADDR 1
25+
#define ETH1_CS 32
26+
#define ETH1_IRQ 33
27+
#define ETH1_RST 18
28+
ETHClass ETH1(1);
29+
#endif
1730

1831
static bool eth_connected = false;
1932

20-
void WiFiEvent(WiFiEvent_t event)
33+
void onEvent(arduino_event_id_t event, arduino_event_info_t info)
2134
{
2235
switch (event) {
2336
case ARDUINO_EVENT_ETH_START:
2437
Serial.println("ETH Started");
2538
//set eth hostname here
26-
ETH.setHostname("esp32-ethernet");
39+
ETH.setHostname("esp32-eth0");
2740
break;
2841
case ARDUINO_EVENT_ETH_CONNECTED:
2942
Serial.println("ETH Connected");
3043
break;
3144
case ARDUINO_EVENT_ETH_GOT_IP:
32-
Serial.println("ETH Got IP");
45+
Serial.printf("ETH Got IP: '%s'\n", esp_netif_get_desc(info.got_ip.esp_netif));
3346
ETH.printInfo(Serial);
47+
#if USE_TWO_ETH_PORTS
48+
ETH1.printInfo(Serial);
49+
#endif
3450
eth_connected = true;
3551
break;
52+
case ARDUINO_EVENT_ETH_LOST_IP:
53+
Serial.println("ETH Lost IP");
54+
eth_connected = false;
55+
break;
3656
case ARDUINO_EVENT_ETH_DISCONNECTED:
3757
Serial.println("ETH Disconnected");
3858
eth_connected = false;
@@ -69,8 +89,12 @@ void testClient(const char * host, uint16_t port)
6989
void setup()
7090
{
7191
Serial.begin(115200);
72-
WiFi.onEvent(WiFiEvent);
92+
WiFi.onEvent(onEvent);
7393
ETH.begin(ETH_TYPE, ETH_ADDR, ETH_CS, ETH_IRQ, ETH_RST, ETH_SPI_HOST, ETH_SPI_SCK, ETH_SPI_MISO, ETH_SPI_MOSI);
94+
#if USE_TWO_ETH_PORTS
95+
// Since SPI bus is shared, we should skip the SPI pins when calling ETH1.begin()
96+
ETH1.begin(ETH1_TYPE, ETH1_ADDR, ETH1_CS, ETH1_IRQ, ETH1_RST, ETH_SPI_HOST);
97+
#endif
7498
}
7599

76100

libraries/Ethernet/src/ETH.cpp

+34-25
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ extern void add_esp_interface_netif(esp_interface_t interface, esp_netif_t* esp_
4545

4646
ETHClass::ETHClass(uint8_t eth_index)
4747
:_eth_started(false)
48-
,_use_static_ip(false)
4948
,_eth_handle(NULL)
5049
,_esp_netif(NULL)
5150
,_eth_index(eth_index)
@@ -616,7 +615,6 @@ bool ETHClass::begin(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq, int
616615
void ETHClass::end(void)
617616
{
618617
_eth_started = false;
619-
_use_static_ip = false;
620618

621619
if(_esp_netif != NULL){
622620
esp_netif_destroy(_esp_netif);
@@ -704,53 +702,52 @@ bool ETHClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, I
704702
}
705703
esp_err_t err = ESP_OK;
706704
esp_netif_ip_info_t info;
705+
esp_netif_dns_info_t d1;
706+
esp_netif_dns_info_t d2;
707+
d1.ip.type = IPADDR_TYPE_V4;
708+
d2.ip.type = IPADDR_TYPE_V4;
707709

708710
if(static_cast<uint32_t>(local_ip) != 0){
709711
info.ip.addr = static_cast<uint32_t>(local_ip);
710712
info.gw.addr = static_cast<uint32_t>(gateway);
711713
info.netmask.addr = static_cast<uint32_t>(subnet);
714+
d1.ip.u_addr.ip4.addr = static_cast<uint32_t>(dns1);
715+
d2.ip.u_addr.ip4.addr = static_cast<uint32_t>(dns2);
712716
} else {
713717
info.ip.addr = 0;
714718
info.gw.addr = 0;
715719
info.netmask.addr = 0;
720+
d1.ip.u_addr.ip4.addr = 0;
721+
d2.ip.u_addr.ip4.addr = 0;
716722
}
717723

724+
// Stop DHCPC
718725
err = esp_netif_dhcpc_stop(_esp_netif);
719726
if(err != ESP_OK && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED){
720727
log_e("DHCP could not be stopped! Error: %d", err);
721728
return false;
722729
}
723730

731+
// Set IPv4, Netmask, Gateway
724732
err = esp_netif_set_ip_info(_esp_netif, &info);
725733
if(err != ERR_OK){
726734
log_e("ETH IP could not be configured! Error: %d", err);
727735
return false;
728736
}
729737

730-
if(info.ip.addr){
731-
_use_static_ip = true;
732-
} else {
738+
// Set DNS1-Server
739+
esp_netif_set_dns_info(_esp_netif, ESP_NETIF_DNS_MAIN, &d1);
740+
741+
// Set DNS2-Server
742+
esp_netif_set_dns_info(_esp_netif, ESP_NETIF_DNS_BACKUP, &d2);
743+
744+
// Start DHCPC if static IP was set
745+
if(info.ip.addr == 0){
733746
err = esp_netif_dhcpc_start(_esp_netif);
734747
if(err != ESP_OK && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED){
735748
log_w("DHCP could not be started! Error: %d", err);
736749
return false;
737750
}
738-
_use_static_ip = false;
739-
}
740-
741-
esp_netif_dns_info_t d;
742-
d.ip.type = IPADDR_TYPE_V4;
743-
744-
if(static_cast<uint32_t>(dns1) != 0) {
745-
// Set DNS1-Server
746-
d.ip.u_addr.ip4.addr = static_cast<uint32_t>(dns1);
747-
esp_netif_set_dns_info(_esp_netif, ESP_NETIF_DNS_MAIN, &d);
748-
}
749-
750-
if(static_cast<uint32_t>(dns2) != 0) {
751-
// Set DNS2-Server
752-
d.ip.u_addr.ip4.addr = static_cast<uint32_t>(dns2);
753-
esp_netif_set_dns_info(_esp_netif, ESP_NETIF_DNS_BACKUP, &d);
754751
}
755752

756753
return true;
@@ -880,21 +877,24 @@ IPv6Address ETHClass::localIPv6()
880877
return IPv6Address(addr.addr);
881878
}
882879

883-
const char * ETHClass::ifkey(void){
880+
const char * ETHClass::ifkey(void)
881+
{
884882
if(_esp_netif == NULL){
885883
return "";
886884
}
887885
return esp_netif_get_ifkey(_esp_netif);
888886
}
889887

890-
const char * ETHClass::desc(void){
888+
const char * ETHClass::desc(void)
889+
{
891890
if(_esp_netif == NULL){
892891
return "";
893892
}
894893
return esp_netif_get_desc(_esp_netif);
895894
}
896895

897-
String ETHClass::impl_name(void){
896+
String ETHClass::impl_name(void)
897+
{
898898
if(_esp_netif == NULL){
899899
return String("");
900900
}
@@ -907,9 +907,18 @@ String ETHClass::impl_name(void){
907907
return String(netif_name);
908908
}
909909

910+
bool ETHClass::connected()
911+
{
912+
return WiFiGenericClass::getStatusBits() & ETH_CONNECTED_BIT;
913+
}
914+
915+
bool ETHClass::hasIP()
916+
{
917+
return WiFiGenericClass::getStatusBits() & ETH_HAS_IP_BIT;
918+
}
919+
910920
bool ETHClass::linkUp()
911921
{
912-
//return WiFiGenericClass::getStatusBits() & ETH_CONNECTED_BIT;
913922
if(_esp_netif == NULL){
914923
return false;
915924
}

libraries/Ethernet/src/ETH.h

+20-20
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class ETHClass {
118118
#if ETH_SPI_SUPPORTS_CUSTOM
119119
bool begin(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq, int rst, SPIClass &spi);
120120
#endif
121-
bool begin(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq, int rst, spi_host_device_t spi_host, int sck, int miso, int mosi);
121+
bool begin(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq, int rst, spi_host_device_t spi_host, int sck=-1, int miso=-1, int mosi=-1);
122122

123123
bool begin(){
124124
#if defined(ETH_PHY_TYPE) && defined(ETH_PHY_ADDR)
@@ -137,37 +137,38 @@ class ETHClass {
137137

138138
void end();
139139

140-
bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000);
141-
140+
// Netif APIs
141+
esp_netif_t * netif(void){ return _esp_netif; }
142+
bool config(IPAddress local_ip = (uint32_t)0x00000000, IPAddress gateway = (uint32_t)0x00000000, IPAddress subnet = (uint32_t)0x00000000, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000);
142143
const char * getHostname();
143144
bool setHostname(const char * hostname);
144-
145-
bool fullDuplex();
146-
bool linkUp();
147-
uint8_t linkSpeed();
148-
bool autoNegotiation();
149-
uint32_t phyAddr();
150-
151-
bool enableIpV6();
152-
IPv6Address localIPv6();
153-
154145
IPAddress localIP();
155146
IPAddress subnetMask();
156147
IPAddress gatewayIP();
157148
IPAddress dnsIP(uint8_t dns_no = 0);
158-
159149
IPAddress broadcastIP();
160150
IPAddress networkID();
161151
uint8_t subnetCIDR();
162-
163-
uint8_t * macAddress(uint8_t* mac);
164-
String macAddress();
165-
166-
esp_netif_t * netif(void){ return _esp_netif; }
152+
bool enableIpV6();
153+
IPv6Address localIPv6();
167154
const char * ifkey(void);
168155
const char * desc(void);
169156
String impl_name(void);
170157

158+
// Event based getters
159+
bool connected();
160+
bool hasIP();
161+
162+
// ETH Handle APIs
163+
uint8_t * macAddress(uint8_t* mac);
164+
String macAddress();
165+
bool fullDuplex();
166+
bool linkUp();
167+
uint8_t linkSpeed();
168+
bool autoNegotiation();
169+
uint32_t phyAddr();
170+
171+
// Info APIs
171172
void printInfo(Print & out);
172173

173174
friend class WiFiClient;
@@ -188,7 +189,6 @@ class ETHClass {
188189

189190
private:
190191
bool _eth_started;
191-
bool _use_static_ip;
192192
esp_eth_handle_t _eth_handle;
193193
esp_netif_t *_esp_netif;
194194
uint8_t _eth_index;

libraries/WiFi/src/WiFiGeneric.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,9 @@ static void _arduino_event_cb(void* arg, esp_event_base_t event_base, int32_t ev
448448
#endif
449449
arduino_event.event_id = ARDUINO_EVENT_ETH_GOT_IP;
450450
memcpy(&arduino_event.event_info.got_ip, event_data, sizeof(ip_event_got_ip_t));
451+
} else if (event_base == ETH_EVENT && event_id == IP_EVENT_ETH_LOST_IP) {
452+
log_v("Ethernet Lost IP");
453+
arduino_event.event_id = ARDUINO_EVENT_ETH_LOST_IP;
451454

452455
/*
453456
* IPv6
@@ -870,6 +873,7 @@ const char * WiFiGenericClass::eventName(arduino_event_id_t id) {
870873
case ARDUINO_EVENT_ETH_CONNECTED: return "ETH_CONNECTED";
871874
case ARDUINO_EVENT_ETH_DISCONNECTED: return "ETH_DISCONNECTED";
872875
case ARDUINO_EVENT_ETH_GOT_IP: return "ETH_GOT_IP";
876+
case ARDUINO_EVENT_ETH_LOST_IP: return "ETH_LOST_IP";
873877
case ARDUINO_EVENT_ETH_GOT_IP6: return "ETH_GOT_IP6";
874878
case ARDUINO_EVENT_WPS_ER_SUCCESS: return "WPS_ER_SUCCESS";
875879
case ARDUINO_EVENT_WPS_ER_FAILED: return "WPS_ER_FAILED";
@@ -1137,6 +1141,8 @@ esp_err_t WiFiGenericClass::_eventCallback(arduino_event_t *event)
11371141
gw[0], gw[1], gw[2], gw[3]);
11381142
#endif
11391143
setStatusBits(ETH_CONNECTED_BIT | ETH_HAS_IP_BIT);
1144+
} else if(event->event_id == ARDUINO_EVENT_ETH_LOST_IP) {
1145+
clearStatusBits(ETH_HAS_IP_BIT);
11401146

11411147
} else if(event->event_id == ARDUINO_EVENT_WIFI_STA_GOT_IP6) {
11421148
setStatusBits(STA_CONNECTED_BIT | STA_HAS_IP6_BIT);

libraries/WiFi/src/WiFiGeneric.h

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ typedef enum {
5959
ARDUINO_EVENT_ETH_CONNECTED,
6060
ARDUINO_EVENT_ETH_DISCONNECTED,
6161
ARDUINO_EVENT_ETH_GOT_IP,
62+
ARDUINO_EVENT_ETH_LOST_IP,
6263
ARDUINO_EVENT_ETH_GOT_IP6,
6364
ARDUINO_EVENT_WPS_ER_SUCCESS,
6465
ARDUINO_EVENT_WPS_ER_FAILED,

0 commit comments

Comments
 (0)