Olimex ESP32 Gateway: Ethernet issues

tomfrance
Posts: 12
Joined: Wed Mar 17, 2021 3:50 pm

Olimex ESP32 Gateway: Ethernet issues

Postby tomfrance » Wed Mar 17, 2021 5:13 pm

Dear friends,

I recently purchased a new dev board - the Olimex ESP32 Gateway. I got the basic stuff running, but I am currently having issues with the ethernet part.

The following code works and I get an IP lease:

Code: Select all

#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "tcpip_adapter.h"
#include "esp_netif.h"
#include "esp_eth.h"
#include "esp_event.h"
#include "esp_log.h"
#include "driver/gpio.h"
#include "sdkconfig.h"

static const char *TAG = "device";

/** 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};
    /* we can get the ethernet driver handle from event data */
    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(TAG, "Ethernet Link Up");
        ESP_LOGI(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]);
        break;
    case ETHERNET_EVENT_DISCONNECTED:
        ESP_LOGI(TAG, "Ethernet Link Down");
        break;
    case ETHERNET_EVENT_START:
        ESP_LOGI(TAG, "Ethernet Started");
        break;
    case ETHERNET_EVENT_STOP:
        ESP_LOGI(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 tcpip_adapter_ip_info_t *ip_info = &event->ip_info;
    const esp_netif_ip_info_t *ip_info = &event->ip_info;

    ESP_LOGI(TAG, "Ethernet Got IP Address");
    ESP_LOGI(TAG, "~~~~~~~~~~~");
    ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip));
    ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
    ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
    ESP_LOGI(TAG, "~~~~~~~~~~~");
}

void app_main() {

[color=#00BF00]    tcpip_adapter_init();[/color]
[color=#FF0000]    //ESP_ERROR_CHECK(esp_netif_init());[/color]

[color=#FF0000]    //esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
    //esp_netif_t *eth_netif = esp_netif_new(&cfg);[/color]

    ESP_ERROR_CHECK(esp_event_loop_create_default());

    // Set default handlers to process TCP/IP stuffs
[color=#FF0000]    //ESP_ERROR_CHECK(esp_eth_set_default_handlers(eth_netif));[/color]

[color=#00BF00]    ESP_ERROR_CHECK(tcpip_adapter_set_default_eth_handlers());[/color]
    
    ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));
    ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));

    // MAC config
    eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
    mac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO;
    mac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO;


    //PHY 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;
    
    
    //vTaskDelay(pdMS_TO_TICKS(10));
   
    esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
    esp_eth_phy_t *phy = esp_eth_phy_new_lan8720(&phy_config);

    esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
    esp_eth_handle_t eth_handle = NULL;
    
    ESP_ERROR_CHECK(esp_eth_driver_install(&config, &eth_handle));
    ESP_ERROR_CHECK(esp_eth_start(eth_handle));
}
However, when I try to switch to the new esp_netif.h header (from tcpip_adapter.h), and replace the green lines by the red ones, I obtain the following error:

E (411) esp_netif_lwip: Cannot start esp_netif: Missing mandatory configuration:
esp_netif->driver_transmit: 0x0, esp_netif->driver_handle:0x0, esp_netif->lwip_input_fn: 0x400ecac0, esp_netif->lwip_init_fn:0x400ecb14
ESP_ERROR_CHECK failed: esp_err_t 0x103 (ESP_ERR_INVALID_STATE) at 0x40083fb0


Obviously, I would like to use the newer solution, as the other one is deprecated. Any ideas how to fix this?


tomfrance
Posts: 12
Joined: Wed Mar 17, 2021 3:50 pm

Re: Olimex ESP32 Gateway: Ethernet issues

Postby tomfrance » Wed Mar 17, 2021 10:36 pm

Hi Wifive!

Thanks for your comment.

I added the following line right after:

Code: Select all

esp_netif_t *eth_netif = esp_netif_new(&cfg);
ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(NULL)));
but it still fails with the following error:

E (515) esp_netif_lwip: Cannot start esp_netif: Missing mandatory configuration:
esp_netif->driver_transmit: 0x400db6a8, esp_netif->driver_handle:0x0, esp_netif->lwip_input_fn: 0x400f3218, esp_netif->lwip_init_fn:0x400f326c
0x400db6a8: esp_eth_transmit at /home/fst/Dev/esp/esp-idf/components/esp_eth/src/esp_eth.c:322

0x400f3218: ethernetif_input at /home/fst/Dev/esp/esp-idf/components/lwip/port/esp32/netif/ethernetif.c:156

0x400f326c: ethernetif_init at /home/fst/Dev/esp/esp-idf/components/lwip/port/esp32/netif/ethernetif.c:196


Actually I am wondering: Why do the lines in the referenced example work, before the resepctive fields (driver_transmit and driver_handle of esp_netif) are populated by esp_netif_attch()?

Code: Select all

esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
    esp_netif_t *eth_netif = esp_netif_new(&cfg);
    // Set default handlers to process TCP/IP stuffs
    ESP_ERROR_CHECK(esp_eth_set_default_handlers(eth_netif));

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Olimex ESP32 Gateway: Ethernet issues

Postby WiFive » Thu Mar 18, 2021 7:19 pm

Code: Select all

ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(NULL)));
Why would you pass null pointer?

tomfrance
Posts: 12
Joined: Wed Mar 17, 2021 3:50 pm

Re: Olimex ESP32 Gateway: Ethernet issues

Postby tomfrance » Fri Mar 19, 2021 4:40 pm

Thank you!

That was by mistake - I actually used the referenced example from the git repository before, but it wouldnt work out of the box. Reason was that I also needed to change the config options:

CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=-1 (disable PHY HW reset)
CONFIG_EXAMPLE_ETH_PHY_ADDR=0

Who is online

Users browsing this forum: Majestic-12 [Bot] and 118 guests