Ethernet IP address not getting assigning with the IP address?

vadivelan.s@aco.com
Posts: 3
Joined: Mon Jan 30, 2023 9:56 am

Ethernet IP address not getting assigning with the IP address?

Postby vadivelan.s@aco.com » Thu May 18, 2023 9:34 am

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "freertos/FreeRTOS.h"
  4. #include "freertos/task.h"
  5. #include "tcpip_adapter.h"
  6. #include "esp_netif.h"
  7. #include "esp_eth.h"
  8. #include "esp_event.h"
  9. #include "esp_log.h"
  10. #include "driver/gpio.h"
  11. #include "sdkconfig.h"
  12. #include "esp_eth_netif_glue.h"
  13. #include "esp_netif_net_stack.h"
  14.  
  15. static const char *TAG = "eth_example";
  16.  
  17. // MAC address to be used for the Ethernet interface
  18. uint8_t mac_addr[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
  19.  
  20. // IP address information for the Ethernet interface
  21. tcpip_adapter_ip_info_t ip_info = {
  22.     .ip.addr = 0, // set to 0 to use DHCP
  23.     .netmask.addr = 0xFFFFFFFF, // default netmask
  24.     .gw.addr = 0 // default gateway
  25. };
  26.  
  27.  
  28. /** Event handler for Ethernet events */
  29. static void eth_event_handler(void *arg, esp_event_base_t event_base,
  30.                               int32_t event_id, void *event_data)
  31. {
  32.     uint8_t mac_addr[6] = {0};
  33.     /* we can get the ethernet driver handle from event data */
  34.     esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;
  35.  
  36.     switch (event_id) {
  37.     case ETHERNET_EVENT_CONNECTED:
  38.         esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
  39.         ESP_LOGI(TAG, "Ethernet Link Up");
  40.         ESP_LOGI(TAG, "Ethernet HW Addr %02x:%02x:%02x:%02x:%02x:%02x",
  41.                  mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  42.         break;
  43.     case ETHERNET_EVENT_DISCONNECTED:
  44.         ESP_LOGI(TAG, "Ethernet Link Down");
  45.         break;
  46.     case ETHERNET_EVENT_START:
  47.         ESP_LOGI(TAG, "Ethernet Started");
  48.         break;
  49.     case ETHERNET_EVENT_STOP:
  50.         ESP_LOGI(TAG, "Ethernet Stopped");
  51.         break;
  52.     default:
  53.         break;
  54.     }
  55. }
  56.  
  57. /** Event handler for IP_EVENT_ETH_GOT_IP */
  58. static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
  59.                                  int32_t event_id, void *event_data)
  60. {
  61.     ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
  62.     const tcpip_adapter_ip_info_t *ip_info = &event->ip_info;
  63.  
  64.     ESP_LOGI(TAG, "Ethernet Got IP Address");
  65.     ESP_LOGI(TAG, "~~~~~~~~~~~");
  66.     ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip));
  67.     ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
  68.     ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
  69.     ESP_LOGI(TAG, "~~~~~~~~~~~");
  70. }
  71.  
  72. void ethernet_task(void *pvParameters)
  73. {
  74.     tcpip_adapter_init();
  75.     //Start the Ethernet interface
  76.    
  77.     ESP_ERROR_CHECK(esp_event_loop_create_default());
  78.  
  79.     esp_netif_config_t cfg= ESP_NETIF_DEFAULT_ETH();
  80.     esp_netif_t *eth_netif =esp_netif_new(&cfg);
  81.  
  82.     ESP_ERROR_CHECK(tcpip_adapter_set_default_eth_handlers());
  83.  
  84.  
  85.    eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  86.     eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  87.     phy_config.phy_addr = 0;
  88.     phy_config.reset_gpio_num = -1;
  89.  
  90.     mac_config.smi_mdc_gpio_num = 23;
  91.     mac_config.smi_mdio_gpio_num = 18;
  92.  
  93.  
  94.     esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
  95.  
  96.     esp_eth_phy_t *phy = esp_eth_phy_new_lan8720(&phy_config);
  97.  
  98.     esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
  99.  
  100.     esp_eth_handle_t eth_handle = NULL;
  101.     ESP_ERROR_CHECK(esp_eth_driver_install(&config, &eth_handle));
  102.  
  103.     ESP_ERROR_CHECK(esp_netif_attach(eth_netif,esp_eth_new_netif_glue(eth_handle)));
  104.  
  105.     ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));
  106.     ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
  107.  
  108.  
  109.     ESP_ERROR_CHECK(esp_eth_start(eth_handle));      
  110.  
  111.     while(1)
  112.     {
  113.         vTaskDelay(pdMS_TO_TICKS(500)); // delay for 1 second
  114.     }
  115. }
  116.  
  117. void ethernet_init()
  118. {
  119.     xTaskCreate(ethernet_task, "eth_task", 4096, NULL, configMAX_PRIORITIES-1, NULL);  
  120. }




This code not getting assigning the IP Address? similar issue(https://github.com/espressif/arduino-esp32/issues/3831)

Who is online

Users browsing this forum: Usama Masood and 93 guests