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(ð_config_spi, ð_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));
}
-Tony