espressif / esp-idf

Espressif IoT Development Framework. Official development framework for Espressif SoCs.
Apache License 2.0
13.77k stars 7.31k forks source link

Failed to get the IP address (IDFGH-4333) #6171

Closed rony91jp closed 3 years ago

rony91jp commented 3 years ago

Hello,

I am currently using ESP32-POE board and tried to build an Ethernet Connection. Following is the result. image Below is the example configuration& ethernet configuration image image

Thank you

suda-morris commented 3 years ago

RMII clock output mode is not always work on esp32, we have two GPIO (16 and 17, they're inverted), in my experience, if GPIO17 can't work, then GPIO16 could work. Many causes can lead to such issues, like PCB trace (remember RMII clock is 50MHz) length. I can't reproduce your issue with my LAN8720 breakout board and some wire jumpers. Maybe you should go to the forum to ask for help.

ryankurte commented 3 years ago

we're seeing a similar issue with a custom board + LAN8710, the MAC comes up, connect / disconnect events work, and data is received / transmitted, but, we never receive an IP. setting the debug level to VERBOSE shows incoming ICMP packets that appear to be corrupt (MAC addresses that do not exist on the network etc.) so we're working on the basis this is an RMII / clock signal integrity issue. do y'all have any advice for further diagnosing this?

rony-91 commented 3 years ago

Hello,

Regarding the above issue, I was able to get the static IP address using the Arduino ethernet example. I used the below command to assign my static IP. ETH.begin();

ETH.config(IPAddress(192, 168, 1, 101),IPAddress(192, 168, 1, 1),IPAddress(255, 255, 255, 0),IPAddress(192, 168, 1, 1), IPAddress(192, 168, 1, 1));

Output: image

My question is, I want to disable the DHCP and want to assign the static IP above the same way in the esp-idf example. Where should I change in the esp-idf ethernet basic example program?

Thank you

suda-morris commented 3 years ago

@rony-91 To set static IP address, firstly you need to stop the dhcpc by esp_netif_dhcpc_stop, then set IP into by esp_netif_set_ip_info

More info, please refer to https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/network/esp_netif.html

rony91jp commented 3 years ago

@suda-morris

I need your little help. Why i did not get the static ip from below program?

include

include "sdkconfig.h"

include "esp_event.h"

include "esp_wifi.h"

include "esp_wifi_default.h"

if CONFIG_EXAMPLE_CONNECT_ETHERNET

include "esp_eth.h"

endif

include "esp_log.h"

include "driver/gpio.h"

include "freertos/FreeRTOS.h"

include "freertos/task.h"

include "freertos/event_groups.h"

include "lwip/err.h"

include "lwip/sys.h"

include "ethernet_connect.h"

define GOT_IPV4_BIT BIT(4)

define GOT_IPV6_BIT BIT(1)

define CONNECTED_BITS (GOT_IPV4_BIT)

static EventGroupHandle_t s_connect_event_group; static esp_ip4_addr_t s_ip_addr; static const char s_connection_name; static esp_netif_t s_example_esp_netif = NULL;

static const char *TAG = "online_connection"; static void start(void); static void stop(void);

static void on_got_ip(void arg, esp_event_base_t event_base, int32_t event_id, void event_data) {

ESP_LOGI(TAG, "Got IP event!");
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
memcpy(&s_ip_addr, &event->ip_info.ip, sizeof(s_ip_addr));
xEventGroupSetBits(s_connect_event_group, GOT_IPV4_BIT);

}

esp_err_t example_connect(void) { if (s_connect_event_group != NULL) { return ESP_ERR_INVALID_STATE; }

s_connect_event_group = xEventGroupCreate();
start();
ESP_ERROR_CHECK(esp_register_shutdown_handler(&stop));
ESP_LOGI(TAG, "Waiting for IP");
ESP_LOGI(TAG, "Connected to %s", s_connection_name);
ESP_LOGI(TAG, "IPv4 address: " IPSTR, IP2STR(&s_ip_addr));
xEventGroupWaitBits(s_connect_event_group, CONNECTED_BITS, true, true, portMAX_DELAY);
ESP_LOGI(TAG, "IPv4 address: " IPSTR, IP2STR(&s_ip_addr));
return ESP_OK;

}

esp_err_t example_disconnect(void) { if (s_connect_event_group == NULL) { return ESP_ERR_INVALID_STATE; } vEventGroupDelete(s_connect_event_group); s_connect_event_group = NULL; stop(); ESP_LOGI(TAG, "Disconnected from %s", s_connection_name); s_connection_name = NULL; return ESP_OK; }

ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET

static esp_eth_handle_t eth_handle = NULL; static esp_eth_mac_t s_mac = NULL; static esp_eth_phy_t s_phy = NULL; static void *s_eth_glue = NULL;

static void start(void) { esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH(); esp_netif_t *eth_netif = esp_netif_new(&cfg); assert(eth_netif); s_example_esp_netif = eth_netif;

// Set default handlers to process TCP/IP stuffs
ESP_ERROR_CHECK(esp_eth_set_default_handlers(eth_netif));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip, NULL));

