Skip to content

Commit 3af1710

Browse files
committedAug 1, 2017
Add initial support for Ethernet and examples
1 parent 5c1b10f commit 3af1710

File tree

4 files changed

+499
-0
lines changed

4 files changed

+499
-0
lines changed
 
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
This sketch shows the Ethernet event usage
3+
4+
*/
5+
6+
#include <ETH.h>
7+
8+
static bool eth_connected = false;
9+
10+
void WiFiEvent(WiFiEvent_t event)
11+
{
12+
switch (event) {
13+
case SYSTEM_EVENT_ETH_START:
14+
Serial.println("ETH Started");
15+
//set eth hostname here
16+
ETH.setHostname("esp32-ethernet");
17+
break;
18+
case SYSTEM_EVENT_ETH_CONNECTED:
19+
Serial.println("ETH Connected");
20+
break;
21+
case SYSTEM_EVENT_ETH_GOT_IP:
22+
Serial.print("ETH MAC: ");
23+
Serial.print(ETH.macAddress());
24+
Serial.print(", IPv4: ");
25+
Serial.print(ETH.localIP());
26+
if (ETH.fullDuplex()) {
27+
Serial.print(", FULL_DUPLEX");
28+
}
29+
Serial.print(", ");
30+
Serial.print(ETH.linkSpeed());
31+
Serial.println("Mbps");
32+
eth_connected = true;
33+
break;
34+
case SYSTEM_EVENT_ETH_DISCONNECTED:
35+
Serial.println("ETH Disconnected");
36+
eth_connected = false;
37+
break;
38+
case SYSTEM_EVENT_ETH_STOP:
39+
Serial.println("ETH Stopped");
40+
eth_connected = false;
41+
break;
42+
default:
43+
break;
44+
}
45+
}
46+
47+
void testClient(const char * host, uint16_t port)
48+
{
49+
Serial.print("\nconnecting to ");
50+
Serial.println(host);
51+
52+
WiFiClient client;
53+
if (!client.connect(host, port)) {
54+
Serial.println("connection failed");
55+
return;
56+
}
57+
client.printf("GET / HTTP/1.1\r\nHost: %s\r\n\r\n", host);
58+
while (client.connected() && !client.available());
59+
while (client.available()) {
60+
Serial.write(client.read());
61+
}
62+
63+
Serial.println("closing connection\n");
64+
client.stop();
65+
}
66+
67+
void setup()
68+
{
69+
Serial.begin(115200);
70+
WiFi.onEvent(WiFiEvent);
71+
ETH.begin();
72+
}
73+
74+
75+
void loop()
76+
{
77+
if (eth_connected) {
78+
testClient("google.com", 80);
79+
}
80+
delay(10000);
81+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
This sketch shows the Ethernet event usage
3+
4+
*/
5+
6+
#include <ETH.h>
7+
8+
#define ETH_ADDR 31
9+
#define ETH_POWER_PIN 17
10+
#define ETH_MDC_PIN 23
11+
#define ETH_MDIO_PIN 18
12+
#define ETH_TYPE ETH_PHY_TLK110
13+
14+
static bool eth_connected = false;
15+
16+
void WiFiEvent(WiFiEvent_t event)
17+
{
18+
switch (event) {
19+
case SYSTEM_EVENT_ETH_START:
20+
Serial.println("ETH Started");
21+
//set eth hostname here
22+
ETH.setHostname("esp32-ethernet");
23+
break;
24+
case SYSTEM_EVENT_ETH_CONNECTED:
25+
Serial.println("ETH Connected");
26+
break;
27+
case SYSTEM_EVENT_ETH_GOT_IP:
28+
Serial.print("ETH MAC: ");
29+
Serial.print(ETH.macAddress());
30+
Serial.print(", IPv4: ");
31+
Serial.print(ETH.localIP());
32+
if (ETH.fullDuplex()) {
33+
Serial.print(", FULL_DUPLEX");
34+
}
35+
Serial.print(", ");
36+
Serial.print(ETH.linkSpeed());
37+
Serial.println("Mbps");
38+
eth_connected = true;
39+
break;
40+
case SYSTEM_EVENT_ETH_DISCONNECTED:
41+
Serial.println("ETH Disconnected");
42+
eth_connected = false;
43+
break;
44+
case SYSTEM_EVENT_ETH_STOP:
45+
Serial.println("ETH Stopped");
46+
eth_connected = false;
47+
break;
48+
default:
49+
break;
50+
}
51+
}
52+
53+
void testClient(const char * host, uint16_t port)
54+
{
55+
Serial.print("\nconnecting to ");
56+
Serial.println(host);
57+
58+
WiFiClient client;
59+
if (!client.connect(host, port)) {
60+
Serial.println("connection failed");
61+
return;
62+
}
63+
client.printf("GET / HTTP/1.1\r\nHost: %s\r\n\r\n", host);
64+
while (client.connected() && !client.available());
65+
while (client.available()) {
66+
Serial.write(client.read());
67+
}
68+
69+
Serial.println("closing connection\n");
70+
client.stop();
71+
}
72+
73+
void setup()
74+
{
75+
Serial.begin(115200);
76+
WiFi.onEvent(WiFiEvent);
77+
ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE);
78+
}
79+
80+
81+
void loop()
82+
{
83+
if (eth_connected) {
84+
testClient("google.com", 80);
85+
}
86+
delay(10000);
87+
}

