#include "bridge_eth.h" #include #include "driver/gpio.h" #include "driver/spi_master.h" #include "esp_eth.h" #include "esp_eth_mac_w5500.h" #include "esp_eth_phy_w5500.h" #include "esp_event.h" #include "esp_log.h" #include "esp_mac.h" #include "esp_netif.h" #include "sdkconfig.h" static const char *TAG = "bridge_eth"; static volatile bool s_up; static uint8_t s_ip[4]; static void on_got_ip(void *arg, esp_event_base_t base, int32_t id, void *data) { (void)arg; (void)base; (void)id; const ip_event_got_ip_t *e = data; s_up = true; esp_ip4_addr_t a = e->ip_info.ip; s_ip[0] = esp_ip4_addr1_16(&a); s_ip[1] = esp_ip4_addr2_16(&a); s_ip[2] = esp_ip4_addr3_16(&a); s_ip[3] = esp_ip4_addr4_16(&a); ESP_LOGI(TAG, "Ethernet address " IPSTR "/" IPSTR, IP2STR(&e->ip_info.ip), IP2STR(&e->ip_info.netmask)); } static void on_eth_down(void *arg, esp_event_base_t base, int32_t id, void *data) { (void)arg; (void)base; (void)id; (void)data; s_up = false; } bool bridge_eth_up(void) { return s_up; } bool bridge_eth_ip(uint8_t ip[4]) { if (!s_up) { return false; } memcpy(ip, s_ip, 4); return true; } esp_err_t bridge_eth_start(void) { // The INT pin uses a GPIO interrupt; the service may already be installed by another driver esp_err_t isr = gpio_install_isr_service(0); if (isr != ESP_OK && isr != ESP_ERR_INVALID_STATE) { ESP_LOGE(TAG, "GPIO ISR service: %s", esp_err_to_name(isr)); return ESP_FAIL; } const spi_host_device_t host = SPI2_HOST; spi_bus_config_t buscfg = { .miso_io_num = CONFIG_BRIDGE_ETH_MISO_GPIO, .mosi_io_num = CONFIG_BRIDGE_ETH_MOSI_GPIO, .sclk_io_num = CONFIG_BRIDGE_ETH_SCLK_GPIO, .quadwp_io_num = -1, .quadhd_io_num = -1, }; if (spi_bus_initialize(host, &buscfg, SPI_DMA_CH_AUTO) != ESP_OK) { ESP_LOGE(TAG, "SPI bus init failed"); return ESP_FAIL; } spi_device_interface_config_t devcfg = { .mode = 0, .clock_speed_hz = CONFIG_BRIDGE_ETH_SPI_CLOCK_MHZ * 1000 * 1000, .queue_size = 20, .spics_io_num = CONFIG_BRIDGE_ETH_CS_GPIO, }; eth_w5500_config_t wcfg = ETH_W5500_DEFAULT_CONFIG(host, &devcfg); wcfg.base.int_gpio_num = CONFIG_BRIDGE_ETH_INT_GPIO; if (CONFIG_BRIDGE_ETH_INT_GPIO < 0) { wcfg.base.poll_period_ms = 10; // no INT wire: poll the chip } eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); phy_config.reset_gpio_num = CONFIG_BRIDGE_ETH_RST_GPIO; esp_eth_mac_t *mac = esp_eth_mac_new_w5500(&wcfg, &mac_config); esp_eth_phy_t *phy = esp_eth_phy_new_w5500(&phy_config); if (!mac || !phy) { ESP_LOGE(TAG, "W5500 MAC/PHY create failed"); return ESP_FAIL; } esp_eth_config_t ecfg = ETH_DEFAULT_CONFIG(mac, phy); esp_eth_handle_t eth = NULL; if (esp_eth_driver_install(&ecfg, ð) != ESP_OK) { ESP_LOGE(TAG, "W5500 not found: check its wiring and power"); spi_bus_free(host); return ESP_FAIL; } // The W5500 has no factory address: use the board's Ethernet MAC from eFuse uint8_t eth_mac[6]; esp_read_mac(eth_mac, ESP_MAC_ETH); esp_eth_ioctl(eth, ETH_CMD_S_MAC_ADDR, eth_mac); esp_netif_config_t ncfg = ESP_NETIF_DEFAULT_ETH(); esp_netif_t *netif = esp_netif_new(&ncfg); if (!netif || esp_netif_attach(netif, esp_eth_new_netif_glue(eth)) != ESP_OK) { ESP_LOGE(TAG, "Ethernet netif failed"); return ESP_FAIL; } esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, on_got_ip, NULL); esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, on_eth_down, NULL); if (esp_eth_start(eth) != ESP_OK) { ESP_LOGE(TAG, "Ethernet start failed"); return ESP_FAIL; } ESP_LOGI(TAG, "W5500 on SPI, waiting for DHCP"); return ESP_OK; }