//Configuration using LAN8720
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();

phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR;
phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO;
mac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO;
mac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO;
s_mac = esp_eth_mac_new_esp32(&mac_config);
s_phy = esp_eth_phy_new_lan8720(&phy_config);

esp_eth_config_t config = ETH_DEFAULT_CONFIG(s_mac, s_phy);
ESP_ERROR_CHECK(esp_eth_driver_install(&config, &eth_handle));
ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle)));
ESP_ERROR_CHECK(esp_eth_start(eth_handle));
s_connection_name = "ETH";

}

static void stop(void) { ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip)); ESP_ERROR_CHECK(esp_eth_stop(eth_handle)); ESP_ERROR_CHECK(esp_eth_del_netif_glue(s_eth_glue)); ESP_ERROR_CHECK(esp_eth_clear_default_handlers(s_example_esp_netif)); ESP_ERROR_CHECK(esp_eth_driver_uninstall(eth_handle)); ESP_ERROR_CHECK(s_phy->del(s_phy)); ESP_ERROR_CHECK(s_mac->del(s_mac));

esp_netif_destroy(s_example_esp_netif);
s_example_esp_netif = NULL;

}

endif // CONFIG_EXAMPLE_CONNECT_ETHERNET

esp_netif_t *get_example_netif(void) { return s_example_esp_netif; }

[Ethernet_connect.h] Here i wrote the static ip address. / Common functions for protocol examples, to establish Wi-Fi or Ethernet connection. This example code is in the Public Domain (or CC0 licensed, at your option.) Unless required by applicable law or agreed to in writing, this software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. /

pragma once

ifdef __cplusplus

extern "C" {

endif

include "esp_err.h"

include "esp_netif.h"

define CONFIG_ESP32_SPIRAM_SUPPORT 1

define CONFIG_MBEDTLS_EXTERNAL_MEM_ALLOC 1

define CONFIG_EXAMPLE_CONNECT_ETHERNET 1

define CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET 1

define CONFIG_EXAMPLE_ETH_PHY_LAN8720 1

define CONFIG_EXAMPLE_ETH_MDC_GPIO 23

define CONFIG_EXAMPLE_ETH_MDIO_GPIO 18

define CONFIG_EXAMPLE_ETH_PHY_ADDR 0

define CONFIG_EXAMPLE_ETH_PHY_RST_GPIO 12

ifdef CONFIG_USE_STATIC_IP

define CONFIG_ETHERNET_HELPER_STATIC_IP4_ADDRESS "192.168.3.3"

define CONFIG_ETHERNET_HELPER_STATIC_IP4_GATEWAY "192.168.3.1"

define CONFIG_ETHERNET_HELPER_STATIC_IP4_NETMASK "255.255.255.0"

endif //CONFIG_USE_STATIC_IP

ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET

//#define EXAMPLE_INTERFACE get_example_netif()

define EXAMPLE_INTERFACE TCPIP_ADAPTER_IF_ETH

endif

ifdef CONFIG_EXAMPLE_CONNECT_WIFI

//#define EXAMPLE_INTERFACE get_example_netif()

define EXAMPLE_INTERFACE TCPIP_ADAPTER_IF_STA

endif

// #define CONFIG_EXAMPLE_CONNECT_IPV6

/**

/**

ifdef __cplusplus

}

endif

[Output is below] image

rony91jp commented 3 years ago

The above problem has solved. Thank you all for your support.