dhcp client start failed

alitergee
Posts: 14
Joined: Wed Aug 17, 2022 1:31 pm

dhcp client start failed

Postby alitergee » Thu Jul 13, 2023 7:30 am

I'm using enc28j60 IC for ethernet and ESP fails to start DHCP client resulting in no IP_EVENT_ETH_GOT_IP event.
I'm not being able to find the root cause of this and how to fix it.
Following are the serial logs and ethernet initialization function.

Serial Logs
I (2864) enc28j60: revision: 6
I (2874) esp_eth.netif.netif_glue: 8c:4b:14:7a:3e:64
I (2874) esp_eth.netif.netif_glue: ethernet attached to netif
I (2874) enc28j60: working in 10Mbps
I (2884) enc28j60: working in full duplex
I (2884) ETHERNET: Ethernet Link Up
W (2894) ETHERNET: [ ETHERNET TRANSMISSION MODE ] : [ FULL DUPLEX ]
I (2894) ETHERNET: Ethernet HW Addr 8c:4b:14:7a:3e:64
I (2904) ETHERNET: [ ETHERNET TRANSMISSION MODE SET TO FULL DUPLEX ]
E (2904) esp_netif_lwip: dhcp client start failed
I (2914) ETHERNET: Ethernet Started
I (2924) ETHERNET: ETHERNET Started successfully
W (12924) ETHERNET: DHCPC (state=0)
I (12924) ETHERNET: DHCP client status : ESP_OK

Ethernet init

Code: Select all

   
    ethernet_events = xEventGroupCreate();
    esp_netif_init();
    esp_event_loop_create_default();
    // Register user defined event handers
    ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, event_handler_for_ethernet, NULL));
    ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, event_handler_for_ethernet, NULL));

    esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH();
    eth_netif = esp_netif_new(&netif_cfg);

    spi_bus_config_t buscfg = {
        .miso_io_num = ETH_SPI_MISO,
        .mosi_io_num = ETH_SPI_MOSI,
        .sclk_io_num = ETH_SPI_SCK,
        .quadwp_io_num = -1,
        .quadhd_io_num = -1,
    };
    ESP_ERROR_CHECK(spi_bus_initialize(ETH_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO));
    /* ENC28J60 ethernet driver is based on spi driver */
    spi_device_interface_config_t devcfg = {
        .command_bits = 3,
        .address_bits = 5,
        .mode = 0,
        .clock_speed_hz = ETH_SPI_CLOCK_MHZ * 1000 * 1000,
        .spics_io_num = ETH_CS_GPIO,
        .queue_size = 20,
        .cs_ena_posttrans = enc28j60_cal_spi_cs_hold_time(ETH_SPI_CLOCK_MHZ),
    };

    spi_device_handle_t spi_handle = NULL;
    ESP_ERROR_CHECK(spi_bus_add_device(ETH_SPI_HOST, &devcfg, &spi_handle));

    eth_enc28j60_config_t enc28j60_config = ETH_ENC28J60_DEFAULT_CONFIG(spi_handle);
    enc28j60_config.int_gpio_num = ETH_INT_GPIO;

    eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
    mac_config.smi_mdc_gpio_num = -1; // ENC28J60 doesn't have SMI interface
    mac_config.smi_mdio_gpio_num = -1;
    esp_eth_mac_t *mac = esp_eth_mac_new_enc28j60(&enc28j60_config, &mac_config);

    eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
    phy_config.autonego_timeout_ms = 0; // ENC28J60 doesn't support auto-negotiation
    phy_config.reset_gpio_num = -1;     // ENC28J60 doesn't have a pin to reset internal PHY
    esp_eth_phy_t *phy = esp_eth_phy_new_enc28j60(&phy_config);

    esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
    esp_eth_handle_t eth_handle = NULL;
    ESP_ERROR_CHECK(esp_eth_driver_install(&eth_config, &eth_handle));

    /* ENC28J60 doesn't burn any factory MAC address, we need to set it manually.
       02:00:00 is a Locally Administered OUI range so should not be used except when testing on a LAN under your control.
    */

    mac->set_addr(mac, (uint8_t[]){
                           mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]});

    // ENC28J60 Errata #1 checkETH_SPI_CLOCK_MHZ
    if (emac_enc28j60_get_chip_info(mac) < ENC28J60_REV_B5 && ETH_SPI_CLOCK_MHZ < 8)
    {
        ESP_LOGE(TAG, "SPI frequency must be at least 8 MHz for chip revision less than 5");
        ESP_ERROR_CHECK(ESP_FAIL);
    }
    // Stop DHCP --------------------------------------------------------------------------------------------------

    /* attach Ethernet driver to TCP/IP stack */
    ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle)));

    /* It is recommended to use ENC28J60 in Full Duplex mode since multiple errata exist to the Half Duplex mode */
    // if CONFIG_EXAMPLE_ENC28J60_DUPLEX_FULL
    /* Set duplex needs to be called after esp_eth_start since the driver is started with auto-negotiation by default */
    err = enc28j60_set_phy_duplex(phy, ETH_DUPLEX_FULL);

    if (err == ESP_OK)
    {
        ESP_LOGI(TAG, "[ ETHERNET TRANSMISSION MODE SET TO FULL DUPLEX ]");
    }

    // #endif
    /* start Ethernet driver state machine */
    err = esp_eth_start(eth_handle);

    if (err == ESP_OK)
        ESP_LOGI(TAG, "ETHERNET Started successfully");
    else
        ESP_LOGE(TAG, "ETHERNET Start failed");

    xEventGroupWaitBits(ethernet_events, CONNECTED_GOT_IP | DISCONNECTED, pdTRUE, pdFALSE, pdMS_TO_TICKS(10000));

    esp_netif_dhcp_status_t status;
    esp_netif_dhcpc_get_status(eth_netif, &status);
    ESP_LOGW(TAG, " DHCPC  (state=%u)", status);
    if (status == 0x0)
    {
        esp_err_t err_dhcp = esp_netif_dhcpc_start(eth_netif);
        ESP_LOGI(TAG, "DHCP client status : %s", esp_err_to_name(err_dhcp));
    }
  
