Skip to content

Commit 5157c4b

Browse files
committed
init
0 parents  commit 5157c4b

27 files changed

+4048
-0
lines changed

LICENSE

Lines changed: 504 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
EthernetESP32 is an alternative to the ESP32 platform's bundled Ethernet library with additional support for ENC28J60.
2+
3+
EthernetESP32 library has the legacy Arduino Ethernet API as the classic Arduino Ethernet library.
4+
5+
EthernetESP32 requires ESP32 platform version 3 or higher.
6+
7+
8+
## SPI modules
9+
10+
EthernetESP32 supports W5500, ENC28J60, DM9051 and KSZ8851SNL SPI Ethernet modules.
11+
12+
### Wiring
13+
14+
Best option is to wire the SPI modules to default SPI pins of your ESP32 module or development board. The default pins are specified in `pins_arduino.h` for the ESP32 variant in variants folder of the platform. For classic ESP32 without pin remapping the default pins are MOSI 23, MISO 19, SCK 18 and CS 5.
15+
16+
If you use other pins for the SPI object, add `Setup.begin(sck, miso, mosi);` in `setup()`, before `Ethernet.begin`.
17+
18+
It is possible to use other CS pin. The library supports INT pin of the modules for faster processing of received packets. Hardware reset is supported if the reset pin of the SPI module is wired to an IO pin.
19+
20+
### In sketch
21+
22+
The driver for specific Ethernet SPI module must be instanced as global in sketch and provide to Ethernet object with `Ethernet.init(driver);` before `Ethernet.begin`.
23+
24+
Example for default pins:
25+
26+
```
27+
#include <EthernetESP32.h>
28+
29+
W5500Driver driver;
30+
31+
void setup() {
32+
33+
Ethernet.init(driver);
34+
35+
Ethernet.begin(); // begin with default MAC address and DHCP IP
36+
37+
```
38+
39+
There are 4 drivers for SPI modules: W5500Driver, ENC28J60Driver, DM9051Driver and KSZ8851SNLDriver.
40+
41+
The constructors for the drivers have 3 optional parameters CS pin, INT pin and reset pin. Default CS pin is the default SPI SS pin. Default value for the INT pin and reset pin is -1, which tells the library to not to use interrupt and hardware reset.
42+
43+
Examples:
44+
45+
```
46+
W5500Driver driver(4); // CS pin is 4
47+
W5500Driver driver(4, 26); // CS pin is 4, INT pin is 26
48+
W5500Driver driver(4, 26, 27); // CS pin is 4, INT pin is 26, reset pin is 27
49+
W5500Driver driver(SS, -1, 27); // CS pin is default, INT is not used and reset pin is 27
50+
```
51+
52+
To use other SPIClass instance set it on driver with `setSPI`. To change the SPI frequency use `setSpiFreq`. Default frequency is 20 MHz.
53+
54+
Example with all options specified (using HSPI as SPI1 on FSPI pins):
55+
56+
```
57+
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF };
58+
W5500Driver driver(5, 26, 27);
59+
SPIClass SPI1(HSPI);
60+
61+
void setup() {
62+
63+
SPI1.begin(18, 19, 23);
64+
65+
driver.setSPI(SPI1);
66+
driver.setSpiFreq(10);
67+
68+
Ethernet.init(driver);
69+
70+
Ethernet.begin(mac);
71+
72+
```
73+
74+
## PHY modules
75+
76+
The EthernetESP32 library supports PHY modules with ESP32 Ethernet peripheral. EMAC is available only on classic ESP32. Supported PHY modules are: LAN8720, TLK110, RTL8201, DP83848 and KSZ80XX series.
77+
78+
### Wiring
79+
80+
Dedicated pins are RMII_TX_EN 21, RMII_TX0 19, RMII_TX1 22, RMII_RX0 25, RMII_RX1_EN 26, RMII_CRS_DV 27. MDC and MDIO pins can be any pin. The default in the library is MDC 23 and MDIO 18. Additionally power pin can be specified for hardware reset.
81+
82+
Clock pin may be input or output. IO 0 is default. For output clock pins 16 and 17 can be used too.
83+
84+
### In sketch
85+
86+
There is one driver class EMACDriver for PHY modules on the Ethernet peripheral. The specific PHY module is specified as first parameter of the constructor. The constants are `ETH_PHY_LAN8720`, `ETH_PHY_TLK110`, `ETH_PHY_RTL8201`, `ETH_PHY_DP83848`,
87+
and `ETH_PHY_KSZ80XX`.
88+
89+
The driver must be instanced as global in sketch and provide to Ethernet object with `Ethernet.init(driver);` before `Ethernet.begin`.
90+
91+
Example for default pins:
92+
93+
```
94+
#include <EthernetESP32.h>
95+
96+
EMACDriver driver(ETH_PHY_LAN8720);
97+
98+
void setup() {
99+
100+
Ethernet.init(driver);
101+
102+
Ethernet.begin();
103+
104+
```
105+
106+
The EMACDriver has optional parameters:
107+
108+
* **mdcPin** - default is 23
109+
* **mdioPin** - default is 18
110+
* **powerPin** - default is -1 (pin not used)
111+
* **clockMode** - values from enum emac_rmii_clock_gpio_t, default is `EMAC_APPL_CLK_OUT_GPIO`,
112+
* **clockMode** - values from enum emac_rmii_clock_mode_t, default is `EMAC_CLK_EXT_IN`
113+
114+
The values for clockPin and clockMode are [enums from ESP-IDF](https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/network/esp_eth.html?highlight=rmii#id14).
115+
116+
## The Ethernet object
117+
118+
### MAC address
119+
120+
By default the library uses a MAC address from the range of MAC address assigned to your ESP32 module. You can specify custom MAC address as (first) parameter of `Ethernet.begin` as in the Arduino Ethernet library.
121+
122+
### Static IP configuration
123+
124+
As in the Arduino Ethernet library static IP configuration is specified with `Ethernet.begin(ip, dns, gateway, netmask)` or `Ethernet.begin(mac, ip, dns, gateway, netmask)`.
125+
126+
## Implementation details
127+
128+
The EthernetESP32 library wraps drivers provided by the ESP-IDF framework. The ENC29J60 driver included in the library is from ESP-IDF examples.
129+
130+
The Ethernet object inherits from NetworkInterface class from the Network library of the ESP32 Arduino platform. It adds Arduino Ethernet API methods not implemented in NetworkInterface.
131+
132+
The integration of the IDF drivers with NetworkInterface is code adopted from ETH.cpp of the bundled Ethernet library.
133+
134+
EthernetClient, EthernetServer and EthernetUDP are macros aliasing NetworkClient, NetworkServer and NetworkUDP from the Network library (as are WiFiCllent, WiFiServer and WiFiUDP in the WiFi library).
135+
136+
Network modules tested with the library are SPI modules W5500 and ENC28J60 and a LAN8720 PHY module.
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
/**
2+
* LegacyEthernetTest from NetApiHelpers library
3+
* modified for the EthernetESP32 library.
4+
*/
5+
6+
#include <EthernetESP32.h>
7+
#include <MacAddress.h>
8+
9+
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF };
10+
11+
W5500Driver driver;
12+
//ENC28J60Driver driver;
13+
//EMACDriver driver(ETH_PHY_LAN8720);
14+
15+
void setup() {
16+
17+
Serial.begin(115200);
18+
delay(500);
19+
while (!Serial);
20+
Serial.println("START");
21+
Serial.println();
22+
23+
Ethernet.init(driver);
24+
25+
Serial.println("Attempting to connect with DHCP ...");
26+
testEthernet(true);
27+
28+
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
29+
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
30+
while (true) {
31+
delay(1); // do nothing, no point running without Ethernet hardware
32+
}
33+
}
34+
Serial.println("Checking link ...");
35+
if (Ethernet.linkStatus() == LinkOFF) {
36+
Serial.println("\tretry...");
37+
delay(500);
38+
}
39+
switch (Ethernet.linkStatus()) {
40+
case LinkOFF:
41+
Serial.println("\tEthernet cable is not connected.");
42+
break;
43+
case LinkON:
44+
Serial.println("\tEthernet cable is connected.");
45+
break;
46+
default:
47+
Serial.println("\tLink state unknown.");
48+
break;
49+
}
50+
Serial.println();
51+
52+
IPAddress ip = Ethernet.localIP();
53+
IPAddress gw = Ethernet.gatewayIP();
54+
IPAddress mask = Ethernet.subnetMask();
55+
IPAddress dns = Ethernet.dnsServerIP();
56+
57+
const char *serverName = "www.google.com";
58+
Serial.print("Resolve IP for ");
59+
Serial.println(serverName);
60+
IPAddress resolvedIP;
61+
if (Ethernet.hostByName("www.google.com", resolvedIP) == 1) {
62+
Serial.print("\t");
63+
Serial.println(resolvedIP);
64+
} else {
65+
Serial.println("\t...ERROR");
66+
}
67+
Serial.println();
68+
69+
Ethernet.end();
70+
71+
// IPAddress ip(192, 168, 1, 177);
72+
// IPAddress gw(192, 168, 1, 1);
73+
// IPAddress dns(192, 168, 1, 1);
74+
// IPAddress mask(255, 255, 255, 0);
75+
76+
Serial.print("Configuring static IP ");
77+
ip[3] = 177;
78+
Serial.println(ip);
79+
Ethernet.begin(mac, ip, dns, gw, mask);
80+
if (Ethernet.dnsServerIP() == mask) {
81+
Serial.println("ERROR: begin() has wrong ordering of parameters");
82+
while (true) {
83+
delay(1);
84+
}
85+
}
86+
testEthernet(false);
87+
if (ip != Ethernet.localIP()) {
88+
Serial.println("ERROR: Static IP was not used.");
89+
while (true) {
90+
delay(1);
91+
}
92+
}
93+
94+
IPAddress dnsIP2(8, 8, 8, 8);
95+
Ethernet.setDnsServerIP(dnsIP2);
96+
if (Ethernet.dnsServerIP() != dnsIP2) {
97+
Serial.print("ERROR: DNS IP setter or getter error. dnsServerIP() returned ");
98+
Serial.println(Ethernet.dnsServerIP());
99+
Serial.println();
100+
}
101+
Ethernet.setDnsServerIP(dns);
102+
103+
Ethernet.end();
104+
105+
Serial.print("Configuring with automatic gateway, DNS and mask with static IP "); // <-------
106+
ip[3] = 178;
107+
Serial.println(ip);
108+
Ethernet.begin(mac, ip);
109+
testEthernet(false);
110+
if (ip != Ethernet.localIP()) {
111+
Serial.println("ERROR: Static IP was not used.");
112+
while (true) {
113+
delay(1);
114+
}
115+
}
116+
IPAddress autoIP(ip);
117+
autoIP[3] = 1;
118+
IPAddress defaultMask(255, 255, 255, 0);
119+
if (Ethernet.gatewayIP() == autoIP && Ethernet.dnsServerIP() == autoIP && Ethernet.subnetMask() == defaultMask) {
120+
Serial.println("Automatic config values are OK");
121+
} else {
122+
Serial.println("ERROR: Automatic config values are wrong");
123+
if (Ethernet.gatewayIP() != autoIP) {
124+
Serial.print("\tgateway IP Address: ");
125+
Serial.println(Ethernet.gatewayIP());
126+
}
127+
if (Ethernet.subnetMask() != defaultMask) {
128+
Serial.print("\tsubnet IP mask: ");
129+
Serial.println(Ethernet.subnetMask());
130+
}
131+
if (Ethernet.dnsServerIP() != autoIP) {
132+
Serial.print("\tDNS server: ");
133+
Serial.println(Ethernet.dnsServerIP());
134+
}
135+
}
136+
Serial.println();
137+
138+
Ethernet.end();
139+
140+
Serial.println("Attempting to connect with DHCP again ...");
141+
testEthernet(true);
142+
143+
if (ip == Ethernet.localIP()) {
144+
Serial.println("ERROR: The IP didn't change from static IP to DHCP assigned IP.");
145+
} else {
146+
Serial.println("Check on router the hostname and MAC address.");
147+
}
148+
Serial.println();
149+
150+
Serial.println("END");
151+
}
152+
153+
void loop() {
154+
delay(1);
155+
}
156+
157+
void testEthernet(bool dhcp) {
158+
159+
if (dhcp) {
160+
Ethernet.setHostname("arduino");
161+
if (!Ethernet.begin(mac)) {
162+
Serial.println("\t...ERROR");
163+
while (true) {
164+
delay(1);
165+
}
166+
} else {
167+
Serial.println("\t...success");
168+
}
169+
}
170+
Serial.println();
171+
172+
printEthernetStatus();
173+
Serial.println();
174+
175+
Serial.print("Attempt to connect to port 80 on ");
176+
Serial.println(Ethernet.gatewayIP());
177+
EthernetClient client;
178+
if (client.connect(Ethernet.gatewayIP(), 80)) {
179+
Serial.println("\t...success");
180+
} else {
181+
Serial.println("\t...ERROR");
182+
}
183+
client.stop();
184+
Serial.println();
185+
}
186+
187+
void printEthernetStatus() {
188+
189+
byte mac[6];
190+
Ethernet.MACAddress(mac);
191+
Serial.print("MAC: ");
192+
Serial.println(MacAddress(mac));
193+
if (mac[0] & 1) { // unicast bit is set
194+
Serial.println("\t is the ordering of the MAC address bytes reversed?");
195+
}
196+
197+
Serial.print("IP Address: ");
198+
Serial.println(Ethernet.localIP());
199+
200+
Serial.print("gateway IP Address: ");
201+
Serial.println(Ethernet.gatewayIP());
202+
203+
Serial.print("subnet IP mask: ");
204+
Serial.println(Ethernet.subnetMask());
205+
206+
Serial.print("DNS server: ");
207+
IPAddress dns = Ethernet.dnsServerIP();
208+
if (dns == INADDR_NONE) {
209+
Serial.println("not set");
210+
} else {
211+
Serial.println(dns);
212+
}
213+
}

0 commit comments

Comments
 (0)