Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP32 with W5500 #1

Open
maialbe2823 opened this issue Apr 28, 2023 · 8 comments
Open

ESP32 with W5500 #1

maialbe2823 opened this issue Apr 28, 2023 · 8 comments

Comments

@maialbe2823
Copy link

maialbe2823 commented Apr 28, 2023

Hi david, is there any way to have this configuration of the ethernet port be able to work for the W5500 eth? I have tried but not luck keep getting no ip. There is no much info out there for the config part of the W5500
https://github.com/david-cermak/esp-network-examples/blob/main/backnet-sever/main/eth_init.c

@david-cermak
Copy link
Owner

Hi @maialbe2823

You simply replace the config part and creation of mac and phy

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_EXAMPLE_ETH_PHY_ADDR;
phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO;
// 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_EXAMPLE_ETH_MDC_GPIO;
esp32_emac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_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_ip101(&phy_config);

with this code

    gpio_install_isr_service(0);
    spi_bus_config_t buscfg = {
        .miso_io_num = CONFIG_EXAMPLE_ETH_SPI_MISO_GPIO,
        .mosi_io_num = CONFIG_EXAMPLE_ETH_SPI_MOSI_GPIO,
        .sclk_io_num = CONFIG_EXAMPLE_ETH_SPI_SCLK_GPIO,
        .quadwp_io_num = -1,
        .quadhd_io_num = -1,
    };
    ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO));
    spi_device_interface_config_t spi_devcfg = {
        .mode = 0,
        .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
        .spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO,
        .queue_size = 20
    };
    /* w5500 ethernet driver is based on spi driver */
    eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg);
    w5500_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO;
    mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
    phy = esp_eth_phy_new_w5500(&phy_config);

    // Install Ethernet driver ...

(which also sets up the SPI bus)