NOTE: I also start Wifi in AP mode at the time of ethernet init.

Please help out.

Thank you.
Last edited by alitergee on Thu Jul 13, 2023 7:56 am, edited 1 time in total.

bidrohini
Posts: 202
Joined: Thu Oct 27, 2022 12:55 pm

Re: dhcp client start failed

Postby bidrohini » Thu Jul 13, 2023 7:55 am

Which library are you using? This one? https://github.com/tobozo/ESP32-ENC28J60
Here is also a hacksterIO tutorial. It may give you some clues: https://www.hackster.io/mvtdesign/wifi- ... ule-8c0250

alitergee
Posts: 14
Joined: Wed Aug 17, 2022 1:31 pm

Re: dhcp client start failed

Postby alitergee » Thu Jul 13, 2023 8:00 am

bidrohini wrote:
Thu Jul 13, 2023 7:55 am
Which library are you using? This one? https://github.com/tobozo/ESP32-ENC28J60
Here is also a hacksterIO tutorial. It may give you some clues: https://www.hackster.io/mvtdesign/wifi- ... ule-8c0250
I'm using en28j60 example code provided by esp-idf.

bidrohini
Posts: 202
Joined: Thu Oct 27, 2022 12:55 pm

Re: dhcp client start failed

Postby bidrohini » Fri Jul 14, 2023 12:28 pm

If the DHCP client is timing out too quickly, you can try increasing the timeout value to allow more time for the DHCP negotiation. Modify the line esp_netif_dhcpc_start(eth_netif) to include a longer timeout value, for example:

Code: Select all

esp_netif_dhcpc_start(eth_netif, pdMS_TO_TICKS(10000)); // Increase timeout to 10 seconds
Also, make sure that a DHCP server is available on the network and capable of assigning IP addresses. You can test this by connecting another device (e.g., a computer or smartphone) to the same network and verifying that it receives an IP address through DHCP.

alitergee
Posts: 14
Joined: Wed Aug 17, 2022 1:31 pm

Re: dhcp client start failed

Postby alitergee » Fri Jul 14, 2023 1:15 pm

Hi,
Thanks for the reply.

According to the esp-idf docs,

Code: Select all

esp_netif_dhcpc_start()

this function is called from the events SYSTEM_EVENT_STA_CONNECTED (for wifi) and SYSTEM_EVENT_ETH_CONNECTED (for ethernet) of the event handler.

In my flow, I'm verifying whether the dhcp client has started or not, and if it is not started calling esp_netif_dhcpc_start() which is a workaround and not a proper solution in my opinion. The dhcp client must not fail to start anytime the system is booted. I'm trying to find the root cause for this.

Yet, I'll try doing the changes you've suggested in the code and test it out.

Thanks again.

Who is online

Users browsing this forum: letter57 and 199 guests