‎libraries/WiFi/src/ETH.cpp

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
/*
2+
ETH.h - espre ETH PHY support.
3+
Based on WiFi.h from Ardiono WiFi shield library.
4+
Copyright (c) 2011-2014 Arduino. All right reserved.
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#include "ETH.h"
22+
#include "eth_phy/phy.h"
23+
#include "eth_phy/phy_tlk110.h"
24+
#include "eth_phy/phy_lan8720.h"
25+
#include "lwip/err.h"
26+
#include "lwip/dns.h"
27+
28+
extern void tcpipInit();
29+
30+
static int _eth_phy_mdc_pin = -1;
31+
static int _eth_phy_mdio_pin = -1;
32+
static int _eth_phy_power_pin = -1;
33+
static eth_phy_power_enable_func _eth_phy_power_enable_orig = NULL;
34+
35+
static void _eth_phy_config_gpio(void)
36+
{
37+
if(_eth_phy_mdc_pin < 0 || _eth_phy_mdio_pin < 0){
38+
log_e("MDC and MDIO pins are not configured!");
39+
return;
40+
}
41+
phy_rmii_configure_data_interface_pins();
42+
phy_rmii_smi_configure_pins(_eth_phy_mdc_pin, _eth_phy_mdio_pin);
43+
}
44+
45+
static void _eth_phy_power_enable(bool enable)
46+
{
47+
pinMode(_eth_phy_power_pin, OUTPUT);
48+
digitalWrite(_eth_phy_power_pin, enable);
49+
delay(1);
50+
}
51+
52+
ETHClass::ETHClass():initialized(false),started(false),staticIP(false)
53+
{
54+
}
55+
56+
ETHClass::~ETHClass()
57+
{}
58+
59+
bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_type_t type)
60+
{
61+
esp_err_t err;
62+
if(initialized){
63+
err = esp_eth_enable();
64+
if(err){
65+
log_e("esp_eth_enable error: %d", err);
66+
return false;
67+
}
68+
started = true;
69+
return true;
70+
}
71+
_eth_phy_mdc_pin = mdc;
72+
_eth_phy_mdio_pin = mdio;
73+
_eth_phy_power_pin = power;
74+
75+
if(type == ETH_PHY_LAN8720){
76+
eth_config_t config = phy_lan8720_default_ethernet_config;
77+
memcpy(&eth_config, &config, sizeof(eth_config_t));
78+
} else if(type == ETH_PHY_TLK110){
79+
eth_config_t config = phy_tlk110_default_ethernet_config;
80+
memcpy(&eth_config, &config, sizeof(eth_config_t));
81+
} else {
82+
log_e("Bad ETH_PHY type: %u", (uint8_t)type);
83+
return false;
84+
}
85+
86+
eth_config.phy_addr = (eth_phy_base_t)phy_addr;
87+
eth_config.gpio_config = _eth_phy_config_gpio;
88+
eth_config.tcpip_input = tcpip_adapter_eth_input;
89+
if(_eth_phy_power_pin >= 0){
90+
_eth_phy_power_enable_orig = eth_config.phy_power_enable;
91+
eth_config.phy_power_enable = _eth_phy_power_enable;
92+
}
93+
94+
tcpipInit();
95+
err = esp_eth_init(&eth_config);
96+
if(!err){
97+
initialized = true;
98+
err = esp_eth_enable();
99+
if(err){
100+
log_e("esp_eth_enable error: %d", err);
101+
} else {
102+
started = true;
103+
return true;
104+
}
105+
} else {
106+
log_e("esp_eth_init error: %d", err);
107+
}
108+
return false;
109+
}
110+
111+
/*
112+
bool ETHClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2)
113+
{
114+
if(!initialized){
115+
return false;
116+
}
117+
118+
tcpip_adapter_ip_info_t info;
119+
info.ip.addr = static_cast<uint32_t>(local_ip);
120+
info.gw.addr = static_cast<uint32_t>(gateway);
121+
info.netmask.addr = static_cast<uint32_t>(subnet);
122+
123+
if(!staticIP){
124+
tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH);
125+
}
126+
if(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_ETH, &info) == ESP_OK) {
127+
staticIP = true;
128+
} else {
129+
return false;
130+
}
131+
ip_addr_t d;
132+
133+
if(dns1 != (uint32_t)0x00000000) {
134+
// Set DNS1-Server
135+
d.u_addr.ip4.addr = static_cast<uint32_t>(dns1);
136+
dns_setserver(0, &d);
137+
}
138+
139+
if(dns2 != (uint32_t)0x00000000) {
140+
// Set DNS2-Server
141+
d.u_addr.ip4.addr = static_cast<uint32_t>(dns2);
142+
dns_setserver(1, &d);
143+
}
144+
145+
return true;
146+
}
147+
*/
148+
149+
IPAddress ETHClass::localIP()
150+
{
151+
tcpip_adapter_ip_info_t ip;
152+
if(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip)){
153+
return IPAddress();
154+
}
155+
return IPAddress(ip.ip.addr);
156+
}
157+
158+
IPAddress ETHClass::subnetMask()
159+
{
160+
tcpip_adapter_ip_info_t ip;
161+
if(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip)){
162+
return IPAddress();
163+
}
164+
return IPAddress(ip.netmask.addr);
165+
}
166+
167+
IPAddress ETHClass::gatewayIP()
168+
{
169+
tcpip_adapter_ip_info_t ip;
170+
if(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip)){
171+
return IPAddress();
172+
}
173+
return IPAddress(ip.gw.addr);
174+
}
175+
176+
IPAddress ETHClass::dnsIP(uint8_t dns_no)
177+
{
178+
ip_addr_t dns_ip = dns_getserver(dns_no);
179+
return IPAddress(dns_ip.u_addr.ip4.addr);
180+
}
181+
182+
const char * ETHClass::getHostname()
183+
{
184+
const char * hostname;
185+
if(tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_ETH, &hostname)){
186+
return NULL;
187+
}
188+
return hostname;
189+
}
190+
191+
bool ETHClass::setHostname(const char * hostname)
192+
{
193+
return tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_ETH, hostname) == 0;
194+
}
195+
196+
bool ETHClass::fullDuplex()
197+
{
198+
return eth_config.phy_get_duplex_mode();
199+
}
200+
201+
bool ETHClass::linkUp()
202+
{
203+
return eth_config.phy_check_link();
204+
}
205+
206+
uint8_t ETHClass::linkSpeed()
207+
{
208+
return eth_config.phy_get_speed_mode()?100:10;
209+
}
210+
211+
bool ETHClass::enableIpV6()
212+
{
213+
return tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_ETH) == 0;
214+
}
215+
216+
IPv6Address ETHClass::localIPv6()
217+
{
218+
static ip6_addr_t addr;
219+
if(tcpip_adapter_get_ip6_linklocal(TCPIP_ADAPTER_IF_ETH, &addr)){
220+
return IPv6Address();
221+
}
222+
return IPv6Address(addr.addr);
223+
}
224+
225+
uint8_t * macAddress(uint8_t* mac)
226+
{
227+
if(!mac){
228+
return NULL;
229+
}
230+
esp_eth_get_mac(mac);
231+
return mac;
232+
}
233+
234+
String ETHClass::macAddress(void)
235+
{
236+
uint8_t mac[6];
237+
char macStr[18] = { 0 };
238+
esp_eth_get_mac(mac);
239+
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
240+
return String(macStr);
241+
}
242+
243+
ETHClass ETH;

