1 /* Ethernet Basic Example
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/task.h"
13 #include "esp_netif.h"
14 #include "esp_eth.h"
15 #include "esp_event.h"
16 #include "esp_log.h"
17 #include "driver/gpio.h"
18 #include "sdkconfig.h"
19 #if CONFIG_ETH_USE_SPI_ETHERNET
20 #include "driver/spi_master.h"
21 #endif // CONFIG_ETH_USE_SPI_ETHERNET
22
23 static const char *TAG = "eth_example";
24
25 #if CONFIG_EXAMPLE_USE_SPI_ETHERNET
26 #define INIT_SPI_ETH_MODULE_CONFIG(eth_module_config, num) \
27 do { \
28 eth_module_config[num].spi_cs_gpio = CONFIG_EXAMPLE_ETH_SPI_CS ##num## _GPIO; \
29 eth_module_config[num].int_gpio = CONFIG_EXAMPLE_ETH_SPI_INT ##num## _GPIO; \
30 eth_module_config[num].phy_reset_gpio = CONFIG_EXAMPLE_ETH_SPI_PHY_RST ##num## _GPIO; \
31 eth_module_config[num].phy_addr = CONFIG_EXAMPLE_ETH_SPI_PHY_ADDR ##num; \
32 } while(0)
33
34 typedef struct {
35 uint8_t spi_cs_gpio;
36 uint8_t int_gpio;
37 int8_t phy_reset_gpio;
38 uint8_t phy_addr;
39 }spi_eth_module_config_t;
40 #endif
41
42 /** Event handler for Ethernet events */
eth_event_handler(void * arg,esp_event_base_t event_base,int32_t event_id,void * event_data)43 static void eth_event_handler(void *arg, esp_event_base_t event_base,
44 int32_t event_id, void *event_data)
45 {
46 uint8_t mac_addr[6] = {0};
47 /* we can get the ethernet driver handle from event data */
48 esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;
49
50 switch (event_id) {
51 case ETHERNET_EVENT_CONNECTED:
52 esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
53 ESP_LOGI(TAG, "Ethernet Link Up");
54 ESP_LOGI(TAG, "Ethernet HW Addr %02x:%02x:%02x:%02x:%02x:%02x",
55 mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
56 break;
57 case ETHERNET_EVENT_DISCONNECTED:
58 ESP_LOGI(TAG, "Ethernet Link Down");
59 break;
60 case ETHERNET_EVENT_START:
61 ESP_LOGI(TAG, "Ethernet Started");
62 break;
63 case ETHERNET_EVENT_STOP:
64 ESP_LOGI(TAG, "Ethernet Stopped");
65 break;
66 default:
67 break;
68 }
69 }
70
71 /** Event handler for IP_EVENT_ETH_GOT_IP */
got_ip_event_handler(void * arg,esp_event_base_t event_base,int32_t event_id,void * event_data)72 static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
73 int32_t event_id, void *event_data)
74 {
75 ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
76 const esp_netif_ip_info_t *ip_info = &event->ip_info;
77
78 ESP_LOGI(TAG, "Ethernet Got IP Address");
79 ESP_LOGI(TAG, "~~~~~~~~~~~");
80 ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip));
81 ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
82 ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
83 ESP_LOGI(TAG, "~~~~~~~~~~~");
84 }
85
app_main(void)86 void app_main(void)
87 {
88 // Initialize TCP/IP network interface (should be called only once in application)
89 ESP_ERROR_CHECK(esp_netif_init());
90 // Create default event loop that running in background
91 ESP_ERROR_CHECK(esp_event_loop_create_default());
92
93 #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
94 // Create new default instance of esp-netif for Ethernet
95 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
96 esp_netif_t *eth_netif = esp_netif_new(&cfg);
97
98 // Init MAC and PHY configs to default
99 eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
100 eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
101
102 phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR;
103 phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO;
104 mac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO;
105 mac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO;
106 esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
107 #if CONFIG_EXAMPLE_ETH_PHY_IP101
108 esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
109 #elif CONFIG_EXAMPLE_ETH_PHY_RTL8201
110 esp_eth_phy_t *phy = esp_eth_phy_new_rtl8201(&phy_config);
111 #elif CONFIG_EXAMPLE_ETH_PHY_LAN87XX
112 esp_eth_phy_t *phy = esp_eth_phy_new_lan87xx(&phy_config);
113 #elif CONFIG_EXAMPLE_ETH_PHY_DP83848
114 esp_eth_phy_t *phy = esp_eth_phy_new_dp83848(&phy_config);
115 #elif CONFIG_EXAMPLE_ETH_PHY_KSZ8041
116 esp_eth_phy_t *phy = esp_eth_phy_new_ksz8041(&phy_config);
117 #elif CONFIG_EXAMPLE_ETH_PHY_KSZ8081
118 esp_eth_phy_t *phy = esp_eth_phy_new_ksz8081(&phy_config);
119 #endif
120 esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
121 esp_eth_handle_t eth_handle = NULL;
122 ESP_ERROR_CHECK(esp_eth_driver_install(&config, ð_handle));
123 /* attach Ethernet driver to TCP/IP stack */
124 ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle)));
125 #endif //CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
126
127 #if CONFIG_EXAMPLE_USE_SPI_ETHERNET
128 // Create instance(s) of esp-netif for SPI Ethernet(s)
129 esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
130 esp_netif_config_t cfg_spi = {
131 .base = &esp_netif_config,
132 .stack = ESP_NETIF_NETSTACK_DEFAULT_ETH
133 };
134 esp_netif_t *eth_netif_spi[CONFIG_EXAMPLE_SPI_ETHERNETS_NUM] = { NULL };
135 char if_key_str[10];
136 char if_desc_str[10];
137 char num_str[3];
138 for (int i = 0; i < CONFIG_EXAMPLE_SPI_ETHERNETS_NUM; i++) {
139 itoa(i, num_str, 10);
140 strcat(strcpy(if_key_str, "ETH_SPI_"), num_str);
141 strcat(strcpy(if_desc_str, "eth"), num_str);
142 esp_netif_config.if_key = if_key_str;
143 esp_netif_config.if_desc = if_desc_str;
144 esp_netif_config.route_prio = 30 - i;
145 eth_netif_spi[i] = esp_netif_new(&cfg_spi);
146 }
147
148 // Init MAC and PHY configs to default
149 eth_mac_config_t mac_config_spi = ETH_MAC_DEFAULT_CONFIG();
150 eth_phy_config_t phy_config_spi = ETH_PHY_DEFAULT_CONFIG();
151
152 // Install GPIO ISR handler to be able to service SPI Eth modlues interrupts
153 gpio_install_isr_service(0);
154
155 // Init SPI bus
156 spi_device_handle_t spi_handle[CONFIG_EXAMPLE_SPI_ETHERNETS_NUM] = { NULL };
157 spi_bus_config_t buscfg = {
158 .miso_io_num = CONFIG_EXAMPLE_ETH_SPI_MISO_GPIO,
159 .mosi_io_num = CONFIG_EXAMPLE_ETH_SPI_MOSI_GPIO,
160 .sclk_io_num = CONFIG_EXAMPLE_ETH_SPI_SCLK_GPIO,
161 .quadwp_io_num = -1,
162 .quadhd_io_num = -1,
163 };
164 ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO));
165
166 // Init specific SPI Ethernet module configuration from Kconfig (CS GPIO, Interrupt GPIO, etc.)
167 spi_eth_module_config_t spi_eth_module_config[CONFIG_EXAMPLE_SPI_ETHERNETS_NUM];
168 INIT_SPI_ETH_MODULE_CONFIG(spi_eth_module_config, 0);
169 #if CONFIG_EXAMPLE_SPI_ETHERNETS_NUM > 1
170 INIT_SPI_ETH_MODULE_CONFIG(spi_eth_module_config, 1);
171 #endif
172
173 // Configure SPI interface and Ethernet driver for specific SPI module
174 esp_eth_mac_t *mac_spi[CONFIG_EXAMPLE_SPI_ETHERNETS_NUM];
175 esp_eth_phy_t *phy_spi[CONFIG_EXAMPLE_SPI_ETHERNETS_NUM];
176 esp_eth_handle_t eth_handle_spi[CONFIG_EXAMPLE_SPI_ETHERNETS_NUM] = { NULL };
177 #if CONFIG_EXAMPLE_USE_KSZ8851SNL
178 spi_device_interface_config_t devcfg = {
179 .mode = 0,
180 .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
181 .queue_size = 20
182 };
183
184 for (int i = 0; i < CONFIG_EXAMPLE_SPI_ETHERNETS_NUM; i++) {
185 // Set SPI module Chip Select GPIO
186 devcfg.spics_io_num = spi_eth_module_config[i].spi_cs_gpio;
187
188 ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle[i]));
189 // KSZ8851SNL ethernet driver is based on spi driver
190 eth_ksz8851snl_config_t ksz8851snl_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(spi_handle[i]);
191
192 // Set remaining GPIO numbers and configuration used by the SPI module
193 ksz8851snl_config.int_gpio_num = spi_eth_module_config[i].int_gpio;
194 phy_config_spi.phy_addr = spi_eth_module_config[i].phy_addr;
195 phy_config_spi.reset_gpio_num = spi_eth_module_config[i].phy_reset_gpio;
196
197 mac_spi[i] = esp_eth_mac_new_ksz8851snl(&ksz8851snl_config, &mac_config_spi);
198 phy_spi[i] = esp_eth_phy_new_ksz8851snl(&phy_config_spi);
199 }
200 #elif CONFIG_EXAMPLE_USE_DM9051
201 spi_device_interface_config_t devcfg = {
202 .command_bits = 1,
203 .address_bits = 7,
204 .mode = 0,
205 .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
206 .queue_size = 20
207 };
208
209 for (int i = 0; i < CONFIG_EXAMPLE_SPI_ETHERNETS_NUM; i++) {
210 // Set SPI module Chip Select GPIO
211 devcfg.spics_io_num = spi_eth_module_config[i].spi_cs_gpio;
212
213 ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle[i]));
214 // dm9051 ethernet driver is based on spi driver
215 eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle[i]);
216
217 // Set remaining GPIO numbers and configuration used by the SPI module
218 dm9051_config.int_gpio_num = spi_eth_module_config[i].int_gpio;
219 phy_config_spi.phy_addr = spi_eth_module_config[i].phy_addr;
220 phy_config_spi.reset_gpio_num = spi_eth_module_config[i].phy_reset_gpio;
221
222 mac_spi[i] = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config_spi);
223 phy_spi[i] = esp_eth_phy_new_dm9051(&phy_config_spi);
224 }
225 #elif CONFIG_EXAMPLE_USE_W5500
226 spi_device_interface_config_t devcfg = {
227 .command_bits = 16, // Actually it's the address phase in W5500 SPI frame
228 .address_bits = 8, // Actually it's the control phase in W5500 SPI frame
229 .mode = 0,
230 .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
231 .queue_size = 20
232 };
233
234 for (int i = 0; i < CONFIG_EXAMPLE_SPI_ETHERNETS_NUM; i++) {
235 // Set SPI module Chip Select GPIO
236 devcfg.spics_io_num = spi_eth_module_config[i].spi_cs_gpio;
237
238 ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle[i]));
239 // w5500 ethernet driver is based on spi driver
240 eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(spi_handle[i]);
241
242 // Set remaining GPIO numbers and configuration used by the SPI module
243 w5500_config.int_gpio_num = spi_eth_module_config[i].int_gpio;
244 phy_config_spi.phy_addr = spi_eth_module_config[i].phy_addr;
245 phy_config_spi.reset_gpio_num = spi_eth_module_config[i].phy_reset_gpio;
246
247 mac_spi[i] = esp_eth_mac_new_w5500(&w5500_config, &mac_config_spi);
248 phy_spi[i] = esp_eth_phy_new_w5500(&phy_config_spi);
249 }
250 #endif //CONFIG_EXAMPLE_USE_W5500
251
252 for (int i = 0; i < CONFIG_EXAMPLE_SPI_ETHERNETS_NUM; i++) {
253 esp_eth_config_t eth_config_spi = ETH_DEFAULT_CONFIG(mac_spi[i], phy_spi[i]);
254 ESP_ERROR_CHECK(esp_eth_driver_install(ð_config_spi, ð_handle_spi[i]));
255
256 /* The SPI Ethernet module might not have a burned factory MAC address, we cat to set it manually.
257 02:00:00 is a Locally Administered OUI range so should not be used except when testing on a LAN under your control.
258 */
259 ESP_ERROR_CHECK(esp_eth_ioctl(eth_handle_spi[i], ETH_CMD_S_MAC_ADDR, (uint8_t[]) {
260 0x02, 0x00, 0x00, 0x12, 0x34, 0x56 + i
261 }));
262
263 // attach Ethernet driver to TCP/IP stack
264 ESP_ERROR_CHECK(esp_netif_attach(eth_netif_spi[i], esp_eth_new_netif_glue(eth_handle_spi[i])));
265 }
266 #endif // CONFIG_ETH_USE_SPI_ETHERNET
267
268 // Register user defined event handers
269 ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, NULL));
270 ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
271
272 /* start Ethernet driver state machine */
273 #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
274 ESP_ERROR_CHECK(esp_eth_start(eth_handle));
275 #endif // CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
276 #if CONFIG_EXAMPLE_USE_SPI_ETHERNET
277 for (int i = 0; i < CONFIG_EXAMPLE_SPI_ETHERNETS_NUM; i++) {
278 ESP_ERROR_CHECK(esp_eth_start(eth_handle_spi[i]));
279 }
280 #endif // CONFIG_EXAMPLE_USE_SPI_ETHERNET
281 }
282