feat(protocol_examples_common): Use Ethernet Init component

- Refactored protocol_examples_common to use ethernet_init component
This commit is contained in:
glmfe
2025-09-29 00:58:24 -03:00
committed by Ondrej Kosta
parent db1efdec09
commit f398594982
64 changed files with 317 additions and 379 deletions
@@ -33,14 +33,14 @@ endif()
idf_component_register(SRCS "${srcs}"
INCLUDE_DIRS "include"
PRIV_REQUIRES esp_netif esp_driver_gpio esp_driver_uart esp_wifi vfs console esp_eth openthread)
PRIV_REQUIRES esp_netif esp_driver_gpio esp_driver_uart esp_wifi vfs console openthread)
if(CONFIG_EXAMPLE_PROVIDE_WIFI_CONSOLE_CMD)
idf_component_optional_requires(PRIVATE console)
endif()
if(CONFIG_EXAMPLE_CONNECT_ETHERNET)
idf_component_optional_requires(PUBLIC esp_eth)
idf_component_optional_requires(PRIVATE esp_eth)
endif()
if(CONFIG_EXAMPLE_CONNECT_THREAD)
@@ -122,100 +122,13 @@ menu "Example Connection Configuration"
default y if !EXAMPLE_CONNECT_WIFI && !EXAMPLE_CONNECT_THREAD
help
Protocol examples can use Wi-Fi, Ethernet and/or Thread to connect to the network.
Choose this option to connect with Ethernet
Choose this option to enable connection with Ethernet.
Go to `Top -> Ethernet Configuration` to configure the Ethernet interface.
if EXAMPLE_CONNECT_ETHERNET
config EXAMPLE_ETHERNET_EMAC_TASK_STACK_SIZE
int "emac_rx task stack size"
default 2048
help
This set stack size for emac_rx task
config EXAMPLE_USE_SPI_ETHERNET
bool
choice EXAMPLE_ETHERNET_TYPE
prompt "Ethernet Type"
default EXAMPLE_USE_INTERNAL_ETHERNET if SOC_EMAC_SUPPORTED
default EXAMPLE_USE_DUMMY
help
Select which kind of Ethernet will be used in the example.
config EXAMPLE_USE_INTERNAL_ETHERNET
depends on SOC_EMAC_SUPPORTED
select ETH_USE_ESP32_EMAC
bool "Internal EMAC"
help
Select internal Ethernet MAC controller.
config EXAMPLE_USE_DUMMY
bool "DUMMY Module"
select ETH_USE_SPI_ETHERNET
help
Dummy option to just to pass builds, will be fixed by IDF-14059
config EXAMPLE_USE_OPENETH
bool "OpenCores Ethernet MAC (EXPERIMENTAL)"
select ETH_USE_OPENETH
help
When this option is enabled, the example is built with support for
OpenCores Ethernet MAC, which allows testing the example in QEMU.
Note that this option is used for internal testing purposes, and
not officially supported. Examples built with this option enabled
will not run on a real ESP32 chip.
endchoice # EXAMPLE_ETHERNET_TYPE
if EXAMPLE_USE_INTERNAL_ETHERNET
choice EXAMPLE_ETH_PHY_MODEL
prompt "Ethernet PHY Device"
default EXAMPLE_ETH_PHY_GENERIC
help
Select the Ethernet PHY device to use in the example.
config EXAMPLE_ETH_PHY_GENERIC
bool "Generic 802.3 PHY"
help
Any Ethernet PHY chip compliant with IEEE 802.3 can be used. However, while
basic functionality should always work, some specific features might be limited,
even if the PHY meets IEEE 802.3 standard. A typical example is loopback
functionality, where certain PHYs may require setting a specific speed mode to
operate correctly.
endchoice
config EXAMPLE_ETH_MDC_GPIO
int "SMI MDC GPIO number"
range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX
default 23 if IDF_TARGET_ESP32
default 31 if IDF_TARGET_ESP32P4
help
Set the GPIO number used by SMI MDC.
config EXAMPLE_ETH_MDIO_GPIO
int "SMI MDIO GPIO number"
range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX
default 18 if IDF_TARGET_ESP32
default 52 if IDF_TARGET_ESP32P4
help
Set the GPIO number used by SMI MDIO.
endif
config EXAMPLE_ETH_PHY_RST_GPIO
int "PHY Reset GPIO number"
range -1 ENV_GPIO_OUT_RANGE_MAX
default 51 if IDF_TARGET_ESP32P4
default 5
help
Set the GPIO number used to reset PHY chip.
Set to -1 to disable PHY chip hardware reset.
config EXAMPLE_ETH_PHY_ADDR
int "PHY Address"
range 0 31 if EXAMPLE_USE_INTERNAL_ETHERNET
default 1
help
Set PHY address according your board schematic.
endif # EXAMPLE_CONNECT_ETHERNET
config EXAMPLE_DISABLE_ETHERNET_CONFIG
bool
select ETHERNET_INIT_OVERRIDE_DISABLE
default y if !EXAMPLE_CONNECT_ETHERNET
config EXAMPLE_CONNECT_PPP
bool "connect using Point to Point interface"
@@ -9,6 +9,7 @@
#include "example_common_private.h"
#include "esp_event.h"
#include "esp_eth.h"
#include "ethernet_init.h"
#include "esp_log.h"
#include "esp_mac.h"
#include "driver/gpio.h"
@@ -72,17 +73,15 @@ static void on_eth_event(void *esp_netif, esp_event_base_t event_base,
#endif // CONFIG_EXAMPLE_CONNECT_IPV6
static esp_eth_handle_t s_eth_handle = NULL;
static esp_eth_mac_t *s_mac = NULL;
static esp_eth_phy_t *s_phy = NULL;
static esp_eth_handle_t *s_eth_handles = NULL;
static uint8_t s_eth_count = 0;
static esp_eth_netif_glue_handle_t s_eth_glue = NULL;
static esp_netif_t *s_eth_netif = NULL;
static esp_netif_t *eth_start(void)
{
// TODO just to pass builds, will be fixed by IDF-14059
#if CONFIG_EXAMPLE_USE_DUMMY
return NULL;
#else
ESP_ERROR_CHECK(ethernet_init_all(&s_eth_handles, &s_eth_count));
esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
// Warning: the interface desc is used in tests to capture actual connection details (IP, gw, mask)
esp_netif_config.if_desc = EXAMPLE_NETIF_DESC_ETH;
@@ -91,69 +90,39 @@ static esp_netif_t *eth_start(void)
.base = &esp_netif_config,
.stack = ESP_NETIF_NETSTACK_DEFAULT_ETH
};
esp_netif_t *netif = esp_netif_new(&netif_config);
assert(netif);
s_eth_netif = esp_netif_new(&netif_config);
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
mac_config.rx_task_stack_size = CONFIG_EXAMPLE_ETHERNET_EMAC_TASK_STACK_SIZE;
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;
#if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
esp32_emac_config.smi_gpio.mdc_num = CONFIG_EXAMPLE_ETH_MDC_GPIO;
esp32_emac_config.smi_gpio.mdio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO;
s_mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
#if CONFIG_EXAMPLE_ETH_PHY_GENERIC
s_phy = esp_eth_phy_new_generic(&phy_config);
#endif // CONFIG_EXAMPLE_ETH_PHY_GENERIC
#elif CONFIG_EXAMPLE_USE_OPENETH
phy_config.autonego_timeout_ms = 100;
s_mac = esp_eth_mac_new_openeth(&mac_config);
s_phy = esp_eth_phy_new_generic(&phy_config);
#endif // CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
// Install Ethernet driver
esp_eth_config_t config = ETH_DEFAULT_CONFIG(s_mac, s_phy);
ESP_ERROR_CHECK(esp_eth_driver_install(&config, &s_eth_handle));
// combine driver with netif
s_eth_glue = esp_eth_new_netif_glue(s_eth_handle);
esp_netif_attach(netif, s_eth_glue);
s_eth_glue = esp_eth_new_netif_glue(s_eth_handles[0]);
esp_netif_attach(s_eth_netif, s_eth_glue);
// Register user defined event handlers
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &eth_on_got_ip, NULL));
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event, netif));
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event, s_eth_netif));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &eth_on_got_ipv6, NULL));
#endif
esp_eth_start(s_eth_handle);
return netif;
#endif // CONFIG_EXAMPLE_USE_DUMMY
ESP_ERROR_CHECK(esp_eth_start(s_eth_handles[0]));
return s_eth_netif;
}
static void eth_stop(void)
{
esp_netif_t *eth_netif = get_example_netif_from_desc(EXAMPLE_NETIF_DESC_ETH);
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, &eth_on_got_ip));
#if CONFIG_EXAMPLE_CONNECT_IPV6
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &eth_on_got_ipv6));
ESP_ERROR_CHECK(esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event));
#endif
ESP_ERROR_CHECK(esp_eth_stop(s_eth_handle));
ESP_ERROR_CHECK(esp_eth_stop(s_eth_handles[0]));
ESP_ERROR_CHECK(esp_eth_del_netif_glue(s_eth_glue));
ESP_ERROR_CHECK(esp_eth_driver_uninstall(s_eth_handle));
s_eth_handle = NULL;
ESP_ERROR_CHECK(s_phy->del(s_phy));
ESP_ERROR_CHECK(s_mac->del(s_mac));
esp_netif_destroy(s_eth_netif);
ethernet_deinit_all(s_eth_handles);
esp_netif_destroy(eth_netif);
}
esp_eth_handle_t get_example_eth_handle(void)
{
return s_eth_handle;
s_eth_glue = NULL;
s_eth_netif = NULL;
s_eth_handles = NULL;
s_eth_count = 0;
}
/* tear down connection, release resources */
@@ -0,0 +1,5 @@
dependencies:
espressif/ethernet_init:
version: "~1.2.0"
rules:
- if: "target != linux"
@@ -13,9 +13,6 @@
#include "esp_err.h"
#if !CONFIG_IDF_TARGET_LINUX
#include "esp_netif.h"
#if CONFIG_EXAMPLE_CONNECT_ETHERNET
#include "esp_eth.h"
#endif
#endif // !CONFIG_IDF_TARGET_LINUX
#ifdef __cplusplus
@@ -136,15 +133,6 @@ esp_netif_t *get_example_netif_from_desc(const char *desc);
void example_register_wifi_connect_commands(void);
#endif
#if CONFIG_EXAMPLE_CONNECT_ETHERNET
/**
* @brief Get the example Ethernet driver handle
*
* @return esp_eth_handle_t
*/
esp_eth_handle_t get_example_eth_handle(void);
#endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
#else
static inline esp_err_t example_connect(void) {return ESP_OK;}
#endif // !CONFIG_IDF_TARGET_LINUX
@@ -1 +1,23 @@
CONFIG_EXAMPLE_ETH_PHY_IP101 CONFIG_EXAMPLE_ETH_PHY_GENERIC
CONFIG_EXAMPLE_ETHERNET_EMAC_TASK_STACK_SIZE CONFIG_ETHERNET_RX_TASK_STACK_SIZE
CONFIG_EXAMPLE_USE_SPI_ETHERNET CONFIG_ETHERNET_SPI_SUPPORT
CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET CONFIG_ETHERNET_INTERNAL_SUPPORT
CONFIG_EXAMPLE_USE_DM9051 CONFIG_ETHERNET_SPI_DEV0_DM9051
CONFIG_EXAMPLE_USE_W5500 CONFIG_ETHERNET_SPI_DEV0_W5500
CONFIG_EXAMPLE_USE_OPENETH CONFIG_ETHERNET_OPENETH_SUPPORT
CONFIG_EXAMPLE_ETH_PHY_GENERIC CONFIG_ETHERNET_PHY_GENERIC
CONFIG_EXAMPLE_ETH_PHY_IP101 CONFIG_ETHERNET_PHY_IP101
CONFIG_EXAMPLE_ETH_PHY_RTL8201 CONFIG_ETHERNET_PHY_RTL8201
CONFIG_EXAMPLE_ETH_PHY_LAN87XX CONFIG_ETHERNET_PHY_LAN87XX
CONFIG_EXAMPLE_ETH_PHY_DP83848 CONFIG_ETHERNET_PHY_DP83848
CONFIG_EXAMPLE_ETH_PHY_KSZ80XX CONFIG_ETHERNET_PHY_KSZ80XX
CONFIG_EXAMPLE_ETH_MDC_GPIO CONFIG_ETHERNET_MDC_GPIO
CONFIG_EXAMPLE_ETH_MDIO_GPIO CONFIG_ETHERNET_MDIO_GPIO
CONFIG_EXAMPLE_ETH_SPI_HOST CONFIG_ETHERNET_SPI_HOST
CONFIG_EXAMPLE_ETH_SPI_SCLK_GPIO CONFIG_ETHERNET_SPI_SCLK_GPIO
CONFIG_EXAMPLE_ETH_SPI_MOSI_GPIO CONFIG_ETHERNET_SPI_MOSI_GPIO
CONFIG_EXAMPLE_ETH_SPI_MISO_GPIO CONFIG_ETHERNET_SPI_MISO_GPIO
CONFIG_EXAMPLE_ETH_SPI_CS_GPIO CONFIG_ETHERNET_SPI_CS0_GPIO
CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ CONFIG_ETHERNET_SPI_CLOCK_MHZ
CONFIG_EXAMPLE_ETH_SPI_INT_GPIO CONFIG_ETHERNET_SPI_INT0_GPIO
CONFIG_EXAMPLE_ETH_PHY_RST_GPIO CONFIG_ETHERNET_PHY_RST_GPIO
CONFIG_EXAMPLE_ETH_PHY_ADDR CONFIG_ETHERNET_PHY_ADDR