‎libraries/WiFi/src/ETH.h

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
ETH.h - espre ETH PHY support.
3+
Based on WiFi.h from Ardiono WiFi shield library.
4+
Copyright (c) 2011-2014 Arduino. All right reserved.
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#ifndef _ETH_H_
22+
#define _ETH_H_
23+
24+
#include "WiFi.h"
25+
#include "esp_eth.h"
26+
27+
#ifndef ETH_PHY_ADDR
28+
#define ETH_PHY_ADDR 0
29+
#endif
30+
31+
#ifndef ETH_PHY_TYPE
32+
#define ETH_PHY_TYPE ETH_PHY_LAN8720
33+
#endif
34+
35+
#ifndef ETH_PHY_POWER
36+
#define ETH_PHY_POWER -1
37+
#endif
38+
39+
#ifndef ETH_PHY_MDC
40+
#define ETH_PHY_MDC 23
41+
#endif
42+
43+
#ifndef ETH_PHY_MDIO
44+
#define ETH_PHY_MDIO 18
45+
#endif
46+
47+
typedef enum { ETH_PHY_LAN8720, ETH_PHY_TLK110, ETH_PHY_MAX} eth_phy_type_t;
48+
49+
class ETHClass {
50+
private:
51+
bool initialized;
52+
bool started;
53+
bool staticIP;
54+
eth_config_t eth_config;
55+
public:
56+
ETHClass();
57+
~ETHClass();
58+
59+
bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO, eth_phy_type_t type=ETH_PHY_TYPE);
60+
61+
// NOT WORKING YET!
62+
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000);
63+
64+
const char * getHostname();
65+
bool setHostname(const char * hostname);
66+
67+
bool fullDuplex();
68+
bool linkUp();
69+
uint8_t linkSpeed();
70+
71+
bool enableIpV6();
72+
IPv6Address localIPv6();
73+
74+
IPAddress localIP();
75+
IPAddress subnetMask();
76+
IPAddress gatewayIP();
77+
IPAddress dnsIP(uint8_t dns_no = 0);
78+
79+
uint8_t * macAddress(uint8_t* mac);
80+
String macAddress();
81+
82+
friend class WiFiClient;
83+
friend class WiFiServer;
84+
};
85+
86+
extern ETHClass ETH;
87+
88+
#endif /* _ETH_H_ */

0 commit comments

Comments
 (0)
Please sign in to comment.