I am now using a board named ESP32-EVB and tried to build an Ethernet connection.
I want to disable the DHCP and want to assign a static IP.
I used the below program but I couldn't get an IP address.
Code: Select all
#include <string.h>
#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"
ret = tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH);
tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &ip_config);
tcpip_adapter_ip_info_t ip_config;
ip4addr_aton(CONFIG_ETHERNET_HELPER_STATIC_IP4_ADDRESS, &ip_info.ip);
ip4addr_aton(CONFIG_ETHERNET_HELPER_STATIC_IP4_GATEWAY, &ip_info.netmask);
ip4addr_aton(CONFIG_ETHERNET_HELPER_STATIC_IP4_NETMASK, &ip_info.gw);
ESP_LOGI(TAG, ret);
#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(0)
#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, ð_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;
}
Code: Select all
#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.4.3"
#define CONFIG_ETHERNET_HELPER_STATIC_IP4_GATEWAY "192.168.4.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
/**
* @brief Configure Wi-Fi or Ethernet, connect, wait for IP
*
* This all-in-one helper function is used in protocols examples to
* reduce the amount of boilerplate in the example.
*
* It is not intended to be used in real world applications.
* See examples under examples/wifi/getting_started/ and examples/ethernet/
* for more complete Wi-Fi or Ethernet initialization code.
*
* Read "Establishing Wi-Fi or Ethernet Connection" section in
* examples/protocols/README.md for more information about this function.
*
* @return ESP_OK on successful connection
*/
esp_err_t example_connect(void);
/**
* Counterpart to example_connect, de-initializes Wi-Fi or Ethernet
*/
esp_err_t example_disconnect(void);
esp_netif_t *get_example_netif(void);
#ifdef __cplusplus
}
#endif
I (523) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (611) system_api: Base MAC address is not set
I (611) system_api: read default base MAC address from EFUSE
I (641) esp_eth.netif.glue: 98:f479:ae:fb
I (641) esp_eth.netif.glue: ethernet attached to netif
I (651) online_connection: Waiting for IP
I (651) online_connection: Connected to ETH
I (651) online_connection: IPv4 address: 0.0.0.0