- #include <stdio.h>
- #include <string.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "tcpip_adapter.h"
- #include "esp_netif.h"
- #include "esp_eth.h"
- #include "esp_event.h"
- #include "esp_log.h"
- #include "driver/gpio.h"
- #include "sdkconfig.h"
- #include "esp_eth_netif_glue.h"
- #include "esp_netif_net_stack.h"
- static const char *TAG = "eth_example";
- // MAC address to be used for the Ethernet interface
- uint8_t mac_addr[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
- // IP address information for the Ethernet interface
- tcpip_adapter_ip_info_t ip_info = {
- .ip.addr = 0, // set to 0 to use DHCP
- .netmask.addr = 0xFFFFFFFF, // default netmask
- .gw.addr = 0 // default gateway
- };
- /** Event handler for Ethernet events */
- static void eth_event_handler(void *arg, esp_event_base_t event_base,
- int32_t event_id, void *event_data)
- {
- uint8_t mac_addr[6] = {0};
- /* we can get the ethernet driver handle from event data */
- esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;
- switch (event_id) {
- case ETHERNET_EVENT_CONNECTED:
- esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
- ESP_LOGI(TAG, "Ethernet Link Up");
- ESP_LOGI(TAG, "Ethernet HW Addr %02x:%02x:%02x:%02x:%02x:%02x",
- mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
- break;
- case ETHERNET_EVENT_DISCONNECTED:
- ESP_LOGI(TAG, "Ethernet Link Down");
- break;
- case ETHERNET_EVENT_START:
- ESP_LOGI(TAG, "Ethernet Started");
- break;
- case ETHERNET_EVENT_STOP:
- ESP_LOGI(TAG, "Ethernet Stopped");
- break;
- default:
- break;
- }
- }
- /** Event handler for IP_EVENT_ETH_GOT_IP */
- static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
- int32_t event_id, void *event_data)
- {
- ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
- const tcpip_adapter_ip_info_t *ip_info = &event->ip_info;
- ESP_LOGI(TAG, "Ethernet Got IP Address");
- ESP_LOGI(TAG, "~~~~~~~~~~~");
- ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip));
- ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
- ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
- ESP_LOGI(TAG, "~~~~~~~~~~~");
- }
- void ethernet_task(void *pvParameters)
- {
- tcpip_adapter_init();
- //Start the Ethernet interface
- ESP_ERROR_CHECK(esp_event_loop_create_default());
- esp_netif_config_t cfg= ESP_NETIF_DEFAULT_ETH();
- esp_netif_t *eth_netif =esp_netif_new(&cfg);
- ESP_ERROR_CHECK(tcpip_adapter_set_default_eth_handlers());
- eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
- eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
- phy_config.phy_addr = 0;
- phy_config.reset_gpio_num = -1;
- mac_config.smi_mdc_gpio_num = 23;
- mac_config.smi_mdio_gpio_num = 18;
- esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
- esp_eth_phy_t *phy = esp_eth_phy_new_lan8720(&phy_config);
- esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
- esp_eth_handle_t eth_handle = NULL;
- ESP_ERROR_CHECK(esp_eth_driver_install(&config, ð_handle));
- ESP_ERROR_CHECK(esp_netif_attach(eth_netif,esp_eth_new_netif_glue(eth_handle)));
- ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, NULL));
- ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
- ESP_ERROR_CHECK(esp_eth_start(eth_handle));
- while(1)
- {
- vTaskDelay(pdMS_TO_TICKS(500)); // delay for 1 second
- }
- }
- void ethernet_init()
- {
- xTaskCreate(ethernet_task, "eth_task", 4096, NULL, configMAX_PRIORITIES-1, NULL);
- }
This code not getting assigning the IP Address? similar issue(https://github.com/espressif/arduino-esp32/issues/3831)