-
Notifications
You must be signed in to change notification settings - Fork 6
Open
Description
Hi. I'm trying to replicate your work here. Thank you for your efforts.
I've made small edits in the code below. However, my IP Event stuff never gets triggered, and returns this:
D (123435) esp_netif_lwip: esp_netif_ip_lost_timer esp_netif:0x3ffbc7fc
D (123435) esp_netif_lwip: check: local, if=0x3ffbc7fc fn=0x400e310c
--- 0x400e310c: esp_netif_update_default_netif_lwip at /root/esp/v5.4/esp-idf/components/esp_netif/lwip/esp_netif_lwip.c:313
D (123435) esp_netif_lwip: esp_netif_update_default_netif_lwip 0x3ffbc7fc
V (123435) esp_netif_objects: esp_netif_next_unsafe 0x0
V (123445) esp_netif_lwip: esp_netif_is_netif_up esp_netif:0x3ffbc7fc
V (123445) esp_netif_objects: esp_netif_next_unsafe 0x3ffbc7fc
V (123455) esp_netif_lwip: esp_netif_is_netif_up esp_netif:0x3ffbc7fc
D (123465) esp_netif_lwip: if0x3ffbc7fc ip lost tmr: raise ip lost event
D (123465) event: no handlers have been registered for event IP_EVENT:5 posted to loop 0x3ffbb8bc
If I try to ping it from my terminal, it just hangs forever. I've done all the soldering that you have.
Kindly looking to your help in this, I am trying to use this for my Senior Design project. Thank you very much.
Code:
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/queue.h"
#include "freertos/event_groups.h"
#include "esp_netif.h"
#include "esp_eth.h"
#include "esp_wifi.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_eth_driver.h"
#include "esp_check.h"
#include "esp_mac.h"
#include "lwip/sockets.h"
#include "lwip/dns.h"
#include "lwip/netdb.h"
#define ETH_PHY_ADDR 1
#define ETH_PHY_RST_GPIO -1 // not connected
#define ETH_MDC_GPIO 23
#define ETH_MDIO_GPIO 18
#define ETH_TAG "ETH"
static EventGroupHandle_t s_eth_event_group;
#define ETHERNET_CONNECTED_BIT BIT0
#define ETHERNET_FAIL_BIT BIT1
#define STATIC_IP 1
#if STATIC_IP
#define S_IP "192.168.1.5"
#define GATEWAY "192.168.1.1"
#define NETMASK "255.255.255.0"
#endif /* STATIC_IP */
static esp_err_t ethernet_packet_handler(void *eth_handle, uint8_t *buffer, uint32_t length, void *priv)
{
ESP_LOGI(ETH_TAG, "Received Ethernet Packet: %d bytes", length);
// Print first 20 bytes (packet headers)
printf("Packet Data: ");
for (uint32_t i = 0; i < length && i < 20; i++) {
printf("%02X ", buffer[i]);
}
printf("\n");
// Free buffer (if allocated dynamically)
free(buffer);
return ESP_OK;
}
/** 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};
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(ETH_TAG, "Ethernet Link Up");
ESP_LOGI(ETH_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]);
esp_netif_t *eth_netif = esp_netif_get_handle_from_ifkey("ETH_DEF");
if (eth_netif) {
ESP_LOGI(ETH_TAG, "Starting DHCP client...");
esp_netif_dhcpc_start(eth_netif);
} else {
ESP_LOGE(ETH_TAG, "Failed to get ETH_DEF interface handle");
}
break;
case ETHERNET_EVENT_DISCONNECTED:
ESP_LOGI(ETH_TAG, "Ethernet Link Down");
xEventGroupSetBits(s_eth_event_group, ETHERNET_FAIL_BIT);
break;
case ETHERNET_EVENT_START:
ESP_LOGI(ETH_TAG, "Ethernet Started");
break;
case ETHERNET_EVENT_STOP:
ESP_LOGI(ETH_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 esp_netif_ip_info_t *ip_info = &event->ip_info;
ESP_LOGE(ETH_TAG, "Ethernet Got IP Address");
ESP_LOGE(ETH_TAG, "~~~~~~~~~~~");
ESP_LOGE(ETH_TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip));
ESP_LOGE(ETH_TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
ESP_LOGE(ETH_TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
ESP_LOGE(ETH_TAG, "~~~~~~~~~~~");
xEventGroupSetBits(s_eth_event_group, ETHERNET_CONNECTED_BIT);
}
static void lost_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
ESP_LOGW(ETH_TAG, "Lost IP address! Reconnecting...");
// Restart DHCP client
esp_netif_t *eth_netif = esp_netif_get_handle_from_ifkey("ETH_DEF");
if (eth_netif) {
ESP_LOGI(ETH_TAG, "Restarting DHCP...");
esp_netif_dhcpc_start(eth_netif);
} else {
ESP_LOGE(ETH_TAG, "Failed to get network interface");
}
}
static esp_eth_handle_t eth_init_internal(esp_eth_mac_t **mac_out, esp_eth_phy_t **phy_out)
{
esp_eth_handle_t ret = NULL;
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
phy_config.phy_addr = ETH_PHY_ADDR;
phy_config.reset_gpio_num = ETH_PHY_RST_GPIO;
eth_esp32_emac_config_t esp32_emac_config = {
.smi_mdc_gpio_num = ETH_MDC_GPIO,
.smi_mdio_gpio_num = ETH_MDIO_GPIO,
.interface = EMAC_DATA_INTERFACE_RMII,
.clock_config = { .rmii = { .clock_mode = EMAC_CLK_OUT, .clock_gpio = EMAC_CLK_OUT_180_GPIO } },
.dma_burst_len = ETH_DMA_BURST_LEN_32
};
esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
esp_eth_phy_t *phy = esp_eth_phy_new_lan87xx(&phy_config);
esp_eth_handle_t eth_handle = NULL;
esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
// Set the custom stack input function to process incoming packets
config.stack_input = ethernet_packet_handler;
ESP_GOTO_ON_FALSE(esp_eth_driver_install(&config, ð_handle) == ESP_OK, NULL, err, ETH_TAG, "Ethernet driver install failed");
if (mac_out != NULL) {
*mac_out = mac;
}
if (phy_out != NULL) {
*phy_out = phy;
}
return eth_handle;
err:
if (eth_handle != NULL) {
esp_eth_driver_uninstall(eth_handle);
}
if (mac != NULL) {
mac->del(mac);
}
if (phy != NULL) {
phy->del(phy);
}
return ret;
}
void ethernet_setup(void)
{
s_eth_event_group = xEventGroupCreate();
esp_eth_handle_t eth_handle;
eth_handle = eth_init_internal(NULL, NULL);
ESP_ERROR_CHECK(esp_netif_init());
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);
#if STATIC_IP
if (esp_netif_dhcpc_stop(eth_netif) != ESP_OK) {
ESP_LOGE(ETH_TAG, "Failed to stop dhcp client");
return;
}
esp_netif_ip_info_t info_t;
memset(&info_t, 0, sizeof(esp_netif_ip_info_t));
// Correct method: Use IP4_ADDR() to set values
IP4_ADDR(&info_t.ip, 192, 168, 1, 5);
IP4_ADDR(&info_t.gw, 192, 168, 1, 1);
IP4_ADDR(&info_t.netmask, 255, 255, 255, 0);
if (esp_netif_set_ip_info(eth_netif, &info_t) != ESP_OK) {
ESP_LOGE(ETH_TAG, "Failed to set IP info");
}
#endif /* STATIC_IP */
ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle)));
ESP_LOGI(ETH_TAG, "Registering event handlers...");
esp_err_t ret1 = esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, NULL);
if (ret1 != ESP_OK) {
ESP_LOGE(ETH_TAG, "Failed to register ETH_EVENT handler: %s", esp_err_to_name(ret1));
}
esp_err_t ret2 = esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL);
esp_err_t ret3 = esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &got_ip_event_handler, NULL);
if (ret2 != ESP_OK) {
ESP_LOGE(ETH_TAG, "Failed to register IP_EVENT_ETH_GOT_IP handler: %s", esp_err_to_name(ret2));
}
if (ret3 != ESP_OK) {
ESP_LOGE(ETH_TAG, "Failed to register IP_EVENT_ETH_GOT_IP handler: %s", esp_err_to_name(ret3));
}
ESP_ERROR_CHECK(esp_eth_start(eth_handle));
ESP_LOGI(ETH_TAG, "ESP ETH START Done.");
ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle)));
EventBits_t bits = xEventGroupWaitBits(s_eth_event_group,
ETHERNET_CONNECTED_BIT | ETHERNET_FAIL_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY);
if (bits & ETHERNET_CONNECTED_BIT) {
ESP_LOGI(ETH_TAG, "Ethernet Connection established.\n");
} else if (bits & ETHERNET_FAIL_BIT) {
ESP_LOGE(ETH_TAG, "Ethernet Connection Failed.");
} else {
ESP_LOGE(ETH_TAG, "UNEXPECTED EVENT");
}
}
void app_main(void)
{
ethernet_setup();
vTaskDelay(2000 / portTICK_PERIOD_MS);
ESP_LOGI(ETH_TAG, "Ethernet Initialized!");
}
Metadata
Metadata
Assignees
Labels
No labels