[Solved, this code works!] ESP32-S3 + W5500 Ethernet no link up

ajosev5
Posts: 5
Joined: Wed Mar 29, 2023 2:38 am

[Solved, this code works!] ESP32-S3 + W5500 Ethernet no link up

Postby ajosev5 » Wed Nov 08, 2023 9:01 pm

Thank you, this is solved. There is a yet undetermined fault in the hardware design on our custom PCB. Connecting via SPI to an external W5500 ethernet module works - Link Up, IP address acquired, and ping success. A good lesson to test software with modules first.

Hello, I'd like to check if anyone can find something incorrect about the code I am using to initialize the ethernet driver with a W5500 SPI/Ethernet chip, on my custom board.

Hardware: ESP32-WROOM-S3 + Wiznet W5500
IDF version 5.0.3

At this point, I am able to confirm communication via SPI to the W5500, through the IDF driver. I am receiving a version 4 back in w5500_verify_id(), and I also wrote a bit of code to verify I am reading the MAC that I wrote to the chip on initialization - w5500_read(emac, W5500_REG_MAC, macRead, 6). However, I am not getting a Link Up event from the driver when I plug it into a router, or any other device. No activity LEDs. Want to make sure, as I study the hardware design, that I'm not missing any "gotchas" in how I am initializing the device. Below is my full code having to do with initializing ethernet.

Code: Select all

static void EthernetHardwareEventHandler(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(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;
    }
}

static void EthernetIpEventHandler(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_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, "~~~~~~~~~~~");
    memcpy(&myEthIP, &event->ip_info, sizeof(myEthIP));
}

void ethernet_init(void)
{
    ESP_ERROR_CHECK(esp_netif_init());

    //  Create instance(s) of esp-netif for SPI Ethernet(s)
    esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
    esp_netif_config_t cfg_spi = {
        .base = &esp_netif_config,
        .stack = ESP_NETIF_NETSTACK_DEFAULT_ETH
    };
    esp_netif_t* eth_netif_spi = NULL;
    char if_key_str[10];
    char if_desc_str[10];
    char num_str[3];
    itoa(0, num_str, 10);
    strcat(strcpy(if_key_str, "ETH_SPI_"), num_str);
    strcat(strcpy(if_desc_str, "eth"), num_str);
    esp_netif_config.if_key = if_key_str;
    esp_netif_config.if_desc = if_desc_str;
    esp_netif_config.route_prio = 30;
    eth_netif_spi = esp_netif_new(&cfg_spi);

    // Init MAC and PHY configs to default
    eth_mac_config_t mac_config_spi = ETH_MAC_DEFAULT_CONFIG();

    eth_phy_config_t phy_config_spi = ETH_PHY_DEFAULT_CONFIG();
    phy_config_spi.autonego_timeout_ms = 0;
    phy_config_spi.reset_gpio_num = -1;

    // Init SPI bus
    spi_bus_config_t buscfg = {
        .miso_io_num = ETHERNET_SPI_MOSI_GPIO,
        .mosi_io_num = ETHERNET_SPI_MISO_GPIO,
        .sclk_io_num = ETHERNET_SPI_CLK_GPIO,
        .quadwp_io_num = -1,
        .quadhd_io_num = -1,
    };
    ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));

    // Configure SPI interface and Ethernet driver for specific SPI module
    esp_eth_mac_t* mac_spi;
    esp_eth_phy_t* phy_spi;
    esp_eth_handle_t eth_handle_spi = NULL;
    spi_device_interface_config_t spi_devcfg = {
        .command_bits = 16, // Actually it's the address phase in W5500 SPI frame
        .address_bits = 8,  // Actually it's the control phase in W5500 SPI frame
        .spics_io_num = ETHERNET_SPI_CS_GPIO,
        .mode = 0,
        .clock_speed_hz = 33 * 1000 * 1000,
        .queue_size = 20
    };

    // Set remaining GPIO numbers and configuration used by the SPI module
    phy_config_spi.phy_addr = 1;

    eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(SPI3_HOST, &spi_devcfg);
    w5500_config.int_gpio_num = ETHERNET_SPI_INT_GPIO;
    mac_spi = esp_eth_mac_new_w5500(&w5500_config, &mac_config_spi);
    phy_spi = esp_eth_phy_new_w5500(&phy_config_spi);

    esp_eth_config_t eth_config_spi = ETH_DEFAULT_CONFIG(mac_spi, phy_spi);
    ESP_ERROR_CHECK(esp_eth_driver_install(&eth_config_spi, &eth_handle_spi));

    uint8_t mac_addr[6];
    esp_read_mac(mac_addr, ESP_MAC_ETH);

    ESP_ERROR_CHECK(esp_eth_ioctl(eth_handle_spi, ETH_CMD_S_MAC_ADDR, mac_addr));

    // attach Ethernet driver to TCP/IP stack
    ESP_ERROR_CHECK(esp_netif_attach(eth_netif_spi, esp_eth_new_netif_glue(eth_handle_spi)));

    // Register user defined event handers
    ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &EthernetHardwareEventHandler, NULL));
    ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &EthernetIpEventHandler, NULL));

    /* start Ethernet driver state machine */
    ESP_ERROR_CHECK(esp_eth_start(eth_handle_spi));
    
}
I am calling ethernet_init(), and am getting ETHERNET_EVENT_START through EthernetHardwareEventHandler() after I call init. However, as mentioned, the problem is I am not getting a Link Up status from the chip (ETHERNET_EVENT_CONNECTED) when I plug it into something. For the initialization code, I basically referenced the IDF guide page and ethernet example. I would be happy to hear any suggestions, things to try, or flaws in my initialization. Thank you very much!

-Tony
Last edited by ajosev5 on Fri Nov 10, 2023 8:39 pm, edited 2 times in total.

username
Posts: 538
Joined: Thu May 03, 2018 1:18 pm

Re: ESP32-S3 + W5500 Ethernet no link up

Postby username » Thu Nov 09, 2023 3:19 am

Did you first try any of the W5500 SPI boards out there first ?
https://www.amazon.com/s?k=w5500+ethern ... ef=sr_pg_1

ajosev5
Posts: 5
Joined: Wed Mar 29, 2023 2:38 am

Re: ESP32-S3 + W5500 Ethernet no link up

Postby ajosev5 » Thu Nov 09, 2023 7:24 pm

username wrote:
Thu Nov 09, 2023 3:19 am
Did you first try any of the W5500 SPI boards out there first ?
https://www.amazon.com/s?k=w5500+ethern ... ef=sr_pg_1
No, but I did order a couple today to try. Good suggestion, and probably what I should have done first. I will report back results.

Who is online

Users browsing this forum: No registered users and 188 guests