1 /* Console example — Ethernet commands
2
3 This example code is in the Public Domain (or CC0 licensed, at your option.)
4
5 Unless required by applicable law or agreed to in writing, this
6 software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7 CONDITIONS OF ANY KIND, either express or implied.
8 */
9 #include <stdio.h>
10 #include <string.h>
11 #include "freertos/FreeRTOS.h"
12 #include "freertos/event_groups.h"
13 #include "esp_netif.h"
14 #include "esp_log.h"
15 #include "esp_console.h"
16 #include "esp_event.h"
17 #include "esp_eth.h"
18 #include "driver/gpio.h"
19 #include "argtable3/argtable3.h"
20 #include "iperf.h"
21 #include "sdkconfig.h"
22 #if CONFIG_ETH_USE_SPI_ETHERNET
23 #include "driver/spi_master.h"
24 #if CONFIG_EXAMPLE_USE_ENC28J60
25 #include "esp_eth_enc28j60.h"
26 #endif //CONFIG_EXAMPLE_USE_ENC28J60
27 #endif // CONFIG_ETH_USE_SPI_ETHERNET
28
29 static esp_netif_ip_info_t ip;
30 static bool started = false;
31 static EventGroupHandle_t eth_event_group;
32 static const int GOTIP_BIT = BIT0;
33 static esp_eth_handle_t eth_handle = NULL;
34 static esp_netif_t *eth_netif = NULL;
35
36 /* "ethernet" command */
37 static struct {
38 struct arg_str *control;
39 struct arg_end *end;
40 } eth_control_args;
41
eth_cmd_control(int argc,char ** argv)42 static int eth_cmd_control(int argc, char **argv)
43 {
44 int nerrors = arg_parse(argc, argv, (void **)ð_control_args);
45 if (nerrors != 0) {
46 arg_print_errors(stderr, eth_control_args.end, argv[0]);
47 return 1;
48 }
49
50 if (!strncmp(eth_control_args.control->sval[0], "info", 4)) {
51 uint8_t mac_addr[6];
52 esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
53 printf("HW ADDR: " MACSTR "\r\n", MAC2STR(mac_addr));
54 esp_netif_get_ip_info(eth_netif, &ip);
55 printf("ETHIP: " IPSTR "\r\n", IP2STR(&ip.ip));
56 printf("ETHMASK: " IPSTR "\r\n", IP2STR(&ip.netmask));
57 printf("ETHGW: " IPSTR "\r\n", IP2STR(&ip.gw));
58 }
59 return 0;
60 }
61
62 /* "iperf" command */
63
64 static struct {
65 struct arg_str *ip;
66 struct arg_lit *server;
67 struct arg_lit *udp;
68 struct arg_lit *version;
69 struct arg_int *port;
70 struct arg_int *length;
71 struct arg_int *interval;
72 struct arg_int *time;
73 struct arg_int *bw_limit;
74 struct arg_lit *abort;
75 struct arg_end *end;
76 } iperf_args;
77
eth_cmd_iperf(int argc,char ** argv)78 static int eth_cmd_iperf(int argc, char **argv)
79 {
80 int nerrors = arg_parse(argc, argv, (void **)&iperf_args);
81 iperf_cfg_t cfg;
82
83 if (nerrors != 0) {
84 arg_print_errors(stderr, iperf_args.end, argv[0]);
85 return 0;
86 }
87
88 memset(&cfg, 0, sizeof(cfg));
89
90 // ethernet iperf only support IPV4 address
91 cfg.type = IPERF_IP_TYPE_IPV4;
92
93 /* iperf -a */
94 if (iperf_args.abort->count != 0) {
95 iperf_stop();
96 return 0;
97 }
98
99 if (((iperf_args.ip->count == 0) && (iperf_args.server->count == 0)) ||
100 ((iperf_args.ip->count != 0) && (iperf_args.server->count != 0))) {
101 ESP_LOGE(__func__, "Wrong mode! ESP32 should run in client or server mode");
102 return 0;
103 }
104
105 /* iperf -s */
106 if (iperf_args.ip->count == 0) {
107 cfg.flag |= IPERF_FLAG_SERVER;
108 }
109 /* iperf -c SERVER_ADDRESS */
110 else {
111 cfg.destination_ip4 = esp_ip4addr_aton(iperf_args.ip->sval[0]);
112 cfg.flag |= IPERF_FLAG_CLIENT;
113 }
114
115 if (iperf_args.length->count == 0) {
116 cfg.len_send_buf = 0;
117 } else {
118 cfg.len_send_buf = iperf_args.length->ival[0];
119 }
120
121
122 /* acquiring for ip, could blocked here */
123 xEventGroupWaitBits(eth_event_group, GOTIP_BIT, pdFALSE, pdTRUE, portMAX_DELAY);
124
125 cfg.source_ip4 = ip.ip.addr;
126 if (cfg.source_ip4 == 0) {
127 return 0;
128 }
129
130 /* iperf -u */
131 if (iperf_args.udp->count == 0) {
132 cfg.flag |= IPERF_FLAG_TCP;
133 } else {
134 cfg.flag |= IPERF_FLAG_UDP;
135 }
136
137 /* iperf -p */
138 if (iperf_args.port->count == 0) {
139 cfg.sport = IPERF_DEFAULT_PORT;
140 cfg.dport = IPERF_DEFAULT_PORT;
141 } else {
142 if (cfg.flag & IPERF_FLAG_SERVER) {
143 cfg.sport = iperf_args.port->ival[0];
144 cfg.dport = IPERF_DEFAULT_PORT;
145 } else {
146 cfg.sport = IPERF_DEFAULT_PORT;
147 cfg.dport = iperf_args.port->ival[0];
148 }
149 }
150
151 /* iperf -i */
152 if (iperf_args.interval->count == 0) {
153 cfg.interval = IPERF_DEFAULT_INTERVAL;
154 } else {
155 cfg.interval = iperf_args.interval->ival[0];
156 if (cfg.interval <= 0) {
157 cfg.interval = IPERF_DEFAULT_INTERVAL;
158 }
159 }
160
161 /* iperf -t */
162 if (iperf_args.time->count == 0) {
163 cfg.time = IPERF_DEFAULT_TIME;
164 } else {
165 cfg.time = iperf_args.time->ival[0];
166 if (cfg.time <= cfg.interval) {
167 cfg.time = cfg.interval;
168 }
169 }
170
171 /* iperf -b */
172 if (iperf_args.bw_limit->count == 0) {
173 cfg.bw_lim = IPERF_DEFAULT_NO_BW_LIMIT;
174 } else {
175 cfg.bw_lim = iperf_args.bw_limit->ival[0];
176 if (cfg.bw_lim <= 0) {
177 cfg.bw_lim = IPERF_DEFAULT_NO_BW_LIMIT;
178 }
179 }
180
181 printf("mode=%s-%s sip=%d.%d.%d.%d:%d, dip=%d.%d.%d.%d:%d, interval=%d, time=%d\r\n",
182 cfg.flag & IPERF_FLAG_TCP ? "tcp" : "udp",
183 cfg.flag & IPERF_FLAG_SERVER ? "server" : "client",
184 cfg.source_ip4 & 0xFF, (cfg.source_ip4 >> 8) & 0xFF, (cfg.source_ip4 >> 16) & 0xFF,
185 (cfg.source_ip4 >> 24) & 0xFF, cfg.sport,
186 cfg.destination_ip4 & 0xFF, (cfg.destination_ip4 >> 8) & 0xFF,
187 (cfg.destination_ip4 >> 16) & 0xFF, (cfg.destination_ip4 >> 24) & 0xFF, cfg.dport,
188 cfg.interval, cfg.time);
189
190 iperf_start(&cfg);
191 return 0;
192 }
193
event_handler(void * arg,esp_event_base_t event_base,int32_t event_id,void * event_data)194 static void event_handler(void *arg, esp_event_base_t event_base,
195 int32_t event_id, void *event_data)
196 {
197 if (event_base == ETH_EVENT && event_id == ETHERNET_EVENT_START) {
198 started = true;
199 } else if (event_base == ETH_EVENT && event_id == ETHERNET_EVENT_STOP) {
200 xEventGroupClearBits(eth_event_group, GOTIP_BIT);
201 started = false;
202 } else if (event_base == IP_EVENT && event_id == IP_EVENT_ETH_GOT_IP) {
203 ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
204 memcpy(&ip, &event->ip_info, sizeof(ip));
205 xEventGroupSetBits(eth_event_group, GOTIP_BIT);
206 }
207 }
208
register_ethernet(void)209 void register_ethernet(void)
210 {
211 eth_event_group = xEventGroupCreate();
212 ESP_ERROR_CHECK(esp_netif_init());
213 ESP_ERROR_CHECK(esp_event_loop_create_default());
214 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
215 eth_netif = esp_netif_new(&cfg);
216
217 eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
218 eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
219 phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR;
220 phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO;
221 #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
222 mac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO;
223 mac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO;
224 esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
225 #if CONFIG_EXAMPLE_ETH_PHY_IP101
226 esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
227 #elif CONFIG_EXAMPLE_ETH_PHY_RTL8201
228 esp_eth_phy_t *phy = esp_eth_phy_new_rtl8201(&phy_config);
229 #elif CONFIG_EXAMPLE_ETH_PHY_LAN87XX
230 esp_eth_phy_t *phy = esp_eth_phy_new_lan87xx(&phy_config);
231 #elif CONFIG_EXAMPLE_ETH_PHY_DP83848
232 esp_eth_phy_t *phy = esp_eth_phy_new_dp83848(&phy_config);
233 #elif CONFIG_EXAMPLE_ETH_PHY_KSZ8041
234 esp_eth_phy_t *phy = esp_eth_phy_new_ksz8041(&phy_config);
235 #elif CONFIG_EXAMPLE_ETH_PHY_KSZ8081
236 esp_eth_phy_t *phy = esp_eth_phy_new_ksz8081(&phy_config);
237 #endif
238 #elif CONFIG_ETH_USE_SPI_ETHERNET
239 gpio_install_isr_service(0);
240 spi_device_handle_t spi_handle = NULL;
241 spi_bus_config_t buscfg = {
242 .miso_io_num = CONFIG_EXAMPLE_ETH_SPI_MISO_GPIO,
243 .mosi_io_num = CONFIG_EXAMPLE_ETH_SPI_MOSI_GPIO,
244 .sclk_io_num = CONFIG_EXAMPLE_ETH_SPI_SCLK_GPIO,
245 .quadwp_io_num = -1,
246 .quadhd_io_num = -1,
247 };
248 ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO));
249
250 #if CONFIG_EXAMPLE_USE_KSZ8851SNL
251 spi_device_interface_config_t devcfg = {
252 .mode = 0,
253 .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
254 .spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO,
255 .queue_size = 20
256 };
257 ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle));
258 /* KSZ8851SNL ethernet driver is based on spi driver */
259 eth_ksz8851snl_config_t ksz8851snl_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(spi_handle);
260 ksz8851snl_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO;
261 esp_eth_mac_t *mac = esp_eth_mac_new_ksz8851snl(&ksz8851snl_config, &mac_config);
262 esp_eth_phy_t *phy = esp_eth_phy_new_ksz8851snl(&phy_config);
263 #elif CONFIG_EXAMPLE_USE_DM9051
264 spi_device_interface_config_t devcfg = {
265 .command_bits = 1,
266 .address_bits = 7,
267 .mode = 0,
268 .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
269 .spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO,
270 .queue_size = 20
271 };
272 ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle));
273 /* dm9051 ethernet driver is based on spi driver */
274 eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle);
275 dm9051_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO;
276 esp_eth_mac_t *mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
277 esp_eth_phy_t *phy = esp_eth_phy_new_dm9051(&phy_config);
278 #elif CONFIG_EXAMPLE_USE_W5500
279 spi_device_interface_config_t devcfg = {
280 .command_bits = 16, // Actually it's the address phase in W5500 SPI frame
281 .address_bits = 8, // Actually it's the control phase in W5500 SPI frame
282 .mode = 0,
283 .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
284 .spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO,
285 .queue_size = 20
286 };
287 ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle));
288 /* w5500 ethernet driver is based on spi driver */
289 eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(spi_handle);
290 w5500_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO;
291 esp_eth_mac_t *mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
292 esp_eth_phy_t *phy = esp_eth_phy_new_w5500(&phy_config);
293 #elif CONFIG_EXAMPLE_USE_ENC28J60
294 /* ENC28J60 ethernet driver is based on spi driver */
295 spi_device_interface_config_t devcfg = {
296 .command_bits = 3,
297 .address_bits = 5,
298 .mode = 0,
299 .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
300 .spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO,
301 .queue_size = 20,
302 .cs_ena_posttrans = enc28j60_cal_spi_cs_hold_time(CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ)
303 };
304 ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle));
305
306 eth_enc28j60_config_t enc28j60_config = ETH_ENC28J60_DEFAULT_CONFIG(spi_handle);
307 enc28j60_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO;
308
309 mac_config.smi_mdc_gpio_num = -1; // ENC28J60 doesn't have SMI interface
310 mac_config.smi_mdio_gpio_num = -1;
311 esp_eth_mac_t *mac = esp_eth_mac_new_enc28j60(&enc28j60_config, &mac_config);
312
313 phy_config.autonego_timeout_ms = 0; // ENC28J60 doesn't support auto-negotiation
314 phy_config.reset_gpio_num = -1; // ENC28J60 doesn't have a pin to reset internal PHY
315 esp_eth_phy_t *phy = esp_eth_phy_new_enc28j60(&phy_config);
316 #endif
317 #endif // CONFIG_ETH_USE_SPI_ETHERNET
318 esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
319 ESP_ERROR_CHECK(esp_eth_driver_install(&config, ð_handle));
320 #if !CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
321 /* The SPI Ethernet module might doesn't have a burned factory MAC address, we cat to set it manually.
322 02:00:00 is a Locally Administered OUI range so should not be used except when testing on a LAN under your control.
323 */
324 ESP_ERROR_CHECK(esp_eth_ioctl(eth_handle, ETH_CMD_S_MAC_ADDR, (uint8_t[]) {
325 0x02, 0x00, 0x00, 0x12, 0x34, 0x56
326 }));
327 #endif
328 ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle)));
329 ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
330 ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &event_handler, NULL));
331 ESP_ERROR_CHECK(esp_eth_start(eth_handle));
332
333 #if CONFIG_EXAMPLE_USE_ENC28J60 && CONFIG_EXAMPLE_ENC28J60_DUPLEX_FULL
334 enc28j60_set_phy_duplex(phy, ETH_DUPLEX_FULL);
335 #endif
336
337 eth_control_args.control = arg_str1(NULL, NULL, "<info>", "Get info of Ethernet");
338 eth_control_args.end = arg_end(1);
339 const esp_console_cmd_t cmd = {
340 .command = "ethernet",
341 .help = "Ethernet interface IO control",
342 .hint = NULL,
343 .func = eth_cmd_control,
344 .argtable = ð_control_args
345 };
346 ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
347
348 iperf_args.ip = arg_str0("c", "client", "<ip>",
349 "run in client mode, connecting to <host>");
350 iperf_args.server = arg_lit0("s", "server", "run in server mode");
351 iperf_args.udp = arg_lit0("u", "udp", "use UDP rather than TCP");
352 iperf_args.version = arg_lit0("V", "ipv6_domain", "use IPV6 address rather than IPV4");
353 iperf_args.port = arg_int0("p", "port", "<port>",
354 "server port to listen on/connect to");
355 iperf_args.length = arg_int0("l", "len", "<length>", "set read/write buffer size");
356 iperf_args.interval = arg_int0("i", "interval", "<interval>",
357 "seconds between periodic bandwidth reports");
358 iperf_args.time = arg_int0("t", "time", "<time>", "time in seconds to transmit for (default 10 secs)");
359 iperf_args.bw_limit = arg_int0("b", "bandwidth", "<bandwidth>", "bandwidth to send at in Mbits/sec");
360 iperf_args.abort = arg_lit0("a", "abort", "abort running iperf");
361 iperf_args.end = arg_end(1);
362 const esp_console_cmd_t iperf_cmd = {
363 .command = "iperf",
364 .help = "iperf command",
365 .hint = NULL,
366 .func = ð_cmd_iperf,
367 .argtable = &iperf_args
368 };
369 ESP_ERROR_CHECK(esp_console_cmd_register(&iperf_cmd));
370 }
371