From c4e63ace661df9748cfae451bde7781e61e0b3d9 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Tue, 17 Oct 2023 21:48:18 +0200 Subject: [PATCH] esp32/network_lan: Fix and simplify the code for ETH-SPI devices. SPI support was not enabled, and was not adapted for esp-idf v5.x. This change enables SPI ethernet for all boards and adapts the code for esp-idf v5.x. The change follows the sample implementation of @hemakumarm72, but adds the changes for the other adapters as well. Further, it simplifies the code by removing actions from netwwork_lan.c which are done in the esp-idf drivers later, like setting the default values for .command_bits and .address_bits, and registering the SPI interface. Tested with a Wiznet W5500 breakout. Signed-off-by: robert-hh --- ports/esp32/boards/sdkconfig.base | 5 ++++ ports/esp32/network_lan.c | 47 +++++++++---------------------- 2 files changed, 19 insertions(+), 33 deletions(-) diff --git a/ports/esp32/boards/sdkconfig.base b/ports/esp32/boards/sdkconfig.base index 5bd9b1aa6c..372633e711 100644 --- a/ports/esp32/boards/sdkconfig.base +++ b/ports/esp32/boards/sdkconfig.base @@ -91,3 +91,8 @@ CONFIG_UART_ISR_IN_IRAM=y CONFIG_ADC_SUPPRESS_DEPRECATE_WARN=y CONFIG_RMT_SUPPRESS_DEPRECATE_WARN=y CONFIG_I2S_SUPPRESS_DEPRECATE_WARN=y + +CONFIG_ETH_USE_SPI_ETHERNET=y +CONFIG_ETH_SPI_ETHERNET_W5500=y +CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL=y +CONFIG_ETH_SPI_ETHERNET_DM9051=y diff --git a/ports/esp32/network_lan.c b/ports/esp32/network_lan.c index 8557700446..0e42f403c5 100644 --- a/ports/esp32/network_lan.c +++ b/ports/esp32/network_lan.c @@ -180,37 +180,15 @@ STATIC mp_obj_t get_lan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar phy_config.phy_addr = self->phy_addr; phy_config.reset_gpio_num = self->phy_power_pin; self->phy = NULL; - #if CONFIG_ETH_USE_SPI_ETHERNET - spi_device_handle_t spi_handle = NULL; - if (IS_SPI_PHY(args[ARG_phy_type].u_int)) { - spi_device_interface_config_t devcfg = { - .mode = 0, - .clock_speed_hz = MICROPY_PY_NETWORK_LAN_SPI_CLOCK_SPEED_MZ * 1000 * 1000, - .queue_size = 20, - .spics_io_num = self->phy_cs_pin, - }; - switch (args[ARG_phy_type].u_int) { - #if CONFIG_ETH_SPI_ETHERNET_DM9051 - case PHY_DM9051: { - devcfg.command_bits = 1; - devcfg.address_bits = 7; - break; - } - #endif - #if CONFIG_ETH_SPI_ETHERNET_W5500 - case PHY_W5500: { - devcfg.command_bits = 16; - devcfg.address_bits = 8; - break; - } - #endif - } - spi_host_device_t host = machine_hw_spi_get_host(args[ARG_spi].u_obj); - if (spi_bus_add_device(host, &devcfg, &spi_handle) != ESP_OK) { - mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("spi_bus_add_device failed")); - } - } + spi_device_interface_config_t devcfg = { + .mode = 0, + .clock_speed_hz = MICROPY_PY_NETWORK_LAN_SPI_CLOCK_SPEED_MZ * 1000 * 1000, + .queue_size = 20, + .spics_io_num = self->phy_cs_pin, + .command_bits = 0, // Can both be set to 0, as the respective + .address_bits = 0, // driver fills in proper default values. + }; #endif switch (args[ARG_phy_type].u_int) { @@ -236,7 +214,8 @@ STATIC mp_obj_t get_lan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar #if CONFIG_ETH_USE_SPI_ETHERNET #if CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL case PHY_KSZ8851SNL: { - eth_ksz8851snl_config_t chip_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(spi_handle); + spi_host_device_t host = machine_hw_spi_get_host(args[ARG_spi].u_obj); + eth_ksz8851snl_config_t chip_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(host, &devcfg); chip_config.int_gpio_num = self->phy_int_pin; mac = esp_eth_mac_new_ksz8851snl(&chip_config, &mac_config); self->phy = esp_eth_phy_new_ksz8851snl(&phy_config); @@ -245,7 +224,8 @@ STATIC mp_obj_t get_lan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar #endif #if CONFIG_ETH_SPI_ETHERNET_DM9051 case PHY_DM9051: { - eth_dm9051_config_t chip_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle); + spi_host_device_t host = machine_hw_spi_get_host(args[ARG_spi].u_obj); + eth_dm9051_config_t chip_config = ETH_DM9051_DEFAULT_CONFIG(host, &devcfg); chip_config.int_gpio_num = self->phy_int_pin; mac = esp_eth_mac_new_dm9051(&chip_config, &mac_config); self->phy = esp_eth_phy_new_dm9051(&phy_config); @@ -254,7 +234,8 @@ STATIC mp_obj_t get_lan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar #endif #if CONFIG_ETH_SPI_ETHERNET_W5500 case PHY_W5500: { - eth_w5500_config_t chip_config = ETH_W5500_DEFAULT_CONFIG(spi_handle); + spi_host_device_t host = machine_hw_spi_get_host(args[ARG_spi].u_obj); + eth_w5500_config_t chip_config = ETH_W5500_DEFAULT_CONFIG(host, &devcfg); chip_config.int_gpio_num = self->phy_int_pin; mac = esp_eth_mac_new_w5500(&chip_config, &mac_config); self->phy = esp_eth_phy_new_w5500(&phy_config);