-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathEthernetModuleBaseTest.ino
More file actions
134 lines (117 loc) · 3.3 KB
/
EthernetModuleBaseTest.ino
File metadata and controls
134 lines (117 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) 2025 by GWENDESIGN. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
// https://github.com/m5stack/M5GFX
// https://github.com/m5stack/M5Unified
//
// Adapted from:
// https://github.com/espressif/arduino-esp32/blob/master/libraries/Ethernet/examples/ETH_W5500_Arduino_SPI/ETH_W5500_Arduino_SPI.ino
//
// Note: ETH GPIOs are for M5Stack Module 13.2 LAN stacked to M5Tab5
// ETH1 GPIOs are for M5Stack Base LAN stacked to M5Tab5
#include <M5Unified.h>
#include <ETH.h>
#include <SPI.h>
// Set this to 1 to enable dual Ethernet support
#define USE_TWO_ETH_PORTS 0
#undef ETH_PHY_TYPE
#ifndef ETH_PHY_CS
#define ETH_PHY_TYPE ETH_PHY_W5500
#define ETH_PHY_ADDR 1
#define ETH_PHY_CS 4 // M5Stack Bus: 5
#define ETH_PHY_IRQ 16 // M5Stack Bus: 35
#define ETH_PHY_RST 35 // M5Stack Bus: 0
#endif
#define ETH_SPI_SCK 5 // M5Stack Bus: 18
#define ETH_SPI_MISO 19 // M5Stack Bus: 19
#define ETH_SPI_MOSI 18 // M5Stack Bus: 23
#if USE_TWO_ETH_PORTS
// Second port on shared SPI bus
#ifndef ETH1_PHY_CS
#define ETH1_PHY_TYPE ETH_PHY_W5500
#define ETH1_PHY_ADDR 1
#define ETH1_PHY_CS 52 // M5Stack Bus 26
#define ETH1_PHY_IRQ 51 // M5Stack Bus 34
#define ETH1_PHY_RST 48 // M5Stack Bus 13
#endif
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");
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));
Serial.println(ETH);
#if USE_TWO_ETH_PORTS
Serial.println(ETH1);
#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);
NetworkClient 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()
{
auto cfg = M5.config();
cfg.serial_baudrate = 115200;
M5.begin(cfg);
delay(3000);
Serial.printf("*** Starting...\n");
M5.Display.setTextSize(4);
M5.Display.println(" M5Tab5 Ethernet Test v1.1");
delay(1000);
Network.onEvent(onEvent);
ETH.begin(ETH_PHY_TYPE, ETH_PHY_ADDR, ETH_PHY_CS, ETH_PHY_IRQ, ETH_PHY_RST, SPI3_HOST, ETH_SPI_SCK, ETH_SPI_MISO, ETH_SPI_MOSI);
#if USE_TWO_ETH_PORTS
ETH1.begin(ETH1_PHY_TYPE, ETH1_PHY_ADDR, ETH1_PHY_CS, ETH1_PHY_IRQ, ETH1_PHY_RST, SPI3_HOST);
#endif
}
void loop()
{
if(eth_connected)
{
testClient("google.com", 80);
}
delay(10000);
}