EMAC TX buffer runs full when no connection is established "emac_esp32_transmit(): insufficient TX buffer size"

DSchmid
Posts: 1
Joined: Tue Apr 04, 2023 6:49 am

EMAC TX buffer runs full when no connection is established "emac_esp32_transmit(): insufficient TX buffer size"

Postby DSchmid » Tue Apr 04, 2023 7:14 am

Hello everyone,

I ran into a small problem. I'm currently working on an application using the ESP Ethernet MAC. Everything works as expected.
The problem starts when no Ethernet cable is connected. After some minutes, I get the error message "emac_esp32_transmit(): insufficient TX buffer size". It seems like the IP-Stack tries sending packets without an established link. So the packets are queued until the TX buffer is full.
Normally, I would expect that packets are first transmitted after a Link-Up-Event. With of curse hasn't occurred when no cable is connected.

Is there a way to prevent queuing packets in the TX buffer before the link is established?

P.S. im using IDF Version: 5.0.1

Here is my code:

Code: Select all

/**
 * @brief Event handler for Ethernet events, stop HTTPD server on disconnect event
 */
static void eth_event_handler(void *arg, esp_event_base_t event_base,
                              int32_t event_id, void *event_data)
{
    //httpd_handle_t* server = (httpd_handle_t*) arg;
    
    switch (event_id) {
    case ETHERNET_EVENT_CONNECTED:
        ESP_LOGI(TAG, "Ethernet Link Up");
        break;
    case ETHERNET_EVENT_DISCONNECTED:
        ESP_LOGI(TAG, "Ethernet Link Down");

        if (server) {
            ESP_LOGI(TAG, "Stopping webserver");
            if (http_stop_server() == ESP_OK) {
                server = NULL;
            } else {
                ESP_LOGE(TAG, "Failed to stop http server");
            }
        }
        break;
    case ETHERNET_EVENT_START:
        ESP_LOGI(TAG, "Ethernet Started");
        break;
    case ETHERNET_EVENT_STOP:
        ESP_LOGI(TAG, "Ethernet Stopped");
        break;
    default:
        break;
    }
}

/**
 * @brief Event handler for IP_EVENT_ETH_GOT_IP, start HTTPD server
 */
static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
                                 int32_t event_id, void *event_data)
{
    if (server == NULL) {
        ESP_LOGI(TAG, "Starting webserver");
        if(http_start_server() == ESP_OK)
        {
            register_uris();
        }
    }
}

esp_err_t mdns_init_internal(char* hostname)
{
    ESP_RETURN_ON_ERROR(mdns_init(), TAG, "initialize mDNS server failed");
    ESP_RETURN_ON_ERROR(mdns_hostname_set(hostname), TAG, "setting hostname failed");
    ESP_RETURN_ON_ERROR(mdns_instance_name_set(hostname), TAG, "setting instance name failed");

    return ESP_OK;
}

/**
 * @brief Internal ESP32 Ethernet initialization
 *
 * @return
 *          - esp_eth_handle_t if init succeeded
 *          - NULL if init failed
 */
static esp_eth_handle_t eth_init_internal(void)
{
    esp_eth_handle_t ret = NULL;

    // Init common MAC and PHY configs to default
    eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
    eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();

    // Update PHY config based on board specific configuration
    phy_config.phy_addr = CONFIG_ETH_PHY_ADDR;
    phy_config.reset_gpio_num = -1;
    // Init vendor specific MAC config to default
    eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
    // Update vendor specific MAC config based on board configuration
    esp32_emac_config.smi_mdc_gpio_num = CONFIG_ETH_MDC_GPIO;
    esp32_emac_config.smi_mdio_gpio_num = CONFIG_ETH_MDIO_GPIO;
    // Create new ESP32 Ethernet MAC instance
    esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
    // Create new PHY instance based on board configuration
    esp_eth_phy_t *phy = esp_eth_phy_new_ksz8081(&phy_config);

    // Init Ethernet driver to default and install it
    esp_eth_handle_t eth_handle = NULL;
    esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
    ESP_GOTO_ON_FALSE(esp_eth_driver_install(&config, &eth_handle) == ESP_OK, NULL, err, TAG, "Ethernet driver install failed");

    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;
}

/**
 * @brief Ethernet and HTTPD server initialization
 *
 * @return
 *          - ESP_OK if init succeeded
 *          - ESP_FAIL if not
 */
esp_err_t eth_init(char* hostname)
{
    gpio_reset_pin(CONFIG_ETH_PHY_RST_GPIO);
    gpio_set_direction(CONFIG_ETH_PHY_RST_GPIO, GPIO_MODE_OUTPUT);
    gpio_set_level(CONFIG_ETH_PHY_RST_GPIO, 1);                         // Disable PHY reset, enable 50 MHz oszillator
    vTaskDelay(10 / portTICK_PERIOD_MS);                                // Wait 10 ms for PHY to start up

    // Initialize Ethernet driver
    esp_eth_handle_t eth_handle = eth_init_internal();
    ESP_RETURN_ON_FALSE(eth_handle, ESP_FAIL, TAG, "internal Ethernet init failed");

    // Initialize TCP/IP network interface aka the esp-netif (should be called only once in application)
    ESP_RETURN_ON_ERROR(esp_netif_init(), TAG, "TCP/IP network interface failed");
 
    // Initialize mDNS server
    ESP_RETURN_ON_ERROR(mdns_init_internal(hostname), TAG, "initialize mDNS server failed");

    // Create instance of esp-netif for Ethernet with default esp-netif configuration parameters.
    esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
    esp_netif_t *eth_netif = esp_netif_new(&cfg);
    // Set device hostname for DHCP
    ESP_RETURN_ON_ERROR(esp_netif_set_hostname(eth_netif, hostname), TAG, "setting hostname failed");
    // Attach Ethernet driver to TCP/IP stack
    ESP_RETURN_ON_ERROR(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle)), TAG, "attaching Ethernet driver to ip stack failed");

    // Register user defined event handers
    ESP_RETURN_ON_ERROR(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, &server), TAG, "creating Ethernet event handler failed");
    ESP_RETURN_ON_ERROR(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, &server), TAG, "creating IP event handler failed");

    // Start Ethernet driver state machine
    ESP_RETURN_ON_ERROR(esp_eth_start(eth_handle), TAG, "starting ethernet state machine failed");

    return ESP_OK;
}

ESP_ondrej
Posts: 210
Joined: Fri May 07, 2021 10:35 am

Re: EMAC TX buffer runs full when no connection is established "emac_esp32_transmit(): insufficient TX buffer size"

Postby ESP_ondrej » Wed Apr 05, 2023 1:53 pm

Hi @DSchmid, this is known issue, please see https://github.com/espressif/esp-idf/issues/10851. It already has been fixed at master branch. Now, it is waiting to be backported to v5.0.

Who is online

Users browsing this forum: craige and 96 guests