see the docs here https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/network/esp_eth.html#spi-ethernet-module
(or a simplified version of the connection phase in IDF examples: https://github.com/espressif/esp-idf/blob/master/examples/common_components/protocol_examples_common/eth_connect.c)

@maialbe2823
Copy link
Author

Got it thanks. And which or how many gpios should I use?

@maialbe2823
Copy link
Author

Hi David, I tried you configuration however I am getting the following errors:
�[0;31mE (22666) w5500.mac: w5500_send_command(124): send command timeout�[0m
�[0;31mE (22666) w5500.mac: emac_w5500_start(280): issue OPEN command failed�[0m
�[0;31mE (22666) w5500.mac: emac_w5500_set_link(370): w5500 start failed�[0m
�[0;31mE (22666) esp_eth: eth_on_state_changed(131): ethernet mac set link failed�[0m
�[0;31mE (22676) w5500.phy: w5500_update_link_duplex_speed(88): change link failed�[0m
�[0;31mE (22686) w5500.phy: w5500_get_link(112): update link duplex speed failed�[0m
This is my setup config c file. Please could you take a look and see why I am getting those errors? I tried some things but they don't do any difference.
//
// Created by david on 11/15/22.
//

#include "network_init.h"
#include "esp_log.h"
#include "esp_event.h"
#include "nvs_flash.h"
#include "bip.h"
#include "esp_eth_driver.h"
#include "driver/gpio.h"
#include "esp_eth_netif_glue.h"
#include "esp_eth_mac.h"
#include "w5500.h"

#define TAG "bacnet-eth-init"

EventGroupHandle_t s_net_event_group;

//#define CONFIG_EXAMPLE_ETH_PHY_IP101 1
//#define CONFIG_EXAMPLE_ETH_MDC_GPIO 23
//#define CONFIG_EXAMPLE_ETH_MDIO_GPIO 18
//#define CONFIG_EXAMPLE_ETH_PHY_RST_GPIO 5
//#define CONFIG_EXAMPLE_ETH_PHY_ADDR 1

#define CONFIG_EXAMPLE_ETH_SPI_MISO_GPIO 19
#define CONFIG_EXAMPLE_ETH_SPI_MOSI_GPIO 23
#define CONFIG_EXAMPLE_ETH_SPI_SCLK_GPIO 18
#define CONFIG_EXAMPLE_ETH_SPI_HOST 1
#define CONFIG_EXAMPLE_ETH_SPI_CS_GPIO 5
#define CONFIG_EXAMPLE_ETH_SPI_INT_GPIO 4

#define CONFIG_EXAMPLE_ETH_SPI_SCLK_MHZ 20

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");
        xEventGroupClearBits(s_net_event_group, CONNECTED_BIT);
        bip_cleanup();
        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 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, "~~~~~~~~~~~");
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
if (xEventGroupGetBits(s_net_event_group) != CONNECTED_BIT) {
    xEventGroupSetBits(s_net_event_group, CONNECTED_BIT);    
}

}
/* tcpip & eth start */
void network_init(void)
{
// Initialize TCP/IP network interface aka the esp-netif (should be called only once in application)
ESP_ERROR_CHECK(esp_netif_init());
// Create default event loop that running in background
ESP_ERROR_CHECK(esp_event_loop_create_default());

ESP_LOGI(TAG, "ESP_ETH_INIT");
s_net_event_group = xEventGroupCreate();

eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();

gpio_install_isr_service(0);
spi_bus_config_t buscfg = {
    .miso_io_num = CONFIG_EXAMPLE_ETH_SPI_MISO_GPIO,
    .mosi_io_num = CONFIG_EXAMPLE_ETH_SPI_MOSI_GPIO,
    .sclk_io_num = CONFIG_EXAMPLE_ETH_SPI_SCLK_GPIO,
    .quadwp_io_num = -1,
    .quadhd_io_num = -1,
};
ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO));
spi_device_interface_config_t spi_devcfg = {
    .mode = 0,
    .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_SCLK_MHZ * 1000 * 1000,
    //.clock_speed_hz = CONFIG_EXAMPLE_W5000_SPI_CLOCK_MHZ  * 1000 * 1000,
    .spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO,
    .queue_size = 20
};
/* w5500 ethernet driver is based on spi driver */
eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg);
w5500_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO;
esp_eth_mac_t *mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
esp_eth_phy_t *phy = esp_eth_phy_new_w5500(&phy_config);

// Install Ethernet driver ...
esp_eth_handle_t eth_handle = NULL;
esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
if (esp_eth_driver_install(&config, &eth_handle) != ESP_OK) {
    ESP_LOGE(TAG, "Ethernet driver install failed");
    abort();
}
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
esp_netif_t *eth_netif = esp_netif_new(&cfg);
// Attach Ethernet driver to TCP/IP stack
ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle)));

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

// Start Ethernet driver state machine
ESP_ERROR_CHECK(esp_eth_start(eth_handle));

ESP_LOGI(TAG, "eth_init finished.");

/* Waiting until we get a connection */
xEventGroupWaitBits(s_net_event_group,
                    CONNECTED_BIT,
                    pdFALSE,
                    pdFALSE,
                    portMAX_DELAY);

}

esp_netif_t* get_netif(void)
{
return esp_netif_get_handle_from_ifkey("ETH_DEF");
}

Also, do I have to setup W5500 in esp-idf menuconfig or not?
Thank you.

@david-cermak
Copy link
Owner

I'd recommend starting with this example: https://github.com/espressif/esp-idf/tree/master/examples/ethernet/basic

You'd need to choose SPI Ethernet (W5500) in menuconfig and it will help you with assigning default pins based on your target device. Once you can successfully connect and get an IP for your board, you can switch to your project.

Looking at the logs, it seems that the SPI connection is not working. It's usually easier to start with something working by default, just to rule out bad wiring, HW, communication...

@maialbe2823
Copy link
Author

Everything working now David. Thanks so much. I had to use the defaults pins like you said.
One more thing David, do you know where can I find information on how to save data into ESP32 flash during runtime? Kind of like saving to eeprom. I haven't find yet much info about it.

@maialbe2823
Copy link
Author

Thank you. I'll try it out.

@maialbe2823
Copy link
Author

maialbe2823 commented May 13, 2023

Hi David,
I got the W5500 working now. Thanks so much.
Also, if I add another W5500 so the same SPI in order to accomplish kind of a Ethernet daisy chain, where do or how do I define the second W5500 pins in order to let him know as ESP-IDF config file only mentions one.
Thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants